def get_cpt_id(procedure, user=None): for iden in procedure.identifiers: if iden["identifier_type"].lower() in ["umls concept uid", "umls concept unique id", "umls concept unique identifier", "umls cui", "umls id", "umls identifier"]: if user is not None: proc_array = [] umls_tgt = User.umls_tgt(user) page_num = 0 base = "https://uts-ws.nlm.nih.gov/rest" ext = "/search/current?string=" + iden["identifier"] + "inputType=sourceUi&searchType=exact&sabs=MTH" while True: tick = User.umls_st(umls_tgt) page_num += 1 query = {"string": query, "ticket": tick, "pageNumber": page_num} r = requests.get(base+ext, params=query) r.encoding = 'utf-8' print(r.text) items = json.loads(r.text) json_data = items["result"] for rep in json_data["results"]: if rep["ui"] not in proc_array and rep["ui"] != "NONE": proc_array.append(rep["ui"]) if json_data["results"][0]["ui"] == "NONE": break return proc_array
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 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 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_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_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 search(query, user=None, source="ebi", search_type="exact", return_id_type="sourceUi"): anat_list = [] anat_id_array = [] if source.lower() in ["ebi", "all"]: url = "http://www.ebi.ac.uk/ols/api/" ext = "search?q=" + str( query ) + "&ontology=aeo,caro,fma,uberon,ceph,ehdaa2,emap,emapa,fbbt,hao,ma,mfmo,plana,tads,tgma,wbbt,xao,zfa,fao" r = requests.get(url + ext, headers={"Content-Type": "application/json"}) if not r.ok: print("An error occurred in the EBI OLS API.") else: decoded = r.json() # See here: # https://www.ebi.ac.uk/ols/ontologies for doc in decoded["response"]["docs"]: if "obo_id" in doc: # Taxon agnostic (I think) if "AEO" in doc["obo_id"]: new_id = doc["obo_id"] if new_id not in anat_id_array: anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=new_id, identifier_type="AEO ID", source="Ontology Lookup Service", name=doc["label"]) anat_list.append(anat_temp) anat_id_array.append(new_id) elif "CARO" in doc["obo_id"]: new_id = doc["obo_id"] if new_id not in anat_id_array: anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=new_id, identifier_type="CARO ID", source="Ontology Lookup Service", name=doc["label"]) anat_list.append(anat_temp) anat_id_array.append(new_id) elif "FAO" in doc["obo_id"]: new_id = doc["obo_id"] if new_id not in anat_id_array: anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=new_id, identifier_type="FAO ID", source="Ontology Lookup Service", name=doc["label"]) anat_list.append(anat_temp) anat_id_array.append(new_id) elif "UBERON" in doc["obo_id"]: new_id = doc["obo_id"] if new_id not in anat_id_array: anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=new_id, identifier_type="UBERON ID", source="Ontology Lookup Service", name=doc["label"]) anat_list.append(anat_temp) anat_id_array.append(new_id) # Taxon specific (I think) elif "CEPH" in doc["obo_id"]: new_id = doc["obo_id"] if new_id not in anat_id_array: anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=new_id, identifier_type="CEPH ID", source="Ontology Lookup Service", taxon="Cephalopoda", name=doc["label"]) anat_list.append(anat_temp) anat_id_array.append(new_id) elif "EHDAA2" in doc["obo_id"]: new_id = doc["obo_id"] if new_id not in anat_id_array: anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=new_id, identifier_type="EHDAA2 ID", source="Ontology Lookup Service", taxon="H**o sapiens", name=doc["label"]) anat_list.append(anat_temp) anat_id_array.append(new_id) elif "EMAP" in doc["obo_id"]: new_id = doc["obo_id"] if new_id not in anat_id_array: anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=new_id, identifier_type="EMAP ID", source="Ontology Lookup Service", taxon="Mus", name=doc["label"]) anat_list.append(anat_temp) anat_id_array.append(new_id) elif "EMAPA" in doc["obo_id"]: new_id = doc["obo_id"] if new_id not in anat_id_array: anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=new_id, identifier_type="EMAPA ID", source="Ontology Lookup Service", taxon="Mus", name=doc["label"]) anat_list.append(anat_temp) anat_id_array.append(new_id) elif "FBbt" in doc["obo_id"]: new_id = doc["obo_id"] if new_id not in anat_id_array: anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=new_id, identifier_type="FBbt ID", source="Ontology Lookup Service", taxon="Drosophila melanogaster", name=doc["label"]) anat_list.append(anat_temp) anat_id_array.append(new_id) elif "FMA" in doc["obo_id"]: new_id = doc["obo_id"] if new_id not in anat_id_array: anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=new_id, identifier_type="FMA ID", source="Ontology Lookup Service", name=doc["label"], taxon="H**o sapiens") anat_list.append(anat_temp) anat_id_array.append(new_id) elif "HAO" in doc["obo_id"]: new_id = doc["obo_id"] if new_id not in anat_id_array: anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=new_id, identifier_type="HAO ID", source="Ontology Lookup Service", taxon="Hymenoptera", name=doc["label"]) anat_list.append(anat_temp) anat_id_array.append(new_id) elif "MA" in doc["obo_id"]: new_id = doc["obo_id"] if new_id not in anat_id_array: anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=new_id, identifier_type="MA ID", source="Ontology Lookup Service", taxon="Mus", name=doc["label"]) anat_list.append(anat_temp) anat_id_array.append(new_id) elif "MFMO" in doc["obo_id"]: new_id = doc["obo_id"] if new_id not in anat_id_array: anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=new_id, identifier_type="MFMO ID", source="Ontology Lookup Service", taxon="Mammalia", name=doc["label"]) anat_list.append(anat_temp) anat_id_array.append(new_id) elif "PLANA" in doc["obo_id"]: new_id = doc["obo_id"] if new_id not in anat_id_array: anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=new_id, identifier_type="PLANA ID", source="Ontology Lookup Service", taxon="Schmidtea mediterranea", name=doc["label"]) anat_list.append(anat_temp) anat_id_array.append(new_id) elif "TADS" in doc["obo_id"]: new_id = doc["obo_id"] if new_id not in anat_id_array: anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=new_id, identifier_type="FBbt ID", source="Ontology Lookup Service", taxon=["Ixodidae", "Argassidae"], name=doc["label"]) anat_list.append(anat_temp) anat_id_array.append(new_id) elif "TGMA" in doc["obo_id"]: new_id = doc["obo_id"] if new_id not in anat_id_array: anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=new_id, identifier_type="TGMA ID", source="Ontology Lookup Service", taxon="Culicidae", name=doc["label"]) anat_list.append(anat_temp) anat_id_array.append(new_id) elif "WBBT" in doc["obo_id"]: new_id = doc["obo_id"] if new_id not in anat_id_array: anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=new_id, identifier_type="WBBT ID", source="Ontology Lookup Service", taxon="Caenorhabditis elegans", name=doc["label"]) anat_list.append(anat_temp) anat_id_array.append(new_id) elif "XAO" in doc["obo_id"]: new_id = doc["obo_id"] if new_id not in anat_id_array: anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=new_id, identifier_type="XAO ID", source="Ontology Lookup Service", taxon="Xenopus laevis", name=doc["label"]) anat_list.append(anat_temp) anat_id_array.append(new_id) elif "ZFA" in doc["obo_id"]: new_id = doc["obo_id"] if new_id not in anat_id_array: anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=new_id, identifier_type="ZFA ID", source="Ontology Lookup Service", taxon="Danio rerio", name=doc["label"]) anat_list.append(anat_temp) anat_id_array.append(new_id) 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 = "/search/current?string=" + query + "&inputType=sourceUi&searchType=words&returnIdType=" + return_id_type while True: tick = User.umls_st(umls_tgt) page_num += 1 query = {"string": query, "ticket": tick, "pageNumber": page_num} r = requests.get(base + ext, params=query) r.encoding = 'utf-8' # print(r.text) items = json.loads(r.text) json_data = items["result"] for rep in json_data["results"]: if rep["ui"] not in anat_id_array and rep["ui"] != "NONE": # Foundational Model of Anatomy. if rep["rootSource"] == "FMA": temp_anat = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=rep["ui"], identifier_type="FMA ID", language=None, source="UMLS Metathesaurus", name=rep["name"], taxon="H**o sapiens") anat_list.append(temp_anat) anat_id_array.append(rep["ui"]) # Neuronames Brain Hierarchy. elif rep["rootSource"] == "NEU": temp_anat = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=rep["ui"], identifier_type="NEU ID", language=None, source="UMLS Metathesaurus", name=rep["name"]) anat_list.append(temp_anat) anat_id_array.append(rep["ui"]) # Digital Anatomist. elif rep["rootSource"] == "UWDA": temp_anat = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=rep["ui"], identifier_type="UWDA ID", language=None, source="UMLS Metathesaurus", name=rep["name"], taxon="H**o sapiens") anat_list.append(temp_anat) anat_id_array.append(rep["ui"]) if json_data["results"][0]["ui"] == "NONE": break if source.lower() in ["ncbo", "all"] and user.ncbo_api_key is not None: base = "http://data.bioontology.org/search" ext = "?q=" + str( query ) + "&ontologies=FMA,PAE,XAO,UBERON,ZEA,FB-BT,FAO,WB-BT,ZFA,HAO,TGMA,ICD11-BODYSYSTEM,DDANAT,SPD,TADS,VHOG,CARO,MA,MOOCCUADO,EMAPA,CTENO,MOOCCIADO,AAO,PHMAMMADO,CEPH,PHFUMIADO,HAROREADO,CISAVIADO,MAT,EHDA,MFMO,CIINTEADO,EHDAA,EMAP,BSAO,PLANA,MFO,EHDAA2,AEO,PORO,MOOCULADO,TAO,VSAO&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: r.raise_for_status() sys.exit() else: decoded = json.loads(r.text) for result in decoded["collection"]: # Foundational Model of Anatomy if "FMA" in result["@id"]: fma_id = result["@id"].split("/obo/")[1] if fma_id not in anat_id_array: anat_id_array.append(fma_id) anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=fma_id, identifier_type="FMA ID", source="NCBO BioPortal", name=result["prefLabel"], taxon="H**o sapiens") anat_list.append(anat_temp) # Vertebrate Skeletal Anatomy Ontology elif "VSAO" in result["@id"]: vsao_id = result["@id"].split("/obo/")[1] if vsao_id not in anat_id_array: anat_id_array.append(vsao_id) anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=vsao_id, identifier_type="VSAO ID", source="NCBO BioPortal", name=result["prefLabel"], taxon="Vertebrata") anat_list.append(anat_temp) # Teleost Anatomy Ontology elif "TAO" in result["@id"]: tao_id = result["@id"].split("/obo/")[1] if tao_id not in anat_id_array: anat_id_array.append(tao_id) anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=tao_id, identifier_type="TAO ID", source="NCBO BioPortal", name=result["prefLabel"], taxon="Teleostei") anat_list.append(anat_temp) # Molgula oculata Anatomy and Development Ontology elif "MOOCULADO" in result["@id"]: mooculado_id = result["@id"].split("/obo/")[1] if mooculado_id not in anat_id_array: anat_id_array.append(mooculado_id) anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=mooculado_id, identifier_type="MOOCULADO ID", source="NCBO BioPortal", name=result["prefLabel"], taxon="Molgula oculata") anat_list.append(anat_temp) # Porifera Ontology elif "PORO" in result["@id"]: poro_id = result["@id"].split("/obo/")[1] if poro_id not in anat_id_array: anat_id_array.append(poro_id) anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=poro_id, identifier_type="PORO ID", source="NCBO BioPortal", name=result["prefLabel"], taxon="Porifera") anat_list.append(anat_temp) # Anatomical Entity Ontology elif "AEO" in result["@id"]: aeo_id = result["@id"].split("/obo/")[1] if aeo_id not in anat_id_array: anat_id_array.append(aeo_id) anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=aeo_id, identifier_type="AEO ID", source="NCBO BioPortal", name=result["prefLabel"], taxon=None) anat_list.append(anat_temp) # Human Developmental Anatomy Ontology, abstract version 2 elif "EHDAA2" in result["@id"]: ehdaa2_id = result["@id"].split("/obo/")[1] if ehdaa2_id not in anat_id_array: anat_id_array.append(ehdaa2_id) anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=ehdaa2_id, identifier_type="EHDAA2 ID", source="NCBO BioPortal", name=result["prefLabel"], taxon="H**o sapiens") anat_list.append(anat_temp) # Medaka Fish Anatomy and Development Ontology elif "MFO" in result["@id"]: mfo_id = result["@id"].split("/obo/")[1] if mfo_id not in anat_id_array: anat_id_array.append(mfo_id) anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=mfo_id, identifier_type="MFO ID", source="NCBO BioPortal", name=result["prefLabel"], taxon="Oryzias latipes") anat_list.append(anat_temp) # Planarian Anatomy and Developmental Stage Ontology elif "PLANA" in result["@id"]: plana_id = result["@id"].split("/obo/")[1] if plana_id not in anat_id_array: anat_id_array.append(plana_id) anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=plana_id, identifier_type="PLANA ID", source="NCBO BioPortal", name=result["prefLabel"], taxon="Schmidtea mediterranea") anat_list.append(anat_temp) # Botryllus schlosseri anatomy and development Ontology elif "BSAO" in result["@id"]: bsao_id = result["@id"].split("/obo/")[1] if bsao_id not in anat_id_array: anat_id_array.append(bsao_id) anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=bsao_id, identifier_type="BSAO ID", source="NCBO BioPortal", name=result["prefLabel"], taxon="Botryllus schlosseri") anat_list.append(anat_temp) # Mouse Gross Anatomy and Development Ontology elif "EMAP" in result["@id"]: emap_id = result["@id"].split("/obo/")[1] if emap_id not in anat_id_array: anat_id_array.append(emap_id) anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=emap_id, identifier_type="EMAP ID", source="NCBO BioPortal", name=result["prefLabel"], taxon="Mus") anat_list.append(anat_temp) # Human Developmental Anatomy Ontology, abstract version 1 elif "EHDAA" in result["@id"]: ehdaa_id = result["@id"].split("/obo/")[1] if ehdaa_id not in anat_id_array: anat_id_array.append(ehdaa_id) anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=ehdaa_id, identifier_type="EHDAA ID", source="NCBO BioPortal", name=result["prefLabel"], taxon="H**o sapiens") anat_list.append(anat_temp) # Ciona intestinalis Anatomy and Development Ontology elif "CIINTEADO" in result["@id"]: ciinteado_id = result["@id"].split("/obo/")[1] if ciinteado_id not in anat_id_array: anat_id_array.append(ciinteado_id) anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=ciinteado_id, identifier_type="CIINTEADO ID", source="NCBO BioPortal", name=result["prefLabel"], taxon="Ciona intestinalis") anat_list.append(anat_temp) # Mammalian Feeding Muscle Ontology elif "MFMO" in result["@id"]: mfmo_id = result["@id"].split("/obo/")[1] if mfmo_id not in anat_id_array: anat_id_array.append(mfmo_id) anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=mfmo_id, identifier_type="MFMO ID", source="NCBO BioPortal", name=result["prefLabel"], taxon="Mammalia") anat_list.append(anat_temp) # Human Developmental Anatomy Ontology, timed version elif "EHDA" in result["@id"]: ehda_id = result["@id"].split("/obo/")[1] if ehda_id not in anat_id_array: anat_id_array.append(ehda_id) anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=ehda_id, identifier_type="EHDA ID", source="NCBO BioPortal", name=result["prefLabel"], taxon="H**o sapiens") anat_list.append(anat_temp) # Minimal Anatomical Terminology elif "MAT" in result["@id"]: mat_id = result["@id"].split("/obo/")[1] if mat_id not in anat_id_array: anat_id_array.append(mat_id) anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=mat_id, identifier_type="MAT ID", source="NCBO BioPortal", name=result["prefLabel"], taxon=None) anat_list.append(anat_temp) # Ciona savignyi Anatomy and Development Ontology elif "CISAVIADO" in result["@id"]: cisaviado_id = result["@id"].split("/obo/")[1] if cisaviado_id not in anat_id_array: anat_id_array.append(cisaviado_id) anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=cisaviado_id, identifier_type="CISAVIADO ID", source="NCBO BioPortal", name=result["prefLabel"], taxon="Ciona savignyi") anat_list.append(anat_temp) # Halocynthia roretzi Anatomy and Development Ontology elif "HAROREADO" in result["@id"]: haroreado_id = result["@id"].split("/obo/")[1] if haroreado_id not in anat_id_array: anat_id_array.append(haroreado_id) anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=haroreado_id, identifier_type="HAROREADO ID", source="NCBO BioPortal", name=result["prefLabel"], taxon="Halocynthia roretzi") anat_list.append(anat_temp) # Phallusia fumigata Anatomy and Development Ontology elif "PHFUMIADO" in result["@id"]: phfumiado_id = result["@id"].split("/obo/")[1] if phfumiado_id not in anat_id_array: anat_id_array.append(phfumiado_id) anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=caro_id, identifier_type="PHFUMIADO ID", source="NCBO BioPortal", name=result["prefLabel"], taxon="Phallusia fumigata") anat_list.append(anat_temp) # Cephalopod Ontology elif "CEPH" in result["@id"]: ceph_id = result["@id"].split("/obo/")[1] if ceph_id not in anat_id_array: anat_id_array.append(ceph_id) anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=ceph_id, identifier_type="CEPH ID", source="NCBO BioPortal", name=result["prefLabel"], taxon=None) anat_list.append(anat_temp) # Anatomy Gross Anatomy Ontology elif "AAO" in result["@id"]: aao_id = result["@id"].split("/obo/")[1] if aao_id not in anat_id_array: anat_id_array.append(aao_id) anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=aao_id, identifier_type="AAO ID", source="NCBO BioPortal", name=result["prefLabel"], taxon="Amphibia") anat_list.append(anat_temp) # Phallusia mammillata Anatomy and Development Ontology elif "PHMAMMADO" in result["@id"]: phmammado_id = result["@id"].split("/obo/")[1] if phmammado_id not in anat_id_array: anat_id_array.append(phmammado_id) anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=phmammado_id, identifier_type="PHMAMMADO ID", source="NCBO BioPortal", name=result["prefLabel"], taxon="Phallusia mammillata") anat_list.append(anat_temp) # Molgula occidentalis Anatomy and Development Ontology elif "MOOCCIADO" in result["@id"]: moocciado_id = result["@id"].split("/obo/")[1] if moocciado_id not in anat_id_array: anat_id_array.append(moocciado_id) anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=moocciado_id, identifier_type="MOOCCIADO ID", source="NCBO BioPortal", name=result["prefLabel"], taxon="Molgula occidentalis") anat_list.append(anat_temp) # Ctenophore Ontology elif "CTENO" in result["@id"]: cteno_id = result["@id"].split("/obo/")[1] if cteno_id not in anat_id_array: anat_id_array.append(cteno_id) anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=cteno_id, identifier_type="CTENO ID", source="NCBO BioPortal", name=result["prefLabel"], taxon="Ctenophora") anat_list.append(anat_temp) # Mouse gross anatomy and development, timed elif "EMAPA" in result["@id"]: emapa_id = result["@id"].split("/obo/")[1] if emapa_id not in anat_id_array: anat_id_array.append(emapa_id) anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=emapa_id, identifier_type="EMAPA ID", source="NCBO BioPortal", name=result["prefLabel"], taxon="Mus musculus") anat_list.append(anat_temp) # Molgula occulta Anatomy and Development Ontology elif "MOOCCUADO" in result["@id"]: mooccuado_id = result["@id"].split("/obo/")[1] if mooccuado_id not in anat_id_array: anat_id_array.append(mooccuado_id) anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=mooccuado_id, identifier_type="MOOCCUADO ID", source="NCBO BioPortal", name=result["prefLabel"], taxon="Molgula occulta") anat_list.append(anat_temp) # Common Anatomy Reference Ontology elif "CARO" in result["@id"]: caro_id = result["@id"].split("/obo/")[1] if caro_id not in anat_id_array: anat_id_array.append(caro_id) anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=caro_id, identifier_type="CARO ID", source="NCBO BioPortal", name=result["prefLabel"], taxon=None) anat_list.append(anat_temp) # Vertebrate Homologous Organ Group Ontology elif "VHOG" in result["@id"]: vhog_id = result["@id"].split("/obo/")[1] if vhog_id not in anat_id_array: anat_id_array.append(vhog_id) anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=vhog_id, identifier_type="VHOG ID", source="NCBO BioPortal", name=result["prefLabel"], taxon="Vertebrata") anat_list.append(anat_temp) # Tick Gross Anatomy Ontology elif "TADS" in result["@id"]: tads_id = result["@id"].split("/obo/")[1] if tads_id not in anat_id_array: anat_id_array.append(tads_id) anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=tads_id, identifier_type="TADS ID", source="NCBO BioPortal", name=result["prefLabel"], taxon=["Ixodidae", "Argassidae"]) anat_list.append(anat_temp) # Spider Ontology elif "SPD" in result["@id"]: spd_id = result["@id"].split("/obo/")[1] if spd_id not in anat_id_array: anat_id_array.append(spd_id) anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=spd_id, identifier_type="SPD ID", source="NCBO BioPortal", name=result["prefLabel"], taxon="Araneae") anat_list.append(anat_temp) # Dictyostelium Discoideum Anatomy Ontology elif "DDANAT" in result["@id"]: ddanat_id = result["@id"].split("/obo/")[1] if ddanat_id not in anat_id_array: anat_id_array.append(ddanat_id) anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=ddanat_id, identifier_type="DDANAT ID", source="NCBO BioPortal", name=result["prefLabel"], taxon="Dictyostelium discoideum") anat_list.append(anat_temp) # Body System Terms from ICD-11 elif "ICD11-BODYSYSTEM" in result["@id"]: icd11_id = result["@id"].split("/obo/")[1] if icd11_id not in anat_id_array: anat_id_array.append(icd11_id) anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=icd11_id, identifier_type="ICD11-BODYSYSTEM Code", source="NCBO BioPortal", name=result["prefLabel"]) anat_list.append(anat_temp) # Mosquito Gross Anatomy Ontology elif "TGMA" in result["@id"]: tgma_id = result["@id"].split("/obo/")[1] if tgma_id not in anat_id_array: anat_id_array.append(tgma_id) anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=tgma_id, identifier_type="TGMA ID", source="NCBO BioPortal", name=result["prefLabel"], taxon="Culicidae") anat_list.append(anat_temp) # Mouse Adult Gross Anatomy Ontology elif "MA" in result["@id"]: ma_id = result["@id"].split("/obo/")[1] if ma_id not in anat_id_array: anat_id_array.append(ma_id) anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=ma_id, identifier_type="MA ID", source="NCBO BioPortal", name=result["prefLabel"], taxon="Mus") anat_list.append(anat_temp) # Hymenoptera Anatomy Ontology elif "HAO" in result["@id"]: hao_id = result["@id"].split("/obo/")[1] if hao_id not in anat_id_array: anat_id_array.append(hao_id) anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=hao_id, identifier_type="HAO ID", source="NCBO BioPortal", name=result["prefLabel"], taxon="Hymenoptera") anat_list.append(anat_temp) # Zebrafish Anatomy and Development Ontology elif "ZFA" in result["@id"]: zfa_id = result["@id"].split("/obo/")[1] if zfa_id not in anat_id_array: anat_id_array.append(zfa_id) anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=zfa_id, identifier_type="ZFA ID", source="NCBO BioPortal", name=result["prefLabel"], taxon="Danio rerio") anat_list.append(anat_temp) # Plant Anatomy elif "PAE" in result["@id"]: pae_id = result["@id"].split("/obo/")[1] if pae_id not in anat_id_array: anat_id_array.append(pae_id) anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=pae_id, identifier_type="PAE ID", source="NCBO BioPortal", name=result["prefLabel"], taxon="Plantae") anat_list.append(anat_temp) # Xenopus Anatomy and Development Ontology elif "XAO" in result["@id"]: xao_id = result["@id"].split("/obo/")[1] if xao_id not in anat_id_array: anat_id_array.append(xao_id) anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=xao_id, identifier_type="XAO ID", source="NCBO BioPortal", name=result["prefLabel"], taxon="Xenopus laevis") anat_list.append(anat_temp) # Uber Anatomy Ontology elif "UBERON" in result["@id"]: uberon_id = result["@id"].split("/obo/")[1] if uberon_id not in anat_id_array: anat_id_array.append(uberon_id) anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=uberon_id, identifier_type="UBERON ID", source="NCBO BioPortal", name=result["prefLabel"], taxon=None) anat_list.append(anat_temp) # Maize Gross Anatomy Ontology elif "ZEA" in result["@id"]: zea_id = result["@id"].split("/obo/")[1] if zea_id not in anat_id_array: anat_id_array.append(zea_id) anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=zea_id, identifier_type="ZEA ID", source="NCBO BioPortal", name=result["prefLabel"], taxon="Zea mays") anat_list.append(anat_temp) # Drosophila Gross Anatomy Ontology elif "FB-BT" in result["@id"]: fbbt_id = result["@id"].split("/obo/")[1] if fbbt_id not in anat_id_array: anat_id_array.append(fbbt_id) anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=fbbt_id, identifier_type="FB-BT ID", source="NCBO BioPortal", name=result["prefLabel"], taxon="Drosophila melanogaster") anat_list.append(anat_temp) # Fungal Gross Anatomy Ontology elif "FAO" in result["@id"]: fao_id = result["@id"].split("/obo/")[1] if fao_id not in anat_id_array: anat_id_array.append(fao_id) anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=fao_id, identifier_type="FAO ID", source="NCBO BioPortal", name=result["prefLabel"], taxon="Fungi") anat_list.append(anat_temp) # C. elegans Gross Anatomy Ontology elif "WB-BT" in result["@id"]: wbbt_id = result["@id"].split("/obo/")[1] if wbbt_id not in anat_id_array: anat_id_array.append(wbbt_id) anat_temp = gnomics.objects.anatomical_structure.AnatomicalStructure( identifier=wbbt_id, identifier_type="WB-BT ID", source="NCBO BioPortal", name=result["prefLabel"], taxon="Caenorhabditis elegans") anat_list.append(anat_temp) return anat_list
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_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_cpm(anat, user=None, source="umls"): cpm_array = [] for iden in anat.identifiers: if iden["identifier_type"].lower() in [ "cpm", "cpm code", "cpm id", "cpm identifier", "med", "med code", "med id", "med identifier" ] and iden["identifier"] not in cpm_array: cpm_array.append(iden["identifier"]) if cpm_array: return cpm_array for iden in anat.identifiers: if iden["identifier_type"].lower() in [ "neu id", "neu identifier", "neu", "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"] with open("sample_2.txt", "a", encoding="utf8") as f: f.write(str(items)) for rep in json_data: if rep["ui"] != "NONE": # CPM ID. if rep["rootSource"] == "CPM" and rep[ "ui"] not in cpm_array: cpm_array.append(rep["ui"]) gnomics.objects.anatomical_structure.AnatomicalStructure.add_identifier( anat, identifier=rep["ui"], identifier_type="CPM ID", language=None, source="UMLS Metathesaurus") if not json_data: break elif iden["identifier_type"].lower() in [ "uwda id", "uwda identifier", "uwda" ]: 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/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"] with open("sample_2.txt", "a", encoding="utf8") as f: f.write(str(items)) for rep in json_data: if rep["ui"] != "NONE": # CPM ID. if rep["rootSource"] == "CPM" and rep[ "ui"] not in cpm_array: cpm_array.append(rep["ui"]) gnomics.objects.anatomical_structure.AnatomicalStructure.add_identifier( anat, identifier=rep["ui"], identifier_type="CPM ID", language=None, source="UMLS Metathesaurus") if not json_data: break elif iden["identifier_type"].lower() in [ "rcd", "rcd id", "rcd identifier", "read code", "read code clinical terms version 3", "read code ctv3", "read code, clinical terms version 3", "read code, ctv3" ]: if source == "umls": anat_array = [] 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"] != "NONE": # CPM ID. if rep["rootSource"] == "CPM" and rep[ "ui"] not in cpm_array: cpm_array.append(rep["ui"]) gnomics.objects.anatomical_structure.AnatomicalStructure.add_identifier( anat, identifier=rep["ui"], identifier_type="CPM ID", language=None, source="UMLS Metathesaurus", name=rep["name"]) if not json_data: break elif iden["identifier_type"].lower() in [ "alcohol and other drug", "alcohol and other drug id", "alcohol and other drug identifier", "aod", "aod id", "aod identifier" ]: if source == "umls": anat_array = [] 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": # CPM ID. if rep["rootSource"] == "CPM" and rep[ "ui"] not in cpm_array: cpm_array.append(rep["ui"]) gnomics.objects.anatomical_structure.AnatomicalStructure.add_identifier( anat, identifier=rep["ui"], identifier_type="CPM ID", language=None, source="UMLS Metathesaurus", name=rep["name"]) if not json_data: break return cpm_array
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_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
def get_mesh_uid(adverse_event, user=None): mesh_array = [] for iden in gnomics.objects.auxiliary_files.identifier.filter_identifiers( adverse_event.identifiers, [ "mesh", "mesh uid", "mesh unique id", "mesh unique identifier", "msh", "msh uid", "msh unique id", "msh unique identifier" ]): if iden["identifier"] not in mesh_array: mesh_array.append(iden["identifier"]) if mesh_array: return mesh_array ids_completed = [] for iden in gnomics.objects.auxiliary_files.identifier.filter_identifiers( adverse_event.identifiers, [ "mdr", "mdr code", "mdr id", "mdr identifier", "meddra", "meddra code", "meddra id", "meddra identifier" ]): if iden["identifier"] not in ids_completed and user is not None: ids_completed.append(iden["identifier"]) if 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 = "/crosswalk/current/source/MDR/" + str( iden["identifier"]) + "?targetSource=MSH" 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' if not r.ok: break else: items = json.loads(r.text) json_data = items["result"] for er in json_data: if er["ui"] not in mesh_array and er[ "ui"] != "NONE": mesh_uid = er["ui"] mesh_name = er["name"] mesh_array.append(mesh_uid) gnomics.objects.adverse_event.AdverseEvent.add_identifier( adverse_event, identifier=mesh_uid, identifier_type="MeSH UID", source="UMLS", name=mesh_name) if not json_data: break if user.ncbo_api_key is not None: base = "http://data.bioontology.org/ontologies/" ext = "MEDDRA/classes/http%3A%2F%2Fpurl.bioontology.org%2Fontology%2FMEDDRA%2F" + str( iden["identifier"] ) + "/mappings/?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) for result in decoded: for subresult in result["classes"]: if "http://purl.bioontology.org/ontology/MESH" in subresult[ "@id"]: mesh_uid = subresult["@id"].split("/MESH/")[1] if mesh_uid not in mesh_array: mesh_array.append(mesh_uid) gnomics.objects.adverse_event.AdverseEvent.add_identifier( adverse_event, identifier=mesh_uid, identifier_type="MeSH UID", source="NCBO BioPortal", name=subresult["prefLabel"]) elif user is None: print( "A valid user with a valid UMLS API Key or valid NCBO BioPortal API Key is necessary for this transaction." ) return mesh_array
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_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_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 search(query, user=None, search_type="exact", return_id_type="sourceUi"): if search_type == "exact" and user is not None: proc_array = [] proc_obj_array = [] umls_tgt = User.umls_tgt(user) page_num = 0 base = "https://uts-ws.nlm.nih.gov/rest" ext = "/search/current?string=" + query + "&inputType=sourceUi&searchType=words&returnIdType=" + return_id_type while True: tick = User.umls_st(umls_tgt) page_num += 1 query = {"string": query, "ticket": tick, "pageNumber": page_num} r = requests.get(base + ext, params=query) r.encoding = 'utf-8' #print(r.text) items = json.loads(r.text) json_data = items["result"] for rep in json_data["results"]: if rep["ui"] not in proc_array and rep["ui"] != "NONE": # Current Procedural Terminology. if rep["rootSource"] == "CPT": temp_proc = gnomics.objects.procedure.Procedure( identifier=rep["ui"], identifier_type="CPT ID", language=None, source="UMLS Metathesaurus", name=rep["name"]) proc_obj_array.append(temp_proc) # Current Procedural Terminology (Spanish). elif rep["rootSource"] == "CPTSP": temp_proc = gnomics.objects.procedure.Procedure( identifier=rep["ui"], identifier_type="CPTSP ID", language=None, source="UMLS Metathesaurus", name=rep["name"]) proc_obj_array.append(temp_proc) # Healthcare Common Procedure Coding System (HCPCS). elif rep["rootSource"] == "HCPCS": temp_proc = gnomics.objects.procedure.Procedure( identifier=rep["ui"], identifier_type="HCPCS ID", language=None, source="UMLS Metathesaurus", name=rep["name"]) proc_obj_array.append(temp_proc) # CPT in Healthcare Common Procedure Coding System (CPT in HCPCS). elif rep["rootSource"] == "HCPT": temp_proc = gnomics.objects.procedure.Procedure( identifier=rep["ui"], identifier_type="HCPT ID", language=None, source="UMLS Metathesaurus", name=rep["name"]) proc_obj_array.append(temp_proc) # ICD-10-PCS. elif rep["rootSource"] == "ICD-10-PCS": temp_proc = gnomics.objects.procedure.Procedure( identifier=rep["ui"], identifier_type="ICD-10-PCS Code", language=None, source="UMLS Metathesaurus", name=rep["name"]) proc_obj_array.append(temp_proc) # LOINC (English). elif rep["rootSource"] == "LNC": temp_proc = gnomics.objects.procedure.Procedure( identifier=rep["ui"], identifier_type="LOINC Code", source="UMLS Metathesaurus", name=rep["name"], language="en") proc_obj_array.append(temp_proc) # LOINC (Austrian German). elif rep["rootSource"] == "LNC-DE-AT": temp_proc = gnomics.objects.procedure.Procedure( identifier=rep["ui"], identifier_type="LOINC Code", source="UMLS Metathesaurus", name=rep["name"], language="de-AT") proc_obj_array.append(temp_proc) # LOINC (Swiss German). elif rep["rootSource"] == "LNC-DE-CH": temp_proc = gnomics.objects.procedure.Procedure( identifier=rep["ui"], identifier_type="LOINC Code", source="UMLS Metathesaurus", name=rep["name"], language="gsw") proc_obj_array.append(temp_proc) # LOINC (German). elif rep["rootSource"] == "LNC-DE-DE": temp_proc = gnomics.objects.procedure.Procedure( identifier=rep["ui"], identifier_type="LOINC Code", source="UMLS Metathesaurus", name=rep["name"], language="de") proc_obj_array.append(temp_proc) # LOINC (Greek). elif rep["rootSource"] == "LNC-EL-GR": temp_proc = gnomics.objects.procedure.Procedure( identifier=rep["ui"], identifier_type="LOINC Code", source="UMLS Metathesaurus", name=rep["name"], language="el") proc_obj_array.append(temp_proc) # LOINC (Argentinian Spanish). elif rep["rootSource"] == "LNC-ES-AR": temp_proc = gnomics.objects.procedure.Procedure( identifier=rep["ui"], identifier_type="LOINC Code", source="UMLS Metathesaurus", name=rep["name"], language="es-AR") proc_obj_array.append(temp_proc) # LOINC (Swiss Spanish). elif rep["rootSource"] == "LNC-ES-CH": temp_proc = gnomics.objects.procedure.Procedure( identifier=rep["ui"], identifier_type="LOINC Code", source="UMLS Metathesaurus", name=rep["name"], language="es-CH") proc_obj_array.append(temp_proc) # LOINC (Spanish). elif rep["rootSource"] == "LNC-ES-ES": temp_proc = gnomics.objects.procedure.Procedure( identifier=rep["ui"], identifier_type="LOINC Code", source="UMLS Metathesaurus", name=rep["name"], language="es") proc_obj_array.append(temp_proc) # LOINC (Estonian). elif rep["rootSource"] == "LNC-ET-EE": temp_proc = gnomics.objects.procedure.Procedure( identifier=rep["ui"], identifier_type="LOINC Code", source="UMLS Metathesaurus", name=rep["name"], language="et") proc_obj_array.append(temp_proc) # LOINC (Belgian French). elif rep["rootSource"] == "LNC-FR-BE": temp_proc = gnomics.objects.procedure.Procedure( identifier=rep["ui"], identifier_type="LOINC Code", source="UMLS Metathesaurus", name=rep["name"], language="fr-BE") proc_obj_array.append(temp_proc) # LOINC (Canadian French). elif rep["rootSource"] == "LNC-FR-CA": temp_proc = gnomics.objects.procedure.Procedure( identifier=rep["ui"], identifier_type="LOINC Code", source="UMLS Metathesaurus", name=rep["name"], language="fr-CA") proc_obj_array.append(temp_proc) # LOINC (Swiss French). elif rep["rootSource"] == "LNC-FR-CH": temp_proc = gnomics.objects.procedure.Procedure( identifier=rep["ui"], identifier_type="LOINC Code", source="UMLS Metathesaurus", name=rep["name"], language="fr-CH") proc_obj_array.append(temp_proc) # LOINC (French). elif rep["rootSource"] == "LNC-FR-FR": temp_proc = gnomics.objects.procedure.Procedure( identifier=rep["ui"], identifier_type="LOINC Code", source="UMLS Metathesaurus", name=rep["name"], language="fr") proc_obj_array.append(temp_proc) # LOINC (Swiss Italian). elif rep["rootSource"] == "LNC-IT-CH": temp_proc = gnomics.objects.procedure.Procedure( identifier=rep["ui"], identifier_type="LOINC Code", source="UMLS Metathesaurus", name=rep["name"], language="it-CH") proc_obj_array.append(temp_proc) # LOINC (Italian). elif rep["rootSource"] == "LNC-IT-IT": temp_proc = gnomics.objects.procedure.Procedure( identifier=rep["ui"], identifier_type="LOINC Code", source="UMLS Metathesaurus", name=rep["name"], language="it") proc_obj_array.append(temp_proc) # LOINC (Korean). elif rep["rootSource"] == "LNC-KO-KR": temp_proc = gnomics.objects.procedure.Procedure( identifier=rep["ui"], identifier_type="LOINC Code", source="UMLS Metathesaurus", name=rep["name"], language="ko") proc_obj_array.append(temp_proc) # LOINC (Dutch). elif rep["rootSource"] == "LNC-NL-NL": temp_proc = gnomics.objects.procedure.Procedure( identifier=rep["ui"], identifier_type="LOINC Code", source="UMLS Metathesaurus", name=rep["name"], language="nl") proc_obj_array.append(temp_proc) # LOINC (Brazilian Portuguese). elif rep["rootSource"] == "LNC-PT-BR": temp_proc = gnomics.objects.procedure.Procedure( identifier=rep["ui"], identifier_type="LOINC Code", source="UMLS Metathesaurus", name=rep["name"], language="pt-BR") proc_obj_array.append(temp_proc) # LOINC (Russian). elif rep["rootSource"] == "LNC-RU-RU": temp_proc = gnomics.objects.procedure.Procedure( identifier=rep["ui"], identifier_type="LOINC Code", source="UMLS Metathesaurus", name=rep["name"], language="ru") proc_obj_array.append(temp_proc) # LOINC (Turkish). elif rep["rootSource"] == "LNC-TR-TR": temp_proc = gnomics.objects.procedure.Procedure( identifier=rep["ui"], identifier_type="LOINC Code", source="UMLS Metathesaurus", name=rep["name"], language="tr") proc_obj_array.append(temp_proc) # LOINC (Chinese). elif rep["rootSource"] == "LNC-ZH-CN": temp_proc = gnomics.objects.procedure.Procedure( identifier=rep["ui"], identifier_type="LOINC Code", source="UMLS Metathesaurus", name=rep["name"], language="zh") proc_obj_array.append(temp_proc) if json_data["results"][0]["ui"] == "NONE": break return proc_obj_array elif search_type == "approximate" and user is not None: proc_array = [] umls_tgt = User.umls_tgt(user) page_num = 0 base = "https://uts-ws.nlm.nih.gov/rest" ext = "/search/current?string=" + query + "&searchType=approximate&returnIdType=" + return_id_type while True: tick = User.umls_st(umls_tgt) page_num += 1 query = {"string": query, "ticket": tick, "pageNumber": page_num} r = requests.get(base + ext, params=query) r.encoding = 'utf-8' print(r.text) items = json.loads(r.text) json_data = items["result"] for rep in json_data["results"]: if rep["ui"] not in proc_array and rep["ui"] != "NONE": proc_array.append(rep["ui"]) if json_data["results"][0]["ui"] == "NONE": break return proc_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_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_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 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 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_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_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_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