def uwda_unit_tests(neu_id, umls_api_key): user = User(umls_api_key=umls_api_key) neu_anat = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=neu_id, identifier_type="NEU ID", source="UMLS") print("Getting UWDA IDs from NEU ID (%s):" % neu_id) for uwda in get_uwda_id(neu_anat, user=user): print("- " + str(uwda))
def snomed_unit_tests(hpo_id, umls_api_key): user = User(umls_api_key=umls_api_key) hpo_phen = gnomics.objects.compound.Compound( identifier=str(hpo_id), identifier_type="HPO ID", source="Human Phenotype Ontology") print("Getting SNOMED-CT IDs from HPO ID (%s):" % hpo_id) for sno in get_snomed_ct_id(hpo_phen, user=user): print("- " + str(sno))
def get_loc_sh(anat, user=None, source="umls"): loc_array = [] for ident in anat.identifiers: if ident["identifier_type"].lower() in ["lch", "lcsh", "library of congress sh", "library of congress subject heading", "loc sh"] and ident["identifier"] not in loc_array: loc_array.append(ident["identifier"]) if loc_array: return loc_array for iden in anat.identifiers: if iden["identifier_type"].lower() in ["neu", "neu id", "neu identifier", "neuronames brain hierarchy id", "neuronames brain hierarchy identifier"]: if source.lower() in ["umls", "all"]: anat_array = [] umls_tgt = User.umls_tgt(user) page_num = 0 base = "https://uts-ws.nlm.nih.gov/rest" ext = "/crosswalk/current/source/NEU/" + iden["identifier"] while True: tick = User.umls_st(umls_tgt) page_num += 1 query = {"ticket": tick, "pageNumber": page_num} r = requests.get(base+ext, params=query) r.encoding = 'utf-8' items = json.loads(r.text) json_data = items["result"] for rep in json_data: if rep["ui"] not in loc_array and rep["ui"] != "NONE": # Library of Congress. if rep["rootSource"] == "LCH": loc_array.append(rep["ui"]) gnomics.objects.anatomical_structure.AnatomicalStructure.add_identifier(anat, identifier=rep["ui"], identifier_type="Library of Congress Subject Heading", language=None, source="UMLS Metathesaurus") if not json_data: break return loc_array
def formula_unit_tests(chemspider_id, pubchem_cid, chemspider_security_token): if chemspider_security_token is not None: print("Creating user...") user = User(chemspider_security_token=chemspider_security_token) print("User created successfully.\n") chemspider_com = gnomics.objects.compound.Compound( identifier=str(chemspider_id), identifier_type="ChemSpider ID", source="ChemSpider") print("\nGetting molecular formula from ChemSpider ID (%s):" % chemspider_id) start = timeit.timeit() molec_array = get_molecular_formula(chemspider_com, user=user) end = timeit.timeit() print("\tTIME ELAPSED: %s seconds." % str(end - start)) print("\tRESULTS:") for com in molec_array: print("\t- %s" % str(com)) pubchem_com = gnomics.objects.compound.Compound( identifier=str(pubchem_cid), identifier_type="PubChem CID", source="PubChem") print("\nGetting molecular formula from PubChem CID (%s):" % pubchem_cid) start = timeit.timeit() molec_array = get_molecular_formula(pubchem_com) end = timeit.timeit() print("\tTIME ELAPSED: %s seconds." % str(end - start)) print("\tRESULTS:") for com in molec_array: print("\t- %s" % str(com)) else: print( "No user provided. Cannot test ChemSpider conversion without ChemSpider security token.\n" ) print("Continuing with PubChem CID conversion...\n") pubchem_com = gnomics.objects.compound.Compound( identifier=str(pubchem_cid), identifier_type="PubChem CID", source="PubChem") print("\nGetting molecular formula from PubChem CID (%s):" % pubchem_cid) start = timeit.timeit() molec_array = get_molecular_formula(pubchem_com) end = timeit.timeit() print("\tTIME ELAPSED: %s seconds." % str(end - start)) print("\tRESULTS:") for com in molec_array: print("\t- %s" % str(com))
def vernacular_name_unit_tests(eol_id, eol_api_key=None): print("Creating user...") user = User(eol_api_key=eol_api_key) print("User created successfully.\n") eol_tax = gnomics.objects.taxon.Taxon(identifier=str(eol_id), identifier_type="EOL ID", source="EOL") print("Getting EOL object from EOL ID (%s):" % eol_id) for iden in get_vernacular_names(eol_tax): print("- %s" % iden)
def chemspider_unit_tests(inchi_id, chemspider_security_token): user = User(chemspider_security_token=chemspider_security_token) inchi_compound = gnomics.objects.compound.Compound(identifier = str(inchi_id), identifier_type = "InChi", source = "PubChem") print("Getting ChemSpider ID from InChI (%s):" % inchi_id) start = timeit.timeit() cs_array = get_chemspider_id(inchi_compound, user = user) end = timeit.timeit() print("\tTIME ELAPSED: %s seconds." % str(end - start)) print("\tRESULTS:") for com in cs_array: print("\t- %s" % str(com))
def reference_pathway_unit_tests(pmid, openphacts_app_id, openphacts_app_key): user = User(openphacts_app_id=openphacts_app_id, openphacts_app_key=openphacts_app_key) pm_ref = gnomics.objects.reference.Reference(identifier=pmid, identifier_type="PubMed ID", source="OpenPHACTS") print("\nGetting pathway identifiers from PubMed ID (%s):" % pmid) for path in get_pathways(pm_ref, user=user): for iden in path.identifiers: print("- %s (%s)" % (str(iden["identifier"]), iden["identifier_type"]))
def drug_gene_unit_tests(drugbank_id, openphacts_app_id, openphacts_app_key): user = User(openphacts_app_id = openphacts_app_id, openphacts_app_key = openphacts_app_key) drugbank_drug = gnomics.objects.drug.Drug(identifier = drugbank_id, identifier_type = "DrugBank ID", source = "OpenPHACTS") start = timeit.timeit() all_genes = get_genes(drugbank_drug, user = user) end = timeit.timeit() print("TIME ELAPSED: %s seconds." % str(end - start)) for gene in all_genes: for iden in gene.identifiers: if iden["identifier_type"] == "HGNC Approved Symbol": print("- %s (%s)" % (iden["identifier"], iden["identifier_type"]))
def basic_search_unit_tests(basic_query, umls_api_key, ncbo_api_key): print("Beginning basic search for '%s'..." % basic_query) basic_search_results = search(basic_query, source="ebi") print( "\nSearch returned %s result(s) with the following identifiers (EBI):" % str(len(basic_search_results))) for anat in basic_search_results: for iden in anat.identifiers: print("- %s: %s (%s)" % (iden["identifier"], iden["name"], iden["identifier_type"])) user = User(umls_api_key=umls_api_key) start = timeit.timeit() basic_search_results = search(basic_query, source="umls", user=user) end = timeit.timeit() print("TIME ELAPSED: %s seconds." % str(end - start)) print( "\nSearch returned %s result(s) with the following identifiers (UMLS):" % str(len(basic_search_results))) for anat in basic_search_results: for iden in anat.identifiers: print("- %s: %s (%s)" % (iden["identifier"], iden["name"], iden["identifier_type"])) user = User(ncbo_api_key=ncbo_api_key) start = timeit.timeit() basic_search_results = search(basic_query, source="ncbo", user=user) end = timeit.timeit() print("TIME ELAPSED: %s seconds." % str(end - start)) print( "\nSearch returned %s result(s) with the following identifiers (NCBO):" % str(len(basic_search_results))) for anat in basic_search_results: for iden in anat.identifiers: print("- %s: %s (%s)" % (iden["identifier"], iden["name"], iden["identifier_type"]))
def mesh_unit_tests(meddra_id, umls_api_key, ncbo_api_key): user = User(umls_api_key=umls_api_key, ncbo_api_key=ncbo_api_key) meddra_ae = gnomics.objects.adverse_event.AdverseEvent( identifier=meddra_id, identifier_type="MedDRA ID", source="UMLS") print("Getting MeSH UIDs from MedDRA ID (%s):" % meddra_id) start = timeit.timeit() mesh_array = get_mesh_uid(meddra_ae, user=user) end = timeit.timeit() print("\tTIME ELAPSED: %s seconds." % str(end - start)) print("\tRESULTS:") for mesh in mesh_array: print("\t- %s" % str(mesh))
def icd10_unit_tests(kegg_disease_id, omim_disease_id, doid, omim_api_key=None): if omim_api_key is not None: print("Creating user...") user = User(omim_api_key=omim_api_key) print("User created successfully.\n") omim_disease = gnomics.objects.disease.Disease( identifier=str(omim_disease_id), identifier_type="MIM Number", source="OMIM") print("Getting ICD-10-CM IDs from MIM Number (%s):" % omim_disease_id) for icd in get_icd10(omim_disease, user=user): print("- " + str(icd)) kegg_disease = gnomics.objects.disease.Disease( identifier=str(kegg_disease_id), identifier_type="KEGG Disease ID", source="KEGG") print("\nGetting ICD-10-CM IDs from KEGG Disease ID (%s):" % kegg_disease_id) for icd10 in get_icd10(kegg_disease): print("- " + str(icd10)) doid_dis = gnomics.objects.disease.Disease(identifier=str(doid), identifier_type="DOID", source="Disease Ontology") print("\nGetting ICD-10-CM IDs from DOID (%s):" % doid) for icd10 in get_icd10(doid_dis): print("- " + str(icd10)) else: kegg_disease = gnomics.objects.disease.Disease( identifier=str(kegg_disease_id), identifier_type="KEGG Disease ID", source="KEGG") print("\nGetting ICD-10-CM IDs from KEGG Disease ID (%s):" % kegg_disease_id) for icd10 in get_icd10(kegg_disease): print("- " + str(icd10)) doid_dis = gnomics.objects.disease.Disease(identifier=str(doid), identifier_type="DOID", source="Disease Ontology") print("\nGetting ICD-10-CM IDs from DOID (%s):" % doid) for icd10 in get_icd10(doid_dis): print("- " + str(icd10))
def tissue_protein_unit_tests(caloha_id, openphacts_app_id, openphacts_app_key): user = User(openphacts_app_id=openphacts_app_id, openphacts_app_key=openphacts_app_key) caloha_tiss = gnomics.objects.tissue.Tissue(identifier=caloha_id, identifier_type="CALOHA ID", source="OpenPHACTS") print("\nGetting protein identifiers from CALOHA identifier (%s):" % caloha_id) for prot in get_proteins(caloha_tiss, user=user): for iden in prot.identifiers: print("- %s (%s)" % (str(iden["identifier"]), iden["identifier_type"]))
def psy_unit_tests(neu_id, uwda_id, umls_api_key): user = User(umls_api_key=umls_api_key) neu_anat = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=neu_id, identifier_type="NEU ID", source="UMLS") print("Getting PSY ID from NEU ID (%s):" % neu_id) for psy in get_psy(neu_anat, user=user): print("- " + str(psy)) uwda_anat = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=uwda_id, identifier_type="UWDA ID", source="UMLS") print("\nGetting PSY ID from UWDA ID (%s):" % uwda_id) for psy in get_psy(uwda_anat, user=user): print("- " + str(psy))
def basic_search_unit_tests(basic_query, umls_api_key): user = User(umls_api_key=umls_api_key) print("Beginning basic searches for '%s'..." % basic_query) start = timeit.timeit() basic_search_results = search(basic_query, user, search_type="exact") end = timeit.timeit() print("TIME ELAPSED: %s seconds." % str(end - start)) print( "\nSearch returned %s exact result(s) with the following procedure identifiers:" % str(len(basic_search_results))) for proc in basic_search_results: for iden in proc.identifiers: print("- %s (%s)" % (iden["identifier"], iden["identifier_type"]))
def snomed_unit_tests(neu_id, uwda_id, umls_api_key): user = User(umls_api_key=umls_api_key) neu_anat = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=neu_id, identifier_type="NEU ID", source="UMLS") print("Getting SNOMED identifiers from NEU ID (%s):" % neu_id) for snomed in get_snomed(neu_anat, user=user): print("- " + str(snomed)) uwda_anat = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=uwda_id, identifier_type="UWDA ID", source="UMLS") print("\nGetting SNOMED identifiers from UWDA ID (%s):" % uwda_id) for snomed in get_snomed(uwda_anat, user=user): print("- " + str(snomed))
def cid_unit_tests(chemspider_id, kegg_compound_id, wikidata_accession, chemspider_security_token=None): if chemspider_security_token is not None: print("\nCreating user...") user = User(chemspider_security_token = chemspider_security_token) print("User created successfully.\n") chemspider_com = gnomics.objects.compound.Compound(identifier = str(chemspider_id), identifier_type = "ChemSpider ID", source = "ChemSpider") print("Getting PubChem CID from ChemSpider ID (%s):" % chemspider_id) start = timeit.timeit() cid_array = get_pubchem_cids(chemspider_com, user = user) end = timeit.timeit() print("\tTIME ELAPSED: %s seconds." % str(end - start)) print("\tRESULTS:") for com in cid_array: print("\t- %s" % str(com)) kegg_com = gnomics.objects.compound.Compound(identifier = str(kegg_compound_id), identifier_type = "KEGG Compound ID", source = "KEGG") print("\nGetting PubChem CID from KEGG Compound ID (%s):" % kegg_compound_id) start = timeit.timeit() cid_array = get_pubchem_cids(kegg_com, user = user) end = timeit.timeit() print("\tTIME ELAPSED: %s seconds." % str(end - start)) print("\tRESULTS:") for com in cid_array: print("\t- %s" % str(com)) wikidata_com = gnomics.objects.compound.Compound(identifier = str(wikidata_accession), identifier_type = "Wikidata Accession", source = "Wikidata") print("\nGetting PubChem CID from Wikidata Accession (%s):" % wikidata_accession) start = timeit.timeit() cid_array = get_pubchem_cids(wikidata_com) end = timeit.timeit() print("\tTIME ELAPSED: %s seconds." % str(end - start)) print("\tRESULTS:") for com in cid_array: print("\t- %s" % str(com)) else: print("No user provided. Cannot test ChemSpider conversion without ChemSpider security token.\n") print("Continuing with KEGG Compound conversion...\n") kegg_com = gnomics.objects.compound.Compound(identifier = str(kegg_compound_id), identifier_type = "KEGG Compound ID", source = "KEGG") print("Getting PubChem CID from KEGG Compund ID (%s):" % kegg_compound_id) start = timeit.timeit() cid_array = get_pubchem_cids(kegg_com) end = timeit.timeit() print("\tTIME ELAPSED: %s seconds." % str(end - start)) print("\tRESULTS:") for com in cid_array: print("\t- %s" % str(com))
def basic_search_unit_tests(basic_query, email): user = User(email=email) print("Beginning basic searches for '%s'..." % basic_query) start = timeit.timeit() basic_search_results = search(basic_query, source="all", user=user) end = timeit.timeit() print("TIME ELAPSED: %s seconds." % str(end - start)) print( "\nEntrez search returned %s result(s) with the following identifiers:" % str(len(basic_search_results))) for ent in basic_search_results: for iden in ent.identifiers: print("- %s: %s (%s) [%s]" % (iden["identifier"], iden["name"], iden["identifier_type"], iden["taxon"]))
def rcd_unit_tests(neu_id, uwda_id, umls_api_key): user = User(umls_api_key=umls_api_key) neu_anat = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=neu_id, identifier_type="NEU ID", source="UMLS") print("Getting read codes from NEU ID (%s):" % neu_id) for rcd in get_read_codes(neu_anat, user=user): print("- " + str(rcd)) uwda_anat = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=uwda_id, identifier_type="UWDA ID", source="UMLS") print("\nGetting read codes from UWDA ID (%s):" % uwda_id) for rcd in get_read_codes(uwda_anat, user=user): print("- " + str(rcd))
def conceptwiki_unit_tests(conceptwiki_id, chemspider_id, chembl_id, schembl_id, openphacts_app_id, openphacts_app_key): user = User(openphacts_app_id=openphacts_app_id, openphacts_app_key=openphacts_app_key) conceptwiki_com = gnomics.objects.compound.Compound( identifier=conceptwiki_id, identifier_type="ConceptWiki ID", source="OpenPHACTS") chemspider_com = gnomics.objects.compound.Compound( identifier=chemspider_id, identifier_type="ChemSpider ID", source="OpenPHACTS") print("Getting ConceptWiki IDs from ChemSpider ID (%s):" % chemspider_id) start = timeit.timeit() conceptwiki_array = get_conceptwiki_id(chemspider_com, user=user) end = timeit.timeit() print("\tTIME ELAPSED: %s seconds." % str(end - start)) print("\tRESULTS:") for com in conceptwiki_array: print("\t- %s" % str(com)) chembl_com = gnomics.objects.compound.Compound(identifier=chembl_id, identifier_type="ChEMBL ID", source="OpenPHACTS") print("\nGetting ConceptWiki IDs from ChEMBL ID (%s):" % chembl_id) start = timeit.timeit() conceptwiki_array = get_conceptwiki_id(chembl_com, user=user) end = timeit.timeit() print("\tTIME ELAPSED: %s seconds." % str(end - start)) print("\tRESULTS:") for com in conceptwiki_array: print("\t- %s" % str(com)) schembl_com = gnomics.objects.compound.Compound( identifier=schembl_id, identifier_type="SCHEMBL ID", source="OpenPHACTS") print("\nGetting ConceptWiki IDs from SCHEMBL ID (%s):" % schembl_id) start = timeit.timeit() conceptwiki_array = get_conceptwiki_id(schembl_com, user=user) end = timeit.timeit() print("\tTIME ELAPSED: %s seconds." % str(end - start)) print("\tRESULTS:") for com in conceptwiki_array: print("\t- %s" % str(com))
def drug_adverse_event_unit_tests(rxcui, fda_api_key): user = User(fda_api_key=fda_api_key) rx_com = gnomics.objects.compound.Compound(identifier=str(rxcui), identifier_type="RxCUI", source="RxNorm") print("Getting adverse events from RxCUI (%s):" % rxcui) start = timeit.timeit() all_aes = get_adverse_events(rx_com, user=user, exact=False, counts=True, details=False) end = timeit.timeit() print("TIME ELAPSED: %s seconds." % str(end - start)) for ae_name, ae_count in all_aes.items(): print("- %s: %s" % (ae_name, str(ae_count)))
def aids_unit_tests(pubchem_aid, bioassay_id, openphacts_app_id, openphacts_app_key): assay = gnomics.objects.assay.Assay(identifier=pubchem_aid, identifier_type="PubChem AID", language=None, source="PubChem") print(get_pubchem_assay(assay)) user = User(openphacts_app_id=openphacts_app_id, openphacts_app_key=openphacts_app_key) bio_assay = gnomics.objects.assay.Assay(identifier=bioassay_id, identifier_type="OIDD Bioassay ID", source="OIDD") print("\nGetting PubChem AIDs from OIDD Bioassay ID (%s):" % bioassay_id) for iden in get_aids(bio_assay, user=user): print("- %s" % iden)
def basic_search_unit_tests(basic_query, ncbo_api_key): user = User(ncbo_api_key=ncbo_api_key) print("Beginning basic search for '%s'..." % basic_query) start = timeit.timeit() basic_search_results = search(basic_query, source="all", user=user) end = timeit.timeit() print("TIME ELAPSED: %s seconds." % str(end - start)) print("\nSearch returned %s result(s) with the following identifiers:" % str(len(basic_search_results))) for ae in basic_search_results: for iden in ae.identifiers: print("- %s (%s) [%s]" % (iden["identifier"], iden["name"], iden["identifier_type"]))
def drug_compound_unit_tests(drugbank_id, openphacts_app_id, openphacts_app_key): user = User(openphacts_app_id=openphacts_app_id, openphacts_app_key=openphacts_app_key) drugbank_drug = gnomics.objects.drug.Drug(identifier=drugbank_id, identifier_type="DrugBank ID", source="OpenPHACTS") start = timeit.timeit() all_coms = get_compounds(drugbank_drug, user=user) end = timeit.timeit() print("TIME ELAPSED: %s seconds." % str(end - start)) print("\nGetting compound identifiers from DrugBank ID (%s):" % drugbank_id) for com in all_coms: for iden in com.identifiers: print("- %s (%s)" % (str(iden["identifier"]), iden["identifier_type"]))
def adverse_event_phenotype_unit_tests(meddra_term, umls_api_key, ncbo_api_key): user = User(umls_api_key=umls_api_key, ncbo_api_key=ncbo_api_key) print("\nGetting HPO IDs from MedDRA Term (%s):" % meddra_term) meddra_ae = gnomics.objects.adverse_event.AdverseEvent( identifier=meddra_term, identifier_type="MedDRA Term", source="UMLS", language="en") start = timeit.timeit() phenotypes = get_phenotypes(meddra_ae, user=user) end = timeit.timeit() print("TIME ELAPSED: %s seconds." % str(end - start)) for phen in phenotypes: for iden in phen.identifiers: print("- %s (%s)" % (str(iden["identifier"]), iden["identifier_type"]))
def uberon_unit_tests(caloha_id, bto_id, openphacts_app_id, openphacts_app_key, ncbo_api_key): user = User(openphacts_app_id=openphacts_app_id, openphacts_app_key=openphacts_app_key, ncbo_api_key=ncbo_api_key) caloha_tiss = gnomics.objects.tissue.Tissue(identifier=caloha_id, identifier_type="CALOHA ID", source="OpenPHACTS") print("Getting UBERON IDs from CALOHA ID (%s):" % caloha_id) for uberon in get_uberon_id(caloha_tiss, user=user): print("- %s" % uberon) bto_tiss = gnomics.objects.tissue.Tissue(identifier=bto_id, identifier_type="BTO ID", source="OpenPHACTS") print("\nGetting UBERON IDs from BTO ID (%s):" % bto_id) for uberon in get_uberon_id(bto_tiss, user=user): print("- %s" % uberon)
def doid_unit_tests(omim_disease_id, doid, omim_api_key=None): if omim_api_key is not None: print("Creating user...") user = User(omim_api_key=omim_api_key) print("User created successfully.\n") omim_disease = gnomics.objects.disease.Disease( identifier=str(omim_disease_id), identifier_type="MIM Number", source="OMIM") print("Getting Disease Ontology IDs from MIM Number (%s):" % omim_disease_id) for sno in get_doid(omim_disease, user=user): print("- " + str(sno)) doid_dis = gnomics.objects.disease.Disease(identifier=str(doid), identifier_type="DOID", source="Disease Ontology") print("\nGetting DO terms from Disease Ontology ID (%s):" % doid) for term in get_do_terms(doid_dis): print("- " + str(term))
def disease_patent_unit_tests(mesh_uid, openphacts_app_id, openphacts_app_key): user = User(openphacts_app_id=openphacts_app_id, openphacts_app_key=openphacts_app_key) mesh_disease = gnomics.objects.disease.Disease(identifier=mesh_uid, identifier_type="MeSH UID", source="OpenPHACTS") print("\nGetting patent identifiers from MeSH UID (%s):" % mesh_uid) for pat in get_patents(mesh_disease, user=user): print(pat) start = timeit.timeit() all_pats = get_patents(mesh_disease, user=user, count_only=True) end = timeit.timeit() print("TIME ELAPSED: %s seconds." % str(end - start)) print("\nGetting number of patent identifiers from MeSH UID (%s):" % mesh_uid) for pat in all_pats: print("- %s" % str(pat))
def reference_person_unit_tests(pmid, doi, orcid_client_id, orcid_client_secret, elsevier_api_key): user = User(elsevier_api_key=elsevier_api_key) pubmed_ref = gnomics.objects.reference.Reference(identifier=pmid, identifier_type="PMID", language=None, source="PubMed") print("Getting authors from PMID (%s):" % pmid) for auth in get_authors(pubmed_ref, user=user): for iden in auth.identifiers: print("- %s (%s)" % (str(iden["identifier"]), iden["identifier_type"])) doi_ref = gnomics.objects.reference.Reference(identifier=doi, identifier_type="DOI", language=None, source="Nature") print("\nGetting authors from DOI (%s):" % doi) for auth in get_authors(doi_ref, user=user): for iden in auth.identifiers: print("- %s (%s)" % (str(iden["identifier"]), iden["identifier_type"]))
def fma_unit_tests(neu_id, uwda_id, wikidata_accession, umls_api_key): user = User(umls_api_key=umls_api_key) neu_anat = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=neu_id, identifier_type="NEU ID", source="UMLS") print("Getting FMA ID from NEU ID (%s):" % neu_id) for fma in get_fma_id(neu_anat, user=user): print("- " + str(fma)) uwda_anat = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=uwda_id, identifier_type="UWDA ID", source="UMLS") print("\nGetting FMA ID from UWDA ID (%s):" % uwda_id) for fma in get_fma_id(uwda_anat, user=user): print("- " + str(fma)) wikidata_anat = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=wikidata_accession, identifier_type="Wikidata Accession", language=None, source="Wikidata") print("\nGetting FMA ID from Wikidata Accession (%s):" % wikidata_accession) for fma in get_fma_id(wikidata_anat): print("- %s" % fma)
def umls_crosswalk(user, from_source, to_source, source_id, other="id", verbose=False): found_array = [] if user is None: print( "A valid user object with a valid UMLS API key is required for this function." ) return found_array elif user.umls_api_key is None: print( "A valid UMLS API key associated with the user object is required for this function." ) return found_array umls_tgt = User.umls_tgt(user) page_num = 0 base = "https://uts-ws.nlm.nih.gov/rest" ext = "/crosswalk/current/source/" + from_source.upper() + "/" + source_id verbose = True while True: tick = User.umls_st(umls_tgt) page_num += 1 query = {"ticket": tick, "pageNumber": page_num} try: r = requests.get( base + ext, params=query, headers={ "Content-Type": "application/json", "User-Agent": "Mozilla/5.0 (Windows NT 6.0; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0" }) r.encoding = 'utf-8' try: items = json.loads(r.text) json_data = items["result"] empty = False for rep in json_data: if other == "id": if rep["ui"] not in found_array and rep["ui"] != "NONE": if rep["rootSource"] == to_source.upper(): found_array.append(rep["ui"]) elif other == "name": if (rep["name"] not in found_array and rep["name"] != "NONE") and (rep["ui"] not in found_array and rep["ui"] != "NONE"): if rep["rootSource"] == to_source.upper(): found_array.append((rep["ui"], rep["name"])) else: print( "This value for 'other' is not currently supported." ) if "results" in json_data: if json_data["results"][0]["ui"] == "NONE": empty = True break if not json_data: break elif empty: break except ValueError as e: if verbose: print( "A value error occurred while attempting to load resulting JSON." ) print("ERROR: %s" % str(e)) break except: if verbose: print("Some other unknown error occurred.") break else: continue except ConnectionError as e: if verbose: print("A connection error occurred while attempting request.") print("ERROR: %s" % str(e)) break except requests.exceptions.ConnectionError as e: if verbose: print("A connection error occurred while attempting request.") print("ERROR: %s" % str(e)) break except HTTPException as e: if verbose: print( "An HTTP exception error occurred while attempting request." ) print("ERROR: %s" % str(e)) break except ProtocolError as e: if verbose: print("A protocol error occurred while attempting request.") print("ERROR: %s" % str(e)) break except urllib3.exceptions.ProtocolError as e: if verbose: print("A protocol error occurred while attempting request.") print("ERROR: %s" % str(e)) break except SysCallError as e: if verbose: print("A system call error occurred while attempting request.") print("ERROR: %s" % str(e)) break except OpenSSL.SSL.SysCallError as e: if verbose: print("A system call error occurred while attempting request.") print("ERROR: %s" % str(e)) break except ssl.SSLError as e: if verbose: print("A system call error occurred while attempting request.") print("ERROR: %s" % str(e)) break except SSLError as e: if verbose: print("A SSL error occurred while attempting request.") print("ERROR: %s" % str(e)) break except requests.exceptions.SSLError as e: if verbose: print("A SSL error occurred while attempting request.") print("ERROR: %s" % str(e)) break except BadStatusLine as e: if verbose: print( "A bad status line error occurred while attempting request." ) print("ERROR: %s" % str(e)) break except httplib.BadStatusLine as e: if verbose: print( "A bad status line error occurred while attempting request." ) print("ERROR: %s" % str(e)) break except http.client.BadStatusLine as e: if verbose: print("A protocol error occurred while attempting request.") print("ERROR: %s" % str(e)) break except MaxRetryError as e: if verbose: print("A max retry error occurred while attempting request.") print("ERROR: %s" % str(e)) break except urllib3.exceptions.MaxRetryError as e: if verbose: print("A max retry error occurred while attempting request.") print("ERROR: %s" % str(e)) break except: print("Some other HTTP/URL error encountered.") break else: break return found_array
def get_snomed_ct_id(phen, user = None): if user is not None: umls_tgt = User.umls_tgt(user) phen_array = [] for ident in phen.identifiers: if ident["identifier_type"].lower() in ["sct id", "sct identifier", "sctid", "snomed ct", "snomed ct concept id", "snomed id", "snomed identifier", "snomed-ct", "snomed-ct concept id", "snomed-ct id", "snomed-ct identifier"]: phen_array.append(ident["identifier"]) if phen_array: return phen_array ids_completed = [] for ident in phen.identifiers: if (ident["identifier_type"].lower() in ["hp code", "hp id", "hp identifier", "hpo code", "hpo id", "hpo identifier", "human phenotype ontology code", "human phenotype ontology id", "human phenotype ontology identifier", "hp", "hpo", "human phenotype ontology"]) and user is not None: stringy = ident["identifier"] page_num = 0 base = "https://uts-ws.nlm.nih.gov/rest" ext = "/crosswalk/current/source/HPO/" + ident["identifier"] + "?targetSource=SNOMEDCT_US" while True: tick = User.umls_st(umls_tgt) page_num += 1 query = {"ticket": tick, "pageNumber": page_num} r = requests.get(base+ext, params=query) r.encoding = 'utf-8' items = json.loads(r.text) json_data = items["result"] for er in json_data: if er["ui"] not in phen_array and er["ui"] != "NONE": snomedct = er["ui"] phen_array.append(snomedct) if not json_data: break elif (ident["identifier_type"].lower() in ["hp code", "hp id", "hp identifier", "hpo code", "hpo id", "hpo identifier", "human phenotype ontology code", "human phenotype ontology id", "human phenotype ontology identifier", "hp", "hpo", "human phenotype ontology"]) and user is None: if ident["identifier"] not in ids_completed: ids_completed.append(ident["identifier"]) hpo_id = ident["identifier"] if ":" in hpo_id: hpo_id = hpo_id.replace(":", "_") url = "https://www.ebi.ac.uk/ols/api/ontologies" ext = "/hp/terms/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252F" + hpo_id r = requests.get(url+ext, headers={"Content-Type": "application/json"}) if not r.ok: print("Something went wrong.") else: decoded = r.json() for xref in decoded["annotation"]["database_cross_reference"]: if "SNOMEDCT_US:" in xref: snomed_id = xref.split("SNOMEDCT_US:")[1] phen_array.append(snomed_id) gnomics.objects.phenotype.Phenotype.add_identifier(phen, identifier = snomed_id, identifier_type = "SNOMED-CT ID", source = "OLS") return phen_array
def get_aod(anat, user=None, source="umls"): aod_array = [] for iden in gnomics.objects.auxiliary_files.identifier.filter_identifiers(anat.identifiers, ["alcohol and other drug", "alcohol and other drug id", "alcohol and other drug identifier", "aod", "aod id", "aod identifier"]): if iden["identifier"] not in aod_array: aod_array.append(iden["identifier"]) if aod_array: return aod_array ids_completed = [] for iden in gnomics.objects.auxiliary_files.identifier.filter_identifiers(anat.identifiers, ["neu id", "neu identifier", "neuronames brain hierarchy id", "neuronames brain hierarchy identifier", "neu"]): if iden["identifier"] not in ids_completed and user is not None: ids_completed.append(iden["identifier"]) if source.lower() in ["umls", "all"]: umls_tgt = User.umls_tgt(user) page_num = 0 base = "https://uts-ws.nlm.nih.gov/rest" ext = "/crosswalk/current/source/NEU/" + str(iden["identifier"]) while True: tick = User.umls_st(umls_tgt) page_num += 1 query = {"ticket": tick, "pageNumber": page_num} r = requests.get(base+ext, params=query) r.encoding = 'utf-8' items = json.loads(r.text) json_data = items["result"] for rep in json_data: if rep["ui"] != "NONE": # AOD ID. if rep["rootSource"] == "AOD" and rep["ui"] not in aod_array: aod_array.append(rep["ui"]) gnomics.objects.anatomical_structure.AnatomicalStructure.add_identifier(anat, identifier=rep["ui"], identifier_type="AOD ID", language=None, source="UMLS Metathesaurus") if not json_data: break for iden in gnomics.objects.auxiliary_files.identifier.filter_identifiers(anat.identifiers, ["uwda", "uwda id", "uwda identifier"]): if iden["identifier"] not in ids_completed and user is not None: ids_completed.append(iden["identifier"]) if source.lower() in ["umls", "all"]: umls_tgt = User.umls_tgt(user) page_num = 0 base = "https://uts-ws.nlm.nih.gov/rest" ext = "/crosswalk/current/source/UWDA/" + str(iden["identifier"]) while True: tick = User.umls_st(umls_tgt) page_num += 1 query = {"ticket": tick, "pageNumber": page_num} r = requests.get(base+ext, params=query) r.encoding = 'utf-8' items = json.loads(r.text) json_data = items["result"] for rep in json_data: if rep["ui"] != "NONE": # AOD ID. if rep["rootSource"] == "AOD" and rep["ui"] not in aod_array: aod_array.append(rep["ui"]) gnomics.objects.anatomical_structure.AnatomicalStructure.add_identifier(anat, identifier=rep["ui"], identifier_type="AOD ID", language=None, source="UMLS Metathesaurus") if not json_data: break return aod_array
def get_mesh_uid(phen, user=None): mesh_array = [] for ident in phen.identifiers: if ident["identifier_type"].lower() in ["mesh", "mesh uid", "mesh unique id", "mesh unique identifier", "msh", "msh uid", "msh unique id", "msh unique identifier"]: if ident["identifier"] not in mesh_array: mesh_array.append(ident["identifier"]) if mesh_array: return mesh_array meddra_array = [] ids_completed = [] for ident in phen.identifiers: if ident["identifier_type"].lower() in ["mdr", "mdr code", "mdr id", "mdr identifier", "meddra", "meddra code", "meddra id", "meddra identifier"]: if user is not None: if ident["identifier"] not in meddra_array: umls_tgt = User.umls_tgt(user) page_num = 0 base = "https://uts-ws.nlm.nih.gov/rest" ext = "/crosswalk/current/source/MDR/" + str(ident["identifier"]) + "?targetSource=MSH" while True: tick = User.umls_st(umls_tgt) page_num += 1 query = {"string": ident["identifier"], "ticket": tick, "pageNumber": page_num} r = requests.get(base+ext, params=query) r.encoding = 'utf-8' try: items = json.loads(r.text) json_data = items["result"] empty = False for rep in json_data: if rep["ui"] not in mesh_array and rep["ui"] != "NONE": gnomics.objects.phenotype.Phenotype.add_identifier(phen, identifier = rep["ui"], identifier_type = "MeSH ID", source = "UMLS") mesh_array.append(rep["ui"]) if "ui" not in rep and not rep["result"]: empty = True break if not json_data: break if empty: break except: break break meddra_array.append(ident["identifier"]) elif ident["identifier_type"].lower() in ["hp code", "hp id", "hp identifier", "hpo code", "hpo id", "hpo identifier", "human phenotype ontology code", "human phenotype ontology id", "human phenotype ontology identifier", "hp", "hpo", "human phenotype ontology"]: if ident["identifier"] not in ids_completed: ids_completed.append(ident["identifier"]) hpo_id = ident["identifier"] if ":" in hpo_id: hpo_id = hpo_id.replace(":", "_") url = "https://www.ebi.ac.uk/ols/api/ontologies" ext = "/hp/terms/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252F" + hpo_id r = requests.get(url+ext, headers={"Content-Type": "application/json"}) if not r.ok: print("Something went wrong.") else: decoded = r.json() for xref in decoded["annotation"]["database_cross_reference"]: if "MSH:" in xref: mesh_uid = xref.split("MSH:")[1] mesh_array.append(mesh_uid) gnomics.objects.phenotype.Phenotype.add_identifier(phen, identifier = mesh_uid, identifier_type = "MeSH UID", source = "OLS") return mesh_array
def get_psy(anat, user=None, source="umls"): anat_array = [] for iden in anat.identifiers: if iden["identifier_type"].lower() in ["neu id", "neu identifier", "neu"]: if source.lower() in ["umls", "all"]: umls_tgt = User.umls_tgt(user) page_num = 0 base = "https://uts-ws.nlm.nih.gov/rest" ext = "/crosswalk/current/source/NEU/" + iden["identifier"] while True: tick = User.umls_st(umls_tgt) page_num += 1 query = {"ticket": tick, "pageNumber": page_num} r = requests.get(base+ext, params=query) r.encoding = 'utf-8' items = json.loads(r.text) json_data = items["result"] for rep in json_data: if rep["ui"] not in anat_array and rep["ui"] != "NONE": # PSY ID. if rep["rootSource"] == "PSY": anat_array.append(rep["ui"]) gnomics.objects.anatomical_structure.AnatomicalStructure.add_identifier(anat, identifier=rep["ui"], identifier_type="PSY ID", language=None, source="UMLS Metathesaurus", name=rep["name"]) if not json_data: break elif iden["identifier_type"].lower() in ["uwda id", "uwda identifier", "uwda"]: if source.lower() in ["umls", "all"]: umls_tgt = User.umls_tgt(user) page_num = 0 base = "https://uts-ws.nlm.nih.gov/rest" ext = "/crosswalk/current/source/UWDA/" + iden["identifier"] while True: tick = User.umls_st(umls_tgt) page_num += 1 query = {"ticket": tick, "pageNumber": page_num} r = requests.get(base+ext, params=query) r.encoding = 'utf-8' items = json.loads(r.text) json_data = items["result"] for rep in json_data: if rep["ui"] not in anat_array and rep["ui"] != "NONE": # PSY ID. if rep["rootSource"] == "PSY": anat_array.append(rep["ui"]) gnomics.objects.anatomical_structure.AnatomicalStructure.add_identifier(anat, identifier=rep["ui"], identifier_type="PSY ID", language=None, source="UMLS Metathesaurus", name=rep["name"]) if not json_data: break elif iden["identifier_type"].lower() in ["rcd id", "rcd identifier", "rcd"]: if source.lower() in ["umls", "all"]: umls_tgt = User.umls_tgt(user) page_num = 0 base = "https://uts-ws.nlm.nih.gov/rest" ext = "/crosswalk/current/source/RCD/" + iden["identifier"] while True: tick = User.umls_st(umls_tgt) page_num += 1 query = {"ticket": tick, "pageNumber": page_num} r = requests.get(base+ext, params=query) r.encoding = 'utf-8' items = json.loads(r.text) json_data = items["result"] for rep in json_data: if rep["ui"] not in anat_array and rep["ui"] != "NONE": # PSY ID. if rep["rootSource"] == "PSY": anat_array.append(rep["ui"]) gnomics.objects.anatomical_structure.AnatomicalStructure.add_identifier(anat, identifier=rep["ui"], identifier_type="PSY ID", language=None, source="UMLS Metathesaurus", name=rep["name"]) if not json_data: break elif iden["identifier_type"].lower() in ["aod id", "aod identifier", "aod"]: if source.lower() in ["umls", "all"]: umls_tgt = User.umls_tgt(user) page_num = 0 base = "https://uts-ws.nlm.nih.gov/rest" ext = "/crosswalk/current/source/AOD/" + iden["identifier"] while True: tick = User.umls_st(umls_tgt) page_num += 1 query = {"ticket": tick, "pageNumber": page_num} r = requests.get(base+ext, params=query) r.encoding = 'utf-8' items = json.loads(r.text) json_data = items["result"] for rep in json_data: if rep["ui"] not in anat_array and rep["ui"] != "NONE": # PSY ID. if rep["rootSource"] == "PSY": anat_array.append(rep["ui"]) gnomics.objects.anatomical_structure.AnatomicalStructure.add_identifier(anat, identifier=rep["ui"], identifier_type="PSY ID", language=None, source="UMLS Metathesaurus", name=rep["name"]) if not json_data: break return anat_array
def get_meddra_id(phen, user=None): meddra_array = [] for ident in phen.identifiers: if ident["identifier_type"].lower() in ["mdr", "mdr code", "mdr id", "mdr identifier", "meddra", "meddra code", "meddra id", "meddra identifier"]: if ident["identifier"] not in meddra_array: hpo_id_array.append(ident["identifier"]) ids_completed = [] for ident in phen.identifiers: if ident["identifier_type"].lower() in ["meddra term", "meddra label", "mdr label", "mdr term"]: if user is not None: umls_tgt = User.umls_tgt(user) page_num = 0 base = "https://uts-ws.nlm.nih.gov/rest" ext = "/search/current?sabs=MDR&searchType=exact&returnIdType=code" while True: tick = User.umls_st(umls_tgt) page_num += 1 query = {"string": ident["identifier"], "ticket": tick, "pageNumber": page_num} r = requests.get(base+ext, params=query) r.encoding = 'utf-8' try: items = json.loads(r.text) json_data = items["result"] empty = False for rep in json_data["results"]: if rep["ui"] not in meddra_array and rep["ui"] != "NONE": gnomics.objects.phenotype.Phenotype.add_identifier(phen, identifier = rep["ui"], identifier_type = "MedDRA ID", source = "UMLS") meddra_array.append(rep["ui"]) if json_data["results"][0]["ui"] == "NONE": empty = True break if not json_data: break if empty: break except: break elif ident["identifier_type"].lower() in ["hp code", "hp id", "hp identifier", "hpo code", "hpo id", "hpo identifier", "human phenotype ontology code", "human phenotype ontology id", "human phenotype ontology identifier", "hp", "hpo", "human phenotype ontology"]: if ident["identifier"] not in ids_completed: ids_completed.append(ident["identifier"]) hpo_id = ident["identifier"] if ":" in hpo_id: hpo_id = hpo_id.replace(":", "_") url = "https://www.ebi.ac.uk/ols/api/ontologies" ext = "/hp/terms/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252F" + hpo_id r = requests.get(url+ext, headers={"Content-Type": "application/json"}) if not r.ok: print("Something went wrong.") else: decoded = r.json() if meddra_array: return meddra_array for ident in phen.identifiers: print("NOT FUNCTIONAL") return ""
def get_hpo_id(phen, user=None): hpo_id_array = [] for ident in phen.identifiers: if ident["identifier_type"].lower() in ["hp code", "hp id", "hp identifier", "hpo code", "hpo id", "hpo identifier", "human phenotype ontology code", "human phenotype ontology id", "human phenotype ontology identifier", "hp", "hpo", "human phenotype ontology"]: if ident["identifier"] not in hpo_id_array: hpo_id_array.append(ident["identifier"]) if hpo_id_array: return hpo_id_array mesh_is_here = False mesh_array = [] for ident in phen.identifiers: if ident["identifier_type"].lower() in ["mesh", "mesh uid", "mesh unique id", "mesh unique identifier", "msh", "msh uid", "msh unique id", "msh unique identifier"]: mesh_is_here = True if ident["identifier"] not in mesh_array: page_num = 0 base = "https://uts-ws.nlm.nih.gov/rest" ext = "/crosswalk/current/source/MSH/" + ident["identifier"] + "?targetSource=HPO" umls_tgt = User.umls_tgt(user) while True: tick = User.umls_st(umls_tgt) page_num += 1 query = {"ticket": tick, "pageNumber": page_num} r = requests.get(base+ext, params=query) r.encoding = 'utf-8' try: items = json.loads(r.text) json_data = items["result"] for er in json_data: if er["ui"] not in hpo_id_array and er["ui"] != "NONE": hpo_id = er["ui"] gnomics.objects.phenotype.Phenotype.add_identifier(phen, identifier = hpo_id, identifier_type = "HPO ID", source = "UMLS", taxon = "H**o sapiens") hpo_id_array.append(hpo_id) if not json_data: break except: break break if hpo_id_array: return hpo_id_array if mesh_is_here: return [] for ident in phen.identifiers: if ident["identifier_type"].lower() in ["mdr", "mdr code", "mdr id", "mdr identifier", "meddra", "meddra code", "meddra id", "meddra identifier"]: mesh_uid = gnomics.objects.phenotype.Phenotype.mesh_uid(phen, user) if mesh_uid: return get_hpo_id(phen, user) else: return [] for ident in phen.identifiers: if ident["identifier_type"].lower() in ["meddra term", "meddra label", "mdr label", "mdr term"]: gnomics.objects.phenotype.Phenotype.meddra_id(phen, user) return get_hpo_id(phen, user)
def get_meddra_obj(adverse_event, user=None, source="umls"): obj_array = [] for ae_obj in adverse_event.adverse_event_objects: if 'object_type' in com_obj: if ae_obj['object_type'].lower() in ['mdr', 'mdr code', 'mdr id', 'mdr identifier', 'meddra', 'meddra code', 'meddra id', 'meddra identifier', 'meddra label', 'meddra object', 'meddra term']: return ae_obj['object'] if obj_array: return obj_array ids_completed = [] if user is not None: for meddra_term in get_meddra_term(adverse_event): if source.lower() in ["umls", "all"] and meddra_term not in ids_completed and user.umls_api_key is not None: umls_tgt = User.umls_tgt(user) page_num = 0 base = "https://uts-ws.nlm.nih.gov/rest" ext = "/search/current?sabs=MDR&searchType=exact&returnIdType=code" id_array = [] while True: tick = User.umls_st(umls_tgt) page_num += 1 query = {"string": str(meddra_term), "ticket": tick, "pageNumber": page_num} r = requests.get(base+ext, params=query) r.encoding = 'utf-8' if not r.ok: break else: items = json.loads(r.text) json_data = items["result"] if json_data["results"][0]["ui"] == "NONE": break else: id_array.append(json_data) if id_array: gnomics.objects.adverse_event.AdverseEvent.add_object(adverse_event, obj = id_array, object_type = "MedDRA Object") obj_array.append(id_array) if source.lower() != "all": ids_completed.append(meddra_term) if source.lower() in ["ncbo", "all"] and meddra_term not in ids_completed and user.ncbo_api_key is not None: base = "http://data.bioontology.org/search" ext = "?q=" + str(meddra_term) + "&ontologies=MEDDRA&require_exact_match=true&roots_only=true/?apikey=" + user.ncbo_api_key r = requests.get(base+ext, headers={"Content-Type": "application/json", "Authorization": "apikey token="+ user.ncbo_api_key}) if not r.ok: continue else: decoded = json.loads(r.text) id_array = [] for result in decoded["collection"]: id_array.append(result) if id_array: gnomics.objects.adverse_event.AdverseEvent.add_object(adverse_event, obj = id_array, object_type = "MedDRA Object") obj_array.append(id_array) if source.lower() != "all": ids_completed.append(meddra_term) return obj_array else: print("MedDRA object cannot be obtained without valid user object (with valid UMLS API key or NCBO API key).")
def get_hpo_term(phen, user=None): hpo_term_array = [] for ident in phen.identifiers: if ident["identifier_type"].lower() in ["hp label", "hp term", "hpo label", "hpo term", "human phenotype ontology label", "human phenotype ontology term"]: if ident["identifier"] not in hpo_id_array: hpo_id_array.append(ident["identifier"]) if hpo_term_array: return hpo_term_array for ident in phen.identifiers: if ident["identifier_type"].lower() in ["hp code", "hp id", "hp identifier", "hpo code", "hpo id", "hpo identifier", "human phenotype ontology code", "human phenotype ontology id", "human phenotype ontology identifier", "hp", "hpo", "human phenotype ontology"]: if user: page_num = 0 base = "https://uts-ws.nlm.nih.gov/rest" ext = "/content/current/source/HPO/" + ident["identifier"] umls_tgt = User.umls_tgt(user) while True: tick = User.umls_st(umls_tgt) page_num += 1 query = {"ticket": tick, "pageNumber": page_num} r = requests.get(base+ext, params=query) r.encoding = 'utf-8' try: items = json.loads(r.text) json_data = items["result"] if "name" in json_data: if json_data["name"] not in hpo_term_array: hpo_term = json_data["name"] gnomics.objects.phenotype.Phenotype.add_identifier(phen, identifier = hpo_term, identifier_type = "HPO Term", source = "UMLS", taxon = "H**o sapiens") hpo_term_array.append(hpo_term) else: break except: break break else: proc_id = ident["identifier"] if ":" in proc_id: proc_id = proc_id.replace(":", "_") url = "https://www.ebi.ac.uk/ols/api/ontologies" ext = "/hp/terms/http%253A%252F%252Fpurl.obolibrary.org%252Fobo%252F" + proc_id r = requests.get(url+ext, headers={"Content-Type": "application/json"}) if not r.ok: print("Something went wrong.") else: new_decoded = r.json() if new_decoded["label"] not in hpo_term_array: gnomics.objects.phenotype.Phenotype.add_identifier(phen, identifier = new_decoded["label"], identifier_type = "HPO Term", source = "OLS", taxon = "H**o sapiens") hpo_term_array.append(new_decoded["label"]) return hpo_term_array
def get_fma_id(anat, user=None, source="umls"): fma_array = [] for ident in anat.identifiers: if ident["identifier_type"].lower() in ["fma", "fma id", "fma identifier", "fmaid", "foundational model of anatomy", "foundational model of anatomy id", "foundational model of anatomy identifier"] and ident["identifier"] not in fma_array: fma_array.append(ident["identifier"]) if fma_array: return fma_array for iden in anat.identifiers: if iden["identifier_type"].lower() in ["neu", "neu id", "neu identifier", "neuronames brain hierarchy id", "neuronames brain hierarchy identifier"]: if source.lower() in ["umls", "all"] and user is not None: umls_tgt = User.umls_tgt(user) page_num = 0 base = "https://uts-ws.nlm.nih.gov/rest" ext = "/crosswalk/current/source/NEU/" + iden["identifier"] while True: tick = User.umls_st(umls_tgt) page_num += 1 query = {"ticket": tick, "pageNumber": page_num} r = requests.get(base+ext, params=query) r.encoding = 'utf-8' items = json.loads(r.text) json_data = items["result"] for rep in json_data: if rep["ui"] not in anat_array and rep["ui"] != "NONE": # FMA ID. if rep["rootSource"] == "FMA": fma_array.append(rep["ui"]) gnomics.objects.anatomical_structure.AnatomicalStructure.add_identifier(anat, identifier=rep["ui"], identifier_type="FMA ID", language=None, source="UMLS Metathesaurus", name=rep["name"]) if not json_data: break elif iden["identifier_type"].lower() in ["uwda", "uwda id", "uwda identifier"]: if source.lower() in ["umls", "all"] and user is not None: umls_tgt = User.umls_tgt(user) page_num = 0 base = "https://uts-ws.nlm.nih.gov/rest" ext = "/crosswalk/current/source/UWDA/" + iden["identifier"] while True: tick = User.umls_st(umls_tgt) page_num += 1 query = {"ticket": tick, "pageNumber": page_num} r = requests.get(base+ext, params=query) r.encoding = 'utf-8' items = json.loads(r.text) json_data = items["result"] for rep in json_data: if rep["ui"] not in anat_array and rep["ui"] != "NONE": # FMA ID. if rep["rootSource"] == "FMA": fma_array.append(rep["ui"]) gnomics.objects.anatomical_structure.AnatomicalStructure.add_identifier(anat, identifier=rep["ui"], identifier_type="FMA ID", language=None, source="UMLS Metathesaurus", name=rep["name"]) if not json_data: break if iden["identifier_type"].lower() in ["wikidata", "wikidata accession", "wikidata id", "wikidata identifier"]: for stuff in gnomics.objects.anatomical_structure.AnatomicalStructure.wikidata(anat): for prop_id, prop_dict in stuff["claims"].items(): base = "https://www.wikidata.org/w/api.php" ext = "?action=wbgetentities&ids=" + prop_id + "&format=json" r = requests.get(base+ext, headers={"Content-Type": "application/json"}) if not r.ok: r.raise_for_status() sys.exit() decoded = json.loads(r.text) en_prop_name = decoded["entities"][prop_id]["labels"]["en"]["value"] if en_prop_name.lower() == "foundational model of anatomy id": for x in prop_dict: if x["mainsnak"]["datavalue"]["value"] not in fma_array: gnomics.objects.anatomical_structure.AnatomicalStructure.add_identifier(anat, identifier = x["mainsnak"]["datavalue"]["value"], identifier_type = "Foundational Model of Anatomy ID", language = None, source = "Wikidata") fma_array.append(x["mainsnak"]["datavalue"]["value"]) elif (ident["identifier_type"].lower() in ["wikipedia", "wikipedia accession", "wikipedia article"]) and ident["language"].lower() == "en": for stuff in gnomics.objects.anatomical_structure.AnatomicalStructure.wikidata(anat): for prop_id, prop_dict in stuff["claims"].items(): base = "https://www.wikidata.org/w/api.php" ext = "?action=wbgetentities&ids=" + prop_id + "&format=json" r = requests.get(base+ext, headers={"Content-Type": "application/json"}) if not r.ok: r.raise_for_status() sys.exit() decoded = json.loads(r.text) en_prop_name = decoded["entities"][prop_id]["labels"]["en"]["value"] if en_prop_name.lower() == "foundational model of anatomy id": for x in prop_dict: if x["mainsnak"]["datavalue"]["value"] not in fma_array: gnomics.objects.anatomical_structure.AnatomicalStructure.add_identifier(anat, identifier = x["mainsnak"]["datavalue"]["value"], identifier_type = "Foundational Model of Anatomy ID", language = None, source = "Wikidata") fma_array.append(x["mainsnak"]["datavalue"]["value"]) elif ident["identifier_type"].lower() in ["uberon", "uberon id", "uberon identifier"]: gnomics.objects.anatomical_structure.AnatomicalStructure.wikipedia_accession(anat, language = "en") anat_array = [] for stuff in gnomics.objects.anatomical_structure.AnatomicalStructure.wikidata(anat): for prop_id, prop_dict in stuff["claims"].items(): base = "https://www.wikidata.org/w/api.php" ext = "?action=wbgetentities&ids=" + prop_id + "&format=json" r = requests.get(base+ext, headers={"Content-Type": "application/json"}) if not r.ok: r.raise_for_status() sys.exit() decoded = json.loads(r.text) en_prop_name = decoded["entities"][prop_id]["labels"]["en"]["value"] if en_prop_name.lower() == "foundational model of anatomy id": for x in prop_dict: if x["mainsnak"]["datavalue"]["value"] not in fma_array: gnomics.objects.anatomical_structure.AnatomicalStructure.add_identifier(anat, identifier = x["mainsnak"]["datavalue"]["value"], identifier_type = "Foundational Model of Anatomy ID", language = None, source = "Wikidata") fma_array.append(x["mainsnak"]["datavalue"]["value"]) return fma_array
def get_aod(anat, user=None, source="umls"): aod_array = [] for iden in gnomics.objects.auxiliary_files.identifier.filter_identifiers( anat.identifiers, [ "alcohol and other drug", "alcohol and other drug id", "alcohol and other drug identifier", "aod", "aod id", "aod identifier" ]): if iden["identifier"] not in aod_array: aod_array.append(iden["identifier"]) if aod_array: return aod_array ids_completed = [] for iden in gnomics.objects.auxiliary_files.identifier.filter_identifiers( anat.identifiers, [ "neu id", "neu identifier", "neuronames brain hierarchy id", "neuronames brain hierarchy identifier", "neu" ]): if iden["identifier"] not in ids_completed and user is not None: ids_completed.append(iden["identifier"]) if source.lower() in ["umls", "all"]: umls_tgt = User.umls_tgt(user) page_num = 0 base = "https://uts-ws.nlm.nih.gov/rest" ext = "/crosswalk/current/source/NEU/" + str( iden["identifier"]) while True: tick = User.umls_st(umls_tgt) page_num += 1 query = {"ticket": tick, "pageNumber": page_num} r = requests.get(base + ext, params=query) r.encoding = 'utf-8' items = json.loads(r.text) json_data = items["result"] for rep in json_data: if rep["ui"] != "NONE": # AOD ID. if rep["rootSource"] == "AOD" and rep[ "ui"] not in aod_array: aod_array.append(rep["ui"]) gnomics.objects.anatomical_structure.AnatomicalStructure.add_identifier( anat, identifier=rep["ui"], identifier_type="AOD ID", language=None, source="UMLS Metathesaurus") if not json_data: break for iden in gnomics.objects.auxiliary_files.identifier.filter_identifiers( anat.identifiers, ["uwda", "uwda id", "uwda identifier"]): if iden["identifier"] not in ids_completed and user is not None: ids_completed.append(iden["identifier"]) if source.lower() in ["umls", "all"]: umls_tgt = User.umls_tgt(user) page_num = 0 base = "https://uts-ws.nlm.nih.gov/rest" ext = "/crosswalk/current/source/UWDA/" + str( iden["identifier"]) while True: tick = User.umls_st(umls_tgt) page_num += 1 query = {"ticket": tick, "pageNumber": page_num} r = requests.get(base + ext, params=query) r.encoding = 'utf-8' items = json.loads(r.text) json_data = items["result"] for rep in json_data: if rep["ui"] != "NONE": # AOD ID. if rep["rootSource"] == "AOD" and rep[ "ui"] not in aod_array: aod_array.append(rep["ui"]) gnomics.objects.anatomical_structure.AnatomicalStructure.add_identifier( anat, identifier=rep["ui"], identifier_type="AOD ID", language=None, source="UMLS Metathesaurus") if not json_data: break return aod_array
def umls_crosswalk(user, from_source, to_source, source_id, other="id", verbose=False): found_array = [] if user is None: print("A valid user object with a valid UMLS API key is required for this function.") return found_array elif user.umls_api_key is None: print("A valid UMLS API key associated with the user object is required for this function.") return found_array umls_tgt = User.umls_tgt(user) page_num = 0 base = "https://uts-ws.nlm.nih.gov/rest" ext = "/crosswalk/current/source/" + from_source.upper() + "/" + source_id verbose = True while True: tick = User.umls_st(umls_tgt) page_num += 1 query = {"ticket": tick, "pageNumber": page_num} try: r = requests.get(base+ext, params=query, headers = {"Content-Type": "application/json", "User-Agent": "Mozilla/5.0 (Windows NT 6.0; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0"}) r.encoding = 'utf-8' try: items = json.loads(r.text) json_data = items["result"] empty = False for rep in json_data: if other == "id": if rep["ui"] not in found_array and rep["ui"] != "NONE": if rep["rootSource"] == to_source.upper(): found_array.append(rep["ui"]) elif other == "name": if (rep["name"] not in found_array and rep["name"] != "NONE") and (rep["ui"] not in found_array and rep["ui"] != "NONE"): if rep["rootSource"] == to_source.upper(): found_array.append((rep["ui"], rep["name"])) else: print("This value for 'other' is not currently supported.") if "results" in json_data: if json_data["results"][0]["ui"] == "NONE": empty = True break if not json_data: break elif empty: break except ValueError as e: if verbose: print("A value error occurred while attempting to load resulting JSON.") print("ERROR: %s" % str(e)) break except: if verbose: print("Some other unknown error occurred.") break else: continue except ConnectionError as e: if verbose: print("A connection error occurred while attempting request.") print("ERROR: %s" % str(e)) break except requests.exceptions.ConnectionError as e: if verbose: print("A connection error occurred while attempting request.") print("ERROR: %s" % str(e)) break except HTTPException as e: if verbose: print("An HTTP exception error occurred while attempting request.") print("ERROR: %s" % str(e)) break except ProtocolError as e: if verbose: print("A protocol error occurred while attempting request.") print("ERROR: %s" % str(e)) break except urllib3.exceptions.ProtocolError as e: if verbose: print("A protocol error occurred while attempting request.") print("ERROR: %s" % str(e)) break except SysCallError as e: if verbose: print("A system call error occurred while attempting request.") print("ERROR: %s" % str(e)) break except OpenSSL.SSL.SysCallError as e: if verbose: print("A system call error occurred while attempting request.") print("ERROR: %s" % str(e)) break except ssl.SSLError as e: if verbose: print("A system call error occurred while attempting request.") print("ERROR: %s" % str(e)) break except SSLError as e: if verbose: print("A SSL error occurred while attempting request.") print("ERROR: %s" % str(e)) break except requests.exceptions.SSLError as e: if verbose: print("A SSL error occurred while attempting request.") print("ERROR: %s" % str(e)) break except BadStatusLine as e: if verbose: print("A bad status line error occurred while attempting request.") print("ERROR: %s" % str(e)) break except httplib.BadStatusLine as e: if verbose: print("A bad status line error occurred while attempting request.") print("ERROR: %s" % str(e)) break except http.client.BadStatusLine as e: if verbose: print("A protocol error occurred while attempting request.") print("ERROR: %s" % str(e)) break except MaxRetryError as e: if verbose: print("A max retry error occurred while attempting request.") print("ERROR: %s" % str(e)) break except urllib3.exceptions.MaxRetryError as e: if verbose: print("A max retry error occurred while attempting request.") print("ERROR: %s" % str(e)) break except: print("Some other HTTP/URL error encountered.") break else: break return found_array
def get_phenotypes(adverse_event, user=None): phen_array = [] ids_completed = [] for iden in gnomics.objects.auxiliary_files.identifier.filter_identifiers(adverse_event.identifiers, ["meddra term", "mdr label", "mdr name", "mdr term", "meddra label", "meddra name"]): if iden["identifier"] not in ids_completed and user is not None: ids_completed.append(iden["identifier"]) if user.ncbo_api_key is not None: # Map MedDRA term to MedDRA ID. meddra_id_array = gnomics.objects.adverse_event.AdverseEvent.meddra_id(adverse_event, user) hpo_id_array = [] for iden in meddra_id_array: base = "http://data.bioontology.org/ontologies/" ext = "MEDDRA/classes/http%3A%2F%2Fpurl.bioontology.org%2Fontology%2FMEDDRA%2F" + str(iden) + "/mappings/?apikey=" + user.ncbo_api_key r = requests.get(base+ext, headers={"Content-Type": "application/json"}) if not r.ok: continue else: decoded = json.loads(r.text) for result in decoded: for subresult in result["classes"]: if "http://purl.obolibrary.org/obo/HP_" in subresult["@id"]: hpo_id = subresult["@id"].split("/obo/")[1] if hpo_id not in hpo_id_array: hpo_id_array.append(hpo_id) temp_phen = gnomics.objects.phenotype.Phenotype(identifier = hpo_id, identifier_type = "HPO ID", source = "NCBO BioPortal", taxon = "H**o sapiens") phen_array.append(temp_phen) if not hpo_id_array: mesh_uid_array = [] mesh_uids = gnomics.objects.adverse_event.AdverseEvent.mesh_uid(adverse_event, user) mesh_uid_array.extend(mesh_uids) for iden in mesh_uid_array: umls_tgt = User.umls_tgt(user) page_num = 0 base = "https://uts-ws.nlm.nih.gov/rest" ext = "/crosswalk/current/source/MSH/" + iden + "?targetSource=HPO" try: tick = User.umls_st(umls_tgt) page_num += 1 query = {"ticket": tick, "pageNumber": page_num} r = requests.get(base+ext, params=query) r.encoding = 'utf-8' if not r.ok: print("No mapping from MeSH to HPO found.") else: items = json.loads(r.text) json_data = items["result"] for er in json_data: if er["ui"] not in hpo_id_array and er["ui"] != "NONE": hpo_id = er["ui"] hpo_id_array.append(hpo_id) temp_phen = gnomics.objects.phenotype.Phenotype(identifier = hpo_id, identifier_type = "HPO ID", source = "UMLS", taxon = "H**o sapiens") phen_array.append(temp_phen) if not json_data: continue except: continue else: print("A valid NCBO API key is necessary to access the NCBO Bioontology API. Please provide a valid user object with such a key.") else: print("A valid user object with an NCBO API key is necessary to access the NCBO Bioontology API. Please provide such an object.") return phen_array