Skip to content

Commit

Permalink
fixed test_topology_generator test and made it so that topology_manag…
Browse files Browse the repository at this point in the history
…er_tests now navigate to the test directory before running tests, so that files generated from tests are not left in whatever directory you `run pytest` from
  • Loading branch information
tlfobe authored and tlfobe committed Sep 6, 2024
1 parent 48a3cd3 commit e7f016a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 114 deletions.
2 changes: 1 addition & 1 deletion terphenyl_simulations/force_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def _get_partial_charges(self, method="am1bcc"):
self.molecule.assign_partial_charges(partial_charge_method=method)
self.molecule.to_file(self.sdf_file, file_format="sdf")
else:
self.molecule = Molecule.from_file(sdf_file)
self.molecule = Molecule.from_file(self.sdf_file)

def _generate_ff_topologies(self):
interchange = Interchange.from_smirnoff(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
using just the input foldamer.build file
"""

from terphenyl_simulations.build import FoldamerBuilder, SystemBuilder
from terphenyl_simulations.build import FoldamerBuilder
import terphenyl_simulations
from unittest.mock import patch, mock_open, MagicMock
from terphenyl_simulations.utils import ROOT_DIR
import pytest
import shutil
Expand All @@ -26,22 +28,8 @@ def setup_foldamer_builder_tests():
yield FoldamerBuilder("mop_tetramer.build", path="output")
os.chdir(top_dir)


@pytest.fixture
def setup_system_builder_tests():
# Navigate to specific test directory
top_dir = os.path.abspath("")
os.chdir(os.path.join(ROOT_DIR, "tests/test_build_workflow"))
if os.path.isdir("output"):
shutil.rmtree("output")
fb = FoldamerBuilder("mop_tetramer.build", path="output")
fb.build_foldamer()
fb.write_pdb()
yield SystemBuilder("TCM", "mop_tetramer.build", path="output")
os.chdir(top_dir)


def test_foldamer_builder_chain(setup_foldamer_builder_tests):
@patch("terphenyl_simulations.build.TopologyManager.add_structure")
def test_foldamer_builder_chain(mock_add_structure, setup_foldamer_builder_tests):
builder = setup_foldamer_builder_tests
builder.build_foldamer()
assert builder.chain.n_particles == 197
Expand All @@ -50,33 +38,11 @@ def test_foldamer_builder_chain(setup_foldamer_builder_tests):
for label in builder.chain.labels["Compound"]:
assert label.name == "CAP"


def test_foldamer_builder_file_writing(setup_foldamer_builder_tests):
@patch("terphenyl_simulations.build.TopologyManager.add_structure")
def test_foldamer_builder_file_writing(mock_add_stricture, setup_foldamer_builder_tests):
builder = setup_foldamer_builder_tests
builder.build_foldamer()
builder.write_pdb()
builder.write_mol()
assert os.path.exists("output/mop_tetramer.pdb")
assert os.path.exists("output/mop_tetramer.mol")


def test_system_builder_inp(setup_system_builder_tests):
builder = setup_system_builder_tests
builder.build_packmol_inp()
assert os.path.exists("output/solvate.inp")
with open("output/solvate.inp", "r") as f:
for line in f.readlines():
assert "OUTPUT_FILENAME" not in line
assert "SOLUTE_PDB" not in line
assert "SOLUTE_POSITION" not in line
assert "SOLVENT_PDB" not in line
assert "N_SOLVENT" not in line
assert "SOLVENT_BOX" not in line


def test_system_builder_packmol(setup_system_builder_tests):
system_builder = setup_system_builder_tests
system_builder.build_packmol_inp()
system_builder.solvate_system()
assert os.path.exists("output/solvated_mop_tetramer.pdb")
assert os.path.exists("output/TCM.pdb")
Original file line number Diff line number Diff line change
Expand Up @@ -77,74 +77,4 @@ def test_default_tg_output(setup_default_ff_tests_post_charges):
default_ff_object._get_partial_charges()
default_ff_object._generate_ff_topologies()
assert os.path.exists("output/mop_dimer_openff-2.0.0.top")
assert os.path.exists("output/mop_dimer_openff-2.0.0.gro")


def test_topology_manager_save_load(navigate_to_test_dir):
assert navigate_to_test_dir is None
tp_manager = TopologyManager(topology_dir="", topology_object="top_manager.pkl")
tp_manager.topology_dictionary["test_key"] = "test_value"
tp_manager.save()

tp_manager_load = TopologyManager(
topology_dir="", topology_object="top_manager.pkl"
)

assert os.path.exists("top_manager.pkl")
assert "test_key" in tp_manager_load.topology_dictionary.keys()
assert tp_manager_load.topology_dictionary["test_key"] == "test_value"
os.remove("top_manager.pkl")


def test_topology_manager_save_top_file(setup_default_ff_tests_post_charges):
default_ff_object = setup_default_ff_tests_post_charges
default_ff_object._get_partial_charges()
default_ff_object._generate_ff_topologies()

tp_manager = TopologyManager(
topology_dir="output_2", topology_object="top_manager.pkl"
)
tp_manager.add_topology(
"mop_tetramer.build",
[
"output/mop_dimer_charges.sdf",
"output/mop_dimer_openff-2.0.0.gro",
"output/mop_dimer_openff-2.0.0.top",
"output/mop_dimer_renum.pdb",
],
)

assert os.path.exists("output_2/top_manager.pkl")
assert os.path.exists("output_2/mop_dimer_charges.sdf")
assert os.path.exists("output_2/mop_dimer_openff-2.0.0.gro")
assert os.path.exists("output_2/mop_dimer_openff-2.0.0.top")
assert os.path.exists("output_2/mop_dimer_renum.pdb")

shutil.rmtree("output_2")


def test_topology_manager_save_top_file(setup_default_ff_tests_post_charges):
default_ff_object = setup_default_ff_tests_post_charges
default_ff_object._get_partial_charges()
default_ff_object._generate_ff_topologies()

tp_manager = TopologyManager(
topology_dir="output_2", topology_object="top_manager.pkl"
)
tp_manager.add_topology(
"mop_tetramer.build",
[
"output/mop_dimer_charges.sdf",
"output/mop_dimer_openff-2.0.0.gro",
"output/mop_dimer_openff-2.0.0.top",
"output/mop_dimer_renum.pdb",
],
)

loaded_tp = TopologyManager(
topology_dir="output_2", topology_object="top_manager.pkl"
)
print(loaded_tp)

assert loaded_tp.topology_dictionary.keys() == tp_manager.topology_dictionary.keys()
shutil.rmtree("output_2")
assert os.path.exists("output/mop_dimer_openff-2.0.0.gro")
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@
import uuid
import yaml
import json
import pytest
from glob import glob
from terphenyl_simulations.build import (
TopologyManager,
TopologyManager, ROOT_DIR
) # Assuming this class is in a module named 'topology_manager'
import terphenyl_simulations


class TestTopologyManager(unittest.TestCase):
@pytest.fixture(autouse = True)
def navigate_to_test_dir(self):
top_dir = os.path.abspath("")
os.chdir(os.path.join(ROOT_DIR, "tests/test_topology_manager"))
yield
os.chdir(top_dir)

@patch("os.makedirs")
@patch("os.path.isdir", return_value=False)
Expand Down

0 comments on commit e7f016a

Please sign in to comment.