diff --git a/mdpath/src/cluster.py b/mdpath/src/cluster.py index 1ed96a2..ad43673 100644 --- a/mdpath/src/cluster.py +++ b/mdpath/src/cluster.py @@ -117,7 +117,7 @@ def pathways_cluster( linkage_matrix = hierarchy.linkage(distance_matrix.values, method="complete") silhouette_scores = [] - for n_clusters in range(2, len(overlap_matrix) + 1): + for n_clusters in range(2, len(overlap_matrix)): cluster_labels = hierarchy.fcluster( linkage_matrix, n_clusters, criterion="maxclust" ) diff --git a/mdpath/tests/test_cluster.py b/mdpath/tests/test_cluster.py index 51f7e5f..4143c7e 100644 --- a/mdpath/tests/test_cluster.py +++ b/mdpath/tests/test_cluster.py @@ -70,24 +70,24 @@ def setup_clustering(load_test_data): return PatwayClustering(df_close_res, top_pathways, num_parallel_processes) -#def test_pathways_cluster(setup_clustering): -# clustering = setup_clustering -# -# with patch("matplotlib.pyplot.savefig") as mock_savefig: -# clusters = clustering.pathways_cluster(n_top_clust=3)# -# -# assert isinstance(clusters, dict) -# assert all(isinstance(k, int) and isinstance(v, list) for k, v in clusters.items()) -# assert len(clusters) > 0 -# -# current_dir = os.path.dirname(os.path.abspath(__file__)) -# file_path = os.path.join(current_dir, "clusters.pkl") -# with open(file_path, "rb") as file: -# saved_clusters = pickle.load(file) -# -# assert clusters == saved_clusters -# -# mock_savefig.assert_called_once_with("clustered_paths.png") +def test_pathways_cluster(setup_clustering): + clustering = setup_clustering + + with patch("matplotlib.pyplot.savefig") as mock_savefig: + clusters = clustering.pathways_cluster(n_top_clust=3)# + + assert isinstance(clusters, dict) + assert all(isinstance(k, int) and isinstance(v, list) for k, v in clusters.items()) + assert len(clusters) > 0 + + current_dir = os.path.dirname(os.path.abspath(__file__)) + file_path = os.path.join(current_dir, "clusters.pkl") + with open(file_path, "rb") as file: + saved_clusters = pickle.load(file) + + assert clusters == saved_clusters + + mock_savefig.assert_called_once_with("clustered_paths.png") @pytest.fixture(scope="module") diff --git a/mdpath/tests/test_mdpath.py b/mdpath/tests/test_mdpath.py index e89196a..ef9f577 100644 --- a/mdpath/tests/test_mdpath.py +++ b/mdpath/tests/test_mdpath.py @@ -80,62 +80,62 @@ def test_mdpath_wrong_input(tmp_path): os.chdir(original_cwd) -#def test_mdpath_output_files(): -# script_dir = os.path.dirname(os.path.abspath(__file__)) -# project_root = os.path.dirname(script_dir) -# mdpath_dir = os.path.join(project_root, "mdpath") -# -# topology = os.path.join(script_dir, "test_topology.pdb") -# trajectory = os.path.join(script_dir, "test_trajectory.dcd") -# numpath = "10" -# bootstrap = "1" -# assert os.path.exists(topology), f"Topology file {topology} does not exist." -# assert os.path.exists(trajectory), f"Trajectory file {trajectory} does not exist." -# -# expected_files = [ -# os.path.join(script_dir, "first_frame.pdb"), -# os.path.join(script_dir, "nmi_df.csv"), -# os.path.join(script_dir, "output.txt"), -# os.path.join(script_dir, "residue_coordinates.pkl"), -# os.path.join(script_dir, "cluster_pathways_dict.pkl"), -# os.path.join(script_dir, "clusters_paths.json"), -# os.path.join(script_dir, "precomputed_clusters_paths.json"), -# os.path.join(script_dir, "quick_precomputed_clusters_paths.json"), -# os.path.join(script_dir, "bootstrap/bootstrap_sample_0.txt"), -# ] -# -# sys.path.insert(0, mdpath_dir) -# -# try: -# from mdpath.mdpath import main as mdpath_main -# except ImportError as e: -# raise ImportError(f"Error importing mdpath: {e}") -# -# original_cwd = os.getcwd() -# os.chdir(script_dir) -# -# try: -# sys.argv = [ -# "mdpath", -# "-top", -# topology, -# "-traj", -# trajectory, -# "-numpath", -# numpath, -# "-bs", -# bootstrap, -# "-lig", -# "272", -# ] -# -# mdpath_main() -# -# for file in expected_files: -# assert os.path.exists(file), f"Expected output file {file} not found." -# -# finally: -# for file in expected_files: -# if os.path.exists(file): -# os.remove(file) -# os.chdir(original_cwd) +def test_mdpath_output_files(): + script_dir = os.path.dirname(os.path.abspath(__file__)) + project_root = os.path.dirname(script_dir) + mdpath_dir = os.path.join(project_root, "mdpath") + + topology = os.path.join(script_dir, "test_topology.pdb") + trajectory = os.path.join(script_dir, "test_trajectory.dcd") + numpath = "10" + bootstrap = "1" + assert os.path.exists(topology), f"Topology file {topology} does not exist." + assert os.path.exists(trajectory), f"Trajectory file {trajectory} does not exist." + + expected_files = [ + os.path.join(script_dir, "first_frame.pdb"), + os.path.join(script_dir, "nmi_df.csv"), + os.path.join(script_dir, "output.txt"), + os.path.join(script_dir, "residue_coordinates.pkl"), + os.path.join(script_dir, "cluster_pathways_dict.pkl"), + os.path.join(script_dir, "clusters_paths.json"), + os.path.join(script_dir, "precomputed_clusters_paths.json"), + os.path.join(script_dir, "quick_precomputed_clusters_paths.json"), + os.path.join(script_dir, "bootstrap/bootstrap_sample_0.txt"), + ] + + sys.path.insert(0, mdpath_dir) + + try: + from mdpath.mdpath import main as mdpath_main + except ImportError as e: + raise ImportError(f"Error importing mdpath: {e}") + + original_cwd = os.getcwd() + os.chdir(script_dir) + + try: + sys.argv = [ + "mdpath", + "-top", + topology, + "-traj", + trajectory, + "-numpath", + numpath, + "-bs", + bootstrap, + "-lig", + "272", + ] + + mdpath_main() + + for file in expected_files: + assert os.path.exists(file), f"Expected output file {file} not found." + + finally: + for file in expected_files: + if os.path.exists(file): + os.remove(file) + os.chdir(original_cwd) diff --git a/mdpath/tests/test_mdpath_tools.py b/mdpath/tests/test_mdpath_tools.py index 43b8799..0efac57 100644 --- a/mdpath/tests/test_mdpath_tools.py +++ b/mdpath/tests/test_mdpath_tools.py @@ -265,40 +265,40 @@ def test_path_comparison(): os.remove(file_path) -#def test_multitraj_analysis(): -# script_dir = os.path.dirname(os.path.abspath(__file__)) -# current_directory = os.getcwd() -# topology = os.path.join(script_dir, "multitraj.pdb") -# multitraj_1 = os.path.join(script_dir, "top_pathways.pkl") -# -# original_stdout = sys.stdout -# sys.stdout = StringIO() -# -# generated_files = [] # Define an empty list for generated_files -# -# try: -# sys.argv = [ -# "mdpath_multitraj", -# "-top", -# topology, -# "-multitraj", -# multitraj_1, -# multitraj_1 -# ] -# with pytest.raises(SystemExit) as exc_info: -# multitraj_analysis() -# -# assert exc_info.value.code == 0, "The command failed with non-zero exit code" -# -# -# generated_files = glob.glob( -# os.path.join(current_directory, "multitraj_clusters_paths.json") -# ) -# assert ( -# len(generated_files) > 0 -# ), "No multitraj_clusters_paths.json file was generated." -# -# finally: -# sys.stdout = original_stdout -# for file_path in generated_files: -# os.remove(file_path) +def test_multitraj_analysis(): + script_dir = os.path.dirname(os.path.abspath(__file__)) + current_directory = os.getcwd() + topology = os.path.join(script_dir, "multitraj.pdb") + multitraj_1 = os.path.join(script_dir, "top_pathways.pkl") + + original_stdout = sys.stdout + sys.stdout = StringIO() + + generated_files = [] # Define an empty list for generated_files + + try: + sys.argv = [ + "mdpath_multitraj", + "-top", + topology, + "-multitraj", + multitraj_1, + multitraj_1 + ] + with pytest.raises(SystemExit) as exc_info: + multitraj_analysis() + + assert exc_info.value.code == 0, "The command failed with non-zero exit code" + + + generated_files = glob.glob( + os.path.join(current_directory, "multitraj_clusters_paths.json") + ) + assert ( + len(generated_files) > 0 + ), "No multitraj_clusters_paths.json file was generated." + + finally: + sys.stdout = original_stdout + for file_path in generated_files: + os.remove(file_path)