-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.py
225 lines (182 loc) · 6.84 KB
/
schema.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
from sqlalchemy import create_engine, Column, Integer, String, ForeignKey, Table, Text, inspect, DateTime, func, \
Numeric, Boolean
from sqlalchemy import Enum as EnumType
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship
# from sqlalchemy_imageattach.entity import Image, image_attachment
from enum import Enum
from sqlalchemy_fsm import FSMField, transition
Base = declarative_base()
# Basics
class EnumString(Enum):
def __str__(self):
return str(self.value)
class Id():
id = Column(Integer, primary_key=True)
class Physical(Id):
well_id = Column(Integer, ForeignKey('well.id'))
well = relationship("Well")
class Virtual(Id):
date_created = Column(DateTime(timezone=True), server_default=func.now())
genbank_file = Column(String) # TODO this is a file, so let's dump to str for now
name = Column(String) # Human readable name. Different than human readable id.
### Virutals
part_conditions = Table('part_conditions', Base.metadata,
Column('left_id', Integer, ForeignKey('conditions.id')),
Column('right_id', Integer, ForeignKey('parts.id'))
)
organism_conditions = Table('organism_conditions', Base.metadata,
Column('condition_id', Integer, ForeignKey('conditions.id')),
Column('organism_id', Integer, ForeignKey('organisms.id'))
)
class ConditionStates(EnumString):
temp_37 = 'Grow at 37c'
temp_30 = 'Grow at 30c'
media_2yt = 'Grow liquid cultures in 2YT'
agar_lb = 'Grow solid cultures on LB'
amp = 'Ampicillin or Carbenicillin resistant'
kan = 'Kanamycin resistant'
cam = 'Chloramphenicol resistant'
tet = 'Tetracycline resistant'
spc = 'Spectinomycin resistant'
class Condition(Base, Id):
__tablename__ = "conditions"
condition = Column(EnumType(ConditionStates))
part = relationship("Part",
secondary=part_conditions,
backref="conditions")
organism = relationship("Organism",
secondary=organism_conditions,
backref="conditions")
class PartState(EnumString):
unordered = 'unordered'
ordered = 'ordered'
synthesis_abandoned = 'synthesis_abandoned'
uncloned = 'uncloned'
building = 'building'
sequence_verified = 'sequence_verified'
class PartType(EnumString):
full_promoter = 'fg-full_promoter'
promoter = 'fg-promoter'
utr5 = 'fg-5utr'
cds = 'fg-cds'
tag = 'fg-tag'
terminator = 'fg-3utr'
operon = 'fg-operon'
vector = 'vector'
class ProvenanceType(EnumString):
synthetic = 'synthetic'
composite = 'composite'
precloned = 'precloned'
# Parts <-> Parts
parts_parts = Table('parts_parts', Base.metadata,
Column('part', ForeignKey('parts.id'), primary_key=True),
Column('provenance_part', ForeignKey('parts.id'), primary_key=True)
)
class Part(Base, Virtual):
__tablename__ = "parts"
hrid = Column(String)
seq = Column(String)
part_type = Column(EnumType(PartType))
part_status = Column(EnumType(PartType)) # TODO transitions here
# A part can have many parts
provenance_parts = relationship('Part',
secondary=parts_parts,
backref='progeny_parts')
provenance_type = Column(EnumType(ProvenanceType))
dna_samples = relationship('dna_samples', backref='parts')
wells = relationship("Well", back_populates="parts")
class Organism(Base, Virtual):
__tablename__ = "organisms"
name = Column(String)
wells = relationship("Well", back_populates="parts")
# Physicals
class SequenceState(EnumString):
not_sequenced = 'not_sequenced'
sequencing_requested = 'sequencing_requested'
sequence_confirmed = 'sequence_confirmed'
sequence_failed = 'sequence_failed'
mutation = 'mutation'
class DnaType(EnumString):
plasmid = 'plasmid'
fragment = 'fragment'
## Wells
class ResidentState(EnumString):
dna = 'dna'
organism = 'organism'
class GeneralState(EnumString):
stocked = "stocked"
terminated = "terminated"
shipped = "shipped"
class WellState(EnumString):
dried = "dried"
liquid = "liquid"
solid = "solid"
class WellSolvent(EnumString):
water = 'water'
te = 'te'
two_yt = '2yt'
# Wells <-> Wells
wells_wells = Table('wells_wells', Base.metadata,
Column('well', ForeignKey('wells.id'), primary_key=True),
Column('provenance_well', ForeignKey('wells.id'), primary_key=True)
)
class Well(Base, Id):
__tablename__ = "wells"
id = Column(Integer, primary_key=True)
volume = Column(Numeric)
address = Column(String)
plate_id = Column(Integer, ForeignKey('plates.id'))
solvent = Column(EnumType(WellSolvent), nullable=True)
logs = relationship("Log", backref="wells")
general_state = Column(EnumType(GeneralState), nullable=False)
@transition(source='stocked', target='terminated')
def terminate(self):
"""This function terminates a well"""
pass
well_state = Column(EnumType(WellState), nullable=False)
@transition(source='dried', target='liquid')
def resuspend(self):
"""This function resuspends a well"""
pass
resident_type = Column(EnumType(ResidentState))
part_id = Column(Integer, ForeignKey('parts.id'), nullable=True)
part = relationship("Part", back_populates="wells")
vector_id = Column(Integer, ForeignKey('parts.id'), nullable=True)
vector = relationship("Part", back_populates="wells")
organism_id = Column(Integer, ForeignKey('organisms.id'), nullable=True)
organism = relationship("Organism", back_populates="wells")
# TODO all cannot be false
# DNA data
ng_quantity = Column(Numeric)
quality = Column(Numeric)
def fmol_quantity(self):
"""This function calculates fmol per ul from volume and ng_quantity"""
pass
sequence_state = Column(EnumType(SequenceState), nullable=False)
@transition(source='not_sequenced', target='sequence_confirmed')
def sequence_dna(self):
"""This function analyzes sequence data. Or something."""
pass
# A well can come from many wells
provenance_wells = relationship('Well',
secondary=wells_wells,
backref='progeny_wells')
## Plates
class PlateTypes(EnumString):
deepwell = 'deepwell'
standard_plate = 'standard_plate'
pcr_plate = 'pcr_plate'
agar_plate = 'agar_plate'
class Plate(Base, Id):
__tablename__ = "plates"
plate_type = Column(EnumType(PlateTypes))
plate_location = Column(String) # Human readable sentence of where to find the plate
wells = relationship("Well", backref="plates")
## Logs
class Log(Base, Id):
"""A log of transitions that occured to wells"""
__tablename__ = "logs"
date_created = Column(DateTime(timezone=True), server_default=func.now())
well_id = Column(Integer, ForeignKey('wells.id'))
transition_written = Column(String)