diff --git a/bin/bigbacter_report_cluster.py b/bin/bigbacter_report_cluster.py index 3a834e9..59f142c 100755 --- a/bin/bigbacter_report_cluster.py +++ b/bin/bigbacter_report_cluster.py @@ -174,6 +174,29 @@ def _dist2csv(data, outfile): writer.writerow(row) +def _dist2csv_long(data, outfile): + """Convert distance matrix to long format with three columns: sample1, sample2, distance.""" + samples = sorted(data.keys()) + + with open(outfile, "w", newline="", encoding="utf-8") as f: + writer = csv.DictWriter(f, fieldnames=['sample1', 'sample2', 'distance']) + writer.writeheader() + + for i, s1 in enumerate(samples): + if s1 not in data: + continue + + for j, s2 in enumerate(samples): + # Only write the upper triangle (including diagonal) + if j >= i: + dist = data[s1].get(s2, 0 if s1 == s2 else None) + if dist is not None: + writer.writerow({ + 'sample1': s1, + 'sample2': s2, + 'distance': dist + }) + def _find_file(indir: Path, pattern: str) -> Optional[Path]: """Find first file matching glob pattern in directory.""" matches = list(indir.glob(pattern)) @@ -252,13 +275,18 @@ def process_run(indir: Path, args: argparse.Namespace) -> None: # ---------------------------- # SNP distance matrix # ---------------------------- - snp_dist_out = Path(outdir) / f"{args.prefix}_snp-dist{args.suffix}csv" + # Determine the naming convention based on recombination masking + dist_suffix = "_masked" if args.recomb_masked else "" + snp_dist_out_wide = Path(outdir) / f"{args.prefix}{dist_suffix}-dist_wide{args.suffix}csv" + snp_dist_out_long = Path(outdir) / f"{args.prefix}{dist_suffix}-dist_long{args.suffix}csv" + if snp_dist_file: snp_dist_data = _list2map(_load_delim(snp_dist_file), key='name') _rename_reference(snp_dist_data, ref_name, depth=2) - _dist2csv(snp_dist_data, outfile=snp_dist_out) + _dist2csv(snp_dist_data, outfile=snp_dist_out_wide) + _dist2csv_long(snp_dist_data, outfile=snp_dist_out_long) _extend_dict(out, _find_links(snp_dist_data, args.strong_link, args.inter_link)) - LOGGER.info(f"Processed SNP distance matrix from {snp_dist_file}") + LOGGER.info(f"Processed SNP distance matrix from {snp_dist_file} (wide and long formats)") # ---------------------------- # MinHash containment distance @@ -373,7 +401,7 @@ def process_run(indir: Path, args: argparse.Namespace) -> None: mr_map = { "summary_file": {"pane_id": "table-1", "file": summary_out}, "snp_tree_file": {"pane_id": "tree-1", "file": snp_tree_out}, - "snp_dist_file": {"pane_id": "matrix-1", "file": snp_dist_out}, + "snp_dist_file": {"pane_id": "matrix-1", "file": snp_dist_out_wide}, "mh_dist_file": {"pane_id": "matrix-2", "file": mh_dist_out}, } @@ -432,7 +460,8 @@ def main() -> None: Expected input directory structure: - samplesheet.csv (required) - summary.csv (SNP summary stats) - - dist_wide.csv (SNP distance matrix) + - dist_wide.csv (SNP distance matrix, wide format) + - dist_long.csv (SNP distance matrix, long format) - optional - core.aln.* (Newick tree file) - *.tsv.gz (K-mer distance matrix) - template.microreact (optional, for Microreact output) diff --git a/conf/modules.config b/conf/modules.config index 092e857..d14bb69 100644 --- a/conf/modules.config +++ b/conf/modules.config @@ -103,7 +103,9 @@ process { [ pattern: 'core.aln', path: { clusterPath(meta, D.aln) }, saveAs: { fn -> update_filename(fn, meta) } ], [ pattern: 'core.full.aln', path: { clusterPath(meta, D.aln) }, saveAs: { fn -> update_filename(fn, meta, label="_full") } ], [ pattern: 'full.csv', path: { clusterPath(meta, D.aln) }, saveAs: { fn -> update_filename(fn, meta, label="_full") } ], - [ pattern: '*.html', path: { clusterPath(meta, D.qc) }, saveAs: { fn -> update_filename(fn, meta, label="_cg-plot") } ] + [ pattern: '*.html', path: { clusterPath(meta, D.qc) }, saveAs: { fn -> update_filename(fn, meta, label="_cg-plot") } ], + [ pattern: 'dist_long.csv', path: { clusterPath(meta, D.dist) }, saveAs: { fn -> update_filename(fn, meta, suffix="_dist_long") } ], + [ pattern: 'dist_wide.csv', path: { clusterPath(meta, D.dist) }, saveAs: { fn -> update_filename(fn, meta, suffix="_dist_wide") } ] ] } withName: 'IQTREE' { @@ -111,13 +113,14 @@ process { stageInMode = 'copy' publishDir = OFF } + withName: 'CLUSTER' { container = 'public.ecr.aws/o8h2f0o1/bigbacter:2.0' publishDir = [ - [ pattern: '*.nwk', path: { clusterPath(meta, D.tree) } ], - [ pattern: '*_summary.*', path: { clusterPath(meta, D.summary) } ], - [ pattern: '*-dist.*', path: { clusterPath(meta, D.dist) } ], - [ pattern: '*.microreact', path: { clusterPath(meta, D.report) } ] + [ pattern: '*.nwk', path: { clusterPath(meta, D.tree) }, mode: params.publish_dir_mode ], + [ pattern: '*_summary.*', path: { clusterPath(meta, D.summary) }, mode: params.publish_dir_mode ], + [ pattern: '*-dist.*', path: { clusterPath(meta, D.dist) }, mode: params.publish_dir_mode ], + [ pattern: '*.microreact', path: { clusterPath(meta, D.report) }, mode: params.publish_dir_mode ] ] } diff --git a/subworkflows/local/report/main.nf b/subworkflows/local/report/main.nf index ae8bb55..ec9361f 100644 --- a/subworkflows/local/report/main.nf +++ b/subworkflows/local/report/main.nf @@ -48,13 +48,20 @@ workflow REPORT { def meta_updated = meta + [recomb_masked: recomb_masked] // Collect all available files (filter out null values) + // Note: snp_dist is now a list [dist_wide, dist_long] def all_files = [ snp_summary, - snp_dist, snp_tree, ref_meta, db_dist ].findAll { it } + + // Add dist files (may be a list) + if (snp_dist instanceof List) { + all_files.addAll(snp_dist) + } else if (snp_dist) { + all_files.add(snp_dist) + } [meta_updated, all_files] } diff --git a/subworkflows/local/variants/main.nf b/subworkflows/local/variants/main.nf index 71d62d3..bb8feff 100644 --- a/subworkflows/local/variants/main.nf +++ b/subworkflows/local/variants/main.nf @@ -221,6 +221,8 @@ workflow VARIANTS { snp_files = SNIPPY_SINGLE.out.results snp_summary = POLYCORE.out.csv snp_dist = POLYCORE.out.dist_wide + .join(POLYCORE.out.dist_long) + .map { meta, dist_wide, dist_long -> [meta, [dist_wide, dist_long]] } snp_tree = IQTREE.out.result versions = ch_versions } diff --git a/tower.yml b/tower.yml index 65c833e..b006eea 100644 --- a/tower.yml +++ b/tower.yml @@ -1,7 +1,9 @@ reports: "**/summary/*": - display: "Summarry file (cluster)" - "**/report/*": + display: "Summary file (cluster)" + "**/*-summary.csv": + display: "Combined summary file (run)" + "**/reports/*": display: "Microreact report (cluster)" "**/aln/*": display: "Genome alignment file (cluster)"