Skip to content

Commit

Permalink
Merge pull request #751 from haddocking/bugfix_traceback_contactmap
Browse files Browse the repository at this point in the history
Bugfix traceback contactmap
  • Loading branch information
mgiulini authored Dec 7, 2023
2 parents 0d58f01 + f4d51f8 commit 86c48dd
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 6 deletions.
2 changes: 1 addition & 1 deletion examples/analysis/contmap-test.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ mode = "local"
# Input molecules
molecules = ["../data/1a2k_r_u.pdb", "../data/1a2k_l_u.pdb"]
# Disable post-processing
postprocess = false
postprocess = true

# =================== #
# Workflow definition #
Expand Down
34 changes: 32 additions & 2 deletions src/haddock/clis/cli_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,35 @@

TRACK_FOLDER = "traceback" # name of the traceback folder

ANA_MODULES = ["caprieval", "seletop", "topoaa", "rmsdmatrix", "clustrmsd", "clustfcc"]

def get_steps_without_pdbs(run_dir, all_steps):
"""
Get the modules that do not produce PDB files.
Parameters
----------
run_dir : str or pathlib.Path
Path to the run directory.
all_steps : list
List of all the steps in the run directory.
Returns
-------
steps_without_pdbs : list
List of steps that did not produce PDB files.
"""
steps_without_pdbs = []
for step in all_steps:
if step.endswith("topoaa"):
steps_without_pdbs.append(step)
else:
step_dir = Path(run_dir, step)
if step_dir.is_dir():
pdbs = list(step_dir.glob("*.pdb*"))
if len(pdbs) == 0:
steps_without_pdbs.append(step)
return steps_without_pdbs


def get_ori_names(n: int, pdbfile: PDBFile, max_topo_len: int) -> tuple[list, int]:
Expand Down Expand Up @@ -155,7 +183,9 @@ def main(run_dir):
# get the module folders from the run_dir input
all_steps = get_module_steps_folders(Path(run_dir))
log.info(f"All_steps: {', '.join(all_steps)}")
sel_step = [st for st in all_steps if st.split("_")[1] not in ANA_MODULES]
ana_modules = get_steps_without_pdbs(run_dir, all_steps)
log.info(f"Modules not to be analysed: {', '.join(ana_modules)}")
sel_step = [st for st in all_steps if st not in ana_modules]
# check if there are steps to traceback
if len(sel_step) == 0:
log.info("No steps to trace back. Exiting.")
Expand Down
7 changes: 5 additions & 2 deletions src/haddock/libs/libworkflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,11 @@ def postprocess(self) -> None:
capri_steps.append(step.order) # type: ignore
# call cli_analyse (no need for capri_dicts, it's all precalculated)
cli_analyse("./", capri_steps, top_cluster=10, format=None, scale=None)
# call cli_traceback
cli_traceback("./")
# call cli_traceback. If it fails, it's not a big deal
try:
cli_traceback("./")
except Exception as e:
log.warning(f"Error running traceback: {e}")


class Workflow:
Expand Down
39 changes: 38 additions & 1 deletion tests/test_cli_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import os
import shutil
from pathlib import Path
import tempfile

import pandas as pd
import pytest

from haddock.clis.cli_traceback import main
from haddock.clis.cli_traceback import main, get_steps_without_pdbs

from . import golden_data

Expand Down Expand Up @@ -39,6 +40,12 @@ def test_main(rigid_json, flexref_json):
shutil.copy(rigid_json, os.path.join(step_dirs[0], "io.json"))
shutil.copy(flexref_json, os.path.join(step_dirs[1], "io.json"))

# create fake, empty pdb files
for i in range(1, 5):
open(os.path.join(step_dirs[0], f"rigidbody_{i}.pdb"), "w").close()
for i in range(1, 3):
open(os.path.join(step_dirs[1], f"flexref_{i}.pdb"), "w").close()

# run haddock3-traceback
main(run_dir)

Expand Down Expand Up @@ -82,3 +89,33 @@ def test_analysis():

# check traceback folder does not exist
assert not os.path.isdir(os.path.join(run_dir, "traceback"))


def test_get_steps_without_pdbs():
"""Test get_steps_without_pdbs."""
with tempfile.TemporaryDirectory(dir=".") as tmpdir:
# build fake run_dir
run_dir = Path(tmpdir)
steps = ["0_topoaa", "1_rigidbody", "2_caprieval"]

for st in steps:
os.mkdir(Path(run_dir, st))

# create fake, empty pdb files
fake_pdb_path = Path(run_dir, steps[1] , f"rigidbody_1.pdb")
open(fake_pdb_path, "w").close()

# get steps without pdbs
obs_steps = get_steps_without_pdbs(run_dir, steps)

# check steps without pdbs
exp_steps = ["0_topoaa", "2_caprieval"]
assert obs_steps == exp_steps

# now we remove the fake pdb file
os.remove(fake_pdb_path)

# get steps without pdbs
obs_steps = get_steps_without_pdbs(run_dir, steps)
exp_steps = ["0_topoaa", "1_rigidbody", "2_caprieval"]
assert obs_steps == exp_steps

0 comments on commit 86c48dd

Please sign in to comment.