#!/usr/bin/env bash

# genome_alignment_and_quant main
# 
# This wrapper script is auto-generated by viash 0.9.0 and is thus a derivative
# work thereof. This software comes with ABSOLUTELY NO WARRANTY from Data
# Intuitive.
# 
# The component may contain files which fall under a different license. The
# authors of this component should specify the license in the header of such
# files, or include a separate license file detailing the licenses of all included
# files.

set -e

if [ -z "$VIASH_TEMP" ]; then
  VIASH_TEMP=${VIASH_TEMP:-$VIASH_TMPDIR}
  VIASH_TEMP=${VIASH_TEMP:-$VIASH_TEMPDIR}
  VIASH_TEMP=${VIASH_TEMP:-$VIASH_TMP}
  VIASH_TEMP=${VIASH_TEMP:-$TMPDIR}
  VIASH_TEMP=${VIASH_TEMP:-$TMP}
  VIASH_TEMP=${VIASH_TEMP:-$TEMPDIR}
  VIASH_TEMP=${VIASH_TEMP:-$TEMP}
  VIASH_TEMP=${VIASH_TEMP:-/tmp}
fi

# define helper functions
# ViashQuote: put quotes around non flag values
# $1     : unquoted string
# return : possibly quoted string
# examples:
#   ViashQuote --foo      # returns --foo
#   ViashQuote bar        # returns 'bar'
#   Viashquote --foo=bar  # returns --foo='bar'
function ViashQuote {
  if [[ "$1" =~ ^-+[a-zA-Z0-9_\-]+=.+$ ]]; then
    echo "$1" | sed "s#=\(.*\)#='\1'#"
  elif [[ "$1" =~ ^-+[a-zA-Z0-9_\-]+$ ]]; then
    echo "$1"
  else
    echo "'$1'"
  fi
}
# ViashRemoveFlags: Remove leading flag
# $1     : string with a possible leading flag
# return : string without possible leading flag
# examples:
#   ViashRemoveFlags --foo=bar  # returns bar
function ViashRemoveFlags {
  echo "$1" | sed 's/^--*[a-zA-Z0-9_\-]*=//'
}
# ViashSourceDir: return the path of a bash file, following symlinks
# usage   : ViashSourceDir ${BASH_SOURCE[0]}
# $1      : Should always be set to ${BASH_SOURCE[0]}
# returns : The absolute path of the bash file
function ViashSourceDir {
  local source="$1"
  while [ -h "$source" ]; do
    local dir="$( cd -P "$( dirname "$source" )" >/dev/null 2>&1 && pwd )"
    source="$(readlink "$source")"
    [[ $source != /* ]] && source="$dir/$source"
  done
  cd -P "$( dirname "$source" )" >/dev/null 2>&1 && pwd
}
# ViashFindTargetDir: return the path of the '.build.yaml' file, following symlinks
# usage   : ViashFindTargetDir 'ScriptPath'
# $1      : The location from where to start the upward search
# returns : The absolute path of the '.build.yaml' file
function ViashFindTargetDir {
  local source="$1"
  while [[ "$source" != "" && ! -e "$source/.build.yaml" ]]; do
    source=${source%/*}
  done
  echo $source
}
# see https://en.wikipedia.org/wiki/Syslog#Severity_level
VIASH_LOGCODE_EMERGENCY=0
VIASH_LOGCODE_ALERT=1
VIASH_LOGCODE_CRITICAL=2
VIASH_LOGCODE_ERROR=3
VIASH_LOGCODE_WARNING=4
VIASH_LOGCODE_NOTICE=5
VIASH_LOGCODE_INFO=6
VIASH_LOGCODE_DEBUG=7
VIASH_VERBOSITY=$VIASH_LOGCODE_NOTICE

# ViashLog: Log events depending on the verbosity level
# usage: ViashLog 1 alert Oh no something went wrong!
# $1: required verbosity level
# $2: display tag
# $3+: messages to display
# stdout: Your input, prepended by '[$2] '.
function ViashLog {
  local required_level="$1"
  local display_tag="$2"
  shift 2
  if [ $VIASH_VERBOSITY -ge $required_level ]; then
    >&2 echo "[$display_tag]" "$@"
  fi
}

# ViashEmergency: log events when the system is unstable
# usage: ViashEmergency Oh no something went wrong.
# stdout: Your input, prepended by '[emergency] '.
function ViashEmergency {
  ViashLog $VIASH_LOGCODE_EMERGENCY emergency "$@"
}

# ViashAlert: log events when actions must be taken immediately (e.g. corrupted system database)
# usage: ViashAlert Oh no something went wrong.
# stdout: Your input, prepended by '[alert] '.
function ViashAlert {
  ViashLog $VIASH_LOGCODE_ALERT alert "$@"
}

# ViashCritical: log events when a critical condition occurs
# usage: ViashCritical Oh no something went wrong.
# stdout: Your input, prepended by '[critical] '.
function ViashCritical {
  ViashLog $VIASH_LOGCODE_CRITICAL critical "$@"
}

# ViashError: log events when an error condition occurs
# usage: ViashError Oh no something went wrong.
# stdout: Your input, prepended by '[error] '.
function ViashError {
  ViashLog $VIASH_LOGCODE_ERROR error "$@"
}

# ViashWarning: log potentially abnormal events
# usage: ViashWarning Something may have gone wrong.
# stdout: Your input, prepended by '[warning] '.
function ViashWarning {
  ViashLog $VIASH_LOGCODE_WARNING warning "$@"
}

# ViashNotice: log significant but normal events
# usage: ViashNotice This just happened.
# stdout: Your input, prepended by '[notice] '.
function ViashNotice {
  ViashLog $VIASH_LOGCODE_NOTICE notice "$@"
}

# ViashInfo: log normal events
# usage: ViashInfo This just happened.
# stdout: Your input, prepended by '[info] '.
function ViashInfo {
  ViashLog $VIASH_LOGCODE_INFO info "$@"
}

# ViashDebug: log all events, for debugging purposes
# usage: ViashDebug This just happened.
# stdout: Your input, prepended by '[debug] '.
function ViashDebug {
  ViashLog $VIASH_LOGCODE_DEBUG debug "$@"
}

# find source folder of this component
VIASH_META_RESOURCES_DIR=`ViashSourceDir ${BASH_SOURCE[0]}`

# find the root of the built components & dependencies
VIASH_TARGET_DIR=`ViashFindTargetDir $VIASH_META_RESOURCES_DIR`

# define meta fields
VIASH_META_NAME="genome_alignment_and_quant"
VIASH_META_FUNCTIONALITY_NAME="genome_alignment_and_quant"
VIASH_META_EXECUTABLE="$VIASH_META_RESOURCES_DIR/$VIASH_META_NAME"
VIASH_META_CONFIG="$VIASH_META_RESOURCES_DIR/.config.vsh.yaml"
VIASH_META_TEMP_DIR="$VIASH_TEMP"


# ViashHelp: Display helpful explanation about this executable
function ViashHelp {
  echo "genome_alignment_and_quant main"
  echo ""
  echo "A viash sub-workflow for genome alignment and quantification stage of"
  echo "nf-core/rnaseq pipeline."
  echo ""
  echo "Input:"
  echo "    --id"
  echo "        type: string, required parameter"
  echo "        example: foo"
  echo "        ID of the sample."
  echo ""
  echo "    -i, --fastq_1"
  echo "        type: file, required parameter, file must exist"
  echo "        example: input.fastq.gz"
  echo "        Path to the sample (or read 1 of paired end sample)."
  echo ""
  echo "    --fastq_2"
  echo "        type: file, file must exist"
  echo "        Path to read 2 of the sample."
  echo ""
  echo "    --strandedness"
  echo "        type: string"
  echo "        choices: [ forward, reverse, unstranded ]"
  echo "        Sample strand-specificity. Must be one of unstranded, forward, or"
  echo "        reverse"
  echo ""
  echo "    --gtf"
  echo "        type: file, file must exist"
  echo "        GTF file"
  echo ""
  echo "    --transcript_fasta"
  echo "        type: file, file must exist"
  echo "        Fasta file of the reference transcriptome."
  echo ""
  echo "    --star_index"
  echo "        type: file, file must exist"
  echo "        STAR index directory."
  echo ""
  echo "    --star_ignore_sjdbgtf"
  echo "        type: boolean"
  echo "        default: false"
  echo "        When using pre-built STAR indices do not re-extract and use splice"
  echo "        junctions from the GTF file"
  echo ""
  echo "    --seq_platform"
  echo "        type: string"
  echo "        Sequencing platform."
  echo ""
  echo "    --seq_center"
  echo "        type: string"
  echo "        Sequencing center."
  echo ""
  echo "    --extra_star_align_args"
  echo "        type: string"
  echo "        default:"
  echo "        Extra arguments to pass to STAR alignment command in addition to"
  echo "        defaults defined by the pipeline."
  echo ""
  echo "    --bam_csi_index"
  echo "        type: boolean"
  echo "        default: false"
  echo "        Create a CSI index for BAM files instead of the traditional BAI index."
  echo "        This will be required for genomes with larger chromosome sizes."
  echo ""
  echo "    --umi_dedup_stats"
  echo "        type: boolean"
  echo "        default: false"
  echo "        Generate output stats when running \"umi_tools dedup\"."
  echo ""
  echo "    --with_umi"
  echo "        type: boolean"
  echo "        default: false"
  echo "        Enable UMI-based read deduplication."
  echo ""
  echo "    --salmon_quant_libtype"
  echo "        type: string"
  echo "        Override Salmon library type inferred based on strandedness defined in"
  echo "        meta object."
  echo ""
  echo "    --extra_salmon_quant_args"
  echo "        type: string"
  echo "        default:"
  echo "        Extra arguments to pass to salmon quant command in addition to defaults"
  echo "        defined by the pipeline."
  echo ""
  echo "    --gtf_group_features"
  echo "        type: string"
  echo "        default: gene_id"
  echo "        Define the attribute type used to group features in the GTF file when"
  echo "        running Salmon."
  echo ""
  echo "    --gtf_extra_attributes"
  echo "        type: string"
  echo "        default: gene_name"
  echo "        By default, the pipeline uses the gene_name field to obtain additional"
  echo "        gene identifiers from the input GTF file when running Salmon."
  echo ""
  echo "    extra_rsem_calculate_expression_args"
  echo "        type: string"
  echo "        Extra arguments to pass to rsem-calculate-expression command in addition"
  echo "        to defaults defined by the pipeline."
  echo ""
  echo "    --aligner"
  echo "        type: string"
  echo "        default: star_salmon"
  echo "        choices: [ star_salmon, star_rsem, hisat2 ]"
  echo "        Specifies the alignment algorithm to use - available options are"
  echo "        'star_salmon', 'star_rsem' and 'hisat2'."
  echo ""
  echo "    --rsem_index"
  echo "        type: file, file must exist"
  echo "        Path to directory for pre-built RSEM index."
  echo ""
  echo "    --salmon_index"
  echo "        type: file, file must exist"
  echo "        Path to directory for pre-built Salmon index."
  echo ""
  echo "Output:"
  echo "    --star_multiqc"
  echo "        type: file, output, file must exist"
  echo "        default: \$id.star_align.log"
  echo ""
  echo "    --genome_bam_sorted"
  echo "        type: file, output, file must exist"
  echo "        default: \$id.genome.bam"
  echo ""
  echo "    --genome_bam_index"
  echo "        type: file, output, file must exist"
  echo "        default: \$id.genome.bam.bai"
  echo ""
  echo "    --genome_bam_stats"
  echo "        type: file, output, file must exist"
  echo "        default: \$id.genome.stats"
  echo ""
  echo "    --genome_bam_flagstat"
  echo "        type: file, output, file must exist"
  echo "        default: \$id.genome.flagstat"
  echo ""
  echo "    --genome_bam_idxstats"
  echo "        type: file, output, file must exist"
  echo "        default: \$id.genome.idxstats"
  echo ""
  echo "    --transcriptome_bam"
  echo "        type: file, output, file must exist"
  echo "        default: \$id.transcriptome.bam"
  echo ""
  echo "    --transcriptome_bam_index"
  echo "        type: file, output, file must exist"
  echo "        default: \$id.transcriptome.bam.bai"
  echo ""
  echo "    --transcriptome_bam_stats"
  echo "        type: file, output, file must exist"
  echo "        default: \$id.transcriptome.stats"
  echo ""
  echo "    --transcriptome_bam_flagstat"
  echo "        type: file, output, file must exist"
  echo "        default: \$id.transcriptome.flagstat"
  echo ""
  echo "    --transcriptome_bam_idxstats"
  echo "        type: file, output, file must exist"
  echo "        default: \$id.transcriptome.idxstats"
  echo ""
  echo "    --quant_out_dir"
  echo "        type: file, output, file must exist"
  echo "        default: \$id.salmon_quant"
  echo ""
  echo "    --quant_results_file"
  echo "        type: file, output, file must exist"
  echo "        default: \$id.quant.sf"
}

# initialise variables
VIASH_MODE='run'
VIASH_ENGINE_ID='native'

# initialise array
VIASH_POSITIONAL_ARGS=''

while [[ $# -gt 0 ]]; do
    case "$1" in
        -h|--help)
            ViashHelp
            exit
            ;;
        ---v|---verbose)
            let "VIASH_VERBOSITY=VIASH_VERBOSITY+1"
            shift 1
            ;;
        ---verbosity)
            VIASH_VERBOSITY="$2"
            shift 2
            ;;
        ---verbosity=*)
            VIASH_VERBOSITY="$(ViashRemoveFlags "$1")"
            shift 1
            ;;
        --version)
            echo "genome_alignment_and_quant main"
            exit
            ;;
        --id)
            [ -n "$VIASH_PAR_ID" ] && ViashError Bad arguments for option \'--id\': \'$VIASH_PAR_ID\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_ID="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --id. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --id=*)
            [ -n "$VIASH_PAR_ID" ] && ViashError Bad arguments for option \'--id=*\': \'$VIASH_PAR_ID\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_ID=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --fastq_1)
            [ -n "$VIASH_PAR_FASTQ_1" ] && ViashError Bad arguments for option \'--fastq_1\': \'$VIASH_PAR_FASTQ_1\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_FASTQ_1="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --fastq_1. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --fastq_1=*)
            [ -n "$VIASH_PAR_FASTQ_1" ] && ViashError Bad arguments for option \'--fastq_1=*\': \'$VIASH_PAR_FASTQ_1\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_FASTQ_1=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        -i)
            [ -n "$VIASH_PAR_FASTQ_1" ] && ViashError Bad arguments for option \'-i\': \'$VIASH_PAR_FASTQ_1\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_FASTQ_1="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to -i. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --fastq_2)
            [ -n "$VIASH_PAR_FASTQ_2" ] && ViashError Bad arguments for option \'--fastq_2\': \'$VIASH_PAR_FASTQ_2\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_FASTQ_2="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --fastq_2. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --fastq_2=*)
            [ -n "$VIASH_PAR_FASTQ_2" ] && ViashError Bad arguments for option \'--fastq_2=*\': \'$VIASH_PAR_FASTQ_2\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_FASTQ_2=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --strandedness)
            [ -n "$VIASH_PAR_STRANDEDNESS" ] && ViashError Bad arguments for option \'--strandedness\': \'$VIASH_PAR_STRANDEDNESS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_STRANDEDNESS="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --strandedness. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --strandedness=*)
            [ -n "$VIASH_PAR_STRANDEDNESS" ] && ViashError Bad arguments for option \'--strandedness=*\': \'$VIASH_PAR_STRANDEDNESS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_STRANDEDNESS=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --gtf)
            [ -n "$VIASH_PAR_GTF" ] && ViashError Bad arguments for option \'--gtf\': \'$VIASH_PAR_GTF\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_GTF="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --gtf. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --gtf=*)
            [ -n "$VIASH_PAR_GTF" ] && ViashError Bad arguments for option \'--gtf=*\': \'$VIASH_PAR_GTF\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_GTF=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --transcript_fasta)
            [ -n "$VIASH_PAR_TRANSCRIPT_FASTA" ] && ViashError Bad arguments for option \'--transcript_fasta\': \'$VIASH_PAR_TRANSCRIPT_FASTA\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRANSCRIPT_FASTA="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --transcript_fasta. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --transcript_fasta=*)
            [ -n "$VIASH_PAR_TRANSCRIPT_FASTA" ] && ViashError Bad arguments for option \'--transcript_fasta=*\': \'$VIASH_PAR_TRANSCRIPT_FASTA\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRANSCRIPT_FASTA=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --star_index)
            [ -n "$VIASH_PAR_STAR_INDEX" ] && ViashError Bad arguments for option \'--star_index\': \'$VIASH_PAR_STAR_INDEX\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_STAR_INDEX="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --star_index. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --star_index=*)
            [ -n "$VIASH_PAR_STAR_INDEX" ] && ViashError Bad arguments for option \'--star_index=*\': \'$VIASH_PAR_STAR_INDEX\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_STAR_INDEX=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --star_ignore_sjdbgtf)
            [ -n "$VIASH_PAR_STAR_IGNORE_SJDBGTF" ] && ViashError Bad arguments for option \'--star_ignore_sjdbgtf\': \'$VIASH_PAR_STAR_IGNORE_SJDBGTF\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_STAR_IGNORE_SJDBGTF="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --star_ignore_sjdbgtf. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --star_ignore_sjdbgtf=*)
            [ -n "$VIASH_PAR_STAR_IGNORE_SJDBGTF" ] && ViashError Bad arguments for option \'--star_ignore_sjdbgtf=*\': \'$VIASH_PAR_STAR_IGNORE_SJDBGTF\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_STAR_IGNORE_SJDBGTF=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --seq_platform)
            [ -n "$VIASH_PAR_SEQ_PLATFORM" ] && ViashError Bad arguments for option \'--seq_platform\': \'$VIASH_PAR_SEQ_PLATFORM\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_SEQ_PLATFORM="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --seq_platform. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --seq_platform=*)
            [ -n "$VIASH_PAR_SEQ_PLATFORM" ] && ViashError Bad arguments for option \'--seq_platform=*\': \'$VIASH_PAR_SEQ_PLATFORM\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_SEQ_PLATFORM=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --seq_center)
            [ -n "$VIASH_PAR_SEQ_CENTER" ] && ViashError Bad arguments for option \'--seq_center\': \'$VIASH_PAR_SEQ_CENTER\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_SEQ_CENTER="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --seq_center. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --seq_center=*)
            [ -n "$VIASH_PAR_SEQ_CENTER" ] && ViashError Bad arguments for option \'--seq_center=*\': \'$VIASH_PAR_SEQ_CENTER\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_SEQ_CENTER=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --extra_star_align_args)
            [ -n "$VIASH_PAR_EXTRA_STAR_ALIGN_ARGS" ] && ViashError Bad arguments for option \'--extra_star_align_args\': \'$VIASH_PAR_EXTRA_STAR_ALIGN_ARGS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_EXTRA_STAR_ALIGN_ARGS="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --extra_star_align_args. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --extra_star_align_args=*)
            [ -n "$VIASH_PAR_EXTRA_STAR_ALIGN_ARGS" ] && ViashError Bad arguments for option \'--extra_star_align_args=*\': \'$VIASH_PAR_EXTRA_STAR_ALIGN_ARGS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_EXTRA_STAR_ALIGN_ARGS=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --bam_csi_index)
            [ -n "$VIASH_PAR_BAM_CSI_INDEX" ] && ViashError Bad arguments for option \'--bam_csi_index\': \'$VIASH_PAR_BAM_CSI_INDEX\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_BAM_CSI_INDEX="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --bam_csi_index. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --bam_csi_index=*)
            [ -n "$VIASH_PAR_BAM_CSI_INDEX" ] && ViashError Bad arguments for option \'--bam_csi_index=*\': \'$VIASH_PAR_BAM_CSI_INDEX\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_BAM_CSI_INDEX=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --umi_dedup_stats)
            [ -n "$VIASH_PAR_UMI_DEDUP_STATS" ] && ViashError Bad arguments for option \'--umi_dedup_stats\': \'$VIASH_PAR_UMI_DEDUP_STATS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_UMI_DEDUP_STATS="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --umi_dedup_stats. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --umi_dedup_stats=*)
            [ -n "$VIASH_PAR_UMI_DEDUP_STATS" ] && ViashError Bad arguments for option \'--umi_dedup_stats=*\': \'$VIASH_PAR_UMI_DEDUP_STATS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_UMI_DEDUP_STATS=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --with_umi)
            [ -n "$VIASH_PAR_WITH_UMI" ] && ViashError Bad arguments for option \'--with_umi\': \'$VIASH_PAR_WITH_UMI\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_WITH_UMI="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --with_umi. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --with_umi=*)
            [ -n "$VIASH_PAR_WITH_UMI" ] && ViashError Bad arguments for option \'--with_umi=*\': \'$VIASH_PAR_WITH_UMI\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_WITH_UMI=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --salmon_quant_libtype)
            [ -n "$VIASH_PAR_SALMON_QUANT_LIBTYPE" ] && ViashError Bad arguments for option \'--salmon_quant_libtype\': \'$VIASH_PAR_SALMON_QUANT_LIBTYPE\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_SALMON_QUANT_LIBTYPE="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --salmon_quant_libtype. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --salmon_quant_libtype=*)
            [ -n "$VIASH_PAR_SALMON_QUANT_LIBTYPE" ] && ViashError Bad arguments for option \'--salmon_quant_libtype=*\': \'$VIASH_PAR_SALMON_QUANT_LIBTYPE\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_SALMON_QUANT_LIBTYPE=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --extra_salmon_quant_args)
            [ -n "$VIASH_PAR_EXTRA_SALMON_QUANT_ARGS" ] && ViashError Bad arguments for option \'--extra_salmon_quant_args\': \'$VIASH_PAR_EXTRA_SALMON_QUANT_ARGS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_EXTRA_SALMON_QUANT_ARGS="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --extra_salmon_quant_args. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --extra_salmon_quant_args=*)
            [ -n "$VIASH_PAR_EXTRA_SALMON_QUANT_ARGS" ] && ViashError Bad arguments for option \'--extra_salmon_quant_args=*\': \'$VIASH_PAR_EXTRA_SALMON_QUANT_ARGS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_EXTRA_SALMON_QUANT_ARGS=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --gtf_group_features)
            [ -n "$VIASH_PAR_GTF_GROUP_FEATURES" ] && ViashError Bad arguments for option \'--gtf_group_features\': \'$VIASH_PAR_GTF_GROUP_FEATURES\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_GTF_GROUP_FEATURES="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --gtf_group_features. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --gtf_group_features=*)
            [ -n "$VIASH_PAR_GTF_GROUP_FEATURES" ] && ViashError Bad arguments for option \'--gtf_group_features=*\': \'$VIASH_PAR_GTF_GROUP_FEATURES\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_GTF_GROUP_FEATURES=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --gtf_extra_attributes)
            [ -n "$VIASH_PAR_GTF_EXTRA_ATTRIBUTES" ] && ViashError Bad arguments for option \'--gtf_extra_attributes\': \'$VIASH_PAR_GTF_EXTRA_ATTRIBUTES\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_GTF_EXTRA_ATTRIBUTES="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --gtf_extra_attributes. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --gtf_extra_attributes=*)
            [ -n "$VIASH_PAR_GTF_EXTRA_ATTRIBUTES" ] && ViashError Bad arguments for option \'--gtf_extra_attributes=*\': \'$VIASH_PAR_GTF_EXTRA_ATTRIBUTES\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_GTF_EXTRA_ATTRIBUTES=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --aligner)
            [ -n "$VIASH_PAR_ALIGNER" ] && ViashError Bad arguments for option \'--aligner\': \'$VIASH_PAR_ALIGNER\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_ALIGNER="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --aligner. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --aligner=*)
            [ -n "$VIASH_PAR_ALIGNER" ] && ViashError Bad arguments for option \'--aligner=*\': \'$VIASH_PAR_ALIGNER\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_ALIGNER=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --rsem_index)
            [ -n "$VIASH_PAR_RSEM_INDEX" ] && ViashError Bad arguments for option \'--rsem_index\': \'$VIASH_PAR_RSEM_INDEX\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_RSEM_INDEX="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --rsem_index. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --rsem_index=*)
            [ -n "$VIASH_PAR_RSEM_INDEX" ] && ViashError Bad arguments for option \'--rsem_index=*\': \'$VIASH_PAR_RSEM_INDEX\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_RSEM_INDEX=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --salmon_index)
            [ -n "$VIASH_PAR_SALMON_INDEX" ] && ViashError Bad arguments for option \'--salmon_index\': \'$VIASH_PAR_SALMON_INDEX\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_SALMON_INDEX="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --salmon_index. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --salmon_index=*)
            [ -n "$VIASH_PAR_SALMON_INDEX" ] && ViashError Bad arguments for option \'--salmon_index=*\': \'$VIASH_PAR_SALMON_INDEX\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_SALMON_INDEX=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --star_multiqc)
            [ -n "$VIASH_PAR_STAR_MULTIQC" ] && ViashError Bad arguments for option \'--star_multiqc\': \'$VIASH_PAR_STAR_MULTIQC\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_STAR_MULTIQC="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --star_multiqc. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --star_multiqc=*)
            [ -n "$VIASH_PAR_STAR_MULTIQC" ] && ViashError Bad arguments for option \'--star_multiqc=*\': \'$VIASH_PAR_STAR_MULTIQC\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_STAR_MULTIQC=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --genome_bam_sorted)
            [ -n "$VIASH_PAR_GENOME_BAM_SORTED" ] && ViashError Bad arguments for option \'--genome_bam_sorted\': \'$VIASH_PAR_GENOME_BAM_SORTED\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_GENOME_BAM_SORTED="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --genome_bam_sorted. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --genome_bam_sorted=*)
            [ -n "$VIASH_PAR_GENOME_BAM_SORTED" ] && ViashError Bad arguments for option \'--genome_bam_sorted=*\': \'$VIASH_PAR_GENOME_BAM_SORTED\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_GENOME_BAM_SORTED=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --genome_bam_index)
            [ -n "$VIASH_PAR_GENOME_BAM_INDEX" ] && ViashError Bad arguments for option \'--genome_bam_index\': \'$VIASH_PAR_GENOME_BAM_INDEX\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_GENOME_BAM_INDEX="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --genome_bam_index. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --genome_bam_index=*)
            [ -n "$VIASH_PAR_GENOME_BAM_INDEX" ] && ViashError Bad arguments for option \'--genome_bam_index=*\': \'$VIASH_PAR_GENOME_BAM_INDEX\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_GENOME_BAM_INDEX=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --genome_bam_stats)
            [ -n "$VIASH_PAR_GENOME_BAM_STATS" ] && ViashError Bad arguments for option \'--genome_bam_stats\': \'$VIASH_PAR_GENOME_BAM_STATS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_GENOME_BAM_STATS="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --genome_bam_stats. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --genome_bam_stats=*)
            [ -n "$VIASH_PAR_GENOME_BAM_STATS" ] && ViashError Bad arguments for option \'--genome_bam_stats=*\': \'$VIASH_PAR_GENOME_BAM_STATS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_GENOME_BAM_STATS=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --genome_bam_flagstat)
            [ -n "$VIASH_PAR_GENOME_BAM_FLAGSTAT" ] && ViashError Bad arguments for option \'--genome_bam_flagstat\': \'$VIASH_PAR_GENOME_BAM_FLAGSTAT\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_GENOME_BAM_FLAGSTAT="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --genome_bam_flagstat. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --genome_bam_flagstat=*)
            [ -n "$VIASH_PAR_GENOME_BAM_FLAGSTAT" ] && ViashError Bad arguments for option \'--genome_bam_flagstat=*\': \'$VIASH_PAR_GENOME_BAM_FLAGSTAT\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_GENOME_BAM_FLAGSTAT=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --genome_bam_idxstats)
            [ -n "$VIASH_PAR_GENOME_BAM_IDXSTATS" ] && ViashError Bad arguments for option \'--genome_bam_idxstats\': \'$VIASH_PAR_GENOME_BAM_IDXSTATS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_GENOME_BAM_IDXSTATS="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --genome_bam_idxstats. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --genome_bam_idxstats=*)
            [ -n "$VIASH_PAR_GENOME_BAM_IDXSTATS" ] && ViashError Bad arguments for option \'--genome_bam_idxstats=*\': \'$VIASH_PAR_GENOME_BAM_IDXSTATS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_GENOME_BAM_IDXSTATS=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --transcriptome_bam)
            [ -n "$VIASH_PAR_TRANSCRIPTOME_BAM" ] && ViashError Bad arguments for option \'--transcriptome_bam\': \'$VIASH_PAR_TRANSCRIPTOME_BAM\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRANSCRIPTOME_BAM="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --transcriptome_bam. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --transcriptome_bam=*)
            [ -n "$VIASH_PAR_TRANSCRIPTOME_BAM" ] && ViashError Bad arguments for option \'--transcriptome_bam=*\': \'$VIASH_PAR_TRANSCRIPTOME_BAM\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRANSCRIPTOME_BAM=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --transcriptome_bam_index)
            [ -n "$VIASH_PAR_TRANSCRIPTOME_BAM_INDEX" ] && ViashError Bad arguments for option \'--transcriptome_bam_index\': \'$VIASH_PAR_TRANSCRIPTOME_BAM_INDEX\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRANSCRIPTOME_BAM_INDEX="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --transcriptome_bam_index. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --transcriptome_bam_index=*)
            [ -n "$VIASH_PAR_TRANSCRIPTOME_BAM_INDEX" ] && ViashError Bad arguments for option \'--transcriptome_bam_index=*\': \'$VIASH_PAR_TRANSCRIPTOME_BAM_INDEX\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRANSCRIPTOME_BAM_INDEX=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --transcriptome_bam_stats)
            [ -n "$VIASH_PAR_TRANSCRIPTOME_BAM_STATS" ] && ViashError Bad arguments for option \'--transcriptome_bam_stats\': \'$VIASH_PAR_TRANSCRIPTOME_BAM_STATS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRANSCRIPTOME_BAM_STATS="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --transcriptome_bam_stats. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --transcriptome_bam_stats=*)
            [ -n "$VIASH_PAR_TRANSCRIPTOME_BAM_STATS" ] && ViashError Bad arguments for option \'--transcriptome_bam_stats=*\': \'$VIASH_PAR_TRANSCRIPTOME_BAM_STATS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRANSCRIPTOME_BAM_STATS=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --transcriptome_bam_flagstat)
            [ -n "$VIASH_PAR_TRANSCRIPTOME_BAM_FLAGSTAT" ] && ViashError Bad arguments for option \'--transcriptome_bam_flagstat\': \'$VIASH_PAR_TRANSCRIPTOME_BAM_FLAGSTAT\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRANSCRIPTOME_BAM_FLAGSTAT="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --transcriptome_bam_flagstat. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --transcriptome_bam_flagstat=*)
            [ -n "$VIASH_PAR_TRANSCRIPTOME_BAM_FLAGSTAT" ] && ViashError Bad arguments for option \'--transcriptome_bam_flagstat=*\': \'$VIASH_PAR_TRANSCRIPTOME_BAM_FLAGSTAT\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRANSCRIPTOME_BAM_FLAGSTAT=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --transcriptome_bam_idxstats)
            [ -n "$VIASH_PAR_TRANSCRIPTOME_BAM_IDXSTATS" ] && ViashError Bad arguments for option \'--transcriptome_bam_idxstats\': \'$VIASH_PAR_TRANSCRIPTOME_BAM_IDXSTATS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRANSCRIPTOME_BAM_IDXSTATS="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --transcriptome_bam_idxstats. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --transcriptome_bam_idxstats=*)
            [ -n "$VIASH_PAR_TRANSCRIPTOME_BAM_IDXSTATS" ] && ViashError Bad arguments for option \'--transcriptome_bam_idxstats=*\': \'$VIASH_PAR_TRANSCRIPTOME_BAM_IDXSTATS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRANSCRIPTOME_BAM_IDXSTATS=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --quant_out_dir)
            [ -n "$VIASH_PAR_QUANT_OUT_DIR" ] && ViashError Bad arguments for option \'--quant_out_dir\': \'$VIASH_PAR_QUANT_OUT_DIR\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_QUANT_OUT_DIR="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --quant_out_dir. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --quant_out_dir=*)
            [ -n "$VIASH_PAR_QUANT_OUT_DIR" ] && ViashError Bad arguments for option \'--quant_out_dir=*\': \'$VIASH_PAR_QUANT_OUT_DIR\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_QUANT_OUT_DIR=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --quant_results_file)
            [ -n "$VIASH_PAR_QUANT_RESULTS_FILE" ] && ViashError Bad arguments for option \'--quant_results_file\': \'$VIASH_PAR_QUANT_RESULTS_FILE\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_QUANT_RESULTS_FILE="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --quant_results_file. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --quant_results_file=*)
            [ -n "$VIASH_PAR_QUANT_RESULTS_FILE" ] && ViashError Bad arguments for option \'--quant_results_file=*\': \'$VIASH_PAR_QUANT_RESULTS_FILE\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_QUANT_RESULTS_FILE=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        ---engine)
            VIASH_ENGINE_ID="$2"
            shift 2
            ;;
        ---engine=*)
            VIASH_ENGINE_ID="$(ViashRemoveFlags "$1")"
            shift 1
            ;;
        ---cpus)
            [ -n "$VIASH_META_CPUS" ] && ViashError Bad arguments for option \'---cpus\': \'$VIASH_META_CPUS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_META_CPUS="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to ---cpus. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        ---cpus=*)
            [ -n "$VIASH_META_CPUS" ] && ViashError Bad arguments for option \'---cpus=*\': \'$VIASH_META_CPUS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_META_CPUS=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        ---memory)
            [ -n "$VIASH_META_MEMORY" ] && ViashError Bad arguments for option \'---memory\': \'$VIASH_META_MEMORY\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_META_MEMORY="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to ---memory. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        ---memory=*)
            [ -n "$VIASH_META_MEMORY" ] && ViashError Bad arguments for option \'---memory=*\': \'$VIASH_META_MEMORY\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_META_MEMORY=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        *)  # positional arg or unknown option
            # since the positional args will be eval'd, can we always quote, instead of using ViashQuote
            VIASH_POSITIONAL_ARGS="$VIASH_POSITIONAL_ARGS '$1'"
            [[ $1 == -* ]] && ViashWarning $1 looks like a parameter but is not a defined parameter and will instead be treated as a positional argument. Use "--help" to get more information on the parameters.
            shift # past argument
            ;;
    esac
done

# parse positional parameters
eval set -- $VIASH_POSITIONAL_ARGS


if   [ "$VIASH_ENGINE_ID" == "native" ]  ; then
  VIASH_ENGINE_TYPE='native'
else
  ViashError "Engine '$VIASH_ENGINE_ID' is not recognized. Options are: native."
  exit 1
fi

# setting computational defaults

# helper function for parsing memory strings
function ViashMemoryAsBytes {
  local memory=`echo "$1" | tr '[:upper:]' '[:lower:]' | tr -d '[:space:]'`
  local memory_regex='^([0-9]+)([kmgtp]i?b?|b)$'
  if [[ $memory =~ $memory_regex ]]; then
    local number=${memory/[^0-9]*/}
    local symbol=${memory/*[0-9]/}
    
    case $symbol in
      b)      memory_b=$number ;;
      kb|k)   memory_b=$(( $number * 1000 )) ;;
      mb|m)   memory_b=$(( $number * 1000 * 1000 )) ;;
      gb|g)   memory_b=$(( $number * 1000 * 1000 * 1000 )) ;;
      tb|t)   memory_b=$(( $number * 1000 * 1000 * 1000 * 1000 )) ;;
      pb|p)   memory_b=$(( $number * 1000 * 1000 * 1000 * 1000 * 1000 )) ;;
      kib|ki)   memory_b=$(( $number * 1024 )) ;;
      mib|mi)   memory_b=$(( $number * 1024 * 1024 )) ;;
      gib|gi)   memory_b=$(( $number * 1024 * 1024 * 1024 )) ;;
      tib|ti)   memory_b=$(( $number * 1024 * 1024 * 1024 * 1024 )) ;;
      pib|pi)   memory_b=$(( $number * 1024 * 1024 * 1024 * 1024 * 1024 )) ;;
    esac
    echo "$memory_b"
  fi
}
# compute memory in different units
if [ ! -z ${VIASH_META_MEMORY+x} ]; then
  VIASH_META_MEMORY_B=`ViashMemoryAsBytes $VIASH_META_MEMORY`
  # do not define other variables if memory_b is an empty string
  if [ ! -z "$VIASH_META_MEMORY_B" ]; then
    VIASH_META_MEMORY_KB=$(( ($VIASH_META_MEMORY_B+999) / 1000 ))
    VIASH_META_MEMORY_MB=$(( ($VIASH_META_MEMORY_KB+999) / 1000 ))
    VIASH_META_MEMORY_GB=$(( ($VIASH_META_MEMORY_MB+999) / 1000 ))
    VIASH_META_MEMORY_TB=$(( ($VIASH_META_MEMORY_GB+999) / 1000 ))
    VIASH_META_MEMORY_PB=$(( ($VIASH_META_MEMORY_TB+999) / 1000 ))
    VIASH_META_MEMORY_KIB=$(( ($VIASH_META_MEMORY_B+1023) / 1024 ))
    VIASH_META_MEMORY_MIB=$(( ($VIASH_META_MEMORY_KIB+1023) / 1024 ))
    VIASH_META_MEMORY_GIB=$(( ($VIASH_META_MEMORY_MIB+1023) / 1024 ))
    VIASH_META_MEMORY_TIB=$(( ($VIASH_META_MEMORY_GIB+1023) / 1024 ))
    VIASH_META_MEMORY_PIB=$(( ($VIASH_META_MEMORY_TIB+1023) / 1024 ))
  else
    # unset memory if string is empty
    unset $VIASH_META_MEMORY_B
  fi
fi
# unset nproc if string is empty
if [ -z "$VIASH_META_CPUS" ]; then
  unset $VIASH_META_CPUS
fi


# storing leftover values in positionals
if [[ $# -gt 0 ]]; then
  VIASH_PAR_EXTRA_RSEM_CALCULATE_EXPRESSION_ARGS="$1"
  shift 1
fi

# check whether required parameters exist
if [ -z ${VIASH_PAR_ID+x} ]; then
  ViashError '--id' is a required argument. Use "--help" to get more information on the parameters.
  exit 1
fi
if [ -z ${VIASH_PAR_FASTQ_1+x} ]; then
  ViashError '--fastq_1' is a required argument. Use "--help" to get more information on the parameters.
  exit 1
fi
if [ -z ${VIASH_META_NAME+x} ]; then
  ViashError 'name' is a required argument. Use "--help" to get more information on the parameters.
  exit 1
fi
if [ -z ${VIASH_META_FUNCTIONALITY_NAME+x} ]; then
  ViashError 'functionality_name' is a required argument. Use "--help" to get more information on the parameters.
  exit 1
fi
if [ -z ${VIASH_META_RESOURCES_DIR+x} ]; then
  ViashError 'resources_dir' is a required argument. Use "--help" to get more information on the parameters.
  exit 1
fi
if [ -z ${VIASH_META_EXECUTABLE+x} ]; then
  ViashError 'executable' is a required argument. Use "--help" to get more information on the parameters.
  exit 1
fi
if [ -z ${VIASH_META_CONFIG+x} ]; then
  ViashError 'config' is a required argument. Use "--help" to get more information on the parameters.
  exit 1
fi
if [ -z ${VIASH_META_TEMP_DIR+x} ]; then
  ViashError 'temp_dir' is a required argument. Use "--help" to get more information on the parameters.
  exit 1
fi

# filling in defaults
if [ -z ${VIASH_PAR_STAR_IGNORE_SJDBGTF+x} ]; then
  VIASH_PAR_STAR_IGNORE_SJDBGTF="false"
fi
if [ -z ${VIASH_PAR_EXTRA_STAR_ALIGN_ARGS+x} ]; then
  VIASH_PAR_EXTRA_STAR_ALIGN_ARGS=""
fi
if [ -z ${VIASH_PAR_BAM_CSI_INDEX+x} ]; then
  VIASH_PAR_BAM_CSI_INDEX="false"
fi
if [ -z ${VIASH_PAR_UMI_DEDUP_STATS+x} ]; then
  VIASH_PAR_UMI_DEDUP_STATS="false"
fi
if [ -z ${VIASH_PAR_WITH_UMI+x} ]; then
  VIASH_PAR_WITH_UMI="false"
fi
if [ -z ${VIASH_PAR_EXTRA_SALMON_QUANT_ARGS+x} ]; then
  VIASH_PAR_EXTRA_SALMON_QUANT_ARGS=""
fi
if [ -z ${VIASH_PAR_GTF_GROUP_FEATURES+x} ]; then
  VIASH_PAR_GTF_GROUP_FEATURES="gene_id"
fi
if [ -z ${VIASH_PAR_GTF_EXTRA_ATTRIBUTES+x} ]; then
  VIASH_PAR_GTF_EXTRA_ATTRIBUTES="gene_name"
fi
if [ -z ${VIASH_PAR_ALIGNER+x} ]; then
  VIASH_PAR_ALIGNER="star_salmon"
fi
if [ -z ${VIASH_PAR_STAR_MULTIQC+x} ]; then
  VIASH_PAR_STAR_MULTIQC="\$id.star_align.log"
fi
if [ -z ${VIASH_PAR_GENOME_BAM_SORTED+x} ]; then
  VIASH_PAR_GENOME_BAM_SORTED="\$id.genome.bam"
fi
if [ -z ${VIASH_PAR_GENOME_BAM_INDEX+x} ]; then
  VIASH_PAR_GENOME_BAM_INDEX="\$id.genome.bam.bai"
fi
if [ -z ${VIASH_PAR_GENOME_BAM_STATS+x} ]; then
  VIASH_PAR_GENOME_BAM_STATS="\$id.genome.stats"
fi
if [ -z ${VIASH_PAR_GENOME_BAM_FLAGSTAT+x} ]; then
  VIASH_PAR_GENOME_BAM_FLAGSTAT="\$id.genome.flagstat"
fi
if [ -z ${VIASH_PAR_GENOME_BAM_IDXSTATS+x} ]; then
  VIASH_PAR_GENOME_BAM_IDXSTATS="\$id.genome.idxstats"
fi
if [ -z ${VIASH_PAR_TRANSCRIPTOME_BAM+x} ]; then
  VIASH_PAR_TRANSCRIPTOME_BAM="\$id.transcriptome.bam"
fi
if [ -z ${VIASH_PAR_TRANSCRIPTOME_BAM_INDEX+x} ]; then
  VIASH_PAR_TRANSCRIPTOME_BAM_INDEX="\$id.transcriptome.bam.bai"
fi
if [ -z ${VIASH_PAR_TRANSCRIPTOME_BAM_STATS+x} ]; then
  VIASH_PAR_TRANSCRIPTOME_BAM_STATS="\$id.transcriptome.stats"
fi
if [ -z ${VIASH_PAR_TRANSCRIPTOME_BAM_FLAGSTAT+x} ]; then
  VIASH_PAR_TRANSCRIPTOME_BAM_FLAGSTAT="\$id.transcriptome.flagstat"
fi
if [ -z ${VIASH_PAR_TRANSCRIPTOME_BAM_IDXSTATS+x} ]; then
  VIASH_PAR_TRANSCRIPTOME_BAM_IDXSTATS="\$id.transcriptome.idxstats"
fi
if [ -z ${VIASH_PAR_QUANT_OUT_DIR+x} ]; then
  VIASH_PAR_QUANT_OUT_DIR="\$id.salmon_quant"
fi
if [ -z ${VIASH_PAR_QUANT_RESULTS_FILE+x} ]; then
  VIASH_PAR_QUANT_RESULTS_FILE="\$id.quant.sf"
fi

# check whether required files exist
if [ ! -z "$VIASH_PAR_FASTQ_1" ] && [ ! -e "$VIASH_PAR_FASTQ_1" ]; then
  ViashError "Input file '$VIASH_PAR_FASTQ_1' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_FASTQ_2" ] && [ ! -e "$VIASH_PAR_FASTQ_2" ]; then
  ViashError "Input file '$VIASH_PAR_FASTQ_2' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_GTF" ] && [ ! -e "$VIASH_PAR_GTF" ]; then
  ViashError "Input file '$VIASH_PAR_GTF' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_TRANSCRIPT_FASTA" ] && [ ! -e "$VIASH_PAR_TRANSCRIPT_FASTA" ]; then
  ViashError "Input file '$VIASH_PAR_TRANSCRIPT_FASTA' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_STAR_INDEX" ] && [ ! -e "$VIASH_PAR_STAR_INDEX" ]; then
  ViashError "Input file '$VIASH_PAR_STAR_INDEX' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_RSEM_INDEX" ] && [ ! -e "$VIASH_PAR_RSEM_INDEX" ]; then
  ViashError "Input file '$VIASH_PAR_RSEM_INDEX' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_SALMON_INDEX" ] && [ ! -e "$VIASH_PAR_SALMON_INDEX" ]; then
  ViashError "Input file '$VIASH_PAR_SALMON_INDEX' does not exist."
  exit 1
fi

# check whether parameters values are of the right type
if [[ -n "$VIASH_PAR_STAR_IGNORE_SJDBGTF" ]]; then
  if ! [[ "$VIASH_PAR_STAR_IGNORE_SJDBGTF" =~ ^(true|True|TRUE|false|False|FALSE|yes|Yes|YES|no|No|NO)$ ]]; then
    ViashError '--star_ignore_sjdbgtf' has to be a boolean. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_BAM_CSI_INDEX" ]]; then
  if ! [[ "$VIASH_PAR_BAM_CSI_INDEX" =~ ^(true|True|TRUE|false|False|FALSE|yes|Yes|YES|no|No|NO)$ ]]; then
    ViashError '--bam_csi_index' has to be a boolean. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_UMI_DEDUP_STATS" ]]; then
  if ! [[ "$VIASH_PAR_UMI_DEDUP_STATS" =~ ^(true|True|TRUE|false|False|FALSE|yes|Yes|YES|no|No|NO)$ ]]; then
    ViashError '--umi_dedup_stats' has to be a boolean. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_WITH_UMI" ]]; then
  if ! [[ "$VIASH_PAR_WITH_UMI" =~ ^(true|True|TRUE|false|False|FALSE|yes|Yes|YES|no|No|NO)$ ]]; then
    ViashError '--with_umi' has to be a boolean. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_META_CPUS" ]]; then
  if ! [[ "$VIASH_META_CPUS" =~ ^[-+]?[0-9]+$ ]]; then
    ViashError 'cpus' has to be an integer. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_META_MEMORY_B" ]]; then
  if ! [[ "$VIASH_META_MEMORY_B" =~ ^[-+]?[0-9]+$ ]]; then
    ViashError 'memory_b' has to be a long. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_META_MEMORY_KB" ]]; then
  if ! [[ "$VIASH_META_MEMORY_KB" =~ ^[-+]?[0-9]+$ ]]; then
    ViashError 'memory_kb' has to be a long. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_META_MEMORY_MB" ]]; then
  if ! [[ "$VIASH_META_MEMORY_MB" =~ ^[-+]?[0-9]+$ ]]; then
    ViashError 'memory_mb' has to be a long. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_META_MEMORY_GB" ]]; then
  if ! [[ "$VIASH_META_MEMORY_GB" =~ ^[-+]?[0-9]+$ ]]; then
    ViashError 'memory_gb' has to be a long. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_META_MEMORY_TB" ]]; then
  if ! [[ "$VIASH_META_MEMORY_TB" =~ ^[-+]?[0-9]+$ ]]; then
    ViashError 'memory_tb' has to be a long. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_META_MEMORY_PB" ]]; then
  if ! [[ "$VIASH_META_MEMORY_PB" =~ ^[-+]?[0-9]+$ ]]; then
    ViashError 'memory_pb' has to be a long. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_META_MEMORY_KIB" ]]; then
  if ! [[ "$VIASH_META_MEMORY_KIB" =~ ^[-+]?[0-9]+$ ]]; then
    ViashError 'memory_kib' has to be a long. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_META_MEMORY_MIB" ]]; then
  if ! [[ "$VIASH_META_MEMORY_MIB" =~ ^[-+]?[0-9]+$ ]]; then
    ViashError 'memory_mib' has to be a long. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_META_MEMORY_GIB" ]]; then
  if ! [[ "$VIASH_META_MEMORY_GIB" =~ ^[-+]?[0-9]+$ ]]; then
    ViashError 'memory_gib' has to be a long. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_META_MEMORY_TIB" ]]; then
  if ! [[ "$VIASH_META_MEMORY_TIB" =~ ^[-+]?[0-9]+$ ]]; then
    ViashError 'memory_tib' has to be a long. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_META_MEMORY_PIB" ]]; then
  if ! [[ "$VIASH_META_MEMORY_PIB" =~ ^[-+]?[0-9]+$ ]]; then
    ViashError 'memory_pib' has to be a long. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi

# check whether value is belongs to a set of choices
if [ ! -z "$VIASH_PAR_STRANDEDNESS" ]; then
  VIASH_PAR_STRANDEDNESS_CHOICES=("forward;reverse;unstranded")
  IFS=';'
  set -f
  if ! [[ ";${VIASH_PAR_STRANDEDNESS_CHOICES[*]};" =~ ";$VIASH_PAR_STRANDEDNESS;" ]]; then
    ViashError '--strandedness' specified value of \'$VIASH_PAR_STRANDEDNESS\' is not in the list of allowed values. Use "--help" to get more information on the parameters.
    exit 1
  fi
  set +f
  unset IFS
fi

if [ ! -z "$VIASH_PAR_ALIGNER" ]; then
  VIASH_PAR_ALIGNER_CHOICES=("star_salmon;star_rsem;hisat2")
  IFS=';'
  set -f
  if ! [[ ";${VIASH_PAR_ALIGNER_CHOICES[*]};" =~ ";$VIASH_PAR_ALIGNER;" ]]; then
    ViashError '--aligner' specified value of \'$VIASH_PAR_ALIGNER\' is not in the list of allowed values. Use "--help" to get more information on the parameters.
    exit 1
  fi
  set +f
  unset IFS
fi

# create parent directories of output files, if so desired
if [ ! -z "$VIASH_PAR_STAR_MULTIQC" ] && [ ! -d "$(dirname "$VIASH_PAR_STAR_MULTIQC")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_STAR_MULTIQC")"
fi
if [ ! -z "$VIASH_PAR_GENOME_BAM_SORTED" ] && [ ! -d "$(dirname "$VIASH_PAR_GENOME_BAM_SORTED")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_GENOME_BAM_SORTED")"
fi
if [ ! -z "$VIASH_PAR_GENOME_BAM_INDEX" ] && [ ! -d "$(dirname "$VIASH_PAR_GENOME_BAM_INDEX")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_GENOME_BAM_INDEX")"
fi
if [ ! -z "$VIASH_PAR_GENOME_BAM_STATS" ] && [ ! -d "$(dirname "$VIASH_PAR_GENOME_BAM_STATS")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_GENOME_BAM_STATS")"
fi
if [ ! -z "$VIASH_PAR_GENOME_BAM_FLAGSTAT" ] && [ ! -d "$(dirname "$VIASH_PAR_GENOME_BAM_FLAGSTAT")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_GENOME_BAM_FLAGSTAT")"
fi
if [ ! -z "$VIASH_PAR_GENOME_BAM_IDXSTATS" ] && [ ! -d "$(dirname "$VIASH_PAR_GENOME_BAM_IDXSTATS")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_GENOME_BAM_IDXSTATS")"
fi
if [ ! -z "$VIASH_PAR_TRANSCRIPTOME_BAM" ] && [ ! -d "$(dirname "$VIASH_PAR_TRANSCRIPTOME_BAM")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_TRANSCRIPTOME_BAM")"
fi
if [ ! -z "$VIASH_PAR_TRANSCRIPTOME_BAM_INDEX" ] && [ ! -d "$(dirname "$VIASH_PAR_TRANSCRIPTOME_BAM_INDEX")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_TRANSCRIPTOME_BAM_INDEX")"
fi
if [ ! -z "$VIASH_PAR_TRANSCRIPTOME_BAM_STATS" ] && [ ! -d "$(dirname "$VIASH_PAR_TRANSCRIPTOME_BAM_STATS")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_TRANSCRIPTOME_BAM_STATS")"
fi
if [ ! -z "$VIASH_PAR_TRANSCRIPTOME_BAM_FLAGSTAT" ] && [ ! -d "$(dirname "$VIASH_PAR_TRANSCRIPTOME_BAM_FLAGSTAT")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_TRANSCRIPTOME_BAM_FLAGSTAT")"
fi
if [ ! -z "$VIASH_PAR_TRANSCRIPTOME_BAM_IDXSTATS" ] && [ ! -d "$(dirname "$VIASH_PAR_TRANSCRIPTOME_BAM_IDXSTATS")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_TRANSCRIPTOME_BAM_IDXSTATS")"
fi
if [ ! -z "$VIASH_PAR_QUANT_OUT_DIR" ] && [ ! -d "$(dirname "$VIASH_PAR_QUANT_OUT_DIR")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_QUANT_OUT_DIR")"
fi
if [ ! -z "$VIASH_PAR_QUANT_RESULTS_FILE" ] && [ ! -d "$(dirname "$VIASH_PAR_QUANT_RESULTS_FILE")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_QUANT_RESULTS_FILE")"
fi

if  [ "$VIASH_ENGINE_ID" == "native" ]  ; then
  if [ "$VIASH_MODE" == "run" ]; then
    VIASH_CMD="bash"
  else
    ViashError "Engine '$VIASH_ENGINE_ID' does not support mode '$VIASH_MODE'."
    exit 1
  fi
fi


# set dependency paths
VIASH_DEP_UMITOOLS_UMITOOLS_DEDUP="$VIASH_META_RESOURCES_DIR/../../../nextflow/umitools/umitools_dedup/main.nf"
VIASH_DEP_UMITOOLS_PREPAREFORQUANT="$VIASH_META_RESOURCES_DIR/../../../nextflow/umitools_prepareforquant/main.nf"
VIASH_DEP_RSEM_RSEM_CALCULATE_EXPRESSION="$VIASH_META_RESOURCES_DIR/../../../nextflow/rsem/rsem_calculate_expression/main.nf"
VIASH_DEP_STAR_STAR_ALIGN_READS="$VIASH_TARGET_DIR/dependencies/vsh/vsh/biobox/nextflow/star/star_align_reads/main.nf"
VIASH_DEP_SAMTOOLS_SAMTOOLS_SORT="$VIASH_TARGET_DIR/dependencies/vsh/vsh/biobox/nextflow/samtools/samtools_sort/main.nf"
VIASH_DEP_SAMTOOLS_SAMTOOLS_INDEX="$VIASH_TARGET_DIR/dependencies/vsh/vsh/biobox/nextflow/samtools/samtools_index/main.nf"
VIASH_DEP_SAMTOOLS_SAMTOOLS_STATS="$VIASH_TARGET_DIR/dependencies/vsh/vsh/biobox/nextflow/samtools/samtools_stats/main.nf"
VIASH_DEP_SAMTOOLS_SAMTOOLS_FLAGSTAT="$VIASH_TARGET_DIR/dependencies/vsh/vsh/biobox/nextflow/samtools/samtools_flagstat/main.nf"
VIASH_DEP_SAMTOOLS_SAMTOOLS_IDXSTATS="$VIASH_TARGET_DIR/dependencies/vsh/vsh/biobox/nextflow/samtools/samtools_idxstats/main.nf"
VIASH_DEP_SALMON_SALMON_QUANT="$VIASH_TARGET_DIR/dependencies/vsh/vsh/biobox/nextflow/salmon/salmon_quant/main.nf"

ViashDebug "Running command: $(echo $VIASH_CMD)"
cat << VIASHEOF | eval $VIASH_CMD
set -e
tempscript=\$(mktemp "$VIASH_META_TEMP_DIR/viash-run-genome_alignment_and_quant-XXXXXX").nf
function clean_up {
  rm "\$tempscript"
}
function interrupt {
  echo -e "\nCTRL-C Pressed..."
  exit 1
}
trap clean_up EXIT
trap interrupt INT SIGINT
cat > "\$tempscript" << 'VIASHMAIN'
//// VIASH START
// The following code has been auto-generated by Viash.

//// VIASH END
workflow run_wf {
  take:
    input_ch

  main:
    output_ch = input_ch
    | map { id, state ->
      def input = state.fastq_2 ? [ state.fastq_1, state.fastq_2 ] : [ state.fastq_1 ]
      def paired = input.size() == 2
      [ id, state + [ paired: paired, input: input ] ]
    }

    | star_align_reads.run (
        runIf: { id, state -> state.aligner == 'star_salmon' },
        fromState: [
          "input": "fastq_1",
          "input_r2": "fastq_2",
          "genome_dir": "star_index",
          "sjdb_gtf_file": "gtf",
          "out_sam_attr_rg_line": "star_sam_attr_rg_line"
        ],
        toState: [
          "genome_bam": "aligned_reads",
          "transcriptome_bam": "reads_aligned_to_transcriptome",
          "star_multiqc": "log"
        ],
        args: [ 
          quant_mode: "TranscriptomeSAM", 
          twopass_mode: "Basic", 
          out_sam_type: "BAM;Unsorted", 
          run_rng_seed: 0, 
          out_filter_multimap_nmax: 20, 
          align_sjdb_overhang_min: 1, 
          out_sam_attributes: "NH;HI;AS;NM;MD", 
          quant_transcriptome_sam_output: "BanSingleEnd" 
        ]
    )

    // GENOME BAM
    | samtools_sort.run (
        runIf: { id, state -> state.aligner == 'star_salmon' },
        fromState: ["input": "genome_bam"],
        toState: ["genome_bam_sorted": "output"],
        key: "genome_sorted"
    )
    | samtools_index.run (
        runIf: { id, state -> state.aligner == 'star_salmon' },
        fromState: [ 
          "input": "genome_bam_sorted", 
          "csi": "bam_csi_index"
        ],
        toState: [ "genome_bam_index": "output" ],
        key: "genome_sorted"
    )
    | samtools_stats.run (
        runIf: { id, state -> state.aligner == 'star_salmon' },
        fromState: [
          "input": "genome_bam_sorted", 
          "bai": "genome_bam_index", 
          "fasta": "fasta"
        ],
        toState: [ "genome_bam_stats": "output" ],
        key: "genome_stats"
    )
    | samtools_flagstat.run (
        runIf: { id, state -> state.aligner == 'star_salmon' },
        fromState: [
          "bam": "genome_bam_sorted", 
          "bai": "genome_bam_index", 
          "fasta": "fasta"
        ],
        toState: [ "genome_bam_flagstat": "output" ],
        key: "genome_flagstat"
    )
    | samtools_idxstats.run(
        runIf: { id, state -> state.aligner == 'star_salmon' },
        fromState: [
          "bam": "genome_bam_sorted", 
          "bai": "genome_bam_index", 
          "fasta": "fasta"
        ],
        toState: [ "genome_bam_idxstats": "output" ],
        key: "genome_idxstats"
    )

    //
    // Remove duplicate reads from BAM file based on UMIs
    // 
    
    // Deduplicate genome BAM file
    | umitools_dedup.run ( 
        runIf: { id, state -> state.with_umi && state.aligner == 'star_salmon' },
        fromState: [
          "paired": "paired", 
          "bam": "genome_bam_sorted", 
          "bai": "genome_bam_index",
          "get_output_stats": "umi_dedup_stats"
        ],
        toState: [ "genome_bam_sorted": "output_bam" ],
        key: "genome_deduped"
    )
    | samtools_index.run (
        runIf: { id, state -> state.with_umi && state.aligner == 'star_salmon' },
        fromState: [
          "input": "genome_bam_sorted", 
          "csi": "bam_csi_index"
        ],
        toState: [ "genome_bam_index": "output" ],
        key: "genome_deduped"
    )
    | samtools_stats.run (
        runIf: { id, state -> state.with_umi && state.aligner == 'star_salmon' },
        fromState: [
          "input": "genome_bam_sorted", 
          "bai": "genome_bam_index", 
          "fasta": "fasta"
        ],
        toState: [ "genome_bam_stats": "output" ],
        key: "genome_deduped_stats"
    )
    | samtools_flagstat.run (
        runIf: { id, state -> state.with_umi && state.aligner == 'star_salmon' },
        fromState: [
          "bam": "genome_bam_sorted", 
          "bai": "genome_bam_index", 
          "fasta": "fasta"
        ],
        toState: [ "genome_bam_flagstat": "output" ],
        key: "genome_deduped_flagstat"
    )
    | samtools_idxstats.run(
        runIf: { id, state -> state.with_umi && state.aligner == 'star_salmon' },
        fromState: [
          "bam": "genome_bam_sorted", 
          "bai": "genome_bam_index", 
          "fasta": "fasta", 
        ],
        toState: [ "genome_bam_idxstats": "output" ],
        key: "genome_deduped_idxstats"
    )

    // Deduplicate transcriptome BAM file

    | samtools_sort.run (
        runIf: { id, state -> state.with_umi && state.aligner == 'star_salmon' },
        fromState: [ "input": "transcriptome_bam" ],
        toState: [ "transcriptome_bam": "output" ],
        key: "transcriptome_sorted"
    )
    | samtools_index.run (
        runIf: { id, state -> state.with_umi && state.aligner == 'star_salmon' },
        fromState: [
          "input": "transcriptome_bam", 
          "csi": "bam_csi_index"
        ],
        toState: [ "transcriptome_bam_index": "output" ],
        key: "transcriptome_sorted"
    )
    | samtools_stats.run (
        runIf: { id, state -> state.with_umi && state.aligner == 'star_salmon' },
        fromState: [
          "input": "transcriptome_bam", 
          "bai": "transcriptome_bam_index", 
        ],
        toState: [ "transcriptome_bam_stats": "output" ],
        key: "transcriptome_stats"
    )
    | samtools_flagstat.run (
        runIf: { id, state -> state.with_umi && state.aligner == 'star_salmon' },
        fromState: [
          "bam": "transcriptome_bam", 
          "bai": "transcriptome_bam_index"
        ],
        toState: [ "transcriptome_bam_flagstat": "output" ],
        key: "transcriptome_flagstat"
    )
    | samtools_idxstats.run(
        runIf: { id, state -> state.with_umi && state.aligner == 'star_salmon' },
        fromState: [
          "bam": "transcriptome_bam", 
          "bai": "transcriptome_bam_index" 
        ],
        toState: [ "transcriptome_bam_idxstats": "output" ],
        key: "transcriptome_idxstats"
    )    
     
    | umitools_dedup.run ( 
        runIf: { id, state -> state.with_umi && state.aligner == 'star_salmon' },
        fromState: [
          "paired": "paired", 
          "bam": "transcriptome_bam", 
          "bai": "transcriptome_bam_index",
          "get_output_stats": "umi_dedup_stats", 
        ],
        toState: [ "transcriptome_bam_deduped": "output_bam" ],
        key: "transcriptome_deduped"
    )
    | samtools_sort.run (
        runIf: { id, state -> state.with_umi && state.aligner == 'star_salmon' }, 
        fromState: [ "input": "transcriptome_bam_deduped" ],
        toState: [ "transcriptome_bam": "output" ],
        key: "transcriptome_deduped_sorted"
    )
    | samtools_index.run (
      runIf: { id, state -> state.with_umi && state.aligner == 'star_salmon' },
      fromState: [
        "input": "transcriptome_bam", 
        "csi": "bam_csi_index"
      ],
      toState: [ "transcriptome_bam_index": "output" ],
      key: "transcriptome_deduped_sorted"
    )
    | samtools_stats.run (
        runIf: { id, state -> state.with_umi && state.aligner == 'star_salmon' }, 
        fromState: [
          "input": "transcriptome_bam", 
          "bai": "transcriptome_bam_index"
        ],
        toState: [ "transcriptome_bam_stats": "output" ],
        key: "transcriptome_deduped_stats"
    )
    | samtools_flagstat.run (
        runIf: { id, state -> state.with_umi && state.aligner == 'star_salmon' }, 
        fromState: [
          "bam": "transcriptome_bam", 
          "bai": "transcriptome_bam_index"
        ],
        toState: [ "transcriptome_bam_flagstat": "output" ],
        key: "transcriptome_deduped_flagstat"
    )
    | samtools_idxstats.run(
        runIf: { id, state -> state.with_umi && state.aligner == 'star_salmon' }, 
        fromState: [
          "bam": "transcriptome_bam", 
          "bai": "transcriptome_bam_index" 
        ],
        toState: [ "transcriptome_bam_idxstats": "output" ],
        key: "transcriptome_deduped_idxstats"
    ) 

    // Fix paired-end reads in name sorted BAM file
    | umitools_prepareforquant.run (
        runIf: { id, state -> state.with_umi && state.paired && state.aligner == 'star_salmon' },
        fromState: [ "bam": "transcriptome_bam" ],
        toState: [ "transcriptome_bam": "output" ]
    )

    // Infer lib-type for salmon quant
    | map { id, state -> 
      def lib_type = (state.paired) ? 
        (
          (state.strandedness == "forward") ? 
            "ISF" : 
            (
              (state.strandedness == "reverse") ? "ISR" : "IU"
            )
        ) 
        : (
          (state.strandedness == "forward") ? 
            "SF" : 
            (
              (state.strandedness == "reverse") ? "SR" : "U"
            )
        ) 
      [ id, state + [lib_type: lib_type] ]
    }

    // Count reads from BAM alignments using Salmon
    | salmon_quant.run ( 
        runIf: { id, state -> state.aligner == 'star_salmon' },
        fromState: [
          "lib_type": "lib_type",
          "alignments": "transcriptome_bam", 
          "targets": "transcript_fasta", 
          "gene_map": "gtf"
        ],
        toState: [ 
          "quant_out_dir": "output",
          "quant_results_file": "quant_results" 
        ]
    )

    | rsem_calculate_expression.run (
        runIf: { id, state -> state.aligner == 'star_rsem' },
        fromState: [
          "id": "id",
          "strandedness": "strandedness",
          "paired": "paired",
          "input": "input",
          "index": "rsem_index",
          "extra_args": "extra_args"
        ],
        toState: [
          "rsem_counts_gene": "counts_gene",
          "rsem_counts_transcripts": "counts_transcripts",
          "rsem_multiqc": "stat",
          "star_multiqc": "logs",
          "bam_star_rsem": "bam_star",
          "bam_genome_rsem": "bam_genome",
          "bam_transcript_rsem": "bam_transcript"
        ]
    )

    // RSEM_Star BAM
    | samtools_sort.run (
        runIf: { id, state -> state.aligner == 'star_rsem' },
        fromState: ["input": "bam_star_rsem"],
        toState: ["genome_bam_sorted": "output"],
        key: "genome_sorted"
    )
    | samtools_index.run (
        runIf: { id, state -> state.aligner == 'star_rsem' },
        fromState: [ 
          "input": "genome_bam_sorted", 
          "csi": "bam_csi_index"
        ],
        toState: [ "genome_bam_index": "output" ],
        key: "genome_sorted"
    )
    | samtools_stats.run (
        runIf: { id, state -> state.aligner == 'star_rsem' },
        fromState: [
          "input": "genome_bam_sorted", 
          "bai": "genome_bam_index", 
          "fasta": "fasta"
        ],
        toState: [ "genome_bam_stats": "output" ],
        key: "genome_stats"
    )
    | samtools_flagstat.run (
        runIf: { id, state -> state.aligner == 'star_rsem' },
        fromState: [
          "bam": "genome_bam_sorted", 
          "bai": "genome_bam_index", 
          "fasta": "fasta" 
        ],
        toState: [ "genome_bam_flagstat": "output" ],
        key: "genome_flagstat"
    )
    | samtools_idxstats.run(
        runIf: { id, state -> state.aligner == 'star_rsem' },
        fromState: [
          "bam": "genome_bam_sorted", 
          "bai": "genome_bam_index", 
          "fasta": "fasta" 
        ],
        toState: [ "genome_bam_idxstats": "output" ],
        key: "genome_idxstats"
    )

    | map { id, state -> 
      def mod_state = state.findAll { key, value -> value instanceof java.nio.file.Path && value.exists() }
      [ id, mod_state ]
    }

    | setState (
      [ "star_alignment": "star_alignment", 
        "star_multiqc": "star_multiqc", 
        "rsem_multiqc": "rsem_multiqc",
        "genome_bam_sorted": "genome_bam_sorted", 
        "genome_bam_index": "genome_bam_index",  
        "genome_bam_stats": "genome_bam_stats", 
        "genome_bam_flagstat": "genome_bam_flagstat", 
        "genome_bam_idxstats": "genome_bam_idxstats", 
        "transcriptome_bam": "transcriptome_bam", 
        "transcriptome_bam_index": "transcriptome_bam_index", 
        "transcriptome_bam_stats": "transcriptome_bam_stats", 
        "transcriptome_bam_flagstat": "transcriptome_bam_flagstat", 
        "transcriptome_bam_idxstats": "transcriptome_bam_idxstats",
        "quant_out_dir": "quant_out_dir",
        "quant_results_file": "quant_results_file" ]
    )
    
  emit:
    output_ch
}
VIASHMAIN
nextflow run . -main-script "\$tempscript" &
wait "\$!"

VIASHEOF


# check whether required files exist
if [ ! -z "$VIASH_PAR_STAR_MULTIQC" ] && [ ! -e "$VIASH_PAR_STAR_MULTIQC" ]; then
  ViashError "Output file '$VIASH_PAR_STAR_MULTIQC' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_GENOME_BAM_SORTED" ] && [ ! -e "$VIASH_PAR_GENOME_BAM_SORTED" ]; then
  ViashError "Output file '$VIASH_PAR_GENOME_BAM_SORTED' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_GENOME_BAM_INDEX" ] && [ ! -e "$VIASH_PAR_GENOME_BAM_INDEX" ]; then
  ViashError "Output file '$VIASH_PAR_GENOME_BAM_INDEX' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_GENOME_BAM_STATS" ] && [ ! -e "$VIASH_PAR_GENOME_BAM_STATS" ]; then
  ViashError "Output file '$VIASH_PAR_GENOME_BAM_STATS' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_GENOME_BAM_FLAGSTAT" ] && [ ! -e "$VIASH_PAR_GENOME_BAM_FLAGSTAT" ]; then
  ViashError "Output file '$VIASH_PAR_GENOME_BAM_FLAGSTAT' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_GENOME_BAM_IDXSTATS" ] && [ ! -e "$VIASH_PAR_GENOME_BAM_IDXSTATS" ]; then
  ViashError "Output file '$VIASH_PAR_GENOME_BAM_IDXSTATS' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_TRANSCRIPTOME_BAM" ] && [ ! -e "$VIASH_PAR_TRANSCRIPTOME_BAM" ]; then
  ViashError "Output file '$VIASH_PAR_TRANSCRIPTOME_BAM' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_TRANSCRIPTOME_BAM_INDEX" ] && [ ! -e "$VIASH_PAR_TRANSCRIPTOME_BAM_INDEX" ]; then
  ViashError "Output file '$VIASH_PAR_TRANSCRIPTOME_BAM_INDEX' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_TRANSCRIPTOME_BAM_STATS" ] && [ ! -e "$VIASH_PAR_TRANSCRIPTOME_BAM_STATS" ]; then
  ViashError "Output file '$VIASH_PAR_TRANSCRIPTOME_BAM_STATS' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_TRANSCRIPTOME_BAM_FLAGSTAT" ] && [ ! -e "$VIASH_PAR_TRANSCRIPTOME_BAM_FLAGSTAT" ]; then
  ViashError "Output file '$VIASH_PAR_TRANSCRIPTOME_BAM_FLAGSTAT' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_TRANSCRIPTOME_BAM_IDXSTATS" ] && [ ! -e "$VIASH_PAR_TRANSCRIPTOME_BAM_IDXSTATS" ]; then
  ViashError "Output file '$VIASH_PAR_TRANSCRIPTOME_BAM_IDXSTATS' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_QUANT_OUT_DIR" ] && [ ! -e "$VIASH_PAR_QUANT_OUT_DIR" ]; then
  ViashError "Output file '$VIASH_PAR_QUANT_OUT_DIR' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_QUANT_RESULTS_FILE" ] && [ ! -e "$VIASH_PAR_QUANT_RESULTS_FILE" ]; then
  ViashError "Output file '$VIASH_PAR_QUANT_RESULTS_FILE' does not exist."
  exit 1
fi


exit 0
