-
Notifications
You must be signed in to change notification settings - Fork 52
/
setup.py
1354 lines (1169 loc) · 47.7 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
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
# GromacsWrapper: setup.py
"""
:mod:`gromacs.setup` -- Setting up a Gromacs MD run
===================================================
Individual steps such as solvating a structure or energy minimization
are set up in individual directories. For energy minimization one
should supply appropriate mdp run input files; otherwise example
templates are used.
.. warning::
You **must** check all simulation parameters for yourself. Do not rely on
any defaults provided here. The scripts provided here are provided under the
assumption that you know what you are doing and you just want to automate
the boring parts of the process.
User functions
--------------
The individual steps of setting up a simple MD simulation are broken down in a
sequence of functions that depend on the previous step(s):
:func:`topology`
generate initial topology file (limited functionality, might require
manual setup)
:func:`solvate`
solvate globular protein and add ions to neutralize
:func:`energy_minimize`
set up energy minimization and run it (using ``mdrun_d``)
:func:`em_schedule`
set up and run multiple energy minimizations one after another (as an
alternative to the simple single energy minimization provided by
:func:`energy_minimize`)
:func:`MD_restrained`
set up restrained MD
:func:`MD`
set up equilibrium MD
Each function uses its own working directory (set with the ``dirname`` keyword
argument, but it should be safe and convenient to use the defaults). Other
arguments assume the default locations so typically not much should have to be
set manually.
One can supply non-standard itp files in the topology directory. In
some cases one does not use the :func:`topology` function at all but
sets up the topology manually. In this case it is safest to call the
topology directory ``top`` and make sure that it contains all relevant
top, itp, and pdb files.
Example
-------
Run a single protein in a dodecahedral box of SPC water molecules and
use the GROMOS96 G43a1 force field. We start with the structure in
``protein.pdb``::
from gromacs.setup import *
f1 = topology(protein='MyProtein', struct='protein.pdb', ff='G43a1', water='spc', force=True, ignh=True)
Each function returns "interesting" new files in a dictionary in such
a away that it can often be used as input for the next function in the
chain (although in most cases one can get away with the defaults of
the keyword arguments)::
f2 = solvate(**f1)
f3 = energy_minimize(**f2)
Now prepare input for a MD run with restraints on the protein::
MD_restrained(**f3)
Use the files in the directory to run the simulation locally or on a
cluster. You can provide your own template for a queuing system
submission script; see the source code for details.
Once the restraint run has completed, use the last frame as input for
the equilibrium MD::
MD(struct='MD_POSRES/md.gro', runtime=1e5)
Run the resulting tpr file on a cluster.
User functions
--------------
The following functions are provided for the user:
.. autofunction:: topology
.. autofunction:: solvate
.. autofunction:: energy_minimize
.. autofunction:: em_schedule
.. autofunction:: MD_restrained
.. autofunction:: MD
Helper functions
----------------
The following functions are used under the hood and are mainly useful when
writing extensions to the module.
.. autofunction:: make_main_index
.. autofunction:: check_mdpargs
.. autofunction:: get_lipid_vdwradii
.. autofunction:: _setup_MD
Defined constants:
.. autodata:: CONC_WATER
.. autodata:: vdw_lipid_resnames
.. autodata:: vdw_lipid_atom_radii
"""
__docformat__ = "restructuredtext en"
import os
import errno
import re
import shutil
import warnings
import logging
logger = logging.getLogger("gromacs.setup")
import gromacs
from . import config
from .exceptions import (
GromacsError,
GromacsFailureWarning,
GromacsValueWarning,
AutoCorrectionWarning,
BadParameterWarning,
UsageWarning,
MissingDataError,
)
from . import run
from . import cbook
from . import qsub
from . import utilities
from .utilities import in_dir, realpath, Timedelta, asiterable, firstof
#: Concentration of water at standard conditions in mol/L.
#: Density at 25 degrees C and 1 atmosphere pressure: rho = 997.0480 g/L.
#: Molecular weight: M = 18.015 g/mol.
#: c = n/V = m/(V*M) = rho/M = 55.345 mol/L.
CONC_WATER = 55.345
# XXX: This is not used anywhere at the moment:
# parse this table for a usable data structure (and then put it directly in the docs)
recommended_mdp_table = """\
Table: recommended mdp parameters for different FF
========== ========= ================
mdp GROMOS OPLS-AA
========== ========= ================
rvdw 1.4 1.0
rlist 1.4 ? 1.0
========== ========= ================
"""
trj_compact_main = gromacs.tools.Trjconv(
ur="compact", center=True, boxcenter="tric", pbc="mol", input=("__main__", "system")
)
# trj_compact_main.__doc__ += "Returns a compact representation of the system centered on the __main__ group"
# TODO:
# - should be part of a class so that we can store the topology etc !!!
# and also store mainselection
def topology(
struct=None,
protein="protein",
top="system.top",
dirname="top",
posres="posres.itp",
ff="oplsaa",
water="tip4p",
**pdb2gmx_args
):
"""Build Gromacs topology files from pdb.
:Keywords:
*struct*
input structure (**required**)
*protein*
name of the output files
*top*
name of the topology file
*dirname*
directory in which the new topology will be stored
*ff*
force field (string understood by ``pdb2gmx``); default
"oplsaa"
*water*
water model (string), default "tip4p"
*pdb2gmxargs*
other arguments for ``pdb2gmx``
.. note::
At the moment this function simply runs ``pdb2gmx`` and uses
the resulting topology file directly. If you want to create
more complicated topologies and maybe also use additional itp
files or make a protein itp file then you will have to do this
manually.
"""
structure = realpath(struct)
new_struct = protein + ".pdb"
if posres is None:
posres = protein + "_posres.itp"
pdb2gmx_args.update(
{
"f": structure,
"o": new_struct,
"p": top,
"i": posres,
"ff": ff,
"water": water,
}
)
with in_dir(dirname):
logger.info(
"[{dirname!s}] Building topology {top!r} from struct = {struct!r}".format(
**vars()
)
)
# perhaps parse output from pdb2gmx 4.5.x to get the names of the chain itp files?
gromacs.pdb2gmx(**pdb2gmx_args)
return {
"top": realpath(dirname, top),
"struct": realpath(dirname, new_struct),
"posres": realpath(dirname, posres),
}
def make_main_index(struct, selection='"Protein"', ndx="main.ndx", oldndx=None):
"""Make index file with the special groups.
This routine adds the group __main__ and the group __environment__
to the end of the index file. __main__ contains what the user
defines as the *central* and *most important* parts of the
system. __environment__ is everything else.
The template mdp file, for instance, uses these two groups for T-coupling.
These groups are mainly useful if the default groups "Protein" and "Non-Protein"
are not appropriate. By using symbolic names such as __main__ one
can keep scripts more general.
:Returns:
*groups* is a list of dictionaries that describe the index groups. See
:func:`gromacs.cbook.parse_ndxlist` for details.
:Arguments:
*struct* : filename
structure (tpr, pdb, gro)
*selection* : string
is a ``make_ndx`` command such as ``"Protein"`` or ``r DRG`` which
determines what is considered the main group for centering etc. It is
passed directly to ``make_ndx``.
*ndx* : string
name of the final index file
*oldndx* : string
name of index file that should be used as a basis; if None
then the ``make_ndx`` default groups are used.
This routine is very dumb at the moment; maybe some heuristics will be
added later as could be other symbolic groups such as __membrane__.
"""
logger.info("Building the main index file {ndx!r}...".format(**vars()))
# pass 1: select
# get a list of groups
# need the first "" to get make_ndx to spit out the group list.
_, out, _ = gromacs.make_ndx(
f=struct, n=oldndx, o=ndx, stdout=False, input=("", "q")
)
groups = cbook.parse_ndxlist(out)
# find the matching groups,
# there is a nasty bug in GROMACS where make_ndx may have multiple
# groups, which caused the previous approach to fail big time.
# this is a work around the make_ndx bug.
# striping the "" allows compatibility with existing make_ndx selection commands.
selection = selection.strip('"')
selected_groups = [g for g in groups if g["name"].lower() == selection.lower()]
if len(selected_groups) > 1:
logging.warn("make_ndx created duplicated groups, performing work around")
if len(selected_groups) <= 0:
msg = "no groups found for selection {0}, available groups are {1}".format(
selection, groups
)
logging.error(msg)
raise ValueError(msg)
# Found at least one matching group, we're OK
# index of last group
last = len(groups) - 1
assert last == groups[-1]["nr"]
group = selected_groups[0]
# pass 2:
# 1) last group is __main__
# 2) __environment__ is everything else (eg SOL, ions, ...)
_, out, _ = gromacs.make_ndx(
f=struct,
n=ndx,
o=ndx,
stdout=False,
# make copy selected group, this now has index last + 1
input=(
"{0}".format(group["nr"]),
# rename this to __main__
"name {0} __main__".format(last + 1),
# make a complement to this group, it get index last + 2
'! "__main__"',
# rename this to __environment__
"name {0} __environment__".format(last + 2),
# list the groups
"",
# quit
"q",
),
)
return cbook.parse_ndxlist(out)
#: Hard-coded lipid residue names for a ``vdwradii.dat`` file. Use together with
#: :data:`~gromacs.setup.vdw_lipid_atom_radii` in :func:`~gromacs.setup.get_lipid_vdwradii`.
vdw_lipid_resnames = ["POPC", "POPE", "POPG", "DOPC", "DPPC", "DLPC", "DMPC", "DPPG"]
#: Increased atom radii for lipid atoms; these are simply the standard values from
#: ``GMXLIB/vdwradii.dat`` increased by 0.1 nm (C) or 0.05 nm (N, O, H).
vdw_lipid_atom_radii = {"C": 0.25, "N": 0.16, "O": 0.155, "H": 0.09}
def get_lipid_vdwradii(outdir=os.path.curdir, libdir=None):
"""Find vdwradii.dat and add special entries for lipids.
See :data:`gromacs.setup.vdw_lipid_resnames` for lipid
resnames. Add more if necessary.
"""
vdwradii_dat = os.path.join(outdir, "vdwradii.dat")
if libdir is not None:
filename = os.path.join(libdir, "vdwradii.dat") # canonical name
if not os.path.exists(filename):
msg = "No VDW database file found in {filename!r}.".format(**vars())
logger.exception(msg)
raise OSError(msg, errno.ENOENT)
else:
try:
filename = os.path.join(os.environ["GMXLIB"], "vdwradii.dat")
except KeyError:
try:
filename = os.path.join(os.environ["GMXDATA"], "top", "vdwradii.dat")
except KeyError:
msg = "Cannot find vdwradii.dat. Set GMXLIB (point to 'top') or GMXDATA ('share/gromacs')."
logger.exception(msg)
raise OSError(msg, errno.ENOENT)
if not os.path.exists(filename):
msg = "Cannot find {filename!r}; something is wrong with the Gromacs installation.".format(
**vars()
)
logger.exception(msg, errno.ENOENT)
raise OSError(msg)
# make sure to catch 3 and 4 letter resnames
patterns = vdw_lipid_resnames + list({x[:3] for x in vdw_lipid_resnames})
# TODO: should do a tempfile...
with open(vdwradii_dat, "w") as outfile:
# write lipid stuff before general
outfile.write("; Special larger vdw radii for solvating lipid membranes\n")
for resname in patterns:
for atom, radius in vdw_lipid_atom_radii.items():
outfile.write(
"{resname:4!s} {atom:<5!s} {radius:5.3f}\n".format(**vars())
)
with open(filename, "r") as infile:
for line in infile:
outfile.write(line)
logger.debug("Created lipid vdW radii file {vdwradii_dat!r}.".format(**vars()))
return realpath(vdwradii_dat)
def solvate_sol(
struct="top/protein.pdb",
top="top/system.top",
distance=0.9,
boxtype="dodecahedron",
water="tip4p",
solvent_name="SOL",
with_membrane=False,
dirname="solvate",
**kwargs
):
structure = realpath(struct)
topology = realpath(top)
# arguments for editconf that we honour
editconf_keywords = [
"box",
"bt",
"angles",
"c",
"center",
"aligncenter",
"align",
"translate",
"rotate",
"princ",
]
editconf_kwargs = dict((k, kwargs.pop(k, None)) for k in editconf_keywords)
editconf_boxtypes = ["triclinic", "cubic", "dodecahedron", "octahedron", None]
# needed for topology scrubbing
scrubber_kwargs = {"marker": kwargs.pop("marker", None)}
# sanity checks and argument dependencies
bt = editconf_kwargs.pop("bt")
boxtype = bt if bt else boxtype # bt takes precedence over boxtype
if not boxtype in editconf_boxtypes:
msg = "Unsupported boxtype {boxtype!r}: Only {boxtypes!r} are possible.".format(
**vars()
)
logger.error(msg)
raise ValueError(msg)
if editconf_kwargs["box"]:
distance = None # if box is set then user knows what she is doing...
if water.lower() in ("spc", "spce"):
water = "spc216"
elif water.lower() == "tip3p":
water = "spc216"
logger.warning(
"TIP3P water model selected: using SPC equilibrated box "
"for initial solvation because it is a reasonable starting point "
"for any 3-point model. EQUILIBRATE THOROUGHLY!"
)
# clean topology (if user added the marker; the default marker is
# ; Gromacs auto-generated entries follow:
n_removed = cbook.remove_molecules_from_topology(topology, **scrubber_kwargs)
with in_dir(dirname):
logger.info("[{dirname!s}] Solvating with water {water!r}...".format(**vars()))
if boxtype is None:
hasBox = False
ext = os.path.splitext(structure)[1]
if ext == ".gro":
hasBox = True
elif ext == ".pdb":
with open(structure) as struct:
for line in struct:
if line.startswith("CRYST"):
hasBox = True
break
if not hasBox:
msg = "No box data in the input structure {structure!r} and boxtype is set to None".format(
**vars()
)
logger.exception(msg)
raise MissingDataError(msg)
distance = boxtype = None # ensures that editconf just converts
editconf_kwargs.update(
{"f": structure, "o": "boxed.gro", "bt": boxtype, "d": distance}
)
gromacs.editconf(**editconf_kwargs)
if with_membrane:
vdwradii_dat = get_lipid_vdwradii() # need to clean up afterwards
logger.info(
"Using special vdW radii for lipids {0!r}".format(vdw_lipid_resnames)
)
try:
gromacs.genbox(p=topology, cp="boxed.gro", cs=water, o="solvated.gro")
except:
if with_membrane:
# remove so that it's not picked up accidentally
utilities.unlink_f(vdwradii_dat)
raise
logger.info("Solvated system with %s", water)
return {
"struct": realpath(dirname, "solvated.gro"),
}
def solvate_ion(
struct="solvated.gro",
top="top/system.top",
concentration=0,
cation="NA",
anion="CL",
solvent_name="SOL",
ndx="main.ndx",
mainselection='"Protein"',
dirname="solvate",
**kwargs
):
structure = realpath(struct)
topology = realpath(top)
# By default, grompp should not choke on a few warnings because at
# this stage the user cannot do much about it (can be set to any
# value but is kept undocumented...)
grompp_maxwarn = kwargs.pop("maxwarn", 10)
# handle additional include directories (kwargs are also modified!)
mdp_kwargs = cbook.add_mdp_includes(topology, kwargs)
with in_dir(dirname):
with open("none.mdp", "w") as mdp:
mdp.write(
"; empty mdp file\ninclude = {include!s}\nrcoulomb = 1\nrvdw = 1\nrlist = 1\n".format(
**mdp_kwargs
)
)
qtotgmx = cbook.grompp_qtot(
f="none.mdp",
o="topol.tpr",
c=structure,
p=topology,
stdout=False,
maxwarn=grompp_maxwarn,
)
qtot = round(qtotgmx)
logger.info(
"[{dirname!s}] After solvation: total charge qtot = {qtotgmx!r} = {qtot!r}".format(
**vars()
)
)
if concentration != 0:
logger.info(
"[{dirname!s}] Adding ions for c = {concentration:f} M...".format(
**vars()
)
)
# target concentration of free ions c ==>
# N = N_water * c/c_water
# add ions for concentration to the counter ions (counter ions are less free)
#
# get number of waters (count OW ... works for SPC*, TIP*P water models)
rc, output, junk = gromacs.make_ndx(
f="topol.tpr",
o="ow.ndx",
input=("keep 0", "del 0", "a OW*", "name 0 OW", "", "q"),
stdout=False,
)
groups = cbook.parse_ndxlist(output)
gdict = {g["name"]: g for g in groups} # overkill...
N_water = gdict["OW"]["natoms"] # ... but dict lookup is nice
N_ions = int(N_water * concentration / CONC_WATER) # number of monovalents
else:
N_ions = 0
# neutralize (or try -neutral switch of genion???)
n_cation = n_anion = 0
if qtot > 0:
n_anion = int(abs(qtot))
elif qtot < 0:
n_cation = int(abs(qtot))
n_cation += N_ions
n_anion += N_ions
if n_cation != 0 or n_anion != 0:
# sanity check:
assert qtot + n_cation - n_anion < 1e-6
logger.info(
"[{dirname!s}] Adding n_cation = {n_cation:d} and n_anion = {n_anion:d} ions...".format(
**vars()
)
)
gromacs.genion(
s="topol.tpr",
o="ionized.gro",
p=topology,
pname=cation,
nname=anion,
np=n_cation,
nn=n_anion,
input=solvent_name,
)
else:
# fake ionized file ... makes it easier to continue without too much fuzz
try:
os.unlink("ionized.gro")
except OSError as err:
if err.errno != errno.ENOENT:
raise
os.symlink("solvated.gro", "ionized.gro")
qtot = cbook.grompp_qtot(
f="none.mdp",
o="ionized.tpr",
c="ionized.gro",
p=topology,
stdout=False,
maxwarn=grompp_maxwarn,
)
if abs(qtot) > 1e-4:
wmsg = "System has non-zero total charge qtot = {qtot:g} e.".format(
**vars()
)
warnings.warn(wmsg, category=BadParameterWarning)
logger.warning(wmsg)
# make main index
try:
make_main_index("ionized.tpr", selection=mainselection, ndx=ndx)
except GromacsError as err:
# or should I rather fail here?
wmsg = (
"Failed to make main index file %r ... maybe set mainselection='...'.\n"
"The error message was:\n%s\n" % (ndx, str(err))
)
logger.warning(wmsg)
warnings.warn(wmsg, category=GromacsFailureWarning)
try:
trj_compact_main(f="ionized.gro", s="ionized.tpr", o="compact.pdb", n=ndx)
except GromacsError as err:
wmsg = (
"Failed to make compact pdb for visualization... pressing on regardless. "
"The error message was:\n%s\n" % str(err)
)
logger.warning(wmsg)
warnings.warn(wmsg, category=GromacsFailureWarning)
return {
"qtot": qtot,
"struct": realpath(dirname, "ionized.gro"),
"ndx": realpath(dirname, ndx), # not sure why this is propagated-is it used?
"mainselection": mainselection,
}
def solvate(
struct="top/protein.pdb",
top="top/system.top",
distance=0.9,
boxtype="dodecahedron",
concentration=0,
cation="NA",
anion="CL",
water="tip4p",
solvent_name="SOL",
with_membrane=False,
ndx="main.ndx",
mainselection='"Protein"',
dirname="solvate",
**kwargs
):
"""Put protein into box, add water, add counter-ions.
Currently this really only supports solutes in water. If you need
to embedd a protein in a membrane then you will require more
sophisticated approaches.
However, you *can* supply a protein already inserted in a
bilayer. In this case you will probably want to set *distance* =
``None`` and also enable *with_membrane* = ``True`` (using extra
big vdw radii for typical lipids).
.. Note:: The defaults are suitable for solvating a globular
protein in a fairly tight (increase *distance*!) dodecahedral
box.
:Arguments:
*struct* : filename
pdb or gro input structure
*top* : filename
Gromacs topology
*distance* : float
When solvating with water, make the box big enough so that
at least *distance* nm water are between the solute *struct*
and the box boundary.
Set *boxtype* to ``None`` in order to use a box size in the input
file (gro or pdb).
*boxtype* or *bt*: string
Any of the box types supported by :class:`~gromacs.tools.Editconf`
(triclinic, cubic, dodecahedron, octahedron). Set the box dimensions
either with *distance* or the *box* and *angle* keywords.
If set to ``None`` it will ignore *distance* and use the box
inside the *struct* file.
*bt* overrides the value of *boxtype*.
*box*
List of three box lengths [A,B,C] that are used by :class:`~gromacs.tools.Editconf`
in combination with *boxtype* (``bt`` in :program:`editconf`) and *angles*.
Setting *box* overrides *distance*.
*angles*
List of three angles (only necessary for triclinic boxes).
*concentration* : float
Concentration of the free ions in mol/l. Note that counter
ions are added in excess of this concentration.
*cation* and *anion* : string
Molecule names of the ions. This depends on the chosen force field.
*water* : string
Name of the water model; one of "spc", "spce", "tip3p",
"tip4p". This should be appropriate for the chosen force
field. If an alternative solvent is required, simply supply the path to a box
with solvent molecules (used by :func:`~gromacs.genbox`'s *cs* argument)
and also supply the molecule name via *solvent_name*.
*solvent_name*
Name of the molecules that make up the solvent (as set in the itp/top).
Typically needs to be changed when using non-standard/non-water solvents.
["SOL"]
*with_membrane* : bool
``True``: use special ``vdwradii.dat`` with 0.1 nm-increased radii on
lipids. Default is ``False``.
*ndx* : filename
How to name the index file that is produced by this function.
*mainselection* : string
A string that is fed to :class:`~gromacs.tools.Make_ndx` and
which should select the solute.
*dirname* : directory name
Name of the directory in which all files for the solvation stage are stored.
*includes*
List of additional directories to add to the mdp include path
*kwargs*
Additional arguments are passed on to
:class:`~gromacs.tools.Editconf` or are interpreted as parameters to be
changed in the mdp file.
"""
sol = solvate_sol(
struct=struct,
top=top,
distance=distance,
boxtype=boxtype,
water=water,
solvent_name=solvent_name,
with_membrane=with_membrane,
dirname=dirname,
**kwargs
)
ion = solvate_ion(
struct=sol["struct"],
top=top,
concentration=concentration,
cation=cation,
anion=anion,
solvent_name=solvent_name,
ndx=ndx,
mainselection=mainselection,
dirname=dirname,
**kwargs
)
return ion
def check_mdpargs(d):
"""Check if any arguments remain in dict *d*."""
if len(d) > 0:
wmsg = "Unprocessed mdp option are interpreted as options for grompp:\n" + str(
d
)
logger.warning(wmsg)
warnings.warn(wmsg, category=UsageWarning)
return len(d) == 0
def energy_minimize(
dirname="em",
mdp=config.templates["em.mdp"],
struct="solvate/ionized.gro",
top="top/system.top",
output="em.pdb",
deffnm="em",
mdrunner=None,
mdrun_args=None,
**kwargs
):
"""Energy minimize the system.
This sets up the system (creates run input files) and also runs
``mdrun_d``. Thus it can take a while.
Additional itp files should be in the same directory as the top file.
Many of the keyword arguments below already have sensible values.
:Keywords:
*dirname*
set up under directory dirname [em]
*struct*
input structure (gro, pdb, ...) [solvate/ionized.gro]
*output*
output structure (will be put under dirname) [em.pdb]
*deffnm*
default name for mdrun-related files [em]
*top*
topology file [top/system.top]
*mdp*
mdp file (or use the template) [templates/em.mdp]
*includes*
additional directories to search for itp files
*mdrunner*
:class:`gromacs.run.MDrunner` instance; by default we
just try :func:`gromacs.mdrun_d` and :func:`gromacs.mdrun` but a
MDrunner instance gives the user the ability to run mpi jobs
etc. [None]
*mdrun_args*
arguments for *mdrunner* (as a dict), e.g. ``{'nt': 2}``;
empty by default
.. versionadded:: 0.7.0
*kwargs*
remaining key/value pairs that should be changed in the
template mdp file, eg ``nstxtcout=250, nstfout=250``.
.. note:: If :func:`~gromacs.mdrun_d` is not found, the function
falls back to :func:`~gromacs.mdrun` instead.
"""
structure = realpath(struct)
topology = realpath(top)
mdp_template = config.get_template(mdp)
deffnm = deffnm.strip()
mdrun_args = {} if mdrun_args is None else mdrun_args
# write the processed topology to the default output
kwargs.setdefault("pp", "processed.top")
# filter some kwargs that might come through when feeding output
# from previous stages such as solvate(); necessary because *all*
# **kwargs must be *either* substitutions in the mdp file *or* valid
# command line parameters for ``grompp``.
kwargs.pop("ndx", None)
# mainselection is not used but only passed through; right now we
# set it to the default that is being used in all argument lists
# but that is not pretty. TODO.
mainselection = kwargs.pop("mainselection", '"Protein"')
# only interesting when passed from solvate()
qtot = kwargs.pop("qtot", 0)
# mdp is now the *output* MDP that will be generated from mdp_template
mdp = deffnm + ".mdp"
tpr = deffnm + ".tpr"
logger.info(
"[{dirname!s}] Energy minimization of struct={struct!r}, top={top!r}, mdp={mdp!r} ...".format(
**vars()
)
)
cbook.add_mdp_includes(topology, kwargs)
if qtot != 0:
# At the moment this is purely user-reported and really only here because
# it might get fed into the function when using the keyword-expansion pipeline
# usage paradigm.
wmsg = "Total charge was reported as qtot = {qtot:g} <> 0; probably a problem.".format(
**vars()
)
logger.warning(wmsg)
warnings.warn(wmsg, category=BadParameterWarning)
with in_dir(dirname):
unprocessed = cbook.edit_mdp(mdp_template, new_mdp=mdp, **kwargs)
check_mdpargs(unprocessed)
gromacs.grompp(
f=mdp, o=tpr, c=structure, r=structure, p=topology, **unprocessed
)
mdrun_args.update(v=True, stepout=10, deffnm=deffnm, c=output)
if mdrunner is None:
mdrun = run.get_double_or_single_prec_mdrun()
mdrun(**mdrun_args)
else:
if type(mdrunner) is type:
# class
# user wants full control and provides simulation.MDrunner **class**
# NO CHECKING --- in principle user can supply any callback they like
mdrun = mdrunner(**mdrun_args)
mdrun.run()
else:
# anything with a run() method that takes mdrun arguments...
try:
mdrunner.run(mdrunargs=mdrun_args)
except AttributeError:
logger.error(
"mdrunner: Provide a gromacs.run.MDrunner class or instance or a callback with a run() method"
)
raise TypeError(
"mdrunner: Provide a gromacs.run.MDrunner class or instance or a callback with a run() method"
)
# em.gro --> gives 'Bad box in file em.gro' warning --- why??
# --> use em.pdb instead.
if not os.path.exists(output):
errmsg = "Energy minimized system NOT produced."
logger.error(errmsg)
raise GromacsError(errmsg)
final_struct = realpath(output)
logger.info(
"[{dirname!s}] energy minimized structure {final_struct!r}".format(**vars())
)
return {
"struct": final_struct,
"top": topology,
"mainselection": mainselection,
}
def em_schedule(**kwargs):
"""Run multiple energy minimizations one after each other.
:Keywords:
*integrators*
list of integrators (from 'l-bfgs', 'cg', 'steep')
[['bfgs', 'steep']]
*nsteps*
list of maximum number of steps; one for each integrator in
in the *integrators* list [[100,1000]]
*kwargs*
mostly passed to :func:`gromacs.setup.energy_minimize`
:Returns: dictionary with paths to final structure ('struct') and
other files
:Example:
Conduct three minimizations:
1. low memory Broyden-Goldfarb-Fletcher-Shannon (BFGS) for 30 steps
2. steepest descent for 200 steps
3. finish with BFGS for another 30 steps
We also do a multi-processor minimization when possible (i.e. for steep
(and conjugate gradient) by using a :class:`gromacs.run.MDrunner` class
for a :program:`mdrun` executable compiled for OpenMP in 64 bit (see
:mod:`gromacs.run` for details)::
import gromacs.run
gromacs.setup.em_schedule(struct='solvate/ionized.gro',
mdrunner=gromacs.run.MDrunnerOpenMP64,
integrators=['l-bfgs', 'steep', 'l-bfgs'],
nsteps=[50,200, 50])
.. Note:: You might have to prepare the mdp file carefully because at the
moment one can only modify the *nsteps* parameter on a
per-minimizer basis.
"""
mdrunner = kwargs.pop("mdrunner", None)
integrators = kwargs.pop("integrators", ["l-bfgs", "steep"])
kwargs.pop("integrator", None) # clean input; we set intgerator from integrators
nsteps = kwargs.pop("nsteps", [100, 1000])
outputs = [
"em{0:03d}_{1!s}.pdb".format(i, integrator)
for i, integrator in enumerate(integrators)
]
outputs[-1] = kwargs.pop("output", "em.pdb")
files = {"struct": kwargs.pop("struct", None)} # fake output from energy_minimize()
for i, integrator in enumerate(integrators):
struct = files["struct"]
logger.info(
"[em %d] energy minimize with %s for maximum %d steps",
i,
integrator,
nsteps[i],
)
kwargs.update(
{
"struct": struct,
"output": outputs[i],
"integrator": integrator,
"nsteps": nsteps[i],
}
)
if not integrator == "l-bfgs":
kwargs["mdrunner"] = mdrunner
else:
kwargs["mdrunner"] = None
logger.warning(
"[em %d] Not using mdrunner for L-BFGS because it cannot "