Skip to content
Snippets Groups Projects
hicstuff_sub.nf 4.55 KiB

include { BOWTIE2_ALIGNMENT } from '../../modules/local/hicstuff/align_bowtie2'
include { FRAGMENT_ENZYME } from '../../modules/local/hicstuff/fragment_enzyme'
include { BAM2PAIRS } from '../../modules/local/hicstuff/bam2pairs'
include { BUILD_MATRIX } from '../../modules/local/hicstuff/build_matrix'
include { BUILD_MATRIX_COOL } from '../../modules/local/hicstuff/build_matrix_cool'
include { BUILD_MATRIX_COOL_ALT } from '../../modules/local/hicstuff/build_matrix_cool_alt'
include { FILTER_EVENT } from '../../modules/local/hicstuff/filter_event'
include { DISTANCE_LAW } from '../../modules/local/hicstuff/distance_law'
include { FILTER_PCR } from '../../modules/local/hicstuff/filter_pcr'
include { GATK4_MARKDUPLICATES } from '../../modules/nf-core/custom/gatk4/markduplicates/main'
include { SAMTOOLS_SORT } from '../../modules/nf-core/custom/samtools/sort/main'
include { SAMTOOLS_SORT_N } from '../../modules/nf-core/custom/samtools_n/sort/main'
include { FILTER_PAIR } from '../../modules/nf-core/custom/filterbam/main'
include { SAMTOOLS_INDEX } from '../../modules/nf-core/custom/samtools/index/main'
include { PICARD_MARKDUPLICATES } from '../../modules/nf-core/custom/picard/markduplicates/main'

// Paired-end to Single-end
def pairToSingle(row, mates) {
    def meta = row[0].clone()
    meta.single_end = true
    meta.mates = mates
    if (mates == "R1") {
        return [meta, [ row[1][0]] ]
    }else if (mates == "R2"){
        return [meta, [ row[1][1]] ]
    }
}

// Single-end to Paired-end
def singleToPair(row){
    def meta = row[0].clone()
    meta.remove('mates')
    meta.single_end = false
    return [ meta, row[1] ]
}

workflow HICSTUFF_SUB {

    take:
    reads
    index
    ligation_site
    digestion
    fasta
    chromosome_size

    main:

    // Align each mates separetly and add mates information in [meta]
    ch_reads_r1 = reads.map{ it -> pairToSingle(it,"R1") }
    ch_reads_r2 = reads.map{ it -> pairToSingle(it,"R2") }
    ch_reads = ch_reads_r1.concat(ch_reads_r2)

    BOWTIE2_ALIGNMENT(
        ch_reads,
        index.collect()
    )

    FRAGMENT_ENZYME(
        digestion,
        fasta
    )

    if (params.filter_pcr && params.filter_pcr_picard ){
        error "Error: filter_pcr and filter_pcr_picard can't both be true at the same time! Set one of them false in the config file"
    }
    else if (params.filter_pcr_picard){
        SAMTOOLS_SORT(
            BOWTIE2_ALIGNMENT.out.bam
        )


        PICARD_MARKDUPLICATES(
            SAMTOOLS_SORT.out.bam,
            fasta.collect(),
            index.collect()
        )

        SAMTOOLS_SORT_N(
            PICARD_MARKDUPLICATES.out.bam
        )

        SAMTOOLS_SORT_N.out.bam.set{ ch_bam }

        FILTER_PAIR(
        ch_bam.combine(ch_bam)
        .map {
            meta1, bam1, meta2, bam2 ->
                meta1.id == meta2.id && meta1.chunk == meta2.chunk && meta1.mates == "R1" && meta2.mates == "R2" ? [ meta1,  bam1,  meta2, bam2 ] : null
        })

        FILTER_PAIR.out.bam.set{ new_ch_bam }

    }
    else {

        BOWTIE2_ALIGNMENT.out.bam.set{ ch_bam }

        ch_bam.combine(ch_bam)
        .map {
            meta1, bam1, meta2, bam2 ->
                meta1.id == meta2.id && meta1.chunk == meta2.chunk && meta1.mates == "R1" && meta2.mates == "R2" ? [ meta1,  bam1,  meta2, bam2 ] : null
        }.set{ new_ch_bam }

    }

    BAM2PAIRS(
        new_ch_bam,
        FRAGMENT_ENZYME.out.info_contigs.collect(),
        digestion,
        fasta.collect()
    )
    BAM2PAIRS.out.idx_pairs.set{ ch_idx_pairs }

    if( params.filter_event ){
        FILTER_EVENT(
            ch_idx_pairs
        )
        FILTER_EVENT.out.idx_pairs_filtered.set{ ch_idx_pairs }
    }

    if ( params.distance_law ){
        DISTANCE_LAW(
            ch_idx_pairs,
            FRAGMENT_ENZYME.out.fragments_list.collect()
        )
    }

    if (params.filter_pcr && params.filter_pcr_picard ){
        error "Error: filter_pcr and filter_pcr_picard can't both be true at the same time! Set one of them false in the config file"
    }
    else if ( params.filter_pcr ){
        FILTER_PCR(
            ch_idx_pairs
        )
        FILTER_PCR.out.idx_pairs_pcrfree.set{ ch_idx_pairs }
    }

    //TODO rajouter filtres + distance law + filtres PCR en options
    // pour les PCR filter, soit le hicstuff soit PICARD

    BUILD_MATRIX(
        ch_idx_pairs,
        FRAGMENT_ENZYME.out.fragments_list.collect()
    )

    BUILD_MATRIX_COOL(
        ch_idx_pairs,
        FRAGMENT_ENZYME.out.fragments_list.collect()
    )

    BUILD_MATRIX_COOL_ALT(
        chromosome_size.collect(),
        ch_idx_pairs
    )
}