-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch_plddt_plotter.py
More file actions
183 lines (167 loc) · 4.99 KB
/
batch_plddt_plotter.py
File metadata and controls
183 lines (167 loc) · 4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env python3
import numpy as np
import re
import pandas as pd
from pathlib import Path
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
import seaborn as sns
import matplotlib.ticker as ticker
from matplotlib.ticker import FormatStrFormatter
# load data
p = Path(".")
png_files = sorted(list(p.rglob("*_0.png")))
pae_files = sorted(list(p.rglob("*_pae.npy")))
score_files = sorted(list(p.rglob("*_0.npz")))
def make_plot(png, pae, score):
# load pae, plddt, and image data
img = plt.imread(png, "png")
pae = np.load(pae)
paedf = pd.DataFrame(pae[0])
data_max = np.max(pae[0])
# load ptm and iptm scores
scores = np.load(score)
# add protein name from filename to add to left of figure
filename = score.stem
filename = filename.replace("_scores_0", "")
filename = re.sub(r"_0$", "", filename)
# set figure
fig, ax = plt.subplots(1, 2, figsize=(12, 6))
# add png of plddt
ax[0].imshow(img)
ax[0].set_axis_off()
# set heatmap colorscheme
purp = sns.light_palette("#918edb", as_cmap=True, reverse=False)
# add pae heatmap
im = ax[1].imshow(
paedf, cmap=purp, extent=[0, len(paedf), len(paedf), 0], aspect="equal"
)
# add colorbar to pae heatmap
cbar = fig.colorbar(
im,
ax=ax[1],
label="Predicted Aligned Error (Å)",
fraction=0.045,
ticks=[0, data_max],
location="right",
)
# change heatmap and colorbar tick and axis settings
cbar.set_ticks([0, data_max])
cbar.ax.yaxis.set_major_formatter(FormatStrFormatter("%.0f"))
ax[1].xaxis.set_major_locator(ticker.LinearLocator(5))
ax[1].yaxis.set_major_locator(ticker.LinearLocator(5))
ax[1].yaxis.set_major_formatter(FormatStrFormatter("%.0f"))
ax[1].xaxis.set_major_formatter(FormatStrFormatter("%.0f"))
ax[1].set_xlabel("Scored residue")
ax[1].set_ylabel("Aligned residue")
ax[1].spines["top"].set_visible(False)
ax[1].spines["right"].set_visible(False)
# add text of protein name to left of figure
fig.text(
0.05,
0.5,
filename,
rotation=90,
verticalalignment="center",
horizontalalignment="center",
fontsize=12,
fontweight="normal",
# bbox=dict(boxstyle="round,pad=0.5", facecolor='lightgrey', edgecolor='lightgrey', alpha=0.8)
)
# make plddt legend with colored circles
legend_elements = [
Line2D(
[0],
[0],
marker="o",
color="w",
markerfacecolor="#1e66f5",
markersize=14,
label="[100, 90)",
),
Line2D(
[0],
[0],
marker="o",
color="w",
markerfacecolor="#04a5e5",
markersize=14,
label="[90, 70)",
),
Line2D(
[0],
[0],
marker="o",
color="w",
markerfacecolor="#f8e1ae",
markersize=14,
label="[70, 50)",
),
Line2D(
[0],
[0],
marker="o",
color="w",
markerfacecolor="#f9b286",
markersize=14,
label="[50, 0]",
),
]
# add plddt legend
ax[0].legend(
handles=legend_elements,
title="pLDDT",
alignment="center",
loc="upper center",
frameon=False,
fancybox=False,
shadow=False,
ncol=4,
bbox_to_anchor=(0.5, 1.15),
)
# format ptm and iptm numbers into a string if they're more than 0
ptm = scores["ptm"]
iptm = scores["iptm"]
ptm = ptm[0] if ptm[0] > 0 else "n/a"
iptm = iptm[0] if iptm[0] > 0 else "n/a"
ptm_formatted = f"{ptm:.2f}" if isinstance(ptm, (int, float, np.number)) else ptm
iptm_formatted = (
f"{iptm:.2f}" if isinstance(iptm, (int, float, np.number)) else iptm
)
# print ptm and iptm text
ptm_text = f"pTM = {ptm_formatted} ipTM = {iptm_formatted}"
# put ptm and iptm label on figure
fig.text(
0.71,
0.890,
ptm_text,
rotation=0,
verticalalignment="center",
horizontalalignment="center",
fontsize=12,
fontweight="normal",
)
# create output dir
outdir = Path("./output_plots")
outdir.mkdir(parents=True, exist_ok=True)
# save figures
# plt.savefig(
# f"{outdir}/{filename}_summary_plot.svg",
# format="svg",
# dpi=300,
# bbox_inches="tight",
# )
plt.savefig(
f"{outdir}/{filename}_summary_plot.png",
format="png",
dpi=300,
bbox_inches="tight",
)
# be sure to close plot each time to avoid consuming lots of memory
plt.close("all")
for png, pae, score in zip(png_files, pae_files, score_files):
print("=== Saving plot ===")
print(f"png file = {png.name} in dir: {png}")
print(f"pae file = {pae.name} in dir: {pae}")
print(f"npz file = {score.name} in dir: {score}")
make_plot(png, pae, score)