-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlineage.py
349 lines (334 loc) · 15.1 KB
/
lineage.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
from .base import BaseHandler
from tornado import gen
import pandas as pd
from .util import create_nested_mutation_query, calculate_proportion, parse_location_id_to_query, get_total_hits
import re
class LineageByCountryHandler(BaseHandler):
@gen.coroutine
def _get(self):
query_pangolin_lineage = self.get_argument("pangolin_lineage", None)
query_mutations = self.get_argument("mutations", None)
query = {
"aggs": {
"prevalence": {
"filter" : {},
"aggs": {
"country": {
"terms": {
"field": "country",
"size": self.size
}
}
}
}
}
}
query_mutations = query_mutations.split(",") if query_mutations is not None else []
query_pangolin_lineage = query_pangolin_lineage.split(",") if query_pangolin_lineage is not None else []
query_obj = create_nested_mutation_query(lineages = query_pangolin_lineage, mutations = query_mutations)
query["aggs"]["prevalence"]["filter"] = query_obj
resp = yield self.asynchronous_fetch(query)
return resp
class LineageByDivisionHandler(BaseHandler):
@gen.coroutine
def _get(self):
query_pangolin_lineage = self.get_argument("pangolin_lineage", None)
query_country = self.get_argument("country", None)
query_mutations = self.get_argument("mutations", None)
query = {
"aggs": {
"prevalence": {
"filter" : {},
"aggs": {
"division": {
"terms": {
"field": "division",
"size": self.size
}
}
}
}
}
}
query_mutations = query_mutations.split(",") if query_mutations is not None else []
query_pangolin_lineage = query_pangolin_lineage.split(",") if query_pangolin_lineage is not None else []
query_obj = create_nested_mutation_query(country = query_country, lineages = query_pangolin_lineage, mutations = query_mutations)
query["aggs"]["prevalence"]["filter"] = query_obj
print(query)
resp = yield self.asynchronous_fetch(query)
return resp
# Calculate total number of sequences with a particular lineage in a country
class LineageAndCountryHandler(BaseHandler):
@gen.coroutine
def _get(self):
query_country = self.get_argument("country", None)
query_pangolin_lineage = self.get_argument("pangolin_lineage", None)
query_mutations = self.get_argument("mutations", None)
query = {
"query": {}
}
query_mutations = query_mutations.split(",") if query_mutations is not None else []
query_pangolin_lineage = query_pangolin_lineage.split(",") if query_pangolin_lineage is not None else []
query_obj = create_nested_mutation_query(country = query_country, lineages = query_pangolin_lineage, mutations = query_mutations)
query["query"] = query_obj
resp = yield self.asynchronous_fetch(query)
return resp
# Calculate total number of sequences with a particular lineage in a division
class LineageAndDivisionHandler(BaseHandler):
@gen.coroutine
def _get(self):
query_country = self.get_argument("country", None)
query_division = self.get_argument("division", None)
query_pangolin_lineage = self.get_argument("pangolin_lineage", None)
query_mutations = self.get_argument("mutations", None)
query = {
"query": {}
}
query_mutations = query_mutations.split(",") if query_mutations is not None else []
query_pangolin_lineage = query_pangolin_lineage.split(",") if query_pangolin_lineage is not None else []
query_obj = create_nested_mutation_query(country = query_country, division = query_division, lineages = query_pangolin_lineage, mutations = query_mutations)
query["query"] = query_obj
resp = yield self.asynchronous_fetch(query)
return resp
class LineageHandler(BaseHandler):
@gen.coroutine
def _get(self):
query_str = self.get_argument("name", None)
size = self.get_argument("size", None)
query = {
"size": 0,
"query": {
"wildcard": {
"pangolin_lineage": {
"value": query_str
}
}
},
"aggs": {
"lineage": {
"terms": {
"field": "pangolin_lineage",
"size": 10000
}
}
}
}
resp = yield self.asynchronous_fetch(query)
path_to_results = ["aggregations", "lineage", "buckets"]
buckets = resp
for i in path_to_results:
buckets = buckets[i]
flattened_response = [{
"name": i["key"],
"total_count": i["doc_count"]
} for i in buckets]
if size:
try:
size = int(size)
except Exception:
return {"success": False, "results": [], "errors": "Invalide size value"}
flattened_response = sorted(flattened_response, key=lambda x: -x["total_count"])
flattened_response = flattened_response[:size]
resp = {"success": True, "results": flattened_response}
return resp
class LineageMutationsHandler(BaseHandler):
gene_mapping = {
"orf1a" : "ORF1a",
"orf1b" : "ORF1b",
"s" : "S",
"orf3a" : "ORF3a",
"e": "E",
"m" : "M",
"orf6": "ORF6",
"orf7a" : "ORF7a",
"orf7b" : "ORF7b",
"orf8" : "ORF8",
"n" : "N",
"orf10" : "ORF10"
}
@gen.coroutine
def _get(self):
pangolin_lineage = self.get_argument("pangolin_lineage", None)
frequency = self.get_argument("frequency", None)
frequency = float(frequency) if frequency != None else 0.8
dict_response = {}
# Query structure: Lineage 1 OR Lineage 2 OR Lineage 3 AND Mutation 1 AND Mutation 2, Lineage 4 AND Mutation 2, Lineage 5 ....
for query_lineage in pangolin_lineage.split(","):
query = {
"size": 0,
"query": {
},
"aggs": {
"mutations": {
"nested": {
"path": "mutations"
},
"aggs": {
"mutations": {
"terms": {
"field": "mutations.mutation",
"size": 10000
},
"aggs": {
"genomes": {
"reverse_nested": {}
}
}
}
}
}
}
}
query_lineage_split = query_lineage.split(" AND ")
query_mutations = []
query_pangolin_lineage = query_lineage_split[0].split(" OR ") # First parameter always lineages separated by commas
if len(query_lineage_split) > 1:
query_mutations = query_lineage_split[1:] # First parameter is always lineage
query["query"] = create_nested_mutation_query(lineages = query_pangolin_lineage, mutations = query_mutations)
#print(query)
resp = yield self.asynchronous_fetch(query)
path_to_results = ["aggregations", "mutations", "mutations", "buckets"]
buckets = resp
for i in path_to_results:
buckets = buckets[i]
flattened_response = [{
"mutation": i["key"],
"mutation_count": i["genomes"]["doc_count"],
"lineage_count": get_total_hits(resp),
"lineage": query_lineage
} for i in buckets]
if len(flattened_response) > 0:
df_response = (
pd.DataFrame(flattened_response)
.assign(
gene = lambda x: x["mutation"].apply(lambda k: self.gene_mapping[k.split(":")[0]] if k.split(":")[0] in self.gene_mapping else k.split(":")[0]),
ref_aa = lambda x: x["mutation"].apply(lambda k: re.findall("[A-Za-z*]+", k.split(":")[1])[0] if "DEL" not in k and "del" not in k and "_" not in k else k).str.upper(),
alt_aa = lambda x: x["mutation"].apply(lambda k: re.findall("[A-Za-z*]+", k.split(":")[1])[1] if "DEL" not in k and "del" not in k and "_" not in k else k.split(":")[1]).str.upper(),
codon_num = lambda x: x["mutation"].apply(lambda k: int(re.findall("[0-9]+", k.split(":")[1])[0])),
codon_end = lambda x: x["mutation"].apply(lambda k: int(re.findall("[0-9]+", k.split(":")[1])[1]) if "/" in k and ("DEL" in k or "del" in k) else None),
type = lambda x: x["mutation"].apply(lambda k: "deletion" if "DEL" in k or "del" in k else "substitution")
)
)
df_response = df_response[df_response["ref_aa"] != df_response["alt_aa"]]
df_response.loc[:, "prevalence"] = df_response["mutation_count"]/df_response["lineage_count"]
df_response.loc[~df_response["codon_end"].isna(), "change_length_nt"] = ((df_response["codon_end"] - df_response["codon_num"]) + 1) * 3
df_response = df_response[df_response["prevalence"] >= frequency].fillna("None")
dict_response[query_lineage] = df_response.to_dict(orient="records")
resp = {"success": True, "results": dict_response}
return resp
class MutationDetailsHandler(BaseHandler):
@gen.coroutine
def _get(self):
mutations = self.get_argument("mutations", None)
mutations = mutations.split(",") if mutations is not None else []
query = {
"size": 0,
"aggs": {
"by_mutations": {
"nested": {
"path": "mutations"
},
"aggs": {
"inner": {
"filter": {
"bool": {
"should": [
{"match": {"mutations.mutation": i}}
for i in mutations
]
}
},
"aggs": {
"by_name": {
"terms": {"field": "mutations.mutation"},
"aggs": {
"by_nested": {
"top_hits": {"size": 1}
}
}
}
}
}
}
}
}
}
resp = yield self.asynchronous_fetch(query)
path_to_results = ["aggregations", "by_mutations", "inner", "by_name", "buckets"]
buckets = resp
for i in path_to_results:
buckets = buckets[i]
flattened_response = []
for i in buckets:
for j in i["by_nested"]["hits"]["hits"]:
tmp = j["_source"]
for k in ["change_length_nt", "codon_num", "pos"]:
if k in tmp and tmp[k] != "None":
tmp[k] = int(float(tmp[k]))
flattened_response.append(tmp)
resp = {"success": True, "results": flattened_response}
return resp
class MutationsByLineage(BaseHandler):
@gen.coroutine
def _get(self):
query_location = self.get_argument("location_id", None)
query_mutations = self.get_argument("mutations", None)
query_pangolin_lineage = self.get_argument("pangolin_lineage", None)
query_mutations = [muts.split(",") for muts in query_mutations.split(" AND ")] if query_mutations is not None else []
query_frequency_threshold = self.get_argument("frequency", None)
query_frequency_threshold = float(query_frequency_threshold) if query_frequency_threshold is not None else 0
results = {}
for muts in query_mutations: # For multiple sets of mutations, create multiple ES queries. Since AND queries are possible doing one ES query with aggregations is cumbersome. Must look for better solution here.
query = {
"size": 0,
"aggs": {
"lineage": {
"terms": {"field": "pangolin_lineage", "size": self.size},
"aggs": {
"mutations": {
"filter": {}
}
}
}
}
}
if query_location is not None:
query["query"] = parse_location_id_to_query(query_location)
if query_pangolin_lineage is not None:
if "query" in query: # Only query added will be bool for location
query["query"]["bool"]["must"].append({
"term": {
"pangolin_lineage": query_pangolin_lineage
}
})
else:
query["query"] = {
"term": {
"pangolin_lineage": query_pangolin_lineage
}
}
query["aggs"]["lineage"]["aggs"]["mutations"]["filter"] = create_nested_mutation_query(mutations = muts)
resp = yield self.asynchronous_fetch(query)
path_to_results = ["aggregations", "lineage", "buckets"]
buckets = resp
for i in path_to_results:
buckets = buckets[i]
flattened_response = []
for i in buckets:
if not i["mutations"]["doc_count"] > 0 or i["key"] == "none":
continue
flattened_response.append({
"pangolin_lineage": i["key"],
"lineage_count": i["doc_count"],
"mutation_count": i["mutations"]["doc_count"]
})
df_response = pd.DataFrame(flattened_response)
if df_response.shape[0] > 0:
prop = calculate_proportion(df_response["mutation_count"], df_response["lineage_count"])
df_response.loc[:, "proportion"] = prop[0]
df_response.loc[:, "proportion_ci_lower"] = prop[1]
df_response.loc[:, "proportion_ci_upper"] = prop[2]
df_response = df_response[df_response["proportion"] >= query_frequency_threshold]
results[",".join(muts)] = df_response.to_dict(orient="records")
resp = {"success": True, "results": results}
return resp