This repository has been archived by the owner on Aug 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 654
/
predict.py
241 lines (204 loc) · 8.29 KB
/
predict.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
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
import argparse
import pathlib
import string
import torch
from esm import Alphabet, FastaBatchedDataset, ProteinBertModel, pretrained, MSATransformer
import pandas as pd
from tqdm import tqdm
from Bio import SeqIO
import itertools
from typing import List, Tuple
import numpy as np
def remove_insertions(sequence: str) -> str:
""" Removes any insertions into the sequence. Needed to load aligned sequences in an MSA. """
# This is an efficient way to delete lowercase characters and insertion characters from a string
deletekeys = dict.fromkeys(string.ascii_lowercase)
deletekeys["."] = None
deletekeys["*"] = None
translation = str.maketrans(deletekeys)
return sequence.translate(translation)
def read_msa(filename: str, nseq: int) -> List[Tuple[str, str]]:
""" Reads the first nseq sequences from an MSA file, automatically removes insertions.
The input file must be in a3m format (although we use the SeqIO fasta parser)
for remove_insertions to work properly."""
msa = [
(record.description, remove_insertions(str(record.seq)))
for record in itertools.islice(SeqIO.parse(filename, "fasta"), nseq)
]
return msa
def create_parser():
parser = argparse.ArgumentParser(
description="Label a deep mutational scan with predictions from an ensemble of ESM-1v models." # noqa
)
# fmt: off
parser.add_argument(
"--model-location",
type=str,
help="PyTorch model file OR name of pretrained model to download (see README for models)",
nargs="+",
)
parser.add_argument(
"--sequence",
type=str,
help="Base sequence to which mutations were applied",
)
parser.add_argument(
"--dms-input",
type=pathlib.Path,
help="CSV file containing the deep mutational scan",
)
parser.add_argument(
"--mutation-col",
type=str,
default="mutant",
help="column in the deep mutational scan labeling the mutation as 'AiB'"
)
parser.add_argument(
"--dms-output",
type=pathlib.Path,
help="Output file containing the deep mutational scan along with predictions",
)
parser.add_argument(
"--offset-idx",
type=int,
default=0,
help="Offset of the mutation positions in `--mutation-col`"
)
parser.add_argument(
"--scoring-strategy",
type=str,
default="wt-marginals",
choices=["wt-marginals", "pseudo-ppl", "masked-marginals"],
help=""
)
parser.add_argument(
"--msa-path",
type=pathlib.Path,
help="path to MSA in a3m format (required for MSA Transformer)"
)
parser.add_argument(
"--msa-samples",
type=int,
default=400,
help="number of sequences to select from the start of the MSA"
)
# fmt: on
parser.add_argument("--nogpu", action="store_true", help="Do not use GPU even if available")
return parser
def label_row(row, sequence, token_probs, alphabet, offset_idx):
wt, idx, mt = row[0], int(row[1:-1]) - offset_idx, row[-1]
assert sequence[idx] == wt, "The listed wildtype does not match the provided sequence"
wt_encoded, mt_encoded = alphabet.get_idx(wt), alphabet.get_idx(mt)
# add 1 for BOS
score = token_probs[0, 1 + idx, mt_encoded] - token_probs[0, 1 + idx, wt_encoded]
return score.item()
def compute_pppl(row, sequence, model, alphabet, offset_idx):
wt, idx, mt = row[0], int(row[1:-1]) - offset_idx, row[-1]
assert sequence[idx] == wt, "The listed wildtype does not match the provided sequence"
# modify the sequence
sequence = sequence[:idx] + mt + sequence[(idx + 1) :]
# encode the sequence
data = [
("protein1", sequence),
]
batch_converter = alphabet.get_batch_converter()
batch_labels, batch_strs, batch_tokens = batch_converter(data)
wt_encoded, mt_encoded = alphabet.get_idx(wt), alphabet.get_idx(mt)
# compute probabilities at each position
log_probs = []
for i in range(1, len(sequence) - 1):
batch_tokens_masked = batch_tokens.clone()
batch_tokens_masked[0, i] = alphabet.mask_idx
with torch.no_grad():
token_probs = torch.log_softmax(model(batch_tokens_masked.cuda())["logits"], dim=-1)
log_probs.append(token_probs[0, i, alphabet.get_idx(sequence[i])].item()) # vocab size
return sum(log_probs)
def main(args):
# Load the deep mutational scan
df = pd.read_csv(args.dms_input)
# inference for each model
for model_location in args.model_location:
model, alphabet = pretrained.load_model_and_alphabet(model_location)
model.eval()
if torch.cuda.is_available() and not args.nogpu:
model = model.cuda()
print("Transferred model to GPU")
batch_converter = alphabet.get_batch_converter()
if isinstance(model, MSATransformer):
data = [read_msa(args.msa_path, args.msa_samples)]
assert (
args.scoring_strategy == "masked-marginals"
), "MSA Transformer only supports masked marginal strategy"
batch_labels, batch_strs, batch_tokens = batch_converter(data)
all_token_probs = []
for i in tqdm(range(batch_tokens.size(2))):
batch_tokens_masked = batch_tokens.clone()
batch_tokens_masked[0, 0, i] = alphabet.mask_idx # mask out first sequence
with torch.no_grad():
token_probs = torch.log_softmax(
model(batch_tokens_masked.cuda())["logits"], dim=-1
)
all_token_probs.append(token_probs[:, 0, i]) # vocab size
token_probs = torch.cat(all_token_probs, dim=0).unsqueeze(0)
df[model_location] = df.apply(
lambda row: label_row(
row[args.mutation_col], args.sequence, token_probs, alphabet, args.offset_idx
),
axis=1,
)
else:
data = [
("protein1", args.sequence),
]
batch_labels, batch_strs, batch_tokens = batch_converter(data)
if args.scoring_strategy == "wt-marginals":
with torch.no_grad():
token_probs = torch.log_softmax(model(batch_tokens.cuda())["logits"], dim=-1)
df[model_location] = df.apply(
lambda row: label_row(
row[args.mutation_col],
args.sequence,
token_probs,
alphabet,
args.offset_idx,
),
axis=1,
)
elif args.scoring_strategy == "masked-marginals":
all_token_probs = []
for i in tqdm(range(batch_tokens.size(1))):
batch_tokens_masked = batch_tokens.clone()
batch_tokens_masked[0, i] = alphabet.mask_idx
with torch.no_grad():
token_probs = torch.log_softmax(
model(batch_tokens_masked.cuda())["logits"], dim=-1
)
all_token_probs.append(token_probs[:, i]) # vocab size
token_probs = torch.cat(all_token_probs, dim=0).unsqueeze(0)
df[model_location] = df.apply(
lambda row: label_row(
row[args.mutation_col],
args.sequence,
token_probs,
alphabet,
args.offset_idx,
),
axis=1,
)
elif args.scoring_strategy == "pseudo-ppl":
tqdm.pandas()
df[model_location] = df.progress_apply(
lambda row: compute_pppl(
row[args.mutation_col], args.sequence, model, alphabet, args.offset_idx
),
axis=1,
)
df.to_csv(args.dms_output)
if __name__ == "__main__":
parser = create_parser()
args = parser.parse_args()
main(args)