-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake_leap.py
More file actions
executable file
·280 lines (248 loc) · 10.2 KB
/
make_leap.py
File metadata and controls
executable file
·280 lines (248 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#! /usr/bin/env python3
'''
A utility to help generate tleap scripts for AMBER parameterization.
Copyright 2025 Charlie Laughton'''
from argparse import ArgumentParser
import sys
import mdtraj as mdt
from pathlib import Path
from crossflow.tasks import SubprocessTask
from crossflow.filehandling import FileHandler
import shutil
__version__ = '1.0.0'
def _aliased(cmd):
'''
Little utility to see if a comand is aliased
'''
alias = SubprocessTask('alias {cmd}')
alias.set_inputs(['cmd'])
alias.set_outputs(['STDOUT'])
al = alias(cmd)
return al
def _check_available(cmd):
'''
Little utility to check a required command is available
'''
if shutil.which(cmd) is None:
if _aliased(cmd) == '':
raise FileNotFoundError(f'Error: cannot find the {cmd} command')
def _check_exists(filename):
'''
Little utility to check if a required file is present
'''
if not Path(filename).exists():
raise FileNotFoundError(f'Error: cannot find required file {filename}')
def leap(amberpdb, ff, het_names=None, solvate=None, padding=10.0, het_dir='.',
n_na=0, n_cl=0, script_only=False):
'''
Parameterize a molecular system using tleap (or just generate the script).
Args:
amberpdb (str): An Amber-compliant PDB file
ff (list): The force fields to use.
het_names (list): List of parameterised heterogens
solvate (str or None): type of periodic box ('box', 'cube', or 'oct')
padding (float): Clearance between solute and any box edge (Angstroms)
het_dir (str or Path): location of the directory containing heterogen
parameters
n_na (int): number of Na+ ions to add (0 = minimal salt)
n_cl (int): number of Cl- ions to add (0 = minimal salt)
script_only (bool): if True, only generate the tleap script
and do not run tleap
Returns:
(FileHandle, FileHandle, str): prmtop file, inpcrd file,
tleap log text
or:
str: the tleap script (if script_only=True)
'''
_check_available('tleap')
inputs = ['script', 'system.pdb']
outputs = ['system.prmtop', 'system.inpcrd', 'STDOUT']
script = "".join([f'source leaprc.{f}\n' for f in ff])
if solvate:
if solvate not in ['oct', 'box', 'cube']:
raise ValueError(f'Error: unrecognised solvate option "{solvate}"')
water_box = 'TIP3PBOX'
for f in ff:
if 'water.opc' in f:
water_box = 'OPCBOX'
elif 'water.tip4pew' in f:
water_box = 'TIP4PEWBOX'
elif 'water.tip4p2005' in f:
water_box = 'TIP4P2005BOX'
elif 'water.spce' in f:
water_box = 'SPCEBOX'
if het_names:
if len(het_names) > 0:
for r in het_names:
_check_exists(Path(het_dir) / f'{r}.frcmod')
_check_exists(Path(het_dir) / f'{r}.mol2')
script += f'loadamberparams {r}.frcmod\n'
script += f'{r} = loadmol2 {r}.mol2\n'
inputs += [f'{r}.mol2', f'{r}.frcmod']
if script_only:
script += f"system = loadpdb {amberpdb}\n"
else:
script += "system = loadpdb system.pdb\n"
if solvate == "oct":
script += f"solvateoct system {water_box} {padding}\n"
elif solvate == "cube":
script += f"solvatebox system {water_box} {padding} iso\n"
elif solvate == "box":
script += f"solvatebox system {water_box} {padding}\n"
if solvate is not None:
script += f"addions system Na+ {n_na}\naddions system Cl- {n_cl}\n"
script += "saveamberparm system system.prmtop system.inpcrd\nquit"
if script_only:
return script
tleap = SubprocessTask('tleap -f script')
tleap.set_inputs(inputs)
tleap.set_outputs(outputs)
fh = FileHandler()
scriptfile = fh.create('scriptfile')
scriptfile.write_text(script)
args = [scriptfile, amberpdb]
if het_names:
if len(het_names) > 0:
for r in het_names:
args += [f'{Path(het_dir)}/{r}.mol2',
f'{Path(het_dir)}/{r}.frcmod']
prmtop, inpcrd, stdout = tleap(*args)
if prmtop is None or inpcrd is None:
raise RuntimeError(f'Error in leap: {stdout}')
return prmtop, inpcrd, stdout
def make_leap(inpdb, outinpcrd, outprmtop, het_names=None,
het_dir='.',
forcefields=None,
solvate=None,
ion_molarity=None,
padding=10.0):
"""
Generate a tleap script to prepare a system for MD simulation.
Args:
inpdb (path-like): name of input PDB file
outinpcrd (path-like): name of output inpcrd file
outprmtop (path-like): name of output prmtop file
het_names (None or list): 3-letter residue names for heterogens
forcefields (None or list): List of forcefields to use
solvate (None or str): Solvation option - can be 'box',
'cube', or 'oct'.
padding (float): minimum distance from any solute atom
to a periodic box boundary (Angstroms)
ion_molarity (float or None): If not None, add Na+ and Cl- ions
to reach this molarity (M)
het_dir (str): Directory to search for heterogen parameter files
Returns:
str: The tleap script as a string
"""
_check_exists(inpdb)
if not forcefields:
print('Warning: no forcefields specified, '
'defaulting to "protein.ff14SB"', file=sys.stderr)
forcefields = ['protein.ff14SB']
has_protein_ff = False
for ff in forcefields:
if 'protein' in ff:
has_protein_ff = True
if not has_protein_ff:
if 'gaff2' in forcefields:
print('Warning: no protein forcefield specified,'
' defaulting to protein.ff19SB.', file=sys.stderr)
forcefields.append('protein.ff19SB')
else:
print('Warning: no protein forcefield specified,'
' defaulting to protein.ff14SB.', file=sys.stderr)
forcefields.append('protein.ff14SB')
if solvate:
if solvate not in ['oct', 'box', 'cube']:
raise ValueError(f'Error: unrecognised solvate option "{solvate}"')
water_ff = False
for ff in forcefields:
if 'water' in ff:
water_ff = True
if not water_ff:
print('Warning: no water forcefield specified but'
' solvation required.', file=sys.stderr)
if 'protein.ff19SB' in forcefields:
print('Defaulting to "water.opc" forcefield.',
file=sys.stderr)
forcefields.append('water.opc')
else:
print('Defaulting to "water.tip3p" forcefield.',
file=sys.stderr)
forcefields.append('water.tip3p')
if het_names is not None:
if 'gaff' not in forcefields and 'gaff2' not in forcefields:
print('Warning - heterogens are present but no gaff/gaff2 '
'forcefield has been specified.', file=sys.stderr)
print('Will default to using "gaff".', file=sys.stderr)
forcefields.append('gaff')
if not ion_molarity:
try:
script = leap(
inpdb, forcefields, het_names=het_names,
solvate=solvate, padding=padding, het_dir=het_dir,
script_only=True)
script = script.replace('system.prmtop', str(outprmtop))
script = script.replace('system.inpcrd', str(outinpcrd))
return script
except RuntimeError as e:
print(f'Error in leap:\n{e}')
exit(1)
try:
prmtop, inpcrd, stdout = leap(
inpdb, forcefields, het_names=het_names,
solvate=solvate, padding=padding, het_dir=het_dir)
except RuntimeError as e:
print(f'Error in leap:\n{e}')
exit(1)
if ion_molarity:
ttmp = mdt.load(inpcrd, top=prmtop)
n_waters = len(ttmp.topology.select('name O and resname HOH'))
n_na = len(ttmp.topology.select('name "Na+"'))
n_cl = len(ttmp.topology.select('name "Cl-"'))
n_ions = int(ion_molarity * n_waters/55.56)
if n_na > 0:
n_cl = n_ions - n_na
n_na = n_ions
else:
n_na = n_ions - n_cl
n_cl = n_ions
n_na = max(n_na, 0)
n_cl = max(n_cl, 0)
try:
script = leap(
inpdb, forcefields, het_names=het_names,
solvate=solvate, padding=padding, het_dir=het_dir,
n_na=n_na, n_cl=n_cl, script_only=True)
script = script.replace('system.prmtop', str(outprmtop))
script = script.replace('system.inpcrd', str(outinpcrd))
return script
except RuntimeError as e:
print(f'Error in leap:\n{e}')
exit(1)
parser = ArgumentParser(description="Generate tleap input script from PDB")
parser.add_argument('--inpdb', help='Input PDB file', required=True)
parser.add_argument('--outinpcrd', help='Output AMBER .inpcrd file',
required=True)
parser.add_argument('--outprmtop', help='Output AMBER .prmtop file',
required=True)
parser.add_argument('--forcefields', nargs='*', help='Force fields to use')
parser.add_argument('--het_names', nargs='*',
help='Names of heterogen residues')
parser.add_argument('--solvate', help='Type of water box to use',
choices=['box', 'cube', 'oct'])
parser.add_argument('--padding',
help='minimum distance of solute atoms from box edge',
type=float, default=10.0)
parser.add_argument('--het_dir', help='Directory for heterogen files',
default='.')
parser.add_argument('--ion_molarity', type=float,
help='Target ionic strength (M)')
parser.add_argument('--version', action='version', version=__version__)
parsed_args = parser.parse_args()
try:
result = make_leap(**vars(parsed_args))
except Exception as e:
print(f'Error: {e}', file=sys.stderr)
sys.exit(1)
print(result)