Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#@title Lauch the Interface
|
2 |
+
#@markdown Run to launch the interface.
|
3 |
+
#@markdown 1. Enter the protein sequence.
|
4 |
+
#@markdown 2. Specify the start and end index (inclusive) of the domain for which you would like to predict mutations (note that indexing starts at 1).
|
5 |
+
#@markdown 3. Select the number of tokens you would like the model to predict for each position.
|
6 |
+
#@markdown 4. Click 'Submit'.
|
7 |
+
#@markdown 5. Click 'Download Outputs' to download the zip file.
|
8 |
+
|
9 |
+
def process_sequence(sequence, domain_bounds, n):
|
10 |
+
start_index = int(domain_bounds['start'][0]) - 1
|
11 |
+
end_index = int(domain_bounds['end'][0])
|
12 |
+
|
13 |
+
top_n_mutations = {}
|
14 |
+
all_logits = []
|
15 |
+
|
16 |
+
for i in range(len(sequence)):
|
17 |
+
if start_index <= i <= (end_index - 1):
|
18 |
+
masked_seq = sequence[:i] + '<mask>' + sequence[i+1:]
|
19 |
+
inputs = tokenizer(masked_seq, return_tensors="pt", padding=True, truncation=True, max_length=2000)
|
20 |
+
inputs = {k: v.to(device) for k, v in inputs.items()}
|
21 |
+
with torch.no_grad():
|
22 |
+
logits = model(**inputs).logits
|
23 |
+
mask_token_index = torch.where(inputs["input_ids"] == tokenizer.mask_token_id)[1]
|
24 |
+
mask_token_logits = logits[0, mask_token_index, :]
|
25 |
+
|
26 |
+
# Define amino acid tokens
|
27 |
+
AAs_tokens = ['L', 'A', 'G', 'V', 'S', 'E', 'R', 'T', 'I', 'D', 'P', 'K', 'Q', 'N', 'F', 'Y', 'M', 'H', 'W', 'C']
|
28 |
+
all_tokens_logits = mask_token_logits.squeeze(0)
|
29 |
+
top_tokens_indices = torch.argsort(all_tokens_logits, dim=0, descending=True)
|
30 |
+
top_tokens_logits = all_tokens_logits[top_tokens_indices]
|
31 |
+
mutation = []
|
32 |
+
# make sure we don't include non-AA tokens
|
33 |
+
for token_index in top_tokens_indices:
|
34 |
+
decoded_token = tokenizer.decode([token_index.item()])
|
35 |
+
if decoded_token in AAs_tokens:
|
36 |
+
mutation.append(decoded_token)
|
37 |
+
if len(mutation) == n:
|
38 |
+
break
|
39 |
+
top_n_mutations[(sequence[i], i)] = mutation
|
40 |
+
|
41 |
+
# collecting logits for the heatmap
|
42 |
+
logits_array = mask_token_logits.cpu().numpy()
|
43 |
+
# filter out non-amino acid tokens
|
44 |
+
filtered_indices = list(range(4, 23 + 1))
|
45 |
+
filtered_logits = logits_array[:, filtered_indices]
|
46 |
+
all_logits.append(filtered_logits)
|
47 |
+
|
48 |
+
|
49 |
+
token_indices = torch.arange(logits.size(-1))
|
50 |
+
tokens = [tokenizer.decode([idx]) for idx in token_indices]
|
51 |
+
filtered_tokens = [tokens[i] for i in filtered_indices]
|
52 |
+
|
53 |
+
all_logits_array = np.vstack(all_logits)
|
54 |
+
normalized_logits_array = F.softmax(torch.tensor(all_logits_array), dim=-1).numpy()
|
55 |
+
transposed_logits_array = normalized_logits_array.T
|
56 |
+
|
57 |
+
# Plotting the heatmap
|
58 |
+
x_tick_positions = np.arange(start_index, end_index, 10)
|
59 |
+
x_tick_labels = [str(pos + 1) for pos in x_tick_positions]
|
60 |
+
|
61 |
+
plt.figure(figsize=(15, 8))
|
62 |
+
plt.rcParams.update({'font.size': 18})
|
63 |
+
|
64 |
+
sns.heatmap(transposed_logits_array, cmap='plasma', xticklabels=x_tick_labels, yticklabels=filtered_tokens)
|
65 |
+
plt.title('Token Probability Heatmap')
|
66 |
+
plt.ylabel('Token')
|
67 |
+
plt.xlabel('Residue Index')
|
68 |
+
plt.yticks(rotation=0)
|
69 |
+
plt.xticks(x_tick_positions - start_index + 0.5, x_tick_labels, rotation=0)
|
70 |
+
|
71 |
+
# Save the figure to a BytesIO object
|
72 |
+
buf = BytesIO()
|
73 |
+
plt.savefig(buf, format='png', dpi = 300)
|
74 |
+
buf.seek(0)
|
75 |
+
plt.close()
|
76 |
+
|
77 |
+
# Convert BytesIO object to an image
|
78 |
+
img = Image.open(buf)
|
79 |
+
|
80 |
+
original_residues = []
|
81 |
+
mutations = []
|
82 |
+
positions = []
|
83 |
+
|
84 |
+
for key, value in top_n_mutations.items():
|
85 |
+
original_residue, position = key
|
86 |
+
original_residues.append(original_residue)
|
87 |
+
mutations.append(value)
|
88 |
+
positions.append(position + 1)
|
89 |
+
|
90 |
+
df = pd.DataFrame({
|
91 |
+
'Original Residue': original_residues,
|
92 |
+
'Predicted Residues': mutations,
|
93 |
+
'Position': positions
|
94 |
+
})
|
95 |
+
df.to_csv("predicted_tokens.csv", index=False)
|
96 |
+
img.save("heatmap.png", dpi = 300)
|
97 |
+
zip_path = "outputs.zip"
|
98 |
+
with zipfile.ZipFile(zip_path, 'w') as zipf:
|
99 |
+
zipf.write("predicted_tokens.csv")
|
100 |
+
zipf.write("heatmap.png")
|
101 |
+
|
102 |
+
|
103 |
+
return df, img, zip_path
|
104 |
+
|
105 |
+
demo = gr.Interface(
|
106 |
+
fn=process_sequence,
|
107 |
+
inputs=[
|
108 |
+
gr.Textbox(label="Sequence", placeholder="Enter the protein sequence here"),
|
109 |
+
gr.Dataframe(
|
110 |
+
headers=["start", "end"],
|
111 |
+
datatype=["number", "number"],
|
112 |
+
row_count=(1, "fixed"),
|
113 |
+
col_count=(2, "fixed"),
|
114 |
+
label="Domain Bounds"
|
115 |
+
),
|
116 |
+
gr.Dropdown([i for i in range(1, 21)], label="Top N Tokens"),
|
117 |
+
],
|
118 |
+
outputs=[
|
119 |
+
gr.Dataframe(label="Predicted Tokens (in order of decreasing likelihood)"),
|
120 |
+
gr.Image(type="pil", label="Heatmap"),
|
121 |
+
gr.File(label="Download Outputs"),
|
122 |
+
],
|
123 |
+
)
|
124 |
+
if __name__ == "__main__":
|
125 |
+
with suppress_output():
|
126 |
+
demo.launch(show_error=False, debug=False)
|