Skip to content

Commit

Permalink
files to ckpt
Browse files Browse the repository at this point in the history
  • Loading branch information
SamCox822 committed Mar 18, 2024
1 parent 6748c9c commit a8502e9
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 29 deletions.
4 changes: 3 additions & 1 deletion mdagent/tools/base_tools/analysis_tools/rgy.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ def rad_gyration_per_frame(self, pdb_id: str) -> str:
self._load_traj(pdb_id)
rg_per_frame = md.compute_rg(self.traj)

self.rgy_file = f"files/radii_of_gyration_{self.pdb_id}.csv"
self.rgy_file = (
f"{self.path_registry}/files/radii_of_gyration_{self.pdb_id}.csv"
)

np.savetxt(
self.rgy_file, rg_per_frame, delimiter=",", header="Radius of Gyration (nm)"
Expand Down
2 changes: 1 addition & 1 deletion mdagent/tools/base_tools/preprocess_tools/clean_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ def _run(self, **input_args) -> str:
# version += 1
#
# file_name = f"tidy_{name}v{version}.{end}"
directory = "files/pdb"
directory = f"{self.path_registry}/files/pdb"
if not os.path.exists(directory):
os.makedirs(directory)
if end == "pdb":
Expand Down
6 changes: 4 additions & 2 deletions mdagent/tools/base_tools/preprocess_tools/packing.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ def generate_input_header(self) -> None:
]
)
)
while os.path.exists(f"files/pdb/{_final_name}_v{self.file_number}.pdb"):
while os.path.exists(
f"{self.path_registry}/pdb/{_final_name}_v{self.file_number}.pdb"
):
self.file_number += 1

self.final_name = f"{_final_name}_v{self.file_number}.pdb"
Expand Down Expand Up @@ -155,7 +157,7 @@ def run_packmol(self):
os.rename(self.final_name, f"files/pdb/{self.final_name}")
self.path_registry.map_path(
f"PACKED_{time_stamp}",
f"files/pdb/{self.final_name}",
f"{self.path_registry}/files/pdb/{self.final_name}",
self.file_description,
)
# move file to files/pdb
Expand Down
4 changes: 2 additions & 2 deletions mdagent/tools/base_tools/preprocess_tools/pdb_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def get_pdb(query_string: str, path_registry: PathRegistry):
file_format=filetype,
)
file_id = path_registry.get_fileid(filename, FileType.PROTEIN)
directory = "files/pdb"
directory = "{self.path_registry}/files/pdb"
# Create the directory if it does not exist
if not os.path.exists(directory):
os.makedirs(directory)
Expand Down Expand Up @@ -184,7 +184,7 @@ def small_molecule_pdb(self, mol_str: str) -> str:
except Exception:
pass
Chem.AllChem.EmbedMolecule(m)
file_name = f"files/pdb/{mol_name}.pdb"
file_name = f"{self.path_registry}/files/pdb/{mol_name}.pdb"
Chem.MolToPDBFile(m, file_name)
self.path_registry.map_path(
mol_name, file_name, f"pdb file for the small molecule {mol_name}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def _run(self, *args, **input):
type=FileType.SIMULATION, Sim_id=base_script_id, modified=True
)
file_id = self.path_registry.get_fileid(filename, type=FileType.SIMULATION)
directory = "files/simulations"
directory = "{self.path_registry}/files/simulations"
if not os.path.exists(directory):
os.makedirs(directory)
with open(f"{directory}/{filename}", "w") as file:
Expand Down
22 changes: 15 additions & 7 deletions mdagent/tools/base_tools/simulation_tools/setup_and_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -793,9 +793,17 @@ def create_simulation(self):
)
)
self.registry_records = [
("holder", f"files/records/{trajectory_name}", traj_desc),
("holder", f"files/records/{log_name}", log_desc),
("holder", f"files/records/{topology_name}", top_desc),
(
"holder",
f"{self.path_registry}/files/records/{trajectory_name}",
traj_desc,
),
("holder", f"{self.path_registry}/files/records/{log_name}", log_desc),
(
"holder",
f"{self.path_registry}/files/records/{topology_name}",
top_desc,
),
]

# TODO add checkpoint too?
Expand Down Expand Up @@ -1128,7 +1136,7 @@ def remove_leading_spaces(text):
script_content = textwrap.dedent(script_content).strip()

# Write to file
directory = "files/simulations"
directory = "{self.path_registry}/files/simulations"
if not os.path.exists(directory):
os.makedirs(directory)

Expand Down Expand Up @@ -1272,15 +1280,15 @@ def _run(self, **input_args):
openmmsim.write_standalone_script(filename=file_name)
self.path_registry.map_path(
sim_id,
f"files/simulations/{file_name}",
f"{self.path_registry}/files/simulations/{file_name}",
f"Basic Simulation of Protein {pdb_id}",
)
if save:
records = openmmsim.registry_records
# move record files to files/records/
print(os.listdir("."))
if not os.path.exists("files/records"):
os.makedirs("files/records")
if not os.path.exists(f"{self.path_registry}/files/records"):
os.makedirs(f"{self.path_registry}/files/records")
for record in records:
os.rename(record[1].split("/")[-1], f"{record[1]}")
for record in records:
Expand Down
30 changes: 15 additions & 15 deletions tests/test_pdb_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ def test_getpdb(fibronectin, get_registry):
assert name.endswith(".pdb")


def test_small_molecule_pdb(molpdb):
def test_small_molecule_pdb(molpdb, get_registry):
# Test with a valid SMILES string
valid_smiles = "C1=CC=CC=C1" # Benzene
expected_output = (
"PDB file for C1=CC=CC=C1 successfully created and saved to "
"files/pdb/benzene.pdb."
)
assert molpdb.small_molecule_pdb(valid_smiles) == expected_output
assert os.path.exists("files/pdb/benzene.pdb")
os.remove("files/pdb/benzene.pdb") # Clean up
assert os.path.exists(f"{get_registry}/files/pdb/benzene.pdb")
os.remove(f"{get_registry}/files/pdb/benzene.pdb") # Clean up

# test with invalid SMILES string and invalid molecule name
invalid_smiles = "C1=CC=CC=C1X"
Expand All @@ -60,33 +60,33 @@ def test_small_molecule_pdb(molpdb):
"PDB file for water successfully created and " "saved to files/pdb/water.pdb."
)
assert molpdb.small_molecule_pdb(valid_name) == expected_output
assert os.path.exists("files/pdb/water.pdb")
os.remove("files/pdb/water.pdb") # Clean up
assert os.path.exists(f"{get_registry}/pdb/water.pdb")
os.remove(f"{get_registry}/files/pdb/water.pdb") # Clean up


def test_packmol_pdb_download_only(packmol):
def test_packmol_pdb_download_only(packmol, get_registry):
packmol.path_registry._remove_path_from_json("water")
packmol.path_registry._remove_path_from_json("benzene")
small_molecules = ["water", "benzene"]
packmol._get_sm_pdbs(small_molecules)
assert os.path.exists("files/pdb/water.pdb")
assert os.path.exists("files/pdb/benzene.pdb")
os.remove("files/pdb/water.pdb")
os.remove("files/pdb/benzene.pdb")
assert os.path.exists(f"{get_registry}/files/pdb/water.pdb")
assert os.path.exists(f"{get_registry}/files/pdb/benzene.pdb")
os.remove(f"{get_registry}/files/pdb/water.pdb")
os.remove(f"{get_registry}/files/pdb/benzene.pdb")


def test_packmol_download_only_once(packmol):
def test_packmol_download_only_once(packmol, get_registry):
packmol.path_registry._remove_path_from_json("water")
small_molecules = ["water"]
packmol._get_sm_pdbs(small_molecules)
assert os.path.exists("files/pdb/water.pdb")
water_time = os.path.getmtime("files/pdb/water.pdb")
assert os.path.exists(f"{get_registry}/files/pdb/water.pdb")
water_time = os.path.getmtime(f"{get_registry}/files/pdb/water.pdb")
time.sleep(5)

# Call the function again with the same molecule
packmol._get_sm_pdbs(small_molecules)
water_time_after = os.path.getmtime("files/pdb/water.pdb")
water_time_after = os.path.getmtime(f"{get_registry}/files/pdb/water.pdb")

assert water_time == water_time_after
# Clean up
os.remove("files/pdb/water.pdb")
os.remove(f"{get_registry}/files/pdb/water.pdb")

0 comments on commit a8502e9

Please sign in to comment.