#!/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 (trimming)
      - Kraken2 (Taxonomic Read Classification
      - HumANN 3
      - Metaphlan 3


outputs:
  fastqc_output:
    label: FASTQC
    doc: Quality reporting by FASTQC
    type: Directory[]
    outputSource: workflow_quality/files_to_folder_fastqc
  filter_output:
    label: Filtered reads
    doc: Reads filtered output folder
    type: Directory[]
    outputSource: workflow_quality/files_to_folder_filtered
  kraken2_output:
    label: KRAKEN2
    doc: Classification output folder by kraken
    type: Directory
    outputSource: kraken_files_to_folder/results

inputs:
  identifier:
    type: string
    doc: Identifier for this dataset used in this workflow
    label: identifier used
  filter_rrna:
    type: boolean
    doc: rRNA read filtering using ...
    label: rRNA filtering
    default: false
  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
  gzip:
    type:
    - "null"
    - boolean
    doc: input data is gzip compressed
    label: gzip compression
    default: false
  kraken_database:
    type: string
    doc: database location of kraken2
    label: Kraken2 database

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
    scatter: [forward_reads, reverse_reads]
    scatterMethod: dotproduct
    in:
      identifier: identifier
      filter_rrna: filter_rrna
      forward_reads: forward_reads
      reverse_reads: reverse_reads
      memory: memory
      threads: threads
    out: [QC_reverse_reads, QC_forward_reads, files_to_folder_filtered, files_to_folder_fastqc]
  #########################################
  # Workflow for the kraken run
  workflow_kraken:
    label: Kraken2 workflow
    doc: Read classification using the kraken2 database
    run: ../metagenomics/kraken2/kraken2.cwl
    scatter: [forward_reads, reverse_reads]
    scatterMethod: dotproduct
    in:
      identifier: identifier
      threads: threads
      database: kraken_database
      forward_reads: workflow_quality/QC_forward_reads
      reverse_reads: workflow_quality/QC_reverse_reads
      gzip: gzip # Based on regex?
    out: [kraken, report]
#############################################
#### Move to folder if not part of a workflow
  kraken_files_to_folder:
    doc: Preparation of kraken output files to a specific output folder
    label: Kraken output
    run: ../expressions/files_to_folder.cwl
    in:
      files: 
        source: [workflow_kraken/kraken, workflow_kraken/report]
        linkMerge: merge_flattened
        pickValue: all_non_null
      destination: 
        valueFrom: $("3_Kraken")
    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: "2021-00-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
