Views
No views yet
from transformers import EsmTokenizer, EsmForMaskedLM
model_path = "/your/path/to/SaProt_650M_AF2"
tokenizer = EsmTokenizer.from_pretrained(model_path)
model = EsmForMaskedLM.from_pretrained(model_path)
#################### Example ####################
device = "cuda"
model.to(device)
seq = "M#EvVpQpL#VyQdYaKv" # Here "#" represents lower plDDT regions (plddt < 70)
tokens = tokenizer.tokenize(seq)
print(tokens)
inputs = tokenizer(seq, return_tensors="pt")
inputs = {k: v.to(device) for k, v in inputs.items()}
outputs = model(**inputs)
print(outputs.logits.shape)
"""
['M#', 'Ev', 'Vp', 'Qp', 'L#', 'Vy', 'Qd', 'Ya', 'Kv']
torch.Size([1, 11, 446])
"""SaProt_650M_AF2.pt. We provide a function to load the model.from utils.esm_loader import load_esm_saprot
model_path = "/your/path/to/SaProt_650M_AF2.pt"
model, alphabet = load_esm_saprot(model_path)1from model.saprot.saprot_foldseek_mutation_model import SaprotFoldseekMutationModel
2
3
4config = {
5 "foldseek_path": None,
6 "config_path": "/your/path/to/SaProt_650M_AF2", # Note this is the directory path of SaProt, not the ".pt" file
7 "load_pretrained": True,
8}
9model = SaprotFoldseekMutationModel(**config)
10tokenizer = model.tokenizer
11
12device = "cuda"
13model.eval()
14model.to(device)
15
16seq = "M#EvVpQpL#VyQdYaKv" # Here "#" represents lower plDDT regions (plddt < 70)
17
18# Predict the effect of mutating the 3rd amino acid to A
19mut_info = "V3A"
20mut_value = model.predict_mut(seq, mut_info)
21print(mut_value)
22
23# Predict mutational effect of combinatorial mutations, e.g. mutating the 3rd amino acid to A and the 4th amino acid to M
24mut_info = "V3A:Q4M"
25mut_value = model.predict_mut(seq, mut_info)
26print(mut_value)
27
28# Predict all effects of mutations at 3rd position
29mut_pos = 3
30mut_dict = model.predict_pos_mut(seq, mut_pos)
31print(mut_dict)
32
33# Predict probabilities of all amino acids at 3rd position
34mut_pos = 3
35mut_dict = model.predict_pos_prob(seq, mut_pos)
36print(mut_dict)1from model.saprot.base import SaprotBaseModel
2from transformers import EsmTokenizer
3
4
5config = {
6 "task": "base",
7 "config_path": "/your/path/to/SaProt_650M_AF2", # Note this is the directory path of SaProt, not the ".pt" file
8 "load_pretrained": True,
9}
10
11model = SaprotBaseModel(**config)
12tokenizer = EsmTokenizer.from_pretrained(config["config_path"])
13
14device = "cuda"
15model.to(device)
16
17seq = "M#EvVpQpL#VyQdYaKv" # Here "#" represents lower plDDT regions (plddt < 70)
18tokens = tokenizer.tokenize(seq)
19print(tokens)
20
21inputs = tokenizer(seq, return_tensors="pt")
22inputs = {k: v.to(device) for k, v in inputs.items()}
23
24embeddings = model.get_hidden_states(inputs, reduction="mean")
25print(embeddings[0].shape)