/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Config file for defining DSL2 per module options and publishing paths
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Available keys to override module options:
        ext.args   = Additional arguments appended to command in module.
        ext.args2  = Second set of arguments appended to command in module (multi-tool modules).
        ext.args3  = Third set of arguments appended to command in module (multi-tool modules).
        ext.prefix = File name prefix for output files.
        ext.when   = When to run the module.
----------------------------------------------------------------------------------------
*/

// TRIMMING

process {

    withName: 'FASTP' {
        ext.args   = [
            !params.trim_fastq             ? '--disable_adapter_trimming'                 : '', // Disable adapter trimming
            params.clip_r1 > 0             ? "--trim_front1 ${params.clip_r1}"            : '', // Remove bp from the 5' end of read 1
            params.clip_r2   > 0           ? "--trim_front2 ${params.clip_r2}"            : '', // Remove bp from the 5' end of read 2
            params.three_prime_clip_r1 > 0 ? "--trim_tail1 ${params.three_prime_clip_r1}" : '', // Remove bp from the 3' end of read 1 AFTER adapter/quality trimming has been performed
            params.three_prime_clip_r2 > 0 ? "--trim_tail2 ${params.three_prime_clip_r2}" : '', // Remove bp from the 3' end of read 2 AFTER adapter/quality trimming has been performed
            params.trim_nextseq            ? '--trim_poly_g'                              : '', // Apply the --nextseq=X option, to trim based on quality after removing poly-G tails
            params.split_fastq > 0         ? "--split_by_lines ${params.split_fastq * 4}" : ''
        ].join(' ').trim()
        publishDir = [
            [
                path: { "${params.outdir}/reports/fastp/${meta.sample}" },
                mode: params.publish_dir_mode,
                pattern: "*.{html,json,log}"
            ],
            [
                path: { "${params.outdir}/preprocessing/" },
                mode: params.publish_dir_mode,
                pattern: "*.fastp.fastq.gz",
                saveAs: { params.save_trimmed || params.save_split_fastqs ?  "fastp/${meta.sample}/${it}" : null }
            ]
        ]
    }
}
