#!/usr/bin/env bash

# pre_processing v0.1.0
# 
# 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="pre_processing"
VIASH_META_FUNCTIONALITY_NAME="pre_processing"
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 "pre_processing v0.1.0"
  echo ""
  echo "A subworkflow for the pre-processing stage of the nf-core/rnaseq pipeline."
  echo ""
  echo "Inputs:"
  echo "    --id"
  echo "        type: string, required parameter"
  echo "        example: foo"
  echo "        ID of the sample."
  echo ""
  echo "    --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"
  echo "        Path to read 2 of the sample."
  echo ""
  echo "    --strandedness"
  echo "        type: string"
  echo "        default: auto"
  echo "        Sample strand-specificity. Must be one of unstranded, forward, reverse"
  echo "        or auto"
  echo ""
  echo "    --bbsplit_index"
  echo "        type: file, file must exist"
  echo "        BBsplit index"
  echo ""
  echo "    --bbsplit_fasta_list"
  echo "        type: file, file must exist"
  echo "        Path to comma-separated file containing a list of reference genomes to"
  echo "        filter reads against with BBSplit. To use BBSplit, \"--skip_bbsplit\" must"
  echo "        be explicitly set to \"false\". The file should contain 2 (comma"
  echo "        separated) columns - short name and full path to reference genome(s)"
  echo ""
  echo "    --ribo_database_manifest"
  echo "        type: file, file must exist"
  echo "        Text file containing paths to fasta files (one per line) that will be"
  echo "        used to create the database for SortMeRNA."
  echo ""
  echo "    --transcript_fasta"
  echo "        type: file, file must exist"
  echo "        Path to FASTA transcriptome file."
  echo ""
  echo "    --gtf"
  echo "        type: file, file must exist"
  echo "        Path to GTF annotation file."
  echo ""
  echo "    --salmon_index"
  echo "        type: file, file must exist"
  echo "        Path to directory containing the Salmon index"
  echo ""
  echo "    --num_trimmed_reads"
  echo "        type: integer"
  echo "        Number of reads after trimming"
  echo ""
  echo "Extra pipeline options:"
  echo "    --skip_qc"
  echo "        type: boolean"
  echo "        Skip QC steps of the workflow."
  echo ""
  echo "FastQC options:"
  echo "    --skip_fastqc"
  echo "        type: boolean"
  echo "        default: false"
  echo "        Skip FatQC step."
  echo ""
  echo "UMI-tools options:"
  echo "    --with_umi"
  echo "        type: boolean"
  echo "        default: false"
  echo "        Enable UMI-based read deduplication."
  echo ""
  echo "    --skip_umi_extract"
  echo "        type: boolean"
  echo "        default: false"
  echo "        Skip umi_tools extract step."
  echo ""
  echo "    --umitools_extract_method"
  echo "        type: string"
  echo "        default: string"
  echo "        choices: [ string, regex ]"
  echo "        UMI pattern to use."
  echo ""
  echo "    --umitools_bc_pattern"
  echo "        type: string"
  echo "        default:"
  echo "        The UMI barcode pattern to use e.g. 'NNNNNN' indicates that the first 6"
  echo "        nucleotides of the read are from the UMI."
  echo ""
  echo "    --umitools_bc_pattern2"
  echo "        type: string"
  echo "        default:"
  echo "        The UMI barcode pattern to use if the UMI is located in read 2."
  echo ""
  echo "    --umi_discard_read"
  echo "        type: integer"
  echo "        default: 0"
  echo "        choices: [ 0, 1, 2 ]"
  echo "        After UMI barcode extraction discard either R1 or R2 by setting this"
  echo "        parameter to 1 or 2, respectively."
  echo ""
  echo "    --umitools_umi_separator"
  echo "        type: string"
  echo "        default: _"
  echo "        The character that separates the UMI in the read name. Most likely a"
  echo "        colon if you skipped the extraction with UMI-tools and used other"
  echo "        software."
  echo ""
  echo "    --umitools_grouping_method"
  echo "        type: string"
  echo "        default: directional"
  echo "        choices: [ unique, percentile, cluster, adjacency, directional ]"
  echo "        Method to use to determine read groups by subsuming those with similar"
  echo "        UMIs. All methods start by identifying the reads with the same mapping"
  echo "        position, but treat similar yet nonidentical UMIs differently."
  echo ""
  echo "    --save_umi_intermeds"
  echo "        type: boolean"
  echo "        default: false"
  echo "        If this option is specified, intermediate FastQ and BAM files produced"
  echo "        by UMI-tools are also saved in the results directory."
  echo ""
  echo "Read trimming options:"
  echo "    --trimmer"
  echo "        type: string"
  echo "        default: trimgalore"
  echo "        choices: [ trimgalore, fastp ]"
  echo "        Specify the trimming tool to use."
  echo ""
  echo "    --extra_trimgalore_args"
  echo "        type: string"
  echo "        Extra arguments to pass to Trim Galore! command in addition to defaults"
  echo "        defined by the pipeline."
  echo ""
  echo "    --min_trimmed_reads"
  echo "        type: integer"
  echo "        default: 10000"
  echo "        Minimum number of trimmed reads below which samples are removed from"
  echo "        further processing. Some downstream steps in the pipeline will fail if"
  echo "        this threshold is too low."
  echo ""
  echo "    --skip_trimming"
  echo "        type: boolean"
  echo "        default: false"
  echo "        Skip the adapter trimming step."
  echo ""
  echo "    --save_trimmed"
  echo "        type: boolean"
  echo "        default: false"
  echo "        Save the trimmed FastQ files in the results directory."
  echo ""
  echo "Alignment options:"
  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 "Read filtering options:"
  echo "    --skip_bbsplit"
  echo "        type: boolean_true"
  echo "        Skip BBSplit for removal of non-reference genome reads."
  echo ""
  echo "    --remove_ribo_rna"
  echo "        type: boolean_true"
  echo "        Enable the removal of reads derived from ribosomal RNA using SortMeRNA."
  echo ""
  echo "Other options:"
  echo "    --extra_fq_subsample_args"
  echo "        type: string"
  echo "        default: --record-count 1000000 --seed 1"
  echo "        Extra arguments to pass to fq subsample command in addition to defaults"
  echo "        defined by the pipeline."
  echo ""
  echo "Output:"
  echo "    --qc_output1"
  echo "        type: file, output"
  echo "        default: \$id.read_1.fastq"
  echo "        Path to output directory"
  echo ""
  echo "    --qc_output2"
  echo "        type: file, output"
  echo "        default: \$id.read_2.fastq"
  echo "        Path to output directory"
  echo ""
  echo "    --fastqc_html_1"
  echo "        type: file, output"
  echo "        default: \$id.read_1.fastqc.html"
  echo "        FastQC HTML report for read 1."
  echo ""
  echo "    --fastqc_html_2"
  echo "        type: file, output"
  echo "        default: \$id.read_2.fastqc.html"
  echo "        FastQC HTML report for read 2."
  echo ""
  echo "    --fastqc_zip_1"
  echo "        type: file, output"
  echo "        default: \$id.read_1.fastqc.zip"
  echo "        FastQC report archive for read 1."
  echo ""
  echo "    --fastqc_zip_2"
  echo "        type: file, output"
  echo "        default: \$id.read_2.fastqc.zip"
  echo "        FastQC report archive for read 2."
  echo ""
  echo "    --trim_log_1"
  echo "        type: file, output"
  echo "        default: \$id.read_1.trimming_report.txt"
  echo ""
  echo "    --trim_log_2"
  echo "        type: file, output"
  echo "        default: \$id.read_2.trimming_report.txt"
  echo ""
  echo "    --trim_html_1"
  echo "        type: file, output"
  echo "        default: \$id.read_1.trimmed_fastqc.html"
  echo ""
  echo "    --trim_html_2"
  echo "        type: file, output"
  echo "        default: \$id.read_2.trimmed_fastqc.html"
  echo ""
  echo "    --trim_zip_1"
  echo "        type: file, output"
  echo "        default: \$id.read_1.trimmed_fastqc.zip"
  echo ""
  echo "    --trim_zip_2"
  echo "        type: file, output"
  echo "        default: \$id.read_2.trimmed_fastqc.zip"
  echo ""
  echo "    --sortmerna_log"
  echo "        type: file, output"
  echo "        default: \$id.sortmerna.log"
  echo "        Sortmerna log file."
  echo ""
  echo "    --salmon_quant_output"
  echo "        type: file, output, file must exist"
  echo "        default: \$id.salmon_quant_output"
  echo "        Results from Salmon quant"
  echo ""
  echo "    --trim_json"
  echo "        type: file, output, file must exist"
  echo "        default: \$id.fastp_out.json"
  echo "        The fastp json format report file name"
  echo ""
  echo "    --trim_html"
  echo "        type: file, output, file must exist"
  echo "        default: \$id.fastp_out.html"
  echo "        The fastp html format report file name"
  echo ""
  echo "    --merged_out"
  echo "        type: file, output, file must exist"
  echo "        File name to store merged fastp output."
}

# 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 "pre_processing v0.1.0"
            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
            ;;
        --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
            ;;
        --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
            ;;
        --bbsplit_fasta_list)
            [ -n "$VIASH_PAR_BBSPLIT_FASTA_LIST" ] && ViashError Bad arguments for option \'--bbsplit_fasta_list\': \'$VIASH_PAR_BBSPLIT_FASTA_LIST\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_BBSPLIT_FASTA_LIST="$2"
            [ $# -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=*)
            [ -n "$VIASH_PAR_BBSPLIT_FASTA_LIST" ] && ViashError Bad arguments for option \'--bbsplit_fasta_list=*\': \'$VIASH_PAR_BBSPLIT_FASTA_LIST\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_BBSPLIT_FASTA_LIST=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --ribo_database_manifest)
            [ -n "$VIASH_PAR_RIBO_DATABASE_MANIFEST" ] && ViashError Bad arguments for option \'--ribo_database_manifest\': \'$VIASH_PAR_RIBO_DATABASE_MANIFEST\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_RIBO_DATABASE_MANIFEST="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --ribo_database_manifest. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --ribo_database_manifest=*)
            [ -n "$VIASH_PAR_RIBO_DATABASE_MANIFEST" ] && ViashError Bad arguments for option \'--ribo_database_manifest=*\': \'$VIASH_PAR_RIBO_DATABASE_MANIFEST\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_RIBO_DATABASE_MANIFEST=$(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
            ;;
        --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
            ;;
        --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
            ;;
        --num_trimmed_reads)
            [ -n "$VIASH_PAR_NUM_TRIMMED_READS" ] && ViashError Bad arguments for option \'--num_trimmed_reads\': \'$VIASH_PAR_NUM_TRIMMED_READS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_NUM_TRIMMED_READS="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --num_trimmed_reads. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --num_trimmed_reads=*)
            [ -n "$VIASH_PAR_NUM_TRIMMED_READS" ] && ViashError Bad arguments for option \'--num_trimmed_reads=*\': \'$VIASH_PAR_NUM_TRIMMED_READS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_NUM_TRIMMED_READS=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --skip_qc)
            [ -n "$VIASH_PAR_SKIP_QC" ] && ViashError Bad arguments for option \'--skip_qc\': \'$VIASH_PAR_SKIP_QC\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_SKIP_QC="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --skip_qc. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --skip_qc=*)
            [ -n "$VIASH_PAR_SKIP_QC" ] && ViashError Bad arguments for option \'--skip_qc=*\': \'$VIASH_PAR_SKIP_QC\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_SKIP_QC=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --skip_fastqc)
            [ -n "$VIASH_PAR_SKIP_FASTQC" ] && ViashError Bad arguments for option \'--skip_fastqc\': \'$VIASH_PAR_SKIP_FASTQC\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_SKIP_FASTQC="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --skip_fastqc. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --skip_fastqc=*)
            [ -n "$VIASH_PAR_SKIP_FASTQC" ] && ViashError Bad arguments for option \'--skip_fastqc=*\': \'$VIASH_PAR_SKIP_FASTQC\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_SKIP_FASTQC=$(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
            ;;
        --skip_umi_extract)
            [ -n "$VIASH_PAR_SKIP_UMI_EXTRACT" ] && ViashError Bad arguments for option \'--skip_umi_extract\': \'$VIASH_PAR_SKIP_UMI_EXTRACT\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_SKIP_UMI_EXTRACT="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --skip_umi_extract. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --skip_umi_extract=*)
            [ -n "$VIASH_PAR_SKIP_UMI_EXTRACT" ] && ViashError Bad arguments for option \'--skip_umi_extract=*\': \'$VIASH_PAR_SKIP_UMI_EXTRACT\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_SKIP_UMI_EXTRACT=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --umitools_extract_method)
            [ -n "$VIASH_PAR_UMITOOLS_EXTRACT_METHOD" ] && ViashError Bad arguments for option \'--umitools_extract_method\': \'$VIASH_PAR_UMITOOLS_EXTRACT_METHOD\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_UMITOOLS_EXTRACT_METHOD="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --umitools_extract_method. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --umitools_extract_method=*)
            [ -n "$VIASH_PAR_UMITOOLS_EXTRACT_METHOD" ] && ViashError Bad arguments for option \'--umitools_extract_method=*\': \'$VIASH_PAR_UMITOOLS_EXTRACT_METHOD\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_UMITOOLS_EXTRACT_METHOD=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --umitools_bc_pattern)
            [ -n "$VIASH_PAR_UMITOOLS_BC_PATTERN" ] && ViashError Bad arguments for option \'--umitools_bc_pattern\': \'$VIASH_PAR_UMITOOLS_BC_PATTERN\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_UMITOOLS_BC_PATTERN="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --umitools_bc_pattern. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --umitools_bc_pattern=*)
            [ -n "$VIASH_PAR_UMITOOLS_BC_PATTERN" ] && ViashError Bad arguments for option \'--umitools_bc_pattern=*\': \'$VIASH_PAR_UMITOOLS_BC_PATTERN\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_UMITOOLS_BC_PATTERN=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --umitools_bc_pattern2)
            [ -n "$VIASH_PAR_UMITOOLS_BC_PATTERN2" ] && ViashError Bad arguments for option \'--umitools_bc_pattern2\': \'$VIASH_PAR_UMITOOLS_BC_PATTERN2\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_UMITOOLS_BC_PATTERN2="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --umitools_bc_pattern2. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --umitools_bc_pattern2=*)
            [ -n "$VIASH_PAR_UMITOOLS_BC_PATTERN2" ] && ViashError Bad arguments for option \'--umitools_bc_pattern2=*\': \'$VIASH_PAR_UMITOOLS_BC_PATTERN2\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_UMITOOLS_BC_PATTERN2=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --umi_discard_read)
            [ -n "$VIASH_PAR_UMI_DISCARD_READ" ] && ViashError Bad arguments for option \'--umi_discard_read\': \'$VIASH_PAR_UMI_DISCARD_READ\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_UMI_DISCARD_READ="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --umi_discard_read. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --umi_discard_read=*)
            [ -n "$VIASH_PAR_UMI_DISCARD_READ" ] && ViashError Bad arguments for option \'--umi_discard_read=*\': \'$VIASH_PAR_UMI_DISCARD_READ\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_UMI_DISCARD_READ=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --umitools_umi_separator)
            [ -n "$VIASH_PAR_UMITOOLS_UMI_SEPARATOR" ] && ViashError Bad arguments for option \'--umitools_umi_separator\': \'$VIASH_PAR_UMITOOLS_UMI_SEPARATOR\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_UMITOOLS_UMI_SEPARATOR="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --umitools_umi_separator. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --umitools_umi_separator=*)
            [ -n "$VIASH_PAR_UMITOOLS_UMI_SEPARATOR" ] && ViashError Bad arguments for option \'--umitools_umi_separator=*\': \'$VIASH_PAR_UMITOOLS_UMI_SEPARATOR\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_UMITOOLS_UMI_SEPARATOR=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --umitools_grouping_method)
            [ -n "$VIASH_PAR_UMITOOLS_GROUPING_METHOD" ] && ViashError Bad arguments for option \'--umitools_grouping_method\': \'$VIASH_PAR_UMITOOLS_GROUPING_METHOD\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_UMITOOLS_GROUPING_METHOD="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --umitools_grouping_method. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --umitools_grouping_method=*)
            [ -n "$VIASH_PAR_UMITOOLS_GROUPING_METHOD" ] && ViashError Bad arguments for option \'--umitools_grouping_method=*\': \'$VIASH_PAR_UMITOOLS_GROUPING_METHOD\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_UMITOOLS_GROUPING_METHOD=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --save_umi_intermeds)
            [ -n "$VIASH_PAR_SAVE_UMI_INTERMEDS" ] && ViashError Bad arguments for option \'--save_umi_intermeds\': \'$VIASH_PAR_SAVE_UMI_INTERMEDS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_SAVE_UMI_INTERMEDS="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --save_umi_intermeds. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --save_umi_intermeds=*)
            [ -n "$VIASH_PAR_SAVE_UMI_INTERMEDS" ] && ViashError Bad arguments for option \'--save_umi_intermeds=*\': \'$VIASH_PAR_SAVE_UMI_INTERMEDS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_SAVE_UMI_INTERMEDS=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --trimmer)
            [ -n "$VIASH_PAR_TRIMMER" ] && ViashError Bad arguments for option \'--trimmer\': \'$VIASH_PAR_TRIMMER\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRIMMER="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --trimmer. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --trimmer=*)
            [ -n "$VIASH_PAR_TRIMMER" ] && ViashError Bad arguments for option \'--trimmer=*\': \'$VIASH_PAR_TRIMMER\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRIMMER=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --extra_trimgalore_args)
            [ -n "$VIASH_PAR_EXTRA_TRIMGALORE_ARGS" ] && ViashError Bad arguments for option \'--extra_trimgalore_args\': \'$VIASH_PAR_EXTRA_TRIMGALORE_ARGS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_EXTRA_TRIMGALORE_ARGS="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --extra_trimgalore_args. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --extra_trimgalore_args=*)
            [ -n "$VIASH_PAR_EXTRA_TRIMGALORE_ARGS" ] && ViashError Bad arguments for option \'--extra_trimgalore_args=*\': \'$VIASH_PAR_EXTRA_TRIMGALORE_ARGS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_EXTRA_TRIMGALORE_ARGS=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --min_trimmed_reads)
            [ -n "$VIASH_PAR_MIN_TRIMMED_READS" ] && ViashError Bad arguments for option \'--min_trimmed_reads\': \'$VIASH_PAR_MIN_TRIMMED_READS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_MIN_TRIMMED_READS="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --min_trimmed_reads. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --min_trimmed_reads=*)
            [ -n "$VIASH_PAR_MIN_TRIMMED_READS" ] && ViashError Bad arguments for option \'--min_trimmed_reads=*\': \'$VIASH_PAR_MIN_TRIMMED_READS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_MIN_TRIMMED_READS=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --skip_trimming)
            [ -n "$VIASH_PAR_SKIP_TRIMMING" ] && ViashError Bad arguments for option \'--skip_trimming\': \'$VIASH_PAR_SKIP_TRIMMING\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_SKIP_TRIMMING="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --skip_trimming. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --skip_trimming=*)
            [ -n "$VIASH_PAR_SKIP_TRIMMING" ] && ViashError Bad arguments for option \'--skip_trimming=*\': \'$VIASH_PAR_SKIP_TRIMMING\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_SKIP_TRIMMING=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --save_trimmed)
            [ -n "$VIASH_PAR_SAVE_TRIMMED" ] && ViashError Bad arguments for option \'--save_trimmed\': \'$VIASH_PAR_SAVE_TRIMMED\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_SAVE_TRIMMED="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --save_trimmed. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --save_trimmed=*)
            [ -n "$VIASH_PAR_SAVE_TRIMMED" ] && ViashError Bad arguments for option \'--save_trimmed=*\': \'$VIASH_PAR_SAVE_TRIMMED\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_SAVE_TRIMMED=$(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
            ;;
        --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=true
            shift 1
            ;;
        --remove_ribo_rna)
            [ -n "$VIASH_PAR_REMOVE_RIBO_RNA" ] && ViashError Bad arguments for option \'--remove_ribo_rna\': \'$VIASH_PAR_REMOVE_RIBO_RNA\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_REMOVE_RIBO_RNA=true
            shift 1
            ;;
        --extra_fq_subsample_args)
            [ -n "$VIASH_PAR_EXTRA_FQ_SUBSAMPLE_ARGS" ] && ViashError Bad arguments for option \'--extra_fq_subsample_args\': \'$VIASH_PAR_EXTRA_FQ_SUBSAMPLE_ARGS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_EXTRA_FQ_SUBSAMPLE_ARGS="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --extra_fq_subsample_args. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --extra_fq_subsample_args=*)
            [ -n "$VIASH_PAR_EXTRA_FQ_SUBSAMPLE_ARGS" ] && ViashError Bad arguments for option \'--extra_fq_subsample_args=*\': \'$VIASH_PAR_EXTRA_FQ_SUBSAMPLE_ARGS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_EXTRA_FQ_SUBSAMPLE_ARGS=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --qc_output1)
            [ -n "$VIASH_PAR_QC_OUTPUT1" ] && ViashError Bad arguments for option \'--qc_output1\': \'$VIASH_PAR_QC_OUTPUT1\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_QC_OUTPUT1="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --qc_output1. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --qc_output1=*)
            [ -n "$VIASH_PAR_QC_OUTPUT1" ] && ViashError Bad arguments for option \'--qc_output1=*\': \'$VIASH_PAR_QC_OUTPUT1\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_QC_OUTPUT1=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --qc_output2)
            [ -n "$VIASH_PAR_QC_OUTPUT2" ] && ViashError Bad arguments for option \'--qc_output2\': \'$VIASH_PAR_QC_OUTPUT2\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_QC_OUTPUT2="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --qc_output2. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --qc_output2=*)
            [ -n "$VIASH_PAR_QC_OUTPUT2" ] && ViashError Bad arguments for option \'--qc_output2=*\': \'$VIASH_PAR_QC_OUTPUT2\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_QC_OUTPUT2=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --fastqc_html_1)
            [ -n "$VIASH_PAR_FASTQC_HTML_1" ] && ViashError Bad arguments for option \'--fastqc_html_1\': \'$VIASH_PAR_FASTQC_HTML_1\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_FASTQC_HTML_1="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --fastqc_html_1. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --fastqc_html_1=*)
            [ -n "$VIASH_PAR_FASTQC_HTML_1" ] && ViashError Bad arguments for option \'--fastqc_html_1=*\': \'$VIASH_PAR_FASTQC_HTML_1\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_FASTQC_HTML_1=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --fastqc_html_2)
            [ -n "$VIASH_PAR_FASTQC_HTML_2" ] && ViashError Bad arguments for option \'--fastqc_html_2\': \'$VIASH_PAR_FASTQC_HTML_2\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_FASTQC_HTML_2="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --fastqc_html_2. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --fastqc_html_2=*)
            [ -n "$VIASH_PAR_FASTQC_HTML_2" ] && ViashError Bad arguments for option \'--fastqc_html_2=*\': \'$VIASH_PAR_FASTQC_HTML_2\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_FASTQC_HTML_2=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --fastqc_zip_1)
            [ -n "$VIASH_PAR_FASTQC_ZIP_1" ] && ViashError Bad arguments for option \'--fastqc_zip_1\': \'$VIASH_PAR_FASTQC_ZIP_1\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_FASTQC_ZIP_1="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --fastqc_zip_1. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --fastqc_zip_1=*)
            [ -n "$VIASH_PAR_FASTQC_ZIP_1" ] && ViashError Bad arguments for option \'--fastqc_zip_1=*\': \'$VIASH_PAR_FASTQC_ZIP_1\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_FASTQC_ZIP_1=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --fastqc_zip_2)
            [ -n "$VIASH_PAR_FASTQC_ZIP_2" ] && ViashError Bad arguments for option \'--fastqc_zip_2\': \'$VIASH_PAR_FASTQC_ZIP_2\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_FASTQC_ZIP_2="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --fastqc_zip_2. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --fastqc_zip_2=*)
            [ -n "$VIASH_PAR_FASTQC_ZIP_2" ] && ViashError Bad arguments for option \'--fastqc_zip_2=*\': \'$VIASH_PAR_FASTQC_ZIP_2\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_FASTQC_ZIP_2=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --trim_log_1)
            [ -n "$VIASH_PAR_TRIM_LOG_1" ] && ViashError Bad arguments for option \'--trim_log_1\': \'$VIASH_PAR_TRIM_LOG_1\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRIM_LOG_1="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --trim_log_1. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --trim_log_1=*)
            [ -n "$VIASH_PAR_TRIM_LOG_1" ] && ViashError Bad arguments for option \'--trim_log_1=*\': \'$VIASH_PAR_TRIM_LOG_1\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRIM_LOG_1=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --trim_log_2)
            [ -n "$VIASH_PAR_TRIM_LOG_2" ] && ViashError Bad arguments for option \'--trim_log_2\': \'$VIASH_PAR_TRIM_LOG_2\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRIM_LOG_2="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --trim_log_2. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --trim_log_2=*)
            [ -n "$VIASH_PAR_TRIM_LOG_2" ] && ViashError Bad arguments for option \'--trim_log_2=*\': \'$VIASH_PAR_TRIM_LOG_2\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRIM_LOG_2=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --trim_html_1)
            [ -n "$VIASH_PAR_TRIM_HTML_1" ] && ViashError Bad arguments for option \'--trim_html_1\': \'$VIASH_PAR_TRIM_HTML_1\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRIM_HTML_1="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --trim_html_1. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --trim_html_1=*)
            [ -n "$VIASH_PAR_TRIM_HTML_1" ] && ViashError Bad arguments for option \'--trim_html_1=*\': \'$VIASH_PAR_TRIM_HTML_1\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRIM_HTML_1=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --trim_html_2)
            [ -n "$VIASH_PAR_TRIM_HTML_2" ] && ViashError Bad arguments for option \'--trim_html_2\': \'$VIASH_PAR_TRIM_HTML_2\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRIM_HTML_2="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --trim_html_2. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --trim_html_2=*)
            [ -n "$VIASH_PAR_TRIM_HTML_2" ] && ViashError Bad arguments for option \'--trim_html_2=*\': \'$VIASH_PAR_TRIM_HTML_2\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRIM_HTML_2=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --trim_zip_1)
            [ -n "$VIASH_PAR_TRIM_ZIP_1" ] && ViashError Bad arguments for option \'--trim_zip_1\': \'$VIASH_PAR_TRIM_ZIP_1\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRIM_ZIP_1="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --trim_zip_1. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --trim_zip_1=*)
            [ -n "$VIASH_PAR_TRIM_ZIP_1" ] && ViashError Bad arguments for option \'--trim_zip_1=*\': \'$VIASH_PAR_TRIM_ZIP_1\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRIM_ZIP_1=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --trim_zip_2)
            [ -n "$VIASH_PAR_TRIM_ZIP_2" ] && ViashError Bad arguments for option \'--trim_zip_2\': \'$VIASH_PAR_TRIM_ZIP_2\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRIM_ZIP_2="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --trim_zip_2. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --trim_zip_2=*)
            [ -n "$VIASH_PAR_TRIM_ZIP_2" ] && ViashError Bad arguments for option \'--trim_zip_2=*\': \'$VIASH_PAR_TRIM_ZIP_2\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRIM_ZIP_2=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --sortmerna_log)
            [ -n "$VIASH_PAR_SORTMERNA_LOG" ] && ViashError Bad arguments for option \'--sortmerna_log\': \'$VIASH_PAR_SORTMERNA_LOG\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_SORTMERNA_LOG="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --sortmerna_log. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --sortmerna_log=*)
            [ -n "$VIASH_PAR_SORTMERNA_LOG" ] && ViashError Bad arguments for option \'--sortmerna_log=*\': \'$VIASH_PAR_SORTMERNA_LOG\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_SORTMERNA_LOG=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --salmon_quant_output)
            [ -n "$VIASH_PAR_SALMON_QUANT_OUTPUT" ] && ViashError Bad arguments for option \'--salmon_quant_output\': \'$VIASH_PAR_SALMON_QUANT_OUTPUT\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_SALMON_QUANT_OUTPUT="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --salmon_quant_output. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --salmon_quant_output=*)
            [ -n "$VIASH_PAR_SALMON_QUANT_OUTPUT" ] && ViashError Bad arguments for option \'--salmon_quant_output=*\': \'$VIASH_PAR_SALMON_QUANT_OUTPUT\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_SALMON_QUANT_OUTPUT=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --trim_json)
            [ -n "$VIASH_PAR_TRIM_JSON" ] && ViashError Bad arguments for option \'--trim_json\': \'$VIASH_PAR_TRIM_JSON\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRIM_JSON="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --trim_json. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --trim_json=*)
            [ -n "$VIASH_PAR_TRIM_JSON" ] && ViashError Bad arguments for option \'--trim_json=*\': \'$VIASH_PAR_TRIM_JSON\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRIM_JSON=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --trim_html)
            [ -n "$VIASH_PAR_TRIM_HTML" ] && ViashError Bad arguments for option \'--trim_html\': \'$VIASH_PAR_TRIM_HTML\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRIM_HTML="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --trim_html. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --trim_html=*)
            [ -n "$VIASH_PAR_TRIM_HTML" ] && ViashError Bad arguments for option \'--trim_html=*\': \'$VIASH_PAR_TRIM_HTML\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRIM_HTML=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --merged_out)
            [ -n "$VIASH_PAR_MERGED_OUT" ] && ViashError Bad arguments for option \'--merged_out\': \'$VIASH_PAR_MERGED_OUT\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_MERGED_OUT="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --merged_out. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --merged_out=*)
            [ -n "$VIASH_PAR_MERGED_OUT" ] && ViashError Bad arguments for option \'--merged_out=*\': \'$VIASH_PAR_MERGED_OUT\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_MERGED_OUT=$(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_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_STRANDEDNESS+x} ]; then
  VIASH_PAR_STRANDEDNESS="auto"
fi
if [ -z ${VIASH_PAR_SKIP_FASTQC+x} ]; then
  VIASH_PAR_SKIP_FASTQC="false"
fi
if [ -z ${VIASH_PAR_WITH_UMI+x} ]; then
  VIASH_PAR_WITH_UMI="false"
fi
if [ -z ${VIASH_PAR_SKIP_UMI_EXTRACT+x} ]; then
  VIASH_PAR_SKIP_UMI_EXTRACT="false"
fi
if [ -z ${VIASH_PAR_UMITOOLS_EXTRACT_METHOD+x} ]; then
  VIASH_PAR_UMITOOLS_EXTRACT_METHOD="string"
fi
if [ -z ${VIASH_PAR_UMITOOLS_BC_PATTERN+x} ]; then
  VIASH_PAR_UMITOOLS_BC_PATTERN=""
fi
if [ -z ${VIASH_PAR_UMITOOLS_BC_PATTERN2+x} ]; then
  VIASH_PAR_UMITOOLS_BC_PATTERN2=""
fi
if [ -z ${VIASH_PAR_UMI_DISCARD_READ+x} ]; then
  VIASH_PAR_UMI_DISCARD_READ="0"
fi
if [ -z ${VIASH_PAR_UMITOOLS_UMI_SEPARATOR+x} ]; then
  VIASH_PAR_UMITOOLS_UMI_SEPARATOR="_"
fi
if [ -z ${VIASH_PAR_UMITOOLS_GROUPING_METHOD+x} ]; then
  VIASH_PAR_UMITOOLS_GROUPING_METHOD="directional"
fi
if [ -z ${VIASH_PAR_SAVE_UMI_INTERMEDS+x} ]; then
  VIASH_PAR_SAVE_UMI_INTERMEDS="false"
fi
if [ -z ${VIASH_PAR_TRIMMER+x} ]; then
  VIASH_PAR_TRIMMER="trimgalore"
fi
if [ -z ${VIASH_PAR_MIN_TRIMMED_READS+x} ]; then
  VIASH_PAR_MIN_TRIMMED_READS="10000"
fi
if [ -z ${VIASH_PAR_SKIP_TRIMMING+x} ]; then
  VIASH_PAR_SKIP_TRIMMING="false"
fi
if [ -z ${VIASH_PAR_SAVE_TRIMMED+x} ]; then
  VIASH_PAR_SAVE_TRIMMED="false"
fi
if [ -z ${VIASH_PAR_EXTRA_SALMON_QUANT_ARGS+x} ]; then
  VIASH_PAR_EXTRA_SALMON_QUANT_ARGS=""
fi
if [ -z ${VIASH_PAR_SKIP_BBSPLIT+x} ]; then
  VIASH_PAR_SKIP_BBSPLIT="false"
fi
if [ -z ${VIASH_PAR_REMOVE_RIBO_RNA+x} ]; then
  VIASH_PAR_REMOVE_RIBO_RNA="false"
fi
if [ -z ${VIASH_PAR_EXTRA_FQ_SUBSAMPLE_ARGS+x} ]; then
  VIASH_PAR_EXTRA_FQ_SUBSAMPLE_ARGS="--record-count 1000000 --seed 1"
fi
if [ -z ${VIASH_PAR_QC_OUTPUT1+x} ]; then
  VIASH_PAR_QC_OUTPUT1="\$id.read_1.fastq"
fi
if [ -z ${VIASH_PAR_QC_OUTPUT2+x} ]; then
  VIASH_PAR_QC_OUTPUT2="\$id.read_2.fastq"
fi
if [ -z ${VIASH_PAR_FASTQC_HTML_1+x} ]; then
  VIASH_PAR_FASTQC_HTML_1="\$id.read_1.fastqc.html"
fi
if [ -z ${VIASH_PAR_FASTQC_HTML_2+x} ]; then
  VIASH_PAR_FASTQC_HTML_2="\$id.read_2.fastqc.html"
fi
if [ -z ${VIASH_PAR_FASTQC_ZIP_1+x} ]; then
  VIASH_PAR_FASTQC_ZIP_1="\$id.read_1.fastqc.zip"
fi
if [ -z ${VIASH_PAR_FASTQC_ZIP_2+x} ]; then
  VIASH_PAR_FASTQC_ZIP_2="\$id.read_2.fastqc.zip"
fi
if [ -z ${VIASH_PAR_TRIM_LOG_1+x} ]; then
  VIASH_PAR_TRIM_LOG_1="\$id.read_1.trimming_report.txt"
fi
if [ -z ${VIASH_PAR_TRIM_LOG_2+x} ]; then
  VIASH_PAR_TRIM_LOG_2="\$id.read_2.trimming_report.txt"
fi
if [ -z ${VIASH_PAR_TRIM_HTML_1+x} ]; then
  VIASH_PAR_TRIM_HTML_1="\$id.read_1.trimmed_fastqc.html"
fi
if [ -z ${VIASH_PAR_TRIM_HTML_2+x} ]; then
  VIASH_PAR_TRIM_HTML_2="\$id.read_2.trimmed_fastqc.html"
fi
if [ -z ${VIASH_PAR_TRIM_ZIP_1+x} ]; then
  VIASH_PAR_TRIM_ZIP_1="\$id.read_1.trimmed_fastqc.zip"
fi
if [ -z ${VIASH_PAR_TRIM_ZIP_2+x} ]; then
  VIASH_PAR_TRIM_ZIP_2="\$id.read_2.trimmed_fastqc.zip"
fi
if [ -z ${VIASH_PAR_SORTMERNA_LOG+x} ]; then
  VIASH_PAR_SORTMERNA_LOG="\$id.sortmerna.log"
fi
if [ -z ${VIASH_PAR_SALMON_QUANT_OUTPUT+x} ]; then
  VIASH_PAR_SALMON_QUANT_OUTPUT="\$id.salmon_quant_output"
fi
if [ -z ${VIASH_PAR_TRIM_JSON+x} ]; then
  VIASH_PAR_TRIM_JSON="\$id.fastp_out.json"
fi
if [ -z ${VIASH_PAR_TRIM_HTML+x} ]; then
  VIASH_PAR_TRIM_HTML="\$id.fastp_out.html"
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_BBSPLIT_INDEX" ] && [ ! -e "$VIASH_PAR_BBSPLIT_INDEX" ]; then
  ViashError "Input file '$VIASH_PAR_BBSPLIT_INDEX' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_BBSPLIT_FASTA_LIST" ] && [ ! -e "$VIASH_PAR_BBSPLIT_FASTA_LIST" ]; then
  ViashError "Input file '$VIASH_PAR_BBSPLIT_FASTA_LIST' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_RIBO_DATABASE_MANIFEST" ] && [ ! -e "$VIASH_PAR_RIBO_DATABASE_MANIFEST" ]; then
  ViashError "Input file '$VIASH_PAR_RIBO_DATABASE_MANIFEST' 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_GTF" ] && [ ! -e "$VIASH_PAR_GTF" ]; then
  ViashError "Input file '$VIASH_PAR_GTF' 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_NUM_TRIMMED_READS" ]]; then
  if ! [[ "$VIASH_PAR_NUM_TRIMMED_READS" =~ ^[-+]?[0-9]+$ ]]; then
    ViashError '--num_trimmed_reads' has to be an integer. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_SKIP_QC" ]]; then
  if ! [[ "$VIASH_PAR_SKIP_QC" =~ ^(true|True|TRUE|false|False|FALSE|yes|Yes|YES|no|No|NO)$ ]]; then
    ViashError '--skip_qc' has to be a boolean. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_SKIP_FASTQC" ]]; then
  if ! [[ "$VIASH_PAR_SKIP_FASTQC" =~ ^(true|True|TRUE|false|False|FALSE|yes|Yes|YES|no|No|NO)$ ]]; then
    ViashError '--skip_fastqc' 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_PAR_SKIP_UMI_EXTRACT" ]]; then
  if ! [[ "$VIASH_PAR_SKIP_UMI_EXTRACT" =~ ^(true|True|TRUE|false|False|FALSE|yes|Yes|YES|no|No|NO)$ ]]; then
    ViashError '--skip_umi_extract' has to be a boolean. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_UMI_DISCARD_READ" ]]; then
  if ! [[ "$VIASH_PAR_UMI_DISCARD_READ" =~ ^[-+]?[0-9]+$ ]]; then
    ViashError '--umi_discard_read' has to be an integer. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_SAVE_UMI_INTERMEDS" ]]; then
  if ! [[ "$VIASH_PAR_SAVE_UMI_INTERMEDS" =~ ^(true|True|TRUE|false|False|FALSE|yes|Yes|YES|no|No|NO)$ ]]; then
    ViashError '--save_umi_intermeds' has to be a boolean. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_MIN_TRIMMED_READS" ]]; then
  if ! [[ "$VIASH_PAR_MIN_TRIMMED_READS" =~ ^[-+]?[0-9]+$ ]]; then
    ViashError '--min_trimmed_reads' has to be an integer. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_SKIP_TRIMMING" ]]; then
  if ! [[ "$VIASH_PAR_SKIP_TRIMMING" =~ ^(true|True|TRUE|false|False|FALSE|yes|Yes|YES|no|No|NO)$ ]]; then
    ViashError '--skip_trimming' has to be a boolean. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_SAVE_TRIMMED" ]]; then
  if ! [[ "$VIASH_PAR_SAVE_TRIMMED" =~ ^(true|True|TRUE|false|False|FALSE|yes|Yes|YES|no|No|NO)$ ]]; then
    ViashError '--save_trimmed' has to be a boolean. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
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_true. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_REMOVE_RIBO_RNA" ]]; then
  if ! [[ "$VIASH_PAR_REMOVE_RIBO_RNA" =~ ^(true|True|TRUE|false|False|FALSE|yes|Yes|YES|no|No|NO)$ ]]; then
    ViashError '--remove_ribo_rna' 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_UMITOOLS_EXTRACT_METHOD" ]; then
  VIASH_PAR_UMITOOLS_EXTRACT_METHOD_CHOICES=("string;regex")
  IFS=';'
  set -f
  if ! [[ ";${VIASH_PAR_UMITOOLS_EXTRACT_METHOD_CHOICES[*]};" =~ ";$VIASH_PAR_UMITOOLS_EXTRACT_METHOD;" ]]; then
    ViashError '--umitools_extract_method' specified value of \'$VIASH_PAR_UMITOOLS_EXTRACT_METHOD\' 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_UMI_DISCARD_READ" ]; then
  VIASH_PAR_UMI_DISCARD_READ_CHOICES=("0;1;2")
  IFS=';'
  set -f
  if ! [[ ";${VIASH_PAR_UMI_DISCARD_READ_CHOICES[*]};" =~ ";$VIASH_PAR_UMI_DISCARD_READ;" ]]; then
    ViashError '--umi_discard_read' specified value of \'$VIASH_PAR_UMI_DISCARD_READ\' 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_UMITOOLS_GROUPING_METHOD" ]; then
  VIASH_PAR_UMITOOLS_GROUPING_METHOD_CHOICES=("unique;percentile;cluster;adjacency;directional")
  IFS=';'
  set -f
  if ! [[ ";${VIASH_PAR_UMITOOLS_GROUPING_METHOD_CHOICES[*]};" =~ ";$VIASH_PAR_UMITOOLS_GROUPING_METHOD;" ]]; then
    ViashError '--umitools_grouping_method' specified value of \'$VIASH_PAR_UMITOOLS_GROUPING_METHOD\' 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_TRIMMER" ]; then
  VIASH_PAR_TRIMMER_CHOICES=("trimgalore;fastp")
  IFS=';'
  set -f
  if ! [[ ";${VIASH_PAR_TRIMMER_CHOICES[*]};" =~ ";$VIASH_PAR_TRIMMER;" ]]; then
    ViashError '--trimmer' specified value of \'$VIASH_PAR_TRIMMER\' 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_QC_OUTPUT1" ] && [ ! -d "$(dirname "$VIASH_PAR_QC_OUTPUT1")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_QC_OUTPUT1")"
fi
if [ ! -z "$VIASH_PAR_QC_OUTPUT2" ] && [ ! -d "$(dirname "$VIASH_PAR_QC_OUTPUT2")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_QC_OUTPUT2")"
fi
if [ ! -z "$VIASH_PAR_FASTQC_HTML_1" ] && [ ! -d "$(dirname "$VIASH_PAR_FASTQC_HTML_1")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_FASTQC_HTML_1")"
fi
if [ ! -z "$VIASH_PAR_FASTQC_HTML_2" ] && [ ! -d "$(dirname "$VIASH_PAR_FASTQC_HTML_2")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_FASTQC_HTML_2")"
fi
if [ ! -z "$VIASH_PAR_FASTQC_ZIP_1" ] && [ ! -d "$(dirname "$VIASH_PAR_FASTQC_ZIP_1")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_FASTQC_ZIP_1")"
fi
if [ ! -z "$VIASH_PAR_FASTQC_ZIP_2" ] && [ ! -d "$(dirname "$VIASH_PAR_FASTQC_ZIP_2")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_FASTQC_ZIP_2")"
fi
if [ ! -z "$VIASH_PAR_TRIM_LOG_1" ] && [ ! -d "$(dirname "$VIASH_PAR_TRIM_LOG_1")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_TRIM_LOG_1")"
fi
if [ ! -z "$VIASH_PAR_TRIM_LOG_2" ] && [ ! -d "$(dirname "$VIASH_PAR_TRIM_LOG_2")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_TRIM_LOG_2")"
fi
if [ ! -z "$VIASH_PAR_TRIM_HTML_1" ] && [ ! -d "$(dirname "$VIASH_PAR_TRIM_HTML_1")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_TRIM_HTML_1")"
fi
if [ ! -z "$VIASH_PAR_TRIM_HTML_2" ] && [ ! -d "$(dirname "$VIASH_PAR_TRIM_HTML_2")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_TRIM_HTML_2")"
fi
if [ ! -z "$VIASH_PAR_TRIM_ZIP_1" ] && [ ! -d "$(dirname "$VIASH_PAR_TRIM_ZIP_1")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_TRIM_ZIP_1")"
fi
if [ ! -z "$VIASH_PAR_TRIM_ZIP_2" ] && [ ! -d "$(dirname "$VIASH_PAR_TRIM_ZIP_2")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_TRIM_ZIP_2")"
fi
if [ ! -z "$VIASH_PAR_SORTMERNA_LOG" ] && [ ! -d "$(dirname "$VIASH_PAR_SORTMERNA_LOG")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_SORTMERNA_LOG")"
fi
if [ ! -z "$VIASH_PAR_SALMON_QUANT_OUTPUT" ] && [ ! -d "$(dirname "$VIASH_PAR_SALMON_QUANT_OUTPUT")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_SALMON_QUANT_OUTPUT")"
fi
if [ ! -z "$VIASH_PAR_TRIM_JSON" ] && [ ! -d "$(dirname "$VIASH_PAR_TRIM_JSON")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_TRIM_JSON")"
fi
if [ ! -z "$VIASH_PAR_TRIM_HTML" ] && [ ! -d "$(dirname "$VIASH_PAR_TRIM_HTML")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_TRIM_HTML")"
fi
if [ ! -z "$VIASH_PAR_MERGED_OUT" ] && [ ! -d "$(dirname "$VIASH_PAR_MERGED_OUT")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_MERGED_OUT")"
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_FASTQC="$VIASH_META_RESOURCES_DIR/../../../nextflow/fastqc/main.nf"
VIASH_DEP_UMITOOLS_UMITOOLS_EXTRACT="$VIASH_META_RESOURCES_DIR/../../../nextflow/umitools/umitools_extract/main.nf"
VIASH_DEP_TRIMGALORE="$VIASH_META_RESOURCES_DIR/../../../nextflow/trimgalore/main.nf"
VIASH_DEP_BBMAP_BBSPLIT="$VIASH_META_RESOURCES_DIR/../../../nextflow/bbmap_bbsplit/main.nf"
VIASH_DEP_SORTMERNA="$VIASH_META_RESOURCES_DIR/../../../nextflow/sortmerna/main.nf"
VIASH_DEP_FQ_SUBSAMPLE="$VIASH_META_RESOURCES_DIR/../../../nextflow/fq_subsample/main.nf"
VIASH_DEP_UMI_TOOLS_UMI_TOOLS_EXTRACT="$VIASH_TARGET_DIR/dependencies/vsh/vsh/biobox/v0.2.0/nextflow/umi_tools/umi_tools_extract/main.nf"
VIASH_DEP_FASTP="$VIASH_TARGET_DIR/dependencies/vsh/vsh/biobox/v0.2.0/nextflow/fastp/main.nf"
VIASH_DEP_SALMON_SALMON_QUANT="$VIASH_TARGET_DIR/dependencies/vsh/vsh/biobox/v0.2.0/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-pre_processing-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] ]
    }

    // Perform QC on input fastq files
    | fastqc.run (
      runIf: { id, state -> !state.skip_qc && !state.skip_fastqc },
      fromState: { id, state ->
        def input = state.paired ? [ state.fastq_1, state.fastq_2 ] : [ state.fastq_1 ]
        [ paired: state.paired,
        input: input ]
      },
      toState: [
        "fastqc_html_1": "fastqc_html_1",
        "fastqc_html_2": "fastqc_html_2",
        "fastqc_zip_1": "fastqc_zip_1",
        "fastqc_zip_2": "fastqc_zip_2"
      ]
    )

    // Extract UMIs from fastq files and discard read 1 or read 2 if required
    | umitools_extract.run (
      runIf: { id, state -> state.with_umi && !state.skip_umi_extract },
      fromState: { id, state ->
        def input = state.paired ? [ state.fastq_1, state.fastq_2 ] : [ state.fastq_1 ]
        def bc_pattern = state.paired ? [ state.umitools_bc_pattern, state.umitools_bc_pattern2 ] : [ state.umitools_bc_pattern ]
        [ paired: state.paired,
        input: input, 
        bc_pattern: bc_pattern, 
        umi_discard_read: state.umi_discard_read ]
      },
      toState: [ 
        "fastq_1": "fastq_1", 
        "fastq_2": "fastq_2"
      ]
    )
    
    // Discard read if required
    | map { id, state -> 
      def paired = state.paired
      def fastq_2 = state.fastq_2
      if (paired && state.with_umi && !state.skip_umi_extract && state.umi_discard_read != 0) {
        fastq_2 = state.remove(state.fastq_2) 
        paired = false
      }
      [ id, state + [paired: paired, fastq_2: fastq_2] ]
    }

    // Trim reads using Trim galore!
    | trimgalore.run (
      runIf: { id, state -> !state.skip_trimming && state.trimmer == "trimgalore" },
      fromState: { id, state ->
        def input = state.paired ? [ state.fastq_1, state.fastq_2 ] : [ state.fastq_1 ]
        [ paired: state.paired,
        input: input, 
        min_trimmed_reads: state.min_trimmed_reads ]
      },
      toState: [
        "fastq_1": "trimmed_r1", 
        "fastq_2": "trimmed_r2",
        "trim_log_1": "trimming_report_r1", 
        "trim_log_2": "trimming_report_r2", 
        "trim_zip_1": "trimmed_fastqc_zip_1",
        "trim_zip_2": "trimmed_fastqc_zip_2",
        "trim_html_1": "trimmed_fastqc_html_1",
        "trim_html_2": "trimmed_fastqc_html_2"
      ],
      args: [gzip: true, fastqc: true]
    )

    // Trim reads using fastp
    | fastp.run(
      runIf: { id, state -> !state.skip_trimming && state.trimmer == "fastp" },
      fromState: [
        "in1": "fastq_1",
        "in2": "fastq_2",
        "merge": "fastp_save_merged", 
        "interleaved_in": "interleaved_reads",
        "detect_adapter_for_pe": "fastp_pe_detect_adapter",
        "adapter_fasta": "fastp_adapter_fasta"
      ],
      toState: [
        "fastq_1": "out1", 
        "fastq_2": "out2",
        "failed_trim": "failed_out",
        "failed_trim_unpaired1": "unpaired1",
        "failed_trim_unpaired2": "unpaired2",
        "trim_json": "json",
        "trim_html": "html",
        "trim_merged_out": "merged_out"
      ]
    )

    // Perform FASTQC on reads trimmed using fastp
    | fastqc.run(
      runIf: { id, state -> !state.skip_trimming && state.trimmer == "fastp" },
      fromState: { id, state ->
        def input = state.paired ? [ state.fastq_1, state.fastq_2 ] : [ state.fastq_1 ]
        [ paired: state.paired,
        input: input ]
      },
      toState: [
        "trim_html_1": "fastqc_html_1",
        "trim_html_2": "fastqc_html_2",
        "trim_zip_1": "fastqc_zip_1",
        "trim_zip_2": "fastqc_zip_2"
      ], 
      key: "fastqc_trimming"
    )

    // Filter out contaminant RNA
    | bbmap_bbsplit.run (
      runIf: { id, state -> !state.skip_bbsplit },
      fromState: { id, state ->
        def input = state.paired ? [ state.fastq_1, state.fastq_2 ] : [ state.fastq_1 ]
        [ paired: state.paired,
        input: input,
        built_bbsplit_index: state.bbsplit_index ]
      },
      args: ["only_build_index": false], 
      toState: [
        "fastq_1": "fastq_1", 
        "fastq_2": "fastq_2"
      ]
    )

    // Sort reads by rRNA and non-rRNA
    | sortmerna.run (
      runIf: { id, state -> state.remove_ribo_rna },
      fromState: { id, state ->
        def input = state.paired ? [ state.fastq_1, state.fastq_2 ] : [ state.fastq_1 ]
        [ paired: state.paired,
          input: input,
          ribo_database_manifest: state.ribo_database_manifest ] 
      },
      toState: [
        "fastq_1": "fastq_1", 
        "fastq_2": "fastq_2",
        "sortmerna_log": "sortmerna_log"
      ] 
    )

    // Sub-sample FastQ files and pseudo-align with Salmon to auto-infer strandedness
    | fq_subsample.run (
      runIf: { id, state -> state.strandedness == 'auto' }, 
      fromState: { id, state ->
        def input = state.paired ? [ state.fastq_1, state.fastq_2 ] : [ state.fastq_1 ]
        [ 
          input: input,
          extra_args: state.extra_fq_subsample_args
        ]
      },
      toState: [
        "subsampled_fastq_1": "output_1",
        "subsampled_fastq_2": "output_2"
      ]
    )

    // 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] ]
    }
    | salmon_quant.run (
      runIf: { id, state -> state.strandedness == 'auto' }, 
      fromState: { id, state ->
        def unmated_reads = !state.paired ? state.subsampled_fastq_1 : null
        def mates1 = state.paired ? state.subsampled_fastq_1 : null
        def mates2 = state.paired ? state.subsampled_fastq_2 : null
        [ unmated_reads: unmated_reads,
          mates1: mates1, 
          mates2: mates2, 
          gene_map: state.gtf, 
          index: state.salmon_index,
          lib_type: state.lib_type ]
      },
      args: [ "skip_quant": true ],
      toState: [ "salmon_quant_output": "output" ]
    )

    | map { id, state -> 
      def mod_state = (!state.paired) ? 
        [trim_log_2: state.remove(state.trim_log_2), trim_zip_2: state.remove(state.trim_zip_2), trim_html_2: state.remove(state.trim_html_2), failed_trim_unpaired2: state.remove(state.failed_trim_unpaired2)] : 
        []
      [ id, state + mod_state ]
    }

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

    | setState ( 
        "fastqc_html_1": "fastqc_html_1",
        "fastqc_html_2": "fastqc_html_2",
        "fastqc_zip_1": "fastqc_zip_1",
        "fastqc_zip_2": "fastqc_zip_2", 
        "qc_output1": "fastq_1",
        "qc_output2": "fastq_2", 
        "trim_log_1": "trim_log_1", 
        "trim_log_2": "trim_log_2", 
        "trim_zip_1": "trim_zip_1",
        "trim_zip_2": "trim_zip_2",
        "trim_html_1": "trim_html_1",
        "trim_html_2": "trim_html_2",
        "sortmerna_log": "sortmerna_log",
        "failed_trim": "failed_trim",
        "failed_trim_unpaired1": "failed_trim_unpaired1",
        "failed_trim_unpaired2": "failed_trim_unpaired2",
        "trim_json": "trim_json",
        "trim_html": "trim_html",
        "trim_merged_out": "trim_merged_out",
        "salmon_quant_output": "salmon_quant_output"
    )

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

VIASHEOF


# check whether required files exist
if [ ! -z "$VIASH_PAR_SALMON_QUANT_OUTPUT" ] && [ ! -e "$VIASH_PAR_SALMON_QUANT_OUTPUT" ]; then
  ViashError "Output file '$VIASH_PAR_SALMON_QUANT_OUTPUT' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_TRIM_JSON" ] && [ ! -e "$VIASH_PAR_TRIM_JSON" ]; then
  ViashError "Output file '$VIASH_PAR_TRIM_JSON' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_TRIM_HTML" ] && [ ! -e "$VIASH_PAR_TRIM_HTML" ]; then
  ViashError "Output file '$VIASH_PAR_TRIM_HTML' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_MERGED_OUT" ] && [ ! -e "$VIASH_PAR_MERGED_OUT" ]; then
  ViashError "Output file '$VIASH_PAR_MERGED_OUT' does not exist."
  exit 1
fi


exit 0
