/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    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.
----------------------------------------------------------------------------------------
*/

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},
            enabled: false
        ]

    withName: 'SAMPLESHEET_CHECK' {
            publishDir = [
                path: {"${params.outdir}/pipeline_info"},
                mode: params.publish_dir_mode
            ]
        }

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

        withName: 'MULTIQC' {
            publishDir = [
                path: {"${params.outdir}/multiqc"},
                mode: params.publish_dir_mode,
                enabled: true
            ]
        }

}

process {

    withName: 'GENERATE_PROTEINS_FROM_VCF' {
            ext.args    = [
                "-t ${params.variant_annotation_style}",
                "-r ${params.variant_reference}",
                params.variant_indel_filter ? "-fINDEL" : "",
                params.variant_frameshift_filter ? "-fFS" : "",
                params.variant_snp_filter ? "-fSNP" : ""
                ].join(' ').trim()
            publishDir  = [
                path: {"${params.outdir}"},
                mode: params.publish_dir_mode,
                pattern: '*.fasta'
            ]
        }

    withName: 'OPENMS_MAPALIGNERIDENTIFICATION' {
            ext.args    = [
                "-model:type linear",
                "-algorithm:max_rt_shift ${params.max_rt_alignment_shift}"
                ].join(' ').trim()
        }

    withName: 'OPENMS_COMETADAPTER' {
            ext.args    = [
                "-precursor_mass_tolerance ${params.precursor_mass_tolerance}",
                "-fragment_mass_tolerance ${params.fragment_mass_tolerance}",
                "-fragment_bin_offset ${params.fragment_bin_offset}",
                "-instrument ${params.instrument}",
                "-num_hits ${params.num_hits}",
                "-digest_mass_range ${params.digest_mass_range}",
                "-max_variable_mods_in_peptide ${params.number_mods}",
                "-missed_cleavages 0",
                "-precursor_charge ${params.prec_charge}",
                "-activation_method ${params.activation_method}",
                "-variable_modifications ${params.variable_mods.tokenize(',').collect {"'${it}'"}.join(" ")}",
                "-enzyme '${params.enzyme}'",
                "-spectrum_batch_size ${params.spectrum_batch_size}"
            ].join(' ').trim()
            publishDir = [
                path: {"${params.outdir}/intermediate_results/comet"},
                mode: params.publish_dir_mode,
                pattern: '*.tsv'
            ]
        }

    withName: 'OPENMS_IDFILTER_FOR_ALIGNMENT' {
            ext.args    = [
                "-remove_decoys",
                "-precursor:length '${params.peptide_min_length}:${params.peptide_max_length}'",
                "-delete_unreferenced_peptide_hits",
                (params.fdr_threshold == '0.01') ? "-score:pep 0.05" : "-score:pep " + params.fdr_threshold
            ].join(' ').trim()
            publishDir  = [
                path: {"${params.outdir}/intermediate_results/alignment"},
                mode: params.publish_dir_mode,
                pattern: '*.idXML'
            ]
        }

    withName: 'OPENMS_IDFILTER_Q_VALUE' {
            ext.prefix  = {"${meta.id}_fdr_filtered"}
            ext.args    = [
                "-remove_decoys",
                "-precursor:length '${params.peptide_min_length}:${params.peptide_max_length}'",
                "-delete_unreferenced_peptide_hits",
                (params.fdr_threshold == '0.01') ? "-score:pep 0.05" : "-score:pep " + params.fdr_threshold
            ].join(' ').trim()
            publishDir  = [
                path: {"${params.outdir}/intermediate_results/alignment"},
                mode: params.publish_dir_mode,
                pattern: '*.idXML'
            ]
        }

    withName: 'OPENMS_PERCOLATORADAPTER' {
            ext.prefix  = {"${meta.id}_all_ids_merged_psm_perc"}
            ext.args    = [
                "-seed 4711",
                "-trainFDR 0.05",
                "-testFDR 0.05",
                "-enzyme no_enzyme",
                "-subset_max_train ${params.subset_max_train}",
                "-doc ${params.description_correct_features} ",
                (params.fdr_level != 'psm_level_fdrs') ? "-" + params.fdr_level : ""
            ].join(' ').trim()
            publishDir  = [
                path: {"${params.outdir}/intermediate_results/percolator"},
                mode: params.publish_dir_mode,
                pattern: '*.idXML'
            ]
        }

    withName: 'OPENMS_PSMFEATUREEXTRACTOR' {
            publishDir  = [
                path: {"${params.outdir}/intermediate_results/features"},
                mode: params.publish_dir_mode,
                pattern: '*.idXML'
            ]
        }

    withName: 'OPENMS_MZTABEXPORTER_QUANT' {
            publishDir  = [
                path: {"${params.outdir}/intermediate_results/features"},
                mode: params.publish_dir_mode,
                pattern: '*.mzTab'
            ]
    }

    withName: 'OPENMS_FEATUREFINDERIDENTIFICATION' {
        publishDir  = [
            path: {"${params.outdir}/intermediate_results/features"},
            mode: params.publish_dir_mode,
            pattern: '*.featureXML'
        ]
    }

    withName: 'OPENMS_TEXTEXPORTER_UNQUANTIFIED|OPENMS_TEXTEXPORTER_QUANTIFIED' {
            publishDir  = [
                path: {"${params.outdir}/"},
                mode: params.publish_dir_mode,
                pattern: '*.tsv'
            ]
        }

    withName: 'OPENMS_TEXTEXPORTER_COMET' {
        ext.prefix  = {"${meta.sample}_${meta.condition}_${meta.id}"}
            publishDir  = [
                path: {"${params.outdir}/intermediate_results/comet"},
                mode: params.publish_dir_mode,
                pattern: '*.tsv'
            ]
        }

    withName: 'OPENMS_IDCONFLICTRESOLVER' {
            publishDir  = [
                path: {"${params.outdir}/intermediate_results/features"},
                mode: params.publish_dir_mode,
                pattern: '*.consensusXML'
            ]
    }
}


// Refine on predicted subset
process {

    if (params.refine_fdr_on_predicted_subset && params.predict_class_1) {
        withName: 'OPENMS_MZTABEXPORTERPERC' {
                ext.prefix  = {"${meta.sample}_${meta.condition}_all_ids_merged_psm_perc_filtered"}
                publishDir  = [
                    path: {"${params.outdir}/intermediate_results/refined_fdr"},
                    mode: params.publish_dir_mode,
                    pattern: '*.mzTab'
                ]
            }

        withName: 'OPENMS_MZTABEXPORTERPSM' {
                ext.prefix  = {"${meta.sample}_${meta.condition}_all_ids_merged"}
                publishDir  = [
                    path: {"${params.outdir}/intermediate_results/refined_fdr"},
                    mode: params.publish_dir_mode,
                    pattern: '*.mzTab'
                ]
            }

        withName: 'MHCFLURRY_PREDICTPSMS' {
                publishDir       = [
                    path: {"${params.outdir}/intermediate_results/MHCFLURRY_PREDICTPSMS"},
                    mode: params.publish_dir_mode,
                    pattern: '*.idXML'
                ]
            }

        withName: 'REFINE_FDR:OPENMS_PERCOLATORADAPTER' {
                ext.prefix  = {"${meta.id}_perc_subset"}
                ext.args    = [
                    "-seed 4711",
                    "-trainFDR 0.05",
                    "-testFDR 0.05",
                    "-enzyme no_enzyme",
                    "-subset_max_train ${params.subset_max_train}",
                    "-doc ${params.description_correct_features} ",
                    (params.fdr_level != 'psm_level_fdrs') ? "-" + params.fdr_level : ""
                ].join(' ').trim()
                publishDir  = [
                    path: {"${params.outdir}/intermediate_results/refined_fdr"},
                    mode: params.publish_dir_mode,
                    pattern: '*.idXML'
                ]
            }

        withName: 'OPENMS_IDFILTER_PSMS' {
                ext.prefix  = {"${meta.id}_pred_filtered"}
                ext.args    = [
                    "-whitelist:ignore_modifications",
                    "-whitelist:peptides"
                ].join(' ').trim()
                publishDir  = [
                    path: {"${params.outdir}/intermediate_results/refined_fdr"},
                    mode: params.publish_dir_mode,
                    pattern: '*.idXML'
                ]
            }

        withName: 'OPENMS_IDFILTER_REFINED' {
                ext.args    = [
                    "-remove_decoys",
                    "-precursor:length '${params.peptide_min_length}:${params.peptide_max_length}'",
                    "-delete_unreferenced_peptide_hits",
                    (params.fdr_threshold == '0.01') ? "-score:pep 0.05" : "-score:pep " + params.fdr_threshold
                ].join(' ').trim()
                publishDir  = [
                    path: {"${params.outdir}/intermediate_results/refined_fdr"},
                    mode: params.publish_dir_mode,
                    pattern: '*.idXML'
                ]
            }
    }
}

// Class I prediction
process {

    if (params.predict_class_1  & !params.skip_quantification) {
        withName: 'MHCFLURRY_PREDICTPEPTIDESCLASS1' {
                publishDir  = [
                    path: {"${params.outdir}/class_1_bindings"},
                    mode: params.publish_dir_mode,
                    pattern: '*.fasta'
                ]
            }
    }

    if (params.predict_class_1  & !params.skip_quantification & params.include_proteins_from_vcf) {
        withName: 'PREDICT_POSSIBLE_CLASS1_NEOEPITOPES' {
                ext.prefix = {"${meta}_vcf_neoepitopes"}
                publishDir = [
                    path: {"${params.outdir}/class_1_bindings"},
                    mode: params.publish_dir_mode,
                    pattern: '*.csv'
                ]
            }

        withName: 'RESOLVE_FOUND_CLASS1_NEOEPITOPES' {
                ext.prefix = {"${meta.sample}_found_neoepitopes"}
                publishDir = [
                    path: {"${params.outdir}/class_1_bindings"},
                    mode: params.publish_dir_mode,
                    pattern: '*.csv'
                ]
            }

        withName: 'MHCFLURRY_PREDICTNEOEPITOPESCLASS1' {
                publishDir = [
                    path: {"${params.outdir}/class_1_bindings"},
                    mode: params.publish_dir_mode,
                    pattern: '*.csv'
                ]
            }
    }
}

// Class II prediction
process {

    if (params.predict_class_2 & !params.skip_quantification) {

        withName: 'MHCNUGGETS_PEPTIDESCLASS2PRE' {
                publishDir  = [
                    path: {"${params.outdir}/class_2_bindings"},
                    mode: params.publish_dir_mode,
                    pattern: '*_peptides'
                ]
            }

        withName: 'MHCNUGGETS_PREDICTPEPTIDESCLASS2' {
                publishDir  = [
                    path: {"${params.outdir}/class_2_bindings"},
                    mode: params.publish_dir_mode,
                    pattern: '*_class_2'
                ]
            }

        withName: 'MHCNUGGETS_PEPTIDESCLASS2POST' {
                publishDir  = [
                    path: {"${params.outdir}/class_2_bindings"},
                    mode: params.publish_dir_mode,
                    pattern: '*.csv'
                ]
            }
    }

    if (params.predict_class_2  & !params.skip_quantification & params.include_proteins_from_vcf) {

        withName: 'PREDICT_POSSIBLE_CLASS2_NEOEPITOPES' {
                publishDir  = [
                    path: {"${params.outdir}/class_2_bindings"},
                    mode: params.publish_dir_mode,
                    pattern: '*.csv'
                ]
            }

        withName: 'RESOLVE_FOUND_CLASS2_NEOEPITOPES' {
                publishDir  = [
                    path: {"${params.outdir}/class_2_bindings"},
                    mode: params.publish_dir_mode,
                    pattern: '*.csv'
                ]
            }

        withName: 'MHCNUGGETS_NEOEPITOPESCLASS2PRE' {
                ext.prefix  = {"${meta}_mhcnuggets_preprocessed"}
                publishDir  = [
                    path: {"${params.outdir}/class_2_bindings"},
                    mode: params.publish_dir_mode,
                    pattern: '*${ext.prefix}'
                ]
            }

        withName: 'MHCNUGGETS_PREDICTNEOEPITOPESCLASS2' {
                ext.prefix  = {"${meta}_predicted_neoepitopes_class_2"}
                publishDir  = [
                    path: {"${params.outdir}/class_2_bindings"},
                    mode: params.publish_dir_mode,
                    pattern: '*${ext.prefix}'
                ]
            }

        withName: 'MHCNUGGETS_NEOEPITOPESCLASS2POST' {
                publishDir  = [
                    path: {"${params.outdir}/class_2_bindings"},
                    mode: params.publish_dir_mode,
                    pattern: '*.csv'
                ]
            }
    }
}

process {

    if (params.predict_RT) {
        withName: 'OPENMS_RTMODEL' {
                publishDir       = [
                        path: {"${params.outdir}/RT_prediction"},
                        mode: params.publish_dir_mode,
                        pattern: '*.txt|*.paramXML'
                    ]
            }

        withName: 'OPENMS_RTPREDICT_FOUND_PEPTIDES' {
                ext.prefix = {"${meta.sample}_id_files_for_rt_prediction_RTpredicted"}
                publishDir  = [
                    path: {"${params.outdir}/RT_prediction"},
                    mode: params.publish_dir_mode,
                    pattern: '*.csv'
                ]
            }

        withName: 'OPENMS_RTPREDICT_NEOEPITOPES' {
                ext.prefix = {"${meta.sample}_txt_file_for_rt_prediction_RTpredicted"}
                publishDir  = [
                    path: {"${params.outdir}/RT_prediction"},
                    mode: params.publish_dir_mode,
                    pattern: '*.csv'
                ]
            }
    }
}

process {

    if (params.annotate_ions) {
        withName: 'PYOPENMS_IONANNOTATOR' {
            ext.prefix = {"${meta.sample}"}
            ext.args   = [
                    "--precursor_charge ${params.prec_charge}",
                    "--fragment_mass_tolerance ${params.fragment_mass_tolerance}",
                    "--remove_precursor_peak ${params.remove_precursor_peak}"
                ].join(' ').trim()
                publishDir  = [
                    path: {"${params.outdir}/intermediate_results/ion_annotations"},
                    mode: params.publish_dir_mode,
                    pattern: '*.tsv'
                ]
            }
        }
}
