Last active
June 4, 2021 16:09
-
-
Save d0choa/2e20548bd8b4e7c191e5b1c3ea4cd81a to your computer and use it in GitHub Desktop.
Querying the known drugs for a given target from Open Targets Platform GraphQL API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Import relevant libraries for HTTP request and JSON formatting | |
import requests | |
import json | |
# Set gene_id variable | |
gene_id = "ENSG00000244734" | |
# Build query string | |
# Extracting | |
query_string = """ | |
query target($ensemblId: String!){ | |
target(ensemblId: $ensemblId){ | |
approvedSymbol | |
knownDrugs{ | |
rows{ | |
drug{ | |
name | |
} | |
disease{ | |
id | |
name | |
} | |
phase | |
drugType | |
urls{ | |
name | |
url | |
} | |
} | |
} | |
} | |
} | |
""" | |
# Set variables object of arguments to be passed to endpoint | |
variables = {"ensemblId": gene_id} | |
# Set base URL of GraphQL API endpoint | |
base_url = "https://api.platform.opentargets.org/api/v4/graphql" | |
# Perform POST request and check status code of response | |
r = requests.post(base_url, json={"query": query_string, "variables": variables}) | |
print(r.status_code) | |
# Transform API response into JSON | |
api_response_as_json = json.loads(r.text) | |
# Print API response to terminal | |
print(api_response_as_json) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I haven't fully checked with a target with many drugs. It might be necessary to use the
knownDrugs(cursor: XXXX)
option to paginate the results.