I am trying to download association scores from a list of IDs using the api/v4/graphql but it feels cumbersome as i am iterating through the lists and making individual calls then concatenating into a dataframe (python code appended below). Probably there is a better way of doing this, any recommendations will be deeply appreciated
import pandas as pd
import json
import requests
dfENSG=dfENSG.str.strip()#contains #gene_id like "ENSG00000169093"...
colS=pd.DataFrame(columns=["ID","name","disease", "datasourceScores"])
cntG=0;
for gene_id in dfENSG:
cntG=cntG+1
print(cntG,gene_id)
query_string = """
query target($ensemblId: String!){
target(ensemblId: $ensemblId){
id
approvedSymbol
associatedDiseases {
count
rows {
disease {
id
name
}
datasourceScores {
id
score
}
}
}
}
}
"""
variables = {"ensemblId": gene_id}
base_url = "https://api.platform.opentargets.org/api/v4/graphql"
r = requests.post(base_url, json={"query": query_string, "variables": variables})
print(r.status_code)
if r.status_code==200:
api_response = json.loads(r.text)
iD=api_response['data']['target']['id']
print(iD)
iG=api_response['data']['target']['approvedSymbol']
print(iG)
aS=api_response['data']['target']['associatedDiseases']['rows']#[1]['datasourceScores'][0]['score']
dataOIDP=pd.DataFrame(aS,columns=["disease", "datasourceScores"])
dataOIDP["ID"]=iD
dataOIDP["name"]=iG
colS=pd.concat([colS,dataOIDP])
colS.to_csv("openTargetResults.csv")