Skip to content

Instantly share code, notes, and snippets.

@d0choa
Last active June 4, 2021 16:09
Show Gist options
  • Save d0choa/2e20548bd8b4e7c191e5b1c3ea4cd81a to your computer and use it in GitHub Desktop.
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
#!/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)
@d0choa
Copy link
Author

d0choa commented Jun 4, 2021

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.

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