Skip to content

Instantly share code, notes, and snippets.

@caodac
Created December 23, 2020 17:07
Show Gist options
  • Save caodac/31adace280e37462b9a8d4b36bd6d0ed to your computer and use it in GitHub Desktop.
Save caodac/31adace280e37462b9a8d4b36bd6d0ed to your computer and use it in GitHub Desktop.
download data from drugs.ncats.io
import requests
import sys
if len(sys.argv) == 1:
print('usage: %s FILE...' % sys.argv[0])
sys.exit(1)
def get_additional_data(id):
r = requests.get('https://drugs.ncats.io/api/v1/substances(%s)/@additional' % id)
if 200 == r.status_code:
return r.json()
return None
def search_inxight(name):
params = {
'q': f'root_names_name:"^{name}$"'
}
r = requests.get('https://drugs.ncats.io/api/v1/substances/search', params)
if 200 != r.status_code:
return None
json = r.json()
count = json['count']
if count == 0:
print('%s\t\t\t\t\t\t' % name)
return None
#print('%s: %s' % (name, r.url))
if count > 1:
print('%s: returns %d results!' % (name, count))
r = json['content'][0]
#print('...%s' % r['uuid'])
data = get_additional_data(r['uuid'])
if data != None:
status = None
year = None
conds = []
for d in data:
if (d['name'] == 'Highest Development Event'
or (d['name'] == 'Earliest Approved Event' and status == None)):
status = d['value']
elif d['name'] == 'Approval Year':
year = d['value']
elif d['name'] == 'Conditions':
conds.append(d['value']['label'])
if status != None and not status['withdrawn']:
print('%s\t%s\t%s\t%s\t%s\t%s\t%s' % (
name, r['approvalID'], r['_name'], year, ';'.join(conds),
status['status'], status['sourceURL']))
for a in sys.argv[1:]:
with open(a) as f:
print('Propriety_Name\tUNII\tPreferred_Name\tYear\tConditions\tStatus\tSource')
for l in f.readlines():
search_inxight(l.strip())
@JulesUKN
Copy link

JulesUKN commented Aug 18, 2023

Hi,

I find the documentation from drugs.ncats (https://drugs.ncats.io/api/v1) not very helpful. Would it be possible to share how you know that one can use "@Additional" in "https://drugs.ncats.io/api/v1/substances(%s)/@additional"
Best
Jules

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment