forked from marrink-lab/gromit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
martinate.sh
executable file
·1818 lines (1453 loc) · 58.4 KB
/
martinate.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=martinate.sh
VERSION=0.99
VERSTAG=devel-180428-1200-TAW
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
(copied from gromit.sh)
__NOTES_FOR_USE_AND_EDITING__
DESCRIPTION=$(cat << __DESCRIPTION__
$PROGRAM $VERSION is a versatile wrapper built around
GROMACS, insane, and martinize, for setting up and running
MARTINI COARSE GRAIN molecular dynamics simulations of solvents,
membranes, proteins and/or nucleic acids in any combination.
It is built to allow automated processing of membrane proteins,
with full control of membrane composition. If no input file is provided,
only a membrane and/or solvent is built.
Options given that do not match an option in this script are passed to
martinize.py.
The script contains a complete and flexible workflow, consisting of the
following steps:
1. Generate topology from input structure
A. Generate atomistic topology (AA)
B. Generate MARTINI CG/multiscale topology (CG)
2. Solvation and adding ions (SOLVENT)
5. Energy minimization (EM)
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 CG molecular dynamics simulations of proteins
and/or nucleic acids in aqueous solution with or without a membrane.
It follows the steps that are commonly taken in MD tutorials
(e.g. those at http://cgmartini.nl).
This program is designed to enable high-throughput processing of CG 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.
## -- 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_martinate.sh
source "${SRCDIR}"/_mdp.sh
source "${SRCDIR}"/_mdrunner.sh
source "${SRCDIR}"/_pdb.sh
source "${SRCDIR}"/_martinate_index.sh
source "${SRCDIR}"/_martinate_multiscale.sh
source "${SRCDIR}"/_martinate_daft.sh
source "${SRCDIR}"/_gmx.sh
trap "archive" 2 9 15 # function archive in _functions.sh
# 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 (martinate) is located
# 3. the PATH
DEPENDENCIES=( dssp gmxrc martinize insane liptop squeeze)
PROGEXEC=( dssp GMXRC martinize.py insane $FFDIR/liptop.py squeeze)
PROGEVAR=( DSSP GMXRC)
# Run control
MONALL= # Monitor all steps
CONTROL=
CHECKTIME=300 # Run control every five minutes
# Stepping stuff
STEPS=(AA CG SOLVENT EM 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=AA
STOP=PRODUCTION
# MARTINI Force field parameters
MARTINI=martini22
DRY=
FFITP=
FFTAG=v2.0
USRITP=() # Additional topologies to include
# Solvents
# NOTE: with standard Martini water, exclude AA/CG interactions
SOLVENTS=( dry polarizable PW pw bmw W simple standard martini )
SOLNAMES=( NO PW PW PW BMW W W W W )
SOLTYPE=( NO polarizable polarizable polarizable polarizable plain plain plain plain )
EPSR_CG=( 15 2.50 2.50 2.50 1.30 15 15 15 15 ) # CG-CG dielectric constant
EPSR_AA=( 0 1.45 1.45 1.45 1.00 0 0 0 0 ) # CG-AA dielectric constant
SOLVFF=( dry p p p bmw ) # MARTINI subversion and solvent tag
# Options:
# Downstream programs:
# - membrane and periodic boundary conditions (insane options):
INSANE=()
# - martinizing
MARTINIZE=()
# This program:
# - protein:
PDB=
TOP=
NDX=
NOHETATM=true
VirtualSites=false
# - multiscaling
ForceField=gromos45a3
MULTI=()
ALL=false
M=false
NCH=0
SOL=
HybridIons=false
# - nonbonded interactions
EPSR=
EPSRF=78
LJDP=6
LJRP=12
LJSW=-1
RC=1.4
# - 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)
DAFT=
# - system setup
Salinity=0.1536 # Salt concentration of solvent
# - 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.5 # Output frequency for positions, energy and log (ns)
DELT=0.020 # Time step in picoseconds
EMSTEPS=500 # Number of steps for EM
Temperature=310 # Degree Kelvin
Tau_T=0.1 # ps
Pressure=1 # Bar
Tau_P=1.0 # ps
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)
hlevel=0
olevel=0
# 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
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 help
--help ) USAGE 0 ; exit 0 ;; #==1 Display help
-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 ) PDB=$2 ; shift 2; continue ;; #==0 Input PDB file
-g ) MSGFILE=$2 ; shift 2; continue ;; #==9 Standard output log file (default: /dev/stdout)
-e ) ERRFILE=$2 ; shift 2; continue ;; #==9 Standard error log file (default: /dev/stderr)
-name ) NAME=$2 ; shift 2; continue ;; #==9 Name of project
-top ) TOP=$2 ; shift 2; continue ;; #==1 Input topology file
-ndx ) NDX=$2 ; shift 2; continue ;; #==1 Input index file
-mdp ) MDP=$2 ; shift 2; continue ;; #==9 MDP (simulation parameter) file
-scratch ) SCRATCH=$2 ; shift 2; continue ;; #==9 Scratch directory to perform simulation
-fetch ) FETCH=$2 ; shift 2; continue ;; #==9 Database to fetch input structure from
-hetatm ) NOHETATM=false ; shift 1; continue ;; #==1 Keep 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 stop protocol
-keep ) KEEP=true ; shift 1; continue ;; #==2 Whether or not to keep intermediate data
-dir ) DIR=$2 ; shift 2; continue ;; #==2 Directory for running simulation
-np ) NP=$2 ; shift 2; continue ;; #==1 Number of cores (processes) to use
-maxh ) MAXH=$2 ; shift 2; continue ;; #==2 Maximum run time
-force ) FORCE=true ; shift 1; continue ;; #==2 Whether or not to force redoing parts already run
-noexec ) EXEC=echo ; shift 1; continue ;; #==2 Whether or not to actually execute the commands
#=1
#=1 Forcefield control options
#=1 --------------------------
#=1
-cg ) MARTINI=$2 ; shift 2; continue ;; #==1 Coarse grain force field
-sol ) SOL=$2 ; shift 2; continue ;; #==1 Solvent type to use
-ffitp ) FFITP=$2 ; shift 2; continue ;; #==2 Coarse-grain force field definition
-ffdir ) FFDIR=$2 ; shift 2; continue ;; #==2 Directory for force field files
-fftag ) FFTAG=$2 ; shift 2; continue ;; #==2 Tag for force field files (v3.0 -> martini_v3.0_lipids.itp)
-itp ) USRITP+=($2) ; shift 2; continue ;; #==2 User-provided ITP file
-dry ) DRY=$2 ; shift 2; continue ;; #==2 Use dry martini from file definition
#=1
#=1 Simulation control options
#=1 --------------------------
#=1
-T ) Temperature=$2 ; shift 2; continue ;; #==1 Temperature
-P ) Pressure=$2 ; shift 2; continue ;; #==1 Pressure
-salt ) Salinity=$2 ; shift 2; continue ;; #==1 Salt concentration
-dt ) DELT=$2 ; shift 2; continue ;; #==2 Integration time step
-time ) TIME=$2 ; shift 2; continue ;; #==1 Production simulation time
-at ) AT=$2 ; shift 2; continue ;; #==1 Output sampling frequency
-em ) EMSTEPS=$2 ; shift 2; continue ;; #==2 Number of steps for EM
# -gmxrc ) GMXRC=$2 ; shift 2; continue ;;
# -dssp ) DSSP=$2 ; shift 2; continue ;;
-rtc ) RotationalConstraints=rtc ; shift ; continue ;; #==2 Whether or not to use rotational constraints
#=2
#=2 Multiscale options
#=2 ------------------
#=2
-m ) MULTI[$((NCH++))]=$2; M=true ; shift 2; continue ;; #==2 Chains for multiscaling
-M ) ALL=true; M=true ; shift 1; continue ;; #==2 Multiscale all chains
-ff ) ForceField=$2 ; shift 2; continue ;; #==2 Atomistic force field for multiscaling
-vsite ) VirtualSites=true ; shift 1; continue ;; #==2 Use virtual sites in multiscaling
-epsr ) EPSR=$2 ; shift 2; continue ;; #==2 Dielectric constant of vacuum
-epsrf ) EPSRF=$2 ; shift 2; continue ;; #==2 Dielectric constant of Reaction-Field
-ljdp ) LJDP=$2 ; shift 2; continue ;; #==2 Lennard-Jones dispersion
-ljrp ) LJRP=$2 ; shift 2; continue ;; #==2 Lennard-Jones repulsion
-ljsw ) LJSW=$2 ; shift 2; continue ;; #==2 Lennard-Jones switch radius
-rc ) RC=$2 ; shift 2; continue ;; #==2 Cut-off for non-bonded terms
#=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.
#=2
#=2 Protocol options
#=2 ----------------
#=2
-daft ) DAFT=$2; NDX=$2 ; shift 2; continue ;; #==2 Run martinate in DAFT pipeline
#=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
# Options for downstream programs
# If the options are given on the command line, they are expanded and each
# option will be formatted as --program-opt=val
--martinize-option=value) olevel=2; hlevel=2; USAGE 1; continue;; #==2 Parameters for martinize
--martinize-*) MARTINIZE+=(${1#--martinize}) ; shift ; continue;;
--insane-option=value) olevel=2; hlevel=2; USAGE 1; continue;; #==2 Parameters for insane
--insane-*) INSANE+=(${1#--insane}) ; shift ; continue;;
# If the options are passed by another program, they will be formatted like
# --program{-opt1=val1,-opt2=val2\,val3}
# In this case the option needs to be parsed explicitly:
--martinize*) MARTINIZE+=($(readOptList $1)) ; shift ; continue;;
--insane*) INSANE+=($(readOptList $1)) ; shift ; continue;;
# Other program-specific options
--*) PROGOPTS[${#PROGOPTS[@]}]=$1 ; shift 1; continue ;;
#=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)
# START/STOP FLOW CONTROL
for ((i=0; i<${#STEPS[@]}; i++)); do [[ ${STEPS[$i]} == ${STEP}* ]] && STEP=$i && break; done
for ((i=0; i<${#STEPS[@]}; i++)); do [[ ${STEPS[$i]} == ${STOP}* ]] && STOP=$i && break; done
# 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. DSSP ##
# Search the DSSP binary, from environment, from path, or guess
# Only required if we have an input file
SOLSTEP=$(get_step_fun CG)
if [[ $STEP -le $SOLSTEP && $STOP -ge $SOLSTEP ]]
then
echo -n '# Checking DSSP binary (for martinizing proteins)... '
DSSP=$(find_program_function dssp)
if [[ $? == 1 ]]
then
warn="DSSP binary not found - Will martinize without secondary structure :S"
store_warning_fun "$warn"
else
echo "$DSSP"
MARTINIZE+=(-dssp=$DSSP)
fi
fi
## 4. Echo mdp/program options specified on the command line
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[@]}
## 5. Locate insane if STEP lies before SOLVENT and STOP lies after.
SOLSTEP=$(get_step_fun SOLVENT)
if [[ $STEP -le $SOLSTEP && $STOP -ge $SOLSTEP ]]
then
INSA=$(find_program_function insane)
if [[ $? != 0 ]]
then
STEP=$NOW
FATAL "Dependency (insane) required for building solvent/membrane, but not found."
fi
fi
#--------------------------------------------------------------------
#---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
#--------------------------------------------------------------------
#---WARMING UP VARIABLE GYMNASTICS
#--------------------------------------------------------------------
## 2. WORKING DIRECTORY AND SOURCE DIRECTORY ##
# SRCDIR=$(pwd)
[[ ! -d $DIR ]] && mkdir -p $DIR; pushd $DIR >/dev/null
## 3. START/STOP FLOW CONTROL ##
NOW=$STEP
echo "# Will run from step ${STEPS[$STEP]} until ${STEPS[$STOP]}"
## 4. SET THE SOLVENT ##
# Select the solvent to use
# Default solvent is martini water
if [[ -z $SOL ]]
then
if [[ $MARTINI == *p ]]
then
SOL=polarizable
fi
fi
SID=; for ((i=0; i<${#SOLVENTS[@]}; i++)); do [[ ${SOLVENTS[$i]} == ${SOL}* ]] && SID=$i; done
# Override if option -dry is given... Serving Dry Martini
[[ -n $DRY ]] && SID=0
# Check whether we found a matching solvent model
[[ -z $SID ]] && echo Unknown solvent model \"$SOL\" specified. && exit
# Check whether the solvent type is polarizable
[[ ${SOLTYPE[$SID]} == polarizable ]] && POLARIZABLE=true || POLARIZABLE=false
## 5. FORCE FIELD ##
# a. ATOMISTIC
# Set pointers to ffnonbonded.itp and ffbonded.itp if we do multiscaling
$M && ffnb=$GMXLIB/$ForceField.ff/ffnonbonded.itp || ffnb=
$M && ffbn=$GMXLIB/$ForceField.ff/ffbonded.itp || ffbn=
# b. COARSE GRAINED (MULTISCALE) ITP
# i. If a forcefield ITP is given, use that
# ii. If a forcefield ITP generating script is available use that
# iii. If $FFDIR contains a suitable forcefield ITP use that (and warn)
# iv. Raise an error
FFMARTINIPY=
if [[ -n $FFITP ]]
then
# i.
cp $FFITP ./martini.itp
else
FFMARTINIPY=$FFDIR/${MARTINI}${SOLVFF[$SID]}.py
if [[ ! -f $FFMARTINIPY ]]
then
if [[ -f $FFDIR/${MARTINI}.py ]]
then
# If martini22p was specified in stead of martini22 with PW,
# then we end up here, setting the script to martini22p.py
FFMARTINIPY=$FFDIR/${MARTINI}.py
fi
fi
if [[ -f $FFMARTINIPY ]]
then
# ii.
if [[ -n $DRY ]]
then
$FFMARTINIPY "$DRY" > martini.itp
else
$FFMARTINIPY $ffnb $ffbn > martini.itp
# UPDATE martini.itp FOR DUMMIES
# Replace the #include statement for ff_dum.itp for atomistic force fields by the contents of it
$M && $SED -i -e "/#include \"ff_dum.itp\"/r$GMXLIB/$ForceField.ff/ff_dum.itp" -e "/#include \"ff_dum.itp\"/d" martini.itp
fi
else
# iii.
# Check if an FF include exists in $FFDIR
FFITP=$(
for itp in $FFDIR/*itp
do
tags=$(grep '\[' $itp)
[[ "$tags" =~ defaults && "$tags" =~ atomtypes && "$tags" =~ nonbond_params ]] && echo $itp
done
)
if [[ -n $FFITP ]]
then
NOTE Found force field include file $FFITP
cp $FFITP martini.itp
fi
fi
fi
if [[ ! -f martini.itp ]]
then
FATAL Could not find forcefield itp file or generating script.
fi
# TODO - get something smarter for the FFTAG
if [[ "$MARTINI" =~ "martini3" ]]
then
FFTAG=v3.0
fi
## 6. ELECTROSTATICS AND TABLES ##
EPSR_CG=${EPSR_CG[$SID]}
EPSR_AA=${EPSR:-${EPSR_AA[$SID]}}
$M && TABLES=-tables || TABLES=
#--------------------------------------------------------------------
#---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
# Always ECHO the first line
NOW=$STEP
#--------------------------------------------------------------------
SHOUT "---= THIS IS WHERE WE START =---"
#--------------------------------------------------------------------
#--------------------------------------------------------------------
#---INPUT CHECKING, SPLITTING, TRIMMING, GROOMING
#--------------------------------------------------------------------
if [[ -n $PDB ]]
then
# If the input file is not found, check whether it was given without extension.
# If that is not the case, then fetch the file from the PDB repository.
if [[ ! -f $PDB ]]
then
PDB=$PDB.pdb
if [[ ! -f $PDB ]]
then
# Try fetching it from the PDB
pdb=$(tr [A-Z] [a-z] <<< ${PDB%.pdb})
fetch_structure $pdb $FETCH
[[ -n $SCRATCH ]] && cp $pdb.pdb $DIR
fi
fi
# If the input file is missing now, raise an errorr
if [[ ! -f $PDB ]]
then
echo Input file $PDB not found and fetching from PDB server failed.
exit 1
fi
# Check whether the input file is here or in another directory.
# In the latter case, copy it here
[[ $PDB == ${PDB##*/} || $PDB == ./${PDB##*/} ]] || cp $PDB .
pdb=${PDB##*/} # Filename
base=${pdb%.*} # Basename
ext=${pdb##*.} # Extension
dirn=${PDB%$pdb} # Directory
[[ $dirn ]] || dirn="."
dirn=`cd $dirn && pwd` # Full path to input file directory
if [ $dirn != `pwd` ]
then
NOTE "The run is performed here (`pwd`), while the input file is elsewhere ($dirn)."
fi
if [[ -z $TOP && $ext == "pdb" ]]
then
if $NOHETATM -a $(grep -q HETATM $PDB)
then
NOTE Removing HETATM entries from PDB file $PDB
$SED -i'' -e "/^HETATM/d" "$PDB"
fi
# Extract a list of chains from PDB file
CHAINS=( $(grep '^\(ATOM\|HETATM\)' $PDB | cut -b 22 | uniq) )
# Unpack lists of chains to multiscale separated by commas
MULTI=( $(for i in ${MULTI[@]}; do echo ${i//,/ }; done) )
# Residues defined in martinize.py
AA=(ALA CYS ASP GLU PHE GLU HIS ILE LYS LEU MET ASN PRO GLN ARG SER THR VAL TRP TYR)
# Sed query for residues (separated by \|):
SED_AA=$($SED 's/ \+/\\\|/g' <<< ${AA[@]})
# ATOM selection (martinizable residues)
ATOM='/^\(ATOM \|HETATM\)/{/.\{17\} *'$SED_AA' */p;}'
# HETATM selection (non-martinizable residues)
HETATM='/^\(ATOM \|HETATM\)/{/.\{17\} *'$SED_AA' */!p;}'
# Split the pdb file in stuff that can be processed with pdb2gmx
# and stuff that cannot be processed with it
# Extract the names of building blocks defined in the rtp file
# of the force field used. These blocks are defined as '[ ALA ]',
# so we match a name in square brackets at the start of a line.
# The name is appended to the hold space.
RTPENTRIES='/^\[ *\(...\).*\].*/{s//\1/;H;}'
# At the end of the list, the building block names are reformatted
# to make a regular expression string matching each word. First,
# the hold space is swapped with the pattern space, the first bit is
# removed and then all embedded newlines are replaced by '\|'
FORMAT='${x;s/\n...\n//;s/\n/\\\|/g;p;}'
# Finally sed is called processing all rtp files of the force field
DEF=$($SED -n -e "$RTPENTRIES" -e "$FORMAT" $GMXLIB/$ForceField.ff/*.rtp)
# Now we can split the input PDB file into a processable and a non-processable part
echo "# Defined building blocks in $ForceField RTP files:"
echo "# $DEF"
#sed -e '/^\(TER\|MODEL\|ENDMDL\)/p' -e '/^\(ATOM \|HETATM\)/{/.\{17\} *\('$DEF'\) */p;}' $dirn/$base.pdb > $base-def.pdb
#sed -e '/^\(TER\|MODEL\|ENDMDL\)/p' -e '/^\(ATOM \|HETATM\)/{/.\{17\} *\('$DEF'\) */!p;}' $dirn/$base.pdb > $base-ndef.pdb
fi
else
base=
fi
if [[ $PBC == retain && -n $PDB ]]
then
PBC="-pbc keep"
else
PBC="-pbc $PBC"
fi
echo "# Done checking"
echo "# Done gymnastics"
NOW=0
if [[ -n $DAFT ]] && ($ALL || [[ -n $MULTI ]])
then
echo "Currently, DAFTly splitting molecules in energy groups and running multiscaled is not possible."
echo "Will run using default multiscale splitting."
# Simply setting DAFT to false should disable any attempt to do splitting
DAFT=
fi
# Set the basename to 'membrane' if no input structure is given
[[ -n $PDB ]] || base=membrane
#---------------------------------------------------------------------
SHOUT "---STEP 1A: GENERATE ATOMISTIC STRUCTURE AND TOPOLOGY"
#---------------------------------------------------------------------
# Skip the atomistic topology if we do not multiscale
if ! $M && [[ $STEP == $NOW ]]
then
echo "Skipping step... (not multiscaling)"
: $((STEP++))
fi
# Skip this step if we run DAFT
if [[ -n $DAFT ]] && [[ $STEP == $NOW ]]
then
echo DAFT run. Skipping step.
: $((STEP++))
fi
if [[ $STEP == $NOW ]]
then
# Output for this section:
OUT=$base-aa.pdb
TOP=$base-aa.top
LOG=01-TOPOLOGY-AA.log
OUTPUT=($OUT $TOP)
# Delete existing output if we force this step
$FORCE && rm ${OUTPUT[@]}
if $(all_exist ${OUTPUT[@]})
then
echo Found $TOP and $OUT. Skipping structure/topology building with pdb2gmx...
: $((STEP++))
fi
fi
MDPMS=
ForceFieldAA=
if [[ $STEP == $NOW ]]
then
# I. Set the mdp tag
MDPMS=ms
# II. Atomistic force field for multiscaling
# 1. List force fields available
AAFF=($(ls -d $GMXLIB/*.ff | $SED 's#.*/\(.*\)\.ff#\1#'))
# 2. Try complete matching
for i in ${AAFF[@]}; do [[ "$i" == "$ForceField" ]] && ForceFieldAA=$i; done
# 3. Try partial matching if no complete match was found
match=0
[[ -n $ForceFieldAA ]] || for i in ${AAFF[@]}; do [[ $i =~ $ForceField ]] && ForceFieldAA=$i && : $(( match++ )); done
if [[ $match -eq 0 && -z $ForceFieldAA ]]
then
echo No matching atomistic forcefield found for $ForceField... Bailing out.
exit 1
elif [[ $match -gt 1 ]]
then
echo Ambiguous selection for atomistic force field... Bailing out.
exit 1
fi
# III. Interaction tables
TABLE="${SRCDIR}"/_table.py
# epsilon_r epsilon_rf cutoff LJ_dispersion LJ_repulsion LJ_cutoff LJ_switch
$TABLE $EPSR_CG $EPSRF $RC $LJDP $LJRP 1.2 0.9 > table.xvg