#!/usr/bin/env nextflow
/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    nf-core/scdownstream
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Github : https://github.com/nf-core/scdownstream
    Website: https://nf-co.re/scdownstream
    Slack  : https://nfcore.slack.com/channels/scdownstream
----------------------------------------------------------------------------------------
*/

/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    IMPORT FUNCTIONS / MODULES / SUBWORKFLOWS / WORKFLOWS
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/

include { SCDOWNSTREAM            } from './workflows/scdownstream'
include { PIPELINE_INITIALISATION } from './subworkflows/local/utils_nfcore_scdownstream_pipeline'
include { PIPELINE_COMPLETION     } from './subworkflows/local/utils_nfcore_scdownstream_pipeline'
include { analysisPlanToList      } from './subworkflows/local/utils_nfcore_scdownstream_pipeline'
/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    NAMED WORKFLOWS FOR PIPELINE
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/

//
// WORKFLOW: Run main analysis pipeline depending on type of input
//
workflow NFCORE_SCDOWNSTREAM {

    take:
    samplesheet                   // channel: samplesheet read in from --input
    ch_base                       // channel: [ val(meta), path(h5ad) ]
    is_extension                  //   value: boolean
    ch_input                      //    file: samplesheet.csv
    ambient_correction            //   value: string
    ambient_corrected_integration //   value: boolean
    doublet_detection             //   value: string
    doublet_detection_threshold   //   value: integer
    doublet_removal               //   value: boolean
    scvi_max_epochs               //   value: integer
    mito_genes                    //   value: string
    sample_n                      //   value: string
    sample_fraction               //   value: string
    cell_cycle_scoring            //   value: boolean
    s_genes                       //    path: file or []
    g2m_genes                     //    path: file or []
    species                       //   value: string
    qc_only                       //   value: boolean
    celldex_reference             //   value: string
    celltypist_model              //   value: string
    cytetype_study_context        //   value: string
    unify_gene_symbols            //   value: boolean
    duplicate_var_resolution      //   value: string
    aggregate_isoforms            //   value: boolean
    feature_selection             //   value: string
    integration_n_features        //   value: integer
    integration_methods           //   value: string
    integration_excluded_genes    //   value: string
    normalization_method          //   value: string
    scvi_model                    //   value: string
    scanvi_model                  //   value: string
    scvi_categorical_covariates   //   value: string
    scvi_continuous_covariates    //   value: string
    scimilarity_model             //   value: string
    symphony_reference            //   value: string
    expimap_gmt                   //   value: string
    skip_liana                    //   value: boolean
    liana_n_perms                 //   value: integer
    liana_max_cells               //   value: integer or null
    liana_subsample_strategy      //   value: string
    liana_subsample_seed          //   value: integer
    cell2cell                     //   value: boolean
    cell2cell_rank                //   value: integer or null
    cell2cell_seed                //   value: integer
    skip_qc_report                //   value: boolean
    scib                          //   value: boolean
    scib_max_cells                //   value: integer or null
    scib_subsample_strategy       //   value: string
    scib_subsample_seed           //   value: integer
    scib_metric_profile           //   value: string
    base_embeddings               //   value: string
    base_label_col                //   value: string
    base_condition_col            //   value: string
    base_donor_col                //   value: string
    integrate_per_label           //   value: boolean
    integrate_per_label_whitelist //   value: string
    cluster_per_label             //   value: boolean
    cluster_global                //   value: boolean
    clustering_resolutions        //   value: string
    neighbors_n_pcs               //   value: integer or null
    tsne                          //   value: boolean
    analysis_plan                 //   value: list of plan rows parsed in main.nf
    de_methods                    //   value: string
    interesting_genes             //    path: file or []
    pseudobulk                    //   value: boolean
    pseudobulk_min_num_cells      //   value: integer
    pseudobulk_min_total_counts   //   value: integer
    reference_condition           //   value: string
    prep_cellxgene                //   value: boolean
    tords                         //   value: boolean
    outdir                        //   value: string
    multiqc_config                //   value: string
    multiqc_logo                  //   value: string
    multiqc_methods_description   //   value: string

    main:

    //
    // WORKFLOW: Run pipeline
    //
    SCDOWNSTREAM (
        samplesheet,
        ch_base,
        is_extension,
        ch_input,
        ambient_correction,
        ambient_corrected_integration,
        doublet_detection,
        doublet_detection_threshold,
        doublet_removal,
        scvi_max_epochs,
        mito_genes,
        sample_n,
        sample_fraction,
        cell_cycle_scoring,
        s_genes,
        g2m_genes,
        species,
        qc_only,
        celldex_reference,
        celltypist_model,
        cytetype_study_context,
        unify_gene_symbols,
        duplicate_var_resolution,
        aggregate_isoforms,
        feature_selection,
        integration_n_features,
        integration_methods,
        integration_excluded_genes,
        normalization_method,
        scvi_model,
        scanvi_model,
        scvi_categorical_covariates,
        scvi_continuous_covariates,
        scimilarity_model,
        symphony_reference,
        expimap_gmt,
        skip_liana,
        liana_n_perms,
        liana_max_cells,
        liana_subsample_strategy,
        liana_subsample_seed,
        cell2cell,
        cell2cell_rank,
        cell2cell_seed,
        skip_qc_report,
        scib,
        scib_max_cells,
        scib_subsample_strategy,
        scib_subsample_seed,
        scib_metric_profile,
        base_embeddings,
        base_label_col,
        base_condition_col,
        base_donor_col,
        integrate_per_label,
        integrate_per_label_whitelist,
        cluster_per_label,
        cluster_global,
        clustering_resolutions,
        neighbors_n_pcs,
        tsne,
        analysis_plan,
        de_methods,
        interesting_genes,
        pseudobulk,
        pseudobulk_min_num_cells,
        pseudobulk_min_total_counts,
        reference_condition,
        prep_cellxgene,
        tords,
        outdir,
        multiqc_config,
        multiqc_logo,
        multiqc_methods_description,
    )

    emit:
    multiqc_report = SCDOWNSTREAM.out.multiqc_report // channel: /path/to/multiqc_report.html
}

/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    RUN MAIN WORKFLOW
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/

workflow {

    main:
    //
    // SUBWORKFLOW: Run initialisation tasks
    //
    PIPELINE_INITIALISATION (
        params.version,
        params.validate_params,
        params.monochrome_logs,
        args,
        params.outdir,
        params.help,
        params.help_full,
        params.show_hidden
    )

    //
    // WORKFLOW: Run main workflow
    //
    ch_base_adata = params.base_adata
        ? channel.value([[id: "base"], file(params.base_adata, checkIfExists: true)])
        : channel.value([[], []])

    def s_genes_file = params.cell_cycle_scoring
        ? file(params.s_genes ?: "${projectDir}/assets/cell_cycle_genes/${params.species}_s_genes.txt", checkIfExists: true)
        : []
    def g2m_genes_file = params.cell_cycle_scoring
        ? file(params.g2m_genes ?: "${projectDir}/assets/cell_cycle_genes/${params.species}_g2m_genes.txt", checkIfExists: true)
        : []

    def analysis_plan = analysisPlanToList()

    def symphony_reference = params.symphony_reference
        ? file(params.symphony_reference, checkIfExists: true)
        : null

    def interesting_genes_file = params.interesting_genes
        ? file(params.interesting_genes, checkIfExists: true)
        : []

    NFCORE_SCDOWNSTREAM (
        PIPELINE_INITIALISATION.out.samplesheet,
        ch_base_adata,
        params.base_adata != null,
        params.input,
        params.ambient_correction,
        params.ambient_corrected_integration,
        params.doublet_detection,
        params.doublet_detection_threshold,
        params.doublet_removal,
        params.scvi_max_epochs,
        params.mito_genes,
        params.sample_n,
        params.sample_fraction,
        params.cell_cycle_scoring,
        s_genes_file,
        g2m_genes_file,
        params.species,
        params.qc_only,
        params.celldex_reference,
        params.celltypist_model,
        params.cytetype_study_context,
        params.unify_gene_symbols,
        params.duplicate_var_resolution,
        params.aggregate_isoforms,
        params.feature_selection,
        params.integration_n_features,
        params.integration_methods,
        params.integration_excluded_genes,
        params.normalization_method,
        params.scvi_model,
        params.scanvi_model,
        params.scvi_categorical_covariates,
        params.scvi_continuous_covariates,
        params.scimilarity_model,
        symphony_reference,
        params.expimap_gmt,
        params.skip_liana,
        params.liana_n_perms,
        params.liana_max_cells,
        params.liana_subsample_strategy,
        params.liana_subsample_seed,
        params.cell2cell,
        params.cell2cell_rank,
        params.cell2cell_seed,
        params.skip_qc_report,
        params.scib,
        params.scib_max_cells,
        params.scib_subsample_strategy,
        params.scib_subsample_seed,
        params.scib_metric_profile,
        params.base_embeddings,
        params.base_label_col,
        params.base_condition_col,
        params.base_donor_col,
        params.integrate_per_label,
        params.integrate_per_label_whitelist,
        params.cluster_per_label,
        params.cluster_global,
        params.clustering_resolutions,
        params.neighbors_n_pcs ?: '',
        params.tsne,
        analysis_plan,
        params.de_methods,
        interesting_genes_file,
        params.pseudobulk,
        params.pseudobulk_min_num_cells,
        params.pseudobulk_min_total_counts,
        params.reference_condition,
        params.prep_cellxgene,
        params.tords,
        params.outdir,
        params.multiqc_config,
        params.multiqc_logo,
        params.multiqc_methods_description
    )

    //
    // SUBWORKFLOW: Run completion tasks
    //
    PIPELINE_COMPLETION (
        params.email,
        params.email_on_fail,
        params.plaintext_email,
        params.outdir,
        params.monochrome_logs,
        NFCORE_SCDOWNSTREAM.out.multiqc_report
    )
}

/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    THE END
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
