From 3e85baf423ae913a390d2da873bfd3e9bf939a4a Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 11 Oct 2024 14:44:13 +0200 Subject: [PATCH] populated identifier table --- omnipath_metabo/schema/_main.py | 23 ++++++++++++++++++++--- omnipath_metabo/schema/_structure.py | 2 +- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/omnipath_metabo/schema/_main.py b/omnipath_metabo/schema/_main.py index 006316e..87aa4d8 100644 --- a/omnipath_metabo/schema/_main.py +++ b/omnipath_metabo/schema/_main.py @@ -1,3 +1,4 @@ +import collections from sqlalchemy import text, select from sqlalchemy.dialects.postgresql import insert @@ -86,9 +87,25 @@ def load(self): self.update_mol_column() select_str_ids = text('SELECT id, smiles FROM structures') - self.session.execute(select_str_ids) - - insert_ids = insert(_structure.Identifier).values( + strids = { + id[1]: id[0] + for id in self.session.execute(select_str_ids) + } + + select_res_ids = text('SELECT id, name FROM resources') + resid= { + id[1]: id[0] + for id in self.session.execute(select_res_ids) + } + resource_key = resid[self.resource.name] + insert_ids = insert(_structure.Identifier).values([ + {'identifier':id, 'structure_id': strids[smiles], 'resource_id': resource_key} + for smiles, _ids in ids.items() + for id in _ids + ]) + self.session.execute(insert_ids) + self.session.commit() + #self.indexer() diff --git a/omnipath_metabo/schema/_structure.py b/omnipath_metabo/schema/_structure.py index 008e889..fc8516c 100644 --- a/omnipath_metabo/schema/_structure.py +++ b/omnipath_metabo/schema/_structure.py @@ -34,7 +34,7 @@ class Identifier(Base): class Resource(Base): __tablename__ = 'resources' id = Column(Integer, primary_key=True) - name = Column(String) + name = Column(String, unique = True, nullable = False) identifier = relationship('Identifier', backref='resource')