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.