#!/usr/bin/env bash

# prepare_genome 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="prepare_genome"
VIASH_META_FUNCTIONALITY_NAME="prepare_genome"
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 "prepare_genome main"
  echo ""
  echo "A subworkflow for preparing all the required genome references"
  echo ""
  echo "Input:"
  echo "    --fasta"
  echo "        type: file, required parameter, file must exist"
  echo "        Path to FASTA genome file."
  echo ""
  echo "    --gtf"
  echo "        type: file, file must exist"
  echo "        Path to GTF annotation file. This parameter is *mandatory* if --genome"
  echo "        is not specified."
  echo ""
  echo "    --gff"
  echo "        type: file, file must exist"
  echo "        Path to GFF3 annotation file. Required if \"--gtf\" is not specified."
  echo ""
  echo "    --additional_fasta"
  echo "        type: file, file must exist"
  echo "        FASTA file to concatenate to genome FASTA file e.g. containing spike-in"
  echo "        sequences."
  echo ""
  echo "    --transcript_fasta"
  echo "        type: file, file must exist"
  echo "        Path to FASTA transcriptome file."
  echo ""
  echo "    --gene_bed"
  echo "        type: file, file must exist"
  echo "        Path to BED file containing gene intervals. This will be created from"
  echo "        the GTF file if not specified."
  echo ""
  echo "    --splicesites"
  echo "        type: file, file must exist"
  echo "        Splice sites file required for HISAT2."
  echo ""
  echo "    --skip_bbsplit"
  echo "        type: boolean"
  echo "        Skip BBSplit for removal of non-reference genome reads."
  echo ""
  echo "    --bbsplit_fasta_list"
  echo "        type: file, multiple values allowed, file must exist"
  echo "        List of reference genomes (separated by \";\") to filter reads against"
  echo "        with BBSplit."
  echo ""
  echo "    --star_index"
  echo "        type: file, file must exist"
  echo "        Path to directory or tar.gz archive for pre-built STAR index."
  echo ""
  echo "    --star_sjdb_gtf_feature_exon"
  echo "        type: string"
  echo "        Feature type in GTF file to be used as exons for building transcripts"
  echo ""
  echo "    --rsem_index"
  echo "        type: file, file must exist"
  echo "        Path to directory or tar.gz archive for pre-built RSEM index."
  echo ""
  echo "    --salmon_index"
  echo "        type: file, file must exist"
  echo "        Path to directory or tar.gz archive for pre-built Salmon index."
  echo ""
  echo "    --kallisto_index"
  echo "        type: file, file must exist"
  echo "        Path to directory or tar.gz archive for pre-built Kallisto index."
  echo ""
  echo "    --bbsplit_index"
  echo "        type: file, file must exist"
  echo "        Path to directory or tar.gz archive for pre-built BBSplit index."
  echo ""
  echo "    --pseudo_aligner_kmer_size"
  echo "        type: integer"
  echo "        default: 31"
  echo "        Kmer length passed to indexing step of pseudoaligners."
  echo ""
  echo "    --gencode"
  echo "        type: boolean"
  echo "        Specify if the GTF annotation is in GENCODE format."
  echo ""
  echo "    --biotype"
  echo "        type: string"
  echo "        Biotype value to use while appending entries to GTF file when additional"
  echo "        fasta file is provided."
  echo ""
  echo "    --filter_gtf"
  echo "        type: boolean"
  echo "        Whether to filter the GTF or not?"
  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 "    --pseudo_aligner"
  echo "        type: string"
  echo "        default: salmon"
  echo "        choices: [ salmon, kallisto ]"
  echo "        Specifies the pseudo aligner to use - available options are 'salmon'."
  echo "        Runs in addition to '--aligner'."
  echo ""
  echo "    --skip_alignment"
  echo "        type: boolean_true"
  echo "        Skip all of the alignment-based processes within the pipeline."
  echo ""
  echo "Output:"
  echo "    --fasta_uncompressed"
  echo "        type: file, output, file must exist"
  echo "        default: reference_genome.fasta"
  echo ""
  echo "    --gtf_uncompressed"
  echo "        type: file, output, file must exist"
  echo "        default: gene_annotation.gtf"
  echo ""
  echo "    --transcript_fasta_uncompressed"
  echo "        type: file, output, file must exist"
  echo "        default: transcriptome.fasta"
  echo ""
  echo "    --gene_bed_uncompressed"
  echo "        type: file, output, file must exist"
  echo "        default: gene_annotation.bed"
  echo ""
  echo "    --star_index_uncompressed"
  echo "        type: file, output, file must exist"
  echo "        default: STAR_index"
  echo "        Path to STAR index."
  echo ""
  echo "    --rsem_index_uncompressed"
  echo "        type: file, output, file must exist"
  echo "        default: RSEM_index"
  echo "        Path to directory or tar.gz archive for pre-built RSEM index."
  echo ""
  echo "    --salmon_index_uncompressed"
  echo "        type: file, output, file must exist"
  echo "        default: Salmon_index"
  echo "        Path to Salmon index."
  echo ""
  echo "    --kallisto_index_uncompressed"
  echo "        type: file, output, file must exist"
  echo "        default: Kallisto_index"
  echo "        Path to Kallisto index."
  echo ""
  echo "    --bbsplit_index_uncompressed"
  echo "        type: file, output, file must exist"
  echo "        default: BBSplit_index"
  echo "        Path to BBSplit index."
  echo ""
  echo "    --chrom_sizes"
  echo "        type: file, output, file must exist"
  echo "        default: reference_genome.fasta.sizes"
  echo "        File containing chromosome lengths"
  echo ""
  echo "    --fai"
  echo "        type: file, output, file must exist"
  echo "        default: reference_genome.fasta.fai"
  echo "        FASTA index file"
}

# 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 "prepare_genome main"
            exit
            ;;
        --fasta)
            [ -n "$VIASH_PAR_FASTA" ] && ViashError Bad arguments for option \'--fasta\': \'$VIASH_PAR_FASTA\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_FASTA="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --fasta. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --fasta=*)
            [ -n "$VIASH_PAR_FASTA" ] && ViashError Bad arguments for option \'--fasta=*\': \'$VIASH_PAR_FASTA\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_FASTA=$(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
            ;;
        --gff)
            [ -n "$VIASH_PAR_GFF" ] && ViashError Bad arguments for option \'--gff\': \'$VIASH_PAR_GFF\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_GFF="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --gff. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --gff=*)
            [ -n "$VIASH_PAR_GFF" ] && ViashError Bad arguments for option \'--gff=*\': \'$VIASH_PAR_GFF\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_GFF=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --additional_fasta)
            [ -n "$VIASH_PAR_ADDITIONAL_FASTA" ] && ViashError Bad arguments for option \'--additional_fasta\': \'$VIASH_PAR_ADDITIONAL_FASTA\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_ADDITIONAL_FASTA="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --additional_fasta. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --additional_fasta=*)
            [ -n "$VIASH_PAR_ADDITIONAL_FASTA" ] && ViashError Bad arguments for option \'--additional_fasta=*\': \'$VIASH_PAR_ADDITIONAL_FASTA\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_ADDITIONAL_FASTA=$(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
            ;;
        --gene_bed)
            [ -n "$VIASH_PAR_GENE_BED" ] && ViashError Bad arguments for option \'--gene_bed\': \'$VIASH_PAR_GENE_BED\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_GENE_BED="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --gene_bed. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --gene_bed=*)
            [ -n "$VIASH_PAR_GENE_BED" ] && ViashError Bad arguments for option \'--gene_bed=*\': \'$VIASH_PAR_GENE_BED\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_GENE_BED=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --splicesites)
            [ -n "$VIASH_PAR_SPLICESITES" ] && ViashError Bad arguments for option \'--splicesites\': \'$VIASH_PAR_SPLICESITES\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_SPLICESITES="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --splicesites. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --splicesites=*)
            [ -n "$VIASH_PAR_SPLICESITES" ] && ViashError Bad arguments for option \'--splicesites=*\': \'$VIASH_PAR_SPLICESITES\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_SPLICESITES=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --skip_bbsplit)
            [ -n "$VIASH_PAR_SKIP_BBSPLIT" ] && ViashError Bad arguments for option \'--skip_bbsplit\': \'$VIASH_PAR_SKIP_BBSPLIT\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_SKIP_BBSPLIT="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --skip_bbsplit. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --skip_bbsplit=*)
            [ -n "$VIASH_PAR_SKIP_BBSPLIT" ] && ViashError Bad arguments for option \'--skip_bbsplit=*\': \'$VIASH_PAR_SKIP_BBSPLIT\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_SKIP_BBSPLIT=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --bbsplit_fasta_list)
            if [ -z "$VIASH_PAR_BBSPLIT_FASTA_LIST" ]; then
              VIASH_PAR_BBSPLIT_FASTA_LIST="$2"
            else
              VIASH_PAR_BBSPLIT_FASTA_LIST="$VIASH_PAR_BBSPLIT_FASTA_LIST;""$2"
            fi
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --bbsplit_fasta_list. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --bbsplit_fasta_list=*)
            if [ -z "$VIASH_PAR_BBSPLIT_FASTA_LIST" ]; then
              VIASH_PAR_BBSPLIT_FASTA_LIST=$(ViashRemoveFlags "$1")
            else
              VIASH_PAR_BBSPLIT_FASTA_LIST="$VIASH_PAR_BBSPLIT_FASTA_LIST;"$(ViashRemoveFlags "$1")
            fi
            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_sjdb_gtf_feature_exon)
            [ -n "$VIASH_PAR_STAR_SJDB_GTF_FEATURE_EXON" ] && ViashError Bad arguments for option \'--star_sjdb_gtf_feature_exon\': \'$VIASH_PAR_STAR_SJDB_GTF_FEATURE_EXON\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_STAR_SJDB_GTF_FEATURE_EXON="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --star_sjdb_gtf_feature_exon. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --star_sjdb_gtf_feature_exon=*)
            [ -n "$VIASH_PAR_STAR_SJDB_GTF_FEATURE_EXON" ] && ViashError Bad arguments for option \'--star_sjdb_gtf_feature_exon=*\': \'$VIASH_PAR_STAR_SJDB_GTF_FEATURE_EXON\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_STAR_SJDB_GTF_FEATURE_EXON=$(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
            ;;
        --kallisto_index)
            [ -n "$VIASH_PAR_KALLISTO_INDEX" ] && ViashError Bad arguments for option \'--kallisto_index\': \'$VIASH_PAR_KALLISTO_INDEX\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_KALLISTO_INDEX="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --kallisto_index. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --kallisto_index=*)
            [ -n "$VIASH_PAR_KALLISTO_INDEX" ] && ViashError Bad arguments for option \'--kallisto_index=*\': \'$VIASH_PAR_KALLISTO_INDEX\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_KALLISTO_INDEX=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --bbsplit_index)
            [ -n "$VIASH_PAR_BBSPLIT_INDEX" ] && ViashError Bad arguments for option \'--bbsplit_index\': \'$VIASH_PAR_BBSPLIT_INDEX\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_BBSPLIT_INDEX="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --bbsplit_index. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --bbsplit_index=*)
            [ -n "$VIASH_PAR_BBSPLIT_INDEX" ] && ViashError Bad arguments for option \'--bbsplit_index=*\': \'$VIASH_PAR_BBSPLIT_INDEX\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_BBSPLIT_INDEX=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --pseudo_aligner_kmer_size)
            [ -n "$VIASH_PAR_PSEUDO_ALIGNER_KMER_SIZE" ] && ViashError Bad arguments for option \'--pseudo_aligner_kmer_size\': \'$VIASH_PAR_PSEUDO_ALIGNER_KMER_SIZE\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_PSEUDO_ALIGNER_KMER_SIZE="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --pseudo_aligner_kmer_size. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --pseudo_aligner_kmer_size=*)
            [ -n "$VIASH_PAR_PSEUDO_ALIGNER_KMER_SIZE" ] && ViashError Bad arguments for option \'--pseudo_aligner_kmer_size=*\': \'$VIASH_PAR_PSEUDO_ALIGNER_KMER_SIZE\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_PSEUDO_ALIGNER_KMER_SIZE=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --gencode)
            [ -n "$VIASH_PAR_GENCODE" ] && ViashError Bad arguments for option \'--gencode\': \'$VIASH_PAR_GENCODE\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_GENCODE="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --gencode. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --gencode=*)
            [ -n "$VIASH_PAR_GENCODE" ] && ViashError Bad arguments for option \'--gencode=*\': \'$VIASH_PAR_GENCODE\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_GENCODE=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --biotype)
            [ -n "$VIASH_PAR_BIOTYPE" ] && ViashError Bad arguments for option \'--biotype\': \'$VIASH_PAR_BIOTYPE\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_BIOTYPE="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --biotype. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --biotype=*)
            [ -n "$VIASH_PAR_BIOTYPE" ] && ViashError Bad arguments for option \'--biotype=*\': \'$VIASH_PAR_BIOTYPE\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_BIOTYPE=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --filter_gtf)
            [ -n "$VIASH_PAR_FILTER_GTF" ] && ViashError Bad arguments for option \'--filter_gtf\': \'$VIASH_PAR_FILTER_GTF\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_FILTER_GTF="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --filter_gtf. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --filter_gtf=*)
            [ -n "$VIASH_PAR_FILTER_GTF" ] && ViashError Bad arguments for option \'--filter_gtf=*\': \'$VIASH_PAR_FILTER_GTF\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_FILTER_GTF=$(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
            ;;
        --pseudo_aligner)
            [ -n "$VIASH_PAR_PSEUDO_ALIGNER" ] && ViashError Bad arguments for option \'--pseudo_aligner\': \'$VIASH_PAR_PSEUDO_ALIGNER\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_PSEUDO_ALIGNER="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --pseudo_aligner. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --pseudo_aligner=*)
            [ -n "$VIASH_PAR_PSEUDO_ALIGNER" ] && ViashError Bad arguments for option \'--pseudo_aligner=*\': \'$VIASH_PAR_PSEUDO_ALIGNER\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_PSEUDO_ALIGNER=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --skip_alignment)
            [ -n "$VIASH_PAR_SKIP_ALIGNMENT" ] && ViashError Bad arguments for option \'--skip_alignment\': \'$VIASH_PAR_SKIP_ALIGNMENT\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_SKIP_ALIGNMENT=true
            shift 1
            ;;
        --fasta_uncompressed)
            [ -n "$VIASH_PAR_FASTA_UNCOMPRESSED" ] && ViashError Bad arguments for option \'--fasta_uncompressed\': \'$VIASH_PAR_FASTA_UNCOMPRESSED\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_FASTA_UNCOMPRESSED="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --fasta_uncompressed. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --fasta_uncompressed=*)
            [ -n "$VIASH_PAR_FASTA_UNCOMPRESSED" ] && ViashError Bad arguments for option \'--fasta_uncompressed=*\': \'$VIASH_PAR_FASTA_UNCOMPRESSED\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_FASTA_UNCOMPRESSED=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --gtf_uncompressed)
            [ -n "$VIASH_PAR_GTF_UNCOMPRESSED" ] && ViashError Bad arguments for option \'--gtf_uncompressed\': \'$VIASH_PAR_GTF_UNCOMPRESSED\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_GTF_UNCOMPRESSED="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --gtf_uncompressed. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --gtf_uncompressed=*)
            [ -n "$VIASH_PAR_GTF_UNCOMPRESSED" ] && ViashError Bad arguments for option \'--gtf_uncompressed=*\': \'$VIASH_PAR_GTF_UNCOMPRESSED\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_GTF_UNCOMPRESSED=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --transcript_fasta_uncompressed)
            [ -n "$VIASH_PAR_TRANSCRIPT_FASTA_UNCOMPRESSED" ] && ViashError Bad arguments for option \'--transcript_fasta_uncompressed\': \'$VIASH_PAR_TRANSCRIPT_FASTA_UNCOMPRESSED\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRANSCRIPT_FASTA_UNCOMPRESSED="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --transcript_fasta_uncompressed. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --transcript_fasta_uncompressed=*)
            [ -n "$VIASH_PAR_TRANSCRIPT_FASTA_UNCOMPRESSED" ] && ViashError Bad arguments for option \'--transcript_fasta_uncompressed=*\': \'$VIASH_PAR_TRANSCRIPT_FASTA_UNCOMPRESSED\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRANSCRIPT_FASTA_UNCOMPRESSED=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --gene_bed_uncompressed)
            [ -n "$VIASH_PAR_GENE_BED_UNCOMPRESSED" ] && ViashError Bad arguments for option \'--gene_bed_uncompressed\': \'$VIASH_PAR_GENE_BED_UNCOMPRESSED\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_GENE_BED_UNCOMPRESSED="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --gene_bed_uncompressed. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --gene_bed_uncompressed=*)
            [ -n "$VIASH_PAR_GENE_BED_UNCOMPRESSED" ] && ViashError Bad arguments for option \'--gene_bed_uncompressed=*\': \'$VIASH_PAR_GENE_BED_UNCOMPRESSED\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_GENE_BED_UNCOMPRESSED=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --star_index_uncompressed)
            [ -n "$VIASH_PAR_STAR_INDEX_UNCOMPRESSED" ] && ViashError Bad arguments for option \'--star_index_uncompressed\': \'$VIASH_PAR_STAR_INDEX_UNCOMPRESSED\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_STAR_INDEX_UNCOMPRESSED="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --star_index_uncompressed. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --star_index_uncompressed=*)
            [ -n "$VIASH_PAR_STAR_INDEX_UNCOMPRESSED" ] && ViashError Bad arguments for option \'--star_index_uncompressed=*\': \'$VIASH_PAR_STAR_INDEX_UNCOMPRESSED\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_STAR_INDEX_UNCOMPRESSED=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --rsem_index_uncompressed)
            [ -n "$VIASH_PAR_RSEM_INDEX_UNCOMPRESSED" ] && ViashError Bad arguments for option \'--rsem_index_uncompressed\': \'$VIASH_PAR_RSEM_INDEX_UNCOMPRESSED\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_RSEM_INDEX_UNCOMPRESSED="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --rsem_index_uncompressed. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --rsem_index_uncompressed=*)
            [ -n "$VIASH_PAR_RSEM_INDEX_UNCOMPRESSED" ] && ViashError Bad arguments for option \'--rsem_index_uncompressed=*\': \'$VIASH_PAR_RSEM_INDEX_UNCOMPRESSED\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_RSEM_INDEX_UNCOMPRESSED=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --salmon_index_uncompressed)
            [ -n "$VIASH_PAR_SALMON_INDEX_UNCOMPRESSED" ] && ViashError Bad arguments for option \'--salmon_index_uncompressed\': \'$VIASH_PAR_SALMON_INDEX_UNCOMPRESSED\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_SALMON_INDEX_UNCOMPRESSED="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --salmon_index_uncompressed. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --salmon_index_uncompressed=*)
            [ -n "$VIASH_PAR_SALMON_INDEX_UNCOMPRESSED" ] && ViashError Bad arguments for option \'--salmon_index_uncompressed=*\': \'$VIASH_PAR_SALMON_INDEX_UNCOMPRESSED\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_SALMON_INDEX_UNCOMPRESSED=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --kallisto_index_uncompressed)
            [ -n "$VIASH_PAR_KALLISTO_INDEX_UNCOMPRESSED" ] && ViashError Bad arguments for option \'--kallisto_index_uncompressed\': \'$VIASH_PAR_KALLISTO_INDEX_UNCOMPRESSED\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_KALLISTO_INDEX_UNCOMPRESSED="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --kallisto_index_uncompressed. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --kallisto_index_uncompressed=*)
            [ -n "$VIASH_PAR_KALLISTO_INDEX_UNCOMPRESSED" ] && ViashError Bad arguments for option \'--kallisto_index_uncompressed=*\': \'$VIASH_PAR_KALLISTO_INDEX_UNCOMPRESSED\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_KALLISTO_INDEX_UNCOMPRESSED=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --bbsplit_index_uncompressed)
            [ -n "$VIASH_PAR_BBSPLIT_INDEX_UNCOMPRESSED" ] && ViashError Bad arguments for option \'--bbsplit_index_uncompressed\': \'$VIASH_PAR_BBSPLIT_INDEX_UNCOMPRESSED\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_BBSPLIT_INDEX_UNCOMPRESSED="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --bbsplit_index_uncompressed. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --bbsplit_index_uncompressed=*)
            [ -n "$VIASH_PAR_BBSPLIT_INDEX_UNCOMPRESSED" ] && ViashError Bad arguments for option \'--bbsplit_index_uncompressed=*\': \'$VIASH_PAR_BBSPLIT_INDEX_UNCOMPRESSED\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_BBSPLIT_INDEX_UNCOMPRESSED=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --chrom_sizes)
            [ -n "$VIASH_PAR_CHROM_SIZES" ] && ViashError Bad arguments for option \'--chrom_sizes\': \'$VIASH_PAR_CHROM_SIZES\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_CHROM_SIZES="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --chrom_sizes. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --chrom_sizes=*)
            [ -n "$VIASH_PAR_CHROM_SIZES" ] && ViashError Bad arguments for option \'--chrom_sizes=*\': \'$VIASH_PAR_CHROM_SIZES\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_CHROM_SIZES=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --fai)
            [ -n "$VIASH_PAR_FAI" ] && ViashError Bad arguments for option \'--fai\': \'$VIASH_PAR_FAI\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_FAI="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --fai. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --fai=*)
            [ -n "$VIASH_PAR_FAI" ] && ViashError Bad arguments for option \'--fai=*\': \'$VIASH_PAR_FAI\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_FAI=$(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


# check whether required parameters exist
if [ -z ${VIASH_PAR_FASTA+x} ]; then
  ViashError '--fasta' 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_PSEUDO_ALIGNER_KMER_SIZE+x} ]; then
  VIASH_PAR_PSEUDO_ALIGNER_KMER_SIZE="31"
fi
if [ -z ${VIASH_PAR_ALIGNER+x} ]; then
  VIASH_PAR_ALIGNER="star_salmon"
fi
if [ -z ${VIASH_PAR_PSEUDO_ALIGNER+x} ]; then
  VIASH_PAR_PSEUDO_ALIGNER="salmon"
fi
if [ -z ${VIASH_PAR_SKIP_ALIGNMENT+x} ]; then
  VIASH_PAR_SKIP_ALIGNMENT="false"
fi
if [ -z ${VIASH_PAR_FASTA_UNCOMPRESSED+x} ]; then
  VIASH_PAR_FASTA_UNCOMPRESSED="reference_genome.fasta"
fi
if [ -z ${VIASH_PAR_GTF_UNCOMPRESSED+x} ]; then
  VIASH_PAR_GTF_UNCOMPRESSED="gene_annotation.gtf"
fi
if [ -z ${VIASH_PAR_TRANSCRIPT_FASTA_UNCOMPRESSED+x} ]; then
  VIASH_PAR_TRANSCRIPT_FASTA_UNCOMPRESSED="transcriptome.fasta"
fi
if [ -z ${VIASH_PAR_GENE_BED_UNCOMPRESSED+x} ]; then
  VIASH_PAR_GENE_BED_UNCOMPRESSED="gene_annotation.bed"
fi
if [ -z ${VIASH_PAR_STAR_INDEX_UNCOMPRESSED+x} ]; then
  VIASH_PAR_STAR_INDEX_UNCOMPRESSED="STAR_index"
fi
if [ -z ${VIASH_PAR_RSEM_INDEX_UNCOMPRESSED+x} ]; then
  VIASH_PAR_RSEM_INDEX_UNCOMPRESSED="RSEM_index"
fi
if [ -z ${VIASH_PAR_SALMON_INDEX_UNCOMPRESSED+x} ]; then
  VIASH_PAR_SALMON_INDEX_UNCOMPRESSED="Salmon_index"
fi
if [ -z ${VIASH_PAR_KALLISTO_INDEX_UNCOMPRESSED+x} ]; then
  VIASH_PAR_KALLISTO_INDEX_UNCOMPRESSED="Kallisto_index"
fi
if [ -z ${VIASH_PAR_BBSPLIT_INDEX_UNCOMPRESSED+x} ]; then
  VIASH_PAR_BBSPLIT_INDEX_UNCOMPRESSED="BBSplit_index"
fi
if [ -z ${VIASH_PAR_CHROM_SIZES+x} ]; then
  VIASH_PAR_CHROM_SIZES="reference_genome.fasta.sizes"
fi
if [ -z ${VIASH_PAR_FAI+x} ]; then
  VIASH_PAR_FAI="reference_genome.fasta.fai"
fi

# check whether required files exist
if [ ! -z "$VIASH_PAR_FASTA" ] && [ ! -e "$VIASH_PAR_FASTA" ]; then
  ViashError "Input file '$VIASH_PAR_FASTA' 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_GFF" ] && [ ! -e "$VIASH_PAR_GFF" ]; then
  ViashError "Input file '$VIASH_PAR_GFF' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_ADDITIONAL_FASTA" ] && [ ! -e "$VIASH_PAR_ADDITIONAL_FASTA" ]; then
  ViashError "Input file '$VIASH_PAR_ADDITIONAL_FASTA' 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_GENE_BED" ] && [ ! -e "$VIASH_PAR_GENE_BED" ]; then
  ViashError "Input file '$VIASH_PAR_GENE_BED' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_SPLICESITES" ] && [ ! -e "$VIASH_PAR_SPLICESITES" ]; then
  ViashError "Input file '$VIASH_PAR_SPLICESITES' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_BBSPLIT_FASTA_LIST" ]; then
  IFS=';'
  set -f
  for file in $VIASH_PAR_BBSPLIT_FASTA_LIST; do
    unset IFS
    if [ ! -e "$file" ]; then
      ViashError "Input file '$file' does not exist."
      exit 1
    fi
  done
  set +f
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
if [ ! -z "$VIASH_PAR_KALLISTO_INDEX" ] && [ ! -e "$VIASH_PAR_KALLISTO_INDEX" ]; then
  ViashError "Input file '$VIASH_PAR_KALLISTO_INDEX' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_BBSPLIT_INDEX" ] && [ ! -e "$VIASH_PAR_BBSPLIT_INDEX" ]; then
  ViashError "Input file '$VIASH_PAR_BBSPLIT_INDEX' does not exist."
  exit 1
fi

# check whether parameters values are of the right type
if [[ -n "$VIASH_PAR_SKIP_BBSPLIT" ]]; then
  if ! [[ "$VIASH_PAR_SKIP_BBSPLIT" =~ ^(true|True|TRUE|false|False|FALSE|yes|Yes|YES|no|No|NO)$ ]]; then
    ViashError '--skip_bbsplit' has to be a boolean. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_PSEUDO_ALIGNER_KMER_SIZE" ]]; then
  if ! [[ "$VIASH_PAR_PSEUDO_ALIGNER_KMER_SIZE" =~ ^[-+]?[0-9]+$ ]]; then
    ViashError '--pseudo_aligner_kmer_size' has to be an integer. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_GENCODE" ]]; then
  if ! [[ "$VIASH_PAR_GENCODE" =~ ^(true|True|TRUE|false|False|FALSE|yes|Yes|YES|no|No|NO)$ ]]; then
    ViashError '--gencode' has to be a boolean. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_FILTER_GTF" ]]; then
  if ! [[ "$VIASH_PAR_FILTER_GTF" =~ ^(true|True|TRUE|false|False|FALSE|yes|Yes|YES|no|No|NO)$ ]]; then
    ViashError '--filter_gtf' has to be a boolean. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_SKIP_ALIGNMENT" ]]; then
  if ! [[ "$VIASH_PAR_SKIP_ALIGNMENT" =~ ^(true|True|TRUE|false|False|FALSE|yes|Yes|YES|no|No|NO)$ ]]; then
    ViashError '--skip_alignment' has to be a boolean_true. 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_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

if [ ! -z "$VIASH_PAR_PSEUDO_ALIGNER" ]; then
  VIASH_PAR_PSEUDO_ALIGNER_CHOICES=("salmon;kallisto")
  IFS=';'
  set -f
  if ! [[ ";${VIASH_PAR_PSEUDO_ALIGNER_CHOICES[*]};" =~ ";$VIASH_PAR_PSEUDO_ALIGNER;" ]]; then
    ViashError '--pseudo_aligner' specified value of \'$VIASH_PAR_PSEUDO_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_FASTA_UNCOMPRESSED" ] && [ ! -d "$(dirname "$VIASH_PAR_FASTA_UNCOMPRESSED")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_FASTA_UNCOMPRESSED")"
fi
if [ ! -z "$VIASH_PAR_GTF_UNCOMPRESSED" ] && [ ! -d "$(dirname "$VIASH_PAR_GTF_UNCOMPRESSED")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_GTF_UNCOMPRESSED")"
fi
if [ ! -z "$VIASH_PAR_TRANSCRIPT_FASTA_UNCOMPRESSED" ] && [ ! -d "$(dirname "$VIASH_PAR_TRANSCRIPT_FASTA_UNCOMPRESSED")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_TRANSCRIPT_FASTA_UNCOMPRESSED")"
fi
if [ ! -z "$VIASH_PAR_GENE_BED_UNCOMPRESSED" ] && [ ! -d "$(dirname "$VIASH_PAR_GENE_BED_UNCOMPRESSED")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_GENE_BED_UNCOMPRESSED")"
fi
if [ ! -z "$VIASH_PAR_STAR_INDEX_UNCOMPRESSED" ] && [ ! -d "$(dirname "$VIASH_PAR_STAR_INDEX_UNCOMPRESSED")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_STAR_INDEX_UNCOMPRESSED")"
fi
if [ ! -z "$VIASH_PAR_RSEM_INDEX_UNCOMPRESSED" ] && [ ! -d "$(dirname "$VIASH_PAR_RSEM_INDEX_UNCOMPRESSED")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_RSEM_INDEX_UNCOMPRESSED")"
fi
if [ ! -z "$VIASH_PAR_SALMON_INDEX_UNCOMPRESSED" ] && [ ! -d "$(dirname "$VIASH_PAR_SALMON_INDEX_UNCOMPRESSED")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_SALMON_INDEX_UNCOMPRESSED")"
fi
if [ ! -z "$VIASH_PAR_KALLISTO_INDEX_UNCOMPRESSED" ] && [ ! -d "$(dirname "$VIASH_PAR_KALLISTO_INDEX_UNCOMPRESSED")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_KALLISTO_INDEX_UNCOMPRESSED")"
fi
if [ ! -z "$VIASH_PAR_BBSPLIT_INDEX_UNCOMPRESSED" ] && [ ! -d "$(dirname "$VIASH_PAR_BBSPLIT_INDEX_UNCOMPRESSED")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_BBSPLIT_INDEX_UNCOMPRESSED")"
fi
if [ ! -z "$VIASH_PAR_CHROM_SIZES" ] && [ ! -d "$(dirname "$VIASH_PAR_CHROM_SIZES")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_CHROM_SIZES")"
fi
if [ ! -z "$VIASH_PAR_FAI" ] && [ ! -d "$(dirname "$VIASH_PAR_FAI")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_FAI")"
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_GUNZIP="$VIASH_META_RESOURCES_DIR/../../../nextflow/gunzip/main.nf"
VIASH_DEP_CAT_ADDITIONAL_FASTA="$VIASH_META_RESOURCES_DIR/../../../nextflow/cat_additional_fasta/main.nf"
VIASH_DEP_GTF2BED="$VIASH_META_RESOURCES_DIR/../../../nextflow/gtf2bed/main.nf"
VIASH_DEP_PREPROCESS_TRANSCRIPTS_FASTA="$VIASH_META_RESOURCES_DIR/../../../nextflow/preprocess_transcripts_fasta/main.nf"
VIASH_DEP_GTF_FILTER="$VIASH_META_RESOURCES_DIR/../../../nextflow/gtf_filter/main.nf"
VIASH_DEP_GETCHROMSIZES="$VIASH_META_RESOURCES_DIR/../../../nextflow/getchromsizes/main.nf"
VIASH_DEP_GFFREAD="$VIASH_TARGET_DIR/dependencies/vsh/vsh/biobox/main/nextflow/gffread/main.nf"
VIASH_DEP_RSEM_RSEM_PREPARE_REFERENCE="$VIASH_TARGET_DIR/dependencies/vsh/vsh/biobox/main/nextflow/rsem/rsem_prepare_reference/main.nf"
VIASH_DEP_UNTAR="$VIASH_TARGET_DIR/dependencies/vsh/vsh/craftbox/v0.1.0/nextflow/untar/main.nf"
VIASH_DEP_STAR_STAR_GENOME_GENERATE="$VIASH_TARGET_DIR/dependencies/vsh/vsh/biobox/main/nextflow/star/star_genome_generate/main.nf"
VIASH_DEP_BBMAP_BBMAP_BBSPLIT="$VIASH_TARGET_DIR/dependencies/vsh/vsh/biobox/main/nextflow/bbmap/bbmap_bbsplit/main.nf"
VIASH_DEP_SALMON_SALMON_INDEX="$VIASH_TARGET_DIR/dependencies/vsh/vsh/biobox/main/nextflow/salmon/salmon_index/main.nf"
VIASH_DEP_KALLISTO_KALLISTO_INDEX="$VIASH_TARGET_DIR/dependencies/vsh/vsh/biobox/main/nextflow/kallisto/kallisto_index/main.nf"

ViashDebug "Running command: $(echo $VIASH_CMD)"
cat << VIASHEOF | eval $VIASH_CMD
set -e
tempscript=\$(mktemp "$VIASH_META_TEMP_DIR/viash-run-prepare_genome-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 

      // Uncompress fasta
      | gunzip.run (
        fromState: [ "input": "fasta" ], 
        toState: [ "fasta": "output" ], 
        key: "gunzip_fasta",
        args: [ output: "reference_genome.fasta" ],
        directives: [ label: [ "lowmem", "midcpu" ] ]
      )

      // uncompress gtf
      | gunzip.run ( 
        runIf: {id, state -> state.gtf},
        fromState: [ "input": "gtf" ], 
        toState: [ "gtf": "output" ], 
        key: "gunzip_gtf",
        args: [output: "gene_annotation.gtf"],
        directives: [ label: [ "lowmem", "midcpu" ] ]
      )

      // uncompress gff
      | gunzip.run ( 
        runIf: {id, state -> !state.gtf && state.gff},
        fromState: [ "input": "gff" ], 
        toState: [ "gff": "output" ], 
        key: "gunzip_gff",
        args: [output: "gene_annotation.gff"],
        directives: [ label: [ "lowmem", "midcpu" ] ]
      )

      // gff to gtf
      | gffread.run (
        runIf: {id, state -> !state.gtf && state.gff}, 
        fromState: [ 
          "input": "gff",
          "genome": "fasta" 
        ], 
        toState: [ "gtf": "outfile" ],
        args: [
          outfile: "gene_annotation.gtf", 
          gtf_output: true,
          keep_attrs: true,
          keep_exon_attrs: true
        ],
        directives: [ label: [ "lowmem", "midcpu" ] ]
      )

      | gtf_filter.run(
        runIf: {id, state -> state.gtf && state.filter_gtf}, 
        fromState: [
          "fasta": "fasta", 
          "gtf": "gtf"
        ], 
        toState: [ "gtf": "filtered_gtf" ],
        args: [filtered_gtf: "gene_annotation.gtf"],
        directives: [ label: [ "lowmem", "midcpu" ] ]
      )

      // uncompress additional fasta
      | gunzip.run (
        runIf: {id, state -> state.additional_fasta}, 
        fromState: [ "input": "additional_fasta" ], 
        toState: [ "additional_fasta": "output" ], 
        key: "gunzip_additional_fasta",
        args: [output: "additional.fasta"],
        directives: [ label: [ "lowmem", "midcpu" ] ]
      )

      // concatenate additional fasta
      | cat_additional_fasta.run (
        runIf: {id, state -> state.additional_fasta}, 
        fromState: [
          "fasta": "fasta", 
          "gtf": "gtf", 
          "additional_fasta": "additional_fasta", 
          "biotype": "biotype"
        ], 
        toState: [
          "fasta": "fasta_output", 
          "gtf": "gtf_output"
        ], 
        args: [
          fasta_output: "genome_additional.fasta", 
          gtf_output: "genome_additional.gtf"
        ],
        directives: [ label: [ "lowmem", "midcpu" ] ]
      ) 

      // uncompress bed file
      | gunzip.run (
        runIf: {id, state -> state.gene_bed}, 
        fromState: [ "input": "gene_bed" ], 
        toState: [ "gene_bed": "output" ], 
        key: "gunzip_gene_bed",
        args: [output: "genome_additional.bed"],
        directives: [ label: [ "lowmem", "midcpu" ] ]
      )

      // gtf to bed 
      | gtf2bed.run (
        runIf: { id, state -> !state.gene_bed}, 
        fromState: [ "gtf": "gtf" ], 
        toState: [ "gene_bed": "bed_output" ], 
        args: [bed_output: "genome_additional.bed"],
        directives: [ label: [ "lowmem", "midcpu" ] ]
      ) 

      // uncompress transcript fasta
      | gunzip.run (
        runIf: {id, state -> state.transcript_fasta}, 
        fromState: [ "input": "transcript_fasta" ], 
        toState: [ "transcript_fasta": "output" ], 
        key: "transcript_fasta", 
        args: [output: "transcriptome.fasta"],
        directives: [ label: [ "lowmem", "midcpu" ] ]
      )

      // preprocess transcripts fasta if gtf is in gencode format
      | preprocess_transcripts_fasta.run (
        runIf: {id, state -> state.transcript_fasta && state.gencode}, 
        fromState: [ "transcript_fasta": "transcript_fasta" ], 
        toState: [ "transcript_fasta": "output" ], 
        args: [output: "transcriptome.fasta"],
        directives: [ label: [ "lowmem", "midcpu" ] ]
      )

      // make transcript FASTA if not provided
      | rsem_prepare_reference.run ( 
        runIf: {id, state -> !state.transcript_fasta}, 
        fromState: [
            "reference_fasta_files": "fasta", 
            "gtf": "gtf"
        ], 
        toState: [ "make_transcript_fasta_output": "output" ], 
        key: "make_transcript_fasta",
        args: [reference_name: "genome"],
        directives: [ label: [ "lowmem", "midcpu" ] ]
      )
      | map { id, state -> 
        def transcript_fasta = (!state.transcript_fasta) ?
          state.make_transcript_fasta_output.listFiles().find{it.name == "genome.transcripts.fa"} : 
          state.transcript_fasta
        [ id, state + [transcript_fasta: transcript_fasta] ]
      }

      // chromosome size and fai index
      | getchromsizes.run (
        fromState: [ "fasta": "fasta" ], 
        toState: [
            "fai": "fai", 
            "sizes": "sizes"
        ], 
        key: "chromsizes", 
        args: [ 
            fai: "genome_additional.fasta.fai", 
            sizes: "genome_additional.fasta.sizes"
        ],
        directives: [ label: [ "lowmem", "midcpu" ] ]
      )

      // untar bbsplit index, if available
      | untar.run (
        runIf: {id, state -> state.bbsplit_index}, 
        fromState: [ "input": "bbsplit_index" ], 
        toState: [ "bbsplit_index": "output" ], 
        key: "untar_bbsplit_index",
        directives: [ label: [ "lowmem", "midcpu" ] ]
      )

      | map { id, state ->
        // Check if bbsplit_fasta_list is defined
        def ref = (state.bbsplit_fasta_list) ?
          [state.fasta] + state.bbsplit_fasta_list :
          [state.fasta]
        [id, state + [bbsplit_ref: ref] ]
      }

      // create bbsplit index, if not already available
      | bbmap_bbsplit.run (
        runIf: {id, state -> !state.skip_bbsplit && !state.bbsplit_index}, 
        fromState: ["ref": "bbsplit_ref"],
        toState: [ "bbsplit_index": "index" ], 
        args: [
            only_build_index: true,
            index: "BBSplit_index"
        ], 
        key: "generate_bbsplit_index"
      )

      // Uncompress STAR index or generate from scratch if required
      | untar.run (
        runIf: {id, state -> state.star_index}, 
        fromState: [ "input": "star_index" ], 
        toState: [ "star_index": "output" ], 
        key: "untar_star_index",
        args: [output: "STAR_index"],
        directives: [ label: [ "lowmem", "midcpu" ] ]
      )

      | star_genome_generate.run (
        runIf: {id, state -> !state.star_index && !state.skip_alignment}, 
        fromState: [ 
            "genome_fasta_files": "fasta", 
            "sjdb_gtf_file": "gtf",
            "sjdb_gtf_feature_exon": "star_sjdb_gtf_feature_exon"
        ], 
        toState: [ "star_index": "index" ], 
        key: "generate_star_index",
        args: [index: "STAR_index"],
        directives: [ label: [ "highmem", "highcpu" ] ]
      )

      // Uncompress RSEM index or generate from scratch if required
      | untar.run (
        runIf: {id, state -> state.rsem_index}, 
        fromState: [ "input": "rsem_index" ], 
        toState: [ "rsem_index": "output" ], 
        key: "untar_rsem_index",
        args: [output: "RSEM_index"],
        directives: [ label: [ "lowmem", "midcpu" ] ]
      )

      | rsem_prepare_reference.run ( 
        runIf: {id, state -> !state.rsem_index && state.aligner == 'star_rsem'}, 
        fromState: [
            "reference_fasta_files": "fasta", 
            "gtf": "gtf"
        ], 
        toState: [ "rsem_index": "output" ], 
        key: "generate_rsem_index",
        args: [reference_name: "genome"],
        directives: [ label: [ "lowmem", "midcpu" ] ]
      )
      
      // TODO: Uncompress HISAT2 index or generate from scratch if required

      // Uncompress Salmon index or generate from scratch if required
      | untar.run (
        runIf: {id, state -> state.salmon_index}, 
        fromState: [ "input": "salmon_index" ], 
        toState: [ "salmon_index": "output" ], 
        key: "untar_salmon_index",
        args: [output: "Salmon_index"],
        directives: [ label: [ "lowmem", "midcpu" ] ]
      )

      | salmon_index.run (
        runIf: {id, state -> (state.aligner == 'star_salmon' || state.pseudo_aligner == "salmon") && !state.salmon_index}, 
        fromState: [ 
            "genome": "fasta", 
            "transcripts": "transcript_fasta", 
            "kmer_len": "pseudo_aligner_kmer_size",
            "gencode": "gencode"
        ], 
        toState: [ "salmon_index": "index" ], 
        key: "generate_salmon_index",
        args: [index: "Salmon_index"],
        directives: [ label: [ "lowmem", "midcpu" ] ]
      )

      // Uncompress Kallisto index or generate from scratch if required
      | untar.run (
        runIf: {id, state -> state.kallisto_index}, 
        fromState: [ "input": "kallisto_index" ], 
        toState: [ "kallisto_index": "output" ], 
        key: "untar_kallisto_index",
        args: [output: "Kallisto_index"],
        directives: [ label: [ "lowmem", "midcpu" ] ]
      )

      | kallisto_index.run(
        runIf: {id, state -> state.pseudo_aligner == "kallisto" && !state.kallisto_index}, 
        fromState: [
            "input": "transcript_fasta",
            "kmer_size": "pseudo_aligner_kmer_size"
        ],
        toState: [ "kallisto_index": "index" ],
        key: "generate_kallisto_index",
        args: [index: "Kallisto_index"],
        directives: [ label: [ "lowmem", "midcpu" ] ]
      )

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

      | setState ( 
        "fasta_uncompressed": "fasta", 
        "gtf_uncompressed": "gtf", 
        "transcript_fasta_uncompressed": "transcript_fasta", 
        "gene_bed_uncompressed": "gene_bed",
        "star_index_uncompressed": "star_index", 
        "salmon_index_uncompressed": "salmon_index",
        "kallisto_index_uncompressed": "kallisto_index", 
        "bbsplit_index_uncompressed": "bbsplit_index", 
        "rsem_index_uncompressed": "rsem_index",
        "chrom_sizes": "sizes", 
        "fai": "fai"
      )

  emit: 
    output_ch
}
VIASHMAIN
nextflow run . -main-script "\$tempscript" &
wait "\$!"

VIASHEOF


# check whether required files exist
if [ ! -z "$VIASH_PAR_FASTA_UNCOMPRESSED" ] && [ ! -e "$VIASH_PAR_FASTA_UNCOMPRESSED" ]; then
  ViashError "Output file '$VIASH_PAR_FASTA_UNCOMPRESSED' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_GTF_UNCOMPRESSED" ] && [ ! -e "$VIASH_PAR_GTF_UNCOMPRESSED" ]; then
  ViashError "Output file '$VIASH_PAR_GTF_UNCOMPRESSED' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_TRANSCRIPT_FASTA_UNCOMPRESSED" ] && [ ! -e "$VIASH_PAR_TRANSCRIPT_FASTA_UNCOMPRESSED" ]; then
  ViashError "Output file '$VIASH_PAR_TRANSCRIPT_FASTA_UNCOMPRESSED' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_GENE_BED_UNCOMPRESSED" ] && [ ! -e "$VIASH_PAR_GENE_BED_UNCOMPRESSED" ]; then
  ViashError "Output file '$VIASH_PAR_GENE_BED_UNCOMPRESSED' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_STAR_INDEX_UNCOMPRESSED" ] && [ ! -e "$VIASH_PAR_STAR_INDEX_UNCOMPRESSED" ]; then
  ViashError "Output file '$VIASH_PAR_STAR_INDEX_UNCOMPRESSED' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_RSEM_INDEX_UNCOMPRESSED" ] && [ ! -e "$VIASH_PAR_RSEM_INDEX_UNCOMPRESSED" ]; then
  ViashError "Output file '$VIASH_PAR_RSEM_INDEX_UNCOMPRESSED' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_SALMON_INDEX_UNCOMPRESSED" ] && [ ! -e "$VIASH_PAR_SALMON_INDEX_UNCOMPRESSED" ]; then
  ViashError "Output file '$VIASH_PAR_SALMON_INDEX_UNCOMPRESSED' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_KALLISTO_INDEX_UNCOMPRESSED" ] && [ ! -e "$VIASH_PAR_KALLISTO_INDEX_UNCOMPRESSED" ]; then
  ViashError "Output file '$VIASH_PAR_KALLISTO_INDEX_UNCOMPRESSED' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_BBSPLIT_INDEX_UNCOMPRESSED" ] && [ ! -e "$VIASH_PAR_BBSPLIT_INDEX_UNCOMPRESSED" ]; then
  ViashError "Output file '$VIASH_PAR_BBSPLIT_INDEX_UNCOMPRESSED' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_CHROM_SIZES" ] && [ ! -e "$VIASH_PAR_CHROM_SIZES" ]; then
  ViashError "Output file '$VIASH_PAR_CHROM_SIZES' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_FAI" ] && [ ! -e "$VIASH_PAR_FAI" ]; then
  ViashError "Output file '$VIASH_PAR_FAI' does not exist."
  exit 1
fi


exit 0
