/*
========================================================================================
    Config file for defining DSL2 per module options
========================================================================================
    Available keys to override module options:
        args            = Additional arguments appended to command in module.
        args2           = Second set of arguments appended to command in module (multi-tool modules).
        args3           = Third set of arguments appended to command in module (multi-tool modules).
        publish_dir     = Directory to publish results.
        publish_by_meta = Groovy list of keys available in meta map to append as directories to "publish_dir" path
                            If publish_by_meta = true                 - Value of ${meta['id']} is appended as a directory to "publish_dir" path
                            If publish_by_meta = ['id', 'custompath'] - If "id" is in meta map and "custompath" isn't then "${meta['id']}/custompath/"
                                                                        is appended as a directory to "publish_dir" path
                            If publish_by_meta = false / null         - No directories are appended to "publish_dir" path
        publish_files   = Groovy map where key = "file_ext" and value = "directory" to publish results for that file extension
                            The value of "directory" is appended to the standard "publish_dir" path as defined above.
                            If publish_files = null (unspecified)     - All files are published.
                            If publish_files = false                  - No files are published.
        suffix          = File name suffix for output files.
----------------------------------------------------------------------------------------
*/

params {
    modules {
        /*
        ========================================================================================
            PRE-ALIGNMENT
        ========================================================================================
        */

        "cat_fastq" {
            publish_dir   = "01_prealign/merged_fastq"
        }
        "fastqc" {
            args          = "--quiet"
            publish_dir   = "01_prealign/pretrim_fastqc"
        }
        "trimgalore" {
            args          = ""
            publish_dir   = "01_prealign/trimgalore"
            publish_files = ["txt":"", "html":"fastqc", "zip":"fastqc"]
        }

        /*
        ========================================================================================
            ALIGNMENT
        ========================================================================================
        */

        // ALIGN BT2 - SPIKE-IN
        "bowtie2_spikein_align" {
            args          = "--end-to-end --very-sensitive --no-overlap --no-dovetail --no-mixed --no-discordant --phred33 -I 10 -X 700"
            suffix        = ".spikein"
            publish_files = false
        }
        "samtools_spikein_sort" {
            suffix        = ".spikein.sorted"
            publish_files = false
        }

        // ALIGN BT2
        "bowtie2_align" {
            args          = "--end-to-end --very-sensitive --no-mixed --no-discordant --phred33 -I 10 -X 700"
            suffix        = ".target"
            publish_files = false
        }
        "samtools_sort" {
            suffix        = ".target.sorted"
            publish_files = false
        }

        /*
        ========================================================================================
            Q-FILTERTING
        ========================================================================================
        */

        "samtools_view_qfilter" {
            suffix        = ".target.filtered"
            publish_files = false
        }
        "samtools_qfilter" {
            suffix        = ".target.filtered"
            publish_files = false
        }

        /*
        ========================================================================================
            DUPLICATES
        ========================================================================================
        */

        "picard_markduplicates" {
            args          = "-ASSUME_SORT_ORDER coordinate -REMOVE_DUPLICATES false -VALIDATION_STRINGENCY LENIENT -TMP_DIR tmp"
            suffix        = ".target.markdup"
            publish_files = false
        }
        "picard_markduplicates_samtools" {
            suffix        = ".target.markdup"
            publish_files = false
        }

        "picard_dedup" {
            args          = "-ASSUME_SORT_ORDER coordinate -REMOVE_DUPLICATES true -VALIDATION_STRINGENCY LENIENT -TMP_DIR tmp"
            suffix        = ".target.dedup"
            publish_files = false
        }
        "picard_dedup_samtools" {
            suffix        = ".target.dedup"
            publish_files = false
        }

        "awk_dedup" {
            args          = ""
            suffix        = ".awk"
            publish_dir   = false
            command       = "'/^[^#]/{print}'"
            command2      = " > int1.txt && head -2 int1.txt > int2.txt && sed 's/\\t/,/g' int2.txt > int3.txt && sed 's/.*/\\L&/g' int3.txt "
            publish_files = false
        }

        /*
        ========================================================================================
            ANNOTATE META WITH ALIGN
        ========================================================================================
        */

        "awk_bt2" {
            suffix        = ".target"
            publish_files = false
        }
        "awk_bt2_spikein" {
            suffix        = ".spikein"
            publish_files = false
        }

        /*
        ========================================================================================
            SCALING/CONVERSION AND PEAK CALLING
        ========================================================================================
        */

        "bedtools_genomecov_bedgraph" {
            //publish_dir   = "03_peak_calling/01_bam_to_bedgraph"
            publish_files = false
        }

        "sort_bedgraph" {
            publish_dir   = "03_peak_calling/01_bam_to_bedgraph"
            suffix        = ".sorted"
        }

        "ucsc_bedclip" {
            suffix        = ".clipped"
            publish_dir   = "03_peak_calling/02_clip_bed"
        }

        "ucsc_bedgraphtobigwig" {
            publish_dir   = "03_peak_calling/03_bed_to_bigwig"
        }

        "seacr" {
            args          = "non stringent"
            suffix        = ".peaks.bed"
            publish_dir   = "03_peak_calling/04_called_peaks"
        }

        /*
        ========================================================================================
            CONSENSUS PEAKS
        ========================================================================================
        */

        "awk_name_peak_bed" {
            command       = "'{OFS = \"\\t\"} {print \$0, FILENAME}'"
            publish_files = false
            ext           = "bed"
        }

        "sort_group_peaks" {
            args          = "-k1,1 -k2,2n"
            publish_files = false
            ext           = "bed"
        }

        "bedtools_merge_groups" {
            args          = " -c 2,3,4,5,6,7,7 -o collapse,collapse,collapse,collapse,collapse,collapse,count_distinct"
            // args          = " -c 7 -o collapse"
            publish_dir   = "03_peak_calling/05_consensus_peaks"
            suffix        = ".consensus.peaks"
        }

        "awk_threshold" {
            publish_dir   = "03_peak_calling/05_consensus_peaks"
            suffix        = ".consensus.peaks.filtered"
            ext           = "bed"
        }

        "plot_peaks" {
            publish_dir = "04_reporting"
        }

        /*
        ========================================================================================
            REPORTING
        ========================================================================================
        */

        "igv" {
            publish_dir   = "04_reporting/igv"
        }

        "dt_compute_mat_gene" {
            args        = "scale-regions --beforeRegionStartLength 3000 --regionBodyLength 5000 --afterRegionStartLength 3000 --skipZeros --missingDataAsZero"
            publish_dir = "04_reporting/heatmaps/gene"
        }

        "dt_compute_mat_peaks" {
            args        = "reference-point -a 3000 -b 3000 --referencePoint center --skipZeros --missingDataAsZero"
            publish_dir = "04_reporting/heatmaps/peaks"
        }

        "dt_plotheatmap_gene" {
            args        = "--sortUsing sum"
            publish_dir = "04_reporting/heatmaps/gene"
        }

        "dt_plotheatmap_peaks" {
            args        = "--sortUsing sum --startLabel \"Peak Start\" --endLabel \"Peak End\" --xAxisLabel \"\" --regionsLabel \"Peaks\""
            publish_dir = "04_reporting/heatmaps/peaks"
        }

        "awk_edit_peak_bed" {
            command     = "'{split(\$6, summit, \":\"); split(summit[2], region, \"-\"); print summit[1]\"\\t\"region[1]\"\\t\"region[2]}'"
            suffix      = ".max_signal"
            publish_files = false
        }

        "export_meta" {
            publish_dir   = "04_reporting"
        }

        "calc_frip" {
            publish_files = false
        }

        "meta_csv_frip_options" {
            publish_files = false
        }

        "bedtools_intersect" {
            publish_files = false
        }

        "calc_peak_repro_cut" {
            args          = "-f 1,2,3,6"
            suffix        = ".repro"
            ext           = "bed"
        }

        "calc_peak_repro" {
            publish_files = false
        }

        "meta_csv_peak_repro_options" {
            publish_files = false
        }

        "generate_reports" {
            publish_dir   = "04_reporting/qc"
        }

        "multiqc" {
            args          = "-v"
            publish_dir   = "04_reporting/multiqc"
        }

        /*
        ========================================================================================
            CALCULATE FRAGMENTS
        ========================================================================================
        */

        "calc_frag_samtools" {
            suffix        = ".mapped"
            publish_files = false
        }

        "calc_frag_samtools_view" {
            args          = "-F 0x04 -b"
            suffix        = ".mapped"
            publish_files = false
        }

        "calc_frag_samtools_sort" {
            args          = "-n"
            suffix        = ".sorted"
            publish_files = false
        }

        "calc_frag_bamtobed" {
            args          = "-bedpe"
            publish_files = false
        }

        "calc_frag_awk" {
            suffix        = ".clean"
            ext           = "bed"
            command       = "'\$1==\$4 && \$6-\$2 < 1000 {print \$0}'"
            publish_files = false
        }

        "calc_frag_cut" {
            args          = "-f 1,2,6"
            suffix        = ".frags"
            ext           = "bed"
            command       = "| sort -T '.' -k1,1 -k2,2n -k3,3n"
            publish_dir   = "03_peak_calling/06_fragments"
        }

        "awk_frag_bin" {
            args         = "-v w=500"
            suffix        = ".frags.bin500"
            ext           = "bed"
            publish_dir   = "03_peak_calling/06_fragments"
            command       = "'{print \$1, int((\$2 + \$3)/(2*w))*w + w/2, FILENAME}'"
            command2      = "| sort -T '.' -k1,1V -k2,2n | uniq -c | awk -v OFS=\"\\t\" '{print \$2, \$3, \$1, \$4}' | sort -T '.' -k1,1V -k2,2n"
        }

        "samtools_frag_len" {
            args          = "-F 0x04"
            args2         = "awk -F'\\t' 'function abs(x){return ((x < 0.0) ? -x : x)} {print abs(\$9)}' | sort -T '.' | uniq -c | awk -v OFS=\"\\t\" '{print \$2, \$1/2}'"
            suffix        = ".frag_len"
            publish_dir   = "03_peak_calling/06_fragments"
        }
    }
}
