cwlVersion: v1.0
class: Workflow
label: ECCB18 tutorial - metagenomics pipeline
requirements:
  - class: ScatterFeatureRequirement
  - class: StepInputExpressionRequirement
  - class: InlineJavascriptRequirement
  - class: MultipleInputFeatureRequirement
  - class: SubworkflowFeatureRequirement


###  INPUTS   ################################################
inputs:
  forward_reads: File
  reverse_reads: File

###  OUTPUTS   ###############################################
outputs:
  bam:
    type: File
    outputSource: joinBamWithIndex/bamWithIndex
  bins:
    type: Directory
    outputSource: binContigs/bins
  binLog:
    type: File
    outputSource: binContigs/log


###  STEPS   #################################################
steps:
  mapReadsToAssembly:
    label: 'map reads to assembly'
    run: tools/bbmap.cwl
    in:
      forward: forward_reads
      reverse: reverse_reads
      reference: assembleMegahit/assembly
    out:
      - sam
      - stats
      - covstats
      - rpkm

  convertSamToBam:
    label: 'convert SAM to BAM format'
    run: tools/samtoolsView.cwl
    in:
      inputFile: mapReadsToAssembly/sam
      outputBam:
        default: true
      uncompressed:
        default: true
      outputFilename:
        default: 'megahit.bam'
    out:
      - stdout

  sortBam:
    label: 'sort BAM file'
    run: tools/samtoolsSort.cwl
    in:
      inputFile: convertSamToBam/stdout
    out:
      - bamFile

  indexBam:
    label: 'index BAM file'
    run: tools/samtoolsIndex.cwl
    in:
      bamFile: sortBam/bamFile
    out:
      - index

  joinBamWithIndex:
    run: ../tools/joinBamWithIndex.cwl
    in:
      bamFile: sortBam/bamFile
      bamIndexFile: indexBam/index
    out:
      - bamWithIndex

  calculateDepthsForBinning:
    label: 'calculate BAM contig depths'
    run: ../metagenomics/metabat2/metabatContigDepths.cwl
    in:
      bamFile: joinBamWithIndex/bamWithIndex
    out:
      - depths

  binContigs:
    label: 'sort contigs into bins'
    run: ../metagenomics/metabat2/metabat2.cwl
    in:
      assembly: assembleMegahit/assembly
      depths: calculateDepthsForBinning/depths
    out:
      - bins
      - log
