Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions bin/bigbacter_report_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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},
}

Expand Down Expand Up @@ -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)
Expand Down
13 changes: 8 additions & 5 deletions conf/modules.config
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,24 @@ 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' {
container = "docker.io/staphb/iqtree2:2.4.0"
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 ]
]
}

Expand Down
9 changes: 8 additions & 1 deletion subworkflows/local/report/main.nf
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
Expand Down
2 changes: 2 additions & 0 deletions subworkflows/local/variants/main.nf
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
6 changes: 4 additions & 2 deletions tower.yml
Original file line number Diff line number Diff line change
@@ -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)"
Expand Down