|
| 1 | +"""SQLAlchemy definitions for the BioSQL database of biological items. |
| 2 | +
|
| 3 | +This provides a non-Seq-based interface to the BioSQL database through python. |
| 4 | +This is useful if you have non-seq items to put into BioSQL, and should also |
| 5 | +expose things not touched on with the Biopython BioSQL interface. Eventually |
| 6 | +this would be a good target for merging. |
| 7 | +
|
| 8 | +http://www.biosql.org/wiki/Main_Page |
| 9 | +
|
| 10 | +Useful URLs for declarative style: |
| 11 | + https://www.bitbucket.org/stephane/model2/src/tip/transifex/model.py |
| 12 | + http://www.sqlalchemy.org/docs/05/sqlalchemy_ext_declarative.html |
| 13 | +""" |
| 14 | + |
| 15 | +def _initialize(Base): |
| 16 | + from sqlalchemy.orm import relation, mapper, dynamic_loader |
| 17 | + from sqlalchemy import MetaData, Table, Column, ForeignKey, Sequence |
| 18 | + from sqlalchemy import String, Unicode, Integer, DateTime, Float |
| 19 | + |
| 20 | + # -- Standard BioSQL tables |
| 21 | + |
| 22 | + class Biodatabase(Base): |
| 23 | + """Entry point to BioSQL databases. |
| 24 | + """ |
| 25 | + __tablename__ = 'biodatabase' |
| 26 | + __table_args__ = {'mysql_engine':'InnoDB', 'autoload' : True} |
| 27 | + biodatabase_id = Column(Integer, primary_key = True) |
| 28 | + entries = relation("Bioentry", backref = "biodb") |
| 29 | + |
| 30 | + class Bioentry(Base): |
| 31 | + """The main bioentry object in BioSQL, containing a biological item. |
| 32 | + """ |
| 33 | + __tablename__ = 'bioentry' |
| 34 | + __table_args__ = {'mysql_engine':'InnoDB', 'autoload' : True} |
| 35 | + bioentry_id = Column(Integer, primary_key = True) |
| 36 | + biodatabase_id = Column(Integer, |
| 37 | + ForeignKey('biodatabase.biodatabase_id')) |
| 38 | + qualifiers = relation("BioentryQualifierValue", backref = "bioentry") |
| 39 | + parent_maps = relation("BioentryRelationship", primaryjoin = |
| 40 | + "Bioentry.bioentry_id == BioentryRelationship.object_bioentry_id", |
| 41 | + lazy="dynamic") |
| 42 | + child_maps = relation("BioentryRelationship", primaryjoin = |
| 43 | + "Bioentry.bioentry_id == BioentryRelationship.subject_bioentry_id", |
| 44 | + order_by = "BioentryRelationship.object_bioentry_id.asc()", |
| 45 | + lazy="dynamic") |
| 46 | + features = relation("SeqFeature", backref="bioentry") |
| 47 | + sequence = relation("Biosequence") |
| 48 | + |
| 49 | + class Biosequence(Base): |
| 50 | + """Represent a sequence attached to a bioentry. |
| 51 | + """ |
| 52 | + __tablename__ = 'biosequence' |
| 53 | + __table_args__ = {'mysql_engine':'InnoDB', 'autoload' : True} |
| 54 | + bioentry_id = Column(Integer, ForeignKey('bioentry.bioentry_id'), |
| 55 | + primary_key = True) |
| 56 | + |
| 57 | + class Ontology(Base): |
| 58 | + """Defined a high level dictionary of ontology key terms. |
| 59 | + """ |
| 60 | + __tablename__ = 'ontology' |
| 61 | + __table_args__ = {'mysql_engine':'InnoDB', 'autoload' : True} |
| 62 | + ontology_id = Column(Integer, primary_key = True) |
| 63 | + |
| 64 | + class Term(Base): |
| 65 | + """Explicitly describe terminology used in key/value pair relationships |
| 66 | + """ |
| 67 | + __tablename__ = 'term' |
| 68 | + __table_args__ = {'mysql_engine':'InnoDB', 'autoload' : True} |
| 69 | + term_id = Column(Integer, primary_key = True) |
| 70 | + ontology_id = Column(Integer, ForeignKey('ontology.ontology_id')) |
| 71 | + ontology = relation("Ontology", backref = "terms") |
| 72 | + |
| 73 | + class BioentryQualifierValue(Base): |
| 74 | + """A key/value annotation pair associated with a Bioentry. |
| 75 | + """ |
| 76 | + __tablename__ = 'bioentry_qualifier_value' |
| 77 | + __table_args__ = {'mysql_engine':'InnoDB', 'autoload' : True} |
| 78 | + bioentry_id = Column(Integer, |
| 79 | + ForeignKey('bioentry.bioentry_id'), primary_key = True) |
| 80 | + term_id = Column(Integer, ForeignKey('term.term_id'), primary_key = True) |
| 81 | + rank = Column(Integer, primary_key = True) |
| 82 | + term = relation("Term", lazy=True) |
| 83 | + |
| 84 | + class BioentryRelationship(Base): |
| 85 | + """Define a relationship between two bioentry objects. |
| 86 | + """ |
| 87 | + __tablename__ = 'bioentry_relationship' |
| 88 | + __table_args__ = {'mysql_engine':'InnoDB', 'autoload' : True} |
| 89 | + object_bioentry_id = Column(Integer, |
| 90 | + ForeignKey('bioentry.bioentry_id'), primary_key = True) |
| 91 | + subject_bioentry_id = Column(Integer, |
| 92 | + ForeignKey('bioentry.bioentry_id')) |
| 93 | + term_id = Column(Integer, ForeignKey('term.term_id'), |
| 94 | + primary_key = True) |
| 95 | + rank = Column(Integer, primary_key = True) |
| 96 | + |
| 97 | + term = relation("Term") |
| 98 | + parent = relation("Bioentry", primaryjoin = |
| 99 | + "Bioentry.bioentry_id == BioentryRelationship.object_bioentry_id") |
| 100 | + child = relation("Bioentry", primaryjoin = |
| 101 | + "Bioentry.bioentry_id == BioentryRelationship.subject_bioentry_id") |
| 102 | + |
| 103 | + seqfeature_dbxref_table = Table('seqfeature_dbxref', Base.metadata, |
| 104 | + Column('seqfeature_id', Integer, |
| 105 | + ForeignKey('seqfeature.seqfeature_id')), |
| 106 | + Column('dbxref_id', Integer, ForeignKey('dbxref.dbxref_id')), |
| 107 | + Column('rank', Integer)) |
| 108 | + |
| 109 | + class DBXref(Base): |
| 110 | + """Database cross reference. |
| 111 | + """ |
| 112 | + __tablename__ = 'dbxref' |
| 113 | + __table_args__ = {'mysql_engine':'InnoDB', 'autoload' : True} |
| 114 | + dbxref_id = Column(Integer, primary_key = True) |
| 115 | + |
| 116 | + class SeqFeature(Base): |
| 117 | + """Provide a feature connected to a bioentry. |
| 118 | + """ |
| 119 | + __tablename__ = 'seqfeature' |
| 120 | + __table_args__ = {'mysql_engine':'InnoDB', 'autoload' : True} |
| 121 | + seqfeature_id = Column(Integer, primary_key = True) |
| 122 | + bioentry_id = Column(Integer, ForeignKey('bioentry.bioentry_id')) |
| 123 | + type_term_id = Column(Integer, ForeignKey('term.term_id')) |
| 124 | + source_term_id = Column(Integer, ForeignKey('term.term_id')) |
| 125 | + type_term = relation("Term", primaryjoin = |
| 126 | + "SeqFeature.type_term_id == Term.term_id") |
| 127 | + source_term = relation("Term", primaryjoin = |
| 128 | + "SeqFeature.source_term_id == Term.term_id") |
| 129 | + qualifiers = relation("SeqFeatureQualifierValue") |
| 130 | + locations = relation("Location") |
| 131 | + dbxrefs = relation("DBXref", secondary=seqfeature_dbxref_table, |
| 132 | + order_by=seqfeature_dbxref_table.columns.rank) |
| 133 | + |
| 134 | + class SeqFeatureQualifierValue(Base): |
| 135 | + """A key/value annotation pair associated with a SeqFeature. |
| 136 | + """ |
| 137 | + __tablename__ = 'seqfeature_qualifier_value' |
| 138 | + __table_args__ = {'mysql_engine':'InnoDB', 'autoload' : True} |
| 139 | + seqfeature_id = Column(Integer, |
| 140 | + ForeignKey('seqfeature.seqfeature_id'), primary_key = True) |
| 141 | + term_id = Column(Integer, ForeignKey('term.term_id'), |
| 142 | + primary_key = True) |
| 143 | + rank = Column(Integer, primary_key = True) |
| 144 | + term = relation("Term", lazy=True) |
| 145 | + |
| 146 | + class Location(Base): |
| 147 | + """Describe a location on a biological sequence. |
| 148 | + """ |
| 149 | + __tablename__ = 'location' |
| 150 | + __table_args__ = {'mysql_engine':'InnoDB', 'autoload' : True} |
| 151 | + location_id = Column(Integer, primary_key = True) |
| 152 | + seqfeature_id = Column(Integer, ForeignKey('seqfeature.seqfeature_id')) |
| 153 | + dbxref_id = Column(Integer, ForeignKey('dbxref.dbxref_id')) |
| 154 | + qualifiers = relation("LocationQualifierValue") |
| 155 | + dbxref = relation("DBXref") |
| 156 | + |
| 157 | + class LocationQualifierValue(Base): |
| 158 | + """A key/value annotation pair associated with a Location. |
| 159 | + """ |
| 160 | + __tablename__ = 'location_qualifier_value' |
| 161 | + __table_args__ = {'mysql_engine':'InnoDB', 'autoload' : True} |
| 162 | + location_id = Column(Integer, |
| 163 | + ForeignKey('location.location_id'), primary_key = True) |
| 164 | + term_id = Column(Integer, ForeignKey('term.term_id'), |
| 165 | + primary_key = True) |
| 166 | + rank = Column(Integer, primary_key = True) |
| 167 | + term = relation("Term", lazy=True) |
| 168 | + |
| 169 | + # ugly assignment of classes to the top level for use |
| 170 | + globals()['Biodatabase'] = Biodatabase |
| 171 | + globals()['Bioentry'] = Bioentry |
| 172 | + globals()['Biosequence'] = Biosequence |
| 173 | + globals()['BioentryQualifierValue'] = BioentryQualifierValue |
| 174 | + globals()['Ontology'] = Ontology |
| 175 | + globals()['Term'] = Term |
| 176 | + globals()['BioentryRelationship'] = BioentryRelationship |
| 177 | + globals()['SeqFeatureQualifierValue'] = SeqFeatureQualifierValue |
| 178 | + globals()['SeqFeature'] = SeqFeature |
| 179 | + globals()['DBXref'] = DBXref |
| 180 | + globals()['LocationQualifierValue'] = LocationQualifierValue |
| 181 | + globals()['Location'] = Location |
0 commit comments