/*
 * Picard MarkDuplicates, sort, index BAM file and run samtools stats, flagstat and idxstats
 */

params.markduplicates_options = [:]
params.samtools_options       = [:]
params.control_only           = false

include { PICARD_MARKDUPLICATES } from '../../modules/nf-core/modules/picard/markduplicates/main' addParams( options: params.markduplicates_options )
include { SAMTOOLS_INDEX        } from '../../modules/nf-core/modules/samtools/index/main'        addParams( options: params.samtools_options       )
include { BAM_STATS_SAMTOOLS    } from './bam_stats_samtools'                                     addParams( options: params.samtools_options       )

workflow MARK_DUPLICATES_PICARD {
    take:
    bam // channel: [ val(meta), [ bam ] ]

    main:
    /*
    * Picard MarkDuplicates
    */
    ch_bam      = Channel.empty()
    metrics     = Channel.empty()
    ch_versions = Channel.empty()
    if( !params.control_only ) {
        PICARD_MARKDUPLICATES ( bam )
        ch_bam      = PICARD_MARKDUPLICATES.out.bam
        metrics     = PICARD_MARKDUPLICATES.out.metrics
        ch_versions = ch_versions.mix(PICARD_MARKDUPLICATES.out.versions)
    }
    else { // Split out non igg files and run only on these
        bam.branch { it ->
            target: it[0].group != 'igg'
            control: it[0].group == 'igg'
        }
        .set { ch_split }

        PICARD_MARKDUPLICATES ( ch_split.control )
        ch_bam      = PICARD_MARKDUPLICATES.out.bam.mix ( ch_split.target )
        metrics     = PICARD_MARKDUPLICATES.out.metrics
        ch_versions = ch_versions.mix(PICARD_MARKDUPLICATES.out.versions)
    }
    //out_bam | view

    /*
    * Index BAM file
    */
    SAMTOOLS_INDEX ( ch_bam )
    ch_versions = ch_versions.mix(SAMTOOLS_INDEX.out.versions)

    // Join bam/bai
    ch_bam
        .map { row -> [row[0].id, row ].flatten()}
        .join ( SAMTOOLS_INDEX.out.bai.map { row -> [row[0].id, row ].flatten()} )
        .map { row -> [row[1], row[2], row[4]] }
        .set { ch_bam_bai }
    //ch_bam_bai | view

    /*
    * Run samtools stats, flagstat and idxstats
    */
    BAM_STATS_SAMTOOLS ( ch_bam_bai )
    ch_versions = ch_versions.mix(BAM_STATS_SAMTOOLS.out.versions)

    emit:
    bam      = ch_bam                            // channel: [ val(meta), [ bam ] ]
    metrics                                      // channel: [ val(meta), [ metrics ] ]

    bai      = SAMTOOLS_INDEX.out.bai            // channel: [ val(meta), [ bai ] ]
    stats    = BAM_STATS_SAMTOOLS.out.stats      // channel: [ val(meta), [ stats ] ]
    flagstat = BAM_STATS_SAMTOOLS.out.flagstat   // channel: [ val(meta), [ flagstat ] ]
    idxstats = BAM_STATS_SAMTOOLS.out.idxstats   // channel: [ val(meta), [ idxstats ] ]

    versions = ch_versions                       // channel: [ versions.yml ]
}
