-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathQueryCOHD.py
More file actions
1123 lines (979 loc) · 50.5 KB
/
Copy pathQueryCOHD.py
File metadata and controls
1123 lines (979 loc) · 50.5 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
'''This module defines the class QueryCOHD. QueryCOHD class is designed to
communicate with the Columbia Open Health Dataset API (cohd.nsides.io) in order
obtain a "co-occurrence" probability between any two terms in medical records.
Test Cases in
[repo]/code/reasoningtool/kg-construction/tests/QueryCOHDTests.py
'''
__author__ = 'Deqing Qu'
__copyright__ = 'Oregon State University'
__credits__ = ['Deqing Qu', 'Stephen Ramsey']
__license__ = 'MIT'
__version__ = '0.1.0'
__maintainer__ = ''
__email__ = ''
__status__ = 'Prototype'
# import requests
# import requests_cache
import sys
import urllib.parse
from cache_control_helper import CacheControlHelper
# configure requests package to use the "QueryCOHD.sqlite" cache
# requests_cache.install_cache('QueryCOHD')
class QueryCOHD:
TIMEOUT_SEC = 120
#API_BASE_URL = 'http://cohd.nsides.io/api'
API_BASE_URL = 'http://cohd.io/api'
HANDLER_MAP = {
'find_concept_id': 'omop/findConceptIDs',
'get_paired_concept_freq': 'frequencies/pairedConceptFreq',
'get_individual_concept_freq': 'frequencies/singleConceptFreq',
'get_associated_concept_domain_freq': 'frequencies/associatedConceptDomainFreq',
'get_concepts': 'omop/concepts',
'get_xref_from_OMOP': 'omop/xrefFromOMOP',
'get_xref_to_OMOP': 'omop/xrefToOMOP',
'get_map_from_standard_concept_id': 'omop/mapFromStandardConceptID',
'get_map_to_standard_concept_id': 'omop/mapToStandardConceptID',
'get_vocabularies': 'omop/vocabularies',
'get_associated_concept_freq': 'frequencies/associatedConceptFreq',
'get_most_frequent_concepts': 'frequencies/mostFrequentConcepts',
'get_chi_square': 'association/chiSquare',
'get_obs_exp_ratio': 'association/obsExpRatio',
'get_relative_frequency': 'association/relativeFrequency',
'get_datasets': 'metadata/datasets',
'get_domain_counts': 'metadata/domainCounts',
'get_domain_pair_counts': 'metadata/domainPairCounts',
'get_patient_count': 'metadata/patientCount',
'get_concept_ancestors': '/omop/conceptAncestors',
'get_concept_descendants': '/omop/conceptDescendants'
}
@staticmethod
def __access_api(handler, url_suffix):
requests = CacheControlHelper()
url = QueryCOHD.API_BASE_URL + '/' + handler + '?' + url_suffix
try:
res = requests.get(url, timeout=QueryCOHD.TIMEOUT_SEC)
except requests.exceptions.Timeout:
print(url, file=sys.stderr)
print('Timeout in QueryCOHD for URL: ' + url, file=sys.stderr)
return None
except KeyboardInterrupt:
sys.exit(0)
except BaseException as e:
print(url, file=sys.stderr)
print('%s received in QueryCOHD for URL: %s' % (e, url), file=sys.stderr)
return None
status_code = res.status_code
if status_code != 200:
print(url, file=sys.stderr)
print('Status code ' + str(status_code) + ' for url: ' + url, file=sys.stderr)
return None
return res.json()
@staticmethod
def find_concept_ids(node_label, domain="", dataset_id=1, min_count=1):
"""search for OMOP concepts by name
Args:
node_label (str): The name of the concept to search for, e.g., "cancer" or "ibuprofen"
domain (str): The domain (e.g., "Condition", "Drug", "Procedure") to restrict the search to. If not
specified, the search will be unrestricted.
dataset_id (int): The dataset to reference when sorting concepts by their frequency. Default: 5-year
dataset (1).
min_count (int): The minimum concept count (inclusive) to include a concept in the search results. Setting
the min_count to 0 will cause findConceptIDs to return all matching standard OMOP concepts (this can be
slow). Setting the min_count to 1 will cause findConceptIDs to only return concepts with count data
(much faster). Default: 1.
Returns:
list: a list of dictionary, including names and IDs, or an empty list if no concept IDs could be obtained
for the given label
example:
[
{
"concept_class_id": "Clinical Finding",
"concept_code": "212602006",
"concept_count": 0,
"concept_id": 4059406,
"concept_name": "Ibuprofen poisoning",
"domain_id": "Condition",
"vocabulary_id": "SNOMED"
},
{
"concept_class_id": "Clinical Finding",
"concept_code": "218613000",
"concept_count": 0,
"concept_id": 4329188,
"concept_name": "Adverse reaction to ibuprofen",
"domain_id": "Condition",
"vocabulary_id": "SNOMED"
},
...
]
"""
if not isinstance(node_label, str) or not isinstance(dataset_id, int) or not isinstance(domain, str):
return []
handler = QueryCOHD.HANDLER_MAP['find_concept_id']
url_suffix = "q=" + node_label + "&dataset_id=" + str(dataset_id) + "&min_count=" + str(min_count)
if domain != "":
url_suffix += "&domain=" + domain
res_json = QueryCOHD.__access_api(handler, url_suffix)
results_list = []
if res_json is not None:
results = res_json.get('results', None)
if results is not None and type(results) == list:
results_list = results
return results_list
@staticmethod
def get_paired_concept_freq(concept_id1, concept_id2, dataset_id=1):
"""Retrieves observed clinical frequencies of a pair of concepts.
Args:
concept_id1 (str): an OMOP id, e.g., "192855"
concept_id2 (str): an OMOP id, e.g., "2008271"
dataset_id (str): The dataset_id of the dataset to query. Default dataset is the 5-year dataset
Returns:
dictionary: a dictionary which contains a numeric frequency and a numeric concept count, or None if no
frequency data could be obtained for the given pair of concept IDs
example:
{
"concept_count": 10,
"concept_frequency": 0.000005585247351056813,
"concept_id_1": 192855,
"concept_id_2": 2008271,
"dataset_id": 1
}
"""
if not isinstance(concept_id1, str) or not isinstance(concept_id2, str) or not isinstance(dataset_id, int):
return {}
handler = QueryCOHD.HANDLER_MAP['get_paired_concept_freq']
url_suffix = "q=" + urllib.parse.quote_plus(concept_id1 + ',' + concept_id2) + "&dataset_id=" + str(dataset_id)
res_json = QueryCOHD.__access_api(handler, url_suffix)
results_dict = {}
if res_json is not None:
results = res_json.get('results', None)
if results is not None and type(results) == list and len(results) > 0:
results_dict = results[0]
return results_dict
@staticmethod
def get_individual_concept_freq(concept_id, dataset_id=1):
"""Retrieves observed clinical frequencies of individual concepts.
Args:
concept_id (str): an OMOP id, e.g., "192855"
dataset_id (str): The dataset_id of the dataset to query. Default dataset is the 5-year dataset
Returns:
dictionary: a dictionary which contains a numeric frequency and a numeric concept count
example:
{
"concept_count": 368,
"concept_frequency": 0.0002055371025188907,
"concept_id": 192855,
"dataset_id": 1
}
"""
if not isinstance(concept_id, str):
return {}
handler = QueryCOHD.HANDLER_MAP['get_individual_concept_freq']
url_suffix = "q=" + urllib.parse.quote_plus(concept_id) + "&dataset_id=" + str(dataset_id)
res_json = QueryCOHD.__access_api(handler, url_suffix)
results_dict = {}
if res_json is not None:
results = res_json.get('results', None)
if results is not None and type(results) == list and len(results) > 0:
results_dict = results[0]
return results_dict
@staticmethod
def get_associated_concept_domain_freq(concept_id, domain, dataset_id=1):
"""Retrieves observed clinical frequencies of all pairs of concepts given a concept id restricted by domain of
the associated concept_id
Args:
concept_id (str): an OMOP id, e.g., "192855"
domain (str): An OMOP domain id, e.g., "Condition", "Drug", "Procedure", etc.
dataset_id (int): The dataset_id of the dataset to query. Default dataset is the 5-year dataset (1).
Returns:
array: an array which contains frequency dictionaries, or an empty array if no data obtained
example:
[
{
"associated_concept_id": 2213283,
"associated_concept_name": "Level IV - Surgical pathology, gross and microscopic examination Abortion - spontaneous/missed Artery, biopsy Bone marrow, biopsy Bone exostosis Brain/meninges, other than for tumor resection Breast, biopsy, not requiring microscopic evaluation of surgica",
"associated_domain_id": "Procedure",
"concept_count": 302,
"concept_frequency": 0.00016867447000191573,
"concept_id": 192855,
"dataset_id": 1
},
{
"associated_concept_id": 2211361,
"associated_concept_name": "Radiologic examination, chest, 2 views, frontal and lateral",
"associated_domain_id": "Procedure",
"concept_count": 257,
"concept_frequency": 0.00014354085692216007,
"concept_id": 192855,
"dataset_id": 1
},
...
]
"""
if not isinstance(concept_id, str) or not isinstance(domain, str) or not isinstance(dataset_id, int):
return []
handler = QueryCOHD.HANDLER_MAP['get_associated_concept_domain_freq']
url_suffix = 'concept_id=' + concept_id + '&domain=' + domain + '&dataset_id=' + str(dataset_id)
res_json = QueryCOHD.__access_api(handler, url_suffix)
results_array = []
if res_json is not None:
results_array = res_json.get('results', [])
return results_array
@staticmethod
def get_xref_from_OMOP(concept_id, mapping_targets, distance=2):
"""Cross-reference from an ontology to OMOP standard concepts
Attempts to map a concept from an external ontology to an OMOP standard concept ID using the EMBL-EBI
Ontology Xref Service (OxO): https://www.ebi.ac.uk/spot/oxo/index. This method maps from the OMOP standard
concept to an intermediate vocabulary included is OxO (ICD9CM, ICD10CM, SNOMEDCT, and MeSH), then uses the OxO
API to map to other ontologies. Multiple mappings may be returned. Results are sorted by total_distance (OxO
distance + OMOP distance) in ascending order.
Args:
concept_id (str): OMOP standard concept_id to map, e.g., "192855"
mapping_targets (str): Target ontologies for OxO. Comma separated target prefixes, e.g., "DOID, UMLS"
distance (int): Mapping distance for OxO. Note: this is the distance used in the OxO API to map from an
ICD9CM, ICD10CM, SNOMEDCT, or MeSH concept to the desired ontology. One additional step may be taken by the
COHD API to map to the OMOP standard concept to ICD9CM, ICD10CM, SNOMEDCT, or MeSH. Default: 2.
Returns:
array: an array which contains cross-reference dictionaries, or an empty array if no data obtained.
example:
[
{
"intermediate_omop_concept_code": "92546004",
"intermediate_omop_concept_id": 192855,
"intermediate_omop_concept_name": "Cancer in situ of urinary bladder",
"intermediate_omop_vocabulary_id": "SNOMED",
"intermediate_oxo_curie": "SNOMEDCT:92546004",
"intermediate_oxo_label": "Cancer in situ of urinary bladder",
"omop_distance": 0,
"oxo_distance": 1,
"source_omop_concept_code": "92546004",
"source_omop_concept_id": 192855,
"source_omop_concept_name": "Cancer in situ of urinary bladder",
"source_omop_vocabulary_id": "SNOMED",
"target_curie": "UMLS:C0154091",
"target_label": "Cancer in situ of urinary bladder",
"total_distance": 1
},
{
"intermediate_omop_concept_code": "D09.0",
"intermediate_omop_concept_id": 35206494,
"intermediate_omop_concept_name": "Carcinoma in situ of bladder",
"intermediate_omop_vocabulary_id": "ICD10CM",
"intermediate_oxo_curie": "ICD10CM:D09.0",
"intermediate_oxo_label": "Carcinoma in situ of bladder",
"omop_distance": 1,
"oxo_distance": 1,
"source_omop_concept_code": "92546004",
"source_omop_concept_id": 192855,
"source_omop_concept_name": "Cancer in situ of urinary bladder",
"source_omop_vocabulary_id": "SNOMED",
"target_curie": "UMLS:C0154091",
"target_label": "Cancer in situ of urinary bladder",
"total_distance": 2
},
...
]
"""
if not isinstance(concept_id, str) or not isinstance(mapping_targets, str) or not isinstance(distance, int):
return []
handler = QueryCOHD.HANDLER_MAP['get_xref_from_OMOP']
url_suffix = 'concept_id=' + concept_id + '&mapping_targets=' + mapping_targets + "&distance=" + str(distance)
res_json = QueryCOHD.__access_api(handler, url_suffix)
results_array = []
if res_json is not None:
results_array = res_json.get('results', [])
return results_array
@staticmethod
def get_xref_to_OMOP(curie, distance=2):
"""Cross-reference from an ontology to OMOP standard concepts
Attempts to map a concept from an external ontology to an OMOP standard concept ID using the EMBL-EBI
Ontology Xref Service (OxO): https://www.ebi.ac.uk/spot/oxo/index. This method attempts to use OxO to map from
the original ontology to an intermediate ontology that is included in OMOP (ICD9CM, ICD10CM, SNOMEDCT, and
MeSH), then uses the OMOP mappings to the standard concepts. Multiple mappings may be returned. Results are
sorted by total_distance (OxO distance + OMOP distance) in ascending order.
Args:
curie (str): Compacy URI (CURIE) of the concept to map, e.g., "DOID:8398"
distance (str): Mapping distance for OxO. Note: this is the distance used in the OxO API to map from the
original concept to an ICD9CM, ICD10CM, SNOMEDCT, or MeSH concept. One additional step may be taken by the
COHD API to map to the OMOP standard concept. Default: 2.
Returns:
array: an array which contains cross-reference dictionaries, or an empty array if no data obtained.
example:
[
{
"intermediate_oxo_id": "ICD9CM:715.3",
"intermediate_oxo_label": "",
"omop_concept_name": "Localized osteoarthrosis uncertain if primary OR secondary",
"omop_distance": 1,
"omop_domain_id": "Condition",
"omop_standard_concept_id": 72990,
"oxo_distance": 1,
"source_oxo_id": "DOID:8398",
"source_oxo_label": "osteoarthritis",
"total_distance": 2
},
{
"intermediate_oxo_id": "SNOMEDCT:396275006",
"intermediate_oxo_label": "Osteoarthritis",
"omop_concept_name": "Osteoarthritis",
"omop_distance": 0,
"omop_domain_id": "Condition",
"omop_standard_concept_id": 80180,
"oxo_distance": 2,
"source_oxo_id": "DOID:8398",
"source_oxo_label": "osteoarthritis",
"total_distance": 2
},
...
]
"""
if not isinstance(curie, str) or not isinstance(distance, int):
return []
handler = QueryCOHD.HANDLER_MAP['get_xref_to_OMOP']
url_suffix = 'curie=' + curie + "&distance=" + str(distance)
res_json = QueryCOHD.__access_api(handler, url_suffix)
results_array = []
if res_json is not None:
results_array = res_json.get('results', [])
return results_array
@staticmethod
def get_concepts(concept_ids):
"""Concept definitions from concept ID
Returns the OMOP concept names and domains for the given list of concept IDs.
Args:
concept_ids (array): concept id array, e.g., ["192855", "2008271"]
Returns:
array: an array which contains concept name, domain id and etc., or an empty array if no data obtained
example:
[
{
"concept_class_id": "Clinical Finding",
"concept_code": "92546004",
"concept_id": 192855,
"concept_name": "Cancer in situ of urinary bladder",
"domain_id": "Condition",
"vocabulary_id": "SNOMED"
},
{
"concept_class_id": "4-dig billing code",
"concept_code": "99.25",
"concept_id": 2008271,
"concept_name": "Injection or infusion of cancer chemotherapeutic substance",
"domain_id": "Procedure",
"vocabulary_id": "ICD9Proc"
},
...
]
"""
if not isinstance(concept_ids, list) or len(concept_ids) <= 0:
return []
for concept_id in concept_ids:
if not isinstance(concept_id, str):
return []
handler = QueryCOHD.HANDLER_MAP['get_concepts']
concept_ids_str = concept_ids[0]
for i, concept_id in enumerate(concept_ids):
if i > 0:
concept_ids_str += "," + concept_id
url_suffix = 'q=' + urllib.parse.quote_plus(concept_ids_str)
res_json = QueryCOHD.__access_api(handler, url_suffix)
results_array = []
if res_json is not None:
results_array = res_json.get('results', [])
return results_array
@staticmethod
def get_map_from_standard_concept_id(concept_id, vocabulary_id=""):
"""Map from a standard concept ID to concept code(s) in an external vocabulary.
Uses the OMOP concept_relationship table to map from a standard concept ID (e.g., 72990) to concept code(s)
(e.g., ICD9CM 715.3, 715.31, 715.32, etc.). An OMOP standard concept ID may map to many concepts in the external
vocabulary.
Args:
concept_id (str): The standard OMOP concept id to map from, e.g., "72990"
vocabulary_id (str): The vocabulary (e.g., "ICD9CM") to map to. If this parameter is not specified, the
method will return mappings to any matching vocabularies. See /omop/vocabularies for the list of supported
vocabularies.
Returns:
array: an array which contains mapping concepts from external vocabularies.
example:
[
{
"concept_class_id": "4-dig nonbill code",
"concept_code": "715.3",
"concept_id": 44834979,
"concept_name": "Osteoarthrosis, localized, not specified whether primary or secondary",
"domain_id": "Condition",
"standard_concept": null,
"vocabulary_id": "ICD9CM"
},
{
"concept_class_id": "5-dig billing code",
"concept_code": "715.30",
"concept_id": 44828036,
"concept_name": "Osteoarthrosis, localized, not specified whether primary or secondary, site unspecified",
"domain_id": "Condition",
"standard_concept": null,
"vocabulary_id": "ICD9CM"
},
...
]
"""
if not isinstance(concept_id, str) or not isinstance(vocabulary_id, str):
return []
handler = QueryCOHD.HANDLER_MAP['get_map_from_standard_concept_id']
url_suffix = 'concept_id=' + concept_id
if vocabulary_id != "":
url_suffix += "&vocabulary_id=" + vocabulary_id
res_json = QueryCOHD.__access_api(handler, url_suffix)
results_array = []
if res_json is not None:
results_array = res_json.get('results', [])
return results_array
@staticmethod
def get_map_to_standard_concept_id(concept_code, vocabulary_id=""):
"""Map from a non-standard concept code to a standard OMOP concept ID.
Args:
concept_code (str): The concept code to map from, e.g., "715.3"
vocabulary_id (str): The vocabulary (e.g., "ICD9CM") that the concept code belongs to. If this parameter is
not specified, the method will return mappings from any source vocabulary with matching concept code. See
/omop/vocabularies for the list of supported vocabularies.
Returns:
array: an array which contains standard OMOP concept IDs
example:
[
{
"source_concept_code": "715.3",
"source_concept_id": 44834979,
"source_concept_name": "Osteoarthrosis, localized, not specified whether primary or secondary",
"source_vocabulary_id": "ICD9CM",
"standard_concept_id": 72990,
"standard_concept_name": "Localized osteoarthrosis uncertain if primary OR secondary",
"standard_domain_id": "Condition"
},
...
]
"""
if not isinstance(concept_code, str) or not isinstance(vocabulary_id, str):
return []
handler = QueryCOHD.HANDLER_MAP['get_map_to_standard_concept_id']
url_suffix = 'concept_code=' + concept_code
if vocabulary_id != "":
url_suffix += "&vocabulary_id=" + vocabulary_id
res_json = QueryCOHD.__access_api(handler, url_suffix)
results_array = []
if res_json is not None:
results_array = res_json.get('results', [])
return results_array
@staticmethod
def get_vocabularies():
"""List of vocabularies.
List of vocabulary_ids. Useful if you need to use /omop/mapToStandardConceptID to map a concept code from a source vocabulary to the OMOP standard vocabulary.
Returns:
array: an array of all vocabularies
example:
[
{
"vocabulary_id": "ABMS"
},
{
"vocabulary_id": "AMT"
},
{
"vocabulary_id": "APC"
},
...
]
"""
handler = QueryCOHD.HANDLER_MAP['get_vocabularies']
url_suffix = ''
res_json = QueryCOHD.__access_api(handler, url_suffix)
results_array = []
if res_json is not None:
results_array = res_json.get('results', [])
return results_array
@staticmethod
def get_associated_concept_freq(concept_id, dataset_id=1):
"""Retrieves observed clinical frequencies of all pairs of concepts given a concept id. Results are returned in
descending order of paired concept count. Note that the largest paired concept counts are often dominated by
associated concepts with high prevalence.
Args:
concept_id (str): An OMOP concept id, e.g., "192855"
dataset_id (int): The dataset_id of the dataset to query. Default dataset is the 5-year dataset (1).
Returns:
array: an array which contains frequency dictionaries, or an empty array if no data obtained
example:
[
{
"associated_concept_id": 2213216,
"associated_concept_name": "Cytopathology, selective cellular enhancement technique with interpretation (eg, liquid based slide preparation method), except cervical or vaginal",
"associated_domain_id": "Measurement",
"concept_count": 330,
"concept_frequency": 0.0001843131625848748,
"concept_id": 192855,
"dataset_id": 1
},
{
"associated_concept_id": 4214956,
"associated_concept_name": "History of clinical finding in subject",
"associated_domain_id": "Observation",
"concept_count": 329,
"concept_frequency": 0.00018375463784976913,
"concept_id": 192855,
"dataset_id": 1
},
...
]
"""
if not isinstance(concept_id, str) or not isinstance(dataset_id, int):
return []
handler = QueryCOHD.HANDLER_MAP['get_associated_concept_freq']
url_suffix = 'q=' + concept_id + '&dataset_id=' + str(dataset_id)
res_json = QueryCOHD.__access_api(handler, url_suffix)
results_array = []
if res_json is not None:
results_array = res_json.get('results', [])
return results_array
@staticmethod
def get_most_frequent_concepts(num, domain="", dataset_id=1):
"""Retrieves the most frequent concepts.
Args:
num (int): The number of concepts to retreieve, e.g., "10"
domain (str): (Optional) The domain_id to restrict to, e.g., "Condition", "Drug", "Procedure"
dataset_id (int): The dataset_id of the dataset to query. Default dataset is the 5-year dataset.
Returns:
array: an array which contains frequency dictionaries
example:
[
{
"concept_class_id": "Clinical Finding",
"concept_count": 233790,
"concept_frequency": 0.1305774978203572,
"concept_id": 320128,
"concept_name": "Essential hypertension",
"dataset_id": 1,
"domain_id": "Condition",
"vocabulary_id": "SNOMED"
},
{
"concept_class_id": "Clinical Finding",
"concept_count": 152005,
"concept_frequency": 0.08489855235973907,
"concept_id": 77670,
"concept_name": "Chest pain",
"dataset_id": 1,
"domain_id": "Condition",
"vocabulary_id": "SNOMED"
},
]
"""
if not isinstance(num, int) or not isinstance(domain, str) or not isinstance(dataset_id, int) or num < 0:
return []
handler = QueryCOHD.HANDLER_MAP['get_most_frequent_concepts']
url_suffix = 'q=' + str(num) + '&dataset_id=' + str(dataset_id)
if domain != "":
url_suffix += "&domain=" + domain
res_json = QueryCOHD.__access_api(handler, url_suffix)
results_array = []
if res_json is not None:
results_array = res_json.get('results', [])
return results_array
@staticmethod
def get_chi_square(concept_id_1, concept_id_2='', domain='', dataset_id=1):
"""Returns the chi-square statistic and p-value between pairs of concepts. Results are returned in descending
order of the chi-square statistic. Note that due to large sample sizes, the chi-square can become very large.
The expected frequencies for the chi-square analysis are calculated based on the single concept frequencies and
assuming independence between concepts. P-value is calculated with 1 DOF.
This method has overloaded behavior based on the specified parameters:
1. concept_id_1 and concept_id_2: Result for the pair (concept_id_1, concept_id_2)
2. concept_id_1: Results for all pairs of concepts that include concept_id_1
3. concept_id_1 and domain: Results for all pairs of concepts including concept_id_1 and where concept_id_2
belongs to the specified domain
Args:
concept_id_1 (str): An OMOP concept id, e.g., "192855"
concept_id_2 (str): An OMOP concept id, e.g., "2008271". If this parameter is specified, then the chi-square
between concept_id_1 and concept_id_2 is returned. If this parameter is not specified, then a list of
chi-squared results between concept_id_1 and other concepts is returned.
domain (str): An OMOP domain id, e.g., "Condition", "Drug", "Procedure", etc., to restrict the associated
concept (concept_id_2) to. If this parameter is not specified, then the domain is unrestricted.
dataset_id (int): The dataset_id of the dataset to query. Default dataset is the 5-year dataset (1).
Returns:
array: an array of chi-square dictionaries.
example:
[
{
"chi_square": 306.2816108187519,
"concept_id_1": 192855,
"concept_id_2": 2008271,
"dataset_id": 1,
"p-value": 1.4101531778039801e-68
}
]
"""
if not isinstance(concept_id_1, str) or not isinstance(concept_id_2, str) or not isinstance(domain, str) or not isinstance(dataset_id, int):
return []
handler = QueryCOHD.HANDLER_MAP['get_chi_square']
url_suffix = 'concept_id_1=' + concept_id_1 + '&dataset_id=' + str(dataset_id)
if domain != "":
url_suffix += "&domain=" + domain
if concept_id_2 != "":
url_suffix += "&concept_id_2=" + concept_id_2
res_json = QueryCOHD.__access_api(handler, url_suffix)
results_array = []
if res_json is not None:
results_array = res_json.get('results', [])
return results_array
@staticmethod
def get_obs_exp_ratio(concept_id_1, concept_id_2="", domain="", dataset_id=1):
"""Returns the natural logarithm of the ratio between the observed count and expected count. Expected count is
calculated from the single concept frequencies and assuming independence between the concepts. Results are
returned in descending order of ln_ratio.
expected_count = Count_1_and_2 * num_patients / (Count_1 * Count_2)
ln_ratio = ln( expected_count )
This method has overloaded behavior based on the specified parameters:
1. concept_id_1 and concept_id_2: Results for the pair (concept_id_1, concept_id_2)
2. concept_id_1: Results for all pairs of concepts that include concept_id_1
3. concept_id_1 and domain: Results for all pairs of concepts including concept_id_1 and where concept_id_2
belongs to the specified domain
Args:
concept_id_1 (str): An OMOP concept id, e.g., "192855"
concept_id_2 (str): An OMOP concept id, e.g., "2008271". If concept_id_2 is unspecified, then this method
will return all pairs of concepts with concept_id_1.
domain (str): An OMOP domain id, e.g., "Condition", "Drug", "Procedure", etc., to restrict the associated
concept (concept_id_2) to. If this parameter is not specified, then the domain is unrestricted.
dataset_id (int): The dataset_id of the dataset to query. Default dataset is the 5-year dataset (1).
Returns:
array: an array of dictionaries which contains the natural logarithm of the ratio between the observed
count and expected count
example:
[
{
"concept_id_1": 192855,
"concept_id_2": 2008271,
"dataset_id": 1,
"expected_count": 0.3070724311632227,
"ln_ratio": 3.483256720088832,
"observed_count": 10
}
]
"""
if not isinstance(concept_id_1, str) or not isinstance(concept_id_2, str) or not isinstance(domain, str) or not isinstance(dataset_id, int):
return []
handler = QueryCOHD.HANDLER_MAP['get_obs_exp_ratio']
url_suffix = 'concept_id_1=' + concept_id_1 + '&dataset_id=' + str(dataset_id)
if domain != "":
url_suffix += "&domain=" + domain
if concept_id_2 != "":
url_suffix += "&concept_id_2=" + concept_id_2
res_json = QueryCOHD.__access_api(handler, url_suffix)
results_array = []
if res_json is not None:
results_array = res_json.get('results', [])
return results_array
@staticmethod
def get_relative_frequency(concept_id_1, concept_id_2="", domain="", dataset_id=1):
"""Relative frequency between pairs of concepts
Calculates the relative frequency (i.e., conditional probability) between pairs of concepts. Results are
returned in descending order of relative frequency. Note that due to the randomization of the counts, the
calcaulted relative frequencies can exceed the limit of 1.0.
Relative Frequency = Count_1_and_2 / Count_2
This method has overloaded behavior based on the specified parameters:
1. concept_id_1 and concept_id_2: Result for the pair (concept_id_1, concept_id_2)
2. concept_id_1: Results for all pairs of concepts that include concept_id_1
3. concept_id_1 and domain: Results for all pairs of concepts including concept_id_1 and where concept_id_2
belongs to the specified domain
Args:
concept_id_1 (str): An OMOP concept id, e.g., "192855"
concept_id_2 (str): An OMOP concept id, e.g., "2008271". If concept_id_2 is unspecified, then this method
will return all pairs of concepts with concept_id_1.
domain (str): An OMOP domain id, e.g., "Condition", "Drug", "Procedure", etc., to restrict the associated
concept (concept_id_2) to. If this parameter is not specified, then the domain is unrestricted.
dataset_id (int): The dataset_id of the dataset to query. Default dataset is the 5-year dataset (1).
Returns:
array: an array of dictionaries which contains the relative frequency between pairs of concepts
example:
[
{
"concept_2_count": 1494,
"concept_id_1": 192855,
"concept_id_2": 2008271,
"concept_pair_count": 10,
"dataset_id": 1,
"relative_frequency": 0.006693440428380187
}
]
"""
if not isinstance(concept_id_1, str) or not isinstance(concept_id_2, str) or not isinstance(domain, str) or not isinstance(dataset_id, int):
return []
handler = QueryCOHD.HANDLER_MAP['get_relative_frequency']
url_suffix = 'concept_id_1=' + concept_id_1 + '&dataset_id=' + str(dataset_id)
if domain != "":
url_suffix += "&domain=" + domain
if concept_id_2 != "":
url_suffix += "&concept_id_2=" + concept_id_2
res_json = QueryCOHD.__access_api(handler, url_suffix)
results_array = []
if res_json is not None:
results_array = res_json.get('results', [])
return results_array
@staticmethod
def get_datasets():
"""Enumerates the datasets available in COHD
Returns:
array: a list of datasets, including dataset ID, name, and description.
example:
[
{'dataset_description': "Clinical data from 2013-2017. Each concept's count reflects "
"the use of that specific concept.",
'dataset_id': 1,
'dataset_name': "5-year non-hierarchical"},
{'dataset_description': "Clinical data from all years in the database. Each concept's"
" count reflects the use of that specific concept.",
'dataset_id': 2,
'dataset_name': "Lifetime non-hierarchical"},
{"dataset_description": "Clinical data from 2013-2017. Each concept's count includes"
" use of that concept and descendant concepts.",
"dataset_id": 3,
"dataset_name": "5-year hierarchical"}
]
"""
handler = QueryCOHD.HANDLER_MAP['get_datasets']
url_suffix = ''
res_json = QueryCOHD.__access_api(handler, url_suffix)
results_array = []
if res_json is not None:
results_array = res_json.get('results', [])
return results_array
@staticmethod
def get_domain_counts(dataset_id=1):
"""The number of concepts in each domain
Args:
dataset_id (int): The dataset_id of the dataset to query. Default dataset is the 5-year dataset (1).
Returns:
array: a list of domains and the number of concepts in each domain.
"""
if not isinstance(dataset_id, int) or dataset_id <= 0:
return []
handler = QueryCOHD.HANDLER_MAP['get_domain_counts']
url_suffix = 'dataset_id=' + str(dataset_id)
res_json = QueryCOHD.__access_api(handler, url_suffix)
results_array = []
if res_json is not None:
results_array = res_json.get('results', [])
return results_array
@staticmethod
def get_domain_pair_counts(dataset_id=1):
"""The number of pairs of concepts in each pair of domains
Args:
dataset_id (int): The dataset_id of the dataset to query. Default dataset is the 5-year dataset (1).
Returns:
array: a list of pairs of domains and the number of pairs of concepts in each.
"""
if not isinstance(dataset_id, int) or dataset_id <= 0:
return []
handler = QueryCOHD.HANDLER_MAP['get_domain_pair_counts']
url_suffix = 'dataset_id=' + str(dataset_id)
res_json = QueryCOHD.__access_api(handler, url_suffix)
results_array = []
if res_json is not None:
results_array = res_json.get('results', [])
return results_array
@staticmethod
def get_patient_count(dataset_id=1):
"""The number of patients in the dataset
Args:
dataset_id (int): The dataset_id of the dataset to query. Default dataset is the 5-year dataset (1).
Returns:
array: a list of dictionaries which contains the number of patients
example:
[
{
"count": 1790431,
"dataset_id": 1
}
]
"""
if not isinstance(dataset_id, int) or dataset_id <= 0:
return {}
handler = QueryCOHD.HANDLER_MAP['get_patient_count']
url_suffix = 'dataset_id=' + str(dataset_id)
res_json = QueryCOHD.__access_api(handler, url_suffix)
results_dict = {}
if res_json is not None:
results = res_json.get('results', [])
if results is not None and type(results) == list and len(results) > 0:
results_dict = results[0]
return results_dict
@staticmethod
def get_concept_ancestors(concept_id, vocabulary_id='', concept_class_id='', dataset_id=3):
"""Retrieves the given concept's hierarchical ancestors and their counts.
Args:
concept_id (str): An OMOP concept id, e.g., "19019073"
vocabulary_id (int): The vocabulary_id to restrict ancestors to. For conditions, SNOMED and MedDRA are
used. For drugs, RxNorm (only and ATC are used. For procedures, SNOMED, MedDRA, and ICD10PCS are used.
Default: unrestricted.
concept_class_id(str): The concept_class_id to restrict ancestors to. Only certain hierarchical concept_
class_ids are used in each vocabuarly: ATC {ATC 1st, ATC 2nd, ATC 3rd, ATC 4th, ATC 5th}; MedDRA {PT,
HLT, HLGT, SOC}; RxNorm {Ingredient, Clinical Drug Form, Clinical Drug Comp, Clinical Drug}.
Default: unrestricted.
dataset_id(int): The dataset_id to retrieve counts from. Default: 3 (5-year hierarchical data set)
Returns:
array: a list of dictionaries which contains the number of patients
example:
[
{
"ancestor_concept_id": 19019073,
"concept_class_id": "Clinical Drug",
"concept_code": "197806",
"concept_count": 121104,
"concept_name": "Ibuprofen 600 MG Oral Tablet",
"domain_id": "Drug",
"max_levels_of_separation": 0,
"min_levels_of_separation": 0,
"standard_concept": "S",
"vocabulary_id": "RxNorm"
},
{
"ancestor_concept_id": 19081499,
"concept_class_id": "Clinical Drug Comp",
"concept_code": "316077",
"concept_count": 121202,
"concept_name": "Ibuprofen 600 MG",
"domain_id": "Drug",
"max_levels_of_separation": 1,
"min_levels_of_separation": 1,
"standard_concept": "S",
"vocabulary_id": "RxNorm"
},
...
]
"""
if not isinstance(concept_id, str) or not isinstance(vocabulary_id, str) \
or not isinstance(concept_class_id, str) or not isinstance(dataset_id, int) or dataset_id <= 0:
return []
handler = QueryCOHD.HANDLER_MAP['get_concept_ancestors']
url_suffix = 'concept_id=' + concept_id + '&dataset_id=' + str(dataset_id)
if vocabulary_id != '':
url_suffix += '&vocabulary_id=' + vocabulary_id
if concept_class_id != '':
url_suffix += '&concept_class_id=' + concept_class_id
res_json = QueryCOHD.__access_api(handler, url_suffix)
results_list = []
if res_json is not None:
results = res_json.get('results', [])
if results is not None and type(results) == list and len(results) > 0:
results_list = results
return results_list