common-workflow-lab / cwl-intermediate-tutorial

https://common-workflow-lab.github.io/cwl-intermediate-tutorial/
Other
2 stars 3 forks source link

Demonstrate use of conditional with conditional secondary file #5

Open pvanheus opened 3 years ago

pvanheus commented 3 years ago

Below is a simple workflow that conditionally creates an index file for a FASTA reference file before using it as an input to a CWL tool.

#!/usr/bin/env bash

if ! [ -f data/reference.fa.sa ]; then
   bwa index data/reference.fa
fi

for sample in samples; do
   bwa mem data/reference.fa $sample > outputs/$sample.sam
done

# we have limited storage space so we
# cleanup indices once we are done
rm -f data/reference.fa.*

The step in this script that creates an index is in a conditional. In the beginner tutorial this script is presented without the conditional but this gives us an opportunity to introduce conditional steps in the intermediate tutorial.

Here is an example workflow using conditional:

#!/usr/bin/env cwl-runner

cwlVersion: v1.2
class: Workflow

requirements:
  InlineJavascriptRequirement: {}
  StepInputExpressionRequirement: {}
  MultipleInputFeatureRequirement: {}

inputs:
  reference:
    type: File
  reads:
    type: File[]
outputs:
  mapped:
    type: File
    outputSource: map/reads_stdout

steps:
  index:
    run: bwa/BWA-Index.cwl
    in:
      InputFile: reference
      IndexName:
        valueFrom: $(inputs.InputFile.basename)
    out:
      - index
    when:
      $( typeof inputs.InputFile.secondaryFiles === 'undefined' || inputs.InputFile.secondaryFiles.length != 4 )
  map:
    run: bwa/BWA-Mem.cwl
    in:
      InputFile: reads
      Index:
        source:
          - index/index
          - reference
        pickValue: first_non_null
    out:
      - reads_stdout

where the input can either be:

reference:
  class: File
  path: /home/pvh/Data/fasta/MN908947_3_Wuhan-Hu-1.fasta
  format: edam:format_1929
reads:
  - class: File
    path: /home/pvh/Data/fastq/foss_caseB_single.fastq.gz
    format: edam:format_1931

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

or (if the index is pre-computed)

reference:
  class: File
  path: /home/pvh/Data/fasta/MN908947_3_Wuhan-Hu-1.fasta.bwt
  format: edam:format_1929
  secondaryFiles:
    - class: File
      path: /home/pvh/Data/fasta/MN908947_3_Wuhan-Hu-1.fasta.ann
    - class: File
      path: /home/pvh/Data/fasta/MN908947_3_Wuhan-Hu-1.fasta.amb
    - class: File
      path: /home/pvh/Data/fasta/MN908947_3_Wuhan-Hu-1.fasta.pac
    - class: File
      path: /home/pvh/Data/fasta/MN908947_3_Wuhan-Hu-1.fasta.sa
reads:
  - class: File
    path: /home/pvh/Data/fastq/foss_caseB_single.fastq.gz
    format: edam:format_1931

$namespaces:
  edam: http://edamontology.org/
$schemas:
  - http://edamontology.org/EDAM_1.18.owl
pvanheus commented 3 years ago

Note - the above required some modifications to the BWA tools to run. Perhaps best to resolve https://github.com/common-workflow-library/bio-cwl-tools/pull/95 and then come back to this example.