forked from OATML-Markslab/EVEscape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evescape_scores.py
executable file
·193 lines (154 loc) · 6.55 KB
/
evescape_scores.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
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.metrics import mean_squared_error, roc_auc_score, precision_recall_curve, auc, roc_curve
from sklearn.impute import SimpleImputer
##############################################
#Chosen parameters
##############################################
temperatures = {"fitness": 1, "surfacc": 1, "exchangability": 2}
flu_thresh = 0.054
hiv_thresh = 0.138
rbd_thresh = 0.57
rbd_xie_thresh = 0.90
##############################################
#Read processed experiments and components scores
##############################################
flu = pd.read_csv("../results/summaries/h1_experiments_and_scores.csv")
flu_ablist = [col for col in flu.columns.values if "mutfracsurvive" in col]
flu_all_ab = [col for col in flu.columns.values if "mutfracsurvive" in col]
hiv = pd.read_csv("../results/summaries/bg505_experiments_and_scores.csv")
hiv_ablist = [
col for col in hiv.columns.values
if "mutfracsurvive" in col and "VRC34" not in col
]
hiv_all_ab = [col for col in hiv.columns.values if "mutfracsurvive" in col]
rbd = pd.read_csv("../results/summaries/rbd_experiments_and_scores.csv")
rbd_ablist = [
col for col in rbd.columns.values if "escape_" in col and "_Bloom" in col
]
rbd_xie_ablist = [
col for col in rbd.columns.values if "escape_" in col and "_Xie" in col
]
spike = pd.read_csv("../results/summaries/spike_scores.csv")
rbd_all_ab = [col for col in rbd.columns.values if "escape_" in col]
##############################################
#Functions to calculate EVEscape and aggregate/binarize experiments
##############################################
def logistic(x):
return 1 / (1 + np.exp(-x))
def standardization(x):
"""Assumes input is numpy array or pandas series"""
return (x - x.mean()) / x.std()
def make_predictors(summary_init, thresh, ablist, scores=True):
summary = summary_init.copy()
#Drop extraneous WCN columns
summary = summary.drop(
columns=[col for col in summary.columns if "wcn_fill_" in col])
summary = summary.drop(
columns=[col for col in summary.columns if "wcn_sc" in col])
summary = summary.drop(
columns=[col for col in summary.columns if "diff" in col])
#Reverse WCN direction so that larger values are more accessible
summary["wcn_fill_r"] = -summary.wcn_fill
summary = summary.drop(columns="wcn_fill")
if scores:
#Calculate max escape for each mutant
summary["max_escape_experiment"] = summary[ablist].max(axis=1)
#Calculate if escape>threshold for each mutant
summary[
"is_escape_experiment"] = summary["max_escape_experiment"] > thresh
#Impute missing values for columns used to calculate EVEscape scores
impute_cols = ["i", "evol_indices", "wcn_fill_r", "charge_ew-hydro"]
df_imp = summary[impute_cols].copy()
imp = SimpleImputer(missing_values=np.nan, strategy="mean")
df_imp = pd.DataFrame(imp.fit_transform(df_imp),
columns=df_imp.columns,
index=df_imp.index)
df_imp = pd.concat([df_imp, summary[["wt", "mut"]]], axis=1)
#Compute EVEscape scores
summary["evescape"] = 0
summary["evescape"] += np.log(
logistic(
standardization(df_imp["evol_indices"]) * 1 /
temperatures["fitness"]))
summary["evescape"] += np.log(
logistic(
standardization(df_imp["wcn_fill_r"]) * 1 /
temperatures["surfacc"]))
summary["evescape"] += np.log(
logistic(
standardization(df_imp["charge_ew-hydro"]) * 1 /
temperatures["exchangability"]))
summary = summary.drop(
columns=[col for col in summary.columns if col == "wcn_fill"])
summary = summary.rename(
columns={
"evol_indices": "fitness_eve",
"wcn_fill_r": "accessibility_wcn",
"charge_ew-hydro": "dissimilarity_charge_hydro"
})
return (summary)
##############################################
#Make Calculations
##############################################
flu = make_predictors(flu, flu_thresh, flu_ablist)
hiv = make_predictors(hiv, hiv_thresh, hiv_ablist)
rbd_bloom = make_predictors(rbd, rbd_thresh, rbd_ablist)
rbd_xie = make_predictors(rbd, rbd_xie_thresh, rbd_xie_ablist)
spike = make_predictors(spike, None, None, scores=False)
rbd_all = rbd_bloom.rename(
columns={
"max_escape_experiment": "max_escape_experiment_bloom",
"is_escape_experiment": "is_escape_experiment_bloom"
}).merge(
rbd_xie.rename(
columns={
"max_escape_experiment": "max_escape_experiment_xie",
"is_escape_experiment": "is_escape_experiment_xie"
}))
rbd_all = rbd_all.rename(columns={
"rbd_ace2_binding": "bloom_ace2_binding",
"rbd_expression": "bloom_expression"
})
rbd_all["is_escape_experiment_all"] = (
rbd_all["is_escape_experiment_bloom"]) | (
rbd_all["is_escape_experiment_xie"])
flu = flu.drop(columns=flu_all_ab)
hiv = hiv.drop(columns=hiv_all_ab)
rbd_all = rbd_all.drop(columns=rbd_all_ab)
rbd_all = rbd_all.drop(columns="Naive Freq")
evescape = rbd_all.pop("evescape")
rbd_all.insert(10, "evescape", evescape)
##############################################
#Save Summaries with EVEscape/binarized experimetnal escape
##############################################
flu.to_csv("../results/summaries_with_scores/flu_h1_evescape.csv", index=False)
hiv.to_csv("../results/summaries_with_scores/hiv_env_evescape.csv",
index=False)
rbd_all.to_csv("../results/summaries_with_scores/spike_rbd_evescape.csv",
index=False)
spike.to_csv("../results/summaries_with_scores/full_spike_evescape.csv",
index=False)
##############################################
#Save Site-Level Summaries with EVEscape/binarized experimetnal escape
##############################################
def make_site(summary_init):
summary = summary_init.copy()
summary = summary.groupby(['wt', 'i']).agg('mean').reset_index()
return (summary)
flu_site = make_site(flu)
hiv_site = make_site(hiv)
rbd_all_site = make_site(rbd_all)
spike_site = make_site(spike)
flu_site.to_csv('../results/summaries_with_scores/flu_h1_evescape_sites.csv',
index=False)
hiv_site.to_csv('../results/summaries_with_scores/hiv_env_evescape_sites.csv',
index=False)
rbd_all_site.to_csv(
'../results/summaries_with_scores/spike_rbd_evescape_sites.csv',
index=False)
spike_site.to_csv(
'../results/summaries_with_scores/full_spike_evescape_sites.csv',
index=False)