Skip to content

Commit

Permalink
merging lanes from baminput
Browse files Browse the repository at this point in the history
  • Loading branch information
jbv2 committed Jan 31, 2025
1 parent 0ceda2c commit 9b1cd04
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 7 deletions.
58 changes: 58 additions & 0 deletions subworkflows/local/merge_lanes_inputbam.nf
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//
// Prepare reference indexing for downstream
//

include { SAMTOOLS_MERGE as SAMTOOLS_MERGE_LANES } from '../../modules/nf-core/samtools/merge/main'
include { SAMTOOLS_SORT as SAMTOOLS_SORT_MERGED_LANES } from '../../modules/nf-core/samtools/sort/main'
include { SAMTOOLS_INDEX as SAMTOOLS_INDEX_MERGED_LANES } from '../../modules/nf-core/samtools/index/main'
include { SAMTOOLS_FLAGSTAT as SAMTOOLS_FLAGSTAT_MAPPED } from '../../modules/nf-core/samtools/flagstat/main'

workflow MERGE_LANES_INPUTBAM {
take:
bams // [ [meta], [bams] [bais]]

main:
ch_versions = Channel.empty()
ch_multiqc_files = Channel.empty()


ch_input_bam = bams

ch_input_for_lane_merge = bams
.map { meta, bam, bai -> [ meta.clone().findAll{ it.key !in ['lane', 'colour_chemistry', 'shard_number'] }, bam ] }
.groupTuple()
.branch {
meta, bam ->
merge: bam.size() > 1
skip: true
}

SAMTOOLS_MERGE_LANES ( ch_input_for_lane_merge.merge, [[], []], [[], []] )
ch_versions.mix( SAMTOOLS_MERGE_LANES.out.versions )

// Then mix back merged and single lane libraries for everything downstream
ch_mergedlanes_for_sorting = ch_input_for_lane_merge.skip
.mix( SAMTOOLS_MERGE_LANES.out.bam )

SAMTOOLS_SORT_MERGED_LANES ( ch_mergedlanes_for_sorting )
ch_mapped_bam = SAMTOOLS_SORT_MERGED_LANES.out.bam
ch_versions.mix( SAMTOOLS_SORT_MERGED_LANES.out.versions )

SAMTOOLS_INDEX_MERGED_LANES( ch_mapped_bam )
ch_mapped_bai = params.fasta_largeref ? SAMTOOLS_INDEX_MERGED_LANES.out.csi : SAMTOOLS_INDEX_MERGED_LANES.out.bai
ch_versions.mix( SAMTOOLS_INDEX_MERGED_LANES.out.versions )

ch_input_for_flagstat = SAMTOOLS_SORT_MERGED_LANES.out.bam.join( SAMTOOLS_INDEX_MERGED_LANES.out.bai, failOnMismatch: true )

SAMTOOLS_FLAGSTAT_MAPPED ( ch_input_for_flagstat )
ch_versions.mix( SAMTOOLS_FLAGSTAT_MAPPED.out.versions.first() )
ch_multiqc_files = ch_multiqc_files.mix( SAMTOOLS_FLAGSTAT_MAPPED.out.flagstat )

emit:
bam = ch_mapped_bam // [ [ meta ], bam ]
bai = ch_mapped_bai // [ [ meta ], bai ]
flagstat = SAMTOOLS_FLAGSTAT_MAPPED.out.flagstat // [ [ meta ], stats ]
mqc = ch_multiqc_files
versions = ch_versions

}
21 changes: 14 additions & 7 deletions workflows/eager.nf
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ include { addNewMetaFromAttributes } from '../subworkflows/local/utils_nfcore_ea
include { REFERENCE_INDEXING } from '../subworkflows/local/reference_indexing'
include { PREPROCESSING } from '../subworkflows/local/preprocessing'
include { MAP } from '../subworkflows/local/map'
include { MERGE_LANES_INPUTBAM } from '../subworkflows/local/merge_lanes_inputbam'
include { FILTER_BAM } from '../subworkflows/local/bamfiltering.nf'
include { DEDUPLICATE } from '../subworkflows/local/deduplicate'
include { MANIPULATE_DAMAGE } from '../subworkflows/local/manipulate_damage'
Expand Down Expand Up @@ -206,17 +207,23 @@ workflow EAGER {

//
// MODULE: flagstats of user supplied input BAMs
//
SAMTOOLS_FLAGSTATS_BAM_INPUT ( ch_bams_from_input )
ch_versions = ch_versions.mix( SAMTOOLS_FLAGSTATS_BAM_INPUT.out.versions )
ch_flagstat_input_bam = SAMTOOLS_FLAGSTATS_BAM_INPUT.out.flagstat // For endorspy
// Maybe remove if subworkflow is already running it?
// SAMTOOLS_FLAGSTATS_BAM_INPUT ( ch_bams_from_input )
// ch_versions = ch_versions.mix( SAMTOOLS_FLAGSTATS_BAM_INPUT.out.versions )
// ch_flagstat_input_bam = SAMTOOLS_FLAGSTATS_BAM_INPUT.out.flagstat // For endorspy

// SUBWORKFLOW: Merging lanes for ch_bams_from_input

MERGE_LANES_INPUTBAM(ch_bams_from_input)
ch_bams_from_input_lanemerged = MERGE_LANES_INPUTBAM.out.bam
.join(MERGE_LANES_INPUTBAM.out.bai)

} else {
ch_bams_from_input = Channel.empty()
ch_flagstat_input_bam = Channel.empty()
}


//
// SUBWORKFLOW: bam filtering (length, mapped/unmapped, quality etc.)
//
Expand All @@ -225,7 +232,7 @@ workflow EAGER {

ch_mapped_for_bamfilter = MAP.out.bam
.join(MAP.out.bai)
.mix(ch_bams_from_input)
.mix(ch_bams_from_input_lanemerged)

FILTER_BAM ( ch_mapped_for_bamfilter )
ch_bamfiltered_for_deduplication = FILTER_BAM.out.genomics
Expand All @@ -236,7 +243,7 @@ workflow EAGER {
} else {
ch_bamfiltered_for_deduplication = MAP.out.bam
.join(MAP.out.bai)
.mix(ch_bams_from_input)
.mix(ch_bams_from_input_lanemerged)
}

ch_reads_for_deduplication = ch_bamfiltered_for_deduplication
Expand Down Expand Up @@ -397,7 +404,7 @@ workflow EAGER {
//

ch_flagstat_for_endorspy_raw = MAP.out.flagstat
.mix( ch_flagstat_input_bam )
.mix( MERGE_LANES_INPUTBAM.out.flagstat )

if ( params.run_bamfiltering & !params.skip_deduplication ) {
ch_for_endorspy = ch_flagstat_for_endorspy_raw
Expand Down

0 comments on commit 9b1cd04

Please sign in to comment.