Skip to content

Commit

Permalink
fix: use collectFile to avoid filename collisions in prep_diffbind
Browse files Browse the repository at this point in the history
  • Loading branch information
kelly-sovacool committed Dec 5, 2023
1 parent f09db39 commit bb9c77b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 23 deletions.
3 changes: 2 additions & 1 deletion main.nf
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,12 @@ workflow CHIPSEQ {
if (params.contrasts) {
contrasts = file(params.contrasts, checkIfExists: true)
// TODO use consensus peaks for regions of interest in diffbind
bam_peaks = CALL_PEAKS.out.bam_peaks
CALL_PEAKS.out.bam_peaks
.combine(deduped_bam)
.map{meta1, bam1, bai1, peak, tool, meta2, bam2, bai2 ->
meta1.control == meta2.id ? [ meta1.sample_basename, meta1 + [tool: tool], bam1, bai1, peak, bam2, bai2 ] : null
}
.set{bam_peaks}
DIFF( bam_peaks, INPUT_CHECK.out.csv, contrasts )

}
Expand Down
4 changes: 3 additions & 1 deletion modules/local/check_contrasts/templates/check_contrasts.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ main <- function(contrasts_filename = "${contrasts}",
)
}

#' Ensure contrast has exactly two groups to compare
#' Check an individual contrast comparison
check_contrast <- function(contrast_name, contrasts_lst) {
# Ensure contrast has exactly two groups to compare
contrast_len <- length(contrasts_lst[[contrast_name]])
assert_that(
contrast_len == 2,
msg = glue("Contrasts must have only two groups per comparison, but {contrast_name} has {contrast_len}")
)
# TODO: check that each sample only appears in one group
}

#' Combine sample sheet with contrast group
Expand Down
26 changes: 11 additions & 15 deletions modules/local/diffbind/prep/main.nf
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
process PREP_DIFFBIND {
tag { "${metas[0].contrast}.${metas[0].tool}" }
tag { "${meta.id}.${meta.contrast}.${meta.tool}" }
label 'process_single'

container "${params.containers.base}"

input:
tuple val(metas), path(bams), path(bais), path(peaks), path(ctrl_bams), path(ctrl_bais)
tuple val(meta), path(bam), path(bai), path(peak), path(ctrl_bam), path(ctrl_bai)

output:
tuple val(new_meta), path("*.csv"), emit: csv
tuple val(meta), path("*.csv"), emit: csv

script:
assert metas*.contrast.toSet().size() == 1
assert metas*.tool.toSet().size() == 1
new_meta = [contrast: metas[0].contrast, tool: metas[0].tool]
def csv_text = [['SampleID', "Replicate", 'Condition', 'bamReads', "ControlID", "bamControl", 'Peaks', 'PeakCaller']]
[metas, bams, bais, peaks]
.transpose()
.each { meta, bam, bai, peak ->
csv_text.add([meta.id, meta.rep, meta.group, bam, meta.control, ctrlbam, peak, meta.tool])
}
csv_text = csv_text*.join(',').join(System.lineSeparator)
def csv_text = [
['SampleID', "Replicate", 'Condition', 'bamReads', "ControlID", "bamControl", 'Peaks', 'PeakCaller'],
[meta.id, meta.rep, meta.group, bam, meta.control, ctrl_bam, peak, meta.tool]
]*.join(',').join(System.lineSeparator)
"""
echo -ne "${csv_text}" > ${metas[0].contrast}.${metas[0].tool}.csv
echo -ne "${csv_text}" > ${meta.contrast}.${meta.tool}.${meta.id}.csv
"""

stub:
"""
touch ${metas[0].contrast}.${metas[0].tool}.csv
touch ${meta.contrast}.${meta.tool}.${meta.id}.csv
"""
}
11 changes: 5 additions & 6 deletions subworkflows/local/diff/main.nf
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,15 @@ workflow DIFF {
bam_peaks
.combine( contrasts )
.map{ sample_basename1, peak_meta, bam, bai, peak, ctrl_bam, ctrl_bai, sample_basename2, con_meta ->
sample_basename1 == sample_basename2 ? [ "${meta.contrast}.${meta.tool}", peak_meta + con_meta, bam, bai, peak, ctrl_bam, ctrl_bai ] : null
sample_basename1 == sample_basename2 ? [ peak_meta + con_meta, bam, bai, peak, ctrl_bam, ctrl_bai ] : null
}
.unique()
.groupTuple()
.map{ it -> // drop meta.contrast
it[1..-1]
}
.set{ ch_peaks_contrasts }
ch_peaks_contrasts | view
ch_peaks_contrasts | PREP_DIFFBIND
PREP_DIFFBIND.out.csv
.collectFile(storeDir: "${params.outdir}/diffbind/contrasts") { meta, row ->
[ "${meta.contrast}.${meta.tool}.csv", row + '\n' ]
}
emit:
diff_peaks = bam_peaks

Expand Down

0 comments on commit bb9c77b

Please sign in to comment.