Views
No views yet
ProtAlBert-PfamProtAlBert-Pfam is a ProtAlBert language model fine-tuned to predict Pfam family from the sequence.ProtAlBert by using the following code snippet:1from transformers import AutoModel, AlbertTokenizer, AutoConfig
2import re
3
4def convert_sequence_to_input(sequence: str, model_name = "Rostlab/prot_albert"):
5 seq = " ".join([aa for aa in sequence])
6 seq = re.sub(r"[UZOB]", "X", seq)
7 tokenizer = AlbertTokenizer.from_pretrained(model_name, trust_remote_code=True, do_lower_case=False)
8 params = dict(return_tensors="pt", padding="max_length",
9 max_length=128,
10 truncation=True,)
11 x = tokenizer(seq, **params)
12 return x
13
14def convert_pfam_idx_to_class(pfam_idx: int) -> str:
15 """
16 Convert the prediction of the model to the corresponding class.
17 :param pfam_idx: index of the pfam class
18 :return: the Pfam family
19 """
20 conversion = {"0": "Methyltransf_25", "1": "LRR_1", "2": "Acetyltransf_7", "3": "His_kinase",
21 "4": "Bac_transf", "5": "Lum_binding", "6": "DNA_binding_1", "7": "Chromate_transp",
22 "8": "Lipase_GDSL_2", "9": "DnaJ_CXXCXGXG"}
23 return conversion.get(str(pfam_idx), "Unknown")
24
25
26model_name = "sayby/prot_albert_pfam"
27model = AutoModel.from_pretrained(model_name, trust_remote_code=True)
28
29
30
31sequence = "ILDVGTGTGKLESLAEFKRDFIGLDVTKEMMALNRNKGKLLLASATQMPIKDGTFDAIVSSFVLRNLPSTKGYFSEGFRTLKEGG"
32x = convert_sequence_to_input(sequence)
33output = model(x)
34pfam_idx = output["logits"].argmax(dim=-1).item()
35pfam = convert_pfam_idx_to_class(pfam_idx)
36print(f"The Pfam family is: {pfam}")