Skip to content

Commit

Permalink
Write one Cytoscape session per dataset
Browse files Browse the repository at this point in the history
  • Loading branch information
agitter committed Sep 8, 2023
1 parent bd6c7d7 commit fa8a8e5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 29 deletions.
10 changes: 5 additions & 5 deletions Snakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down
24 changes: 8 additions & 16 deletions docker-wrappers/Cytoscape/cytoscape_util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import argparse
import os
import subprocess
import time
from typing import List
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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')

Expand All @@ -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__':
Expand Down
15 changes: 7 additions & 8 deletions src/analysis/cytoscape.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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:
Expand Down

0 comments on commit fa8a8e5

Please sign in to comment.