forked from vanallenlab/moalmanac-db
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdereference.py
More file actions
1519 lines (1298 loc) · 60.9 KB
/
Copy pathdereference.py
File metadata and controls
1519 lines (1298 loc) · 60.9 KB
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Postpones evaluation of type annotations so classes can be referenced before they are defined
# (avoids needing quotes around type names)
from __future__ import annotations
import argparse
import os
import pathlib
import typing
# Local imports
from utils import json_utils
from utils import populate_statement_description_from_indication
from utils import read
from utils import write
class BaseTable:
"""
A base class for managing and dereferencing records across database tables. This class provides common
functionality for dereferencing keys that reference other tables. It serves as a template for specific table
classes, which inherit from BaseTable and implement additional table-specific logic.
Attributes:
records (list[dict]): list of dictionaries that represent one table within the relational database.
"""
def __init__(self, records: list[dict]):
"""
Initializes the BaseTable with a list of records.
Args:
records (list[dict]): list of dictionaries that represent one table within the relational database.
"""
self.records = records
@staticmethod
def dereference_single(
record: dict, referenced_key: str, referenced_records: list[dict]
) -> None:
"""
Dereferences a key for each record in records, where the key's value references a single record.
Args:
record (dict): the dictionary that contains a key to dereference.
referenced_key (str): name of the key in `records` to dereference.
referenced_records (list[dict]): list of dictionaries that the `referenced_key` refers to.
Raises:
KeyError: If the referenced_key is not found in a record.
"""
if referenced_key not in record:
raise KeyError(f"Key '{referenced_key}' not found in {record}.")
referenced_record = json_utils.get_record_by_key_value(
records=referenced_records,
key="id",
value=record[referenced_key],
strict=True,
)
record[referenced_key] = referenced_record
@staticmethod
def dereference_list(
record: dict,
referenced_key: str,
referenced_records: list[dict],
key_always_present: bool = True,
) -> None:
"""
Dereferences a key for a provided `record`, where the key's value is of type List that references multiple records in another table.
Args:
record (dict): the dictionary that contains a key to dereference.
referenced_key (str): name of the key in `record` to dereference.
referenced_records (str): list of dictionaries that the `referenced_key` refers to.
key_always_present (bool): If True, the `referenced_key` is present in all records.
Raises:
KeyError: If the `referenced_key` is not found in a record when `key_always_present` is True.
"""
if key_always_present and (referenced_key not in record):
raise KeyError(
f"Key '{referenced_key}' not found but should be found in {record}"
)
if referenced_key not in record:
pass
else:
_values = []
for value in record[referenced_key]:
_value = json_utils.get_record_by_key_value(
records=referenced_records, key="id", value=value
)
_values.append(_value)
record[referenced_key] = _values
@staticmethod
def replace_key(record: dict, old_key: str, new_key: str) -> None:
"""
Dereferences a key for each record in records, where the key's value references a single record.
Args:
record (dict): the dictionary that contains a key to replace.
old_key (str): the name of the key in `record` to replace.
new_key (str): the new key name that will replace `old_key` in `record`.
Raises:
KeyError: If the `old_key` is not found in the record.
"""
if old_key not in record:
raise KeyError(f"Key '{old_key}' not found in {record}")
record[new_key] = record.pop(old_key)
@staticmethod
def remove_key(record: dict, key: str) -> None:
"""
Removes a key from the provided dictionary.
Args:
record (dict): the dictionary that contains a key to remove.
key (str): name of the key in `record` to remove.
Raises:
KeyError: If the `key` is not found in `record`.
"""
if key not in record:
raise KeyError(f"Key '{key}' not found in {record}")
record.pop(key)
class Agents(BaseTable):
"""
Represents the Agents table. This class inherits common functionality from the BaseTable class and
dereferences keys that reference other tables. This table does not currently reference any other tables.
Attributes:
records (list[dict]): A list of dictionaries representing the agent records.
"""
pass
class Biomarkers(BaseTable):
"""
Represents the Biomarkers table. This class inherits common functionality from the BaseTable class and
dereferences keys that reference other tables. This table references the following tables:
- Genes (initial key: `genes`, resulting key: `genes`)
Attributes:
records (list[dict]): A list of dictionaries representing the biomarker records.
"""
def dereference(
self,
genes: Genes,
resolve_dependencies: bool = True,
codings: Codings | None = None,
mappings: Mappings | None = None,
) -> None:
"""
Dereferences all referenced keys within the Genes table and optionally resolves dependencies
within these related tables.
Calls functions within this class to replace references with actual data from the referenced tables.
If `resolve_dependencies` is set to True and the dependent table(s) are provided, it will also ensure that
references are also fully dereferenced by utilizing the `dereference` function from their respective classes.
Args:
genes (Genes): An instance of the Genes class representing the genes table.
resolve_dependencies (bool): If `True`, resolve dependencies within referenced tables.
codings (Codings): An instance of the Codings class representing the codings table.
mappings (Mappings): An instance of the Mappings class representing the mappings table.
"""
if resolve_dependencies:
if codings and mappings:
genes.dereference(
codings=codings,
mappings=mappings,
resolve_dependencies=True,
)
self.dereference_genes(genes=genes)
def dereference_genes(self, genes: Genes) -> None:
"""
Dereferences the `genes` key in each biomarker record.
Utilizes the `dereference_list` function from the BaseTable class to replace the value associated with the
`genes` key within each record. This key is not expected to be present within each record, so no KeyError will
be raised if it is missing.
Args:
genes (Genes): An instance of the Genes class representing the genes table.
"""
for record in self.records:
self.dereference_list(
record=record,
referenced_key="genes",
referenced_records=genes.records,
key_always_present=False,
)
class Codings(BaseTable):
"""
Represents the Codings table. This class inherits common functionality from the BaseTable class and
dereferences keys that reference other tables. This table does not currently reference any other tables.
Attributes:
records (list[dict]): A list of dictionaries representing the coding records.
"""
pass
class Contributions(BaseTable):
"""
Represents the Contributions table. This class inherits common functionality from the BaseTable class and
dereferences keys that reference other tables. This table references the following tables:
- Agents (initial key: `agent_id`, resulting key: `agents`)
Attributes:
records (list[dict]): A list of dictionaries representing the contribution records.
"""
def dereference(self, agents: Agents) -> None:
"""
Dereferences all referenced keys within the Contributions table.
Args:
agents (Agents): An instance of the Agents class representing the agents table.
"""
self.dereference_agents(agents=agents)
def dereference_agents(self, agents: Agents) -> None:
"""
Dereferences the `agent_id` key in each contribution record.
Utilizes the `dereference_single` function from the BaseTable class to replace the value associated with the
`agent_id` key within each record. This key is expected to be present within each record, so a
KeyError will be raised if it is missing.
Args:
agents (Agents): An instance of the Agents class representing the agents table.
Raises:
KeyError: If the referenced_key, `agent_id`, is not found in a record.
"""
for record in self.records:
self.dereference_single(
record=record,
referenced_key="agent_id",
referenced_records=agents.records,
)
self.replace_key(record=record, old_key="agent_id", new_key="contributor")
class Diseases(BaseTable):
"""
Represents the Diseases table. This class inherits common functionality from the BaseTable class and
dereferences keys that reference other tables. This table references the following tables:
- Codings (initial key: `primary_coding_id`, resulting_key: `primaryCoding`)
- Mappings (initial key: `mappings`, resulting_key: `mappings`)
Attributes:
records (list[dict]): A list of dictionaries representing the therapy records.
"""
def dereference(
self,
codings: Codings,
mappings: Mappings,
resolve_dependencies: bool = True,
) -> None:
"""
Dereferences all referenced keys within the Diseases table and optionally resolves dependencies
within these related tables.
Calls functions within this class to replace references with actual data from the referenced tables.
If `resolve_dependencies` is set to True and the dependent table(s) are provided, it will also ensure that
references are also fully dereferenced by utilizing the `dereference` function from their respective classes.
Args:
codings (Codings): An instance of the Codings class representing the codings table.
mappings (Mappings): An instance of the Mappings class representing the mappings table.
resolve_dependencies (bool): If `True`, resolve dependencies within referenced tables.
"""
if resolve_dependencies:
mappings.dereference(codings=codings)
self.dereference_codings(codings=codings)
self.dereference_mappings(mappings=mappings)
def dereference_codings(self, codings: Codings) -> None:
"""
Dereferences the `primary_coding_id` key in each strength record.
Utilizes the `dereference_single` function from the BaseTable class to replace the value associated with the
`primary_coding_id` key within each record. This key is expected to be present within each record, so a
KeyError will be raised if it is missing.
Args:
codings (Codings): An instance of the Codings class representing the codings table.
Raises:
KeyError: If the referenced_key, `primary_coding_id`, is not found in a record.
"""
for record in self.records:
self.dereference_single(
record=record,
referenced_key="primary_coding_id",
referenced_records=codings.records,
)
self.replace_key(
record=record,
old_key="primary_coding_id",
new_key="primaryCoding",
)
def dereference_mappings(self, mappings: Mappings) -> None:
"""
Dereferences the `mappings` key in each gene record.
Utilizes the `dereference_list` function from the BaseTable class to replace the value associated with the
`mappings` key within each record. This key is expected to be present within each record, so a
KeyError will be raised if it is missing.
Args:
mappings (Mappings): An instance of the Mappings class representing the mappings table.
Raises:
KeyError: If the referenced_key, `mappings`, is not found in a record.
"""
for record in self.records:
self.dereference_list(
record=record,
referenced_key="mappings",
referenced_records=mappings.records,
key_always_present=True,
)
for mapping in record["mappings"]:
self.remove_key(record=mapping, key="id")
self.remove_key(record=mapping, key="primary_coding_id")
class Documents(BaseTable):
"""
Represents the Documents table. This class inherits common functionality from the BaseTable class and
dereferences keys that reference other tables. This table references the following tables:
- Agents (initial key: `agent_id`, resulting key: `agent`)
Attributes:
records (list[dict]): A list of dictionaries representing the document records.
"""
def convert_fields_to_extensions(self):
"""
Converts relevant keys to extensions.
"""
extension_fields = [
'agent',
'company',
'drug_name_brand',
'drug_name_generic',
'first_publication_date',
'identification_number',
'publication_date',
'status'
]
for record in self.records:
extensions = [
{
"name": "agent",
"value": record["agent"],
"description": "The organization that published this document.",
},
{
"name": "company",
"value": record["company"],
"description": "The company that manufactures the cancer drug. Only applicable to market authorization documents.",
},
{
"name": "drug_name_brand",
"value": record["drug_name_brand"],
"description": "The brand name of the cancer drug, per this document. Only applicable to market authorization documents.",
},
{
"name": "drug_name_generic",
"value": record["drug_name_generic"],
"description": "The generic name of the cancer drug, per this document. Only applicable to market authorization documents.",
},
{
"name": "first_publication_date",
"value": record["first_publication_date"],
"description": "The publication date for the initial version of this document.",
},
{
"name": "identification_number",
"value": record["identification_number"],
"description": "Identification number used by the publishing organization.",
},
{
"name": "publication_date",
"value": record["publication_date"],
"description": "The publication date for the document.",
},
{
"name": "status",
"value": record["status"],
"description": "Whether this document is Active or Deprecated within moalmanac-db.",
}
]
record['extensions'] = extensions
for field in extension_fields:
self.remove_key(record=record, key=field)
def dereference(self, agents: Agents, urls: URLs) -> None:
"""
Dereferences all referenced keys within the Documents table.
Args:
agents (Agents): An instance of the Agents class representing the agents table.
"""
self.dereference_agents(agents=agents)
self.dereference_urls(urls=urls)
self.convert_fields_to_extensions()
def dereference_agents(self, agents: Agents) -> None:
"""
Dereferences the `agent_id` key in each document record.
Utilizes the `dereference_single` function from the BaseTable class to replace the value associated with the
`agent_id` key within each record. This key is expected to be present within each record, so a
KeyError will be raised if it is missing.
Args:
agents (Agents): An instance of the Agents class representing the agents table.
Raises:
KeyError: If the referenced_key, agent_id`, is not found in a record.
"""
for record in self.records:
self.dereference_single(
record=record,
referenced_key="agent_id",
referenced_records=agents.records,
)
self.replace_key(
record=record,
old_key="agent_id",
new_key="agent",
)
def dereference_urls(self, urls: URLs) -> None:
"""
Dereferences the `urls` key in each document record.
Utilizes the `dereference_list` function from the BaseTable class to replace the value associated with the `urls` key within each record.
Args:
urls (URLs): An instance of the URLs class representing the URls table.
"""
for record in self.records:
self.dereference_list(
record=record,
referenced_key="urls",
referenced_records=urls.records,
key_always_present=True
)
record['urls'] = [item['url'] for item in record['urls']]
class Genes(BaseTable):
"""
Represents the Genes table. This class inherits common functionality from the BaseTable class and
dereferences keys that reference other tables. This table references the following tables:
- Codings (initial key: `primary_coding_id`, resulting_key: `primaryCoding`)
- Mappings (initial key: `mappings`, resulting_key: `mappings`)
Attributes:
records (list[dict]): A list of dictionaries representing the therapy records.
"""
def dereference(
self,
codings: Codings,
mappings: Mappings,
resolve_dependencies: bool = True,
) -> None:
"""
Dereferences all referenced keys within the Genes table and optionally resolves dependencies
within these related tables.
Calls functions within this class to replace references with actual data from the referenced tables.
If `resolve_dependencies` is set to True and the dependent table(s) are provided, it will also ensure that
references are also fully dereferenced by utilizing the `dereference` function from their respective classes.
Args:
codings (Codings): An instance of the Codings class representing the codings table.
mappings (Mappings): An instance of the Mappings class representing the mappings table.
resolve_dependencies (bool): If `True`, resolve dependencies within referenced tables.
"""
if resolve_dependencies:
if codings and mappings:
mappings.dereference(codings=codings)
self.dereference_codings(codings=codings)
self.dereference_mappings(mappings=mappings)
def dereference_codings(self, codings: Codings) -> None:
"""
Dereferences the `primary_coding_id` key in each strength record.
Utilizes the `dereference_single` function from the BaseTable class to replace the value associated with the
`primary_coding_id` key within each record. This key is expected to be present within each record, so a
KeyError will be raised if it is missing.
Args:
codings (Codings): An instance of the Codings class representing the codings table.
Raises:
KeyError: If the referenced_key, `primary_coding_id`, is not found in a record.
"""
for record in self.records:
self.dereference_single(
record=record,
referenced_key="primary_coding_id",
referenced_records=codings.records,
)
self.replace_key(
record=record,
old_key="primary_coding_id",
new_key="primaryCoding",
)
def dereference_mappings(self, mappings: Mappings) -> None:
"""
Dereferences the `mappings` key in each gene record.
Utilizes the `dereference_list` function from the BaseTable class to replace the value associated with the
`mappings` key within each record. This key is expected to be present within each record, so a
KeyError will be raised if it is missing.
Args:
mappings (Mappings): An instance of the Mappings class representing the mappings table.
Raises:
KeyError: If the referenced_key, `mappings`, is not found in a record.
"""
for record in self.records:
self.dereference_list(
record=record,
referenced_key="mappings",
referenced_records=mappings.records,
key_always_present=True,
)
for mapping in record["mappings"]:
self.remove_key(record=mapping, key="id")
self.remove_key(record=mapping, key="primary_coding_id")
class Indications(BaseTable):
"""
Represents the Indications table. This class inherits common functionality from the BaseTable class and
dereferences keys that reference other tables. This table references the following tables:
- Documents (initial key: `document_id`, resulting key: `document`)
Attributes:
records (list[dict]): A list of dictionaries representing the indication records.
"""
def dereference(
self,
documents: Documents,
resolve_dependencies: bool = True,
agents: Agents | None = None,
urls: URLs | None = None,
) -> None:
"""
Dereferences all referenced keys within the Indications table and optionally resolves dependencies
within these related tables.
Calls functions within this class to replace references with actual data from the referenced tables.
If `resolve_dependencies` is set to True and the dependent table(s) are provided, it will also ensure that
references are also fully dereferenced by utilizing the `dereference` function from their respective classes.
Args:
documents (Documents): An instance of the Documents class representing the documents table.
resolve_dependencies (bool): If `True`, resolve dependencies within referenced tables.
agents (Agents): An instance of the Agents class representing the agents table.
urls (URLs): An instance of the URLs class representing the urls table.
"""
if resolve_dependencies:
if agents is None or urls is None:
raise ValueError("If resolve_dependencies=True, both 'agents' and 'urls' must be provided.")
documents.dereference(agents=agents, urls=urls)
self.dereference_documents(documents=documents)
def dereference_documents(self, documents: Documents) -> None:
"""
Dereferences the `document_id` key in each indication record.
Utilizes the `dereference_single` function from the BaseTable class to replace the value associated with the
`document_id` key within each record. This key is expected to be present within each record, so a
KeyError will be raised if it is missing.
Args:
documents (Documents): An instance of the Documents class representing the documents table.
Raises:
KeyError: If the referenced_key, `document_id`, is not found in a record.
"""
for record in self.records:
self.dereference_single(
record=record,
referenced_key="document_id",
referenced_records=documents.records,
)
self.replace_key(record=record, old_key="document_id", new_key="document")
class Mappings(BaseTable):
"""
Represents the Mappings table. This class inherits common functionality from the BaseTable class and
dereferences keys that reference other tables. This table references the following tables:
- Codings (initial key: `coding_id`, resulting key: `coding`)
Attributes:
records (list[dict]): A list of dictionaries representing the contribution records.
"""
def dereference(self, codings: Codings) -> None:
"""
Dereferences all referenced keys within the Mappings table.
Calls functions within this class to replace references with actual data from the referenced tables.
If `resolve_dependencies` is set to True and the dependent table(s) are provided, it will also ensure that
references are also fully dereferenced by utilizing the `dereference` function from their respective classes.
Args:
codings (Codings): An instance of the Codings class representing the codings table.
"""
self.dereference_codings(codings=codings)
def dereference_codings(self, codings: Codings) -> None:
"""
Dereferences the `coding_id` key in each coding record.
Utilizes the `dereference_list` function from the BaseTable class to replace the value associated with the
`coding_id` key within each record. This key is not expected to be present within each record, so no KeyError will
be raised if it is missing.
Args:
codings (Codings): An instance of the Codings class representing the codings table.
"""
for record in self.records:
self.dereference_single(
record=record,
referenced_key="coding_id",
referenced_records=codings.records,
)
self.replace_key(record=record, old_key="coding_id", new_key="coding")
class Propositions(BaseTable):
"""
Represents the Propositions table. This class inherits common functionality from the BaseTable class and
dereferences keys that reference other tables. This table references the following tables:
- Biomarkers (initial key: `biomarkers`, resulting key: `biomarkers`)
- Diseases (initial key: `conditionQualifier_id`, resulting key: `conditionQualifier`)
- Therapies (initial key: `therapy_id`, resulting key: `objectTherapeutic`)
- TherapyGroups (initial_key: `therapy_group_id`, resulting key: `objectTherapeutic`)
Attributes:
records (list[dict]): A list of dictionaries representing the proposition records.
"""
def dereference(
self,
biomarkers: Biomarkers,
diseases: Diseases,
therapies: Therapies,
therapy_groups: TherapyGroups,
resolve_dependencies: bool = True,
codings: Codings | None = None,
genes: Genes | None = None,
mappings: Mappings | None = None,
) -> None:
"""
Dereferences all referenced keys within the Propositions table and optionally resolves dependencies
within these related tables.
Calls functions within this class to replace references with actual data from the referenced tables.
If `resolve_dependencies` is set to True and the dependent table(s) are provided, it will also ensure that
references are also fully dereferenced by utilizing the `dereference` function from their respective classes.
Args:
biomarkers (Biomarkers): An instance of the Biomarkers class representing the biomarkers table.
diseases (Diseases): An instance of the Diseases class representing the diseases table.
therapies (Therapies): list of dictionaries to dereference `therapy_ids` against.
therapy_groups (TherapyGroups): list of dictionaries to dereference `therapy_group_ids` against.
resolve_dependencies (bool): If `True`, resolve dependencies within referenced tables.
codings (Codings): An instance of the Codings class representing the codings table.
genes (Genes): An instance of the Genes class representing the genes table.
mappings (Mappings): An instance of the Mappings class representing the mappings table.
"""
if resolve_dependencies:
if diseases and codings and mappings:
diseases.dereference(
codings=codings,
mappings=mappings,
resolve_dependencies=True,
)
if genes and codings and mappings:
genes.dereference(
codings=codings,
mappings=mappings,
resolve_dependencies=True,
)
biomarkers.dereference(genes=genes, resolve_dependencies=False)
if therapies and codings and mappings:
therapies.dereference(
codings=codings,
mappings=mappings,
resolve_dependencies=True,
)
if therapies and therapy_groups:
therapy_groups.dereference(
therapies=therapies, resolve_dependencies=False
)
# Maybe change this to just do therapy groups?
self.dereference_biomarkers(biomarkers=biomarkers)
self.dereference_diseases(diseases=diseases)
self.dereference_therapeutics(
therapies=therapies, therapy_groups=therapy_groups
)
def dereference_biomarkers(self, biomarkers: Biomarkers) -> None:
"""
Dereferences the `biomarkers` key in each proposition record.
Utilizes the `dereference_list` function from the BaseTable class to replace the value associated with the
`biomarkers` key within each record. This key is expected to be present within each record, so a KeyError will
be raised if it is missing.
Args:
biomarkers (Biomarkers): An instance of the Biomarkers class representing the biomarkers table.
Raises:
KeyError: If the referenced_key, `biomarkers`, is not found in a record.
"""
for record in self.records:
self.dereference_list(
record=record,
referenced_key="biomarkers",
referenced_records=biomarkers.records,
key_always_present=True,
)
def dereference_diseases(self, diseases: Diseases) -> None:
"""
Dereferences the `conditionQualifier_id` key in each proposition record.
Utilizes the `dereference_list` function from the BaseTable class to replace the value associated with the
`conditionQualifier_id` key within each record. This key is expected to be present within each record, so a
KeyError will be raised if it is missing.
Args:
diseases (Diseases): An instance of the Diseases class representing the diseases table.
Raises:
KeyError: If the referenced_key, `conditionQualifier_id`, is not found in a record.
"""
for record in self.records:
self.dereference_single(
record=record,
referenced_key="conditionQualifier_id",
referenced_records=diseases.records,
)
self.replace_key(
record=record,
old_key="conditionQualifier_id",
new_key="conditionQualifier",
)
def dereference_therapeutics(
self, therapies: Therapies, therapy_groups: TherapyGroups
) -> None:
"""
Dereferences the `therapy_id` key or `therapy_group_id` key in each proposition record.
Utilizes the `dereference_list` function from the BaseTable class to replace the value associated with the
`objectTherapeutic` key within each record.
Args:
therapies (Therapies): list of dictionaries to dereference `therapy_ids` against.
therapy_groups (TherapyGroups): list of dictionaries to dereference `therapy_group_ids` against.
Raises:
KeyError: If neither referenced_key values, `therapy_id` or `therapy_group_id, are not found in a record.
"""
for record in self.records:
if isinstance(record["therapy_id"], int):
self.dereference_single(
record=record,
referenced_key="therapy_id",
referenced_records=therapies.records,
)
self.replace_key(
record=record,
old_key="therapy_id",
new_key="objectTherapeutic",
)
self.remove_key(record=record, key="therapy_group_id")
elif isinstance(record["therapy_group_id"], int):
self.dereference_single(
record=record,
referenced_key="therapy_group_id",
referenced_records=therapy_groups.records,
)
self.replace_key(
record=record,
old_key="therapy_group_id",
new_key="objectTherapeutic",
)
self.remove_key(record=record, key="therapy_id")
else:
raise KeyError(
f"Neither 'therapy_id' nor 'therapy_group_id' are keys found in {record}"
)
class Statements(BaseTable):
"""
Represents the Statements table. This class inherits common functionality from the BaseTable class and
dereferences keys that reference other tables. This table references the following tables:
- Contributions (initial key: `contributions`, resulting key: `contributions`)
- Documents (initial key: `reportedIn`, resulting key: `reportedIn`)
- Indications (initial key: `indication_id, resulting key: `indication`)
- Propositions (initial key: `proposition_id`, resulting key: `proposition`)
- Strengths (initial key: `strength_id`, resulting key: `strength`)
Attributes:
records (list[dict]): A list of dictionaries representing the statement records.
"""
def dereference(
self,
codings: Codings,
contributions: Contributions,
documents: Documents,
indications: Indications,
mappings: Mappings,
propositions: Propositions,
strengths: Strengths,
resolve_dependencies: bool = True,
agents: Agents | None = None,
biomarkers: Biomarkers | None = None,
diseases: Diseases | None = None,
genes: Genes | None = None,
therapies: Therapies | None = None,
therapy_groups: TherapyGroups | None = None,
urls: URLs | None = None,
) -> None:
"""
Dereferences all referenced keys within the Propositions table and optionally resolves dependencies
within these related tables.
Calls functions within this class to replace references with actual data from the referenced tables.
If `resolve_dependencies` is set to True and the dependent table(s) are provided, it will also ensure that
references are also fully dereferenced by utilizing the `dereference` function from their respective classes.
Args:
contributions (Contributions): An instance of the Contributions class representing the contributions table.
documents (Documents): An instance of the Documents class representing the documents table.
indications (Indications): An instance of the Indications class representing the indications table.
propositions (Propositions): An instance of the Propositions class representing the propositions table.
resolve_dependencies (bool): If `True`, resolve dependencies within referenced tables.
agents (Agents): An instance of the Agents class representing the agents table.
biomarkers (Biomarkers): An instance of the Biomarkers class representing the biomarkers table.
codings (Codings): An instance of the Codings class representing the codings table.
diseases (Diseases): An instance of the Diseases class representing the diseases table.
genes (Genes): An instance of the Genes class representing the genes table.
mappings (Mappings): An instance of the Mappings class representing the mappings table.
strengths (Strengths): An instance of the Strengths class representing the strengths table.
therapies (Therapies): list of dictionaries to dereference `therapy_ids` against.
therapy_groups (TherapyGroups): list of dictionaries to dereference `therapy_group_ids` against.
urls (URLs): An instance of the URLs class representing the urls table.
"""
if resolve_dependencies:
if codings and mappings:
mappings.dereference(codings=codings)
if agents:
contributions.dereference(agents=agents)
if agents and urls:
documents.dereference(agents=agents, urls=urls)
if biomarkers and genes:
genes.dereference(
codings=codings,
mappings=mappings,
resolve_dependencies=False,
)
biomarkers.dereference(genes=genes, resolve_dependencies=False)
if diseases:
diseases.dereference(
codings=codings,
mappings=mappings,
resolve_dependencies=False,
)
if therapies:
therapies.dereference(
codings=codings,
mappings=mappings,
resolve_dependencies=False,
)
if therapies and therapy_groups:
therapy_groups.dereference(therapies=therapies)
if biomarkers and diseases and therapies and therapy_groups:
propositions.dereference(
biomarkers=biomarkers,
diseases=diseases,
therapies=therapies,
therapy_groups=therapy_groups,
resolve_dependencies=False,
)
indications.dereference(documents=documents, resolve_dependencies=False)
strengths.dereference(codings=codings)
self.dereference_contributions(contributions=contributions)
self.dereference_documents(documents=documents)
self.dereference_indications(indications=indications)
self.dereference_propositions(propositions=propositions)
self.dereference_strengths(strengths=strengths)
def dereference_contributions(self, contributions: Contributions) -> None:
"""
Dereferences the `contributions` key in each statement record.
Utilizes the `dereference_list` function from the BaseTable class to replace the value associated with the
`contributions` key within each record. This key is expected to be present within each record, so a
KeyError will be raised if it is missing.
Args:
contributions (Contributions): An instance of the Contributions class representing the Contributions table.
Raises:
KeyError: If the referenced_key, `contributions`, is not found in a record.
"""
for record in self.records:
self.dereference_list(
record=record,
referenced_key="contributions",
referenced_records=contributions.records,
key_always_present=True,
)
def dereference_documents(self, documents: Documents) -> None:
"""
Dereferences the `reportedIn` key in each statement record.
Utilizes the `dereference_list` function from the BaseTable class to replace the value associated with the
`reportedIn` key within each record. This key is expected to be present within each record, so a
KeyError will be raised if it is missing.
Args:
documents (Documents): An instance of the Documents class representing the documents table.
Raises:
KeyError: If the referenced_key, `reportedIn`, is not found in a record.
"""
for record in self.records:
self.dereference_list(
record=record,
referenced_key="reportedIn",
referenced_records=documents.records,
key_always_present=True,
)
def dereference_indications(self, indications: Indications) -> None:
"""
Dereferences the `indication_id` key in each statement record.
Utilizes the `dereference_list` function from the BaseTable class to replace the value associated with the
`indication_id` key within each record. This key is expected to be present within each record, so a
KeyError will be raised if it is missing.
Note: This will eventually not be expected to be present within each record, once we add more than regulatory approvals.
Args:
indications (Indications): An instance of the Indications class representing the indications table.
Raises:
KeyError: If the referenced_key, `indication_id`, is not found in a record.
"""
for record in self.records: