-
Notifications
You must be signed in to change notification settings - Fork 0
/
DSSP.py
277 lines (248 loc) · 10.4 KB
/
DSSP.py
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import sys
import numpy as np
import os
import argparse
import matplotlib.pyplot as plt
import matplotlib.patheffects as pe
import matplotlib.patches as mpatches
import mdtraj as md
import config.settings as c
from tools import utils, traj_funcs
def main(argv):
try:
parser = argparse.ArgumentParser()
parser.add_argument("-p", "--path",
action = "store",
dest = "path",
required = True,
help = "Set path to the data directory.")
parser.add_argument("-s", "--subset",
action = "store_true",
dest = "subset",
default = False,
help = ("Make a DSSP plot for just a subset "
"of residues."))
parser.add_argument("-u", "--umbrella",
action = "store_true",
dest = "umbrella",
default = False,
help = ("Make plots from umbrella sampling "
"data, with a separate DSSP plot for "
"each window."))
parser.add_argument("-f", "--figpath",
action = "store",
dest = "fig_path",
default = False,
help = ("Set a path destination for the "
"figure."))
parser.add_argument("-t", "--topol",
action = "store",
dest = "topol",
default = "protein.gro",
help = ("File name for topology, inside the "
"path directory."))
parser.add_argument("-b", "--bridge",
action = "store_true",
dest = "salt_bridge",
default = False,
help = ("Set a path destination for the "
"figure."))
args = parser.parse_args()
except argparse.ArgumentError:
print("Command line arguments are ill-defined, please check the "
"arguments")
raise
# Assign group selection from argparse
subset = args.subset
umbrella = args.umbrella
salt_bridge = args.salt_bridge
topol = args.topol
path = f"{ c.data_head }/{ args.path }"
fig_path = f"{ c.figure_head }/{ args.fig_path }"
# convert path to a list
if umbrella:
# Get a list of paths to all the window directories
ds = os.listdir(path)
windows = [path + "/" for d in ds if d.startswith("window")]
else:
data_paths = [path]
# iterate over all paths
for w, p in enumerate(data_paths):
xtc = "fitted_traj_100.xtc"
if umbrella:
# Concatenate the runs for a single umbrella to one
# trajectory
traj_files = []
trajs = []
prev_time = 0
for run in np.arange(1,5):
traj_files.append(f"{ p }/run{ run }/{ xtc }")
for f in traj_files:
traj = md.load(f, top=f"{ path }/{ topol }")
traj.time += prev_time
prev_time = traj.time[-1]
trajs.append(traj)
t = md.join(trajs)
else:
# Otherwise, just one set of traj data to load in
print("Loading in trajectory...")
t = md.load(f"{ p }/{ xtc }", #stride=100,
top=f"{ path }/{ topol }")
print(t)
# Use a subset of the residues
if salt_bridge:
t = t.atom_slice(t.top.select(("resid 49 to 99 or "
"resid 174 to 224")))
elif subset:
t = t.atom_slice(t.top.select('resid 179 to 253'))
else:
# All residues, excluding the ligand
t = t.atom_slice(t.top.select('resid 1 to 254'))
print(t.time)
# Compute DSSP for the residue selection
dssp = md.compute_dssp(t, simplified=False)
plot_dssp(dssp, t, subset, salt_bridge, fig_path, umbrella, w+1)
return None
def plot_dssp(dssp, t, subset, salt_bridge, path, umbrella, w):
"""Makes an image plot of the secondary structure from DSSP data.
Parameters
----------
dssp : np.ndarray
A 2D array of the character codes of the secondary structure of
each residue at each time frame; shape=(n_frames, n_residues).
time : np.ndarray
The simulation time corresponding to each frame, in picoseconds.
subset : boolean
Use a subset of the residues related to the beta and alpha flaps
for constructing the DSSP plots?
salt_bridge: boolean
Use a subset of the residues related to the salt bridge K57--E200
for constructing the DSSP plots?
path : str
Path to the primary working directory.
umbrella : boolean
DSSP plot is based on four 25 ns runs in an umbrella window?
window : int
Number of the umbrella window.
Returns
-------
None.
"""
codes = {"H" : 0, "B" : 1, "E" : 2, "G" : 3, "I" : 4, "T" : 5,
"S" : 6, " " : 7, "C" : 8}
codename = {0 : "alpha-helix", 1 : "beta-bridge", 2: "beta-strand",
3 : r"$3_{10}$-helix", 4 : "pi-helix", 5 : "turn",
6 : "bend", 7 : "loops", 8 : "coil"}
dssp = dssp.T
print(dssp.shape)
dssp_colors = np.vectorize(codes.get)(dssp)
if salt_bridge:
fig, axs = plt.subplots(2,1, sharex=True,
constrained_layout=True, figsize=(12,6))
ax1, ax2 = axs
im1 = ax1.imshow(dssp_colors[:50,:], aspect='auto')
im2 = ax2.imshow(dssp_colors[50:,:], aspect='auto')
values = np.unique(dssp_colors)
time = t.time
resids = [r.resSeq for r in t.topology.residues]
# Get the colors of the values, according to the colormap used
# by imshow
colors = [im1.cmap(im1.norm(value)) for value in values]
ax = ax2
else:
fig, ax = plt.subplots(1, 1, constrained_layout=True,
figsize=(12,4))
im = ax.imshow(dssp_colors, aspect='auto')
values = np.unique(dssp_colors)
time = t.time
resids = [r.resSeq for r in t.topology.residues]
# Get the colors of the values, according to the colormap used
# by imshow
colors = [im.cmap(im.norm(value)) for value in values]
# Create a patch (proxy artist) for every color
patches = [mpatches.Patch(color=colors[i], label=codename[i]) \
for i in range(len(values))]
if subset:
left, right = ax.get_xlim()
ax.hlines([195-180,218-180], left - 100, right + 100,
linestyles="dashed", lw=3, colors="#D90000",
path_effects=[pe.Stroke(linewidth=5, foreground='#E8E9E8'),
pe.Normal()])
ax.hlines([219.5-180,231-180], left - 100, right + 100,
linestyles="dashed", lw=3, colors="#F93BFF",
path_effects=[pe.Stroke(linewidth=5, foreground='#E8E9E8'),
pe.Normal()])
ax.set_yticks(np.arange(1, 75, 20))
ax.set_yticklabels(np.arange(180, 255, 20))
ax.set_ylabel("Residue Number", labelpad=5, fontsize=24)
ax.grid(False)
elif salt_bridge:
residues = t.topology.residues
resids1 = [r.resSeq for r in residues if r.resSeq < 150]
resids2 = [r.resSeq for r in residues if r.resSeq > 150]
print(resids1,"\n",resids2)
ax1.set_yticks(np.arange(0, len(resids1), 25))
ax1.set_yticklabels(resids1[::25])
ax2.set_yticks(np.arange(0, len(resids2), 25))
ax2.set_yticklabels(resids2[::25])
ax1.grid(False)
ax2.grid(False)
shared_ylabel = fig.add_subplot(111, frameon=False,
xticks=[], yticks=[])
shared_ylabel.yaxis.tick_left()
shared_ylabel.yaxis.set_label_coords(-0.5, 0)
shared_ylabel.yaxis.set_label_position("left")
shared_ylabel.set_ylabel("Residue Number", labelpad=75,
fontsize=24)
ax1.tick_params(axis='y', labelsize=20, direction='inout',
width=2, length=10, pad=10)
ax1.tick_params(axis='x', labelsize=20, direction='inout',
width=2, length=10)
else:
ax.set_ylabel("Residue Number", labelpad=5, fontsize=24)
ax.grid(False)
# Plot settings
ax.tick_params(axis='y', labelsize=20, direction='inout', width=2,
length=10, pad=10)
ax.tick_params(axis='x', labelsize=20, direction='inout', width=2,
length=10, pad=10)
plt.legend(handles=patches, bbox_to_anchor=(1.05, 1), loc=2,
borderaxespad=0., fontsize=18)
# X-axis labels
if time[-1] > 1e6:
frames = dssp.T.shape[0]
xticks = np.arange(0,frames+1,frames/10)
ax.set_xticks(xticks)
ax.set_xticklabels(list(map(lambda x : str(np.round(x/1e6,0)),
time[::len(time) // 10])))
ax.set_xlabel("Time (µs)", labelpad=5, fontsize=28)
elif umbrella:
xticks = np.arange(0,100e2+1,25e2)
ax.set_xticks(xticks)
c = ax.set_xticklabels(list(map(lambda x : str(x/1e2).split(".")[0],
xticks)))
ax.set_xlabel("Time (ns)", labelpad=5, fontsize=28)
bottom, top = ax.get_ylim()
ax.vlines(np.arange(25e2,100e2,25e2), bottom, top, ls="dashed",
lw=3, colors="#194D33",
path_effects=[pe.Stroke(linewidth=5, foreground='#E8E9E8'),
pe.Normal()])
plt.title(f"Window { w }", fontsize=24)
else:
frames = dssp.T.shape[1]
xticks = np.arange(0,frames+1,frames/10)
ax.set_xticks(xticks)
ax.set_xticklabels(list(map(lambda x : str(x/1e3).split(".")[0],
time[::len(time) // 10])))
ax.set_xlabel("Time (ns)", labelpad=5, fontsize=24)
if umbrella:
utils.save_figure(fig, f"{ path }/dssp_{ w }.png")
elif subset:
utils.save_figure(fig, f"{ path }/dssp_flaps.png")
else:
utils.save_figure(fig, f"{ path }/dssp.png")
plt.show()
plt.close()
return None
if __name__ == '__main__':
main(sys.argv)