-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_models.py
456 lines (345 loc) · 15.4 KB
/
db_models.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
from typing import Optional
from sqlmodel import Field, Relationship, SQLModel, UniqueConstraint
# ============================================================================
"""Terminology:
Database = DB
FK = Foreign Key
PK = Primary Key
"""
""" Naming Conventions:
SQLModel uses python type hints to infer the database schema.
The following classes define the schema of the database.
Since we are using python syntax, it is important to note that:
- Class names are singular and in CamelCase.
We explicitly define the table name using the __tablename__ attribute.
This is done to ensure that the table name is in snake_case and plural form,
which is the convention in SQL databases (DBs).
Due to incompatibility issues between:
- SQLModel
- SQLAlchemy (that creates the engine for the database)
We will be writing the code without:
- using the "from __future__ import annotations" syntax
- using the "from typing import List" syntax
Therefore we will also be sure to use 'list["ClassName"]' or 'ClassName' when
defining certain relationships.
"""
# ============================================================================
# Many-to-Many (MtM) Link Tables
# ============================================================================
"""
Many to Many (MtM) relationships are represented using link tables in SQL DBs.
These tables contain FKs to the PKs of the tables that are related.
The link tables are named using the following convention:
- Class naming: Class1Class2Link
- Table naming: table1_table2_link
In the SQLModel documentation, these tables are referred to as "link tables".
However, in the context of SQL DBs, these tables are also known as:
- association table
- secondary table
- junction table
- intermediate table
- join table
- through table
- relationship table
- connection table
- cross-reference table
We will be using the term "link table" for coherence with the SQLModel doc.
Link tables/classes are always initialized at the beginning of the models.
This is necessary because, even with "from __future__ import annotations",
the class won't be recognized if "link_model = Class1Class2Link" is
used BEFORE the class is declared.
"""
class DatasetAuthorLink(SQLModel, table=True):
"""
MtM link table between Dataset and Author
LOGIC: A dataset can have many authors and
an author can have published many datasets.
"""
__tablename__ = "datasets_authors_link"
dataset_id: Optional[int] = Field(
default=None, foreign_key="datasets.dataset_id", primary_key=True
)
author_id: Optional[int] = Field(
default=None, foreign_key="authors.author_id", primary_key=True
)
class DatasetMoleculeLink(SQLModel, table=True):
"""
MtM link table between Dataset and Molecule
LOGIC: A dataset can have 0 or many molecules and if we have a molecule,
it is definitely in a dataset.
"""
__tablename__ = "datasets_molecules_link"
dataset_id: Optional[int] = Field(
default=None, foreign_key="datasets.dataset_id", primary_key=True
)
molecule_id: Optional[int] = Field(
default=None, foreign_key="molecules.molecule_id", primary_key=True
)
class MoleculeTopologyLink(SQLModel, table=True):
"""
MtM link table between Molecule and TopologyFile
LOGIC: A molecule is definitely in one or more topology files and
a topology file necessarily has one or more molecules.
"""
__tablename__ = "molecules_topologies_link"
molecule_id: Optional[int] = Field(
default=None, foreign_key="molecules.molecule_id", primary_key=True
)
file_id: Optional[int] = Field(
default=None, foreign_key="topology_files.file_id", primary_key=True
)
class DatasetKeywordLink(SQLModel, table=True):
"""
MtM link table between Dataset and Molecule
LOGIC: A dataset can have 0 or many keywords and,
a keyword it is definitely in a dataset or many datasets.
"""
__tablename__ = "datasets_keywords_link"
dataset_id: Optional[int] = Field(
default=None, foreign_key="datasets.dataset_id", primary_key=True
)
keyword_id: Optional[int] = Field(
default=None, foreign_key="keywords.keyword_id", primary_key=True
)
# ============================================================================
# Main Tables
# ============================================================================
"""
Here we define the main tables of the database schema.
By "main tables" we mean the ones that represent the main entities in the DB.
These tables are those that have the most attributes
and relationships with other tables.
"""
class Dataset(SQLModel, table=True):
__tablename__ = "datasets"
# Attributes/Table columns -----------------------------------------------
dataset_id: Optional[int] = Field(default=None, primary_key=True)
origin_id: int = Field(foreign_key="dataset_origins.origin_id")
id_in_origin: str
doi: Optional[str] = Field(default=None)
date_created: str # YYYY-MM-DD format
date_last_modified: str # YYYY-MM-DD format
date_last_crawled: str # ("%Y-%m-%dT%H:%M:%S")
file_number: int = 0
download_number: int = 0
view_number: int = 0
license: Optional[str] = Field(default=None)
url: str
title: str
description: Optional[str] = None
# Relationships: files, origins, authors, molecules ----------------------
# A dataset can have many files, authors, and molecules
# (although it can have zero molecules)
# A dataset can have only one origin (not a list)
file: list["File"] = Relationship(back_populates="dataset")
origin: Optional["DatasetOrigin"] = Relationship(back_populates="dataset")
author: list["Author"] = Relationship(
back_populates="dataset", link_model=DatasetAuthorLink
)
molecule: Optional[list["Molecule"]] = Relationship(
back_populates="dataset", link_model=DatasetMoleculeLink
)
keyword: Optional[list["Keyword"]] = Relationship(
back_populates="dataset", link_model=DatasetKeywordLink
)
class File(SQLModel, table=True):
__tablename__ = "files"
# Attributes/Table columns -----------------------------------------------
file_id: Optional[int] = Field(default=None, primary_key=True)
dataset_id: int = Field(foreign_key="datasets.dataset_id")
name: str
file_type_id: int = Field(foreign_key="file_types.file_type_id")
size_in_bytes: Optional[float] = Field(default=None)
# files that belong to a zip file don't have md5
md5: Optional[str] = Field(default=None)
# files that belong to a zip file don't have url
url: Optional[str] = Field(default=None)
software_id: Optional[int] = Field(foreign_key="software.software_id")
is_from_zip_file: bool = Field(index=True)
parent_zip_file_id: Optional[int] = Field(
# notice the lowercase "f" to refer to the database table name
foreign_key="files.file_id",
default=None,
nullable=True,
)
# Relationships: datasets, files, topology_files, parameter_files, -------
# trajectory_files, software, file_types
parent: Optional["File"] = Relationship(
back_populates="children",
sa_relationship_kwargs=dict(remote_side="File.file_id"),
) # notice the uppercase "F" to refer to this table class
children: list["File"] = Relationship(back_populates="parent")
dataset: Dataset = Relationship(back_populates="file")
topology_file: Optional["TopologyFile"] = Relationship(back_populates="file")
parameter_file: Optional["ParameterFile"] = Relationship(back_populates="file")
trajectory_file: Optional["TrajectoryFile"] = Relationship(back_populates="file")
softwares: Optional["Software"] = Relationship(back_populates="file")
file_type: "FileType" = Relationship(back_populates="file")
class Author(SQLModel, table=True):
__tablename__ = "authors"
__table_args__ = (UniqueConstraint("name", "orcid"),)
# Attributes/Table columns -----------------------------------------------
author_id: Optional[int] = Field(default=None, primary_key=True)
name: str
orcid: Optional[str] = Field(default=None)
# Relationships: dataset
dataset: list[Dataset] = Relationship(
back_populates="author", link_model=DatasetAuthorLink
)
class Molecule(SQLModel, table=True):
__tablename__ = "molecules"
# Attributes/Table columns -----------------------------------------------
molecule_id: Optional[int] = Field(default=None, primary_key=True)
name: str
formula: str
sequence: str
molecule_type_id: Optional[int] = Field(
foreign_key="molecule_types.molecule_type_id"
)
# Relationships: datasets, topology_files, -------------------------------
# molecules_external_db, molecule_types
dataset: list[Dataset] = Relationship(
back_populates="molecule", link_model=DatasetMoleculeLink)
topology_file: list["TopologyFile"] = Relationship(
back_populates="molecule", link_model=MoleculeTopologyLink
)
mol_ext_db: Optional[list["MoleculeExternalDb"]] = Relationship(
back_populates="molecule"
)
molecule_type: Optional["MoleculeType"] = Relationship(back_populates="molecule")
class MoleculeExternalDb(SQLModel, table=True):
__tablename__ = "molecules_external_db"
# Attributes/Table columns -----------------------------------------------
mol_ext_db_id: Optional[int] = Field(default=None, primary_key=True)
molecule_id: int = Field(foreign_key="molecules.molecule_id")
db_name: str = Field(index=True)
id_in_external_db: str
database_id: Optional[int] = Field(foreign_key="databases.database_id")
# Relationships: molecules, databases ------------------------------------
molecule: Molecule = Relationship(back_populates="mol_ext_db")
database: Optional["Database"] = Relationship(back_populates="mol_ext_db")
# ============================================================================
# Simulation Files Tables
# ============================================================================
"""
These tables correspond to the files that are used in molecular simulations.
The tables are named after the file types:
- Topology files
- Parameter files
- Trajectory files
These tables have a one-to-one relationship with the `Files` table.
This means that each record in these tables
corresponds to exactly one record in the `Files` table.
This is why in these tables, the `file_id` is both the PK and a FK.
"""
class TopologyFile(SQLModel, table=True):
__tablename__ = "topology_files"
# File id is both the PK but also a FK to the Files table
file_id: Optional[int] = Field(
default=None, primary_key=True, foreign_key="files.file_id"
)
atom_number: int
has_protein: bool
has_nucleic: bool
has_lipid: bool
has_glucid: bool
has_water_ion: bool
# Relationships: files, molecules
file: File = Relationship(back_populates="topology_file")
molecule: list[Molecule] = Relationship(
back_populates="topology_file", link_model=MoleculeTopologyLink
)
class ParameterFile(SQLModel, table=True):
__tablename__ = "parameter_files"
file_id: Optional[int] = Field(
default=None, primary_key=True, foreign_key="files.file_id"
)
dt: Optional[float] = Field(default=None)
nsteps: Optional[int] = Field(default=None)
temperature: Optional[float] = Field(default=None)
thermostat_id: Optional[int] = Field(foreign_key="thermostats.thermostat_id")
barostat_id: Optional[int] = Field(foreign_key="barostats.barostat_id")
integrator_id: Optional[int] = Field(foreign_key="integrators.integrator_id")
# Relationships: files, thermostats, barostats, integrators
file: File = Relationship(back_populates="parameter_file")
thermostat: Optional["Thermostat"] = Relationship(back_populates="parameter_file")
barostat: Optional["Barostat"] = Relationship(back_populates="parameter_file")
integrator: Optional["Integrator"] = Relationship(back_populates="parameter_file")
class TrajectoryFile(SQLModel, table=True):
__tablename__ = "trajectory_files"
file_id: Optional[int] = Field(
default=None, primary_key=True, foreign_key="files.file_id"
)
atom_number: int
frame_number: int
# Relationships: files
file: File = Relationship(back_populates="trajectory_file")
# ============================================================================
# "Type" Tables
# ============================================================================
"""
These tables are used to store the, what we call,
"type" of the entities in the database.
For example, the different types of files that are used in molecular
simulations, the different types of molecules, etc.
These tables have a one-to-many relationship with the main tables.
"""
class Keyword(SQLModel, table=True):
__tablename__ = "keywords"
# Attributes/Table columns -----------------------------------------------
keyword_id: Optional[int] = Field(default=None, primary_key=True)
entry: str = Field(unique=True)
# Relationships: datasets
dataset: list[Dataset] = Relationship(
back_populates="keyword", link_model=DatasetKeywordLink
)
class FileType(SQLModel, table=True):
__tablename__ = "file_types"
file_type_id: Optional[int] = Field(default=None, primary_key=True)
name: str = Field(unique=True)
# Relationships: files
file: list[File] = Relationship(back_populates="file_type")
class MoleculeType(SQLModel, table=True):
__tablename__ = "molecule_types"
molecule_type_id: Optional[int] = Field(default=None, primary_key=True)
name: str = Field(unique=True)
# Relationships: molecules
molecule: list[Molecule] = Relationship(back_populates="molecule_type")
class Database(SQLModel, table=True):
__tablename__ = "databases"
database_id: Optional[int] = Field(default=None, primary_key=True)
name: str = Field(unique=True)
# Relationships: molecules_external_db
mol_ext_db: list[MoleculeExternalDb] = Relationship(back_populates="database")
class DatasetOrigin(SQLModel, table=True):
__tablename__ = "dataset_origins"
origin_id: Optional[int] = Field(default=None, primary_key=True)
name: str = Field(unique=True)
# Relationships: datasets
dataset: list[Dataset] = Relationship(back_populates="origin")
class Software(SQLModel, table=True):
__tablename__ = "software"
__table_args__ = (UniqueConstraint("name", "version"),)
software_id: Optional[int] = Field(default=None, primary_key=True)
name: str
version: Optional[str] = Field(default=None)
# Relationships: files
file: list[File] = Relationship(back_populates="softwares")
class Thermostat(SQLModel, table=True):
__tablename__ = "thermostats"
thermostat_id: Optional[int] = Field(default=None, primary_key=True)
name: str = Field(unique=True)
# Relationships: parameter_files
parameter_file: list[ParameterFile] = Relationship(back_populates="thermostat")
class Barostat(SQLModel, table=True):
__tablename__ = "barostats"
barostat_id: Optional[int] = Field(default=None, primary_key=True)
name: str = Field(unique=True)
# Relationships: parameter_files
parameter_file: list[ParameterFile] = Relationship(back_populates="barostat")
class Integrator(SQLModel, table=True):
__tablename__ = "integrators"
integrator_id: Optional[int] = Field(default=None, primary_key=True)
name: str = Field(unique=True)
# Relationships: parameter_files
parameter_file: list[ParameterFile] = Relationship(back_populates="integrator")