-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMol_Analysis.py
More file actions
1098 lines (956 loc) · 41.9 KB
/
Mol_Analysis.py
File metadata and controls
1098 lines (956 loc) · 41.9 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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (C) 2018 Matteo Lambrughi, Matteo Tiberti, Maria Francesca
# Allega, Valentina Sora, Mads Nygaard, Agota Toth, Juan Salamanca Viloria,
# Emmanuelle Bignon, Elena Papaleo <elenap@cancer.dk>, Cancer Structural
# Biology, Danish Cancer Society Research Center, 2100, Copenhagen, Denmark
# Copyright (C) 2024 Joachim Breitenstein, Matteo Tiberti
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#!/usr/bin/env python3
__author__ = "Mads Nygaard, Agota Toth, Joachim Breitenstein, Matteo Tiberti"
__date__ = "07062024"
### Global dependencies
import os
import matplotlib as mpl
if os.environ.get('DISPLAY') is None:
mpl.use("Agg")
import matplotlib.pyplot as plt
import gromacs
import numpy as np
from copy import deepcopy
import multiprocessing as mp
### Monkey patch for mpi
if True:
def rename_attribute(object_, old_attribute_name, new_attribute_name):
setattr(object_, new_attribute_name, getattr(object_,
old_attribute_name))
rename_attribute(gromacs.tools, "Make_ndx_mpi", "Make_ndx")
rename_attribute(gromacs.tools, "Trjconv_mpi", "Trjconv")
rename_attribute(gromacs.tools, "Convert_tpr_mpi", "Convert_tpr")
rename_attribute(gromacs.tools, "Gmxcheck_mpi", "Gmxcheck")
rename_attribute(gromacs.tools, "G_mindist_mpi", "G_mindist")
rename_attribute(gromacs.tools, "Editconf_mpi", "Editconf")
rename_attribute(gromacs.tools, "G_rms_mpi", "G_rms")
rename_attribute(gromacs.tools, "G_gyrate_mpi", "G_gyrate")
rename_attribute(gromacs.tools, "G_rmsf_mpi", "G_rmsf")
### Classes
### Extended dict with variables for diff variables
class fnamesclass(dict):
def set_group(self, group):
self.group = group
def get_group(self):
return self.group
def set_folder(self, folder):
self.folder = folder
def get_folder(self):
return self.folder
def set_chains(self, chains):
if type(chains) is not list:
self.chains = [chains]
else:
self.chains = chains[:]
def get_chains(self):
return self.chains[:]
def merge(self, fnamesobj):
pass
def __repr__(self):
return "Group: %s , Folder: %s " % (self.group, self.folder) + \
super(fnamesclass, self).__repr__()
# def __getitem__(self, y):
# item = super(fnamesclass, self).__getitem__(y)
# temp = fnamesclass({y: item})
# temp.set_folder(self.get_folder())
# temp.set_group(self.get_group())
# return temp
class gTools_runner:
"""Runs gromacs wrapper, keeps track of input/output filenames etc."""
def __init__(self, wdir, odir="Mol_An", splitlen=[10], f=None, tpr=None, ndxfile=None,
outf=None, outs=None, outp=None, chains=None, center=None,
ndxparams=None, dryrun=False, skip=None, verbose=False,
dt=None, group=0):
# Globally sets verbose mode
gromacs.environment.flags['capture_output'] = not verbose
# Store different variables of self
self.splitlen = splitlen
self.dryrun = dryrun
if not os.path.isdir(wdir):
raise IOError("Folder:%s is not found" % wdir)
else:
self.wdir = os.path.abspath(wdir) + "/"
realodir = wdir+"/"+odir
# Create output directory if not present
if not os.path.isdir(realodir):
os.makedirs(realodir)
for i in splitlen:
if not os.path.isdir(realodir+"/rmsf"+str(i)):
os.makedirs(realodir+"/rmsf"+str(i))
if (os.path.isfile(wdir+"/residuetypes.dat")) and \
(not os.path.isfile(os.getcwd()+"/residuetypes.dat")):
import shutil
print("Copy residuetypes.dat to current dir. Can be removed after"
" script is completed.")
shutil.copyfile(wdir+"/residuetypes.dat", os.getcwd() +
"/residuetypes.dat")
self.odir = os.path.abspath(realodir) + "/"
self.ndxfile = self.odir + ndxfile
self.outf = self.odir + outf
self.outs = self.odir + outs
self.outp = self.odir + outp
self.f = self.wdir + f
self.tpr = self.wdir + tpr
self.logfile = self.odir + "file.log"
self.errfile = self.odir + "errfile.log"
self.fnames = fnamesclass()
self.fnames.set_group(group)
self.fnames.set_folder(self.odir)
if center is None:
self.center = "Protein"
else:
self.center = center
self.skip = skip
self.dt = dt
if chains is None:
self.fnames.set_chains("Protein")
else:
self.fnames.set_chains(chains)
# Try to guess the ndx parameters if missing from config
# (high chance of faliure here)
self.ndxparams = self._guessndxparam(ndxparams)
def _checkpath(self, fname, wdir):
"""Check if path is absolute, not used..."""
if fname[0] == "/":
return os.path.abspath(fname)
else:
return os.path.abspath(wdir + fname)
def _guessndxparam(self, ndxparams):
"""Very crude script to guess parameters for creation of ndx file.
Returns list of params. Self.chains, self.center needs to be
specified"""
params = []
if ndxparams is not None:
if type(ndxparams) is not list:
ndxparams = [ndxparams]
for param in ndxparams:
params.append(param.lower())
params.append(param.lower() + " & 3")
for chain in self.fnames.get_chains():
if not any(n in chain.lower() for n in ["ch", "protein"]):
print("Something fishy is going on with the chains")
elif "protein" in chain.lower():
params.append("1 & 3")
else:
params.append("chain " + chain.lower().split("ch ")[-1])
params.append("chain " + chain.lower().split("ch ")[-1] + " & 3")
params.append(self.center.replace("_", " ").replace("C-alpha", "3"))
return list(set(params))
def _timesplit(self, newdt, stop, start=0):
"""Returns list of (start,stop) points to split a trajectory
based on the newdt value."""
splits = []
if stop is None:
with open(self.odir + "r_gyrate.xvg", "r") as f:
lastline = f.readlines()[-1]
stop = int(float(lastline.split()[0]))
self.simlen = stop
if newdt < self.dt:
return ((None, None), )
else:
splits.append((start, (start+newdt)))
for n in range(start+newdt, stop, newdt):
#print(self.trjdt)
splits.append((n+self.trjdt, (n+newdt)))
return splits
def _errorcatch(self, excode, stdo, erro, funname):
"""Not used"""
print(erro)
print(stdo)
print("Something went wrong, %s exited with errorcode %s")
raw_input("Press any key to continiue")
def _addfilename(self, key, fname):
"""Add filename to the filenamedict"""
if fname not in self.fnames.get(key, []): # Add filename if unique
self.fnames.setdefault(key, []).append(fname)
def getfilenames(self):
"""Get filenamedict"""
return deepcopy(self.fnames)
def getodir(self):
"""Get output dir location(str)"""
return self.odir
def getinfo(self, fname):
"""Run gromacs check to get info from traj.
Returns (nframes,dt, simlen(fs))"""
g_check = gromacs.tools.Gmxcheck(
f=fname,
stdout=False,
stderr=False
)
print("Getting info from traj")
excode, out, err = g_check.run()
for line in err.split("\n"):
sline = line.split()
if all(k in sline for k in ("don't", "match")):
print(" ".join(sline))
import re
if len(re.findall(r"\((\d+)[ ,]", " ".join(sline))) > 0:
self.trjdt = int(re.findall(r"\((\d+)[ ,]", " ".join(sline))[0])
if len(sline) > 1:
if "Time" == sline[0]:
comments = list(map(int, sline[1:]))
break
self.frames = comments[0]
if len(comments) > 1:
self.trjdt = comments[1]
self.simlen = (comments[0]-1)*comments[1]
else:
self.simlen = None
return (self.frames, self.trjdt, self.simlen)
def mkndx(self, commands, ffile=None, **kwargs):
"""Runs gromacs make_ndx with commands(list)"""
if commands[-1].lower() != "q":
commands = list(commands)
commands.append("q")
if ffile is None:
ffile = self.tpr
g_mkndx = gromacs.tools.Make_ndx(
f=ffile,
o=self.ndxfile,
input=commands
)
if not self.dryrun:
print("Making ndx file")
g_mkndx.run(**kwargs)
def mindist(self, num_cores=10, **kwargs):
"""Merges output from mindist_slice, keeping header only once and removing duplicate rows."""
merged_file = f"{self.odir}/min_pbc_dist_merged.xvg"
if self.dryrun:
if os.path.isfile(merged_file):
self._addfilename("mindist", merged_file)
print(f"Reusing existing mindist file: {merged_file}")
else:
print(f"Dry run: existing mindist file not found: {merged_file}")
return
# Determine chunk size based on total simulation length and split
if not hasattr(self, 'simlen') or not hasattr(self, 'trjdt'):
self.getinfo(self.outf) # Populate simlen and trjdt attributes
# Calculate chunk boundaries using np.linspace
ends = np.linspace(0, self.simlen, num_cores + 1, dtype=int)
splits = [(ends[i], ends[i + 1] - 1) if i < len(ends) - 2 else (ends[i], ends[i + 1])
for i in range(len(ends) - 1)]
print(f"Total time slices for mindist: {len(splits)}")
# Run mindist_slice in parallel to generate individual .xvg files
pool = mp.Pool(processes=num_cores)
args = [(begin, end, self.outf, self.tpr, self.odir, self.dryrun) for begin, end in splits]
with mp.Pool(processes=num_cores) as pool:
results = [data for data in pool.starmap(mindist_slice, args) if data is not None]
# Convert merged data to a numpy array for easier handling
merged_data = np.vstack(results)
# Prepare the headers manually
headers = [
'@ title "Minimum Distance Over Time"',
'@ xaxis label "Time (ps)"',
'@ yaxis label "Distance (nm)"',
'@TYPE xy'
]
header_str = "\n".join(headers)
np.savetxt(merged_file, merged_data, fmt= ' '.join(['%d'] + ['%.3f'] * (merged_data.shape[1] - 1)), delimiter=' ', header=header_str, comments='')
self._addfilename("mindist", merged_file)
print(f"Merged file created at: {merged_file}")
def editconf(self, **kwargs):
"""Generates a new conf file"""
g_editconf = gromacs.tools.Editconf(
f=self.tpr,
n=self.ndxfile,
o=self.outs,
input="Protein",
)
if not self.dryrun:
print("Updating conf")
excode, out, err = g_editconf.run()
def trjconv(self, **kwargs):
"""Generates a new centered trjconv file"""
center = self.center
g_trjconv = gromacs.tools.Trjconv(
f=self.f,
s=self.tpr,
o=self.outf,
n=self.ndxfile,
skip=self.skip,
dt=self.dt,
pbc="cluster",
ur="compact",
center=True,
input=("Protein", center, "Protein"),
) # trjconv -f traj.trr -s npt.tpr -dump time -o confout.dump.gro -sep -pbc mol -ur compact
if not self.dryrun:
print("Centering trajectory")
excode, out, err = g_trjconv.run()
self.getinfo(self.outf)
def convert_tpr(self, **kwargs):
"""Generates a new tpr file with a specific selection of atoms"""
g_convert_tpr = gromacs.tools.Convert_tpr(
s=self.tpr,
o=self.outp,
n=self.ndxfile,
input = "Protein") # echo "Protein" | gmx_mpi convert_tpr -s sim.tpr -o update.tpr -index index.ndx:
if not self.dryrun:
print("Generating filtered tpr")
excode, out, err = g_convert_tpr.run()
def rmsd(self, **kwargs):
"""Calcs the rmsd for the two chains"""
chains = self.fnames.get_chains()
for chain in chains:
outfile = self.odir + "rmsd_%s.xvg" % (chain)
self._addfilename("rmsd", outfile)
g_rmsd = gromacs.tools.G_rms(
f=self.outf,
s=self.outp,
o=outfile,
n=self.ndxfile,
input=(chain, chain),
)
if not self.dryrun:
print("Calculating RMSD")
excode, out, err = g_rmsd.run()
def rmsd_matrix(self, mainchain_group="Mainchain", rmsd_group="Mainchain", **kwargs):
"""
Runs gmx rms and extracts the average RMSD (mainchain-mainchain).
"""
if self.dryrun:
return
rmsd_xvg = self.odir + "rmsd.xvg"
rmsd_file = self.odir + "rmsd_matrix.xpm"
g_rms = gromacs.tools.G_rms(
f=self.outf,
s=self.outp,
o=rmsd_xvg,
m=rmsd_file,
dt=200,
input=(str(mainchain_group), str(rmsd_group))
)
print("Calculating RMSD matrix and extracting average RMSD...")
excode, out, err = g_rms.run()
if excode != 0:
print("gmx rms failed for RMSD matrix calculation.")
return None
avg_rmsd = None
compiled_re = re.compile(r"^RMSD: Min .*, Max .*, Avg (.*)")
lines = out.split('\n') + err.split('\n')
for line in lines:
match = compiled_re.match(line)
if match:
avg_rmsd = match.groups()[0]
try:
avg_rmsd = float(avg_rmsd)
except ValueError:
print("Error: Extracted Avg RMSD is not a valid float.")
return None
print(f"Average RMSD: {avg_rmsd:.6f} nm")
stats_file = os.path.join(self.odir, "rmsd_matrix_avg.txt")
np.savetxt(stats_file, [avg_rmsd], header="RMSD Matrix Average (nm)", comments="", fmt="%.6f")
return avg_rmsd
print("Could not find Avg RMSD in the output.")
return None
def rg(self, **kwargs):
"""Calcs the rg for the complete protein"""
outfile = self.odir + "r_gyrate.xvg"
self._addfilename("rg", outfile)
g_rg = gromacs.tools.G_gyrate(
f=self.outf,
s=self.outp,
o=outfile,
input="protein",
)
if not self.dryrun:
print("Calculating Rg")
excode, out, err = g_rg.run()
def rmsf(self, splitlen, **kwargs):
"""Calcs the rmsf broken into chunks of splitlen(ns) size"""
#chains = [x for x in self.fnames.get_chains() if "C-alpha" in x]
chains = self.fnames.get_chains()
splitlenfs = splitlen*1000
try:
splits = self._timesplit(splitlenfs, self.simlen)
except AttributeError:
self.getinfo(self.outf)
splits = self._timesplit(splitlenfs, self.simlen)
for chain in chains:
chain = chain + "_&_C-alpha"
chain_fname = chain.replace("&", "n") # Fix for not using & in filename
for begin, end in splits:
outfile = self.odir + "rmsf"+str(splitlen) + "/rmsf_%s_%s.xvg" % (chain_fname, end)
self._addfilename("rmsf" + str(splitlen) + "_" + chain_fname, outfile)
g_rmsf = gromacs.tools.G_rmsf(
f=self.outf,
s=self.outp,
o=outfile,
od=self.odir + "rmsf"+str(splitlen) + "/rmsf_od_%s_%s.xvg" % (chain_fname, end),
b=begin,
e=end,
n=self.ndxfile,
res=True,
input=(chain, chain),
)
if not self.dryrun:
print("Calculating RMSF t %s to %s" % (begin, end))
excode, out, err = g_rmsf.run()
def pdbout(self, pdbname="pdbmovie.pdb", **kwargs):
"""Outputs a pdb trajectory, protein fitted of the centered traj"""
g_pdbout = gromacs.tools.Trjconv(
f=self.outf,
s=self.outp,
o=self.odir + pdbname,
fit="rot+trans",
input=["Protein", "Protein"]
)
if not self.dryrun:
print("Printing .pdb movie")
excode, out, err = g_pdbout.run(**kwargs)
def runall(self, num_cores=10, **kwargs):
self.torun = {"mkndx": True, "mindist": True, "trjconv": True,
"editconf": True, "convert_tpr": True, "rmsd": True,
"rg": True, "rmsf": True, "pdbout": True}
self.torun.update(kwargs)
if self.torun["mkndx"] is True: # To create the .gro file
self.mkndx(commands=self.ndxparams, ffile=self.tpr)
#print self.ndxparams
if self.torun["editconf"] is True:
self.editconf()
# if self.torun["mkndx"] is True: #Consisting of only protein
# self.mkndx(commands = self.ndxparams, ffile = self.outs)
if self.torun["convert_tpr"] is True:
self.convert_tpr()
if self.torun["trjconv"] is True:
self.trjconv()
else:
self.getinfo(self.outf)
if self.torun["mindist"] is True:
self.mindist(num_cores=num_cores)
if self.torun["rmsd"] is True:
self.rmsd()
if self.torun["rg"] is True:
self.rg()
if self.torun["rmsf"] is True:
for i in self.splitlen:
self.rmsf(i)
if self.torun.get("rmsd_matrix", True):
self.rmsd_matrix()
if self.torun["pdbout"] is True:
self.pdbout(skip=100)
class gTools_plotter:
"""Plotting tool for xvg files made with Gromacs.
Uses the gromacs wrapper"""
class xvg_read(gromacs.fileformats.XVG):
"""Updated class of the XVG reader to handle current axes better"""
def plot(self, ax, **kwargs):
self.fig.sca(ax)
gromacs.fileformats.XVG.plot(self, **kwargs)
return self.fig.gca()
def __init__(self, fnames, odir, begin=0, end=None, splitlen=[10], chains=["Protein"], figsize=(8.27, 11.69)):
"""Makes the figure etc"""
from cycler import cycler
self.fnames = fnames
self.odir = odir
self.begin = begin
self.end = end
self.splitlen = splitlen
self.fig = plt.figure(figsize=figsize)
plt.rc('axes', prop_cycle=(cycler('color', ['k', 'r', 'g', 'b', 'm', 'y', 'c'])))
self.axlist = []
print(self.odir)
def _filter_rmsfiles(self, flist, begin, end):
# Removing rmsf times if begin is set
if end is not None:
shortend_list = [f for f in flist if (int(
re.search(r"(\d+)\.xvg", f).group(1)) >= begin) and
(int(re.search(r"(\d+)\.xvg", f).group(1)) <= end)]
else:
shortend_list = [f for f in flist if (int(
re.search(r"(\d+)\.xvg", f).group(1)) >= begin)]
if len(shortend_list) <= 0:
print("Less than 0 rmsf files after filtering, using original list")
shortend_list = flist
return shortend_list
def resplotstd(self, flist, label, ax=None, std_fill=True, std_bar=True,
shift=0, **kwargs):
"""Plots residues vs val. Calculates the stddev for the filelist"""
if ax is None:
ax = plt.gca()
filter_rmsflist = self._filter_rmsfiles(flist=flist, begin=self.begin, end=self.end)
data, xaxis, yaxis = self.stddata(filter_rmsflist)
upperstd = data[1]+data[2]
lowerstd = data[1]-data[2]
data[0] = data[0]+shift
lines = ax.plot(data[0], data[1], label=label, **kwargs)
plotcol = lines[-1].get_color()
if std_fill:
ax.fill_between(data[0],
upperstd,
lowerstd,
alpha=0.1,
facecolor=plotcol,
lw=0)
if std_bar:
ax.bar(data[0], data[2], color=plotcol)
ax.set_xlabel(xaxis)
ax.set_ylabel(yaxis)
#ax.set_ylim((0, upperstd[2:-3].max()))
ax.set_xlim((data[0].min(), data[0].max()))
ax.tick_params(direction='out')
ax.yaxis.tick_left()
ax.xaxis.tick_bottom()
# ax.set_ylim((0, 1.2))
# if "chB" in label:
# ylims = [0, 0.3]
# elif "chA" in label:
# ylims = [0, 0.45]
# ax.set_ylim(ylims)
# self.axlist[-1].xaxis.set_minor_locato(AutoMinorLocator(1))
def stddata(self, filelist):
"""Calculates the stddev for the files in the filelist.
Returns (np.array(resnr|data|stddev),xlabel,ylabel)"""
datalist = []
for fname in filelist:
if not os.path.isfile(fname):
fname = fname.replace("_n_", "_&_")
data = self.xvg_read(fname)
datalist.append(data.array[1])
xaxis, yaxis = data.xaxis, data.yaxis
dataarray = np.array(datalist)
arrayout = np.array((data.array[0], dataarray.mean(axis=0),
dataarray.std(axis=0)))
return (arrayout, xaxis, yaxis)
def tplot(self, flist, label, winsize=20, **kwargs):
"""Plots time vs value for all files in flist with a smoothed version
on top."""
colours = ["k", "r", "g", "b", "m", "y", "c"]
for idx, fname in enumerate(flist):
color = colours[idx % len(colours)]
boxlabel = None
if "rmsd" in label:
boxlabel = fname[-11:-4]
if (fname[-11] == "r") and ("ch" not in boxlabel):
boxlabel = fname[-9:-4]
if "ch" in boxlabel:
boxlabel = fname[-7:-4]
data = self.xvg_read(fname)
data.parse()
if "Time (ps)" in data.xaxis:
xlabel = "Time (ns)"
xvals = data.array[0]*0.001
ylabel = data.yaxis
smoothnum = winsize//(xvals.max()/len(xvals))
if not smoothnum % 2:
smoothnum -= 1
if smoothnum < 2:
smoothnum = 3
self.axlist[-1].plot(xvals, data.array[1], label=boxlabel, markersize=15,
c=color, alpha=0.5, **kwargs)
if winsize < xvals.max():
self.axlist[-1].plot(xvals, savitzky_golay(data.array[1],
smoothnum, 1), c=color, **kwargs)
self.axlist[-1].set_xlabel(xlabel)
self.axlist[-1].set_ylabel(ylabel)
if "rmsd" in label:
self.axlist[-1].legend(loc="lower right", fontsize="x-small")
if self.end is not None:
self.axlist[-1].set_xlim(xmin=self.begin/1000, xmax=self.end/1000)
else:
self.axlist[-1].set_xlim(xmin=self.begin/1000)
self.axlist[-1].tick_params(direction='out')
self.axlist[-1].yaxis.tick_left()
self.axlist[-1].xaxis.tick_bottom()
# ylims = None
# if label == "rmsd":
# ylims= [0, 1.0]
# if ylims is not None:
# self.axlist[-1].set_ylim(ylims)
def showfig(self):
"""Shows the fig in a window"""
self.fig.show()
raw_input("Press key to close window")
def savefig(self, filename, local=False):
"""Saves the fig as filename"""
if local is True:
if not os.path.isdir("./Mol_Analysis"):
os.makedirs("./Mol_Analysis")
self.fig.savefig("./Mol_Analysis/" + filename, format="pdf")
else:
self.fig.savefig(self.odir + filename, format="pdf")
def addplot(self, ncols, nrows, count):
self.axlist.append(self.fig.add_subplot(ncols, nrows, count))
def plotall(self, groupfolderlist, local=True, ncols=3, nrows=2, **kwargs):
"""Plots all files in self.fnames in one figure"""
self.torun = {"mindist": True, "trjconv": True, "editconf": True,
"rmsd": True, "rg": True, "rmsf": True, "pdbout": True}
self.torun.update(**kwargs)
nplots = len(self.fnames.keys())
if nrows==2 and nplots>6:
sys.exit("More than 6 plot! Please use -large")
print("Plotting %s plots" % (nplots))
protein_name = groupfolderlist[0][0]
# Set the title with the protein name and "simulation"
self.fig.suptitle(f"{protein_name} simulation")
count = 0
for key, flist in sorted(self.fnames.items()):
count += 1
self.addplot(ncols, nrows, count)
if "rmsf" in key:
self.resplotstd(flist=flist, label=key, ax=self.axlist[-1])
elif "group" in key:
pass
else:
self.tplot(flist=flist, label=key)
self.axlist[-1].set_title(key.replace("_", " "))
self.fig.tight_layout()
self.fig.subplots_adjust(top=0.92)
if local is True:
localpath = os.getcwd().split("/")
tags = [x for x in self.odir.split("/") if x not in localpath]
fname = "plot_" + "_".join(tags) + ".pdf"
self.savefig(fname, local=True)
else:
fname = "plot.pdf"
self.savefig(fname)
### Functions
def mindist_slice(begin, end, outf, tpr, odir, dryrun):
output_file = f"{odir}/mindist_chunk_{begin}_{end}.xvg"
# Run GROMACS mindist to generate the .xvg file for this slice
g_mindist = gromacs.tools.G_mindist(
f=outf,
s=tpr,
pi=True,
od=output_file,
b=begin,
e=end,
input="Protein",
stdout=False,
stderr=False
)
if not dryrun:
try:
excode, out, err = g_mindist.run()
if excode != 0:
print(f"g_mindist failed for slice {begin}-{end}: excode={excode}, err={err}")
return None
else:
print(f"g_mindist completed for slice {begin}-{end}: excode={excode}")
except Exception as e:
print(f"Exception occurred while running g_mindist for slice {begin}-{end}: {e}")
return None
else:
print(f"Dry run: g_mindist would have been run for slice {begin}-{end}")
data = np.loadtxt(output_file, comments=('@', '#'))
os.remove(output_file)
return data
def savitzky_golay(y, window_size, order, deriv=0, rate=1):
r"""Smooth (and optionally differentiate) data with a Savitzky-Golay filter.
The Savitzky-Golay filter removes high frequency noise from data.
It has the advantage of preserving the original shape and
features of the signal better than other types of filtering
approaches, such as moving averages techniques.
Parameters
----------
y : array_like, shape (N,)
the values of the time history of the signal.
window_size : int
the length of the window. Must be an odd integer number.
order : int
the order of the polynomial used in the filtering.
Must be less then `window_size` - 1.
deriv: int
the order of the derivative to compute (default = 0 means only smoothing)
Returns
-------
ys : ndarray, shape (N)
the smoothed signal (or it's n-th derivative).
Notes
-----
The Savitzky-Golay is a type of low-pass filter, particularly
suited for smoothing noisy data. The main idea behind this
approach is to make for each point a least-square fit with a
polynomial of high order over a odd-sized window centered at
the point.
References
----------
.. [1] A. Savitzky, M. J. E. Golay, Smoothing and Differentiation of
Data by Simplified Least Squares Procedures. Analytical
Chemistry, 1964, 36 (8), pp 1627-1639.
.. [2] Numerical Recipes 3rd Edition: The Art of Scientific Computing
W.H. Press, S.A. Teukolsky, W.T. Vetterling, B.P. Flannery
Cambridge University Press ISBN-13: 9780521880688
"""
# From http://scipy.github.io/old-wiki/pages/Cookbook/SavitzkyGolay
import numpy as np
from math import factorial
try:
window_size = np.abs(int(window_size))
order = np.abs(int(order))
except ValueError:
raise ValueError("window_size and order have to be of type int")
if window_size % 2 != 1 or window_size < 1:
raise TypeError("window_size size must be a positive odd number")
if window_size < order + 2:
raise TypeError("window_size is too small for the polynomials order")
order_range = range(order+1)
half_window = (window_size - 1) // 2
# precompute coefficients
b = np.mat([[k**i for i in order_range] for k in
range(-half_window, half_window+1)])
m = np.linalg.pinv(b).A[deriv] * rate**deriv * factorial(deriv)
# pad the signal at the extremes with
# values taken from the signal itself
firstvals = y[0] - np.abs(y[1:half_window+1][::-1] - y[0])
lastvals = y[-1] + np.abs(y[-half_window-1:-1][::-1] - y[-1])
y = np.concatenate((firstvals, y, lastvals))
return np.convolve(m[::-1], y, mode='valid')
### Main
if __name__ == "__main__":
import argparse
import configparser
import tempfile
import re
# Classes in Main
class Listconfigparser(configparser.ConfigParser):
def listread(self, fname):
self.dirs = []
# Open the temporary file in text mode with UTF-8 encoding
with tempfile.TemporaryFile(mode='w+', encoding='utf-8') as tfile:
with open(fname, "r") as f:
folderlist = False
for line in f:
if line.strip() == "[Folders]":
folderlist = True
continue
elif "[" in line and folderlist:
folderlist = False
if folderlist and line.strip() and not line.startswith("#"):
parts = line.strip().split(None, 1)
if len(parts) == 1:
self.dirs.append(("0", parts[0]))
else:
self.dirs.append(tuple(parts))
elif not folderlist:
tfile.write(line)
tfile.seek(0)
self.read_file(tfile)
return self.getlist()
def listwrite(self, f):
"""Writes the config to the file f including the list"""
if len(self.dirs) > 0:
f.write("[Folders]\n")
f.writelines(self.forprint(self.dirs))
f.write("")
self.write(f)
def forprint(self, alist):
newlist = []
for n in alist:
newlist.append(" ".join(n)+"\n")
return newlist
def appendfolder(self, dirname):
"""Append to the list"""
self.dirs.append(dirname+"\n")
def getlist(self):
"""Gets the list cleaned"""
return self.dirs
def multithreader(l_processes, n_threads): # A default mulithreader
import queue
import threading
import time
exitFlag = 0
if len(l_processes) < n_threads:
n_threads = len(l_processes)
class updatedThread(threading.Thread):
def __init__(self, threadID, name, q):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.q = q
def run(self):
print("Starting T" + self.name)
process_data(self.name, self.q)
print("Exiting T" + self.name)
def process_data(threadName, q):
while not exitFlag:
queueLock.acquire()
if not workQueue.empty():
data = q.get()
queueLock.release()
time.sleep(float(threadName)*0.01)
print("T%s running:%s" % (threadName, data[1]))
runandplot(data[1], group=data[0])
else:
queueLock.release()
threadList = range(n_threads)
nameList = l_processes
queueLock = threading.Lock()
workQueue = queue.Queue(0)
threads = []
threadID = 1
tnow = time.time()
# Create new threads
for tName in threadList:
thread = updatedThread(threadID, tName, workQueue)
thread.start()
threads.append(thread)
threadID += 1
# Fill the queue
queueLock.acquire()
for word in nameList:
workQueue.put(word)
queueLock.release()
# Wait for queue to empty
while not workQueue.empty():
pass
# Notify threads it's time to exit
exitFlag = 1
# Wait for all threads to complete
for t in threads:
t.join()
print("Exiting Main Thread after %d sec" % (time.time()-tnow)
)
### Argparser
parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,
description=("This is a script that can "
"assist in plotting data of "
"MD simulations with "
"matplotlib."))
parser.add_argument("-f", metavar="config.cfg", default="config.cfg",
help="Specify the input config file")
parser.add_argument("-n", action="store_true",
help="Dryrun, no gromacs analysis are run")
parser.add_argument("-nomindist", action="store_false",
help="No gromacs mindist analysis are run")
parser.add_argument("-nopdb", action="store_false",
help="No .pdb movie are output")
parser.add_argument("-v", action="store_true", help="Verbose mode")
parser.add_argument("-nt", metavar="N", type=int, default=1,
help="Number of threads/cores for parallel processing")
parser.add_argument("-local", action="store_true",
help="Locate plots in folder of script")
parser.add_argument("-nogroup", action="store_true", help="No groupplot")
parser.add_argument("-begin", type=int, default=0, help="Begin from time X (fs)")
parser.add_argument("-end", type=int, default=None, help="End at time X (fs)")
parser.add_argument("-large", action="store_true", help="Can plot 9 plots")
parser.add_argument("-splitlen", metavar="slen", default=10, help="Length of timewindow for RMSF calculation (ns). If multiple than separated with comma")
parser.add_argument("-nomkndx", action="store_false",
help="Do not recreate index file; reuse existing ndxfile")
args = parser.parse_args()
timescales = []
try:
timescales = args.splitlen.split(",")
except AttributeError:
timescales.append(args.splitlen)
timescales = [int(i) for i in timescales]
# print timescales
# Comment following lines out
#args.f = "./config_1us.cfg"
#args.n = True
#args.local = True
#args.nogroup = True
#args.nomindist = False
#args.nopdb = False
#args.begin = 10000
# Start by reading config file
config = Listconfigparser()
config.listread(args.f)
groupfolderlist = config.getlist()
# Storing defaults here:
settings = {"odir": "Mol_An", "f": None, "tpr": None,
"outf": "center_traj.xtc", "outs": "updated.gro",
"outp": "updated.tpr", "ndxfile": "index.ndx",
"ndxparams": None, "chains": ["Protein"], "center": "Protein",
"skip": None, "dt": 1}
# Sorting out the config file, switching out the defaults:
for key in sorted(settings.keys()):
try: