workflow task_16s {
    Array[File] data_files = [ "/ceph_disk2/data/hongyuan/mnt/data/208/SRR22281726_1.fastq.gz", "/ceph_disk2/data/hongyuan/mnt/data/208/SRR22281726_2.fastq.gz", "/ceph_disk2/data/hongyuan/mnt/data/208/SRR22281727_1.fastq.gz", "/ceph_disk2/data/hongyuan/mnt/data/208/SRR22281727_2.fastq.gz", "/ceph_disk2/data/hongyuan/mnt/data/208/SRR22281728_1.fastq.gz", "/ceph_disk2/data/hongyuan/mnt/data/208/SRR22281728_2.fastq.gz", "/ceph_disk2/data/hongyuan/mnt/data/208/SRR22281729_1.fastq.gz", "/ceph_disk2/data/hongyuan/mnt/data/208/SRR22281729_2.fastq.gz", "/ceph_disk2/data/hongyuan/mnt/data/208/SRR22281730_1.fastq.gz", "/ceph_disk2/data/hongyuan/mnt/data/208/SRR22281730_2.fastq.gz", "/ceph_disk2/data/hongyuan/mnt/data/208/SRR22281731_1.fastq.gz", "/ceph_disk2/data/hongyuan/mnt/data/208/SRR22281731_2.fastq.gz", "/ceph_disk2/data/hongyuan/mnt/data/208/SRR22281732_1.fastq.gz", "/ceph_disk2/data/hongyuan/mnt/data/208/SRR22281732_2.fastq.gz", "/ceph_disk2/data/hongyuan/mnt/data/208/SRR22281733_1.fastq.gz", "/ceph_disk2/data/hongyuan/mnt/data/208/SRR22281733_2.fastq.gz", "/ceph_disk2/data/hongyuan/mnt/data/208/SRR22281734_1.fastq.gz", "/ceph_disk2/data/hongyuan/mnt/data/208/SRR22281734_2.fastq.gz", "/ceph_disk2/data/hongyuan/mnt/data/208/SRR22281735_1.fastq.gz", "/ceph_disk2/data/hongyuan/mnt/data/208/SRR22281735_2.fastq.gz" ]
    String denoisePaired_TruncLenR = "0"
    String denoisePaired_TruncLenF = "0"
    String denoisePaired_TrimLeftF = "29"
    String denoisePaired_TrimLeftR = "21"
    String threads = "10"

    File classifySklearn_Classifier = "/ceph_disk2/data/hongyuan/mnt/data/public_file_data/wuzhong/16SModel_1663056656/gg-13-8-99-nb-classifier.qza"

    Array[String] alpha_metrics = ["ace", "chao1","shannon","simpson", "goods_coverage","pielou_e"]
    Array[String] beta_metrics = ["braycurtis", "jaccard"]
    Array[String] betaPhylo_metrics = ["unweighted-unifrac", "weighted-unifrac"]

    #    String sampleingDepth = "50000"
    String alphaRarefaction_MaxDepth = "50000"
    String rarefy_SamplingDepth = "10000"
    String rarefy_WithReplacement = "False"
    String beta_jaccard_Pseudocount = "1"
    String betaPhylo_VarianceAdjusted = "False"
    String betaPhylo_BypassTips = "False"
    String betaPhylo_Alpha = "1.0"
    File MetadataFile="/ceph_disk2/data/hongyuan/mnt/data/279/metadata.tsv"

    String metadata_column = "分组"
    File db_red_file="/ceph_disk2/data/hongyuan/mnt/data/public_data/1710230841/red_tax.db.csv"
    File db_alien_file="/ceph_disk2/data/hongyuan/mnt/data/public_data/1710230841/alien_tax.db.csv"

    call metadata_init {
        input:
            metadata = MetadataFile,
            files = data_files
    }
    call fqc {
        input:
            fastqdir = metadata_init.data_fqc_str
    }
    call qc_task {
        input:
            fastqdir = metadata_init.data_dir_str
    }
    call importData {
        input:
            Data = metadata_init.data_dir_str,
    }
    call denoisePaired {
        input:
            TruncLenR = denoisePaired_TruncLenR,
            TruncLenF = denoisePaired_TruncLenF,
            TrimLeftF = denoisePaired_TrimLeftF,
            TrimLeftR = denoisePaired_TrimLeftR,
            NThreads = threads,
            AtccPairedEnd = importData.atccPairedEnd,
    }

    call tableSummary {
        input:
            otuTable = denoisePaired.atccTable,
            metadata_file = metadata_init.metadata_result,
    }
    call alignToTree {
        input:
            AtccSeqs = denoisePaired.atccSeqs,
    }
    call classifySklearn {
        input:
            Classifier = classifySklearn_Classifier,
            Reads = denoisePaired.atccSeqs,
            NJobs = threads,
    }
    call rarefy {
        input:
            SamplingDepth = rarefy_SamplingDepth,
            WithReplacement = rarefy_WithReplacement,
            AtccTable = denoisePaired.atccTable,
    }

    call alphaDiversity {
        input:

            RarefiedTable = rarefy.rarefiedTable,
            RootedTreeFromSeqs=alignToTree.rootedTreeFromSeqs,
            AlphaMetrics=alpha_metrics,
    }
    call betaDiversity {
        input:
            Jobs = threads,
            BetaMetrics=beta_metrics,
            BetaPhylo_Metrics=betaPhylo_metrics,
            RootedTreeFromSeqs=alignToTree.rootedTreeFromSeqs,
            Pseudocount = beta_jaccard_Pseudocount,
            RarefiedTable = rarefy.rarefiedTable,
            MetadataFile=metadata_init.metadata_result,
            MetadataField = metadata_column,
    }

    call barplot {
        input:
            Table = rarefy.rarefiedTable,
            Taxonomy = classifySklearn.classification,
            MetadataFile = metadata_init.metadata_result,
    }
    call alphaRarefaction {
        input:
            MaxDepth = alphaRarefaction_MaxDepth,
            Table = rarefy.rarefiedTable,
            Phylogeny = alignToTree.rootedTreeFromSeqs,
            MetadataFile = metadata_init.metadata_result,
    }
    call platform_16s_report_task {
        input:
            GroupField = metadata_column,
            FastqcDir = qc_task.result_dir_str,
            StatsFile = denoisePaired.statsFile,
            MetadataFile = metadata_init.metadata_result,
            OtuTable = denoisePaired.otuTable,
            Taxonomy = classifySklearn.taxonomy_file,
            Sequences = denoisePaired.sequencesFa,
            DB_Red = db_red_file,
            DB_Alien = db_alien_file,
    }
    call report_edna_plot_task {
        input:
            GroupFile=platform_16s_report_task.group,
            GroupField=metadata_column,
            OTUStat1 = platform_16s_report_task.otu_stat_1,
            OTUStat2 = platform_16s_report_task.otu_stat_2,
            OTUStatAlien =platform_16s_report_task.otu_stat_alien,
            OTUStatRed = platform_16s_report_task.otu_stat_red,
            OTUDepth = platform_16s_report_task.otu_depth,
            G_By_Sample = platform_16s_report_task.g_by_sample,
            G_By_Group = platform_16s_report_task.g_by_group,
            S_By_Sample = platform_16s_report_task.s_by_sample,
            S_By_Group = platform_16s_report_task.s_by_group,
            TaxGenus = platform_16s_report_task.tax_genus,
            TaxSpecies = platform_16s_report_task.tax_species,

            alpha_rarefaction_csv = alphaRarefaction.alpha_rarefaction_csv,
            alpha_vectors_tsv = alphaDiversity.matrix_tsv,
            distance_matrix_files = betaDiversity.distance_matrix_files,
            ordination_pcoa_files = betaDiversity.ordination_pcoa_files,
            ordination_tsne_files = betaDiversity.ordination_tsne_files,
            ordination_umap_files = betaDiversity.ordination_umap_files,
            beta_significance_files = betaDiversity.beta_significance_files,
    }
    call collect_report_data {
        input:
            table1 = platform_16s_report_task.table1,
            table2 = platform_16s_report_task.table2,
            table3 = platform_16s_report_task.table3,
            featureFreq_png =  tableSummary.featureFreq_png,
            featureFreq_pdf =  tableSummary.featureFreq_pdf,
            sampleFreq_png =  tableSummary.sampleFreq_png,
            sampleFreq_pdf =  tableSummary.sampleFreq_pdf,
            OTUStat1 = platform_16s_report_task.otu_stat_1,
            OTUStat2 = platform_16s_report_task.otu_stat_2,
            OTUStatAlien =platform_16s_report_task.otu_stat_alien,
            OTUStatRed = platform_16s_report_task.otu_stat_red,
            G_By_Sample = platform_16s_report_task.g_by_sample,
            G_By_Group = platform_16s_report_task.g_by_group,
            S_By_Sample = platform_16s_report_task.s_by_sample,
            S_By_Group = platform_16s_report_task.s_by_group,
            TaxGenus = platform_16s_report_task.tax_genus,
            TaxSpecies = platform_16s_report_task.tax_species,
            OTUStat1Plot = report_edna_plot_task.otu_stat_1,
            OTUStat2Plot = report_edna_plot_task.otu_stat_2,
            OTUStatRedPlot = report_edna_plot_task.otu_stat_r,
            OTUStatAlienPlot = report_edna_plot_task.otu_stat_a,
            TaxAnalysis1Plot = report_edna_plot_task.tax_analysis_1,
            TaxAnalysis2Plot = report_edna_plot_task.tax_analysis_2,
            TaxAnalysis3Plot = report_edna_plot_task.tax_analysis_3,
            alphaPlots = report_edna_plot_task.alpha_plots,
            alphaDepthPlots = report_edna_plot_task.depth_plots,
            alphaRareficationPlot1 = report_edna_plot_task.alpha_rarefication_plot1,
            alphaRareficationPlot2 = report_edna_plot_task.alpha_rarefication_plot2,
            betaSignificancePlots = report_edna_plot_task.beta_significance_plots,
            PCOA_Plots = report_edna_plot_task.pcoa_plots,
            UMAP_Plots = report_edna_plot_task.umap_plots,
            TSNE_Plots = report_edna_plot_task.tsne_plots,
    }

    output {
        File respath = collect_report_data.result
        File otu_sample_freq_file = collect_report_data.otu_sample_freq
        File otu_feature_freq_file = collect_report_data.otu_feature_freq
        File tax_stat_1 = collect_report_data.tax_stat_1
        File tax_stat_2 = collect_report_data.tax_stat_2
        File tax_stat_a = collect_report_data.tax_stat_a
        File tax_stat_r = collect_report_data.tax_stat_r

        File tax_analysis_1 = collect_report_data.tax_analysis_1
        File tax_analysis_2 = collect_report_data.tax_analysis_2
        File tax_analysis_3 = collect_report_data.tax_analysis_3

        File alpha_depth_plots = collect_report_data.alpha_depth_plots
        File alpha_rarefication_plot1 = collect_report_data.alpha_rarefication_plot1
        File alpha_rarefication_plot2 = collect_report_data.alpha_rarefication_plot2

        File alpha_plots = collect_report_data.alpha_plots

        File beta_sig_plots = collect_report_data.beta_sig_plots
        File pcoa_plots = collect_report_data.pcoa_plots
        File umap_plots = collect_report_data.umap_plots
        File tsne_plots = collect_report_data.tsne_plots
    }

}

task metadata_init {
    File metadata
    Array[File] files
    command {
        generate_16s_metadata ${metadata} ${sep=" " files}
    }

    runtime {
        docker: "dockerhub.genostack.com:8090/angs/toolkit:1.3.0"
    }
    output {
        File metadata_result = "metadata.tsv"
        File data_dir = "data"
        File data_fqc = "data1"
        String data_dir_str = "${data_dir}"
        String data_fqc_str = "${data_fqc}"
    }
}

task fqc {
    String fastqdir

    command {
        fqc batch-qc fqc-data ${fastqdir}
    }

    runtime {
        docker: "dockerhub.genostack.com:8090/angs/fqc:1.5.7"
    }
    output {
        File cwl_fqc_add_Read_Lengths_result = "plot_data"
    }
}

task qc_task {
    String fastqdir

    command {
        mkdir fastqc_result
        fastqc -o fastqc_result ${fastqdir}/*.fastq.gz
    }

    runtime {
        docker: "dockerhub.genostack.com:8090/angs/pipeline_data_collect:1.1.11"
    }
    output {
        File result_dir = "fastqc_result"
        String result_dir_str = "${result_dir}"
    }
}

task importData {
    String Data

    # export 后同级目录输出很多 fastq.gz 文件
    command {
        qiime tools import --type  'SampleData[PairedEndSequencesWithQuality]' --input-path ${Data} --input-format CasavaOneEightSingleLanePerSampleDirFmt --output-path atcc-paired-end.qza
        qiime tools export --input-path ./atcc-paired-end.qza --output-path ./
    }

    runtime {
        docker: "quay.io/qiime2/core:2022.2"
    }
    output {
        File atccPairedEnd = "atcc-paired-end.qza"
    }
}

task denoisePaired {
    String TruncLenR
    String TruncLenF
    String TrimLeftF
    String TrimLeftR
    String NThreads
    File AtccPairedEnd

    # todo 测试使用cp复制历史的结果，待恢复
    # qiime dada2 denoise-paired --i-demultiplexed-seqs ${AtccPairedEnd} --p-trim-left-f ${TrimLeftF} --p-trim-left-r ${TrimLeftR} --p-trunc-len-f ${TruncLenF} --p-trunc-len-r ${TruncLenR} --p-n-threads ${NThreads} --o-table atcc_table.qza --o-representative-sequences atcc_seqs.qza --o-denoising-stats atcc_stats.qza

    command {
        cp /cromwell-executions/task_16s/5ba255d9-9f7c-4325-9a6e-58037a7c86d2/call-denoisePaired/execution/*.qza ./
        cp /cromwell-executions/task_16s/5ba255d9-9f7c-4325-9a6e-58037a7c86d2/call-denoisePaired/execution/stats.tsv ./
        cp /cromwell-executions/task_16s/5ba255d9-9f7c-4325-9a6e-58037a7c86d2/call-denoisePaired/execution/dna-sequences.fasta ./
        cp /cromwell-executions/task_16s/5ba255d9-9f7c-4325-9a6e-58037a7c86d2/call-denoisePaired/execution/feature-table.biom ./
        cp /cromwell-executions/task_16s/5ba255d9-9f7c-4325-9a6e-58037a7c86d2/call-denoisePaired/execution/otu-table.tsv ./

    }

    runtime {
        docker: "quay.io/qiime2/core:2022.2"
    }
    output {
        File atccTable = "atcc_table.qza"
        File atccStats = "atcc_stats.qza"
        File atccSeqs = "atcc_seqs.qza"
        File biomFile = "feature-table.biom"
        File otuTable = "otu-table.tsv"
        File statsFile = "stats.tsv"
        File sequencesFa = "dna-sequences.fasta"
    }
}

task tableSummary {
    File otuTable
    File metadata_file

    command {
        qiime feature-table summarize \
        --i-table ${otuTable} \
        --o-visualization table.qzv \
        --m-sample-metadata-file ${metadata_file}

        qiime tools export --input-path table.qzv --output-path ./
        rm -rf js licenses q2templateassets css *.html

    }

    runtime {
        docker: "quay.io/qiime2/core:2022.2"
    }
    output {
        File featureFreq_pdf = "feature-frequencies.pdf"
        File featureFreq_png = "feature-frequencies.png"
        File featureFreq_csv = "feature-frequency-detail.csv"
        File sampleFreq_pdf = "sample-frequencies.pdf"
        File sampleFreq_png = "sample-frequencies.png"
        File sampleFreq_csv = "sample-frequency-detail.csv"
    }

}

task alignToTree {
    File AtccSeqs

    command {
        qiime phylogeny align-to-tree-mafft-fasttree --i-sequences ${AtccSeqs} --o-alignment alignment_from_seqs.qza --o-masked-alignment masked_alignment_from_seqs.qza --o-tree tree_from_seqs.qza --o-rooted-tree rooted_tree_from_seqs.qza
    }

    runtime {
        docker: "quay.io/qiime2/core:2022.2"
    }
    output {
        File alignmentFromSeqs = "alignment_from_seqs.qza"
        File maskedAlignmentFromSeqs = "masked_alignment_from_seqs.qza"
        File treeFromSeqs = "tree_from_seqs.qza"
        File rootedTreeFromSeqs = "rooted_tree_from_seqs.qza"
    }
}

task classifySklearn {
    File Classifier
    File Reads
    String NJobs

    # todo 测试使用cp复制历史的结果，待恢复
    # qiime feature-classifier classify-sklearn --i-classifier ${Classifier} \
    # --i-reads ${Reads} --p-n-jobs ${NJobs} --o-classification taxonomy.qza
    # qiime tools export --input-path taxonomy.qza --output-path ./
    command {
        cp /cromwell-executions/task_16s/5ba255d9-9f7c-4325-9a6e-58037a7c86d2/call-classifySklearn/execution/taxonomy.*  ./
    }

    runtime {
        docker: "quay.io/qiime2/core:2022.2"
    }
    output {
        File classification = "taxonomy.qza"
        File taxonomy_file = "taxonomy.tsv"
    }
}

task rarefy {
    String SamplingDepth
    String WithReplacement
    File AtccTable

    command {
        qiime feature-table rarefy --i-table ${AtccTable} --p-sampling-depth ${SamplingDepth} --p-with-replacement ${WithReplacement} --o-rarefied-table rarefied_table.qza
    }

    runtime {
        docker: "quay.io/qiime2/core:2022.2"
    }
    output {
        File rarefiedTable = "rarefied_table.qza"
    }
}

task alphaDiversity {
    File RarefiedTable
    File RootedTreeFromSeqs
    Array[String] AlphaMetrics

    command {

        for matrix in ${sep=" " AlphaMetrics};do
        qiime diversity alpha --i-table ${RarefiedTable} --p-metric $matrix --o-alpha-diversity $matrix'.qza'
        qiime tools export --input-path $matrix'.qza' --output-path ./
        mv alpha-diversity.tsv $matrix'.tsv'
        done

        qiime diversity-lib faith-pd \
        --i-table ${RarefiedTable} \
        --i-phylogeny ${RootedTreeFromSeqs} \
        --o-vector faith-pd.qza

        qiime tools export --input-path faith-pd.qza --output-path ./
        mv alpha-diversity.tsv faith-pd.tsv
    }

    runtime {
        docker: "quay.io/qiime2/core:2022.2"
    }
    output {
        Array[File] matrix_qza = glob("*.qza")
        Array[File] matrix_tsv = glob("*.tsv")
    }
}

task betaDiversity {
    String Jobs
    String Pseudocount
    File MetadataFile
    String MetadataField
    File RarefiedTable
    File RootedTreeFromSeqs
    Array[String] BetaMetrics
    Array[String] BetaPhylo_Metrics

    command {
        for matrix in ${sep=" " BetaPhylo_Metrics};do
        qiime diversity-lib $matrix \
        --p-threads ${Jobs} \
        --i-table ${RarefiedTable} \
        --i-phylogeny ${RootedTreeFromSeqs} \
        --o-distance-matrix $matrix'.qza'
        done

        for matrix in ${sep=" " BetaMetrics};do
        qiime diversity beta \
        --p-n-jobs ${Jobs} \
        --p-metric $matrix \
        --i-table ${RarefiedTable} \
        --o-distance-matrix $matrix'.qza'
        done

        for matrix in ${sep=" " BetaMetrics} ${sep=" " BetaPhylo_Metrics};do
        qiime tools export --input-path $matrix'.qza' --output-path ./
        mv distance-matrix.tsv distance_matrix_$matrix'.tsv'

        for method in pcoa tsne umap;do
        qiime diversity $method --i-distance-matrix $matrix'.qza'  --o-$method $method'_'$matrix'.qza'
        qiime tools export --input-path $method'_'$matrix'.qza' --output-path ./
        mv ordination.txt ordination_$method'_'$matrix'.txt'
        done
        qiime diversity beta-group-significance \
        --i-distance-matrix $matrix'.qza' \
        --m-metadata-file ${MetadataFile} \
        --m-metadata-column ${MetadataField} \
        --o-visualization $matrix-${MetadataField}-significance.qzv \
        --p-pairwise
        qiime tools export --input-path $matrix-${MetadataField}-significance.qzv --output-path ./
        rm -rf *-boxplots.png *-boxplots.pdf index.html q2templateassets permanova-pairwise.csv
        mv raw_data.tsv $matrix-${MetadataField}-significance.tsv
        done
    }

    runtime {
        docker: "quay.io/qiime2/core:2022.2"
    }
    output {
        Array[File] matrix_qza = glob("*.qza")
        Array[File] distance_matrix_files = glob("distance_matrix_*.tsv")
        Array[File] ordination_pcoa_files = glob("ordination_pcoa_*.txt")
        Array[File] ordination_tsne_files = glob("ordination_tsne_*.txt")
        Array[File] ordination_umap_files = glob("ordination_umap_*.txt")
        Array[File] beta_significance_files = glob("*-significance.tsv")
    }
}

task barplot {
    File Table
    File Taxonomy
    File MetadataFile

    command {
        qiime taxa barplot --i-table ${Table} --i-taxonomy ${Taxonomy} --m-metadata-file ${MetadataFile} --o-visualization taxa-bar-plots.qzv
    }

    runtime {
        docker: "quay.io/qiime2/core:2022.2"
    }
    output {
        File visualization = "taxa-bar-plots.qzv"
    }
}

task alphaRarefaction {
    String MaxDepth
    File Table
    File Phylogeny
    File MetadataFile

    # --p-metrics chao1 shannon simpson
    command {
        qiime diversity alpha-rarefaction --i-table ${Table} \
        --i-phylogeny ${Phylogeny} --p-max-depth ${MaxDepth} \
        --m-metadata-file ${MetadataFile} --o-visualization alpha-rarefaction.qzv

        qiime tools export --input-path ./alpha-rarefaction.qzv --output-path ./
        rm -rf *.jsonp dist index.html q2templateassets

    }

    runtime {
        docker: "quay.io/qiime2/core:2022.2"
    }
    output {
        File visualization = "alpha-rarefaction.qzv"
        Array[File] alpha_rarefaction_csv = glob("*.csv")
    }
}

task alpha_group_significance {
    File qza
    File metadata
    String metric

    command {
        qiime diversity alpha-group-significance \
        --i-alpha-diversity ${qza} \
        --m-metadata-file ${metadata} \
        --o-visualization alpha.${metric}.qzv
    }

    runtime {
        docker: "quay.io/qiime2/core:2022.2"
    }
    output {
        File faith_pd_group_qzv = "alpha.${metric}.qzv"
    }
}

task platform_16s_report_task {
    String GroupField
    File FastqcDir
    File StatsFile
    File MetadataFile
    File OtuTable
    File Taxonomy
    File Sequences
    File DB_Red
    File DB_Alien

    command {
        platform_16s_report -q ${FastqcDir} -g ${GroupField} -c ${StatsFile} \
        -m ${MetadataFile} -u ${OtuTable} -s ${Sequences} -t ${Taxonomy} -o ./

        platform_16s_otu_stat -f table2.csv -g sample_group.csv -r ${DB_Red} -a ${DB_Alien} -o ./

        platform_16s_species_analysis -f table2.csv -g sample_group.csv -o ./
    }

    runtime {
        docker: "dockerhub.genostack.com:8090/angs/pipeline_data_collect:1.1.11"
    }
    output {
        File table1 = "table1.csv"
        File table2 = "table2.csv"
        File table3 = "table3.csv"
        File group = "sample_group.csv"
        File otu_stat_1 = "otu_stat_1.csv"
        File otu_stat_2 = "otu_stat_2.csv"
        File otu_stat_red = "otu_stat_red.csv"
        File otu_stat_alien = "otu_stat_alien.csv"
        File otu_depth = "otu_depth.csv"
        File g_by_sample = "species_analysis_g_by_sample.csv"
        File g_by_group = "species_analysis_g_by_group.csv"
        File s_by_sample = "species_analysis_s_by_sample.csv"
        File s_by_group = "species_analysis_s_by_group.csv"
        Array[File] tax_datas = glob("tax_*.csv")
        Array[File] venn_datas = glob("venn_data_*.csv")
        File tax_genus = "tax_Genus.csv"
        File tax_species = "tax_Species.csv"

    }
}

task report_edna_plot_task {
    File GroupFile
    String GroupField
    File OTUStat1
    File OTUStat2
    File OTUStatAlien
    File OTUStatRed
    File OTUDepth
    File G_By_Sample
    File G_By_Group
    File S_By_Sample
    File S_By_Group
    File TaxGenus
    File TaxSpecies

    Array[File] alpha_rarefaction_csv
    Array[File] alpha_vectors_tsv
    Array[File] distance_matrix_files
    Array[File] ordination_pcoa_files
    Array[File] ordination_tsne_files
    Array[File] ordination_umap_files
    Array[File] beta_significance_files

    command <<<
        plot_otu_stat_1.r -i ${OTUStat1} -o 物种分类鉴定统计_
        plot_otu_stat_2.r -i ${OTUStat2} -o OTU分类统计和注释_
        plot_otu_stat_3.r -i ${OTUStatAlien} -o 入侵物种清单图
        plot_otu_stat_4.r -i ${OTUStatRed} -o 中国物种红色名录统计图

        plot_wuzhong_bar.r -n 20 -i ${G_By_Sample} -o 属水平_样本水平_优势物种图
        plot_wuzhong_bar.r -n 20 -i ${G_By_Group} -o 属水平_组水平_优势物种图
        plot_wuzhong_bar.r -n 20 -i ${S_By_Sample} -o 种水平_样本水平_优势物种图
        plot_wuzhong_bar.r -n 20 -i ${S_By_Group} -o 种水平_组水平_优势物种图

        plot_wuzhong_bar_2.r -n 20 -i ${G_By_Sample} -o 属水平_样本水平_物种一致性比较图
        plot_wuzhong_bar_2.r -n 20 -i ${G_By_Group} -o 属水平_组水平_物种一致性比较图
        plot_wuzhong_bar_2.r -n 20 -i ${S_By_Sample} -o 种水平_样本水平_物种一致性比较图
        plot_wuzhong_bar_2.r -n 20 -i ${S_By_Group} -o 种水平_组水平_物种一致性比较图

        plot_wuzhong_flower.r -i ${TaxGenus} -n 属水平_样本水平_广泛分布物种分析图
        plot_wuzhong_flower.r -i ${TaxSpecies} -n 种水平_样本水平_广泛分布物种分析图

        depth.r -i ${OTUDepth} -o otu_depth
        run_rarefaction.sh -i ${sep="," alpha_rarefaction_csv} -g ${GroupField}
        run_alpha.sh -g ${GroupFile} -i ${sep="," alpha_vectors_tsv} -x ${GroupField}
        run_beta.sh -m ${GroupField} -i ${sep="," beta_significance_files}

        run_pcoa.sh -i ${sep="," ordination_pcoa_files} -g ${GroupFile}
        run_tsne_umap.sh -i ${sep="," ordination_tsne_files},${sep="," ordination_umap_files}  -g ${GroupFile}

    >>>
    runtime {
        docker: "dockerhub.genostack.com:8090/angs/platform_report_plot:1.0.8"
    }
    output {

        Array[File] otu_stat_1 = glob("物种分类鉴定统计_*")
        Array[File] otu_stat_2 = glob("OTU分类统计和注释_*")
        Array[File] otu_stat_a = glob("入侵物种清单图*")
        Array[File] otu_stat_r = glob("中国物种红色名录统计图*")

        Array[File] tax_analysis_1 = glob("*_优势物种图*")
        Array[File] tax_analysis_2 = glob("*_物种一致性比较图*")
        Array[File] tax_analysis_3 = glob("*广泛分布物种分析图*")

        Array[File] alpha_plots = glob("alpha_*.p*")
        Array[File] depth_plots = glob("otu_depth.p*")
        Array[File] alpha_rarefication_plot1 = glob("*plot1.p*")
        Array[File] alpha_rarefication_plot2 = glob("*plot2.p*")
        Array[File] beta_significance_plots = glob("beta_*_distance_to_*.p*")
        Array[File] pcoa_plots = glob("pcoa_*_plot.p*")
        Array[File] umap_plots = glob("umap_*_plot.p*")
        Array[File] tsne_plots = glob("tsne_*_plot.p*")
    }
}

task collect_report_data {
    File table1
    File table2
    File table3
    File featureFreq_png
    File featureFreq_pdf
    File sampleFreq_png
    File sampleFreq_pdf
    File OTUStat1
    File OTUStat2
    File OTUStatAlien
    File OTUStatRed
    File G_By_Sample
    File G_By_Group
    File S_By_Sample
    File S_By_Group
    File TaxGenus
    File TaxSpecies
    Array[File] OTUStat1Plot
    Array[File] OTUStat2Plot
    Array[File] OTUStatRedPlot
    Array[File] OTUStatAlienPlot
    Array[File] TaxAnalysis1Plot
    Array[File] TaxAnalysis2Plot
    Array[File] TaxAnalysis3Plot
    Array[File] alphaPlots
    Array[File] alphaDepthPlots
    Array[File] alphaRareficationPlot1
    Array[File] alphaRareficationPlot2
    Array[File] betaSignificancePlots
    Array[File] PCOA_Plots
    Array[File] UMAP_Plots
    Array[File] TSNE_Plots

    command {
        mkdir -p result/4.结果总结/4.1信息概览/4.1.1采样信息
        mkdir -p result/4.结果总结/4.1信息概览/4.1.2OTU鉴定概述
        mkdir -p result/4.结果总结/4.1信息概览/4.1.3OTU分布统计
        mkdir -p result/4.结果总结/4.1信息概览/4.1.4OTU频次统计
        mkdir -p result/4.结果总结/4.1信息概览/4.1.5物种分类鉴定统计
        mkdir -p result/4.结果总结/4.1信息概览/4.1.6OTU分类注释统计
        mkdir -p result/4.结果总结/4.1信息概览/4.1.7入侵物种清单
        mkdir -p result/4.结果总结/4.1信息概览/4.1.8中国物种红色名录
        mkdir -p result/4.结果总结/4.2物种分析/4.2.1优势物种分析
        mkdir -p result/4.结果总结/4.2物种分析/4.2.2物种一致性比较
        mkdir -p result/4.结果总结/4.2物种分析/4.2.3广泛分布物种分析
        mkdir -p result/4.结果总结/4.3多样性分析质控/4.3.1样本深度统计
        mkdir -p result/4.结果总结/4.3多样性分析质控/4.3.2测序深度饱和度测试
        mkdir -p result/4.结果总结/4.3多样性分析质控/4.3.3测序深度折线图
        mkdir -p result/4.结果总结/4.4α多样性分析
        mkdir -p result/4.结果总结/4.5β多样性分析/4.5.1Beta多样性

        cp ${sampleFreq_png} ${sampleFreq_pdf} result/4.结果总结/4.1信息概览/4.1.3OTU分布统计/
        cp ${featureFreq_png} ${featureFreq_pdf} result/4.结果总结/4.1信息概览/4.1.4OTU频次统计/

        cp ${sep=" " OTUStat1Plot} result/4.结果总结/4.1信息概览/4.1.5物种分类鉴定统计/
        cp ${sep=" " OTUStat2Plot} result/4.结果总结/4.1信息概览/4.1.6OTU分类注释统计/
        cp ${sep=" " OTUStatAlienPlot} result/4.结果总结/4.1信息概览/4.1.7入侵物种清单/
        cp ${sep=" " OTUStatRedPlot} result/4.结果总结/4.1信息概览/4.1.8中国物种红色名录/

        report_csv_to_excel -i ${OTUStat1} -o result/4.结果总结/4.1信息概览/4.1.5物种分类鉴定统计/物种分类鉴定统计表.xlsx
        report_csv_to_excel -i ${OTUStat2} -o result/4.结果总结/4.1信息概览/4.1.6OTU分类注释统计/OTU分类注释统计表.xlsx
        report_csv_to_excel -i ${OTUStatAlien} -o result/4.结果总结/4.1信息概览/4.1.7入侵物种清单/入侵物种清单表.xlsx
        report_csv_to_excel -i ${OTUStatRed} -o result/4.结果总结/4.1信息概览/4.1.8中国物种红色名录/中国物种红色名录统计表.xlsx

        cp ${sep=" " TaxAnalysis1Plot} result/4.结果总结/4.2物种分析/4.2.1优势物种分析/
        cp ${sep=" " TaxAnalysis2Plot} result/4.结果总结/4.2物种分析/4.2.2物种一致性比较/
        cp ${sep=" " TaxAnalysis3Plot} result/4.结果总结/4.2物种分析/4.2.3广泛分布物种分析/

        cp ${sep=" " alphaDepthPlots} result/4.结果总结/4.3多样性分析质控/4.3.1样本深度统计/
        cp ${sep=" " alphaRareficationPlot1} result/4.结果总结/4.3多样性分析质控/4.3.2测序深度饱和度测试/
        cp ${sep=" " alphaRareficationPlot2} result/4.结果总结/4.3多样性分析质控/4.3.3测序深度折线图/


        cp ${sep=" " alphaPlots} result/4.结果总结/4.4α多样性分析/
        cp ${sep=" " alphaRareficationPlot1} result/4.结果总结/4.4α多样性分析/
        cp ${sep=" " alphaRareficationPlot2} result/4.结果总结/4.4α多样性分析/
        cp ${sep=" " betaSignificancePlots} result/4.结果总结/4.5β多样性分析/4.5.1Beta多样性/
        cp ${sep=" " PCOA_Plots} result/4.结果总结/4.5β多样性分析/4.5.1Beta多样性/
        cp ${sep=" " UMAP_Plots} result/4.结果总结/4.5β多样性分析/4.5.1Beta多样性/
        cp ${sep=" " TSNE_Plots} result/4.结果总结/4.5β多样性分析/4.5.1Beta多样性/

        report_csv_to_excel -i ${G_By_Sample} -o result/4.结果总结/4.2物种分析/4.2.1优势物种分析/属水平_样本水平_优势物种表.xlsx
        report_csv_to_excel -i ${G_By_Group} -o result/4.结果总结/4.2物种分析/4.2.1优势物种分析/属水平_组水平_优势物种表.xlsx
        report_csv_to_excel -i ${S_By_Sample} -o result/4.结果总结/4.2物种分析/4.2.1优势物种分析/种水平_样本水平_优势物种表.xlsx
        report_csv_to_excel -i ${S_By_Group} -o result/4.结果总结/4.2物种分析/4.2.1优势物种分析/种水平_组水平_优势物种表.xlsx
        report_csv_to_excel -i ${G_By_Sample} -o result/4.结果总结/4.2物种分析/4.2.2物种一致性比较/属水平_样本水平_物种一致性比较.xlsx
        report_csv_to_excel -i ${G_By_Group} -o result/4.结果总结/4.2物种分析/4.2.2物种一致性比较/属水平_组水平_物种一致性比较.xlsx
        report_csv_to_excel -i ${S_By_Sample} -o result/4.结果总结/4.2物种分析/4.2.2物种一致性比较/种水平_样本水平_物种一致性比较.xlsx
        report_csv_to_excel -i ${S_By_Group} -o result/4.结果总结/4.2物种分析/4.2.2物种一致性比较/种水平_组水平_物种一致性比较.xlsx

        report_collect_to_json -i ${sampleFreq_png} -m ImageMultiple -o ./otu_sample_freq.json
        report_collect_to_json -i ${featureFreq_png} -m ImageMultiple -o ./otu_feature_freq.json

        report_collect_to_json -i ${sep="," OTUStat1Plot} -m ImageMultiple -o ./tax_stat_1.json
        report_collect_to_json -i ${sep="," OTUStat2Plot} -m ImageMultiple -o ./tax_stat_2.json
        report_collect_to_json -i ${sep="," OTUStatAlienPlot} -m ImageMultiple -o ./tax_stat_a.json
        report_collect_to_json -i ${sep="," OTUStatRedPlot} -m ImageMultiple -o ./tax_stat_r.json

        report_collect_to_json -i ${sep="," TaxAnalysis1Plot} -m ImageMultiple -o ./tax_analysis_1.json
        report_collect_to_json -i ${sep="," TaxAnalysis2Plot} -m ImageMultiple -o ./tax_analysis_2.json
        report_collect_to_json -i ${sep="," TaxAnalysis3Plot} -m ImageMultiple -o ./tax_analysis_3.json

        report_collect_to_json -i ${sep="," alphaDepthPlots} -m ImageMultiple -o ./alpha_depth_plots.json
        report_collect_to_json -i ${sep="," alphaRareficationPlot1} -m ImageMultiple -o ./alpha_rarefication_plot1.json
        report_collect_to_json -i ${sep="," alphaRareficationPlot2} -m ImageMultiple -o ./alpha_rarefication_plot2.json

        report_collect_to_json -i ${sep="," alphaPlots} -m ImageMultiple -o ./alpha_plots.json
        report_collect_to_json -i ${sep="," betaSignificancePlots} -m ImageMultiple -o ./beta_sig_plots.json

        report_collect_to_json -i ${sep="," PCOA_Plots} -m ImageMultiple -o ./pcoa_plots.json
        report_collect_to_json -i ${sep="," UMAP_Plots} -m ImageMultiple -o ./umap_plots.json
        report_collect_to_json -i ${sep="," TSNE_Plots} -m ImageMultiple -o ./tsne_plots.json

    }

    runtime {
        docker: "dockerhub.genostack.com:8090/angs/pipeline_data_collect:1.1.11"
    }
    output {
        File result = "result"
        File otu_sample_freq = "otu_sample_freq.json"
        File otu_feature_freq = "otu_feature_freq.json"
        File tax_stat_1 = "tax_stat_1.json"
        File tax_stat_2 = "tax_stat_2.json"
        File tax_stat_a = "tax_stat_a.json"
        File tax_stat_r = "tax_stat_r.json"
        File tax_analysis_1 = "tax_analysis_1.json"
        File tax_analysis_2 = "tax_analysis_2.json"
        File tax_analysis_3 = "tax_analysis_3.json"
        File alpha_plots = "alpha_plots.json"
        File alpha_depth_plots = "alpha_depth_plots.json"
        File alpha_rarefication_plot1 = "alpha_rarefication_plot1.json"
        File alpha_rarefication_plot2 = "alpha_rarefication_plot2.json"
        File beta_sig_plots = "beta_sig_plots.json"
        File pcoa_plots = "pcoa_plots.json"
        File umap_plots = "umap_plots.json"
        File tsne_plots = "tsne_plots.json"
    }
}