forked from facebookresearch/esm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
score_log_likelihoods.py
74 lines (65 loc) · 2.31 KB
/
score_log_likelihoods.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
# Copyright (c) Facebook, Inc. and its affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
#
# Scores sequences based on a given structure.
#
# usage:
# score_log_likelihoods.py [-h] [--outpath OUTPATH] [--chain CHAIN] pdbfile seqfile
import argparse
from biotite.sequence.io.fasta import FastaFile, get_sequences
import numpy as np
from pathlib import Path
import torch
import torch.nn.functional as F
from tqdm import tqdm
import esm
import esm.inverse_folding
def main():
parser = argparse.ArgumentParser(
description='Score sequences based on a given structure.'
)
parser.add_argument(
'pdbfile', type=str,
help='input filepath, either .pdb or .cif',
)
parser.add_argument(
'seqfile', type=str,
help='input filepath for variant sequences in a .fasta file',
)
parser.add_argument(
'--outpath', type=str,
help='output filepath for scores of variant sequences',
default='output/sequence_scores.csv',
)
parser.add_argument(
'--chain', type=str,
help='chain id for the chain of interest', default='A',
)
args = parser.parse_args()
model, alphabet = esm.pretrained.esm_if1_gvp4_t16_142M_UR50()
model = model.eval()
coords, seq = esm.inverse_folding.util.load_coords(args.pdbfile, args.chain)
print('Native sequence loaded from structure file:')
print(seq)
print('\n')
ll, _ = esm.inverse_folding.util.score_sequence(
model, alphabet, coords, seq)
print('Native sequence')
print(f'Log likelihood: {ll:.2f}')
print(f'Perplexity: {np.exp(-ll):.2f}')
print('\nScoring variant sequences from sequence file..\n')
infile = FastaFile()
infile.read(args.seqfile)
seqs = get_sequences(infile)
Path(args.outpath).parent.mkdir(parents=True, exist_ok=True)
with open(args.outpath, 'w') as fout:
fout.write('seqid,log_likelihood\n')
for header, seq in tqdm(seqs.items()):
ll, _ = esm.inverse_folding.util.score_sequence(
model, alphabet, coords, str(seq))
fout.write(header + ',' + str(ll) + '\n')
print(f'Results saved to {args.outpath}')
if __name__ == '__main__':
main()