|
import numpy as np |
|
from transformers import AutoTokenizer, AutoModelForSequenceClassification |
|
from transformers import set_seed |
|
import torch |
|
import torch.nn as nn |
|
import warnings |
|
from tqdm import tqdm |
|
import gradio as gr |
|
|
|
warnings.filterwarnings('ignore') |
|
device = "cuda:0" |
|
model_checkpoint1 = "facebook/esm2_t12_35M_UR50D" |
|
tokenizer = AutoTokenizer.from_pretrained(model_checkpoint1) |
|
|
|
|
|
class MyModel(nn.Module): |
|
def __init__(self): |
|
super().__init__() |
|
self.bert1 = AutoModelForSequenceClassification.from_pretrained(model_checkpoint1, num_labels=3000).cuda() |
|
|
|
|
|
self.bn1 = nn.BatchNorm1d(256) |
|
self.bn2 = nn.BatchNorm1d(128) |
|
self.bn3 = nn.BatchNorm1d(64) |
|
self.relu = nn.LeakyReLU() |
|
self.fc1 = nn.Linear(3000, 256) |
|
self.fc2 = nn.Linear(256, 128) |
|
self.fc3 = nn.Linear(128, 64) |
|
self.output_layer = nn.Linear(64, 2) |
|
self.dropout = nn.Dropout(0.3) |
|
|
|
def forward(self, x): |
|
with torch.no_grad(): |
|
bert_output = self.bert1(input_ids=x['input_ids'].to(device), |
|
attention_mask=x['attention_mask'].to(device)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
output_feature = self.dropout(bert_output["logits"]) |
|
output_feature = self.dropout(self.relu(self.bn1(self.fc1(output_feature)))) |
|
output_feature = self.dropout(self.relu(self.bn2(self.fc2(output_feature)))) |
|
output_feature = self.dropout(self.relu(self.bn3(self.fc3(output_feature)))) |
|
output_feature = self.dropout(self.output_layer(output_feature)) |
|
|
|
return torch.softmax(output_feature, dim=1) |
|
|
|
|
|
def AMP(test_sequences, model): |
|
|
|
max_len = 18 |
|
test_data = tokenizer(test_sequences, max_length=max_len, padding="max_length", truncation=True, |
|
return_tensors='pt') |
|
model = model.to(device) |
|
model.eval() |
|
out_probability = [] |
|
with torch.no_grad(): |
|
predict = model(test_data).cuda() |
|
out_probability.extend(np.max(np.array(predict.cpu()), axis=1).tolist()) |
|
test_argmax = np.argmax(predict.cpu(), axis=1).tolist() |
|
id2str = {0: "non-AMP", 1: "AMP"} |
|
return id2str[test_argmax[0]], out_probability[0] |
|
|
|
|
|
def classify_sequence(sequence): |
|
|
|
valid_amino_acids = set("ACDEFGHIKLMNPQRSTVWY") |
|
sequence = sequence.upper() |
|
|
|
if all(aa in valid_amino_acids for aa in sequence) and len(sequence) >= 3: |
|
result, probability = AMP(sequence, model) |
|
return "yes" if result == "AMP" else "no" |
|
else: |
|
return "Invalid Sequence" |
|
|
|
|
|
model = MyModel() |
|
model.load_state_dict(torch.load("best_model.pth")) |
|
|
|
iface = gr.Interface( |
|
fn=classify_sequence, |
|
inputs=gr.inputs.Textbox(label="Enter Sequence"), |
|
outputs=gr.outputs.Textbox(label="AMP Classification (yes/no)"), |
|
live=True, |
|
title="AMP Sequence Detector", |
|
description="Enter a sequence to detect if it is an AMP (Antimicrobial Peptide) or not (yes/no)." |
|
) |
|
|
|
if __name__ == "__main__": |
|
iface.launch() |
|
|
|
|
|
|
|
|