#!/usr/bin/env cwl-runner
cwlVersion: v1.2
class: Workflow
requirements:
  StepInputExpressionRequirement: {}
  InlineJavascriptRequirement: {}
  MultipleInputFeatureRequirement: {}
  SubworkflowFeatureRequirement: {}
  ScatterFeatureRequirement: {}

label: Metagenomics workflow
doc: |
    Workflow for Metagenomics from raw reads to annotated bins.
    Steps:
      - workflow_quality.cwl:
          - FastQC (control)
          - fastp (quality trimming)
          - bbmap contamination filter
      - SPAdes (Assembly)
      - QUAST (Assembly quality report)
      - BBmap (Read mapping to assembly)
      - MetaBat2 (binning)
      - CheckM (bin completeness and contamination)
      - GTDB-Tk (bin taxonomic classification)

outputs:
  filtered_stats:
    label: Filtered statistics
    doc: Statistics on quality and preprocessing of the reads
    type: Directory
    outputSource: workflow_quality/reports_to_folder
  spades_output:
    label: SPADES
    doc: Metagenome assembly output by SPADES
    type: Directory
    outputSource: spades_files_to_folder/results
  quast_output:
    label: QUAST
    doc: Quast analysis output folder
    type: Directory
    outputSource: quast_files_to_folder/results
  bam_output:
    label: BAM files
    doc: Mapping results in indexed BAM format
    type: Directory
    outputSource: sorted_bam_files_to_folder/results
  metabat2_output:
    label: MetaBat2
    doc: MetaBat2 output directory
    type: Directory
    outputSource: metabat_files_to_folder/results
  checkm_output:
    label: CheckM
    doc: CheckM output directory
    type: Directory
    outputSource: checkm_files_to_folder/results
  gtdbtk_output:
    label: GTDB-Tk
    doc: GTDB-Tk output directory
    type: Directory
    outputSource: gtdbtk_files_to_folder/results
  # sapp_output:
  #   label: SAPP Genomes
  #   type: Directory
  #   doc: Folder containing genomes annotated by SAPP
  #   outputSource: sapp_files_to_folder/results

inputs:
  identifier:
    type: string
    doc: Identifier for this dataset used in this workflow
    label: identifier used
  forward_reads:
    type: File[]
    doc: forward sequence file locally
    label: forward reads
  reverse_reads:
    type: File[]
    doc: reverse sequence file locally
    label: reverse reads
  threads:
    type: int?
    doc: number of threads to use for computational processes
    label: number of threads
    default: 2
  memory:
    type: int?
    doc: maximum memory usage in megabytes
    label: memory usage (mb)
    default: 4000
  pacbio_reads:
    type: File[]?
    doc: file with PacBio reads locally
    label: pacbio reads
  bbmap_reference:
    type: string
    doc: bbmap reference fasta file for contamination filtering
    label: contamination reference file
  run_gtdbtk:
    type: boolean
    label: Run GTDB-Tk
    doc: Run GTDB-Tk taxonomic bin classification when true
    default: true


steps:
#############################################
#### Workflow for quality and filtering using fastqc, fastp and optionally bbduk
  workflow_quality:
    label: Quality and filtering workflow
    doc: Quality assessment of illumina reads with rRNA filtering option
    run: workflow_quality.cwl
    in:
      forward_reads: forward_reads
      reverse_reads: reverse_reads
      bbmap_reference: bbmap_reference
      memory: memory
      threads: threads
      identifier: identifier
      step: 
        default: 1
    out: [QC_reverse_reads, QC_forward_reads, reports_to_folder]
#############################################
#### assembly using SPADES
  workflow_spades:
    doc: Genome assembly using spades with illumina/pacbio reads
    label: SPADES assembly
    run: ../assembly/spades.cwl
    in:
      forward_reads:
        source: [ workflow_quality/QC_forward_reads ]
        linkMerge: merge_nested
      reverse_reads: 
        source: [ workflow_quality/QC_reverse_reads ]
        linkMerge: merge_nested
      pacbio_reads: pacbio_reads
      metagenomics:
        default: true
      memory: memory
      threads: threads
    out: [contigs, scaffolds, assembly_graph, contigs_assembly_paths, scaffolds_assembly_paths, contigs_before_rr, params, log, internal_config, internal_dataset]
#############################################
#### assembly quality assessment using quast
  workflow_quast:
    doc: Genome assembly quality assessment using Quast
    label: Quast workflow
    run: ../quast/quast.cwl
    in:
      assembly: workflow_spades/contigs
    out: [basicStats, icarusDir, icarusHtml, quastReport, quastLog, transposedReport]
#############################################
#### read mapping using bbmap
  workflow_bbmap:
    label: bbmap read mapping
    doc: Illumina read mapping using BBmap on assembled contigs
    run: ../bbmap/bbmap.cwl
    in:
      identifier: identifier
      reference: workflow_spades/contigs
      forward_reads: workflow_quality/QC_forward_reads
      reverse_reads: workflow_quality/QC_reverse_reads
      memory: memory
    out: [sam, stats, covstats, rpkm]
#############################################
#### read mapping using bbmap
  workflow_sam_to_sorted_bam:
    label: sam conversion to sorted bam
    doc: sam file conversion to a sorted indexed bam file
    run: ../samtools/sam_to_sorted-bam.cwl
    in:
      identifier: identifier
      sam: workflow_bbmap/sam
      threads: threads
    out: [sortedbam]
#############################################
#### contig depths file retrieval metabat2
  workflow_metabat2_contig_depths:
    label: Contig depths 
    doc: metabatContigDepths to obtain the depth file used in the binning process
    run: ../metagenomics/metabat2/metabatContigDepths.cwl
    in:
      # bamFile: workflow_merge_bam_files/sortedbam
      bamFile: workflow_sam_to_sorted_bam/sortedbam
    out: [depths]
#############################################
#### assembly binning using metabat2
  workflow_metabat2:
    doc: Binning procedure using metabat2
    label: metabat2 binning
    run: ../metagenomics/metabat2/metabat2.cwl
    in:
      identifier: identifier
      threads: threads
      assembly: workflow_spades/contigs
      depths: workflow_metabat2_contig_depths/depths
    out: [bin_dir, bins, log]
#############################################
#### CheckM bin quality assessment
  workflow_checkm:
    doc: CheckM bin quality assessment
    label: CheckM
    run: ../metagenomics/checkm/checkm_lineagewf.cwl
    in:
      identifier: identifier
      threads: threads
      bin_dir: workflow_metabat2/bin_dir
    out: [checkm_out_table, checkm_out_folder]
#############################################
#### Get unbinned contigs fasta
  workflow_getunbinned:
    doc: Get unbinned contigs fasta
    label: unbinned_contigs
    run: ../metagenomics/get_unbinned_contigs.cwl
    in:
      identifier: identifier
      threads: threads
      assembly_fasta: workflow_spades/contigs
      bin_dir: workflow_metabat2/bin_dir
    out: [unbinned_fasta]
#############################################
#### Taxomic assigment of bins with GTDB-Tk
  workflow_gtdbtk:
    doc: Taxomic assigment of bins with GTDB-Tk
    label: GTDBTK
    when: $(inputs.run_gtdbtk)
    run: ../metagenomics/gtdbtk/gtdbtk_classify_wf.cwl
    in:
      run_gtdbtk: run_gtdbtk
      identifier: identifier
      threads: threads
      bin_dir: workflow_metabat2/bin_dir
    out: [gtdbtk_summary, gtdbtk_out_folder]
#############################################
#### Compress GTDB-Tk output folder
  workflow_compress_gtdbtk:
    doc: Compress GTDB-Tk output folder
    label: Compress GTDB-Tk
    when: $(inputs.run_gtdbtk)
    run: ../bash/compress_directory.cwl
    in:
      run_gtdbtk: run_gtdbtk
      indir: workflow_gtdbtk/gtdbtk_out_folder
    out: [outfile]
#############################################
  # bin annotation using SAPP
  # workflow_sapp:
  #   doc: Semantic Annotation Platform with Provenance to annotate each bin obtained from metabat2
  #   label: Semantic Annotation Platform with Provenance
  #   run: workflow_sapp.cwl
  #   scatter: [fasta]
  #   in:
  #     fasta: workflow_metabat/bins
  #     codon:
  #       valueFrom: ${ return 11; }
  #     identifier:
  #       valueFrom: ${ return inputs.fasta.nameroot; }
  #     single:
  #       default: true
  #     threads: threads
  #   out: [conversion, prodigal, interproscan]
#############################################
#### Compress large Spades output
  compress_spades:
    label: Spades compressed
    doc: Compress the large Spades assembly output files
    run: ../bash/pigz.cwl
    scatter: [inputfile]
    scatterMethod: dotproduct
    in:
      threads: threads
      inputfile:
        source: [workflow_spades/contigs, workflow_spades/scaffolds,workflow_spades/assembly_graph,workflow_spades/contigs_before_rr, workflow_spades/contigs_assembly_paths,workflow_spades/scaffolds_assembly_paths]
        linkMerge: merge_flattened
        pickValue: all_non_null
    out: [outfile]
#############################################
#### Move to folder if not part of a workflow
  spades_files_to_folder:
    doc: Preparation of spades output files to a specific output folder
    label: SPADES output folder
    run: ../expressions/files_to_folder.cwl
    in:
      files:
        source: [compress_spades/outfile, workflow_spades/params, workflow_spades/log, workflow_spades/internal_config, workflow_spades/internal_dataset]
        linkMerge: merge_flattened
        pickValue: all_non_null
      destination:
        valueFrom: $("2_SPAdes_Assembly")
    out:
      [results]
#############################################
#### Move to folder if not part of a workflow
  quast_files_to_folder:
    doc: Preparation of quast output files to a specific output folder
    label: QUAST output folder
    run: ../expressions/files_to_folder.cwl
    in:
      identifier: identifier
      files: 
        source: [workflow_quast/quastLog, workflow_quast/icarusHtml, workflow_quast/quastReport, workflow_quast/transposedReport]
        linkMerge: merge_flattened
      folders:
        source: [workflow_quast/basicStats, workflow_quast/icarusDir]
        linkMerge: merge_flattened
      destination:
        valueFrom: $("3_Quast_AsssemblyQuality_")$(inputs.identifier)
    out:
      [results]
#############################################
#### Move to folder if not part of a workflow
  sorted_bam_files_to_folder:
    doc: Preparation of mapped reads (sorted bam files) to a specific output folder
    label: Mapped reads output folder
    run: ../expressions/files_to_folder.cwl
    in:
      files: 
        source: [workflow_sam_to_sorted_bam/sortedbam ,workflow_bbmap/stats, workflow_bbmap/covstats, workflow_bbmap/rpkm]
        linkMerge: merge_flattened
      destination:
        valueFrom: $("4_BBMAP_ReadMapping")
    out:
      [results]
#############################################
#### Move to folder if not part of a workflow
  metabat_files_to_folder:
    doc: Preparation of MetaBat2 output files + unbinned contigs to a specific output folder
    label: MetaBat2 output folder
    run: ../expressions/files_to_folder.cwl
    in:
      files: 
        source: [workflow_metabat2/log, workflow_metabat2/bins, workflow_getunbinned/unbinned_fasta]
        linkMerge: merge_flattened
      destination:
        valueFrom: $("5_MetaBat2_Bins")
    out:
      [results]
#############################################
#### Move to folder if not part of a workflow
  checkm_files_to_folder:
    doc: Preparation of CheckM output files to a specific output folder
    label: CheckM output
    run: ../expressions/files_to_folder.cwl
    in:
      files: 
        source: [workflow_checkm/checkm_out_table]
        linkMerge: merge_flattened
      folders:
        source: [workflow_checkm/checkm_out_folder]
        linkMerge: merge_flattened
      destination:
        valueFrom: $("6_CheckM_binQuality")
    out:
      [results]
#############################################
#### Move to folder if not part of a workflow
  gtdbtk_files_to_folder:
    doc: Preparation of GTDB-Tk output files to a specific output folder
    label: GTBD-Tk output folder
    when: $(inputs.run_gtdbtk)
    run: ../expressions/files_to_folder.cwl
    in:
      run_gtdbtk: run_gtdbtk
      files:
        source: [workflow_gtdbtk/gtdbtk_summary,workflow_compress_gtdbtk/outfile]
        linkMerge: merge_flattened
        pickValue: all_non_null
      destination:
        valueFrom: $("7_GTDB-Tk_binTaxonomy")
    out:
      [results]
#################################################
  #### Move to folder if not part of a workflow
  # sapp_files_to_folder:
  #   doc: Preparation of sapp output files to a specific output folder
  #   label: SAPP output
  #   run: ../expressions/files_to_folder.cwl
  #   in:
  #     files: 
  #       source: [workflow_sapp/conversion, workflow_sapp/prodigal , workflow_sapp/interproscan]
  #       linkMerge: merge_flattened
  #     destination: 
  #       valueFrom: $("8_SAPP")
  #   out:
  #     [results]
#############################################

s:author:
  - class: s:Person
    s:identifier: https://orcid.org/0000-0001-8172-8981
    s:email: mailto:jasper.koehorst@wur.nl
    s:name: Jasper Koehorst
  - class: s:Person
    s:identifier: https://orcid.org/0000-0001-9524-5964
    s:email: mailto:bart.nijsse@wur.nl
    s:name: Bart Nijsse

s:citation: https://m-unlock.nl
s:codeRepository: https://gitlab.com/m-unlock/cwl
s:dateCreated: "2020-00-00"
s:dateModified: "2021-05-00"
s:license: https://spdx.org/licenses/Apache-2.0 
s:copyrightHolder: "UNLOCK - Unlocking Microbial Potential"

$namespaces:
  s: https://schema.org/
  edam: http://edamontology.org/

$schemas:
 - http://edamontology.org/EDAM_1.18.owl
