#!/usr/bin/env cwl-runner

cwlVersion: v1.2

class: Workflow

label: "Retrieval of genomes using the GCA identifiers from ENA"

doc: |
    Runs the genome retrieval application with genome identifiers from the European Nucleotide Archive

requirements:
 - class: InlineJavascriptRequirement
 - class: ScatterFeatureRequirement
 - class: MultipleInputFeatureRequirement
 - class: SubworkflowFeatureRequirement
 - class: StepInputExpressionRequirement

hints:
  DockerRequirement:
    dockerPull: munlock/base

inputs:
  enaBrowserTools:
    type: string
    doc: Path of the local enaBrowserTools git repository
    label: enaBrowserTools location
    default: /unlock/infrastructure/binaries/enaBrowserTools
  identifier:
    type: string
    doc: Genome Accession number according to ENA
    label: Genome accession number (GCA_....)
  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

outputs:
  x12:
    type: File[]?
    outputSource: [rename/file_renamed, enaBrowserTools/xml]
    linkMerge: merge_flattened
    pickValue: all_non_null

steps:
  #########################################
  # The ENA downloader
  enaBrowserTools:
    run: ../ena/enabrowsertools.cwl
    in:
      enaBrowserTools: enaBrowserTools
      gca: identifier
    out: [gz, xml, dat]
  #########################################
  # Decompress gz files
  decompress:
    when: $(inputs.infile != null)
    run: ../bash/decompress.cwl
    in:
      infile: enaBrowserTools/gz
    out: [outfile]
  # #########################################
  merge: 
    # when: $(inputs.infiles != null)
    run: ../bash/concatenate.cwl
    in:
      infiles: 
        source: [decompress/outfile, enaBrowserTools/dat]
        linkMerge: merge_flattened
        pickValue: all_non_null
      outname: identifier
    out: [output]
  #########################################
  #
  compress:
    run: ../bash/compress.cwl
    in:
      infile: merge/output
    out: [output]
  #########################################
  #
  rename:
    run: ../bash/rename.cwl
    in:
      identifier: identifier
      rename_this: compress/output
      newname: 
        valueFrom: $(inputs.identifier + ".dat.gz")
    out: [file_renamed]


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

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
