Skip to content

Commit

Permalink
Fix import issues due to refactoring as package
Browse files Browse the repository at this point in the history
  • Loading branch information
kzinovjev committed Jan 30, 2020
1 parent 17bb9d5 commit 8066743
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 188 deletions.
2 changes: 1 addition & 1 deletion enlighten2/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from enlighten2 import wrappers, sanderflow, utils, pdb_utils
from enlighten2 import wrappers, sanderflow, utils, pdb_utils, tleap
165 changes: 85 additions & 80 deletions enlighten2/dynam.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,83 +15,88 @@ def get_dynam_dir(system):
return "dynam" + postfix


parser = argparse.ArgumentParser(
description=""
"Performs a fast equilibration of a simulation system "
"prepared using prep.py. Does the following:"
" - initial brief minimization of hydrogens (100 steps)"
" - simulated annealing (10000 steps)"
" - final brief minimization (100 steps)",
formatter_class=argparse.RawDescriptionHelpFormatter
)

parser.add_argument("system", help="name of the simulation system (as in prep.py)")
parser.add_argument('-relax', action='store_true')
parser.add_argument("params", help="additional parameters",
type=argparse.FileType(), nargs='?')
args = parser.parse_args()

if not os.path.isdir(args.system):
print("Directory {} is not found. Please run prep first.".format(args.system),
file=sys.stderr)
print("If you HAVE run prep.sh, please run dynam.py "
"from the same directory as prep.sh.",
file=sys.stderr)
exit(1)

params = {
"steps": 25000,
"belly_radius": 4.5,
"central_atom": None
}

if args.params is not None:
params = {**params, **json.load(args.params)}
args.params.close()

prep_params_path = os.path.join(args.system, 'tleap', 'params')
if os.path.isfile(prep_params_path):
with open(prep_params_path, 'r') as f:
params = {**params, **json.load(f)}

bellymask = ":{} <@{}".format(params["central_atom"],
params["belly_radius"])

sander_params = {**params, "bellymask": bellymask}

relax_params = [
{"name": "minimize_h_all", "template": "minh"},
{"name": "minimize_h_in_sphere", "template": "minh_ibelly", "params": sander_params},
{"name": "annealing_with_restraints", "template": "sa_ca", "params": sander_params},
{"name": "annealing_without_restraints", "template": "sa", "params": sander_params},
{"name": "minimize_all_in_sphere", "template": "min_ibelly", "params": sander_params}
]

params = {}
dynam_params = [
{"name": "heat", "template": "heat", "params": sander_params},
{"name": "md", "template": "md", "params": sander_params, "monitor": True},
{"name": "minimize", "template": "min", "params": sander_params},
]

tleap_dir = os.path.abspath(os.path.join(args.system, 'tleap'))
prmtop = "{}/{}.top".format(tleap_dir, args.system)
mode = 'relax' if args.relax else 'dynam'

if mode == 'relax':
crd = "{}/{}.rst".format(tleap_dir, args.system)
sanderflow_params = relax_params
working_dir = '{}/{}'.format(args.system, 'relax')
else:
relax_dir = os.path.abspath(os.path.join(args.system, 'relax'))
crd = "{}/{}_relax.rst".format(relax_dir, args.system)
sanderflow_params = dynam_params
working_dir = '{}/{}'.format(args.system, get_dynam_dir(args.system))

utils.set_working_directory(working_dir)
status, result = sanderflow.run(prmtop, crd, sanderflow_params)
if not status:
sys.exit(1)

final_crd = "{system}_{mode}.rst".format(system=args.system, mode=mode)
os.system("cp {crd} {final_crd}".format(crd=result.crd, final_crd=final_crd))
def main():

parser = argparse.ArgumentParser(
description=""
"Performs a fast equilibration of a simulation system "
"prepared using prep.py. Does the following:"
" - initial brief minimization of hydrogens (100 steps)"
" - simulated annealing (10000 steps)"
" - final brief minimization (100 steps)",
formatter_class=argparse.RawDescriptionHelpFormatter
)

parser.add_argument("system", help="name of the simulation system (as in prep.py)")
parser.add_argument('-relax', action='store_true')
parser.add_argument("params", help="additional parameters",
type=argparse.FileType(), nargs='?')
args = parser.parse_args()

if not os.path.isdir(args.system):
print("Directory {} is not found. Please run prep first.".format(args.system),
file=sys.stderr)
print("If you HAVE run prep.sh, please run dynam.py "
"from the same directory as prep.sh.",
file=sys.stderr)
exit(1)

params = {
"steps": 25000,
"belly_radius": 4.5,
"central_atom": None
}

if args.params is not None:
params = {**params, **json.load(args.params)}
args.params.close()

prep_params_path = os.path.join(args.system, 'tleap', 'params')
if os.path.isfile(prep_params_path):
with open(prep_params_path, 'r') as f:
params = {**params, **json.load(f)}

bellymask = ":{} <@{}".format(params["central_atom"],
params["belly_radius"])

sander_params = {**params, "bellymask": bellymask}

relax_params = [
{"name": "minimize_h_all", "template": "minh"},
{"name": "minimize_h_in_sphere", "template": "minh_ibelly", "params": sander_params},
{"name": "annealing_with_restraints", "template": "sa_ca", "params": sander_params},
{"name": "annealing_without_restraints", "template": "sa", "params": sander_params},
{"name": "minimize_all_in_sphere", "template": "min_ibelly", "params": sander_params}
]

dynam_params = [
{"name": "heat", "template": "heat", "params": sander_params},
{"name": "md", "template": "md", "params": sander_params, "monitor": True},
{"name": "minimize", "template": "min", "params": sander_params},
]

tleap_dir = os.path.abspath(os.path.join(args.system, 'tleap'))
prmtop = "{}/{}.top".format(tleap_dir, args.system)
mode = 'relax' if args.relax else 'dynam'

if mode == 'relax':
crd = "{}/{}.rst".format(tleap_dir, args.system)
sanderflow_params = relax_params
working_dir = '{}/{}'.format(args.system, 'relax')
else:
relax_dir = os.path.abspath(os.path.join(args.system, 'relax'))
crd = "{}/{}_relax.rst".format(relax_dir, args.system)
sanderflow_params = dynam_params
working_dir = '{}/{}'.format(args.system, get_dynam_dir(args.system))

utils.set_working_directory(working_dir)
status, result = sanderflow.run(prmtop, crd, sanderflow_params)
if not status:
sys.exit(1)

final_crd = "{system}_{mode}.rst".format(system=args.system, mode=mode)
os.system("cp {crd} {final_crd}".format(crd=result.crd, final_crd=final_crd))


if __name__ == '__main__':
main()
212 changes: 109 additions & 103 deletions enlighten2/prep.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,107 +34,113 @@ def run_propka(pdb, ph, ph_offset):
return pdb


parser = argparse.ArgumentParser(
description=""
"Prepares the simulation system by performing the following actions:\n"
" - ligand parameterisation (with antechamber/prmchk2)\n"
" - pdb protonation (apart from ligands, they need to "
" be protonated already!)\n"
" - tleap to solvate and write starting parm7/rst7 (top/rst)\n\n",
formatter_class=argparse.RawDescriptionHelpFormatter
)

parser.add_argument("name", help="name of the job")
parser.add_argument("pdb", help="protonated PDB file",
type=argparse.FileType())
parser.add_argument("ligand",
help="name of the residue to be used as the ligand")
parser.add_argument("charge", help="charge of the ligand", type=int)
parser.add_argument("params", help="JSON file with advanced parameters",
type=argparse.FileType(), nargs='?')

args = parser.parse_args()

job_name = args.name
ligand_name = args.ligand
ligand_charge = args.charge

params = {
'antechamber': {
'ligand': args.ligand,
'charge': args.charge,
'ligand_chainID': "L",
'ligand_index': 1,
},
'propka': {
'with_propka': True,
'ph': 7.0,
'ph_offset': 0.7,
},
'tleap': {
'template': 'sphere',
'solvent_radius': 20.0,
'solvent_closeness': 0.75,
'include': []
def main():

parser = argparse.ArgumentParser(
description=""
"Prepares the simulation system by performing the following actions:\n"
" - ligand parameterisation (with antechamber/prmchk2)\n"
" - pdb protonation (apart from ligands, they need to "
" be protonated already!)\n"
" - tleap to solvate and write starting parm7/rst7 (top/rst)\n\n",
formatter_class=argparse.RawDescriptionHelpFormatter
)

parser.add_argument("name", help="name of the job")
parser.add_argument("pdb", help="protonated PDB file",
type=argparse.FileType())
parser.add_argument("ligand",
help="name of the residue to be used as the ligand")
parser.add_argument("charge", help="charge of the ligand", type=int)
parser.add_argument("params", help="JSON file with advanced parameters",
type=argparse.FileType(), nargs='?')

args = parser.parse_args()

job_name = args.name
ligand_name = args.ligand
ligand_charge = args.charge

params = {
'antechamber': {
'ligand': args.ligand,
'charge': args.charge,
'ligand_chainID': "L",
'ligand_index': 1,
},
'propka': {
'with_propka': True,
'ph': 7.0,
'ph_offset': 0.7,
},
'tleap': {
'template': 'sphere',
'solvent_radius': 20.0,
'solvent_closeness': 0.75,
'include': []
}
}
}

if args.params is not None:
params = utils.merge_dicts_of_dicts(params, json.load(args.params))
args.params.close()
params['tleap']['include'] = [os.path.abspath(path)
for path in params['tleap']['include']]

if os.path.exists(job_name):
print("It appears you've already (attempted to) run prep.py with {0}. "
"Delete folder {0} or rename pdb if you want to run it again."
.format(job_name), file=sys.stderr)
sys.exit(1)

print("Starting PREP protocol in {}/".format(job_name))
sys.stdout.flush()
utils.set_working_directory(job_name)

pdb = pdb_utils.Pdb(args.pdb)
ligand_index = params['antechamber']['ligand_index']
ligand_atoms = get_ligand_atoms(pdb, ligand_name, ligand_index)

# Only generate ligand frcmod if it is not found in include paths
ligand_frcmod = utils.file_in_paths(ligand_name + '.frcmod',
params['tleap']['include'])
antechamber = wrappers.AntechamberWrapper(
pdb_utils.Pdb(atoms=ligand_atoms), ligand_name, ligand_charge,
create_frcmod=ligand_frcmod is None
)
sys.stdout.flush()
if ligand_frcmod is None:
params['tleap']['include'].append(antechamber.working_directory)

# Change ligand chain ID to ligand_chainID
ligand_chainID = params['antechamber']['ligand_chainID']
pdb_utils.modify_atoms(ligand_atoms, 'chainID', ligand_chainID)

# Run pdb4amber and reduce
reduceResults = wrappers.Pdb4AmberReduceWrapper(pdb)
sys.stdout.flush()
pdb = reduceResults.pdb

# Run propka31 if requested and found
if params['propka']['with_propka']:
pdb = run_propka(pdb=pdb,
ph=params['propka']['ph'],
ph_offset=params['propka']['ph_offset'])
sys.stdout.flush()

ligand = pdb.get_residues_by_name(ligand_name)[ligand_index-1]
params['tleap']['name'] = os.path.basename(job_name)
params['tleap']['pdb'] = pdb
params['tleap']['ligand'] = ligand
tleap = wrappers.TleapWrapper(params['tleap']['template'],
params['tleap']['include'],
reduceResults.nonprot_residues,
params['tleap'])
sys.stdout.flush()
os.system("cp {} .".format(tleap.top))
os.system("cp {} .".format(tleap.rst))
print("Finished PREP protocol.")

if args.params is not None:
params = utils.merge_dicts_of_dicts(params, json.load(args.params))
args.params.close()
params['tleap']['include'] = [os.path.abspath(path)
for path in params['tleap']['include']]

if os.path.exists(job_name):
print("It appears you've already (attempted to) run prep.py with {0}. "
"Delete folder {0} or rename pdb if you want to run it again."
.format(job_name), file=sys.stderr)
sys.exit(1)

print("Starting PREP protocol in {}/".format(job_name))
sys.stdout.flush()
utils.set_working_directory(job_name)

pdb = pdb_utils.Pdb(args.pdb)
ligand_index = params['antechamber']['ligand_index']
ligand_atoms = get_ligand_atoms(pdb, ligand_name, ligand_index)

# Only generate ligand frcmod if it is not found in include paths
ligand_frcmod = utils.file_in_paths(ligand_name + '.frcmod',
params['tleap']['include'])
antechamber = wrappers.AntechamberWrapper(
pdb_utils.Pdb(atoms=ligand_atoms), ligand_name, ligand_charge,
create_frcmod=ligand_frcmod is None
)
sys.stdout.flush()
if ligand_frcmod is None:
params['tleap']['include'].append(antechamber.working_directory)

# Change ligand chain ID to ligand_chainID
ligand_chainID = params['antechamber']['ligand_chainID']
pdb_utils.modify_atoms(ligand_atoms, 'chainID', ligand_chainID)

# Run pdb4amber and reduce
reduceResults = wrappers.Pdb4AmberReduceWrapper(pdb)
sys.stdout.flush()
pdb = reduceResults.pdb

# Run propka31 if requested and found
if params['propka']['with_propka']:
pdb = run_propka(pdb=pdb,
ph=params['propka']['ph'],
ph_offset=params['propka']['ph_offset'])
sys.stdout.flush()

ligand = pdb.get_residues_by_name(ligand_name)[ligand_index-1]
params['tleap']['name'] = os.path.basename(job_name)
params['tleap']['pdb'] = pdb
params['tleap']['ligand'] = ligand
tleap = wrappers.TleapWrapper(params['tleap']['template'],
params['tleap']['include'],
reduceResults.nonprot_residues,
params['tleap'])
sys.stdout.flush()
os.system("cp {} .".format(tleap.top))
os.system("cp {} .".format(tleap.rst))
print("Finished PREP protocol.")


if __name__ == '__main__':
main()
2 changes: 1 addition & 1 deletion enlighten2/tleap/sphere.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import sys
import pdb_utils
from enlighten2 import pdb_utils


def run(params, template):
Expand Down
Loading

0 comments on commit 8066743

Please sign in to comment.