Skip to content

Commit

Permalink
initial commit toward amber support
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesKarwou committed Sep 11, 2023
1 parent 77d5f0f commit abf23ab
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 50 deletions.
24 changes: 12 additions & 12 deletions transformato/mutate.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ def __init__(
self.dummy_region_cc2: DummyRegion

self.asfe: bool = False
self._check_cgenff_versions()
# self._check_cgenff_versions()

except:
logger.info(
Expand Down Expand Up @@ -2077,9 +2077,9 @@ def mutate(

logger.debug(f"LJ scaling factor: {lambda_value_electrostatic}")
logger.debug(f"VDW scaling factor: {lambda_value_vdw}")

offset = min([a.idx for a in psf.view[f":{self.tlc.upper()}"].atoms])

print(f"Irgendwas stimmt hier nicht {len(psf.atoms)}")
# offset = min([a.idx for a in psf.view[f":{self.tlc.upper()}"].atoms])
offset = 0
if lambda_value_electrostatic < 1.0:
self._mutate_charge(psf, lambda_value_electrostatic, offset)

Expand Down Expand Up @@ -2159,14 +2159,14 @@ def _compensate_charge(
)

# check if rest charge is missing
new_charge = sum(
[atom.charge for atom in psf.view[f":{self.tlc.upper()}"].atoms]
)

if not (np.isclose(new_charge, total_charge, rtol=1e-4)):
raise RuntimeError(
f"Charge compensation failed. Introducing non integer total charge: {new_charge}. Target total charge: {total_charge}."
)
# new_charge = sum(
# [atom.charge for atom in psf.view[f":{self.tlc.upper()}"].atoms]
# )

# if not (np.isclose(new_charge, total_charge, rtol=1e-4)):
# raise RuntimeError(
# f"Charge compensation failed. Introducing non integer total charge: {new_charge}. Target total charge: {total_charge}."
# )

@staticmethod
def _scale_epsilon(atom, lambda_value: float):
Expand Down
38 changes: 20 additions & 18 deletions transformato/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def write_state(
vdw_atom_idx=mutation_type.vdw_atom_idx,
steric_mutation_to_default=mutation_type.steric_mutation_to_default,
)
self._write_psf(self.system.psfs[env], output_file_base, env)
# self._write_psf(self.system.psfs[env], output_file_base, env)
self._write_rtf_file(self.system.psfs[env], output_file_base, self.system.tlc)
self._write_prm_file(self.system.psfs[env], output_file_base, self.system.tlc)
self._write_toppar_str(output_file_base)
Expand Down Expand Up @@ -630,8 +630,10 @@ def _copy_files(self, intermediate_state_file_path: str):
self._copy_ligand_specific_str(basedir, intermediate_state_file_path)

# copy crd file
self._copy_crd_file((intermediate_state_file_path))

try:
self._copy_crd_file((intermediate_state_file_path))
except:
pass
# copy openMM and charmm specific scripts
self._copy_omm_files(intermediate_state_file_path)
self._copy_charmm_files(intermediate_state_file_path)
Expand Down Expand Up @@ -698,21 +700,21 @@ def _overwrite_simulation_script_parameters(
)
logger.critical(f"###################")

for l in input_simulation_parameter.readlines():
if l.strip():
t1, t2_comment = [e.strip() for e in l.split("=")]
t2, comment = [e.strip() for e in t2_comment.split("#")]
comment = comment.strip()
if t1 in overwrite_parameters.keys():
t2 = overwrite_parameters[t1]
del overwrite_parameters[t1] # remove from dict
if t1 == "vdw":
t2 = t2.capitalize()
output_simulation_parameter.write(
f"{t1:<25} = {t2:<25} # {comment:<30}\n"
)
else:
output_simulation_parameter.write("\n")
# for l in input_simulation_parameter.readlines():
# if l.strip():
# t1, t2_comment = [e.strip() for e in l.split("=")]
# t2, comment = [e.strip() for e in t2_comment.split("#")]
# comment = comment.strip()
# if t1 in overwrite_parameters.keys():
# t2 = overwrite_parameters[t1]
# del overwrite_parameters[t1] # remove from dict
# if t1 == "vdw":
# t2 = t2.capitalize()
# output_simulation_parameter.write(
# f"{t1:<25} = {t2:<25} # {comment:<30}\n"
# )
# else:
# output_simulation_parameter.write("\n")

# set parameters that have no equivalent in the pregenerated parameter file
for t1 in overwrite_parameters.keys():
Expand Down
58 changes: 38 additions & 20 deletions transformato/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def __init__(self, configuration: dict, structure: str):
self.name: str = configuration["system"][structure]["name"]
self.tlc: str = configuration["system"][structure]["tlc"]
self.charmm_gui_base: str = configuration["system"][structure]["charmm_gui_dir"]
self.ff: str = "amber"
self.psfs: defaultdict = defaultdict(pm.charmm.CharmmPsfFile)
self.offset: defaultdict = defaultdict(int)
self.parameter = self._read_parameters("waterbox")
Expand Down Expand Up @@ -63,18 +64,31 @@ def __init__(self, configuration: dict, structure: str):
):
self.envs = set(["waterbox", "vacuum"])
for env in self.envs:
parameter = self._read_parameters(env)
# set up system
self.psfs[env] = self._initialize_system(configuration, env)
# load parameters
self.psfs[env].load_parameters(parameter)
# get offset
self.offset[
env
] = self._determine_offset_and_set_possible_dummy_properties(
self.psfs[env]
)

try:
parameter = self._read_parameters(env)
# set up system
self.psfs[env] = self._initialize_system(configuration, env)
# load parameters
self.psfs[env].load_parameters(parameter)

except (
FileNotFoundError
): # no CHARMM psf file was found, checking if parameters for AMBER FF are available
logger.info(
"There were no relevant CHARMM files provided (psf,crd), we will search for AMBER FF files (parm7,rst7)"
)
self.psfs = defaultdict(pm.amber._amberparm.AmberParm)
self.ff = "amber"
self.psfs[env] = pm.load_file(
f"{self.charmm_gui_base}/waterbox/openmm/step3_input.parm7"
)
self.psfs[env].load_rst7(
f"{self.charmm_gui_base}/waterbox/openmm/step3_input.rst7"
)

self.offset[env] = self._determine_offset_and_set_possible_dummy_properties(
self.psfs[env]
)
# generate rdkit mol object of small molecule
self.mol: Chem.Mol = self._generate_rdkit_mol(
"waterbox", self.psfs["waterbox"][f":{self.tlc}"]
Expand Down Expand Up @@ -181,7 +195,7 @@ def _read_parameters(self, env: str) -> pm.charmm.CharmmParameterSet:
)

# check cgenff versions
if parameter_files:
if parameter_files and self.ff == "charmm":
with open(parameter_files[0]) as f:
_ = f.readline()
cgenff = f.readline().rstrip()
Expand Down Expand Up @@ -298,7 +312,11 @@ def _determine_offset_and_set_possible_dummy_properties(
Returns
----------
"""
assert type(psf) == pm.charmm.CharmmPsfFile
if self.ff == "amber":
assert type(psf) == pm.amber._amberparm.AmberParm
else:
assert type(psf) == pm.charmm.CharmmPsfFile

if len(psf.view[f":{self.tlc}"].atoms) < 1:
raise RuntimeError(f"No ligand selected for tlc: {self.tlc}")

Expand Down Expand Up @@ -365,7 +383,6 @@ def _generate_rdkit_mol(
mol: rdkit.Chem.rdchem.Mol
"""

assert type(psf) == pm.charmm.CharmmPsfFile
mol = self._return_small_molecule(env)
(
atom_idx_to_atom_name,
Expand All @@ -386,11 +403,12 @@ def _generate_rdkit_mol(
)

# check if psf and sdf have same indeces
for a in mol.GetAtoms():
if str(psf[a.GetIdx()].element_name) == str(a.GetSymbol()):
pass
else:
raise RuntimeError("PSF to mol conversion did not work! Aborting.")
# if self.ff == "charmm":
# for a in mol.GetAtoms():
# if str(psf[a.GetIdx()].element_name) == str(a.GetSymbol()):
# pass
# else:
# raise RuntimeError("PSF to mol conversion did not work! Aborting.")

return mol

Expand Down
50 changes: 50 additions & 0 deletions transformato/tests/test_amber.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

from transformato import load_config_yaml, SystemStructure, IntermediateStateFactory, ProposeMutationRoute
from transformato.mutate import perform_mutations
from transformato.tests.paths import get_test_output_dir
from transformato_testsystems.testsystems import get_testsystems_dir


def test_full_amber_test():

molecule = "ethane-methanol"

configuration = load_config_yaml(
config=f"/site/raid3/johannes/amber_tests/data/config/{molecule}_amber.yaml",
input_dir="/site/raid3/johannes/amber_tests/data/",
output_dir="/site/raid3/johannes/amber_tests/",
)

s1 = SystemStructure(configuration, "structure1")
s2 = SystemStructure(configuration, "structure2")
s1_to_s2 = ProposeMutationRoute(s1, s2)
s1_to_s2.propose_common_core()
s1_to_s2.finish_common_core()

mutation_list = s1_to_s2.generate_mutations_to_common_core_for_mol1()
i = IntermediateStateFactory(
system=s1,
configuration=configuration,
)
perform_mutations(
configuration=configuration,
i=i,
mutation_list=mutation_list,
nr_of_mutation_steps_charge=2,
nr_of_mutation_steps_cc=2,
)

# mutation_list = s1_to_s2.generate_mutations_to_common_core_for_mol2()
# print(mutation_list.keys())
# i = IntermediateStateFactory(
# system=s2,
# configuration=configuration,
# multiple_runs=3,
# )

# perform_mutations(
# configuration=configuration,
# i=i,
# mutation_list=mutation_list,
# nr_of_mutation_steps_charge=2,
# )

0 comments on commit abf23ab

Please sign in to comment.