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
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
# This module contains utility functions for zero-shot predictions using various protein language models

import argparse
from Bio import SeqIO
import numpy as np
import pandas as pd
import scipy.stats as ss
import torch
from tqdm import tqdm

from model.utils.other_utils import read_msa, greedy_select, msa_splicer, AAs

def zero_shot_esm_dms(wt_seq, 
                    model_locations = ['esm1v_t33_650M_UR90S_1',
                    'esm1v_t33_650M_UR90S_2',
                    'esm1v_t33_650M_UR90S_3',
                    'esm1v_t33_650M_UR90S_4',
                    'esm1v_t33_650M_UR90S_5',
                    'esm2_t36_3B_UR50D'],
                    scoring_strategy='wt-marginals',
                    num_msa_seqs=400,
                    **kwargs):
    """
    Perform deep mutational scanning using ESM model.
    
    Args:
        wt_seq (str): Wild-type protein sequence
        scoring_strategy (str): 'wt-marginals' or 'masked-marginals'
        **kwargs: Additional arguments
    
    Returns:
        pandas.DataFrame: DataFrame containing mutation scores and statistics
    """
    from esm import pretrained, MSATransformer
    
    # create list of all possible single point mutations in the wildtype sequence
    amino_acids = AAs[:-1]
    mutations = []
    for i, residue in enumerate(wt_seq):
        for aa in amino_acids:
            if wt_seq[i] == aa:
                continue
            mutations.append(wt_seq[i] + str(i + 1) + aa)

    # Compute token probs for each model.
    model_probs = []

    if torch.backends.mps.is_available():
        device = "mps"
    elif torch.cuda.is_available():
        device = "cuda:0"
    else:
        device = "cpu"

    for model_location in model_locations:
        model, alphabet = pretrained.load_model_and_alphabet(model_location)
        model.eval()
        model = model.to(device)

        batch_converter = alphabet.get_batch_converter()

        if isinstance(model, MSATransformer):

            assert kwargs['msa_file'] is not None, 'No MSA file provided.'
            msa = read_msa(kwargs['msa_file'])

            # Prep the MSA, making the appropriate mutations
            inputs = greedy_select(msa, num_seqs=num_msa_seqs) # can change this to pass more/fewer sequences
            #This splices the MSA to exclude gaps in the first sequence, due to MSATransformer context window 
            #size limit of 1024. If your MSA width is less than 1024, then you don't need to do this
            data = [msa_splicer(inputs)]

            # Run the model, retrieve logits
            _, __, 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.to(device))["logits"], dim=-1
                    )
                all_token_probs.append(token_probs[:, 0, i])  # vocab size
            token_probs = torch.cat(all_token_probs, dim=0).unsqueeze(0)

        else: 
            data = [
                ('protein1', wt_seq),
            ]
            batch_labels, batch_strs, batch_tokens = batch_converter(data)

            if scoring_strategy == 'wt-marginals':
                with torch.no_grad():
                    token_probs = torch.log_softmax(model(batch_tokens.to(device))['logits'], dim=-1)

            elif 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.to(device))['logits'], dim=-1
                        )
                    all_token_probs.append(token_probs[:, i])  # vocab size
                token_probs = torch.cat(all_token_probs, dim=0).unsqueeze(0)

            else:
                raise ValueError(f'Invalid scoring strategy {scoring_strategy}')
            
        model_probs.append(token_probs.cpu().numpy()[0])
    
    X = []
    for model_prob in model_probs:

        X_sub = []
        for mutation in mutations:
            wt, idx, mt = mutation[0], int(mutation[1:-1])-1, mutation[-1]
            assert wt_seq[idx] == wt, 'Wild-type residue does not match input sequence.'
            
            wt_encoded, mt_encoded = alphabet.tok_to_idx[wt], alphabet.tok_to_idx[mt]

            score = model_prob[idx + 1, mt_encoded] - model_prob[idx + 1, wt_encoded]
            if not np.isfinite(score):
                score = 0.
            
            X_sub.append(score)

        X.append(X_sub)
    
    # set up dataframe
    data = {'mutations': mutations}
    for i in range(len(X)):
        data[f'model_{i+1}_logratio'] = X[i]
    df = pd.DataFrame(data)

    # Calculate average log ratio across all models
    logratio_cols = [f'model_{i+1}_logratio' for i in range(len(X))]
    df['average_model_logratio'] = df[logratio_cols].mean(axis=1)

    # Calculate pass/fail for each model
    for i in range(len(X)):
        df[f'model_{i+1}_pass'] = df[f'model_{i+1}_logratio'].apply(lambda x: 1 if x > 0 else 0)

    # Sum up total passes
    pass_cols = [f'model_{i+1}_pass' for i in range(len(X))]
    df['total_model_pass'] = df[pass_cols].sum(axis=1)
    df.sort_values(by='average_model_logratio', ascending=False, inplace=True)
    df.sort_values(by='total_model_pass', ascending=False, inplace=True)

    df_ls = []

    # sort dataframe by total_model_pass and then by average_model_logratio

    total_model_pass_list = list(set(df['total_model_pass'].values))
    total_model_pass_list = total_model_pass_list[::-1]

    for model_pass_value in total_model_pass_list:
        subset = df[df['total_model_pass'] == model_pass_value].copy()
        subset.sort_values(by='average_model_logratio', ascending=False, inplace=True)
        df_ls.append(subset)

    df_sorted = pd.concat(df_ls)

    return df_sorted

def zero_shot_esm_if_dms(wt_seq, pdb_file, chain_id = 'A', scoring_strategy='wt-marginals', **kwargs):
    """
    Perform deep mutational scanning using ESM-IF (Inverse Folding) model.
    
    Args:
        wt_seq (str): Wild-type protein sequence
        pdb_file (str): Path to PDB file
        chain_id (str): Chain ID in the PDB file
        scoring_strategy (str): Currently not used, kept for consistency
        **kwargs: Additional arguments
    
    Returns:
        pandas.DataFrame: DataFrame containing mutation scores
    """
    import torch_geometric
    import torch_sparse
    from torch_geometric.nn import MessagePassing
    import esm
    from esm import pretrained
    from esm.inverse_folding.util import CoordBatchConverter

    amino_acids = AAs[:-1]
    mutations = []
    for i, residue in enumerate(wt_seq):
        for aa in amino_acids:
            if wt_seq[i] == aa:
                continue
            mutations.append(wt_seq[i] + str(i + 1) + aa)

    model_locations = ['esm_if1_gvp4_t16_142M_UR50']

    model, alphabet = pretrained.load_model_and_alphabet(model_locations[0])
    model = model.eval()

    structure = esm.inverse_folding.util.load_structure(pdb_file, chain_id)
    coords, native_seq = esm.inverse_folding.util.extract_coords_from_structure(structure)
    
    if native_seq == wt_seq:
        print(f"Native sequence from structure matches input sequence ({len(native_seq)} residues)")
    else:
        print(f"Warning: Native sequence from structure ({len(native_seq)} residues) does not match input sequence ({len(wt_seq)} residues)")

    device = next(model.parameters()).device
    batch_converter = CoordBatchConverter(alphabet)
    batch = [(coords, None, wt_seq)]
    coords, confidence, strs, tokens, padding_mask = batch_converter(
        batch, device=device)

    prev_output_tokens = tokens[:, :-1].to(device)
    target = tokens[:, 1:]
    logits, _ = model.forward(coords, padding_mask, confidence, prev_output_tokens)

    # Average model scores and find scores for the mutations-of-interest.

    scores = logits.detach().numpy()[0]
    mutation_score = {}
    for pos in range(len(wt_seq)):
        wt = wt_seq[pos]
        for mt in alphabet.all_toks:
            mutation = f'{wt}{pos + 1}{mt}'
            mutation_score[mutation] = scores[alphabet.tok_to_idx[mt], pos]

    X = []
    for mutation in mutations:
        wt = mutation[0]+mutation[1:-1]+mutation[0]
        score = mutation_score[mutation] - mutation_score[wt]
        if not np.isfinite(score):
            score = 0.
        
        X.append(score)

    df = pd.DataFrame({'mutations': mutations, 'logratio': X})

    return df

def zero_shot_esm(
        mutations,
        model_locations,
        sequence,
        scoring_strategy='wt-marginals',
        **kwargs
):
    """
    Perform zero-shot prediction using ESM (Evolutionary Scale Modeling) model.
    
    Args:
        mutations (list): List of mutation sets
        model_locations (list): List of ESM model file paths
        sequence (str): Original protein sequence
        scoring_strategy (str): 'wt-marginals' or 'masked-marginals'
        **kwargs: Additional arguments (e.g., device)
    
    Returns:
        numpy.ndarray: Array of mutation scores
    """
    from esm import pretrained

    # Compute token probs for each model.

    model_probs = []
    
    for model_location in model_locations:
        model, alphabet = pretrained.load_model_and_alphabet(model_location)
        model.eval()
        model = model.to(kwargs['device'])

        batch_converter = alphabet.get_batch_converter()

        data = [
            ('protein1', sequence),
        ]
        batch_labels, batch_strs, batch_tokens = batch_converter(data)

        if scoring_strategy == 'wt-marginals':
            with torch.no_grad():
                token_probs = torch.log_softmax(model(batch_tokens.to(kwargs['device']))['logits'], dim=-1)

        elif 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.to(kwargs['device']))['logits'], dim=-1
                    )
                all_token_probs.append(token_probs[:, i])  # vocab size
            token_probs = torch.cat(all_token_probs, dim=0).unsqueeze(0)

        else:
            raise ValueError(f'Invalid scoring strategy {scoring_strategy}')
        
        model_probs.append(token_probs.cpu().numpy()[0])

    
    # Sum model scores and find scores for the mutations-of-interest.
    scores = np.sum(model_probs, axis=0)
    mutation_score = {}
    for pos in range(len(sequence)):
        wt = sequence[pos]
        for mt in alphabet.all_toks:
            mutation = f'{wt}{pos + 1}{mt}'
            mutation_score[mutation] = scores[pos + 1, alphabet.tok_to_idx[mt]]

    X = []
    for mutation_set in mutations:
        score = np.mean([
            mutation_score[mutation] for mutation in mutation_set
        ])
        if not np.isfinite(score):
            score = 0.
        X.append(score)

    return np.array(X)

def zero_shot_msa(
        mutations,
        sequence,   
        **kwargs,
):
    """
    Perform zero-shot prediction using MSA Transformer model.
    
    Args:
        mutations (list): List of mutation sets
        sequence (str): Original protein sequence
        **kwargs: Additional arguments (must include 'msa_file')
    
    Returns:
        numpy.ndarray: Array of mutation scores
    """
    import esm
    import torch
    torch.set_grad_enabled(False)
    # Check to see if there is an MSA file in **kwargs.
    assert kwargs['msa_file'] is not None, 'No MSA file provided.'
    msa = read_msa(kwargs['msa_file'])

    # Instantiate the model
    msa_transformer, msa_transformer_alphabet = esm.pretrained.esm_msa1b_t12_100M_UR50S()
    msa_transformer = msa_transformer.eval()
    msa_transformer_batch_converter = msa_transformer_alphabet.get_batch_converter()

    # Prep the MSA, making the appropriate mutations
    inputs = greedy_select(msa, num_seqs=128) # can change this to pass more/fewer sequences
    #This splices the MSA to exclude gaps in the first sequence, due to MSATransformer context window 
    #size limit of 1024. If your MSA width is less than 1024, then you don't need to do this
    inputs = [msa_splicer(inputs)]

    # Run the model, retrieve logits
    _, __, msa_transformer_batch_tokens = msa_transformer_batch_converter(inputs)
    msa_transformer_batch_tokens = msa_transformer_batch_tokens.to(next(msa_transformer.parameters()).device)
    predictions = msa_transformer.forward(msa_transformer_batch_tokens, repr_layers=[12])
    logits = predictions['logits'][0][0]
    token_probs = torch.softmax(logits, dim=-1)
    print(token_probs.shape)

    print('pulling out mutations')
    # Pull specific logits out for the mutations-of-interest.
    mutation_score = {}
    for pos in range(len(sequence)):
        wt = sequence[pos]
        for mt in msa_transformer_alphabet.all_toks:
            mutation = f'{wt}{pos + 1}{mt}'
            mutation_score[mutation] = token_probs[pos + 1, msa_transformer_alphabet.tok_to_idx[mt]]

    X = []
    for mutation_set in mutations:
        score = np.mean([
            mutation_score[mutation] for mutation in mutation_set
        ])
        if not np.isfinite(score):
            score = 0.
        X.append(score)
    
    return np.array(X)

def zero_shot_esm_if(       
        mutations,
        model_locations,
        sequence,
        pdb_file, 
        chain_id,
        **kwargs
):
    """
    Perform zero-shot prediction using ESM-IF (Inverse Folding) model.
    
    Args:
        mutations (list): List of mutation sets
        model_locations (list): List of ESM-IF model file paths
        sequence (str): Original protein sequence
        pdb_file (str): Path to PDB file
        chain_id (str): Chain ID in the PDB file
        **kwargs: Additional arguments
    
    Returns:
        numpy.ndarray: Array of mutation scores
    """
    # Check that imports are correctly installed
    import torch_geometric
    import torch_sparse
    from torch_geometric.nn import MessagePassing
    import esm
    from esm import pretrained
    from esm.inverse_folding.util import CoordBatchConverter

    # If one of the above fails, run the following in your conda environment
    # import torch

    # def format_pytorch_version(version):
    #   return version.split('+')[0]

    # TORCH_version = torch.__version__
    # TORCH = format_pytorch_version(TORCH_version)

    # def format_cuda_version(version):
    #   return 'cu' + version.replace('.', '')

    # CUDA_version = torch.version.cuda
    # CUDA = format_cuda_version(CUDA_version)

    # !pip install -q torch-scatter -f https://data.pyg.org/whl/torch-{TORCH}+{CUDA}.html
    # !pip install -q torch-sparse -f https://data.pyg.org/whl/torch-{TORCH}+{CUDA}.html
    # !pip install -q torch-cluster -f https://data.pyg.org/whl/torch-{TORCH}+{CUDA}.html
    # !pip install -q torch-spline-conv -f https://data.pyg.org/whl/torch-{TORCH}+{CUDA}.html
    # !pip install -q torch-geometric

    # # Install esm
    # !pip install -q git+https://github.com/facebookresearch/esm.git

    # # Install biotite
    # !pip install -q biotite

    # Compute token probs for each model.

    model, alphabet = pretrained.load_model_and_alphabet(model_locations[0])
    model = model.eval()

    structure = esm.inverse_folding.util.load_structure(pdb_file, chain_id)
    coords, native_seq = esm.inverse_folding.util.extract_coords_from_structure(structure)

    device = next(model.parameters()).device
    batch_converter = CoordBatchConverter(alphabet)
    batch = [(coords, None, sequence)]
    coords, confidence, strs, tokens, padding_mask = batch_converter(
        batch, device=device)

    prev_output_tokens = tokens[:, :-1].to(device)
    target = tokens[:, 1:]
    logits, _ = model.forward(coords, padding_mask, confidence, prev_output_tokens)

    # Average model scores and find scores for the mutations-of-interest.

    scores = logits.detach().numpy()[0]
    mutation_score = {}
    for pos in range(len(sequence)):
        wt = sequence[pos]
        for mt in alphabet.all_toks:
            mutation = f'{wt}{pos + 1}{mt}'
            mutation_score[mutation] = scores[alphabet.tok_to_idx[mt], pos] # logits are vocab x length (no padding)

    X = []
    for mutation_set in mutations:
        score = np.mean([
            mutation_score[mutation] for mutation in mutation_set
        ])
        if not np.isfinite(score):
            score = 0.
        X.append(score)

    return np.array(X)