-
Notifications
You must be signed in to change notification settings - Fork 0
/
genetics.py
182 lines (145 loc) · 6.51 KB
/
genetics.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
from sgqlc.endpoint.http import HTTPEndpoint
from sgqlc.operation import Operation
from graphql_types import *
from pandas import json_normalize
import pandas as pd
import numpy as np
class Genetics:
url = 'https://api.genetics.opentargets.org/graphql'
endpoint = HTTPEndpoint(url)
def genes_for_variant(self, variants, data_frame=True):
op = Operation(Query)
for variant in variants:
op.genes_for_variant(variant_id=variant, __alias__='alias_' + variant).__fields__()
data = self.endpoint(op)
if data_frame:
return json_normalize([alias for aliases in data['data'].values() for alias in aliases], sep='_')
else:
result = (op+data)
return {x: getattr(result, f'alias_{x}') for x in variants}
def search_rsid(self, rs_ids, data_frame=True):
op = Operation(Query)
for rs in rs_ids:
s = op.search(query_string=rs, __alias__='alias_' + rs)
s.variants.id()
s.variants.rs_id()
data = self.endpoint(op)
if data_frame:
dfs = []
for q, aliases in data['data'].items():
if aliases['variants']:
for alias in aliases['variants']:
dfs.append(pd.DataFrame(dict(id=[alias['id']])).assign(rsid=q.split('alias_')[1]))
else:
dfs.append(pd.DataFrame(dict(id=[np.nan])).assign(rsid=q.split('alias_')[1]))
if dfs:
return pd.concat(dfs, axis=0).reset_index(drop=True)
else:
result = (op+data)
return {x: getattr(result, f'alias_{x}') for x in rs_ids}
def study_info(self, study_ids, data_frame=True):
op = Operation(Query)
for study_id in study_ids:
s = op.study_info(study_id=study_id, __alias__='alias_' + study_id)
s.__fields__()
try:
data = self.endpoint(op)
except:
data = self.endpoint(op)
if data_frame:
return pd.json_normalize(data['data'].values(), sep='_')
else:
result = (op+data)
return {x: getattr(result, f'alias_{x}') for x in study_ids}
def variant_info(self, variant_ids, data_frame=True):
op = Operation(Query)
for variant_id in variant_ids:
s = op.variant_info(variant_id=variant_id, __alias__='alias_' + variant_id)
s.__fields__()
try:
data = self.endpoint(op)
except:
data = self.endpoint(op)
if data_frame:
return pd.json_normalize(data['data'].values(), sep='_')
else:
result = (op+data)
return {x: getattr(result, f'alias_{x}') for x in variant_ids}
def top_overlapped_studies(self, study_ids, data_frame=True):
op = Operation(Query)
for study_id in study_ids:
s = op.top_overlapped_studies(study_id=study_id, __alias__='alias_' + study_id)
s.__fields__()
data = self.endpoint(op)
if data_frame:
return pd.json_normalize(data['data'].values(), sep='_')
else:
result = (op+data)
return {x: getattr(result, f'alias_{x}') for x in study_ids}
def manhattan(self, study_ids, data_frame=True):
op = Operation(Query)
for study_id in study_ids:
m = op.manhattan(study_id=study_id, __alias__='alias_' + study_id)
m.associations.best_genes.gene.id()
m.associations.best_genes.gene.symbol()
m.associations.best_coloc_genes.gene.id()
m.associations.best_coloc_genes.gene.symbol()
m.associations.best_locus2_genes.gene.id()
m.associations.best_locus2_genes.gene.symbol()
m.associations.best_locus2_genes.score()
m.associations().__fields__()
m.associations().variant().__fields__()
try:
data = self.endpoint(op)
except:
data = self.endpoint(op)
if data_frame:
dfs = []
for study, aliases in data['data'].items():
for alias in aliases['associations']:
dfs.append(json_normalize(alias, sep='_').assign(study=study.split('alias_')[1]))
if dfs:
return pd.concat(dfs, axis=0).reset_index(drop=True)
else:
result = (op+data)
return {x: getattr(result, f'alias_{x}') for x in study_ids}
def tag_variants_and_studies_for_index_variant(self, index_variants, study_id=None, data_frame=True):
op = Operation(Query)
for index_variant in index_variants:
s = op.tag_variants_and_studies_for_index_variant(variant_id=index_variant, __alias__='alias_' + index_variant)
s.associations.study.__fields__('study_id')
s.associations().__fields__()
data = self.endpoint(op)
if data_frame:
dfs = []
for variant, aliases in data['data'].items():
for alias in aliases['associations']:
dfs.append(json_normalize(alias, sep='_').assign(queryVariant=variant.split('alias_')[1]))
if dfs:
df = pd.concat(dfs, axis=0).reset_index(drop=True)
if study_id is not None:
df = df[df.study_studyId == study_id]
return df
else:
result = (op+data)
return {x: getattr(result, f'alias_{x}') for x in study_ids}
def index_variants_and_studies_for_tag_variant(self, index_variants, study_id=None, data_frame=True):
op = Operation(Query)
for index_variant in index_variants:
s = op.index_variants_and_studies_for_tag_variant(variant_id=index_variant, __alias__='alias_' + index_variant)
s.associations.study.__fields__('study_id')
s.associations().__fields__()
data = self.endpoint(op)
if data_frame:
dfs = []
for variant, aliases in data['data'].items():
for alias in aliases['associations']:
dfs.append(json_normalize(alias, sep='_').assign(queryVariant=variant.split('alias_')[1]))
if dfs:
df = pd.concat(dfs, axis=0).reset_index(drop=True)
if study_id is not None:
df = df[df.study_studyId == study_id]
return df
else:
result = (op+data)
return {x: getattr(result, f'alias_{x}') for x in study_ids}