Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UniqueConstraint not enforced in CompartmentalizedComponent #9

Closed
psalvy opened this issue Oct 30, 2014 · 5 comments
Closed

UniqueConstraint not enforced in CompartmentalizedComponent #9

psalvy opened this issue Oct 30, 2014 · 5 comments
Assignees
Labels

Comments

@psalvy
Copy link

psalvy commented Oct 30, 2014

Hi again,

I came across an issue while uploading several times my model, at which point I discovered that the UniqueConstraint of the rows in the table CompartmentalizedComponent are not enforced, despite its declaration:

class CompartmentalizedComponent(Base):
    __tablename__='compartmentalized_component'

    id = Column(Integer, Sequence('wids'), primary_key=True)
    component_id = Column(Integer, ForeignKey('component.id'), nullable=False)
    compartment_id = Column(Integer, ForeignKey('compartment.id'), nullable=False)
    UniqueConstraint('compartment_id', 'component_id')

And I also discovered that the upload method for compartmentalized components in model_loading.py:

    def loadCompartmentalizedComponent(self, modellist, session):
        for model in modellist:
            for metabolite in model.metabolites:
                identifier = session.query(Compartment).filter(Compartment.name == metabolite.id[-1:len(metabolite.id)]).first()
                m = session.query(Metabolite).filter(Metabolite.name == metabolite.id.split("_")[0]).first()
                #m = session.query(Metabolite).filter(Metabolite.kegg_id == metabolite.notes.get("KEGGID")[0]).first()
                object = Compartmentalized_Component(component_id = m.id, compartment_id = identifier.id)
                session.add(object)

does not check for prior existence of the said compartmentalized component, unlike how it's done with the metabolites. What happens next in loadModelCompartmentalizedComponent when calling

compartmentalized_component_query = session.query(Compartmentalized_Component).filter(Compartmentalized_Component.component_id == componentquery.id).filter(Compartmentalized_Component.compartment_id == compartmentquery.id).first()

is that the component is linked to one among many identical compartmentalized components (first() call). This gets worse when you run a query like

compartmentalized_components = make_dict(session.query(CompartmentalizedComponent).\
    join(ModelCompartmentalizedComponent, CompartmentalizedComponent.id == \
        ModelCompartmentalizedComponent.compartmentalized_component_id).\
    filter(ModelCompartmentalizedComponent.model_id == modelObject.id))

to get the list of compartmentalized components used in your model, and try to fetch from it an id equal to a ReactionMatrix.compartmentalized_component_id. It does not work because this last one points to another sibling of this compartmentalized component which likely not the one in your model.

I don't know if this was very clear, but to sum up there are two points:

  • UniqueConstraint does not seem to be enforced (I have no idea as to why)
  • loadCompartmentalizedComponents does not check if the compartmentalized component already exists in the database.

A simple fix for this one would be:

def loadCompartmentalizedComponent(self, modellist, session):
    for model in modellist:
        for metabolite in model.metabolites:
            metaboliteObject = session.query(Metabolite).\
                filter(Metabolite.name == metabolite.id[:-2]).first()
            compartmentObject = session.query(Compartment).\
                filter(Compartment.symbol == metabolite.id[-1]).first()
            compartmentalized_component_found = True if session.query(CompartmentalizedComponent).\
                    filter(CompartmentalizedComponent.component_id == component_query.id).\
                    filter(CompartmentalizedComponent.compartment_id == compartment_query.id)\

            if not session.query(CompartmentalizedComponent).\
                        filter(CompartmentalizedComponent.component_id == component_query.id).\
                        filter(CompartmentalizedComponent.compartment_id == compartment_query.id):                
                object = CompartmentalizedComponent(
                    component_id = metaboliteObject.id, 
                    compartment_id = compartmentObject.id)
                session.add(object)

I attached a printscreen as an illustration.
bug_report_ome_cc

@steve-federowicz
Copy link
Member

Awesome!! Thanks Pierre.

Assigning @jslu9 as this could be critical for BiGG and he has the closest familiarity with that code.

@jslu9 we could probably also use this to develop a test case to make sure we don't get similar issues in other areas of the code.

Thanks Pierre!

@psalvy
Copy link
Author

psalvy commented Nov 3, 2014

My pleasure ! I'm sorry, I did not know about this get_or_create function, thanks for showing it to me !
I'll keep you updated if I find new stuff.

Pierre

EDIT:
I think I kinow why the unique constraint is not enforced: it is not declared as a table_args, but placed 'naked' in the column declaration. As we are using the declarative mode of SQLAlchemy, the line

    UniqueConstraint('compartment_id', 'component_id')

should be replaced by:

    __table_args__ = (
        UniqueConstraint('compartment_id', 'component_id'),
        {})

EDIT2: This goes also for the classes:

Model
ModelReaction
ReactionMatrix

in models.py that have the same problem

@jslu9
Copy link
Contributor

jslu9 commented Nov 3, 2014

Ok, I'm looking into it. It's strange that the unique constraint is not
being enforced by sqlalchemy. I'm going to put the unique constraints into
the table args and see if it configures the table correctly that way.

On Mon, Nov 3, 2014 at 11:32 AM, Pierre SALVY notifications@github.com
wrote:

My pleasure ! I'm sorry, I did not know about this get_or_create function,
thanks for showing it to me !
I'll keep you updated if I find new stuff.

Pierre


Reply to this email directly or view it on GitHub
#9 (comment).

@psalvy
Copy link
Author

psalvy commented Nov 3, 2014

Also, in the declaration of the Model class, as follows:

class Model(Base):
    __tablename__='model'
    id = Column(Integer, Sequence('wids'), primary_key=True)
    bigg_id = Column(String)
    first_created = Column(DateTime)
    genome_id = Column(Integer, ForeignKey('genome.id'))
    genome = relationship('Genome', backref='model')
    UniqueConstraint('name', 'firstcreated')
    notes = Column(String)
    __table_args__ = (UniqueConstraint('bigg_id'),{})
    def __repr__(self):
        return "Model (#%d) %s %s" % (self.id, self.bigg_id, self.first_created)

I noticed a double declaration of the UniqueConstraint, one of which is invalid according to what I posted above, and I also noticed that they do not ask for the same uniqueness.

@zakandrewking
Copy link
Contributor

@jslu9 Is this still happening?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants