From fa8a8e58c8582de35e28a689685ee68872e0dc6d Mon Sep 17 00:00:00 2001 From: Anthony Gitter Date: Fri, 8 Sep 2023 17:42:38 -0500 Subject: [PATCH] Write one Cytoscape session per dataset --- Snakefile | 10 ++++----- docker-wrappers/Cytoscape/cytoscape_util.py | 24 +++++++-------------- src/analysis/cytoscape.py | 15 ++++++------- 3 files changed, 20 insertions(+), 29 deletions(-) diff --git a/Snakefile b/Snakefile index 585bd9f9..ffc5ee92 100644 --- a/Snakefile +++ b/Snakefile @@ -70,7 +70,7 @@ def make_final_input(wildcards): final_input.extend(expand('{out_dir}{sep}{dataset}-{algorithm_params}{sep}gsstyle.json',out_dir=out_dir,sep=SEP,dataset=dataset_labels,algorithm_params=algorithms_with_params)) if config["analysis"]["cytoscape"]["include"]: - final_input.extend(expand('{out_dir}{sep}cytoscape-session.cys',out_dir=out_dir,sep=SEP)) + final_input.extend(expand('{out_dir}{sep}{dataset}-cytoscape.cys',out_dir=out_dir,sep=SEP,dataset=dataset_labels)) if config["analysis"]["ml"]["include"]: final_input.extend(expand('{out_dir}{sep}{dataset}-pca.png',out_dir=out_dir,sep=SEP,dataset=dataset_labels,algorithm_params=algorithms_with_params)) @@ -238,13 +238,13 @@ rule viz_graphspace: graphspace.write_json(input.standardized_file,output.graph_json,output.style_json,directed=algorithm_directed[wildcards.algorithm]) -# Write Cytoscape session file with all pathways for all datasets +# Write a Cytoscape session file with all pathways for each dataset rule viz_cytoscape: - input: pathways = expand('{out_dir}{sep}{dataset}-{algorithm_params}{sep}pathway.txt', out_dir=out_dir, sep=SEP, dataset=dataset_labels, algorithm_params=algorithms_with_params) + input: pathways = expand('{out_dir}{sep}{{dataset}}-{algorithm_params}{sep}pathway.txt', out_dir=out_dir, sep=SEP, algorithm_params=algorithms_with_params) output: - session = SEP.join([out_dir, 'cytoscape-session.cys']) + session = SEP.join([out_dir, '{dataset}-cytoscape.cys']) run: - cytoscape.run_cytoscape_container(input.pathways, out_dir, SINGULARITY) + cytoscape.run_cytoscape_container(input.pathways, output.session, SINGULARITY) # Write a single summary table for all pathways for each dataset diff --git a/docker-wrappers/Cytoscape/cytoscape_util.py b/docker-wrappers/Cytoscape/cytoscape_util.py index 9860d0aa..ebb0b560 100644 --- a/docker-wrappers/Cytoscape/cytoscape_util.py +++ b/docker-wrappers/Cytoscape/cytoscape_util.py @@ -1,5 +1,4 @@ import argparse -import os import subprocess import time from typing import List @@ -29,19 +28,12 @@ def get_parser() -> argparse.ArgumentParser: ) parser.add_argument( - "--outdir", - dest='outdir', + "--output", + dest='output', type=str, - default='output', - help='The output directory of the session file. Default: output' - ) - - parser.add_argument( - "--outlabel", - dest='outlabel', - type=str, - default='cytoscape-session', - help='The output filename of the session file, which will have the extension .cys. Default: cytoscape-session' + default='cytoscape-session.cys', + help='The output filename of the Cytoscape session file, which will have the extension .cys added if it is not ' + 'already provided. Default: cytoscape-session.cys' ) return parser @@ -99,7 +91,7 @@ def parse_name(pathway: str) -> (str, str): return parts[0], parts[1] -def load_pathways(pathways: List[str], out_dir: str, out_label: str) -> None: +def load_pathways(pathways: List[str], output: str) -> None: if len(pathways) == 0: raise ValueError('One or more pathway files are required') @@ -113,12 +105,12 @@ def load_pathways(pathways: List[str], out_dir: str, out_label: str) -> None: ) p4c.networks.rename_network(name, network=suid) - p4c.session.save_session(os.path.join(out_dir, out_label)) + p4c.session.save_session(output) def main(): opts = parse_arguments() - load_pathways(opts.pathways, opts.outdir, opts.outlabel) + load_pathways(opts.pathways, opts.output) if __name__ == '__main__': diff --git a/src/analysis/cytoscape.py b/src/analysis/cytoscape.py index 99c6dfbe..87c7819b 100644 --- a/src/analysis/cytoscape.py +++ b/src/analysis/cytoscape.py @@ -5,7 +5,7 @@ from src.util import prepare_volume, run_container -def run_cytoscape_container(pathways: List[Union[str, PurePath]], out_dir: str, singularity: bool = False) -> None: +def run_cytoscape_container(pathways: List[Union[str, PurePath]], output_file: str, singularity: bool = False) -> None: """ 1. take pathways and create volume mappings 2. setup wrapper command @@ -21,7 +21,8 @@ def run_cytoscape_container(pathways: List[Union[str, PurePath]], out_dir: str, # Each volume is a tuple (src, dest) volumes = list() - cytoscape_output_dir = Path(out_dir, 'cytoscape').absolute() + # A temporary directory for Cytoscape output files + cytoscape_output_dir = Path(output_file.replace('.cys', '')).absolute() cytoscape_output_dir.mkdir(parents=True, exist_ok=True) # TODO update the latest p4cytoscape and use env variable to control the log directory instead @@ -30,14 +31,12 @@ def run_cytoscape_container(pathways: List[Union[str, PurePath]], out_dir: str, # Only needed when running in Singularity volumes.append((cytoscape_output_dir, PurePath(work_dir, 'CytoscapeConfiguration'))) - # Map the output directory - bind_path, mapped_out_dir = prepare_volume(out_dir, work_dir) + # Map the output file + bind_path, mapped_output = prepare_volume(output_file, work_dir) volumes.append(bind_path) - # Create the Python command to run inside the container - command = ['python', '/py4cytoscape/cytoscape_util.py', - '--outdir', mapped_out_dir, - '--outlabel', 'cytoscape-session'] + # Create the initial Python command to run inside the container + command = ['python', '/py4cytoscape/cytoscape_util.py', '--output', mapped_output] # Map the pathway filenames and add them to the Python command for pathway in pathways: