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

label: NonSpliced RNAseq workflow
doc: | 
  Workflow for NonSpliced RNAseq data alignment with multiple aligners.
  Steps:  
      - workflow_quality.cwl:
          - FastQC (control)
          - fastp (trimming)
      - bowtie2 (read mapping)
      - sam_to_sorted-bam
      - featurecounts (transcript read counts)
      - kallisto (transcript [pseudo]counts)

outputs:
  files_to_folder_fastqc:
    type: Directory
    label: FASTQC
    doc: Quality reporting by FASTQC
    outputSource: quality/files_to_folder_fastqc
  files_to_folder_filtered:
    label: Filtered reads folder
    doc: Output folder with filtered reads.
    type: Directory
    outputSource: quality/files_to_folder_filtered
  files_to_folder_bowtie2:
    type: Directory
    label: bowtie2 output
    doc: bowtie2 mapping results folder. Contains sorted bam file, metrics file and mapping statistics (stdout).
    outputSource: bowtie2_files_to_folder/results
  files_to_folder_featurecounts:
    type: Directory
    label: FeatureCounts output
    doc: FeatureCounts results folder. Contains readcounts, summary and mapping statistics (stdout).
    outputSource: featurecounts_files_to_folder/results
  files_to_folder_kallisto:
    type: Directory
    label: kallisto output
    doc: kallisto results folder. Contains transcript abundances, run info and summary.
    outputSource: kallisto_files_to_folder/results

inputs:
  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: Max memory
    default: 4000
  filter_rrna:
    type: boolean
    label: Filer rRNA
    doc: Filter rRNA from reads if true
    default: false
  prefix_id:
    type: string
    label: Filename prefix
    doc: Prefix of the output filenames.
    default: out
  forward_reads:
    type: File
    doc: forward sequence file locally
    label: forward reads
  reverse_reads:
    type: File
    doc: reverse sequence file locally
    label: reverse reads
  bowtie2-indexfolder:
    type: Directory
    label:  bowtie2 index
    doc: Folder location of the bowtie2 index files.
  kallisto-indexfolder:
    type: Directory?
    label: kallisto index
    doc: Folder location of the kallisto index file.
  gtf:
    type: File?
    label: GTF file
    doc: GTF file location

steps:
  #########################################
  # Workflow for quality and filtering using fastqc, fastp and optionally bbduk
  quality:
    label: Quality and filtering workflow
    doc: Quality assessment of illumina reads with rRNA filtering option
    in:
      filter_rrna: filter_rrna
      prefix_id: prefix_id
      forward_reads: forward_reads
      reverse_reads: reverse_reads
      threads: threads
      memory: memory
    run: ../workflows/workflow_quality.cwl
    out: [files_to_folder_fastqc, files_to_folder_filtered, QC_forward_reads, QC_reverse_reads]
  #########################################
  # bowtie2 alignment
  bowtie2:
    label: bowtie2
    doc: runs bowtie2 alignment on the genome with the quality filtered reads.
    in:
      prefix: prefix_id
      forward_reads: quality/QC_forward_reads
      reverse_reads: quality/QC_reverse_reads
      indexfolder: bowtie2-indexfolder
      threads: threads
    run: ../bowtie2/bowtie2_align_simple.cwl
    out:     
      [sam, metricsfile,bowtie2_stats]
  #########################################
  # convert sam file to sorted bam file
  sam_to_sorted-bam:
    label: sam to sorted bam
    doc: Converts a SAM file to a sorted BAM file
    in:
      prefix: 
       source: prefix_id
       valueFrom: $(self+"_bowtie2")
      sam: bowtie2/sam
      threads: threads
    run: ../samtools/sam_to_sorted-bam.cwl
    out: 
      [sortedbam]
  #########################################
  # Feature counts with GTF file and with bowtie2 mapped output
  featurecounts:
    label: FeatureCounts
    doc: Calculates gene counts with bowtie2 mapped data and input GTF file with FeatureCounts.
    in:
      prefix: prefix_id
      bam: sam_to_sorted-bam/sortedbam
      gtf: gtf
      threads: threads
    when: $(inputs.gtf != undefined)
    run: ../RNAseq/featurecounts.cwl
    out: 
      [readcounts, summary, overview]
  #########################################
  # kallisto transcript abundances
  kallisto:
    label: kallisto
    doc: Calculates transcript abundances
    in:
      gtf: gtf
      prefix: prefix_id
      forward_reads: quality/QC_forward_reads
      reverse_reads: quality/QC_reverse_reads
      indexfolder: kallisto-indexfolder
      threads: threads
    when: $(inputs.gtf != undefined)
    run: ../RNAseq/kallisto/kallisto_quant.cwl
    out:
      [abundance.h5, abundance.tsv, run_info, summary]

#############################################
#### Move to folder if not part of a workflow
  bowtie2_files_to_folder:
    label: bowtie2 output
    doc: Preparation of bowtie2 output files to a specific output folder
    in:
      files: 
        source: [sam_to_sorted-bam/sortedbam, bowtie2/metricsfile, bowtie2/bowtie2_stats]
        linkMerge: merge_flattened
        pickValue: all_non_null
      destination: 
        default: "3_bowtie2-alignment"
    run: ../expressions/files_to_folder.cwl
    out:
      [results]

  featurecounts_files_to_folder:
    label: FeatureCounts output
    doc: Preparation of FeatureCounts output files to a specific output folder
    in:
      gtf: gtf
      files: 
        source: [featurecounts/readcounts, featurecounts/summary, featurecounts/overview]
        linkMerge: merge_flattened
        pickValue: all_non_null
      destination:
        default: "4_FeatureCounts"
    when: $(inputs.gtf != undefined)
    run: ../expressions/files_to_folder.cwl
    out:
      [results]
      
  kallisto_files_to_folder:
    label: kallisto output
    doc: Preparation of kallisto output files to a specific output folder
    in:
      gtf: gtf
      files: 
        source: [kallisto/abundance.h5, kallisto/abundance.tsv, kallisto/run_info, kallisto/summary]
        linkMerge: merge_flattened
        pickValue: all_non_null
      destination: 
        default: "5_Kallisto"
    when: $(inputs.gtf != undefined)
    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