From 4f7f37d5dee85a3dceac0eea9b0f6ed695638d11 Mon Sep 17 00:00:00 2001 From: Victor Reys Date: Wed, 6 Sep 2023 08:43:52 +0200 Subject: [PATCH 1/5] create textual output --- src/arctic3d/cli_resclust.py | 41 ++++++++++++++++++++++++++++++------ tests/test_cli_resclust.py | 20 ++++++++++++++++++ 2 files changed, 54 insertions(+), 7 deletions(-) diff --git a/src/arctic3d/cli_resclust.py b/src/arctic3d/cli_resclust.py index 7b70459..0c8c9d5 100644 --- a/src/arctic3d/cli_resclust.py +++ b/src/arctic3d/cli_resclust.py @@ -23,6 +23,8 @@ `linkage` : the linkage strategy. `criterion` : the criterion to extract the clusters. + + `output` : the path where to output clusters data. """ import argparse import sys @@ -36,6 +38,7 @@ get_clustering_dict, ) from arctic3d.modules.input import Input +from arctic3d.modules.output import create_output_folder argument_parser = argparse.ArgumentParser() @@ -88,6 +91,13 @@ "--chain", help="Segment ID to be considered", required=False ) +argument_parser.add_argument( + "--output", + help="Path to the generated output dictionary", + type=str, + required=False, +) + def load_args(arguments): """ @@ -128,7 +138,7 @@ def maincli(): cli(argument_parser, main) -def main(input_arg, residue_list, chain, threshold, linkage, criterion): +def main(input_arg, residue_list, chain, threshold, linkage, criterion, output): """Main function.""" log.setLevel("INFO") @@ -192,14 +202,31 @@ def main(input_arg, residue_list, chain, threshold, linkage, criterion): ) cl_dict = get_clustering_dict(clusters, unique_sorted_resids) - for el in cl_dict.keys(): - log.info( - f"cluster {el}, residues" - f" {' '.join([str(res) for res in cl_dict[el]])}" - ) else: log.info("Only one residue, no clustering performed.") - log.info(f"cluster 1, residues {unique_sorted_resids[0]}") + # fake cluster dict with only one entry + cl_dict = {1: unique_sorted_resids} + + # log data + for el in cl_dict.keys(): + log.info( + f"cluster {el}, residues" + f" {' '.join([str(res) for res in cl_dict[el]])}" + ) + + # check if data must be flushed to output file + if output: + # initiate output directory + output_basepath = create_output_folder(output, uniprot_id='resclust') + # write txt file + log.info(f'writing clusters data in "{output_basepath}/Clusters.txt"') + with open(f'{output_basepath}/clustered_residues.out', 'w') as filout: + for el in cl_dict.keys(): + filout.write( + f"cluster {el} -> " + f"{' '.join([str(res) for res in cl_dict[el]])}" + "\n" + ) if __name__ == "__main__": diff --git a/tests/test_cli_resclust.py b/tests/test_cli_resclust.py index 7ac322a..486d37c 100644 --- a/tests/test_cli_resclust.py +++ b/tests/test_cli_resclust.py @@ -2,6 +2,9 @@ import pytest +import os +import shutil + from arctic3d.cli_resclust import main from . import golden_data @@ -21,6 +24,7 @@ def test_resclust_cli(example_pdbpath): 7.0, "average", "distance", + None, ) @@ -33,6 +37,7 @@ def test_wrong_residue_list(example_pdbpath): 9.0, "average", "distance", + None, ) assert e.type == SystemExit assert e.value.code == 1 @@ -46,4 +51,19 @@ def test_resclust_maxclust(example_pdbpath): 2, "average", "maxclust", + None, + ) + +def test_resclust_genoutput(example_pdbpath): + main( + example_pdbpath, + "100,101,102,133,134,135", + None, + 2, + "average", + "maxclust", + "resclustout", ) + assert os.path.exists("resclustout") == True + assert os.path.exists("resclustout/clustered_residues.out") == True + shutil.rmtree("resclustout") From 7138ea96272a40880056820c30acd308b1eede71 Mon Sep 17 00:00:00 2001 From: Victor Reys <132575181+VGPReys@users.noreply.github.com> Date: Wed, 6 Sep 2023 08:51:25 +0200 Subject: [PATCH 2/5] test_cli_resclust.py lint --- tests/test_cli_resclust.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_cli_resclust.py b/tests/test_cli_resclust.py index 486d37c..c1e68fc 100644 --- a/tests/test_cli_resclust.py +++ b/tests/test_cli_resclust.py @@ -54,6 +54,7 @@ def test_resclust_maxclust(example_pdbpath): None, ) + def test_resclust_genoutput(example_pdbpath): main( example_pdbpath, From c91ff93e8c1951429d072a968bb5c26c80ab29be Mon Sep 17 00:00:00 2001 From: Victor Reys <132575181+VGPReys@users.noreply.github.com> Date: Wed, 6 Sep 2023 08:56:21 +0200 Subject: [PATCH 3/5] Filename matching issue --- src/arctic3d/cli_resclust.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/arctic3d/cli_resclust.py b/src/arctic3d/cli_resclust.py index 0c8c9d5..618929c 100644 --- a/src/arctic3d/cli_resclust.py +++ b/src/arctic3d/cli_resclust.py @@ -219,8 +219,9 @@ def main(input_arg, residue_list, chain, threshold, linkage, criterion, output): # initiate output directory output_basepath = create_output_folder(output, uniprot_id='resclust') # write txt file - log.info(f'writing clusters data in "{output_basepath}/Clusters.txt"') - with open(f'{output_basepath}/clustered_residues.out', 'w') as filout: + output_fname = f'{output_basepath}/clustered_residues.out' + log.info(f'writing clusters data in "{output_fname}"') + with open(output_fname, 'w') as filout: for el in cl_dict.keys(): filout.write( f"cluster {el} -> " From 061b36600789e5621f69c990cd3cf45a05418e12 Mon Sep 17 00:00:00 2001 From: Victor Reys <132575181+VGPReys@users.noreply.github.com> Date: Wed, 6 Sep 2023 09:05:33 +0200 Subject: [PATCH 4/5] test_cli_resclust.py lint --- tests/test_cli_resclust.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_cli_resclust.py b/tests/test_cli_resclust.py index c1e68fc..55c225f 100644 --- a/tests/test_cli_resclust.py +++ b/tests/test_cli_resclust.py @@ -65,6 +65,6 @@ def test_resclust_genoutput(example_pdbpath): "maxclust", "resclustout", ) - assert os.path.exists("resclustout") == True - assert os.path.exists("resclustout/clustered_residues.out") == True + assert os.path.exists("resclustout") is True + assert os.path.exists("resclustout/clustered_residues.out") is True shutil.rmtree("resclustout") From 93fa9e1f88c2a0a9dc5b14f5e9356cde5b261099 Mon Sep 17 00:00:00 2001 From: Victor Reys <132575181+VGPReys@users.noreply.github.com> Date: Wed, 6 Sep 2023 10:24:23 +0200 Subject: [PATCH 5/5] Update cli_resclust.py lint --- src/arctic3d/cli_resclust.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/arctic3d/cli_resclust.py b/src/arctic3d/cli_resclust.py index 618929c..9afccf4 100644 --- a/src/arctic3d/cli_resclust.py +++ b/src/arctic3d/cli_resclust.py @@ -138,7 +138,15 @@ def maincli(): cli(argument_parser, main) -def main(input_arg, residue_list, chain, threshold, linkage, criterion, output): +def main( + input_arg, + residue_list, + chain, + threshold, + linkage, + criterion, + output, +): """Main function.""" log.setLevel("INFO")