/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Nextflow config file for running minimal tests
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Defines input files and everything required to run a fast and simple pipeline test.

    Use as follows:
        nextflow run nf-core/sarek -profile test,<extra_test_profile>,<docker/singularity> --outdir <OUTDIR>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/

try {
    includeConfig "https://raw.githubusercontent.com/nf-core/modules/master/tests/config/test_data.config"
} catch (Exception e) {
    System.err.println("WARNING: Could not load nf-core/modules test data config")
}

params {
    config_profile_name        = 'Test profile'
    config_profile_description = 'Minimal test dataset to check pipeline function'

    // Limit resources so that this can run on GitHub Actions
    max_cpus   = 2
    max_memory = '6.5GB'
    max_time   = '8.h'

    // Input data
    input = "${projectDir}/tests/csv/3.0/fastq_single.csv"

    // Small reference genome
    genome            = null
    igenomes_ignore   = true
    dbsnp             = params.test_data['homo_sapiens']['genome']['dbsnp_146_hg38_vcf_gz']
    fasta             = params.test_data['homo_sapiens']['genome']['genome_fasta']
    germline_resource = params.test_data['homo_sapiens']['genome']['gnomad_r2_1_1_vcf_gz']
    intervals         = params.test_data['homo_sapiens']['genome']['genome_interval_list']
    known_indels      = params.test_data['homo_sapiens']['genome']['mills_and_1000g_indels_vcf_gz']
    snpeff_db         = 'WBcel235.105'
    snpeff_genome     = 'WBcel235'
    snpeff_version    = '5.1'
    split_fastq       = 0
    vep_cache_version = 106
    vep_genome        = 'WBcel235'
    vep_species       = 'caenorhabditis_elegans'
    vep_version       = '106.1'


    // Ignore params that will throw warning through params validation
    schema_ignore_params = 'genomes,test_data,snpeff_version,vep_version'
}

profiles {
    annotation {
        params.input               = "${projectDir}/tests/csv/3.0/vcf_single.csv"
        params.step                = 'annotate'
    }
    no_intervals {
        params.intervals           = null
        params.no_intervals        = true
    }
    pair {
        params.input               = "${projectDir}/tests/csv/3.0/fastq_pair.csv"
    }
    markduplicates_bam {
        params.input               = "${projectDir}/tests/csv/3.0/mapped_single_bam.csv"
        params.step                = 'markduplicates'
    }
    markduplicates_cram {
        params.input               = "${projectDir}/tests/csv/3.0/mapped_single_cram.csv"
        params.step                = 'markduplicates'
    }
    prepare_recalibration_bam {
        params.input               = "${projectDir}/tests/csv/3.0/mapped_single_bam.csv"
        params.step                = 'prepare_recalibration'
    }
    prepare_recalibration_cram {
        params.input               = "${projectDir}/tests/csv/3.0/mapped_single_cram.csv"
        params.step                = 'prepare_recalibration'
    }
    recalibrate_bam {
        params.input               = "${projectDir}/tests/csv/3.0/prepare_recalibration_single_bam.csv"
        params.step                = 'recalibrate'
    }
    recalibrate_cram {
        params.input               = "${projectDir}/tests/csv/3.0/prepare_recalibration_single_cram.csv"
        params.step                = 'recalibrate'
    }
    save_bam_mapped {
        params.save_bam_mapped     = true
    }
    skip_bqsr {
        params.skip_tools          = "baserecalibrator"
    }
    skip_markduplicates {
        params.skip_tools          = "markduplicates"
    }
    split_fastq {
        params.split_fastq         = 150000
        params.save_split_fastqs   = true
    }
    targeted {
        params.intervals               = params.test_data['homo_sapiens']['genome']['genome_multi_interval_bed']
        params.wes                     = true
        params.nucleotides_per_second  = 20
    }
    tools {
        params.input               = "${projectDir}/tests/csv/3.0/recalibrated.csv"
        params.dbsnp               = params.test_data['homo_sapiens']['genome']['dbsnp_138_hg38_21_vcf_gz']
        params.fasta               = params.test_data['homo_sapiens']['genome']['genome_21_fasta']
        params.germline_resource   = params.test_data['homo_sapiens']['genome']['gnomad_r2_1_1_21_vcf_gz']
        params.intervals           = params.test_data['homo_sapiens']['genome']['genome_21_multi_interval_bed']
        params.pon                 = params.test_data['homo_sapiens']['genome']['mills_and_1000g_indels_21_vcf_gz']
        params.step                = 'variant_calling'
        params.wes                 = true

        params.nucleotides_per_second = 20
    }
    tools_germline {
        params.input               = "${projectDir}/tests/csv/3.0/recalibrated_germline.csv"
        params.dbsnp               = params.test_data['homo_sapiens']['genome']['dbsnp_138_hg38_21_vcf_gz']
        params.known_indels        = params.test_data['homo_sapiens']['genome']['mills_and_1000g_indels_21_vcf_gz']
        params.fasta               = params.test_data['homo_sapiens']['genome']['genome_21_fasta']
        params.intervals           = params.test_data['homo_sapiens']['genome']['genome_21_multi_interval_bed']
        params.step                = 'variant_calling'
        params.wes                 = true

        params.nucleotides_per_second = 20
    }
    tools_tumoronly {
        params.input               = "${projectDir}/tests/csv/3.0/recalibrated_tumoronly.csv"
        params.dbsnp               = params.test_data['homo_sapiens']['genome']['dbsnp_138_hg38_21_vcf_gz']
        params.fasta               = params.test_data['homo_sapiens']['genome']['genome_21_fasta']
        params.germline_resource   = params.test_data['homo_sapiens']['genome']['gnomad_r2_1_1_21_vcf_gz']
        params.intervals           = params.test_data['homo_sapiens']['genome']['genome_21_multi_interval_bed']
        params.pon                 = params.test_data['homo_sapiens']['genome']['mills_and_1000g_indels_21_vcf_gz']
        params.step                = 'variant_calling'
        params.wes                 = true

        params.nucleotides_per_second = 20
    }
    tools_somatic {
        params.input               = "${projectDir}/tests/csv/3.0/recalibrated_somatic.csv"
        params.chr_dir             = params.test_data['homo_sapiens']['genome']['genome_21_chromosomes_dir']
        params.dbsnp               = params.test_data['homo_sapiens']['genome']['dbsnp_138_hg38_21_vcf_gz']
        params.fasta               = params.test_data['homo_sapiens']['genome']['genome_21_fasta']
        params.germline_resource   = params.test_data['homo_sapiens']['genome']['gnomad_r2_1_1_21_vcf_gz']
        params.intervals           = params.test_data['homo_sapiens']['genome']['genome_21_multi_interval_bed']
        params.pon                 = params.test_data['homo_sapiens']['genome']['mills_and_1000g_indels_21_vcf_gz']
        params.step                = 'variant_calling'
        params.wes                 = true

        params.nucleotides_per_second = 20
    }
    // can only be tested locally due to too large cram files for GHA
    // download corresponding input files (ascat_somatic.csv) from ftp://ftp.1000genomes.ebi.ac.uk/vol1/ftp/phase3/data/
    // test works only without chromosome annotated loci files available at https://github.com/VanLoo-lab/ascat/tree/master/ReferenceFiles/WGS
    tools_somatic_ascat{
        params.input               = "${baseDir}/tests/csv/3.0/ascat_somatic.csv"
        params.chr_dir             = params.test_data['homo_sapiens']['genome']['genome_21_chromosomes_dir']
        params.ascat_loci          = "/mnt/volume/repos/modules/test_ascat2/G1000_loci_hg19.zip"
        params.ascat_chromosomes   = 'c("21", "22")'
        params.ascat_min_base_qual = 30
        params.germline_resource   = params.test_data['homo_sapiens']['genome']['gnomad_r2_1_1_21_vcf_gz']
        params.intervals           = params.test_data['homo_sapiens']['genome']['genome_21_multi_interval_bed']
        params.step                = 'variant_calling'
        params.joint_germline      = true
        params.wes                 = false
        params.tools               = 'ascat'
        params.igenomes_ignore     = false
        params.genome             = 'GATK.GRCh37'

    }
    trimming {
        params.clip_r1             = 1
        params.clip_r2             = 1
        params.three_prime_clip_r1 = 1
        params.three_prime_clip_r2 = 1
        params.trim_fastq          = true
    }
    umi {
        params.input               = "${projectDir}/tests/csv/3.0/fastq_umi.csv"
        params.umi_read_structure  = '+T 7M1S+T'
    }
    use_gatk_spark {
        params.use_gatk_spark      = 'baserecalibrator,markduplicates'
    }
    variantcalling_channels {
        params.input               = "${projectDir}/tests/csv/3.0/recalibrated.csv"
        params.fasta               = params.test_data['homo_sapiens']['genome']['genome_21_fasta']
        params.intervals           = params.test_data['homo_sapiens']['genome']['genome_21_multi_interval_bed']
        params.wes                 = true
        params.step                = 'variant_calling'

        params.nucleotides_per_second = 20
    }
    alignment_to_fastq {
        params.input               = "${projectDir}/tests/csv/3.0/bam_for_remapping.csv"
    }
}

process {
    withName:'FREEC_SOMATIC'{
        ext.args = {
            [
                "sample":[
                    inputformat: 'pileup',
                    mateorientation: 'FR'
                ],
                "general" :[
                    bedgraphoutput: "TRUE",
                    noisydata: "TRUE",
                    minexpectedgc: "0",
                    readcountthreshold: "1",
                    sex: meta.sex,
                    window: "10",
                    ],
                "control":[
                    inputformat: "pileup",
                    mateorientation: "FR"
                ]
            ]
        }
    }

    if (params.tools && params.tools.split(',').contains('mutect2')) {
        withName: 'NFCORE_SAREK:SAREK:PAIR_VARIANT_CALLING:GATK_TUMOR_NORMAL_SOMATIC_VARIANT_CALLING:MUTECT2'{
            //sample name from when the test data was generated
            ext.args = { "--f1r2-tar-gz ${task.ext.prefix}.f1r2.tar.gz --normal-sample normal " }
        }
    }

    withName: 'FILTERVARIANTTRANCHES'{
        ext.args         = { "--info-key CNN_1D --indel-tranche 0" }
    }
}
