From e2edf1eb6b3e83cec4b042e4726d4ed634cbcd2f Mon Sep 17 00:00:00 2001 From: youainti Date: Thu, 23 Mar 2023 16:08:08 -0700 Subject: [PATCH] created base of umls linking script --- scripts/umls_requests.py | 58 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 scripts/umls_requests.py diff --git a/scripts/umls_requests.py b/scripts/umls_requests.py new file mode 100644 index 0000000..8d3da9a --- /dev/null +++ b/scripts/umls_requests.py @@ -0,0 +1,58 @@ +from dotenv import dotenv_values +import requests +import json +from drugtools.env_setup import ENV,postgres_conn +from psycopg2 import extras + + + +class Requestor(): + def __init__(self,api_key): + self.key = api_key + + def search(self,search_term,inputType="sourceUi", returnIdType="code", addnl_terms={}): + query_terms = { + "apiKey":self.key, + "sabs":"ICD10", + "string":search_term, + "returnIdType":returnIdType, + "inputType":inputType + } | addnl_terms + query = "https://uts-ws.nlm.nih.gov/rest/search/current/" + + r = requests.get(query,params=query_terms) + print(r.url) + return r + + +r = Requestor(ENV.get("UMLS_API_KEY")) +print(json.dumps(r.search("leukemia").json(),indent=2)) + + +conditions_link = {} + +pconn = postgres_conn() +pcurse = pconn.cursor(cursor_factory=extras.DictCursor) +sql = """ +select nct_id, downcase_mesh_term +from ctgov.browse_conditions bc +where +mesh_type = 'mesh-list' +and +nct_id in (select distinct nct_id from history.trial_snapshots ts) +order by nct_id +; +""" +pcurse.execute(sql) +rows = pcurse.fetchall() +for row in rows: + nctid = row[0] + condition = row[1] + print(nctid,condition) + + results = r.search(row[1]).json().get('result', Exception("No result entry in json")).get('results',Exception("No results entry in json")) + for entry in results: + print("\t", entry["ui"], entry["name"]) + + +