-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtarget_disease.py
81 lines (65 loc) · 3 KB
/
target_disease.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# Import relevant libraries
import requests
import json
def get_target_disease_association(ensembl_id, disease_keyword_list, index=0, size=100):
# Set variables object of arguments to be passed to endpoint
# ensembl_id taken in as elements of a list for multiple queries
api_response_as_json_data_list = []
for diseases in disease_keyword_list:
variables = {"ensemblId": ensembl_id,
"disease": diseases,
"index": index,
"size": size}
# Build query string
query_string = """
query target($ensemblId: String!, $disease: String!, $index: Int!, $size: Int!){
target(ensemblId: $ensemblId){
id
approvedSymbol
approvedName
associatedDiseases(BFilter: $disease, page: {
index: $index,
size: $size
}){
count
rows{
disease{
name
id
}
score
}
}
}
}
"""
# Set base URL of GraphQL API endpoint
base_url = "https://api.platform.opentargets.org/api/v4/graphql"
# Perform POST requests and check status code of response
json_query = {"query": query_string,
"variables": variables}
r = requests.post(base_url, json=json_query)
# Transform API response into JSON format
api_response_as_json = json.loads(r.text)
api_response_as_json_data = api_response_as_json["data"]
association_score_list = api_response_as_json_data["target"]["associatedDiseases"]["rows"]
api_response_as_json_data_list.extend(association_score_list)
api_response_as_json_data_list_unique = \
list({v['disease']['id']: v for v in api_response_as_json_data_list}.values())
return api_response_as_json_data_list_unique
def extract_association_scores(disease_list_unique):
associated_diseases_count = len(disease_list_unique)
# Using list comprehension to iterate through list of association information, and retrieve disease name,
# id and score
association_score_nested_list = \
[[score["disease"]["name"], score["disease"]["id"], score["score"]] for score in disease_list_unique]
return associated_diseases_count, association_score_nested_list
def aggregate_association_scores(association_score_nested_list):
# Index [2] gives position 3, where the disease association score is stored in the nested list
if len(association_score_nested_list) == 0:
average_score = 0
else:
total_score = sum(disease[2] for disease in association_score_nested_list)
total_count = len(association_score_nested_list)
average_score = total_score / total_count
return average_score