Update README.md
Browse files
README.md
CHANGED
|
@@ -1,5 +1,118 @@
|
|
| 1 |
-
---
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license_name: unknown
|
| 3 |
+
license_link: LICENSE
|
| 4 |
+
---
|
| 5 |
+
import torch
|
| 6 |
+
import pandas as pd
|
| 7 |
+
import seaborn as sns
|
| 8 |
+
import matplotlib.pylab as plt
|
| 9 |
+
from mlm import *
|
| 10 |
+
import matplotlib.pyplot as plt
|
| 11 |
+
from predict import predict
|
| 12 |
+
|
| 13 |
+
import esm
|
| 14 |
+
|
| 15 |
+
_, alphabet = esm.pretrained.esm1b_t33_650M_UR50S()
|
| 16 |
+
|
| 17 |
+
raw_sequence = "SNPYQRGPNPTRSALTTDGPFSVATYSVSRLSVSGFGGGVIYYPTGTTLTFGGIAMSPGYTADASSLAWLGRRLASHGFVVIVINTNSRLDFPDSRASQLSAALNYLRTSSPSAVRARLDANRLAVAGHSMGGGATLRISEQIPTLKAGVPLTPWHTDKTFNTPVPQLIVGAEADTVAPVSQHAIPFYQNLPSTTPKVYVELDNATHFAPNSPNAAISVYTISWMKLWVDNDTRYRQFLCNVNDPALSDFRSNNRHCQ"
|
| 18 |
+
|
| 19 |
+
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
|
| 20 |
+
|
| 21 |
+
model = torch.load('./trans_best.pt', map_location=device)
|
| 22 |
+
|
| 23 |
+
model.eval()
|
| 24 |
+
|
| 25 |
+
def read_vocab(vocab_file):
|
| 26 |
+
vocab_dict = {}
|
| 27 |
+
with open(vocab_file, 'r') as vocab_file:
|
| 28 |
+
for i, line in enumerate(vocab_file):
|
| 29 |
+
vocab = line.replace("\n", "")
|
| 30 |
+
vocab_dict[vocab] = i
|
| 31 |
+
return vocab_dict
|
| 32 |
+
|
| 33 |
+
vocab = read_vocab('vocab.txt')
|
| 34 |
+
|
| 35 |
+
def tokenizer(seq):
|
| 36 |
+
x = torch.zeros(len(seq)+2)
|
| 37 |
+
x[0] = 2
|
| 38 |
+
for i, aa in enumerate(seq):
|
| 39 |
+
w = vocab.get(aa)
|
| 40 |
+
if w:
|
| 41 |
+
x[i+1] = w
|
| 42 |
+
else:
|
| 43 |
+
x[i+1] = 1
|
| 44 |
+
x[-1] = 3
|
| 45 |
+
return x.long()
|
| 46 |
+
|
| 47 |
+
def check_heatmap():
|
| 48 |
+
inp = tokenizer(raw_sequence).view(1, -1)
|
| 49 |
+
map = []
|
| 50 |
+
for i, w in enumerate(raw_sequence):
|
| 51 |
+
wild = vocab[w]
|
| 52 |
+
inp[0][i] = 4
|
| 53 |
+
with torch.no_grad():
|
| 54 |
+
y = model(inp.to(device))
|
| 55 |
+
map.append(list(y[0].cpu()[i].numpy() - y[0].cpu()[i].numpy()[wild])[5:])
|
| 56 |
+
inp = tokenizer(raw_sequence).view(1, -1)
|
| 57 |
+
return map
|
| 58 |
+
|
| 59 |
+
map = check_heatmap()
|
| 60 |
+
|
| 61 |
+
plot_x = []
|
| 62 |
+
plot_y = []
|
| 63 |
+
|
| 64 |
+
for x, y in zip([i for i in range(len(map))], np.array(map).mean(axis=-1)):
|
| 65 |
+
if x+36 < 280: # we remove the unstructured C-terminal
|
| 66 |
+
plot_x.append(x+36)
|
| 67 |
+
plot_y.append(y)
|
| 68 |
+
|
| 69 |
+
df = pd.DataFrame({'pos':plot_x, 'score':plot_y})
|
| 70 |
+
top10 = df.sort_values('score', ascending=False).head(10)
|
| 71 |
+
|
| 72 |
+
# sns.set_style('white')
|
| 73 |
+
# fig, ax = plt.subplots(figsize=(2,3.4))
|
| 74 |
+
# sns.lineplot(x=(np.array(plot_x)-36)/(len(plot_x)), y=plot_y, color='grey')
|
| 75 |
+
# ax.set_xlim([0,1])
|
| 76 |
+
# ax.set_ylim([-2.5,4.5])
|
| 77 |
+
# ax.set_yticks(np.arange(8)-2.5)
|
| 78 |
+
# ax.set_xticks([0,235/max(plot_x)])
|
| 79 |
+
# ax.set_xticklabels([35, 235])
|
| 80 |
+
# ax.scatter(x=(top10['pos']-36)/len(plot_x), y=top10['score'], color='brown', zorder=10)
|
| 81 |
+
# ax.text(x=0.2,y=-2.2,s='BhrPETase', fontsize=15)
|
| 82 |
+
|
| 83 |
+
with open("mutations-BhrPETase-new.fasta", 'a') as ofile:
|
| 84 |
+
x = (top10['pos']-36).to_numpy()
|
| 85 |
+
inds = []
|
| 86 |
+
scores = []
|
| 87 |
+
for i in x:
|
| 88 |
+
masked_seq = raw_sequence[:i] + "<mask>" + raw_sequence[i+1:]
|
| 89 |
+
predicted_sequence, predicted_scores, mask_idx = predict(masked_seq)
|
| 90 |
+
predicted_sequence = predicted_sequence[5:-5:]
|
| 91 |
+
res = predicted_sequence[i]
|
| 92 |
+
inds.append(mask_idx)
|
| 93 |
+
scores.append(predicted_scores[4:-4:])
|
| 94 |
+
ofile.write(f"{raw_sequence[i]}{i+1}{res}\n")
|
| 95 |
+
ofile.write(f"{raw_sequence[:i] + res + raw_sequence[i+1:]}\n")
|
| 96 |
+
ofile.write(f"Old: {raw_sequence[i]}\n")
|
| 97 |
+
ofile.write(f"New: {predicted_sequence[i]}\n\n")
|
| 98 |
+
|
| 99 |
+
amino_acids = []
|
| 100 |
+
for i in range(len(scores[0])):
|
| 101 |
+
amino_acids.append(alphabet.get_tok(4+i))
|
| 102 |
+
print(np.array(scores[0]).argmax(axis=0))
|
| 103 |
+
# ploting the heatmap of the scores
|
| 104 |
+
fig, ax = plt.subplots(figsize=(8,6))
|
| 105 |
+
sns.heatmap(np.array(scores), cmap='coolwarm', ax=ax, square=True, linewidth=0.5, cbar_kws={"shrink": 0.5})
|
| 106 |
+
# Increase cell size
|
| 107 |
+
ax.set_aspect(1.5)
|
| 108 |
+
# Rename the x_axis labels using the indices of the mutations
|
| 109 |
+
ax.set_yticklabels([f"{i+2} ({raw_sequence[i]})" for i in inds])
|
| 110 |
+
ax.set_xticklabels(amino_acids, rotation=0, ha="right", rotation_mode="anchor")
|
| 111 |
+
ax.set_title("Predicted Scores of Mutations on Top 10 Positions on BhrPETase")
|
| 112 |
+
ax.set_xlabel("Amino Acid Mutations")
|
| 113 |
+
ax.set_ylabel("Position on BhrPETase")
|
| 114 |
+
# Control the size of the color bar
|
| 115 |
+
plt.setp(ax.get_yticklabels(), rotation=0, ha="right",
|
| 116 |
+
rotation_mode="anchor")
|
| 117 |
+
plt.savefig("heatmap-BhrPETase-new.png", dpi=300)
|
| 118 |
+
print("DONE!")
|