-
Notifications
You must be signed in to change notification settings - Fork 12
/
gromit.sh
executable file
·2484 lines (1932 loc) · 81 KB
/
gromit.sh
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
#!/bin/bash
PROGRAM=${0##*/}
VERSION=2.5 # 20170706.1200
AUTHORS="Tsjerk A. Wassenaar, PhD"
YEAR="2018"
AFFILIATION="
University of Groningen
Nijenborgh 7
9747AG Groningen
The Netherlands"
: << __NOTES_FOR_USE_AND_EDITING__
IF YOU CHANGE THE PARAMETERS AND/OR WORKFLOW, PLEASE RENAME THE PROGRAM AND
STATE THE NATURE AND PURPOSE OF THE CHANGES MADE.
This has grown to be a rather complicated bash script. It is intended to
work through the MD process as a person would, issuing shell commands and reading
and editing files. Bash feels more natural for this than a Python/C wrapper.
It is advised to (get to) know about bash loops and variable substitution, as
these are used plenty. In addition, since there are many occassions where files
need to be read and edited, there are a lot of calls to sed, with quite a
few less ordinary commands.
To keep the code manageable, it is structured in sections and every section is
ordered, preferrably by numbered chunks. In addition, there is extensive
documentation. Every statement should be clear, either by itself or by a
preceding explanation. In case advanced bash/sed/... features are used, they
ought to be explained. That will keep the program manageable and make it a nice
place for learning tricks :)
Oh, and please note that usual copyright laws apply...
TAW - 20120718
__NOTES_FOR_USE_AND_EDITING__
DESCRIPTION=$(cat << __DESCRIPTION__
$PROGRAM $VERSION is a versatile wrapper for setting up and running
molecular dynamics simulations of proteins and/or nucleic acids in solvent.
The script contains a complete and flexible workflow, consisting of the
following steps:
1. Generate topology from input structure
A. Process structure against force field (TOPOLOGY)
B. Add ligands (LIGANDS)
2. Set up periodic boundary conditions (BOX)
3. Energy minimize system in vacuum (EMVACUUM)
4. Solvation and adding ions (SOLVATION)
5. Energy minimization (EMSOL)
6. Position restrained NVT equilibration (NVT-PR)
7. Unrestrained NpT equilibration (NPT)
8. Equilibration under run conditions (PREPRODUCTION)
9. Production simulation
A. Run input file (TPR)
B. Simulation (possibly in parts) (PRODUCTION)
The program allows running only part of the workflow by specifying the
start and end step (-step/-stop), using an argument uniquely matching
one of the tags given between parentheses.
This program requires a working installation of Gromacs. To link
the program to the correct version of Gromacs, it should be placed in the
Gromacs binaries directory or the Gromacs GMXRC file should be passed as
argument to the option -gmxrc
The workflow contained within this program corresponds to a standard protocol
that should suffice for routine molecular dynamics simulations of proteins
and/or nucleic acids in aqueous solution. It follows the steps that are
commonly taken in MD tutorials (e.g. http://md.chem.rug.nl/~mdcourse/molmod2012/).
This program is designed to enable high-throughput processing of molecular
dynamics simulations in which specific settings are varied systematically. These
settings include protein/nucleic acid, ligand, temperature, and pressure, as well
as many others.
The program supports a number of specific protocols, including Linear Interaction
Energy (LIE), Transition Path Sampling (TPS), and GRID-distributed MD.
## -- IMPORTANT -- ##
Molecular dynamics simulations are complex, with many contributing factors.
The workflow in this program has been tested extensively and used many times.
Nonetheless, it should not be considered failsafe. No MD protocol ever is.
Despite careful set up, simulations may crash, and the possibility that a crash
is encountered is larger when many simulations are run. If the run crashes,
the intermediate results will be kept and can be investigated to identify the
source of the problem.
If the run finishes to completion, this does not automatically imply that the
results are good. The results from the simulations should always be subjected
to integrity and quality assurance checks to assert that they are correct within
the objectives of the study.
__DESCRIPTION__
)
#--------------------------------------------------------------------
#---Parsing COMMAND LINE ARGUMENTS AND DEPENDENCIES--
#--------------------------------------------------------------------
CMD="$0 $@"
echo "$CMD" | tee CMD
# Directory where this script is
SDIR=$( [[ $0 != ${0%/*} ]] && cd ${0%/*}; pwd )
SRCDIR="$SDIR"/source
FFDIR="$SDIR"/forcefield
# Sourcing modules
source "$SRCDIR"/_logging.sh
source "${SRCDIR}"/_optionhandling.sh
source "${SRCDIR}"/_functions.sh
source "${SRCDIR}"/_mdp_gromit.sh
source "${SRCDIR}"/_mdp.sh
source "${SRCDIR}"/_mdrunner.sh
source "${SRCDIR}"/_pdb.sh
source "${SRCDIR}"/_gmx.sh
trap "archive" 2 9 15
# These will be looked for before running, and can be set from the cmdline, e.g.:
# -gmxrc /usr/local/gromacs-5.1/bin/GMXRC
# If not set, the default name will be searched for in
# 1. the environment (if PROGEVAR is given)
# 2. the directory where this calling script (gromit) is located
# 3. the PATH
DEPENDENCIES=( gmxrc squeeze)
PROGEXEC=( GMXRC squeeze)
PROGEVAR=( GMXRC)
# Residue groups used for classifying atoms in the structure file.
# Ions are typically considered positioned after solvent.
# Membrane is the complementary group to the rest.
# The structure file is assumed to have the following composition:
# - Protein
# - Nucleic acids
# - Membrane
# - Solvent
# - Ions
# All groups are optional (as long as there are some)
amino_acids=(ALA CYS ASP GLU PHE GLY HIS HIH ILE LYS LEU MET ASN PRO HYP GLN ARG SER THR VAL TRP TYR)
nucleic_acids=(DG DA DC DT G A C U)
solvent_names=(W WF PW BMW SOL HOH)
# Run control
MONALL= # Monitor all steps
CONTROL=
CHECKTIME=300 # Run control every five minutes
# Stepping stuff
STEPS=(TOPOLOGY LIGANDS BOX EMVACUUM SOLVATION EMSOLVENT NVT-PR NPT PREPRODUCTION TPR PRODUCTION ANALYSIS END)
get_step_fun() { for ((i=0; i<${#STEPS[@]}; i++)) do [[ ${STEPS[$i]} =~ ^$1 ]] && echo $i; done; }
STEP=
STOP=PRODUCTION
# Force field
ForceFieldFamilies=(gromos charmm amber opls )
ForceFieldSolvents=(spc tip3p tip3p tip4p)
SolventFiles=( spc216 spc216 spc216 tip4p)
ForceField=gromos45a3
WaterModel=
SolModel=
SolName=SOL
SolFile=
LIGANDS=()
VirtualSites=false
AtomTypes=()
MoleculeTypes=()
# Options:
# Downstream programs:
# This program:
# - protein:
fnIN= # Input file name
TOP= # Topology file
HETATM=true # Keep HETATM records in input PDB file
# - run control and files
DIR="." # Directory to run and write
TPR= # Run input file... skip to production or to STEP
NAME= # Run name
FETCH= # Try to fetch PDB file from web
MSGFILE=/dev/stdout # Master log file (stdout)
ERRFILE=/dev/stderr # Error log file (stderr)
EXEC= # Execute run
NP=1 # Number of processors
MDP= # User-provided MDP file
MDARGS= # User-provided arguments to mdrun
MAXH=-1 # Maximum duration of run
JUNK=() # Garbage collection
SCRATCH= # Scratch directory
ARCHIVE= # Archive file name
FORCE=false # Overwrite existing run data
KEEP=false # Keep intermediate rubbish (except junk)
GMXRC= # File for loading GMX version
SQUEEZE= # Squeeze executable
ANALYSIS=() # Analysis tags
# - system setup
PBCDIST=2.25 # Minimal distance between periodic images
BOXTYPE=dodecahedron # Box type
Salt=NA,CL # Salt species
SaltCharge=1,-1 # Charges of salt species
Salinity=0.1539976 # Salt concentration of solvent
CHARGE= # Net charge to set on system
NDLP=false # Use optimal simulation cell (near-densest-lattice-packing)
# - group definitions
NATOMS=0 # Total number of atoms
Biomol=() # Biomolecules (protein, nucleic acid, lipid)
Solute=() # Solute molecule (protein or so)
Membrane=() # Lipids, excluding protein
Solvent=() # Solvent, including ions
Ligand=() # Ligands
Ligenv=() # Ligand environment to consider for LIE contributions
CoupleGroups=() # Groups for temperature coupling
EnergyGroups=() # Groups for energy calculations
LIE=false # LIE run
# - simulation parameters
TIME=0 # Production run time (ns)
AT=0.05 # Output frequency for positions, energy and log (ns)
EquilTime=0.1 # Equilibration run time (ns)
PreTime=0.5 # Preproduction run time (ns)
EMSteps=500 # Number of steps for EM
Temperature=200,300 # Degree Kelvin
Tau_T=0.1 # ps
Pressure=1 # Bar
Tau_P=1.0 # ps
PosreFC=200,200 # Position restraint force constant(s)
Electrostatics= # Electrostatics scheme to use if not opting for default
SEED=$$ # Random seed for velocity generation
RotationalConstraints= # Use rotational constraints, which is mandatory with NDLP
# User defined gromacs program options and simulation parameters (way flexible!)
PROGOPTS=() # User-defined program options (--program-option=value)
MDPOPTS=() # User-defined mdp parametesrs (--mdp-option=value)
# Collect errors, warnings and notes to (re)present to user at the end
# Spaces are replaced by the unlikely combination QQQ to keep the
# messages together.
errors_array=()
store_error_fun() { a="$@"; errors_array+=(${x// /QQQ}); FATAL "$@"; }
warnings_array=()
store_warning_fun() { a=$@; warnings_array+=(${x// /QQQ}); WARN "$@"; }
notes_array=()
store_note_fun() { a=$@; notes_array+=(${x// /QQQ}); NOTE "$@"; }
##>> OPTIONS
if [[ -z "$1" ]]; then
echo "No command line arguments give. Please read the program usage:"
USAGE 1
exit
fi
hlevel=1
olevel=1
while [ -n "$1" ]; do
# Check for program option
depset=false
NDEP=${#DEPENDENCIES[@]}
for ((i=0; i<$NDEP; i++))
do
if [[ $1 == "-${DEPENDENCIES[$i]}" ]]
then
PROGEXEC[$i]=$2
shift 2
depset=true
fi
done
# If we set a dependency, skip to the next cycle
$depset && continue
# Check for other options
case $1 in
#=0
#=0 OPTIONS
#=0 =======
#=0
-h ) USAGE 0 ; exit 0 ;; #==0 Display basic help
--help ) hlevel=9; olevel=9; USAGE 0 ; exit 0 ;; #==1 Display all help (advanced users)
-hlevel ) hlevel=$2 ; shift 2; continue ;; #==1 Set level of help (use before -h/--help)
-olevel ) olevel=$2 ; shift 2; continue ;; #==1 Set level of options to display
#=1
#=1 File options
#=1 ------------
#=1
-f ) fnIN=$2 ; shift 2; continue ;; #==0 Input coordinate file (PDB)
-g ) MSGFILE=$2 ; shift 2; continue ;; #==1 Standard output log file (default: /dev/stdout)
-e ) ERRFILE=$2 ; shift 2; continue ;; #==1 Standard error log file (default: /dev/stderr)
-tpr ) TPR=$2 ; shift 2; continue ;; #==1 Run input file
-name ) NAME=$2 ; shift 2; continue ;; #==1 Name of project
-top ) TOP=$2 ; shift 2; continue ;; #==1 Input topology file
-atp ) AtomTypes+=($2) ; shift 2; continue ;; #==2 Additional atom type definitions (force field file)
-itp ) MoleculeTypes+=($2) ; shift 2; continue ;; #==2 Additional molecule type definitions
-l ) LIGANDS+=($2) ; shift 2; continue ;; #==2 Ligands to include (topology or structure,topology)
-mdp ) MDP=$2 ; shift 2; continue ;; #==2 MDP (simulation parameter) file
-scratch ) SCRATCH=$2 ; shift 2; continue ;; #==2 Scratch directory to perform simulation
-fetch ) FETCH=$2 ; shift 2; continue ;; #==1 Database to fetch input structure from
-rmhet ) HETATM=false ; shift ; continue ;; #==2 Whether or not to remove HETATM records
#=1
#=1 Overall control options
#=1 -----------------------
#=1
-step ) STEP=$2 ; shift 2; continue ;; #==1 Step to start protocol
-stop ) STOP=$2 ; shift 2; continue ;; #==1 Step to end protocol
-grid ) GRID=true ; shift 1; continue ;; #==2 GRID-enabled run
-keep ) KEEP=true ; shift ; continue ;; #==2 Whether or not to keep intermediate data
-dir ) DIR=$2 ; shift 2; continue ;; #==2 Directory where to perform simulation (make if required)
-np ) NP=$2 ; shift 2; continue ;; #==1 Number of processors/threads to use
-maxh ) MAXH=$2 ; shift 2; continue ;; #==2 Maximum time to run (in hours)
-archive ) ARCHIVE=${2%.tgz}.tgz ; shift 2; continue ;; #==2 Archive file name to save data in
-force ) FORCE=true ; shift ; continue ;; #==2 Whether or not to force redoing parts already run
-noexec ) EXEC=echo ; shift ; continue ;; #==2 Whether or not to actually execute the commands
#=1
#=1 Simulation control options
#=1 --------------------------
#=1
-rtc ) RotationalConstraints=rtc ; shift ; continue ;; #==2 Whether or not to use rotational constraints
-ndlp ) NDLP=true; RotationalConstraints=rtc ; shift ; continue ;; #==2 Whether or not to use NDLP (molecular shaped) PBC
-bt ) BOXTYPE=$2 ; shift 2; continue ;; #==2 Box type to use
-salt ) Salt=$2 ; shift 2; continue ;; #==2 Salt to use (NA,CL)
-conc ) Salinity=$2 ; shift 2; continue ;; #==2 Salt concentration
-sq ) SaltCharge=$2 ; shift 2; continue ;; #==2 Charge of ions from salt (1,-1)
-charge ) CHARGE=$2 ; shift 2; continue ;; #==2
-t ) Temperature=$2 ; shift 2; continue ;; #==1 Temperature
-ttau ) Tau_T=$2 ; shift 2; continue ;; #==2 Temperature coupling constant
-p ) Pressure=$2 ; shift 2; continue ;; #==1 Pressure
-ptau ) Tau_P=$2 ; shift 2; continue ;; #==2 Pressure coupling constant
-d ) PBCDIST=$2 ; shift 2; continue ;; #==1 Distance between images over PBC
-prfc ) PosreFC=$2 ; shift 2; continue ;; #==2 Position restraint force constant
-time ) TIME=$2 ; shift 2; continue ;; #==1 Time to run production simulation (ns)
-at ) AT=$2 ; shift 2; continue ;; #==1 Output resolution
-em ) EMSteps=$2 ; shift 2; continue ;; #==2 Number of steps in EM
-equil ) EquilTime=$2 ; shift 2; continue ;; #==2 Equilibration run time
-pre ) PreTime=$2 ; shift 2; continue ;; #==2 Time for preproduction run
-elec ) Electrostatics=$2 ; shift 2; continue ;; #==2 Electrostatics treatment (cutoff/RF/PME)
-ff ) ForceField=$2 ; shift 2; continue ;; #==1 Force field to use
-vsite ) VirtualSites=true ; shift ; continue ;; #==2 Whether or not to use virtual sites
-seed ) SEED=$2 ; shift 2; continue ;; #==2 Seed for random number generator
-solvent ) SolModel=$2 ; shift 2; continue ;; #==2 Solvent model name(s); can be itp file(s)
-solfile ) SolFile=$2 ; shift 2; continue ;; #==2 Solvent (configuration) file
#=1
#=1 Analysis options
#=1 ----------------
#=1
-lie ) LIE=true ; shift ; continue ;; #==2 Whether or not to use LIE setup and analysis
-analysis) ANALYSIS+=($2) ; shift 2; continue ;; #==2 Analysis protocols to run
#=2
#=2 Perform analysis specified (provided it is implemented) *STR: None
#=2 Analysis routines can be added to the end of the script
#=2 They should be tagged to allow being called through the
#=2 command line.
#=2
#=2 Currently available routines:
#=2
#=2 * LIE
#=2 ---
#=2 Extract ligand - environment interaction energies
#=2 for post-hoc LIE calculations. This analysis
#=2 requires running with the option -lie and is then
#=2 selected automatically.
#=1
#=1 Monitor options
#=1 ---------------
#=1
-monall ) MONALL=-monitor ; shift 1; continue ;; #==2 Monitor all steps using control script
-control ) #==2 Simulation monitor script
while [[ -n $2 && $2 != ';' ]]
do
CONTROL="$CONTROL $2"
shift
done
shift 2
echo MONITOR COMMAND: $CONTROL
continue;;
-ctime ) CHECKTIME=$2 ; shift 2; continue ;; #==2 Time for running monitor
#=2
#=2 A control process is either a program, script or command
#=2 that monitors the production run and terminates it
#=2 upon a certain condition, indicated by its exit code.
#=1
#=1 Advanced control options
#=1 ------------------------
#=1
#=2 This program allows specifying options for advanced control of
#=2 program invocation and simulation parameters. These options are
#=2 described below.
#=2
# The first one is the template/dummy for the help system
--mdp-option=value) olevel=2; hlevel=2; USAGE 1; continue;; #==2 Command-line specified simulation parameters
--mdp-* ) MDPOPTS+=(${1#--mdp-}) ; shift ; continue ;;
#=2
#=2 This will add 'option = value' to the MDP file for all simulations
#=2 following energy minimization. MDP options specified on the command line
#=2 take precedence over those specified in an input file (-mdp), which take
#=2 precedence over parameters defined in this script.
#=2 If the option takes multiple arguments, then 'value' should be a
#=2 comma separated list.
#=2 The STEP/STOP controls can be used to set parameters for (pre)production
#=2 simulations selectively.
#=2
#=2 --program-option=value Command-line specified program parameters
--* ) PROGOPTS+=($1) ; shift ; continue ;;
#=2
#=2 This will add "-option value" to the command line of the call to 'program'.
#=2 Note that this does not allow overriding options specified internally.
#=2 Trying to do so will result in an error due to double specification of the
#=2 option. If the option takes multiple arguments, then 'value' should be a
#=2 comma separated list.
#=0
#=0
# All options should be covered above. Anything else raises an error here.
*) BAD_OPTION "$1";;
esac
done
##<< OPTIONS
#--------------------------------------------------------------------
#---GLOBAL PARAMETERS AND STUFF--
#--------------------------------------------------------------------
exec 3>&1 4>&2
[[ -n $MSGFILE ]] && exec 1>$MSGFILE
[[ -n $ERRFILE ]] && exec 2>$ERRFILE
cat << __RUNINFO__
$PROGRAM version $VERSION:
(c)$YEAR $AUTHOR
$AFFILIATION
Now executing...
$CMD
__RUNINFO__
echo $CMD > cmd.log
# Time. To keep track of the remaining run time
START=$(date +%s)
# Set the scratch directory, if any:
# scratch directory, user name, random number
if [[ -n $SCRATCH ]]
then
# The scratch directory can be specified as
# (escaped) variable, like \$TMPDIR. This
# variable will be expanded at runtime.
# That may be handy on clusters, where the
# $TMPDIR is set for every node.
if [[ ${SCRATCH:0:1} == '$' ]]
then
tmp=${SCRATCH:1}
SCRATCH=${!tmp}
fi
# To ensure that there is no further rubbish
# the scratch directory is extended with the
# username, the data and the process ID. There
# the run will be performed.
SCRATCH=$SCRATCH/$(date +%F).$USER.$$
if ! mkdir -p $SCRATCH
then
echo Scratch directory $SCRATCH is not available... exiting
exit
fi
echo $SCRATCH > SCRATCH
fi
#--------------------------------------------------------------------
#---Sed and awk
#--------------------------------------------------------------------
# Awk expression for extracting moleculetype
# - at the line matching 'moleculetype'
# read in the next line
# continue reading next lines until one is not beginning with ;
# print the first field
AWK_MOLTYPE='/moleculetype/{getline; while ($0 ~ /^ *;/) getline; print $1}'
#--------------------------------------------------------------------
#---GROMACS AND RELATED STUFF
#--------------------------------------------------------------------
## 0. Finding programs
dependency_not_found_error()
{
FATAL The required dependency $@ was not found.
}
NDEP=${#DEPENDENCIES[@]}
find_program_function()
{
for ((i=0; i<$NDEP; i++)); do
if [[ ${DEPENDENCIES[$i]} == "$1" ]]
then
progr=${PROGEXEC[$i]}
envvar=${PROGEVAR[$i]}
fi
done
# Check if the program is in the environment
[[ -n $envvar ]] && [[ -f ${!envvar} ]] && echo ${!envvar} && return 0
# Check if the program is in the directory of this script
[[ -f $SDIR/$progr ]] && echo $SDIR/$progr && return 0
# Check if the program is in the PATH
# Python scripts may be available as 'binaries' (martinize/insane)
which $progr 2>/dev/null && return 0
which ${progr%.py} 2>/dev/null && return 0 || return 1
}
## 1. GROMACS ##
load_gromacs
# 2. SQUEEZE/NDLP for minimal-volume simulation.
# - Requires Gromacs with RTC support
# - Requires SQUEEZE executable:
if $NDLP
then
SQUEEZE=$(find_program_function squeeze)
if [[ $? != 0 ]]
then
FATAL "NDLP SETUP REQUESTED, BUT SQUEEZE EXECUTABLE NOT FOUND"
fi
if ! $SQUEEZE -h >/dev/null 2>&1
then
echo
echo "Squeeze was probably compiled for a different version of Gromacs."
FATAL "NDLP SETUP REQUESTED, BUT SQUEEZE EXECUTABLE FAILED"
fi
fi
METHOD=$(cat << __METHOD__
Simulations were performed using GROMACS version $GMXVERSION [1] through the automated workflow $PROGRAM [2].
[1] $GMXREF
[2] $THISREF
__METHOD__
)
#--------------------------------------------------------------------
#---TIMING
#--------------------------------------------------------------------
# Maximum time in seconds
if [[ $MAXH =~ ":" ]]
then
# Format HH:MM:SS
ifs=$IFS; IFS=":"; MAXS=($MAXH); IFS=$ifs
MAXS=$((3600*MAXS[0] + 60*MAXS[1] + MAXS[2]))
else
# Format x.y HH
MAXS=$(awk '{printf "%d\n", $1*3600}' <<< $MAXH )
fi
if (( MAXS > 0 ))
then
UNTIL=$(( $(date +%s) + MAXS ))
echo "# $PROGRAM will run until $(date --date=@$UNTIL), or until run has finished"
else
echo "# No maximum runtime set. Will run until finished or until crash."
fi
# This variable will be reset to the time needed for the last run run
# A following run will usually take longer.
LASTRUN=0
#--------------------------------------------------------------------
#---INITIAL CHECKS AND LOGGING
#--------------------------------------------------------------------
## 1. Expand options that can take multiple, comma-separated values
# This concerns options for equilibration, such as temperature,
# pressure and position restraint force constants. These will
# be used to set up cycles of equilibration. Position restraints Fc
# and temperature are cycled together in STEP 6 (NVT), followed by
# pressure equilibration in STEP 7 (NPT).
# Store the Internal Field Separator (IFS)
ifs=$IFS
IFS=","
Temperature=($Temperature)
Tau_T=($Tau_T)
Pressure=($Pressure)
Tau_P=($Tau_P)
PosreFC=($PosreFC)
Salt=($Salt)
SaltCharge=($SaltCharge)
# Restore the field separator
IFS=$ifs
## 2. Echo options for direct modulation of program calls
MSG="Program options specified on command line:"
echo_additional_options ${PROGOPTS[@]}
MSG="MDP options specified on command line (note how flexible!):"
echo_additional_options ${MDPOPTS[@]}
#--------------------------------------------------------------------
#---WARMING UP VARIABLE GYMNASTICS
#--------------------------------------------------------------------
# Parse input file names - expand to full path
pdb=${fnIN##*/} # Filename
dirn=$(cd ${fnIN%${fnIN##*/}}./; pwd) # Directory
ext=${pdb##*.} # Extension
[[ "$ext" == "tpr" && -z $TPR ]] && TPR=$pdb # Got a run input file as input file
[[ -n $fnIN ]] && fnIN=$dirn/$pdb # Input file with full path
topdir=$(cd ${TOP%${TOP##*/}}./; pwd) # Full path to directory of topology file
[[ -n $TOP ]] && TOP=$topdir/${TOP##*/} # Topology file with full path
# Set the name. If one is given, that one is used. Otherwise,
# if an input structure is given, the name is derived from it.
# If there is no input structure, but there is a ligand, then
# "ligand" is used. If nothing is set, then just use
# "wallace"
base=${pdb%.*} # Set basename
[[ -n $NAME ]] && base=$NAME # Override base name if name is given
[[ -z $base && -n $LIGANDS ]] && base=ligand # Still unset.., set to "ligand" if we have a ligand
[[ -z $base ]] && base=wallace # Okay, then just go with this
# Change working directory, creating one if necessary
[[ ! -d $DIR ]] && mkdir -p $DIR
[[ -n $TPR && ! -f $DIR/${TPR##*/} ]] && cp $fnIN $DIR
cd $DIR
DIR=$(pwd)
echo
echo "# $( date +"%a %b %d %H:%M:%S %Y" ) "
echo "# Command:"
echo $CMD
echo
[[ -n $fnIN ]] && echo "# Input structure: $fnIN"
[[ -n $TOP ]] && echo "# Input topology: $TOP"
echo "# Base name: $base"
echo "# Source directory: $dirn"
echo "# Command issued from: $BDIR"
echo "# Run directory: $DIR"
[[ -n $SCRATCH ]] && echo "# Scratch directory: $SCRATCH"
# Copy topology stuff if we specify a topology
[[ -n $TOP ]] && cp $topdir/*itp ./
# Set a trap for signals
#function archive ()
#{
# if [[ -n $ARCHIVE ]]
# then
# tar cfz $DIR/$ARCHIVE.tmp.tgz $(ls | grep -v $ARCHIVE)
# mv $DIR/$ARCHIVE.tmp.tgz $DIR/$ARCHIVE
# fi
# exit
#}
#trap "archive" 2 9 15
# Set the forcefield tag
case $ForceField in
gromos*) ForceFieldFamily=gromos;;
amber*) ForceFieldFamily=amber;;
charmm*) ForceFieldFamily=charmm;;
opls*) ForceFieldFamily=opls;;
esac
#--------------------------------------------------------------------
#---SET THE SOLVENT MODEL
#--------------------------------------------------------------------
# If it is not specified, the solvent is the water model
# most appropriate for the force field. This can be overriden
# by explicitly stating an alternative water model or by
# providing a solvent model topology.
# The solvent model needs to be accompanied by a solvent
# structure file, which is to be used for the solvation.
# For the water models these files are available in the
# distribution.
if [[ -z $SolModel ]]
then
# Get the right solvent model for the force field selected
for ((i=0; i<${#ForceFieldFamilies[@]}; i++))
do
if [[ $ForceFieldFamily == ${ForceFieldFamilies[$i]} ]]
then
WaterModel=${ForceFieldSolvents[$i]}
SolModel=$WaterModel
SolventTopology=$ForceField.ff/$WaterModel.itp
[[ -z $SolFile ]] && SolFile=${SolventFiles[$i]}
fi
done
else
# If the name is known (in the ForceFieldSolvents list)
# use the corresponding file
found=false
# Get the right solvent model for the force field selected
for ((i=0; i<${#ForceFieldFamilies[@]}; i++))
do
if [[ $SolModel == ${ForceFieldSolvents[$i]} ]]
then
WaterModel=$SolModel
SolventTopology=$WaterModel.itp
[[ -z $SolFile ]] && SolFile=${SolventFiles[$i]}
found=true
fi
done
if ! $found
then
# Solvent model was specified, but not found
if [[ -f $SolModel ]]
then
SolName=$(awk "$AWK_MOLTYPE" $SolModel)
SolventTopology=$SolModel
NOTE Using solvent model $SolName from $SolModel with file $SolFile
else
# Don't know what to do...
FATAL The solvent model should be a registered name or provided as topology
fi
fi
fi
#--------------------------------------------------------------------
#---STEPPING STUFF
#--------------------------------------------------------------------
# Set the starting/stopping step
# For LIE calculations also execute the analysis part
$LIE && ANALYSIS+=(LIE)
# If we have a TPR as input file run only production
[[ -n $TPR ]] && STEP=PRODUCTION
# If there is analysis to be done, set the stopping step accordingly
[[ -n $ANALYSIS ]] && [[ PRODUCTION == ${STOP}* || END == ${STOP}* ]] && STOP=ANALYSIS
# Step up to the step-in step
for ((i=0; i<${#STEPS[@]}; i++)); do [[ ${STEPS[$i]} == ${STEP}* ]] && STEP=$i && break; done
# Step up to the stop-step: stop if the step stepped up to is the step to stop at
for ((i=0; i<${#STEPS[@]}; i++)); do [[ ${STEPS[$i]} == ${STOP}* ]] && STOP=$i && break; done
echo "# Starting at step $STEP: ${STEPS[$STEP]}"
echo "# Stopping at step $STOP: ${STEPS[$STOP]}"
# Remove flags from previous runs
[[ -e DONE ]] && rm DONE
[[ -e ERROR ]] && echo "# Found ERROR flag, probably from previous run. Trying again." && rm ERROR
#--------------------------------------------------------------------
#---INFORMATIVE OUTPUT--
#--------------------------------------------------------------------
echo "# Starting MD protocol for $fnIN"
if [[ -z $TPR ]]
then
echo "# Using $ForceFieldFamily force field $ForceField with $WaterModel water model"
[[ -n $Electrostatics ]] \
&& echo "# Using $Electrostatics for treatment of long range coulomb interactions" \
|| echo "# Inferring electrostatics treatment from force field family (check mdp files)"
$VirtualSites \
&& echo "# Using virtual sites" \
|| echo "# Not using virtual sites"
$NDLP \
&& echo "# Simulations will be performed using a near-densest lattice packing unit cell" \
|| echo "# Simulations will be performed in a rhombic dodecahedron unit cell"
fi
#--------------------------------------------------------------------
#---SIMULATION PARAMETERS--
#--------------------------------------------------------------------
## OT N ## For every parameter not defined the default is used
## NOTE ## This is probably fine for equilibration, but check the defaults to be sure
## E OT ## The list as is was set up for gromacs 4.5 and 5.1
init_mdp_parameters
read_mdp_file
read_mdp_options
#--------------------------------------------------------------------
#--------------------------------------------------------------------
#---SUBROUTINES--
#--------------------------------------------------------------------
ERROR=0
function pdb2gmx_error()
{
$SED -n -e '/^Fatal error/,/^----/p' $1
exit_error "Converting structure with pdb2gmx failed."
}
function getCharge ()
{
# Routine for extracting the charge from a TPR file
# a. Extract and set the active molecule name
AWK_TPR_MOLNAME='/^ *name=/{sub(".*=","",$0); T=$0}'
# b. Extract the moleculetype name
AWK_TPR_MOLTYPE='/^ *moltype *=/{M=$4}'
# c. Extract the number of instances of the current moleculetype
# and initialize charge for moleculetype
AWK_TPR_MOLNUM='/#molecules *=/{N[M]=$3; C[M]=0}'
# d. Extract and accumulate charge
AWK_TPR_CHARGE='/^ *atom.*q=/{sub(".*q=","",$0); sub(",.*","",$0); C[T]+=$0}'
# e. Finalize
AWK_TPR_END='END{S=0; for (i in C) {S+=N[i]*C[i]}; if (S<0) S-=0.5; else S+=0.5; printf "%d\n", S}'
dump=$(which gmxdump)
[[ -n ${dump} ]] || dump=$(which gmx)
[[ -n ${dump} ]] || FATAL gmx and gmxdump not found, cannot determine charge of system from tpr file
${dump} -s $1 2>&1 | awk "$AWK_TPR_MOLNAME $AWK_TPR_MOLTYPE $AWK_TPR_MOLNUM $AWK_TPR_CHARGE $AWK_TPR_END"
}
# Routine for generating a simple index file
function INDEX()
{
[[ -n $2 ]] && fn=$2 || fn=basic.ndx
exec 6>&1 && exec >$fn
fmt="%5d %5d %5d %5d %5d %5d %5d %5d %5d %5d"
# Total number of atoms
N=$(awk '{getline; print; exit}' $1)
echo "[ System ]"
printf "$fmt\n" `SEQ 1 $N` | $SED 's/ 0//g'
# Solvent atoms (including ions, etc, listed after 'SOL')
SOL=$(( $($SED -n '/'$SolName'/{=;q;}' $1) - 2 ))
echo "[ Solvent ]"
printf "$fmt\n" `SEQ $SOL $N` | $SED 's/ 0//g'
# Base system: solute and membrane, if present
echo "[ Base ]"
printf "$fmt\n" `SEQ 1 $((SOL - 1))` | $SED 's/ 0//g'
# Membrane, if any
MEMBRANE=$($SED -n '/\(POP\|DPP\|DMP\|DOP\|PPC\)/{=;q;}' $1)
if [[ -n $MEMBRANE ]]
then
echo '[ Membrane ]'
printf "$fmt\n" `SEQ $MEMBRANE $((SOL - 1))` | $SED 's/ 0//g'
else
MEMBRANE=SOL
fi
echo '[ Solute ]'
printf "$fmt\n" `SEQ 1 $((MEMBRANE - 1))` | $SED 's/ 0//g'
exec 1>&6 6>&-
return 0
}
# Always ECHO the first line
NOW=$STEP
#--------------------------------------------------------------------
SHOUT "---= THIS IS WHERE WE really START =---"
#--------------------------------------------------------------------
NOW=0
# We may be working in two directories:
# 1. The RUN directory $DIR
# 2. The SCRATCH directory $SCRATCH
# If we are working in the scratch directory, we
# copy back files after every step to keep the
# run directory in sync.
# What we copy back depends on the value of $KEEP.
# If that is 'true' everything is copied.
# We always go to the scratch directory if we have one.
[[ -n $SCRATCH ]] && cd $SCRATCH && echo $DIR > SOURCE
# If we do not have an input file, definitely skip the first step
# This may happen if we set up a ligand-in-solvent simulation, or
# just a box of solvent, or maybe a membrane...
[[ $STEP == $NOW && -z $fnIN ]] && : $((STEP++)) && echo "# Skipping step 1A: No structure to generate topology for."
# If we are at this step, but have a top file already, increase the STEP
if [[ $STEP == $NOW && -n $TOP && -n $fnIN ]]
then
echo "# Topology file ${TOP##*/} provided for structure ${fnIN##*/}. Skipping step 1A."
# If the accompanying GRO file does not exist, convert the PDB file
[[ -e $base.gro ]] || ${GMX}editconf -f $fnIN -o $base.gro &>/dev/null
: $(( STEP++ ))
fi
# If we are still at this step, do some checking of input and
# maybe fetch a file from the PDB.
if [[ $STEP == $NOW ]]
then
# Check the input file
if [[ ! -f $dirn/$pdb ]]
then
[[ -f $dirn/$pdb.pdb ]] && pdb=$pdb.pdb
[[ -f $dirn/$pdb.pdb.gz ]] && pdb=$pdb.pdb.gz
[[ -f $dirn/$pdb.gz ]] && pdb=$pdb.gz
fi
PDB=$dirn/$pdb
if [[ -f $PDB ]]
then
[[ -n $SCRATCH ]] && cp $PDB .
elif [[ -n $FETCH ]]
then
fetch_structure $pdb $FETCH
[[ -n $SCRATCH ]] && cp $pdb $DIR
else
echo "# Input file $dirn/$pdb specified, but not found."
echo "# For automated download of a PDB file, add -fetch to the command line."
exit 1
fi