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

label: Read quality control, trimming and contamination filter. 
doc: | 
  Workflow for (paired) read quality control, trimming and contamination filtering. 
  Will output a merged set of read pairs, when multiple datasets are used.
  Steps:  
      - FastQC (read quality control)
      - fastp (read quality trimming)
      - bbduk used for rrna filtering
      - bbmap for contamination filter

outputs:
  reports_to_folder:
    type: Directory
    label: Filtered reads folder
    doc: Output folder with filtered reads, stats and reports.
    outputSource: reports_files_to_folder/results
  QC_forward_reads:
    type: File
    label: Filtered forward read
    doc: Filtered forward read with fastp and (optionally) rrna filtered. (this output is mainly used for other workflows)
    outputSource: 
      - bbduk_rrna/out_forward_reads
      - bbmap_contamination/out_forward_reads
    pickValue: first_non_null
  QC_reverse_reads:
    type: File
    label: Filtered reverse read
    doc: Filtered reverse read with fastp and (optionally) rrna filtered. (this output is mainly used for other workflows)
    outputSource: 
      - bbduk_rrna/out_reverse_reads
      - bbmap_contamination/out_reverse_reads
    pickValue: first_non_null

inputs:
  identifier:
    type: string
    doc: Identifier for this dataset used in this workflow
    label: identifier used
  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: Maximum memory in MB
    default: 4000
  filter_rrna:
    type: boolean
    label: filter rRNA
    doc: Optionally remove rRNA sequences from the reads.
    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
  bbmap_reference:
    type: string
    doc: bbmap reference fasta file for contamination filtering
    label: Contamination reference file
    default: "/unlock/references/databases/bbduk/GCA_000001405.28_GRCh38.p13_genomic.fna.gz"

  step:
    type: int?
    label: CWL base step number
    doc: Step number for order of steps
    default: 1

steps:
#############################################
#### FASTQC
  fastqc:
    label: FastQC
    doc: Quality assessment and report of reads
    in:
      fastqs: 
        source: [forward_reads, reverse_reads]
        linkMerge: merge_flattened
        pickValue: all_non_null
    run: ../fastqc/fastqc.cwl
    out: [html_files]
#############################################
#### Merge step for multiple forward fastqs
  fastq_merge_fwd:
    label: fastq pair merge file
    doc: Merge multiple paired ends of fastq files
    run: ../fastq/merge.cwl
    in:
      identifier: 
        source: identifier
        valueFrom: $(self+"_filtered_1.fq.gz")
      files: forward_reads
    out: [output_file]
  # Merge step for multiple reverse fastqs
  fastq_merge_rev:
    label: fastq pair merge file
    doc: Merge multiple paired ends of fastq files
    run: ../fastq/merge.cwl
    in:
      identifier: 
        source: identifier
        valueFrom: $(self+"_filtered_2.fq.gz")
      files: reverse_reads
    out: [output_file]
#############################################
#### fastp read quality filtering
  fastp:
    label: fastp
    doc: Read quality filtering and (barcode) trimming.
    run: ../fastp/fastp.cwl
    in:
      identifier: identifier
      forward_reads: fastq_merge_fwd/output_file
      reverse_reads: fastq_merge_rev/output_file
      threads: threads
    out: [out_forward_reads, out_reverse_reads, html_report, json_report]
#########################################
#### Workflow for reference based contamination filter
  bbmap_contamination:
    label: contamination filter (bbmap)
    doc: Filters contamination sequences from reads using bbmap
    run: ../bbmap/bbmap_filter.cwl
    in:
      identifier: identifier
      forward_reads: fastp/out_forward_reads
      reverse_reads: fastp/out_reverse_reads
      reference: bbmap_reference
      threads: threads
      memory: memory
    out: [out_forward_reads, out_reverse_reads, stats, covstats]
#############################################
#### bbduk rRNA Filter
  bbduk_rrna:
    label: rrna filter (bbduk)
    doc: Filters rrna sequences from reads using bbduk
    when: $(inputs.filter_rrna)
    run: ../bbmap/bbduk_rrna-filter.cwl
    in:
      identifier: 
        source: identifier
        valueFrom: $(self+"_filtered")
      filter_rrna: filter_rrna
      forward_reads: fastp/out_forward_reads
      reverse_reads: fastp/out_reverse_reads
      threads: threads
      memory: memory    
    out: [out_forward_reads, out_reverse_reads, stats_file,summary]

#############################################
#### Move to folder if not part of a workflow
  reports_files_to_folder:
    label: fastp output
    doc: Preparation of fastp output files to a specific output folder
    in:
      files: 
        source: [fastqc/html_files, fastp/html_report, fastp/json_report, bbmap_contamination/stats, bbmap_contamination/covstats, bbduk_rrna/stats_file, bbduk_rrna/summary]
        linkMerge: merge_flattened
        pickValue: all_non_null
      step: step
      destination: 
        valueFrom: |
          ${
            var step = inputs.step;
            return step+"_Read_Quality";
          }
    run: ../expressions/files_to_folder.cwl
    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: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