/*
========================================================================================
    Config file for defining DSL2 per module options
========================================================================================
    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.
----------------------------------------------------------------------------------------
*/

process {

    publishDir  = [
        path: { "${params.outdir}/${task.process.tokenize(':')[-1].tokenize('_')[0].toLowerCase()}" },
        mode: params.publish_dir_mode,
        saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
    ]

    // Local Subworkflows
    // INPUT_CHECK
    withName: 'NFCORE_QUANTMS:QUANTMS:INPUT_CHECK:SAMPLESHEET_CHECK' {
        publishDir = [
            path: { "${params.outdir}/pipeline_info" },
            mode: 'copy',
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

    withName: 'NFCORE_QUANTMS:QUANTMS:CUSTOM_DUMPSOFTWAREVERSIONS' {
        publishDir = [
            path: { "${params.outdir}/pipeline_info" },
            mode: params.publish_dir_mode,
            pattern: '*_versions.yml'
        ]
    }

}

// OPENMSPEAKPICKER
if (params.openms_peakpicking){
    process {
            withName: '.*:OPENMSPEAKPICKER' {
            publishDir  = [
                path: { "${params.outdir}/openmspeakpicker" },
                mode: params.publish_dir_mode,
                pattern: '*.log',
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }
    }
}

// DECOYDATABASE
if (params.add_decoys) {
    process {
        withName: '.*:DECOYDATABASE' {
            publishDir  = [
                path: { "${params.outdir}/decoydatabase" },
                mode: params.publish_dir_mode,
                pattern: '*.log',
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }
    }
}

if (params.posterior_probabilities == "percolator") {
    process {
        // EXTRACTPSMFEATURE
        withName: '.*:ID:PSMRESCORING:EXTRACTPSMFEATURES' {
            publishDir  = [
                path: { "${params.outdir}/extractpsmfeature" },
                mode: params.publish_dir_mode,
                pattern: '*.log',
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }

        //PERCOLATOR
        withName: '.*:ID:PSMRESCORING:PERCOLATOR' {
            ext.args    = "-debug $params.percolator_debug"
        }
    }
} else {
    process {
        // IDPEP
        withName: '.*:ID:PSMRESCORING:IDPEP' {
            ext.args    = "-debug $params.idpep_debug"
        }
    }

    if (params.search_engines.split(",").size() == 1) {
        process {
            // FDRIDPEP
            withName: '.*:FDRIDPEP' {
                ext.args    = "-PSM true -protein false"
            }
        }
    }
}

if (params.search_engines.split(",").size() == 1) {
    process {

        // IDSCORESWITCHER
        withName: '.*:ID:PSMFDRCONTROL:IDSCORESWITCHER' {
            ext.args    = [
                "-new_score_orientation lower_better",
                "-old_score \"Posterior Error Probability\"",
                "-new_score_type q-value",
                "-debug $params.idscoreswitcher_debug"
            ].join(' ').trim()
            publishDir  = [
                path: { "${params.outdir}/idscoreswitcher" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }
    }
} else {
    process {
        // FDRCONSENSUSID
        withName: '.*:FDRCONSENSUSID' {
            ext.args    = "-PSM true -protein false"
        }
    }
}

if (params.enable_mod_localization) {
    process {

        // IDSCORESWITCHERFORLUCIPHOR
        withName: '.*:ID:PHOSPHOSCORING:IDSCORESWITCHERFORLUCIPHOR' {
            ext.args    = [
                "-new_score_orientation lower_better",
                "-old_score \"q-value\"",
                "-new_score_type \"Posterior Error Probability\"",
                "-debug $params.idscoreswitcher_debug"
            ].join(' ').trim()
            publishDir  = [
                path: { "${params.outdir}/idscoreswitcherforluciphor" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }

        // LuciphorAdapter
        withName: '.*:ID:PHOSPHOSCORING:LUCIPHORADAPTER' {
            ext.args    = "-debug $params.luciphor_debug"
        }
    }
}

process {
    // ISOBARICANALYZER
    withName: '.*:TMT:.*:ISOBARICANALYZER' {
        ext.args    = "-debug $params.iso_debug"
        publishDir  = [
            path: { "${params.outdir}/isobaricanalyzer" },
            mode: params.publish_dir_mode,
            pattern: '*.log',
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

    // IDMAPPER
    withName: '.*:TMT:.*:IDMAPPER' {
        ext.args    = "-debug $params.idmapper_debug"
        publishDir  = [
            path: { "${params.outdir}/idmapper" },
            mode: params.publish_dir_mode,
            pattern: '*.{log,consensusXML}',
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

    // IDFILTER on PROTEIN LEVEL
    level = params.protein_quant == 'strictly_unique_peptides' ? 'prot' : 'protgroup'
    decoys_present = params.quantify_decoys ? ' ' : '-remove_decoys'
    withName: '.*:TMT:PROTEININFERENCE:IDFILTER' {
        ext.args    = [
            "-score:${level} \"$params.protein_level_fdr_cutoff\"",
            "-score:pep \"$params.psm_level_fdr_cutoff\"",
            "-delete_unreferenced_peptide_hits",
            "${decoys_present}"
        ].join(' ').trim()
        ext.suffix  = '.consensusXML'
        publishDir  = [
            path: { "${params.outdir}/epifilter" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

    // PROTEINQUANTIFIER
    withName: '.*:TMT:PROTEINQUANT:PROTEINQUANTIFIER' {
        ext.args    = "-debug 0"
    }

    // MSSTATSCONVERTER
    withName: '.*:TMT:PROTEINQUANT:MSSTATSCONVERTER' {
        ext.args    = "-debug 0"
    }
}

if (params.protein_inference_method.equals("bayesian")) {
    process {
        // EPIFANY
        withName: '.*:EPIFANY' {
            ext.args    = "-keep_best_psm_only false -debug $params.protein_inference_debug"
            publishDir  = [
                path: { "${params.outdir}/epifany" },
                mode: params.publish_dir_mode,
                pattern: '*.log',
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }
    }
} else {
    process {
        // PROTEININFERENCER
        withName: '.*:PROTEININFERENCER' {
            ext.args    = "-debug $params.protein_inference_debug"
            publishDir  = [
                path: { "${params.outdir}/proteininferencer" },
                mode: params.publish_dir_mode,
                pattern: '*.log',
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }
    }
}

process {

    // IDFILTER
    withName: '.*:ID:PSMFDRCONTROL:IDFILTER' {
        ext.args    = "-score:pep \"$params.run_fdr_cutoff\""
        ext.suffix  = '.idXML'
        publishDir  = [
            path: { "${params.outdir}/idfilter" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

    // PROTEOMICSLFQ
    withName: '.*:LFQ:PROTEOMICSLFQ' {
        ext.args    = "-debug $params.plfq_debug"
    }

    // DIA-NN
    withName: '.*:DIA:.*' {
        ext.when = { !(session.config.conda && session.config.conda.enabled) }
        publishDir  = [
            path: { "${params.outdir}/${task.process.tokenize(':')[-1].toLowerCase()}" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

}
