#!/usr/bin/env bash

# trimgalore spaceranger_count
# 
# 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.
# 
# Component authors:
#  * Sai Nirmayi Yasa (author, maintainer)

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="trimgalore"
VIASH_META_FUNCTIONALITY_NAME="trimgalore"
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 "trimgalore spaceranger_count"
  echo ""
  echo "A wrapper tool around Cutadapt and FastQC to consistently apply quality and"
  echo "adapter trimming to FastQ files."
  echo ""
  echo "Input:"
  echo "    --input"
  echo "        type: file, required parameter, multiple values allowed, file must exist"
  echo "        example: sample1_r1.fq;sample1_r2.fq;sample2_r1.fq;sample2_r2.fq"
  echo "        Input files. Note that paired-end files need to be supplied in a"
  echo "        pairwise fashion, e.g. file1_1.fq file1_2.fq SRR2_1.fq.gz SRR2_2.fq.gz"
  echo ""
  echo "Trimming options:"
  echo "    -q, --quality"
  echo "        type: integer"
  echo "        example: 20"
  echo "        Trim low-quality ends (below the specified Phred score) from reads in"
  echo "        addition to adapter removal. For RRBS samples, quality trimming will be"
  echo "        performed first, and adapter trimming is carried in a second round."
  echo "        Other files are quality and adapter trimmed in a single pass. The"
  echo "        algorithm is the same as the one used by BWA (Subtract INT from all"
  echo "        qualities; compute partial sums from all indices to the end of the"
  echo "        sequence; cut sequence at the index at which the sum is minimal)."
  echo ""
  echo "    --phred33"
  echo "        type: boolean_true"
  echo "        Instructs Cutadapt to use ASCII+33 quality scores as Phred scores"
  echo "        (Sanger/Illumina 1.9+ encoding) for quality trimming."
  echo ""
  echo "    --phred64"
  echo "        type: boolean_true"
  echo "        Instructs Cutadapt to use ASCII+64 quality scores as Phred scores"
  echo "        (Illumina 1.5 encoding) for quality trimming."
  echo ""
  echo "    --fastqc"
  echo "        type: boolean_true"
  echo "        Run FastQC in the default mode on the FastQ file once trimming is"
  echo "        complete."
  echo ""
  echo "    --fastqc_args"
  echo "        type: string"
  echo "        example: --nogroup --noextract"
  echo "        Passes extra arguments (excluding files) to FastQC. If more than one"
  echo "        argument is to be passed to FastQC they must be in the form \"arg1 arg2"
  echo "        ...\". Passing extra arguments will automatically invoke FastQC, so"
  echo "        --fastqc does not have to be specified separately."
  echo ""
  echo "    --fastqc_contaminants"
  echo "        type: file, file must exist"
  echo "        example: contaminants.txt"
  echo "        Specifies a non-default file which contains the list of contaminants for"
  echo "        FastQC to screen overrepresented sequences against. The file must"
  echo "        contain sets of named contaminants in the form name[tab]sequence. Lines"
  echo "        prefixed with a hash will be ignored."
  echo ""
  echo "    --fastqc_adapters"
  echo "        type: file, file must exist"
  echo "        example: adapters.txt"
  echo "        Specifies a non-default file which contains the list of adapter"
  echo "        sequences which which FasstQC will explicity search against the library."
  echo "        The file must contain sets of named adapters in the form"
  echo "        name[tab]sequence.  Lines prefixed with a hash will be ignored."
  echo ""
  echo "    --fastqc_limits"
  echo "        type: file, file must exist"
  echo "        example: limits.txt"
  echo "        Specifies a non-default file which contains a set of criteria which"
  echo "        FastQC will use to determine the warn/error limits for the various"
  echo "        modules. This file can also be used to selectively remove some modules"
  echo "        from the output all together.  The format needs to mirror the default"
  echo "        limits.txt file found in the Configuration folder."
  echo ""
  echo "    -a, --adapter"
  echo "        type: string"
  echo "        example: AGCTCCCG"
  echo "        Adapter sequence to be trimmed. If not specified explicitly, Trim Galore"
  echo "        will try to auto-detect whether the Illumina universal, Nextera"
  echo "        transposase or Illumina small RNA adapter sequence was used. A single"
  echo "        base may also be given as e.g. -a A{10}, to be expanded to -a"
  echo "        AAAAAAAAAA."
  echo "        At a special request, multiple adapters can also be specified like so:"
  echo "          -a  \" AGCTCCCG -a TTTCATTATAT -a TTTATTCGGATTTAT\" -a2 \" AGCTAGCG -a"
  echo "        TCTCTTATAT -a TTTCGGATTTAT\","
  echo "        or so:"
  echo "          -a \"file:../multiple_adapters.fa\" -a2 \"file:../different_adapters.fa\""
  echo "        Potentially in conjucntion with the parameter \"-n 3\" to trim all"
  echo "        adapters."
  echo ""
  echo "    -a2, --adapter2"
  echo "        type: string"
  echo "        example: AGCTCCCG"
  echo "        Optional adapter sequence to be trimmed off read 2 of paired-end files."
  echo "        This option requires '--paired' to be specified as well. If the"
  echo "        libraries to be trimmed are smallRNA then a2 will be set to the Illumina"
  echo "        small RNA 5' adapter automatically (GATCGTCGGACT). A single base may"
  echo "        also be given as e.g. -a2 A{10}, to be expanded to -a2 AAAAAAAAAA."
  echo ""
  echo "    --illumina"
  echo "        type: boolean_true"
  echo "        Adapter sequence to be trimmed is the first 13bp of the Illumina"
  echo "        universal adapter 'AGATCGGAAGAGC' instead of the default auto-detection"
  echo "        of adapter sequence."
  echo ""
  echo "    --stranded_illumina"
  echo "        type: boolean_true"
  echo "        Adapter sequence to be trimmed is the first 13bp of the Illumina"
  echo "        stranded mRNA or Total RNA adapter 'ACTGTCTCTTATA' instead of the"
  echo "        default auto-detection of adapter sequence."
  echo ""
  echo "    --nextera"
  echo "        type: boolean_true"
  echo "        Adapter sequence to be trimmed is the first 12bp of the Nextera adapter"
  echo "        'CTGTCTCTTATA' instead of the default auto-detection of adapter"
  echo "        sequence."
  echo ""
  echo "    --small_rna"
  echo "        type: boolean_true"
  echo "        Adapter sequence to be trimmed is the first 12bp of the Illumina Small"
  echo "        RNA 3' Adapter 'TGGAATTCTCGG' instead of the default auto-detection of"
  echo "        adapter sequence. Selecting to trim smallRNA adapters will also lower"
  echo "        the --length value to 18bp. If the smallRNA libraries are paired-end"
  echo "        then a automatically (GATCGTCGGACT) unless -a 2 had been defined"
  echo "        explicitly."
  echo ""
  echo "    --consider_already_trimmed"
  echo "        type: integer"
  echo "        During adapter auto-detection, the limit set by this argument allows the"
  echo "        user to set a threshold up to which the file is considered already"
  echo "        adapter-trimmed. If no adapter sequence exceeds this threshold, no"
  echo "        additional adapter trimming will be performed (technically, the adapter"
  echo "        is set to '-a X'). Quality trimming is still performed as usual."
  echo ""
  echo "    --max_length"
  echo "        type: integer"
  echo "        Discard reads that are longer than the specified value after trimming."
  echo "        This is only advised for smallRNA sequencing to remove non-small RNA"
  echo "        sequences."
  echo ""
  echo "    --stringency"
  echo "        type: integer"
  echo "        example: 1"
  echo "        Overlap with adapter sequence required to trim a sequence. Defaults to a"
  echo "        very stringent setting of 1, i.e. even a single bp of overlapping"
  echo "        sequence will be trimmed off from the 3' end of any read."
  echo ""
  echo "    -e, --error_rate"
  echo "        type: double"
  echo "        example: 0.1"
  echo "        Maximum allowed error rate (no. of errors divided by the length of the"
  echo "        matching region)"
  echo ""
  echo "    --gzip"
  echo "        type: boolean_true"
  echo "        Compress the output file with GZIP. If the input files are"
  echo "        GZIP-compressed the output files will automatically be GZIP compressed"
  echo "        as well. As of v0.2.8 the compression will take place on the fly."
  echo ""
  echo "    --dont_gzip"
  echo "        type: boolean_true"
  echo "        Output files won't be compressed with GZIP. This option overrides"
  echo "        --gzip."
  echo ""
  echo "    --length"
  echo "        type: integer"
  echo "        example: 20"
  echo "        Discard reads that became shorter than the specified length because of"
  echo "        either quality or adapter trimming. A value of '0' effectively disables"
  echo "        this behaviour. For paired-end files, both reads of a read-pair need to"
  echo "        be longer than the specified length to be printed out to validated"
  echo "        paired-end files. If only one read became too short there is the"
  echo "        possibility of keeping such unpaired single-end reads using the"
  echo "        --retain_unpaired option."
  echo ""
  echo "    --max_n"
  echo "        type: integer"
  echo "        The total number of Ns a read may contain before it will be removed"
  echo "        altogether.In a paired-end setting, either read exceeding this limit"
  echo "        will result in the entire pair being removed from the trimmed output"
  echo "        files. If COUNT is a number between 0 and 1, it is interpreted as a"
  echo "        fraction of the read length."
  echo ""
  echo "    --trim_n"
  echo "        type: boolean_true"
  echo "        Removes Ns from either side of the read. This option does currently not"
  echo "        work in RRBS mode."
  echo ""
  echo "    --no_report_file"
  echo "        type: boolean_true"
  echo "        If specified no report file will be generated."
  echo ""
  echo "    --suppress_warn"
  echo "        type: boolean_true"
  echo "        If specified any output to STDOUT or STDERR will be suppressed."
  echo ""
  echo "    --clip_R1"
  echo "        type: integer"
  echo "        Instructs TrimGalore to remove given number of bp from the 5' end of"
  echo "        read 1 (or single-end reads). This may be useful if the qualities were"
  echo "        very poor, or if there is some sort of unwanted bias at the 5' end."
  echo ""
  echo "    --clip_R2"
  echo "        type: integer"
  echo "        Instructs TrimGalore to remove given number bp from the 5' end of read 2"
  echo "        (paired-end reads only). This may be useful if the qualities were very"
  echo "        poor, or if there is some sort of unwanted bias at the 5' end. For"
  echo "        paired-end BS-Seq, it is recommended to remove the first few bp because"
  echo "        the end-repair reaction may introduce a bias towards low methylation."
  echo ""
  echo "    --three_prime_clip_R1"
  echo "        type: integer"
  echo "        Instructs Trim Galore to remove spacified number of bp from the 3' end"
  echo "        of read 1 (or single-end reads) AFTER adapter/quality trimming has been"
  echo "        performed. This may remove some  bias from the 3' end that is not"
  echo "        directly related to adapter sequence or basecall quality."
  echo ""
  echo "    --three_prime_clip_R2"
  echo "        type: integer"
  echo "        Instructs Trim Galore to remove <int> bp from the 3' end of read 2 AFTER"
  echo "        adapter/quality trimming has been performed. This may remove some"
  echo "        unwanted bias from the 3' end that is not directly related to adapter"
  echo "        sequence or basecall quality."
  echo ""
  echo "    --nextseq"
  echo "        type: integer"
  echo "        This enables the option '--nextseq-trim=3'CUTOFF' within Cutadapt, which"
  echo "        will set a quality cutoff (that is normally given with -q instead), but"
  echo "        qualities of G bases are ignored. This trimming is in common for the"
  echo "        NextSeq- and NovaSeq-platforms, where basecalls without any signal are"
  echo "        called as high-quality G bases. This is mutually exlusive with '-q INT'."
  echo ""
  echo "    --basename"
  echo "        type: string"
  echo "        Use specified name (PREFERRED_NAME) as the basename for output files,"
  echo "        instead of deriving the filenames from the input files. Single-end data"
  echo "        would be called PREFERRED_NAME_trimmed.fq(.gz), or"
  echo "        PREFERRED_NAME_val_1.fq(.gz) and PREFERRED_NAME_val_2.fq(.gz) for"
  echo "        paired-end data. --basename only works when 1 file (single-end) or 2"
  echo "        files (paired-end) are specified, but not for longer lists."
  echo ""
  echo "Specific trimming options without adapter/quality trimming:"
  echo "    --hardtrim5"
  echo "        type: integer"
  echo "        Instead of performing adapter-/quality trimming, this option will simply"
  echo "        hard-trim sequences to <int> bp at the 5'-end. Once hard-trimming of"
  echo "        files is complete, Trim Galore will exit. Hard-trimmed output files will"
  echo "        end in .<int>_5prime.fq(.gz)."
  echo ""
  echo "    --hardtrim3"
  echo "        type: integer"
  echo "        Instead of performing adapter-/quality trimming, this option will simply"
  echo "        hard-trim sequences to <int> bp at the 3'-end. Once hard-trimming of"
  echo "        files is complete, Trim Galore will exit. Hard-trimmed output files will"
  echo "        end in .<int>_3prime.fq(.gz)."
  echo ""
  echo "    --clock"
  echo "        type: boolean_true"
  echo "        In this mode, reads are trimmed in a specific way that is currently used"
  echo "        for the Mouse Epigenetic Clock."
  echo ""
  echo "    --polyA"
  echo "        type: boolean_true"
  echo "        This is a new, still experimental, trimming mode to identify and remove"
  echo "        poly-A tails from sequences. When --polyA is selected, Trim Galore"
  echo "        attempts to identify from the first supplied sample whether sequences"
  echo "        contain more often a stretch of either 'AAAAAAAAAA' or 'TTTTTTTTTT'."
  echo "        This determines if Read 1 of a paired-end end file, or single-end files,"
  echo "        are trimmed for PolyA or PolyT. In case of paired-end sequencing, Read2"
  echo "        is trimmed for the complementary base from the start of the reads. The"
  echo "        auto-detection uses a default of A{20} for Read1 (3'-end trimming) and"
  echo "        T{150} for Read2 (5'-end trimming). These values may be changed manually"
  echo "        using the options -a and -a2. In addition to trimming the sequences,"
  echo "        white spaces are replaced with _ and it records in the read ID how many"
  echo "        bases were trimmed so it can later be used to identify PolyA trimmed"
  echo "        sequences. This is currently done by writing tags to both the start"
  echo "        (\"32:A:\") and end (\"_PolyA:32\") of the reads. The poly-A trimming mode"
  echo "        expects that sequences were both adapter and quality  before looking for"
  echo "        Poly-A tails, and it is the user's responsibility to carry out an"
  echo "        initial round of trimming."
  echo ""
  echo "    --implicon"
  echo "        type: boolean_true"
  echo "        This is a special mode of operation for paired-end data, such as"
  echo "        required for the IMPLICON method, where a UMI sequence is getting"
  echo "        transferred from the start of Read 2 to the readID of both reads."
  echo "        Following this, Trim Galore will exit. In it's current implementation,"
  echo "        the UMI carrying reads come in the following format"
  echo "          Read 1  5' FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF 3'"
  echo "          Read 2  3' UUUUUUUUFFFFFFFFFFFFFFFFFFFFFFFFFFFF 5'"
  echo "        Where UUUUUUUU is a random 8-mer unique molecular identifier (UMI) and"
  echo "        FFFFFFF... is the actual fragment to be sequenced. The UMI of Read 2"
  echo "        (R2) is written into the read ID of both reads and removed from the"
  echo "        actual sequence."
  echo ""
  echo "RRBS-specific options:"
  echo "    --rrbs"
  echo "        type: boolean_true"
  echo "        Specifies that the input file was an MspI digested RRBS sample"
  echo "        (recognition site is CCGG). Single-end or Read 1 sequences (paired-end)"
  echo "        which were adapter-trimmed will have a further 2 bp removed from their"
  echo "        3' end. Sequences which were merely trimmed because of poor quality will"
  echo "        not be shortened further. Read 2 of paired-end libraries will in"
  echo "        addition have the first 2 bp removed from the 5' end (by setting"
  echo "        '--clip_r2 2'). This is to avoid using artificial methylation calls from"
  echo "        the filled-in cytosine positions close to the 3' MspI site in sequenced"
  echo "        fragments. This option is not recommended for users of the Tecan Ovation"
  echo "        RRBS Methyl-Seq with TrueMethyl oxBS 1-16 kit (see below)."
  echo ""
  echo "    --non_directional"
  echo "        type: boolean_true"
  echo "        Selecting this option for non-directional RRBS libraries will screen"
  echo "        quality-trimmed sequences for 'CAA' or 'CGA' at the start of the read"
  echo "        and, if found, removes the first two basepairs. Like with the option"
  echo "        '--rrbs' this avoids using cytosine positions that were filled-in during"
  echo "        the end-repair step. '--non_directional' requires '--rrbs' to be"
  echo "        specified as well. Note that this option does not set '--clip_r2 2' in"
  echo "        paired-end mode."
  echo ""
  echo "    --keep"
  echo "        type: boolean_true"
  echo "        Keep the quality trimmed intermediate file."
  echo ""
  echo "Paired-end specific options:"
  echo "    --paired"
  echo "        type: boolean_true"
  echo "        This option performs length trimming of quality/adapter/RRBS trimmed"
  echo "        reads for paired-end files. To pass the validation test, both sequences"
  echo "        of a sequence pair are required to have a certain minimum length which"
  echo "        is governed by the option --length (see above). If only one read passes"
  echo "        this length threshold the other read can be rescued (see option"
  echo "        --retain_unpaired). Using this option lets you discard too short read"
  echo "        pairs without disturbing the sequence-by-sequence order of FastQ files"
  echo "        which is required by many aligners. Trim Galore expects paired-end files"
  echo "        to be supplied in a pairwise fashion, e.g. file1_1.fq file1_2.fq"
  echo "        SRR2_1.fq.gz SRR2_2.fq.gz ... ."
  echo ""
  echo "    --retain_unpaired"
  echo "        type: boolean_true"
  echo "        If only one of the two paired-end reads became too short, the longer"
  echo "        read will be written to either '.unpaired_1.fq' or '.unpaired_2.fq'"
  echo "        output files. The length cutoff for unpaired single-end reads is"
  echo "        governed by the parameters -r1/--length_1 and -r2/--length_2."
  echo ""
  echo "    -r1, --length_1"
  echo "        type: integer"
  echo "        example: 35"
  echo "        Unpaired single-end read length cutoff needed for read 1 to be written"
  echo "        to '.unpaired_1.fq' output file. These reads may be mapped in single-end"
  echo "        mode."
  echo ""
  echo "    -r2, --length_2"
  echo "        type: integer"
  echo "        example: 35"
  echo "        Unpaired single-end read length cutoff needed for read 2 to be written"
  echo "        to '.unpaired_2.fq' output file. These reads may be mapped in single-end"
  echo "        mode."
  echo ""
  echo "Output:"
  echo "    -o, --output_dir"
  echo "        type: file, required parameter, output, file must exist"
  echo "        default: trimmed_output"
  echo "        If specified all output will be written to this directory instead of the"
  echo "        current directory."
  echo ""
  echo "    --trimmed_r1"
  echo "        type: file, output, file must exist"
  echo "        example: read_1.fastq"
  echo "        Output file for read 1. Only works when 1 file (single-end) or 2 files"
  echo "        (paired-end) are specified, but not for longer lists."
  echo ""
  echo "    --trimmed_r2"
  echo "        type: file, output, file must exist"
  echo "        example: read_2.fastq"
  echo "        Output file for read 2. Only works when 1 file (single-end) or 2 files"
  echo "        (paired-end) are specified, but not for longer lists."
  echo ""
  echo "    --trimming_report_r1"
  echo "        type: file, output, file must exist"
  echo "        example: read_1.trimming_report.txt"
  echo "        Trimming report for read 1. Only works when 1 file (single-end) or 2"
  echo "        files (paired-end) are specified, but not for longer lists."
  echo ""
  echo "    --trimming_report_r2"
  echo "        type: file, output, file must exist"
  echo "        example: read_2.trimming_report.txt"
  echo "        Trimming report for read 1. Only works when 1 file (single-end) or 2"
  echo "        files (paired-end) are specified, but not for longer lists."
  echo ""
  echo "    --trimmed_fastqc_html_1"
  echo "        type: file, output, file must exist"
  echo "        example: read_1.fastqc.html"
  echo "        FastQC report for trimmed (single-end) reads (or read 1 for paired-end)."
  echo "        Only works when 1 file (single-end) or 2 files (paired-end) are"
  echo "        specified, but not for longer lists."
  echo ""
  echo "    --trimmed_fastqc_html_2"
  echo "        type: file, output, file must exist"
  echo "        example: read_2.fastqc.html"
  echo "        FastQC report for trimmed reads (read2 for paired-end). Only works when"
  echo "        1 file (single-end) or 2 files (paired-end) are specified, but not for"
  echo "        longer lists."
  echo ""
  echo "    --trimmed_fastqc_zip_1"
  echo "        type: file, output, file must exist"
  echo "        example: read_1.fastqc.zip"
  echo "        FastQC results for trimmed (single-end) reads (or read 1 for"
  echo "        paired-end). Only works when 1 file (single-end) or 2 files (paired-end)"
  echo "        are specified, but not for longer lists."
  echo ""
  echo "    --trimmed_fastqc_zip_2"
  echo "        type: file, output, file must exist"
  echo "        example: read_2.fastqc.zip"
  echo "        FastQC results for trimmed reads (read2 for paired-end). Only works when"
  echo "        1 file (single-end) or 2 files (paired-end) are specified, but not for"
  echo "        longer lists."
  echo ""
  echo "    --unpaired_r1"
  echo "        type: file, output, file must exist"
  echo "        example: unpaired_read_1.fastq"
  echo "        Output file for unpired read 1. Only works when 1 file (single-end) or 2"
  echo "        files (paired-end) are specified, but not for longer lists."
  echo ""
  echo "    --unpaired_r2"
  echo "        type: file, output, file must exist"
  echo "        example: unpaired_read_2.fastq"
  echo "        Output file for unpaired read 2. Only works when 1 file (single-end) or"
  echo "        2 files (paired-end) are specified, but not for longer lists."
}

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

######## Helper functions for setting up Docker images for viash ########
# expects: ViashDockerBuild

# ViashDockerInstallationCheck: check whether Docker is installed correctly
#
# examples:
#   ViashDockerInstallationCheck
function ViashDockerInstallationCheck {
  ViashDebug "Checking whether Docker is installed"
  if [ ! command -v docker &> /dev/null ]; then
    ViashCritical "Docker doesn't seem to be installed. See 'https://docs.docker.com/get-docker/' for instructions."
    exit 1
  fi

  ViashDebug "Checking whether the Docker daemon is running"
  local save=$-; set +e
  local docker_version=$(docker version --format '{{.Client.APIVersion}}' 2> /dev/null)
  local out=$?
  [[ $save =~ e ]] && set -e
  if [ $out -ne 0 ]; then
    ViashCritical "Docker daemon does not seem to be running. Try one of the following:"
    ViashCritical "- Try running 'dockerd' in the command line"
    ViashCritical "- See https://docs.docker.com/config/daemon/"
    exit 1
  fi
}

# ViashDockerRemoteTagCheck: check whether a Docker image is available 
# on a remote. Assumes `docker login` has been performed, if relevant.
#
# $1                  : image identifier with format `[registry/]image[:tag]`
# exit code $?        : whether or not the image was found
# examples:
#   ViashDockerRemoteTagCheck python:latest
#   echo $?                                     # returns '0'
#   ViashDockerRemoteTagCheck sdaizudceahifu
#   echo $?                                     # returns '1'
function ViashDockerRemoteTagCheck {
  docker manifest inspect $1 > /dev/null 2> /dev/null
}

# ViashDockerLocalTagCheck: check whether a Docker image is available locally
#
# $1                  : image identifier with format `[registry/]image[:tag]`
# exit code $?        : whether or not the image was found
# examples:
#   docker pull python:latest
#   ViashDockerLocalTagCheck python:latest
#   echo $?                                     # returns '0'
#   ViashDockerLocalTagCheck sdaizudceahifu
#   echo $?                                     # returns '1'
function ViashDockerLocalTagCheck {
  [ -n "$(docker images -q $1)" ]
}

# ViashDockerPull: pull a Docker image
#
# $1                  : image identifier with format `[registry/]image[:tag]`
# exit code $?        : whether or not the image was found
# examples:
#   ViashDockerPull python:latest
#   echo $?                                     # returns '0'
#   ViashDockerPull sdaizudceahifu
#   echo $?                                     # returns '1'
function ViashDockerPull {
  ViashNotice "Checking if Docker image is available at '$1'"
  if [ $VIASH_VERBOSITY -ge $VIASH_LOGCODE_INFO ]; then
    docker pull $1 && return 0 || return 1
  else
    local save=$-; set +e
    docker pull $1 2> /dev/null > /dev/null
    local out=$?
    [[ $save =~ e ]] && set -e
    if [ $out -ne 0 ]; then
      ViashWarning "Could not pull from '$1'. Docker image doesn't exist or is not accessible."
    fi
    return $out
  fi
}

# ViashDockerPush: push a Docker image
#
# $1                  : image identifier with format `[registry/]image[:tag]`
# exit code $?        : whether or not the image was found
# examples:
#   ViashDockerPush python:latest
#   echo $?                                     # returns '0'
#   ViashDockerPush sdaizudceahifu
#   echo $?                                     # returns '1'
function ViashDockerPush {
  ViashNotice "Pushing image to '$1'"
  local save=$-; set +e
  local out
  if [ $VIASH_VERBOSITY -ge $VIASH_LOGCODE_INFO ]; then
    docker push $1
    out=$?
  else
    docker push $1 2> /dev/null > /dev/null
    out=$?
  fi
  [[ $save =~ e ]] && set -e
  if [ $out -eq 0 ]; then
    ViashNotice "Container '$1' push succeeded."
  else
    ViashError "Container '$1' push errored. You might not be logged in or have the necessary permissions."
  fi
  return $out
}

# ViashDockerPullElseBuild: pull a Docker image, else build it
#
# $1                  : image identifier with format `[registry/]image[:tag]`
# ViashDockerBuild    : a Bash function which builds a docker image, takes image identifier as argument.
# examples:
#   ViashDockerPullElseBuild mynewcomponent
function ViashDockerPullElseBuild {
  local save=$-; set +e
  ViashDockerPull $1
  local out=$?
  [[ $save =~ e ]] && set -e
  if [ $out -ne 0 ]; then
    ViashDockerBuild $@
  fi
}

# ViashDockerSetup: create a Docker image, according to specified docker setup strategy
#
# $1          : image identifier with format `[registry/]image[:tag]`
# $2          : docker setup strategy, see DockerSetupStrategy.scala
# examples:
#   ViashDockerSetup mynewcomponent alwaysbuild
function ViashDockerSetup {
  local image_id="$1"
  local setup_strategy="$2"
  if [ "$setup_strategy" == "alwaysbuild" -o "$setup_strategy" == "build" -o "$setup_strategy" == "b" ]; then
    ViashDockerBuild $image_id --no-cache $(ViashDockerBuildArgs "$engine_id")
  elif [ "$setup_strategy" == "alwayspull" -o "$setup_strategy" == "pull" -o "$setup_strategy" == "p" ]; then
    ViashDockerPull $image_id
  elif [ "$setup_strategy" == "alwayspullelsebuild" -o "$setup_strategy" == "pullelsebuild" ]; then
    ViashDockerPullElseBuild $image_id --no-cache $(ViashDockerBuildArgs "$engine_id")
  elif [ "$setup_strategy" == "alwayspullelsecachedbuild" -o "$setup_strategy" == "pullelsecachedbuild" ]; then
    ViashDockerPullElseBuild $image_id $(ViashDockerBuildArgs "$engine_id")
  elif [ "$setup_strategy" == "alwayscachedbuild" -o "$setup_strategy" == "cachedbuild" -o "$setup_strategy" == "cb" ]; then
    ViashDockerBuild $image_id $(ViashDockerBuildArgs "$engine_id")
  elif [[ "$setup_strategy" =~ ^ifneedbe ]]; then
    local save=$-; set +e
    ViashDockerLocalTagCheck $image_id
    local outCheck=$?
    [[ $save =~ e ]] && set -e
    if [ $outCheck -eq 0 ]; then
      ViashInfo "Image $image_id already exists"
    elif [ "$setup_strategy" == "ifneedbebuild" ]; then
      ViashDockerBuild $image_id --no-cache $(ViashDockerBuildArgs "$engine_id")
    elif [ "$setup_strategy" == "ifneedbecachedbuild" ]; then
      ViashDockerBuild $image_id $(ViashDockerBuildArgs "$engine_id")
    elif [ "$setup_strategy" == "ifneedbepull" ]; then
      ViashDockerPull $image_id
    elif [ "$setup_strategy" == "ifneedbepullelsebuild" ]; then
      ViashDockerPullElseBuild $image_id --no-cache $(ViashDockerBuildArgs "$engine_id")
    elif [ "$setup_strategy" == "ifneedbepullelsecachedbuild" ]; then
      ViashDockerPullElseBuild $image_id $(ViashDockerBuildArgs "$engine_id")
    else
      ViashError "Unrecognised Docker strategy: $setup_strategy"
      exit 1
    fi
  elif [ "$setup_strategy" == "push" -o "$setup_strategy" == "forcepush" -o "$setup_strategy" == "alwayspush" ]; then
    ViashDockerPush "$image_id"
  elif [ "$setup_strategy" == "pushifnotpresent" -o "$setup_strategy" == "gentlepush" -o "$setup_strategy" == "maybepush" ]; then
    local save=$-; set +e
    ViashDockerRemoteTagCheck $image_id
    local outCheck=$?
    [[ $save =~ e ]] && set -e
    if [ $outCheck -eq 0 ]; then
      ViashNotice "Container '$image_id' exists, doing nothing."
    else
      ViashNotice "Container '$image_id' does not yet exist."
      ViashDockerPush "$image_id"
    fi
  elif [ "$setup_strategy" == "donothing" -o "$setup_strategy" == "meh" ]; then
    ViashNotice "Skipping setup."
  else
    ViashError "Unrecognised Docker strategy: $setup_strategy"
    exit 1
  fi
}

# ViashDockerCheckCommands: Check whether a docker container has the required commands
#
# $1                  : image identifier with format `[registry/]image[:tag]`
# $@                  : commands to verify being present
# examples:
#   ViashDockerCheckCommands bash:4.0 bash ps foo
function ViashDockerCheckCommands {
  local image_id="$1"
  shift 1
  local commands="$@"
  local save=$-; set +e
  local missing # mark 'missing' as local in advance, otherwise the exit code of the command will be missing and always be '0'
  missing=$(docker run --rm --entrypoint=sh "$image_id" -c "for command in $commands; do command -v \$command >/dev/null 2>&1; if [ \$? -ne 0 ]; then echo \$command; exit 1; fi; done")
  local outCheck=$?
  [[ $save =~ e ]] && set -e
  if [ $outCheck -ne 0 ]; then
  	ViashError "Docker container '$image_id' does not contain command '$missing'."
  	exit 1
  fi
}

# ViashDockerBuild: build a docker image
# $1                               : image identifier with format `[registry/]image[:tag]`
# $...                             : additional arguments to pass to docker build
# $VIASH_META_TEMP_DIR             : temporary directory to store dockerfile & optional resources in
# $VIASH_META_NAME                 : name of the component
# $VIASH_META_RESOURCES_DIR        : directory containing the resources
# $VIASH_VERBOSITY                 : verbosity level
# exit code $?                     : whether or not the image was built successfully
function ViashDockerBuild {
  local image_id="$1"
  shift 1

  # create temporary directory to store dockerfile & optional resources in
  local tmpdir=$(mktemp -d "$VIASH_META_TEMP_DIR/dockerbuild-$VIASH_META_NAME-XXXXXX")
  local dockerfile="$tmpdir/Dockerfile"
  function clean_up {
    rm -rf "$tmpdir"
  }
  trap clean_up EXIT

  # store dockerfile and resources
  ViashDockerfile "$VIASH_ENGINE_ID" > "$dockerfile"

  # generate the build command
  local docker_build_cmd="docker build -t '$image_id' $@ '$VIASH_META_RESOURCES_DIR' -f '$dockerfile'"

  # build the container
  ViashNotice "Building container '$image_id' with Dockerfile"
  ViashInfo "$docker_build_cmd"
  local save=$-; set +e
  if [ $VIASH_VERBOSITY -ge $VIASH_LOGCODE_INFO ]; then
    eval $docker_build_cmd
  else
    eval $docker_build_cmd &> "$tmpdir/docker_build.log"
  fi

  # check exit code
  local out=$?
  [[ $save =~ e ]] && set -e
  if [ $out -ne 0 ]; then
    ViashError "Error occurred while building container '$image_id'"
    if [ $VIASH_VERBOSITY -lt $VIASH_LOGCODE_INFO ]; then
      ViashError "Transcript: --------------------------------"
      cat "$tmpdir/docker_build.log"
      ViashError "End of transcript --------------------------"
    fi
    exit 1
  fi
}

######## End of helper functions for setting up Docker images for viash ########

# ViashDockerFile: print the dockerfile to stdout
# $1    : engine identifier
# return : dockerfile required to run this component
# examples:
#   ViashDockerFile
function ViashDockerfile {
  local engine_id="$1"

  if [[ "$engine_id" == "docker" ]]; then
    cat << 'VIASHDOCKER'
FROM quay.io/biocontainers/trim-galore:0.6.10--hdfd78af_0
ENTRYPOINT []
RUN echo "TrimGalore: `trim_galore --version | sed -n 's/.*version\s\+\([0-9]\+\.[0-9]\+\.[0-9]\+\).*/\1/p'`" > /var/software_versions.txt

LABEL org.opencontainers.image.authors="Sai Nirmayi Yasa"
LABEL org.opencontainers.image.description="Companion container for running component trimgalore"
LABEL org.opencontainers.image.created="2024-12-17T15:30:45Z"
LABEL org.opencontainers.image.source="https://github.com/FelixKrueger/TrimGalore"
LABEL org.opencontainers.image.revision="952ff0843093b538cbfd6fefdecf2e7a0bc9e70b"
LABEL org.opencontainers.image.version="spaceranger_count"

VIASHDOCKER
  fi
}

# ViashDockerBuildArgs: return the arguments to pass to docker build
# $1    : engine identifier
# return : arguments to pass to docker build
function ViashDockerBuildArgs {
  local engine_id="$1"

  if [[ "$engine_id" == "docker" ]]; then
    echo ""
  fi
}

# ViashAbsolutePath: generate absolute path from relative path
# borrowed from https://stackoverflow.com/a/21951256
# $1     : relative filename
# return : absolute path
# examples:
#   ViashAbsolutePath some_file.txt   # returns /path/to/some_file.txt
#   ViashAbsolutePath /foo/bar/..     # returns /foo
function ViashAbsolutePath {
  local thePath
  local parr
  local outp
  local len
  if [[ ! "$1" =~ ^/ ]]; then
    thePath="$PWD/$1"
  else
    thePath="$1"
  fi
  echo "$thePath" | (
    IFS=/
    read -a parr
    declare -a outp
    for i in "${parr[@]}"; do
      case "$i" in
      ''|.) continue ;;
      ..)
        len=${#outp[@]}
        if ((len==0)); then
          continue
        else
          unset outp[$((len-1))]
        fi
        ;;
      *)
        len=${#outp[@]}
        outp[$len]="$i"
      ;;
      esac
    done
    echo /"${outp[*]}"
  )
}
# ViashDockerAutodetectMount: auto configuring docker mounts from parameters
# $1                             : The parameter value
# returns                        : New parameter
# $VIASH_DIRECTORY_MOUNTS        : Added another parameter to be passed to docker
# $VIASH_DOCKER_AUTOMOUNT_PREFIX : The prefix to be used for the automounts
# examples:
#   ViashDockerAutodetectMount /path/to/bar      # returns '/viash_automount/path/to/bar'
#   ViashDockerAutodetectMountArg /path/to/bar   # returns '--volume="/path/to:/viash_automount/path/to"'
function ViashDockerAutodetectMount {
  local abs_path=$(ViashAbsolutePath "$1")
  local mount_source
  local base_name
  if [ -d "$abs_path" ]; then
    mount_source="$abs_path"
    base_name=""
  else
    mount_source=`dirname "$abs_path"`
    base_name=`basename "$abs_path"`
  fi
  local mount_target="$VIASH_DOCKER_AUTOMOUNT_PREFIX$mount_source"
  if [ -z "$base_name" ]; then
    echo "$mount_target"
  else
    echo "$mount_target/$base_name"
  fi
}
function ViashDockerAutodetectMountArg {
  local abs_path=$(ViashAbsolutePath "$1")
  local mount_source
  local base_name
  if [ -d "$abs_path" ]; then
    mount_source="$abs_path"
    base_name=""
  else
    mount_source=`dirname "$abs_path"`
    base_name=`basename "$abs_path"`
  fi
  local mount_target="$VIASH_DOCKER_AUTOMOUNT_PREFIX$mount_source"
  ViashDebug "ViashDockerAutodetectMountArg $1 -> $mount_source -> $mount_target"
  echo "--volume=\"$mount_source:$mount_target\""
}
function ViashDockerStripAutomount {
  local abs_path=$(ViashAbsolutePath "$1")
  echo "${abs_path#$VIASH_DOCKER_AUTOMOUNT_PREFIX}"
}
# initialise variables
VIASH_DIRECTORY_MOUNTS=()

# configure default docker automount prefix if it is unset
if [ -z "${VIASH_DOCKER_AUTOMOUNT_PREFIX+x}" ]; then
  VIASH_DOCKER_AUTOMOUNT_PREFIX="/viash_automount"
fi

# initialise docker variables
VIASH_DOCKER_RUN_ARGS=(-i --rm)

# 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 "trimgalore spaceranger_count"
            exit
            ;;
        --input)
            if [ -z "$VIASH_PAR_INPUT" ]; then
              VIASH_PAR_INPUT="$2"
            else
              VIASH_PAR_INPUT="$VIASH_PAR_INPUT;""$2"
            fi
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --input. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --input=*)
            if [ -z "$VIASH_PAR_INPUT" ]; then
              VIASH_PAR_INPUT=$(ViashRemoveFlags "$1")
            else
              VIASH_PAR_INPUT="$VIASH_PAR_INPUT;"$(ViashRemoveFlags "$1")
            fi
            shift 1
            ;;
        --quality)
            [ -n "$VIASH_PAR_QUALITY" ] && ViashError Bad arguments for option \'--quality\': \'$VIASH_PAR_QUALITY\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_QUALITY="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --quality. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --quality=*)
            [ -n "$VIASH_PAR_QUALITY" ] && ViashError Bad arguments for option \'--quality=*\': \'$VIASH_PAR_QUALITY\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_QUALITY=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        -q)
            [ -n "$VIASH_PAR_QUALITY" ] && ViashError Bad arguments for option \'-q\': \'$VIASH_PAR_QUALITY\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_QUALITY="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to -q. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --phred33)
            [ -n "$VIASH_PAR_PHRED33" ] && ViashError Bad arguments for option \'--phred33\': \'$VIASH_PAR_PHRED33\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_PHRED33=true
            shift 1
            ;;
        --phred64)
            [ -n "$VIASH_PAR_PHRED64" ] && ViashError Bad arguments for option \'--phred64\': \'$VIASH_PAR_PHRED64\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_PHRED64=true
            shift 1
            ;;
        --fastqc)
            [ -n "$VIASH_PAR_FASTQC" ] && ViashError Bad arguments for option \'--fastqc\': \'$VIASH_PAR_FASTQC\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_FASTQC=true
            shift 1
            ;;
        --fastqc_args)
            [ -n "$VIASH_PAR_FASTQC_ARGS" ] && ViashError Bad arguments for option \'--fastqc_args\': \'$VIASH_PAR_FASTQC_ARGS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_FASTQC_ARGS="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --fastqc_args. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --fastqc_args=*)
            [ -n "$VIASH_PAR_FASTQC_ARGS" ] && ViashError Bad arguments for option \'--fastqc_args=*\': \'$VIASH_PAR_FASTQC_ARGS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_FASTQC_ARGS=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --fastqc_contaminants)
            [ -n "$VIASH_PAR_FASTQC_CONTAMINANTS" ] && ViashError Bad arguments for option \'--fastqc_contaminants\': \'$VIASH_PAR_FASTQC_CONTAMINANTS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_FASTQC_CONTAMINANTS="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --fastqc_contaminants. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --fastqc_contaminants=*)
            [ -n "$VIASH_PAR_FASTQC_CONTAMINANTS" ] && ViashError Bad arguments for option \'--fastqc_contaminants=*\': \'$VIASH_PAR_FASTQC_CONTAMINANTS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_FASTQC_CONTAMINANTS=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --fastqc_adapters)
            [ -n "$VIASH_PAR_FASTQC_ADAPTERS" ] && ViashError Bad arguments for option \'--fastqc_adapters\': \'$VIASH_PAR_FASTQC_ADAPTERS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_FASTQC_ADAPTERS="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --fastqc_adapters. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --fastqc_adapters=*)
            [ -n "$VIASH_PAR_FASTQC_ADAPTERS" ] && ViashError Bad arguments for option \'--fastqc_adapters=*\': \'$VIASH_PAR_FASTQC_ADAPTERS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_FASTQC_ADAPTERS=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --fastqc_limits)
            [ -n "$VIASH_PAR_FASTQC_LIMITS" ] && ViashError Bad arguments for option \'--fastqc_limits\': \'$VIASH_PAR_FASTQC_LIMITS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_FASTQC_LIMITS="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --fastqc_limits. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --fastqc_limits=*)
            [ -n "$VIASH_PAR_FASTQC_LIMITS" ] && ViashError Bad arguments for option \'--fastqc_limits=*\': \'$VIASH_PAR_FASTQC_LIMITS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_FASTQC_LIMITS=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --adapter)
            [ -n "$VIASH_PAR_ADAPTER" ] && ViashError Bad arguments for option \'--adapter\': \'$VIASH_PAR_ADAPTER\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_ADAPTER="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --adapter. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --adapter=*)
            [ -n "$VIASH_PAR_ADAPTER" ] && ViashError Bad arguments for option \'--adapter=*\': \'$VIASH_PAR_ADAPTER\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_ADAPTER=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        -a)
            [ -n "$VIASH_PAR_ADAPTER" ] && ViashError Bad arguments for option \'-a\': \'$VIASH_PAR_ADAPTER\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_ADAPTER="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to -a. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --adapter2)
            [ -n "$VIASH_PAR_ADAPTER2" ] && ViashError Bad arguments for option \'--adapter2\': \'$VIASH_PAR_ADAPTER2\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_ADAPTER2="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --adapter2. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --adapter2=*)
            [ -n "$VIASH_PAR_ADAPTER2" ] && ViashError Bad arguments for option \'--adapter2=*\': \'$VIASH_PAR_ADAPTER2\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_ADAPTER2=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        -a2)
            [ -n "$VIASH_PAR_ADAPTER2" ] && ViashError Bad arguments for option \'-a2\': \'$VIASH_PAR_ADAPTER2\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_ADAPTER2="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to -a2. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --illumina)
            [ -n "$VIASH_PAR_ILLUMINA" ] && ViashError Bad arguments for option \'--illumina\': \'$VIASH_PAR_ILLUMINA\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_ILLUMINA=true
            shift 1
            ;;
        --stranded_illumina)
            [ -n "$VIASH_PAR_STRANDED_ILLUMINA" ] && ViashError Bad arguments for option \'--stranded_illumina\': \'$VIASH_PAR_STRANDED_ILLUMINA\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_STRANDED_ILLUMINA=true
            shift 1
            ;;
        --nextera)
            [ -n "$VIASH_PAR_NEXTERA" ] && ViashError Bad arguments for option \'--nextera\': \'$VIASH_PAR_NEXTERA\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_NEXTERA=true
            shift 1
            ;;
        --small_rna)
            [ -n "$VIASH_PAR_SMALL_RNA" ] && ViashError Bad arguments for option \'--small_rna\': \'$VIASH_PAR_SMALL_RNA\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_SMALL_RNA=true
            shift 1
            ;;
        --consider_already_trimmed)
            [ -n "$VIASH_PAR_CONSIDER_ALREADY_TRIMMED" ] && ViashError Bad arguments for option \'--consider_already_trimmed\': \'$VIASH_PAR_CONSIDER_ALREADY_TRIMMED\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_CONSIDER_ALREADY_TRIMMED="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --consider_already_trimmed. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --consider_already_trimmed=*)
            [ -n "$VIASH_PAR_CONSIDER_ALREADY_TRIMMED" ] && ViashError Bad arguments for option \'--consider_already_trimmed=*\': \'$VIASH_PAR_CONSIDER_ALREADY_TRIMMED\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_CONSIDER_ALREADY_TRIMMED=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --max_length)
            [ -n "$VIASH_PAR_MAX_LENGTH" ] && ViashError Bad arguments for option \'--max_length\': \'$VIASH_PAR_MAX_LENGTH\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_MAX_LENGTH="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --max_length. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --max_length=*)
            [ -n "$VIASH_PAR_MAX_LENGTH" ] && ViashError Bad arguments for option \'--max_length=*\': \'$VIASH_PAR_MAX_LENGTH\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_MAX_LENGTH=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --stringency)
            [ -n "$VIASH_PAR_STRINGENCY" ] && ViashError Bad arguments for option \'--stringency\': \'$VIASH_PAR_STRINGENCY\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_STRINGENCY="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --stringency. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --stringency=*)
            [ -n "$VIASH_PAR_STRINGENCY" ] && ViashError Bad arguments for option \'--stringency=*\': \'$VIASH_PAR_STRINGENCY\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_STRINGENCY=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --error_rate)
            [ -n "$VIASH_PAR_ERROR_RATE" ] && ViashError Bad arguments for option \'--error_rate\': \'$VIASH_PAR_ERROR_RATE\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_ERROR_RATE="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --error_rate. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --error_rate=*)
            [ -n "$VIASH_PAR_ERROR_RATE" ] && ViashError Bad arguments for option \'--error_rate=*\': \'$VIASH_PAR_ERROR_RATE\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_ERROR_RATE=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        -e)
            [ -n "$VIASH_PAR_ERROR_RATE" ] && ViashError Bad arguments for option \'-e\': \'$VIASH_PAR_ERROR_RATE\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_ERROR_RATE="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to -e. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --gzip)
            [ -n "$VIASH_PAR_GZIP" ] && ViashError Bad arguments for option \'--gzip\': \'$VIASH_PAR_GZIP\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_GZIP=true
            shift 1
            ;;
        --dont_gzip)
            [ -n "$VIASH_PAR_DONT_GZIP" ] && ViashError Bad arguments for option \'--dont_gzip\': \'$VIASH_PAR_DONT_GZIP\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_DONT_GZIP=true
            shift 1
            ;;
        --length)
            [ -n "$VIASH_PAR_LENGTH" ] && ViashError Bad arguments for option \'--length\': \'$VIASH_PAR_LENGTH\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_LENGTH="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --length. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --length=*)
            [ -n "$VIASH_PAR_LENGTH" ] && ViashError Bad arguments for option \'--length=*\': \'$VIASH_PAR_LENGTH\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_LENGTH=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --max_n)
            [ -n "$VIASH_PAR_MAX_N" ] && ViashError Bad arguments for option \'--max_n\': \'$VIASH_PAR_MAX_N\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_MAX_N="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --max_n. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --max_n=*)
            [ -n "$VIASH_PAR_MAX_N" ] && ViashError Bad arguments for option \'--max_n=*\': \'$VIASH_PAR_MAX_N\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_MAX_N=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --trim_n)
            [ -n "$VIASH_PAR_TRIM_N" ] && ViashError Bad arguments for option \'--trim_n\': \'$VIASH_PAR_TRIM_N\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRIM_N=true
            shift 1
            ;;
        --no_report_file)
            [ -n "$VIASH_PAR_NO_REPORT_FILE" ] && ViashError Bad arguments for option \'--no_report_file\': \'$VIASH_PAR_NO_REPORT_FILE\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_NO_REPORT_FILE=true
            shift 1
            ;;
        --suppress_warn)
            [ -n "$VIASH_PAR_SUPPRESS_WARN" ] && ViashError Bad arguments for option \'--suppress_warn\': \'$VIASH_PAR_SUPPRESS_WARN\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_SUPPRESS_WARN=true
            shift 1
            ;;
        --clip_R1)
            [ -n "$VIASH_PAR_CLIP_R1" ] && ViashError Bad arguments for option \'--clip_R1\': \'$VIASH_PAR_CLIP_R1\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_CLIP_R1="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --clip_R1. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --clip_R1=*)
            [ -n "$VIASH_PAR_CLIP_R1" ] && ViashError Bad arguments for option \'--clip_R1=*\': \'$VIASH_PAR_CLIP_R1\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_CLIP_R1=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --clip_R2)
            [ -n "$VIASH_PAR_CLIP_R2" ] && ViashError Bad arguments for option \'--clip_R2\': \'$VIASH_PAR_CLIP_R2\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_CLIP_R2="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --clip_R2. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --clip_R2=*)
            [ -n "$VIASH_PAR_CLIP_R2" ] && ViashError Bad arguments for option \'--clip_R2=*\': \'$VIASH_PAR_CLIP_R2\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_CLIP_R2=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --three_prime_clip_R1)
            [ -n "$VIASH_PAR_THREE_PRIME_CLIP_R1" ] && ViashError Bad arguments for option \'--three_prime_clip_R1\': \'$VIASH_PAR_THREE_PRIME_CLIP_R1\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_THREE_PRIME_CLIP_R1="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --three_prime_clip_R1. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --three_prime_clip_R1=*)
            [ -n "$VIASH_PAR_THREE_PRIME_CLIP_R1" ] && ViashError Bad arguments for option \'--three_prime_clip_R1=*\': \'$VIASH_PAR_THREE_PRIME_CLIP_R1\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_THREE_PRIME_CLIP_R1=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --three_prime_clip_R2)
            [ -n "$VIASH_PAR_THREE_PRIME_CLIP_R2" ] && ViashError Bad arguments for option \'--three_prime_clip_R2\': \'$VIASH_PAR_THREE_PRIME_CLIP_R2\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_THREE_PRIME_CLIP_R2="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --three_prime_clip_R2. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --three_prime_clip_R2=*)
            [ -n "$VIASH_PAR_THREE_PRIME_CLIP_R2" ] && ViashError Bad arguments for option \'--three_prime_clip_R2=*\': \'$VIASH_PAR_THREE_PRIME_CLIP_R2\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_THREE_PRIME_CLIP_R2=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --nextseq)
            [ -n "$VIASH_PAR_NEXTSEQ" ] && ViashError Bad arguments for option \'--nextseq\': \'$VIASH_PAR_NEXTSEQ\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_NEXTSEQ="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --nextseq. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --nextseq=*)
            [ -n "$VIASH_PAR_NEXTSEQ" ] && ViashError Bad arguments for option \'--nextseq=*\': \'$VIASH_PAR_NEXTSEQ\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_NEXTSEQ=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --basename)
            [ -n "$VIASH_PAR_BASENAME" ] && ViashError Bad arguments for option \'--basename\': \'$VIASH_PAR_BASENAME\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_BASENAME="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --basename. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --basename=*)
            [ -n "$VIASH_PAR_BASENAME" ] && ViashError Bad arguments for option \'--basename=*\': \'$VIASH_PAR_BASENAME\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_BASENAME=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --hardtrim5)
            [ -n "$VIASH_PAR_HARDTRIM5" ] && ViashError Bad arguments for option \'--hardtrim5\': \'$VIASH_PAR_HARDTRIM5\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_HARDTRIM5="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --hardtrim5. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --hardtrim5=*)
            [ -n "$VIASH_PAR_HARDTRIM5" ] && ViashError Bad arguments for option \'--hardtrim5=*\': \'$VIASH_PAR_HARDTRIM5\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_HARDTRIM5=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --hardtrim3)
            [ -n "$VIASH_PAR_HARDTRIM3" ] && ViashError Bad arguments for option \'--hardtrim3\': \'$VIASH_PAR_HARDTRIM3\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_HARDTRIM3="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --hardtrim3. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --hardtrim3=*)
            [ -n "$VIASH_PAR_HARDTRIM3" ] && ViashError Bad arguments for option \'--hardtrim3=*\': \'$VIASH_PAR_HARDTRIM3\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_HARDTRIM3=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --clock)
            [ -n "$VIASH_PAR_CLOCK" ] && ViashError Bad arguments for option \'--clock\': \'$VIASH_PAR_CLOCK\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_CLOCK=true
            shift 1
            ;;
        --polyA)
            [ -n "$VIASH_PAR_POLYA" ] && ViashError Bad arguments for option \'--polyA\': \'$VIASH_PAR_POLYA\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_POLYA=true
            shift 1
            ;;
        --implicon)
            [ -n "$VIASH_PAR_IMPLICON" ] && ViashError Bad arguments for option \'--implicon\': \'$VIASH_PAR_IMPLICON\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_IMPLICON=true
            shift 1
            ;;
        --rrbs)
            [ -n "$VIASH_PAR_RRBS" ] && ViashError Bad arguments for option \'--rrbs\': \'$VIASH_PAR_RRBS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_RRBS=true
            shift 1
            ;;
        --non_directional)
            [ -n "$VIASH_PAR_NON_DIRECTIONAL" ] && ViashError Bad arguments for option \'--non_directional\': \'$VIASH_PAR_NON_DIRECTIONAL\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_NON_DIRECTIONAL=true
            shift 1
            ;;
        --keep)
            [ -n "$VIASH_PAR_KEEP" ] && ViashError Bad arguments for option \'--keep\': \'$VIASH_PAR_KEEP\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_KEEP=true
            shift 1
            ;;
        --paired)
            [ -n "$VIASH_PAR_PAIRED" ] && ViashError Bad arguments for option \'--paired\': \'$VIASH_PAR_PAIRED\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_PAIRED=true
            shift 1
            ;;
        --retain_unpaired)
            [ -n "$VIASH_PAR_RETAIN_UNPAIRED" ] && ViashError Bad arguments for option \'--retain_unpaired\': \'$VIASH_PAR_RETAIN_UNPAIRED\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_RETAIN_UNPAIRED=true
            shift 1
            ;;
        --length_1)
            [ -n "$VIASH_PAR_LENGTH_1" ] && ViashError Bad arguments for option \'--length_1\': \'$VIASH_PAR_LENGTH_1\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_LENGTH_1="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --length_1. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --length_1=*)
            [ -n "$VIASH_PAR_LENGTH_1" ] && ViashError Bad arguments for option \'--length_1=*\': \'$VIASH_PAR_LENGTH_1\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_LENGTH_1=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        -r1)
            [ -n "$VIASH_PAR_LENGTH_1" ] && ViashError Bad arguments for option \'-r1\': \'$VIASH_PAR_LENGTH_1\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_LENGTH_1="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to -r1. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --length_2)
            [ -n "$VIASH_PAR_LENGTH_2" ] && ViashError Bad arguments for option \'--length_2\': \'$VIASH_PAR_LENGTH_2\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_LENGTH_2="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --length_2. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --length_2=*)
            [ -n "$VIASH_PAR_LENGTH_2" ] && ViashError Bad arguments for option \'--length_2=*\': \'$VIASH_PAR_LENGTH_2\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_LENGTH_2=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        -r2)
            [ -n "$VIASH_PAR_LENGTH_2" ] && ViashError Bad arguments for option \'-r2\': \'$VIASH_PAR_LENGTH_2\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_LENGTH_2="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to -r2. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --output_dir)
            [ -n "$VIASH_PAR_OUTPUT_DIR" ] && ViashError Bad arguments for option \'--output_dir\': \'$VIASH_PAR_OUTPUT_DIR\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_OUTPUT_DIR="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --output_dir. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --output_dir=*)
            [ -n "$VIASH_PAR_OUTPUT_DIR" ] && ViashError Bad arguments for option \'--output_dir=*\': \'$VIASH_PAR_OUTPUT_DIR\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_OUTPUT_DIR=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        -o)
            [ -n "$VIASH_PAR_OUTPUT_DIR" ] && ViashError Bad arguments for option \'-o\': \'$VIASH_PAR_OUTPUT_DIR\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_OUTPUT_DIR="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to -o. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --trimmed_r1)
            [ -n "$VIASH_PAR_TRIMMED_R1" ] && ViashError Bad arguments for option \'--trimmed_r1\': \'$VIASH_PAR_TRIMMED_R1\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRIMMED_R1="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --trimmed_r1. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --trimmed_r1=*)
            [ -n "$VIASH_PAR_TRIMMED_R1" ] && ViashError Bad arguments for option \'--trimmed_r1=*\': \'$VIASH_PAR_TRIMMED_R1\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRIMMED_R1=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --trimmed_r2)
            [ -n "$VIASH_PAR_TRIMMED_R2" ] && ViashError Bad arguments for option \'--trimmed_r2\': \'$VIASH_PAR_TRIMMED_R2\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRIMMED_R2="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --trimmed_r2. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --trimmed_r2=*)
            [ -n "$VIASH_PAR_TRIMMED_R2" ] && ViashError Bad arguments for option \'--trimmed_r2=*\': \'$VIASH_PAR_TRIMMED_R2\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRIMMED_R2=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --trimming_report_r1)
            [ -n "$VIASH_PAR_TRIMMING_REPORT_R1" ] && ViashError Bad arguments for option \'--trimming_report_r1\': \'$VIASH_PAR_TRIMMING_REPORT_R1\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRIMMING_REPORT_R1="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --trimming_report_r1. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --trimming_report_r1=*)
            [ -n "$VIASH_PAR_TRIMMING_REPORT_R1" ] && ViashError Bad arguments for option \'--trimming_report_r1=*\': \'$VIASH_PAR_TRIMMING_REPORT_R1\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRIMMING_REPORT_R1=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --trimming_report_r2)
            [ -n "$VIASH_PAR_TRIMMING_REPORT_R2" ] && ViashError Bad arguments for option \'--trimming_report_r2\': \'$VIASH_PAR_TRIMMING_REPORT_R2\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRIMMING_REPORT_R2="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --trimming_report_r2. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --trimming_report_r2=*)
            [ -n "$VIASH_PAR_TRIMMING_REPORT_R2" ] && ViashError Bad arguments for option \'--trimming_report_r2=*\': \'$VIASH_PAR_TRIMMING_REPORT_R2\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRIMMING_REPORT_R2=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --trimmed_fastqc_html_1)
            [ -n "$VIASH_PAR_TRIMMED_FASTQC_HTML_1" ] && ViashError Bad arguments for option \'--trimmed_fastqc_html_1\': \'$VIASH_PAR_TRIMMED_FASTQC_HTML_1\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRIMMED_FASTQC_HTML_1="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --trimmed_fastqc_html_1. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --trimmed_fastqc_html_1=*)
            [ -n "$VIASH_PAR_TRIMMED_FASTQC_HTML_1" ] && ViashError Bad arguments for option \'--trimmed_fastqc_html_1=*\': \'$VIASH_PAR_TRIMMED_FASTQC_HTML_1\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRIMMED_FASTQC_HTML_1=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --trimmed_fastqc_html_2)
            [ -n "$VIASH_PAR_TRIMMED_FASTQC_HTML_2" ] && ViashError Bad arguments for option \'--trimmed_fastqc_html_2\': \'$VIASH_PAR_TRIMMED_FASTQC_HTML_2\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRIMMED_FASTQC_HTML_2="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --trimmed_fastqc_html_2. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --trimmed_fastqc_html_2=*)
            [ -n "$VIASH_PAR_TRIMMED_FASTQC_HTML_2" ] && ViashError Bad arguments for option \'--trimmed_fastqc_html_2=*\': \'$VIASH_PAR_TRIMMED_FASTQC_HTML_2\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRIMMED_FASTQC_HTML_2=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --trimmed_fastqc_zip_1)
            [ -n "$VIASH_PAR_TRIMMED_FASTQC_ZIP_1" ] && ViashError Bad arguments for option \'--trimmed_fastqc_zip_1\': \'$VIASH_PAR_TRIMMED_FASTQC_ZIP_1\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRIMMED_FASTQC_ZIP_1="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --trimmed_fastqc_zip_1. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --trimmed_fastqc_zip_1=*)
            [ -n "$VIASH_PAR_TRIMMED_FASTQC_ZIP_1" ] && ViashError Bad arguments for option \'--trimmed_fastqc_zip_1=*\': \'$VIASH_PAR_TRIMMED_FASTQC_ZIP_1\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRIMMED_FASTQC_ZIP_1=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --trimmed_fastqc_zip_2)
            [ -n "$VIASH_PAR_TRIMMED_FASTQC_ZIP_2" ] && ViashError Bad arguments for option \'--trimmed_fastqc_zip_2\': \'$VIASH_PAR_TRIMMED_FASTQC_ZIP_2\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRIMMED_FASTQC_ZIP_2="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --trimmed_fastqc_zip_2. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --trimmed_fastqc_zip_2=*)
            [ -n "$VIASH_PAR_TRIMMED_FASTQC_ZIP_2" ] && ViashError Bad arguments for option \'--trimmed_fastqc_zip_2=*\': \'$VIASH_PAR_TRIMMED_FASTQC_ZIP_2\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_TRIMMED_FASTQC_ZIP_2=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --unpaired_r1)
            [ -n "$VIASH_PAR_UNPAIRED_R1" ] && ViashError Bad arguments for option \'--unpaired_r1\': \'$VIASH_PAR_UNPAIRED_R1\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_UNPAIRED_R1="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --unpaired_r1. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --unpaired_r1=*)
            [ -n "$VIASH_PAR_UNPAIRED_R1" ] && ViashError Bad arguments for option \'--unpaired_r1=*\': \'$VIASH_PAR_UNPAIRED_R1\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_UNPAIRED_R1=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --unpaired_r2)
            [ -n "$VIASH_PAR_UNPAIRED_R2" ] && ViashError Bad arguments for option \'--unpaired_r2\': \'$VIASH_PAR_UNPAIRED_R2\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_UNPAIRED_R2="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --unpaired_r2. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --unpaired_r2=*)
            [ -n "$VIASH_PAR_UNPAIRED_R2" ] && ViashError Bad arguments for option \'--unpaired_r2=*\': \'$VIASH_PAR_UNPAIRED_R2\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_UNPAIRED_R2=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        ---engine)
            VIASH_ENGINE_ID="$2"
            shift 2
            ;;
        ---engine=*)
            VIASH_ENGINE_ID="$(ViashRemoveFlags "$1")"
            shift 1
            ;;
        ---setup)
            VIASH_MODE='setup'
            VIASH_SETUP_STRATEGY="$2"
            shift 2
            ;;
        ---setup=*)
            VIASH_MODE='setup'
            VIASH_SETUP_STRATEGY="$(ViashRemoveFlags "$1")"
            shift 1
            ;;
        ---dockerfile)
            VIASH_MODE='dockerfile'
            shift 1
            ;;
        ---docker_run_args)
            VIASH_DOCKER_RUN_ARGS+=("$2")
            shift 2
            ;;
        ---docker_run_args=*)
            VIASH_DOCKER_RUN_ARGS+=("$(ViashRemoveFlags "$1")")
            shift 1
            ;;
        ---docker_image_id)
            VIASH_MODE='docker_image_id'
            shift 1
            ;;
        ---debug)
            VIASH_MODE='debug'
            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'
elif   [ "$VIASH_ENGINE_ID" == "docker" ]  ; then
  VIASH_ENGINE_TYPE='docker'
else
  ViashError "Engine '$VIASH_ENGINE_ID' is not recognized. Options are: docker, native."
  exit 1
fi

if [[ "$VIASH_ENGINE_TYPE" == "docker" ]]; then
  # check if docker is installed properly
  ViashDockerInstallationCheck

  # determine docker image id
  if [[ "$VIASH_ENGINE_ID" == 'docker' ]]; then
    VIASH_DOCKER_IMAGE_ID='images.viash-hub.com/vsh/biobox/trimgalore:spaceranger_count'
  fi

  # print dockerfile
  if [ "$VIASH_MODE" == "dockerfile" ]; then
    ViashDockerfile "$VIASH_ENGINE_ID"
    exit 0

  elif [ "$VIASH_MODE" == "docker_image_id" ]; then
    echo "$VIASH_DOCKER_IMAGE_ID"
    exit 0
  
  # enter docker container
  elif [[ "$VIASH_MODE" == "debug" ]]; then
    VIASH_CMD="docker run --entrypoint=bash ${VIASH_DOCKER_RUN_ARGS[@]} -v '$(pwd)':/pwd --workdir /pwd -t $VIASH_DOCKER_IMAGE_ID"
    ViashNotice "+ $VIASH_CMD"
    eval $VIASH_CMD
    exit 

  # build docker image
  elif [ "$VIASH_MODE" == "setup" ]; then
    ViashDockerSetup "$VIASH_DOCKER_IMAGE_ID" "$VIASH_SETUP_STRATEGY"
    ViashDockerCheckCommands "$VIASH_DOCKER_IMAGE_ID" 'ps' 'bash'
    exit 0
  fi

  # check if docker image exists
  ViashDockerSetup "$VIASH_DOCKER_IMAGE_ID" ifneedbepullelsecachedbuild
  ViashDockerCheckCommands "$VIASH_DOCKER_IMAGE_ID" 'ps' 'bash'
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_INPUT+x} ]; then
  ViashError '--input' is a required argument. Use "--help" to get more information on the parameters.
  exit 1
fi
if [ -z ${VIASH_PAR_OUTPUT_DIR+x} ]; then
  ViashError '--output_dir' 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_PHRED33+x} ]; then
  VIASH_PAR_PHRED33="false"
fi
if [ -z ${VIASH_PAR_PHRED64+x} ]; then
  VIASH_PAR_PHRED64="false"
fi
if [ -z ${VIASH_PAR_FASTQC+x} ]; then
  VIASH_PAR_FASTQC="false"
fi
if [ -z ${VIASH_PAR_ILLUMINA+x} ]; then
  VIASH_PAR_ILLUMINA="false"
fi
if [ -z ${VIASH_PAR_STRANDED_ILLUMINA+x} ]; then
  VIASH_PAR_STRANDED_ILLUMINA="false"
fi
if [ -z ${VIASH_PAR_NEXTERA+x} ]; then
  VIASH_PAR_NEXTERA="false"
fi
if [ -z ${VIASH_PAR_SMALL_RNA+x} ]; then
  VIASH_PAR_SMALL_RNA="false"
fi
if [ -z ${VIASH_PAR_GZIP+x} ]; then
  VIASH_PAR_GZIP="false"
fi
if [ -z ${VIASH_PAR_DONT_GZIP+x} ]; then
  VIASH_PAR_DONT_GZIP="false"
fi
if [ -z ${VIASH_PAR_TRIM_N+x} ]; then
  VIASH_PAR_TRIM_N="false"
fi
if [ -z ${VIASH_PAR_NO_REPORT_FILE+x} ]; then
  VIASH_PAR_NO_REPORT_FILE="false"
fi
if [ -z ${VIASH_PAR_SUPPRESS_WARN+x} ]; then
  VIASH_PAR_SUPPRESS_WARN="false"
fi
if [ -z ${VIASH_PAR_CLOCK+x} ]; then
  VIASH_PAR_CLOCK="false"
fi
if [ -z ${VIASH_PAR_POLYA+x} ]; then
  VIASH_PAR_POLYA="false"
fi
if [ -z ${VIASH_PAR_IMPLICON+x} ]; then
  VIASH_PAR_IMPLICON="false"
fi
if [ -z ${VIASH_PAR_RRBS+x} ]; then
  VIASH_PAR_RRBS="false"
fi
if [ -z ${VIASH_PAR_NON_DIRECTIONAL+x} ]; then
  VIASH_PAR_NON_DIRECTIONAL="false"
fi
if [ -z ${VIASH_PAR_KEEP+x} ]; then
  VIASH_PAR_KEEP="false"
fi
if [ -z ${VIASH_PAR_PAIRED+x} ]; then
  VIASH_PAR_PAIRED="false"
fi
if [ -z ${VIASH_PAR_RETAIN_UNPAIRED+x} ]; then
  VIASH_PAR_RETAIN_UNPAIRED="false"
fi

# check whether required files exist
if [ ! -z "$VIASH_PAR_INPUT" ]; then
  IFS=';'
  set -f
  for file in $VIASH_PAR_INPUT; 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_FASTQC_CONTAMINANTS" ] && [ ! -e "$VIASH_PAR_FASTQC_CONTAMINANTS" ]; then
  ViashError "Input file '$VIASH_PAR_FASTQC_CONTAMINANTS' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_FASTQC_ADAPTERS" ] && [ ! -e "$VIASH_PAR_FASTQC_ADAPTERS" ]; then
  ViashError "Input file '$VIASH_PAR_FASTQC_ADAPTERS' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_FASTQC_LIMITS" ] && [ ! -e "$VIASH_PAR_FASTQC_LIMITS" ]; then
  ViashError "Input file '$VIASH_PAR_FASTQC_LIMITS' does not exist."
  exit 1
fi

# check whether parameters values are of the right type
if [[ -n "$VIASH_PAR_QUALITY" ]]; then
  if ! [[ "$VIASH_PAR_QUALITY" =~ ^[-+]?[0-9]+$ ]]; then
    ViashError '--quality' has to be an integer. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_PHRED33" ]]; then
  if ! [[ "$VIASH_PAR_PHRED33" =~ ^(true|True|TRUE|false|False|FALSE|yes|Yes|YES|no|No|NO)$ ]]; then
    ViashError '--phred33' has to be a boolean_true. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_PHRED64" ]]; then
  if ! [[ "$VIASH_PAR_PHRED64" =~ ^(true|True|TRUE|false|False|FALSE|yes|Yes|YES|no|No|NO)$ ]]; then
    ViashError '--phred64' has to be a boolean_true. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_FASTQC" ]]; then
  if ! [[ "$VIASH_PAR_FASTQC" =~ ^(true|True|TRUE|false|False|FALSE|yes|Yes|YES|no|No|NO)$ ]]; then
    ViashError '--fastqc' has to be a boolean_true. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_ILLUMINA" ]]; then
  if ! [[ "$VIASH_PAR_ILLUMINA" =~ ^(true|True|TRUE|false|False|FALSE|yes|Yes|YES|no|No|NO)$ ]]; then
    ViashError '--illumina' has to be a boolean_true. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_STRANDED_ILLUMINA" ]]; then
  if ! [[ "$VIASH_PAR_STRANDED_ILLUMINA" =~ ^(true|True|TRUE|false|False|FALSE|yes|Yes|YES|no|No|NO)$ ]]; then
    ViashError '--stranded_illumina' has to be a boolean_true. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_NEXTERA" ]]; then
  if ! [[ "$VIASH_PAR_NEXTERA" =~ ^(true|True|TRUE|false|False|FALSE|yes|Yes|YES|no|No|NO)$ ]]; then
    ViashError '--nextera' has to be a boolean_true. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_SMALL_RNA" ]]; then
  if ! [[ "$VIASH_PAR_SMALL_RNA" =~ ^(true|True|TRUE|false|False|FALSE|yes|Yes|YES|no|No|NO)$ ]]; then
    ViashError '--small_rna' has to be a boolean_true. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_CONSIDER_ALREADY_TRIMMED" ]]; then
  if ! [[ "$VIASH_PAR_CONSIDER_ALREADY_TRIMMED" =~ ^[-+]?[0-9]+$ ]]; then
    ViashError '--consider_already_trimmed' has to be an integer. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_MAX_LENGTH" ]]; then
  if ! [[ "$VIASH_PAR_MAX_LENGTH" =~ ^[-+]?[0-9]+$ ]]; then
    ViashError '--max_length' has to be an integer. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_STRINGENCY" ]]; then
  if ! [[ "$VIASH_PAR_STRINGENCY" =~ ^[-+]?[0-9]+$ ]]; then
    ViashError '--stringency' has to be an integer. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_ERROR_RATE" ]]; then
  if ! [[ "$VIASH_PAR_ERROR_RATE" =~ ^[-+]?(\.[0-9]+|[0-9]+(\.[0-9]*)?)([eE][-+]?[0-9]+)?$ ]]; then
    ViashError '--error_rate' has to be a double. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_GZIP" ]]; then
  if ! [[ "$VIASH_PAR_GZIP" =~ ^(true|True|TRUE|false|False|FALSE|yes|Yes|YES|no|No|NO)$ ]]; then
    ViashError '--gzip' has to be a boolean_true. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_DONT_GZIP" ]]; then
  if ! [[ "$VIASH_PAR_DONT_GZIP" =~ ^(true|True|TRUE|false|False|FALSE|yes|Yes|YES|no|No|NO)$ ]]; then
    ViashError '--dont_gzip' has to be a boolean_true. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_LENGTH" ]]; then
  if ! [[ "$VIASH_PAR_LENGTH" =~ ^[-+]?[0-9]+$ ]]; then
    ViashError '--length' has to be an integer. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_MAX_N" ]]; then
  if ! [[ "$VIASH_PAR_MAX_N" =~ ^[-+]?[0-9]+$ ]]; then
    ViashError '--max_n' has to be an integer. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_TRIM_N" ]]; then
  if ! [[ "$VIASH_PAR_TRIM_N" =~ ^(true|True|TRUE|false|False|FALSE|yes|Yes|YES|no|No|NO)$ ]]; then
    ViashError '--trim_n' has to be a boolean_true. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_NO_REPORT_FILE" ]]; then
  if ! [[ "$VIASH_PAR_NO_REPORT_FILE" =~ ^(true|True|TRUE|false|False|FALSE|yes|Yes|YES|no|No|NO)$ ]]; then
    ViashError '--no_report_file' has to be a boolean_true. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_SUPPRESS_WARN" ]]; then
  if ! [[ "$VIASH_PAR_SUPPRESS_WARN" =~ ^(true|True|TRUE|false|False|FALSE|yes|Yes|YES|no|No|NO)$ ]]; then
    ViashError '--suppress_warn' has to be a boolean_true. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_CLIP_R1" ]]; then
  if ! [[ "$VIASH_PAR_CLIP_R1" =~ ^[-+]?[0-9]+$ ]]; then
    ViashError '--clip_R1' has to be an integer. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_CLIP_R2" ]]; then
  if ! [[ "$VIASH_PAR_CLIP_R2" =~ ^[-+]?[0-9]+$ ]]; then
    ViashError '--clip_R2' has to be an integer. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_THREE_PRIME_CLIP_R1" ]]; then
  if ! [[ "$VIASH_PAR_THREE_PRIME_CLIP_R1" =~ ^[-+]?[0-9]+$ ]]; then
    ViashError '--three_prime_clip_R1' has to be an integer. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_THREE_PRIME_CLIP_R2" ]]; then
  if ! [[ "$VIASH_PAR_THREE_PRIME_CLIP_R2" =~ ^[-+]?[0-9]+$ ]]; then
    ViashError '--three_prime_clip_R2' has to be an integer. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_NEXTSEQ" ]]; then
  if ! [[ "$VIASH_PAR_NEXTSEQ" =~ ^[-+]?[0-9]+$ ]]; then
    ViashError '--nextseq' has to be an integer. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_HARDTRIM5" ]]; then
  if ! [[ "$VIASH_PAR_HARDTRIM5" =~ ^[-+]?[0-9]+$ ]]; then
    ViashError '--hardtrim5' has to be an integer. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_HARDTRIM3" ]]; then
  if ! [[ "$VIASH_PAR_HARDTRIM3" =~ ^[-+]?[0-9]+$ ]]; then
    ViashError '--hardtrim3' has to be an integer. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_CLOCK" ]]; then
  if ! [[ "$VIASH_PAR_CLOCK" =~ ^(true|True|TRUE|false|False|FALSE|yes|Yes|YES|no|No|NO)$ ]]; then
    ViashError '--clock' has to be a boolean_true. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_POLYA" ]]; then
  if ! [[ "$VIASH_PAR_POLYA" =~ ^(true|True|TRUE|false|False|FALSE|yes|Yes|YES|no|No|NO)$ ]]; then
    ViashError '--polyA' has to be a boolean_true. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_IMPLICON" ]]; then
  if ! [[ "$VIASH_PAR_IMPLICON" =~ ^(true|True|TRUE|false|False|FALSE|yes|Yes|YES|no|No|NO)$ ]]; then
    ViashError '--implicon' has to be a boolean_true. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_RRBS" ]]; then
  if ! [[ "$VIASH_PAR_RRBS" =~ ^(true|True|TRUE|false|False|FALSE|yes|Yes|YES|no|No|NO)$ ]]; then
    ViashError '--rrbs' has to be a boolean_true. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_NON_DIRECTIONAL" ]]; then
  if ! [[ "$VIASH_PAR_NON_DIRECTIONAL" =~ ^(true|True|TRUE|false|False|FALSE|yes|Yes|YES|no|No|NO)$ ]]; then
    ViashError '--non_directional' has to be a boolean_true. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_KEEP" ]]; then
  if ! [[ "$VIASH_PAR_KEEP" =~ ^(true|True|TRUE|false|False|FALSE|yes|Yes|YES|no|No|NO)$ ]]; then
    ViashError '--keep' has to be a boolean_true. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_PAIRED" ]]; then
  if ! [[ "$VIASH_PAR_PAIRED" =~ ^(true|True|TRUE|false|False|FALSE|yes|Yes|YES|no|No|NO)$ ]]; then
    ViashError '--paired' has to be a boolean_true. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_RETAIN_UNPAIRED" ]]; then
  if ! [[ "$VIASH_PAR_RETAIN_UNPAIRED" =~ ^(true|True|TRUE|false|False|FALSE|yes|Yes|YES|no|No|NO)$ ]]; then
    ViashError '--retain_unpaired' has to be a boolean_true. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_LENGTH_1" ]]; then
  if ! [[ "$VIASH_PAR_LENGTH_1" =~ ^[-+]?[0-9]+$ ]]; then
    ViashError '--length_1' has to be an integer. Use "--help" to get more information on the parameters.
    exit 1
  fi
fi
if [[ -n "$VIASH_PAR_LENGTH_2" ]]; then
  if ! [[ "$VIASH_PAR_LENGTH_2" =~ ^[-+]?[0-9]+$ ]]; then
    ViashError '--length_2' has to be an integer. 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

# create parent directories of output files, if so desired
if [ ! -z "$VIASH_PAR_OUTPUT_DIR" ] && [ ! -d "$(dirname "$VIASH_PAR_OUTPUT_DIR")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_OUTPUT_DIR")"
fi
if [ ! -z "$VIASH_PAR_TRIMMED_R1" ] && [ ! -d "$(dirname "$VIASH_PAR_TRIMMED_R1")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_TRIMMED_R1")"
fi
if [ ! -z "$VIASH_PAR_TRIMMED_R2" ] && [ ! -d "$(dirname "$VIASH_PAR_TRIMMED_R2")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_TRIMMED_R2")"
fi
if [ ! -z "$VIASH_PAR_TRIMMING_REPORT_R1" ] && [ ! -d "$(dirname "$VIASH_PAR_TRIMMING_REPORT_R1")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_TRIMMING_REPORT_R1")"
fi
if [ ! -z "$VIASH_PAR_TRIMMING_REPORT_R2" ] && [ ! -d "$(dirname "$VIASH_PAR_TRIMMING_REPORT_R2")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_TRIMMING_REPORT_R2")"
fi
if [ ! -z "$VIASH_PAR_TRIMMED_FASTQC_HTML_1" ] && [ ! -d "$(dirname "$VIASH_PAR_TRIMMED_FASTQC_HTML_1")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_TRIMMED_FASTQC_HTML_1")"
fi
if [ ! -z "$VIASH_PAR_TRIMMED_FASTQC_HTML_2" ] && [ ! -d "$(dirname "$VIASH_PAR_TRIMMED_FASTQC_HTML_2")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_TRIMMED_FASTQC_HTML_2")"
fi
if [ ! -z "$VIASH_PAR_TRIMMED_FASTQC_ZIP_1" ] && [ ! -d "$(dirname "$VIASH_PAR_TRIMMED_FASTQC_ZIP_1")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_TRIMMED_FASTQC_ZIP_1")"
fi
if [ ! -z "$VIASH_PAR_TRIMMED_FASTQC_ZIP_2" ] && [ ! -d "$(dirname "$VIASH_PAR_TRIMMED_FASTQC_ZIP_2")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_TRIMMED_FASTQC_ZIP_2")"
fi
if [ ! -z "$VIASH_PAR_UNPAIRED_R1" ] && [ ! -d "$(dirname "$VIASH_PAR_UNPAIRED_R1")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_UNPAIRED_R1")"
fi
if [ ! -z "$VIASH_PAR_UNPAIRED_R2" ] && [ ! -d "$(dirname "$VIASH_PAR_UNPAIRED_R2")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_UNPAIRED_R2")"
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

if [[ "$VIASH_ENGINE_TYPE" == "docker" ]]; then
  # detect volumes from file arguments
  VIASH_CHOWN_VARS=()
if [ ! -z "$VIASH_PAR_INPUT" ]; then
  VIASH_TEST_INPUT=()
  IFS=';'
  for var in $VIASH_PAR_INPUT; do
    unset IFS
    VIASH_DIRECTORY_MOUNTS+=( "$(ViashDockerAutodetectMountArg "$var")" )
    var=$(ViashDockerAutodetectMount "$var")
    VIASH_TEST_INPUT+=( "$var" )
  done
  VIASH_PAR_INPUT=$(IFS=';' ; echo "${VIASH_TEST_INPUT[*]}")
fi
if [ ! -z "$VIASH_PAR_FASTQC_CONTAMINANTS" ]; then
  VIASH_DIRECTORY_MOUNTS+=( "$(ViashDockerAutodetectMountArg "$VIASH_PAR_FASTQC_CONTAMINANTS")" )
  VIASH_PAR_FASTQC_CONTAMINANTS=$(ViashDockerAutodetectMount "$VIASH_PAR_FASTQC_CONTAMINANTS")
fi
if [ ! -z "$VIASH_PAR_FASTQC_ADAPTERS" ]; then
  VIASH_DIRECTORY_MOUNTS+=( "$(ViashDockerAutodetectMountArg "$VIASH_PAR_FASTQC_ADAPTERS")" )
  VIASH_PAR_FASTQC_ADAPTERS=$(ViashDockerAutodetectMount "$VIASH_PAR_FASTQC_ADAPTERS")
fi
if [ ! -z "$VIASH_PAR_FASTQC_LIMITS" ]; then
  VIASH_DIRECTORY_MOUNTS+=( "$(ViashDockerAutodetectMountArg "$VIASH_PAR_FASTQC_LIMITS")" )
  VIASH_PAR_FASTQC_LIMITS=$(ViashDockerAutodetectMount "$VIASH_PAR_FASTQC_LIMITS")
fi
if [ ! -z "$VIASH_PAR_OUTPUT_DIR" ]; then
  VIASH_DIRECTORY_MOUNTS+=( "$(ViashDockerAutodetectMountArg "$VIASH_PAR_OUTPUT_DIR")" )
  VIASH_PAR_OUTPUT_DIR=$(ViashDockerAutodetectMount "$VIASH_PAR_OUTPUT_DIR")
  VIASH_CHOWN_VARS+=( "$VIASH_PAR_OUTPUT_DIR" )
fi
if [ ! -z "$VIASH_PAR_TRIMMED_R1" ]; then
  VIASH_DIRECTORY_MOUNTS+=( "$(ViashDockerAutodetectMountArg "$VIASH_PAR_TRIMMED_R1")" )
  VIASH_PAR_TRIMMED_R1=$(ViashDockerAutodetectMount "$VIASH_PAR_TRIMMED_R1")
  VIASH_CHOWN_VARS+=( "$VIASH_PAR_TRIMMED_R1" )
fi
if [ ! -z "$VIASH_PAR_TRIMMED_R2" ]; then
  VIASH_DIRECTORY_MOUNTS+=( "$(ViashDockerAutodetectMountArg "$VIASH_PAR_TRIMMED_R2")" )
  VIASH_PAR_TRIMMED_R2=$(ViashDockerAutodetectMount "$VIASH_PAR_TRIMMED_R2")
  VIASH_CHOWN_VARS+=( "$VIASH_PAR_TRIMMED_R2" )
fi
if [ ! -z "$VIASH_PAR_TRIMMING_REPORT_R1" ]; then
  VIASH_DIRECTORY_MOUNTS+=( "$(ViashDockerAutodetectMountArg "$VIASH_PAR_TRIMMING_REPORT_R1")" )
  VIASH_PAR_TRIMMING_REPORT_R1=$(ViashDockerAutodetectMount "$VIASH_PAR_TRIMMING_REPORT_R1")
  VIASH_CHOWN_VARS+=( "$VIASH_PAR_TRIMMING_REPORT_R1" )
fi
if [ ! -z "$VIASH_PAR_TRIMMING_REPORT_R2" ]; then
  VIASH_DIRECTORY_MOUNTS+=( "$(ViashDockerAutodetectMountArg "$VIASH_PAR_TRIMMING_REPORT_R2")" )
  VIASH_PAR_TRIMMING_REPORT_R2=$(ViashDockerAutodetectMount "$VIASH_PAR_TRIMMING_REPORT_R2")
  VIASH_CHOWN_VARS+=( "$VIASH_PAR_TRIMMING_REPORT_R2" )
fi
if [ ! -z "$VIASH_PAR_TRIMMED_FASTQC_HTML_1" ]; then
  VIASH_DIRECTORY_MOUNTS+=( "$(ViashDockerAutodetectMountArg "$VIASH_PAR_TRIMMED_FASTQC_HTML_1")" )
  VIASH_PAR_TRIMMED_FASTQC_HTML_1=$(ViashDockerAutodetectMount "$VIASH_PAR_TRIMMED_FASTQC_HTML_1")
  VIASH_CHOWN_VARS+=( "$VIASH_PAR_TRIMMED_FASTQC_HTML_1" )
fi
if [ ! -z "$VIASH_PAR_TRIMMED_FASTQC_HTML_2" ]; then
  VIASH_DIRECTORY_MOUNTS+=( "$(ViashDockerAutodetectMountArg "$VIASH_PAR_TRIMMED_FASTQC_HTML_2")" )
  VIASH_PAR_TRIMMED_FASTQC_HTML_2=$(ViashDockerAutodetectMount "$VIASH_PAR_TRIMMED_FASTQC_HTML_2")
  VIASH_CHOWN_VARS+=( "$VIASH_PAR_TRIMMED_FASTQC_HTML_2" )
fi
if [ ! -z "$VIASH_PAR_TRIMMED_FASTQC_ZIP_1" ]; then
  VIASH_DIRECTORY_MOUNTS+=( "$(ViashDockerAutodetectMountArg "$VIASH_PAR_TRIMMED_FASTQC_ZIP_1")" )
  VIASH_PAR_TRIMMED_FASTQC_ZIP_1=$(ViashDockerAutodetectMount "$VIASH_PAR_TRIMMED_FASTQC_ZIP_1")
  VIASH_CHOWN_VARS+=( "$VIASH_PAR_TRIMMED_FASTQC_ZIP_1" )
fi
if [ ! -z "$VIASH_PAR_TRIMMED_FASTQC_ZIP_2" ]; then
  VIASH_DIRECTORY_MOUNTS+=( "$(ViashDockerAutodetectMountArg "$VIASH_PAR_TRIMMED_FASTQC_ZIP_2")" )
  VIASH_PAR_TRIMMED_FASTQC_ZIP_2=$(ViashDockerAutodetectMount "$VIASH_PAR_TRIMMED_FASTQC_ZIP_2")
  VIASH_CHOWN_VARS+=( "$VIASH_PAR_TRIMMED_FASTQC_ZIP_2" )
fi
if [ ! -z "$VIASH_PAR_UNPAIRED_R1" ]; then
  VIASH_DIRECTORY_MOUNTS+=( "$(ViashDockerAutodetectMountArg "$VIASH_PAR_UNPAIRED_R1")" )
  VIASH_PAR_UNPAIRED_R1=$(ViashDockerAutodetectMount "$VIASH_PAR_UNPAIRED_R1")
  VIASH_CHOWN_VARS+=( "$VIASH_PAR_UNPAIRED_R1" )
fi
if [ ! -z "$VIASH_PAR_UNPAIRED_R2" ]; then
  VIASH_DIRECTORY_MOUNTS+=( "$(ViashDockerAutodetectMountArg "$VIASH_PAR_UNPAIRED_R2")" )
  VIASH_PAR_UNPAIRED_R2=$(ViashDockerAutodetectMount "$VIASH_PAR_UNPAIRED_R2")
  VIASH_CHOWN_VARS+=( "$VIASH_PAR_UNPAIRED_R2" )
fi
if [ ! -z "$VIASH_META_RESOURCES_DIR" ]; then
  VIASH_DIRECTORY_MOUNTS+=( "$(ViashDockerAutodetectMountArg "$VIASH_META_RESOURCES_DIR")" )
  VIASH_META_RESOURCES_DIR=$(ViashDockerAutodetectMount "$VIASH_META_RESOURCES_DIR")
fi
if [ ! -z "$VIASH_META_EXECUTABLE" ]; then
  VIASH_DIRECTORY_MOUNTS+=( "$(ViashDockerAutodetectMountArg "$VIASH_META_EXECUTABLE")" )
  VIASH_META_EXECUTABLE=$(ViashDockerAutodetectMount "$VIASH_META_EXECUTABLE")
fi
if [ ! -z "$VIASH_META_CONFIG" ]; then
  VIASH_DIRECTORY_MOUNTS+=( "$(ViashDockerAutodetectMountArg "$VIASH_META_CONFIG")" )
  VIASH_META_CONFIG=$(ViashDockerAutodetectMount "$VIASH_META_CONFIG")
fi
if [ ! -z "$VIASH_META_TEMP_DIR" ]; then
  VIASH_DIRECTORY_MOUNTS+=( "$(ViashDockerAutodetectMountArg "$VIASH_META_TEMP_DIR")" )
  VIASH_META_TEMP_DIR=$(ViashDockerAutodetectMount "$VIASH_META_TEMP_DIR")
fi
  
  # get unique mounts
  VIASH_UNIQUE_MOUNTS=($(for val in "${VIASH_DIRECTORY_MOUNTS[@]}"; do echo "$val"; done | sort -u))
fi

if [[ "$VIASH_ENGINE_TYPE" == "docker" ]]; then
  # change file ownership
  function ViashPerformChown {
    if (( ${#VIASH_CHOWN_VARS[@]} )); then
      set +e
      VIASH_CMD="docker run --entrypoint=bash --rm ${VIASH_UNIQUE_MOUNTS[@]} $VIASH_DOCKER_IMAGE_ID -c 'chown $(id -u):$(id -g) --silent --recursive ${VIASH_CHOWN_VARS[@]}'"
      ViashDebug "+ $VIASH_CMD"
      eval $VIASH_CMD
      set -e
    fi
  }
  trap ViashPerformChown EXIT
fi

if [[ "$VIASH_ENGINE_TYPE" == "docker" ]]; then
  # helper function for filling in extra docker args
  if [ ! -z "$VIASH_META_MEMORY_B" ]; then
    VIASH_DOCKER_RUN_ARGS+=("--memory=${VIASH_META_MEMORY_B}")
  fi
  if [ ! -z "$VIASH_META_CPUS" ]; then
    VIASH_DOCKER_RUN_ARGS+=("--cpus=${VIASH_META_CPUS}")
  fi
fi

if [[ "$VIASH_ENGINE_TYPE" == "docker" ]]; then
  VIASH_CMD="docker run --entrypoint=bash ${VIASH_DOCKER_RUN_ARGS[@]} ${VIASH_UNIQUE_MOUNTS[@]} $VIASH_DOCKER_IMAGE_ID"
fi


# set dependency paths


ViashDebug "Running command: $(echo $VIASH_CMD)"
cat << VIASHEOF | eval $VIASH_CMD
set -e
tempscript=\$(mktemp "$VIASH_META_TEMP_DIR/viash-run-trimgalore-XXXXXX").sh
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.
$( if [ ! -z ${VIASH_PAR_INPUT+x} ]; then echo "${VIASH_PAR_INPUT}" | sed "s#'#'\"'\"'#g;s#.*#par_input='&'#" ; else echo "# par_input="; fi )
$( if [ ! -z ${VIASH_PAR_QUALITY+x} ]; then echo "${VIASH_PAR_QUALITY}" | sed "s#'#'\"'\"'#g;s#.*#par_quality='&'#" ; else echo "# par_quality="; fi )
$( if [ ! -z ${VIASH_PAR_PHRED33+x} ]; then echo "${VIASH_PAR_PHRED33}" | sed "s#'#'\"'\"'#g;s#.*#par_phred33='&'#" ; else echo "# par_phred33="; fi )
$( if [ ! -z ${VIASH_PAR_PHRED64+x} ]; then echo "${VIASH_PAR_PHRED64}" | sed "s#'#'\"'\"'#g;s#.*#par_phred64='&'#" ; else echo "# par_phred64="; fi )
$( if [ ! -z ${VIASH_PAR_FASTQC+x} ]; then echo "${VIASH_PAR_FASTQC}" | sed "s#'#'\"'\"'#g;s#.*#par_fastqc='&'#" ; else echo "# par_fastqc="; fi )
$( if [ ! -z ${VIASH_PAR_FASTQC_ARGS+x} ]; then echo "${VIASH_PAR_FASTQC_ARGS}" | sed "s#'#'\"'\"'#g;s#.*#par_fastqc_args='&'#" ; else echo "# par_fastqc_args="; fi )
$( if [ ! -z ${VIASH_PAR_FASTQC_CONTAMINANTS+x} ]; then echo "${VIASH_PAR_FASTQC_CONTAMINANTS}" | sed "s#'#'\"'\"'#g;s#.*#par_fastqc_contaminants='&'#" ; else echo "# par_fastqc_contaminants="; fi )
$( if [ ! -z ${VIASH_PAR_FASTQC_ADAPTERS+x} ]; then echo "${VIASH_PAR_FASTQC_ADAPTERS}" | sed "s#'#'\"'\"'#g;s#.*#par_fastqc_adapters='&'#" ; else echo "# par_fastqc_adapters="; fi )
$( if [ ! -z ${VIASH_PAR_FASTQC_LIMITS+x} ]; then echo "${VIASH_PAR_FASTQC_LIMITS}" | sed "s#'#'\"'\"'#g;s#.*#par_fastqc_limits='&'#" ; else echo "# par_fastqc_limits="; fi )
$( if [ ! -z ${VIASH_PAR_ADAPTER+x} ]; then echo "${VIASH_PAR_ADAPTER}" | sed "s#'#'\"'\"'#g;s#.*#par_adapter='&'#" ; else echo "# par_adapter="; fi )
$( if [ ! -z ${VIASH_PAR_ADAPTER2+x} ]; then echo "${VIASH_PAR_ADAPTER2}" | sed "s#'#'\"'\"'#g;s#.*#par_adapter2='&'#" ; else echo "# par_adapter2="; fi )
$( if [ ! -z ${VIASH_PAR_ILLUMINA+x} ]; then echo "${VIASH_PAR_ILLUMINA}" | sed "s#'#'\"'\"'#g;s#.*#par_illumina='&'#" ; else echo "# par_illumina="; fi )
$( if [ ! -z ${VIASH_PAR_STRANDED_ILLUMINA+x} ]; then echo "${VIASH_PAR_STRANDED_ILLUMINA}" | sed "s#'#'\"'\"'#g;s#.*#par_stranded_illumina='&'#" ; else echo "# par_stranded_illumina="; fi )
$( if [ ! -z ${VIASH_PAR_NEXTERA+x} ]; then echo "${VIASH_PAR_NEXTERA}" | sed "s#'#'\"'\"'#g;s#.*#par_nextera='&'#" ; else echo "# par_nextera="; fi )
$( if [ ! -z ${VIASH_PAR_SMALL_RNA+x} ]; then echo "${VIASH_PAR_SMALL_RNA}" | sed "s#'#'\"'\"'#g;s#.*#par_small_rna='&'#" ; else echo "# par_small_rna="; fi )
$( if [ ! -z ${VIASH_PAR_CONSIDER_ALREADY_TRIMMED+x} ]; then echo "${VIASH_PAR_CONSIDER_ALREADY_TRIMMED}" | sed "s#'#'\"'\"'#g;s#.*#par_consider_already_trimmed='&'#" ; else echo "# par_consider_already_trimmed="; fi )
$( if [ ! -z ${VIASH_PAR_MAX_LENGTH+x} ]; then echo "${VIASH_PAR_MAX_LENGTH}" | sed "s#'#'\"'\"'#g;s#.*#par_max_length='&'#" ; else echo "# par_max_length="; fi )
$( if [ ! -z ${VIASH_PAR_STRINGENCY+x} ]; then echo "${VIASH_PAR_STRINGENCY}" | sed "s#'#'\"'\"'#g;s#.*#par_stringency='&'#" ; else echo "# par_stringency="; fi )
$( if [ ! -z ${VIASH_PAR_ERROR_RATE+x} ]; then echo "${VIASH_PAR_ERROR_RATE}" | sed "s#'#'\"'\"'#g;s#.*#par_error_rate='&'#" ; else echo "# par_error_rate="; fi )
$( if [ ! -z ${VIASH_PAR_GZIP+x} ]; then echo "${VIASH_PAR_GZIP}" | sed "s#'#'\"'\"'#g;s#.*#par_gzip='&'#" ; else echo "# par_gzip="; fi )
$( if [ ! -z ${VIASH_PAR_DONT_GZIP+x} ]; then echo "${VIASH_PAR_DONT_GZIP}" | sed "s#'#'\"'\"'#g;s#.*#par_dont_gzip='&'#" ; else echo "# par_dont_gzip="; fi )
$( if [ ! -z ${VIASH_PAR_LENGTH+x} ]; then echo "${VIASH_PAR_LENGTH}" | sed "s#'#'\"'\"'#g;s#.*#par_length='&'#" ; else echo "# par_length="; fi )
$( if [ ! -z ${VIASH_PAR_MAX_N+x} ]; then echo "${VIASH_PAR_MAX_N}" | sed "s#'#'\"'\"'#g;s#.*#par_max_n='&'#" ; else echo "# par_max_n="; fi )
$( if [ ! -z ${VIASH_PAR_TRIM_N+x} ]; then echo "${VIASH_PAR_TRIM_N}" | sed "s#'#'\"'\"'#g;s#.*#par_trim_n='&'#" ; else echo "# par_trim_n="; fi )
$( if [ ! -z ${VIASH_PAR_NO_REPORT_FILE+x} ]; then echo "${VIASH_PAR_NO_REPORT_FILE}" | sed "s#'#'\"'\"'#g;s#.*#par_no_report_file='&'#" ; else echo "# par_no_report_file="; fi )
$( if [ ! -z ${VIASH_PAR_SUPPRESS_WARN+x} ]; then echo "${VIASH_PAR_SUPPRESS_WARN}" | sed "s#'#'\"'\"'#g;s#.*#par_suppress_warn='&'#" ; else echo "# par_suppress_warn="; fi )
$( if [ ! -z ${VIASH_PAR_CLIP_R1+x} ]; then echo "${VIASH_PAR_CLIP_R1}" | sed "s#'#'\"'\"'#g;s#.*#par_clip_R1='&'#" ; else echo "# par_clip_R1="; fi )
$( if [ ! -z ${VIASH_PAR_CLIP_R2+x} ]; then echo "${VIASH_PAR_CLIP_R2}" | sed "s#'#'\"'\"'#g;s#.*#par_clip_R2='&'#" ; else echo "# par_clip_R2="; fi )
$( if [ ! -z ${VIASH_PAR_THREE_PRIME_CLIP_R1+x} ]; then echo "${VIASH_PAR_THREE_PRIME_CLIP_R1}" | sed "s#'#'\"'\"'#g;s#.*#par_three_prime_clip_R1='&'#" ; else echo "# par_three_prime_clip_R1="; fi )
$( if [ ! -z ${VIASH_PAR_THREE_PRIME_CLIP_R2+x} ]; then echo "${VIASH_PAR_THREE_PRIME_CLIP_R2}" | sed "s#'#'\"'\"'#g;s#.*#par_three_prime_clip_R2='&'#" ; else echo "# par_three_prime_clip_R2="; fi )
$( if [ ! -z ${VIASH_PAR_NEXTSEQ+x} ]; then echo "${VIASH_PAR_NEXTSEQ}" | sed "s#'#'\"'\"'#g;s#.*#par_nextseq='&'#" ; else echo "# par_nextseq="; fi )
$( if [ ! -z ${VIASH_PAR_BASENAME+x} ]; then echo "${VIASH_PAR_BASENAME}" | sed "s#'#'\"'\"'#g;s#.*#par_basename='&'#" ; else echo "# par_basename="; fi )
$( if [ ! -z ${VIASH_PAR_HARDTRIM5+x} ]; then echo "${VIASH_PAR_HARDTRIM5}" | sed "s#'#'\"'\"'#g;s#.*#par_hardtrim5='&'#" ; else echo "# par_hardtrim5="; fi )
$( if [ ! -z ${VIASH_PAR_HARDTRIM3+x} ]; then echo "${VIASH_PAR_HARDTRIM3}" | sed "s#'#'\"'\"'#g;s#.*#par_hardtrim3='&'#" ; else echo "# par_hardtrim3="; fi )
$( if [ ! -z ${VIASH_PAR_CLOCK+x} ]; then echo "${VIASH_PAR_CLOCK}" | sed "s#'#'\"'\"'#g;s#.*#par_clock='&'#" ; else echo "# par_clock="; fi )
$( if [ ! -z ${VIASH_PAR_POLYA+x} ]; then echo "${VIASH_PAR_POLYA}" | sed "s#'#'\"'\"'#g;s#.*#par_polyA='&'#" ; else echo "# par_polyA="; fi )
$( if [ ! -z ${VIASH_PAR_IMPLICON+x} ]; then echo "${VIASH_PAR_IMPLICON}" | sed "s#'#'\"'\"'#g;s#.*#par_implicon='&'#" ; else echo "# par_implicon="; fi )
$( if [ ! -z ${VIASH_PAR_RRBS+x} ]; then echo "${VIASH_PAR_RRBS}" | sed "s#'#'\"'\"'#g;s#.*#par_rrbs='&'#" ; else echo "# par_rrbs="; fi )
$( if [ ! -z ${VIASH_PAR_NON_DIRECTIONAL+x} ]; then echo "${VIASH_PAR_NON_DIRECTIONAL}" | sed "s#'#'\"'\"'#g;s#.*#par_non_directional='&'#" ; else echo "# par_non_directional="; fi )
$( if [ ! -z ${VIASH_PAR_KEEP+x} ]; then echo "${VIASH_PAR_KEEP}" | sed "s#'#'\"'\"'#g;s#.*#par_keep='&'#" ; else echo "# par_keep="; fi )
$( if [ ! -z ${VIASH_PAR_PAIRED+x} ]; then echo "${VIASH_PAR_PAIRED}" | sed "s#'#'\"'\"'#g;s#.*#par_paired='&'#" ; else echo "# par_paired="; fi )
$( if [ ! -z ${VIASH_PAR_RETAIN_UNPAIRED+x} ]; then echo "${VIASH_PAR_RETAIN_UNPAIRED}" | sed "s#'#'\"'\"'#g;s#.*#par_retain_unpaired='&'#" ; else echo "# par_retain_unpaired="; fi )
$( if [ ! -z ${VIASH_PAR_LENGTH_1+x} ]; then echo "${VIASH_PAR_LENGTH_1}" | sed "s#'#'\"'\"'#g;s#.*#par_length_1='&'#" ; else echo "# par_length_1="; fi )
$( if [ ! -z ${VIASH_PAR_LENGTH_2+x} ]; then echo "${VIASH_PAR_LENGTH_2}" | sed "s#'#'\"'\"'#g;s#.*#par_length_2='&'#" ; else echo "# par_length_2="; fi )
$( if [ ! -z ${VIASH_PAR_OUTPUT_DIR+x} ]; then echo "${VIASH_PAR_OUTPUT_DIR}" | sed "s#'#'\"'\"'#g;s#.*#par_output_dir='&'#" ; else echo "# par_output_dir="; fi )
$( if [ ! -z ${VIASH_PAR_TRIMMED_R1+x} ]; then echo "${VIASH_PAR_TRIMMED_R1}" | sed "s#'#'\"'\"'#g;s#.*#par_trimmed_r1='&'#" ; else echo "# par_trimmed_r1="; fi )
$( if [ ! -z ${VIASH_PAR_TRIMMED_R2+x} ]; then echo "${VIASH_PAR_TRIMMED_R2}" | sed "s#'#'\"'\"'#g;s#.*#par_trimmed_r2='&'#" ; else echo "# par_trimmed_r2="; fi )
$( if [ ! -z ${VIASH_PAR_TRIMMING_REPORT_R1+x} ]; then echo "${VIASH_PAR_TRIMMING_REPORT_R1}" | sed "s#'#'\"'\"'#g;s#.*#par_trimming_report_r1='&'#" ; else echo "# par_trimming_report_r1="; fi )
$( if [ ! -z ${VIASH_PAR_TRIMMING_REPORT_R2+x} ]; then echo "${VIASH_PAR_TRIMMING_REPORT_R2}" | sed "s#'#'\"'\"'#g;s#.*#par_trimming_report_r2='&'#" ; else echo "# par_trimming_report_r2="; fi )
$( if [ ! -z ${VIASH_PAR_TRIMMED_FASTQC_HTML_1+x} ]; then echo "${VIASH_PAR_TRIMMED_FASTQC_HTML_1}" | sed "s#'#'\"'\"'#g;s#.*#par_trimmed_fastqc_html_1='&'#" ; else echo "# par_trimmed_fastqc_html_1="; fi )
$( if [ ! -z ${VIASH_PAR_TRIMMED_FASTQC_HTML_2+x} ]; then echo "${VIASH_PAR_TRIMMED_FASTQC_HTML_2}" | sed "s#'#'\"'\"'#g;s#.*#par_trimmed_fastqc_html_2='&'#" ; else echo "# par_trimmed_fastqc_html_2="; fi )
$( if [ ! -z ${VIASH_PAR_TRIMMED_FASTQC_ZIP_1+x} ]; then echo "${VIASH_PAR_TRIMMED_FASTQC_ZIP_1}" | sed "s#'#'\"'\"'#g;s#.*#par_trimmed_fastqc_zip_1='&'#" ; else echo "# par_trimmed_fastqc_zip_1="; fi )
$( if [ ! -z ${VIASH_PAR_TRIMMED_FASTQC_ZIP_2+x} ]; then echo "${VIASH_PAR_TRIMMED_FASTQC_ZIP_2}" | sed "s#'#'\"'\"'#g;s#.*#par_trimmed_fastqc_zip_2='&'#" ; else echo "# par_trimmed_fastqc_zip_2="; fi )
$( if [ ! -z ${VIASH_PAR_UNPAIRED_R1+x} ]; then echo "${VIASH_PAR_UNPAIRED_R1}" | sed "s#'#'\"'\"'#g;s#.*#par_unpaired_r1='&'#" ; else echo "# par_unpaired_r1="; fi )
$( if [ ! -z ${VIASH_PAR_UNPAIRED_R2+x} ]; then echo "${VIASH_PAR_UNPAIRED_R2}" | sed "s#'#'\"'\"'#g;s#.*#par_unpaired_r2='&'#" ; else echo "# par_unpaired_r2="; fi )
$( if [ ! -z ${VIASH_META_NAME+x} ]; then echo "${VIASH_META_NAME}" | sed "s#'#'\"'\"'#g;s#.*#meta_name='&'#" ; else echo "# meta_name="; fi )
$( if [ ! -z ${VIASH_META_FUNCTIONALITY_NAME+x} ]; then echo "${VIASH_META_FUNCTIONALITY_NAME}" | sed "s#'#'\"'\"'#g;s#.*#meta_functionality_name='&'#" ; else echo "# meta_functionality_name="; fi )
$( if [ ! -z ${VIASH_META_RESOURCES_DIR+x} ]; then echo "${VIASH_META_RESOURCES_DIR}" | sed "s#'#'\"'\"'#g;s#.*#meta_resources_dir='&'#" ; else echo "# meta_resources_dir="; fi )
$( if [ ! -z ${VIASH_META_EXECUTABLE+x} ]; then echo "${VIASH_META_EXECUTABLE}" | sed "s#'#'\"'\"'#g;s#.*#meta_executable='&'#" ; else echo "# meta_executable="; fi )
$( if [ ! -z ${VIASH_META_CONFIG+x} ]; then echo "${VIASH_META_CONFIG}" | sed "s#'#'\"'\"'#g;s#.*#meta_config='&'#" ; else echo "# meta_config="; fi )
$( if [ ! -z ${VIASH_META_TEMP_DIR+x} ]; then echo "${VIASH_META_TEMP_DIR}" | sed "s#'#'\"'\"'#g;s#.*#meta_temp_dir='&'#" ; else echo "# meta_temp_dir="; fi )
$( if [ ! -z ${VIASH_META_CPUS+x} ]; then echo "${VIASH_META_CPUS}" | sed "s#'#'\"'\"'#g;s#.*#meta_cpus='&'#" ; else echo "# meta_cpus="; fi )
$( if [ ! -z ${VIASH_META_MEMORY_B+x} ]; then echo "${VIASH_META_MEMORY_B}" | sed "s#'#'\"'\"'#g;s#.*#meta_memory_b='&'#" ; else echo "# meta_memory_b="; fi )
$( if [ ! -z ${VIASH_META_MEMORY_KB+x} ]; then echo "${VIASH_META_MEMORY_KB}" | sed "s#'#'\"'\"'#g;s#.*#meta_memory_kb='&'#" ; else echo "# meta_memory_kb="; fi )
$( if [ ! -z ${VIASH_META_MEMORY_MB+x} ]; then echo "${VIASH_META_MEMORY_MB}" | sed "s#'#'\"'\"'#g;s#.*#meta_memory_mb='&'#" ; else echo "# meta_memory_mb="; fi )
$( if [ ! -z ${VIASH_META_MEMORY_GB+x} ]; then echo "${VIASH_META_MEMORY_GB}" | sed "s#'#'\"'\"'#g;s#.*#meta_memory_gb='&'#" ; else echo "# meta_memory_gb="; fi )
$( if [ ! -z ${VIASH_META_MEMORY_TB+x} ]; then echo "${VIASH_META_MEMORY_TB}" | sed "s#'#'\"'\"'#g;s#.*#meta_memory_tb='&'#" ; else echo "# meta_memory_tb="; fi )
$( if [ ! -z ${VIASH_META_MEMORY_PB+x} ]; then echo "${VIASH_META_MEMORY_PB}" | sed "s#'#'\"'\"'#g;s#.*#meta_memory_pb='&'#" ; else echo "# meta_memory_pb="; fi )
$( if [ ! -z ${VIASH_META_MEMORY_KIB+x} ]; then echo "${VIASH_META_MEMORY_KIB}" | sed "s#'#'\"'\"'#g;s#.*#meta_memory_kib='&'#" ; else echo "# meta_memory_kib="; fi )
$( if [ ! -z ${VIASH_META_MEMORY_MIB+x} ]; then echo "${VIASH_META_MEMORY_MIB}" | sed "s#'#'\"'\"'#g;s#.*#meta_memory_mib='&'#" ; else echo "# meta_memory_mib="; fi )
$( if [ ! -z ${VIASH_META_MEMORY_GIB+x} ]; then echo "${VIASH_META_MEMORY_GIB}" | sed "s#'#'\"'\"'#g;s#.*#meta_memory_gib='&'#" ; else echo "# meta_memory_gib="; fi )
$( if [ ! -z ${VIASH_META_MEMORY_TIB+x} ]; then echo "${VIASH_META_MEMORY_TIB}" | sed "s#'#'\"'\"'#g;s#.*#meta_memory_tib='&'#" ; else echo "# meta_memory_tib="; fi )
$( if [ ! -z ${VIASH_META_MEMORY_PIB+x} ]; then echo "${VIASH_META_MEMORY_PIB}" | sed "s#'#'\"'\"'#g;s#.*#meta_memory_pib='&'#" ; else echo "# meta_memory_pib="; fi )

## VIASH END
#!/bin/bash

set -eo pipefail

[[ ! -d \$output_dir ]] && mkdir -p \$par_output_dir

IFS=";" read -ra input <<< \$par_input

unset_if_false=( 
    par_phred33 
    par_phred64 
    par_fastqc 
    par_illumina 
    par_stranded_illumina 
    par_nextera 
    par_small_rna 
    par_gzip 
    par_dont_gzip 
    par_trim_n 
    par_no_report_file 
    par_suppress_warn 
    par_clock 
    par_polyA 
    par_implicon 
    par_rrbs 
    par_non_directional 
    par_keep 
    par_paired 
    par_retain_unpaired 
)

for par in \${unset_if_false[@]}; do
    test_val="\${!par}"
    [[ "\$test_val" == "false" ]] && unset \$par
done

# Add FastQC file arguments to fastqc_args
fastqc_args="\${par_fastqc_args}"
if [ -f "\$par_fastqc_contaminants" ]; then 
    fastqc_args+=" --contaminants \$par_fastqc_contaminants"
fi
if [ -f "\$par_fastqc_adapters" ]; then 
    fastqc_args+=" --adapters \$par_fastqc_adapters"
fi
if [ -f "\$par_fastqc_limits" ]; then 
    fastqc_args+=" --limits \$par_fastqc_limits"
fi

trim_galore \\
    \${par_quality:+-q "\${par_quality}"} \\
    \${par_phred33:+--phred33} \\
    \${par_phred64:+--phred64 } \\
    \${par_fastqc:+--fastqc } \\
    \${fastqc_args:+--fastqc_args "\${fastqc_args}"} \\
    \${par_adapter:+-a "\${par_adapter}"} \\
    \${par_adapter2:+-a2 "\${par_adapter2}"} \\
    \${par_illumina:+--illumina} \\
    \${par_stranded_illumina:+--stranded_illumina} \\
    \${par_nextera:+--nextera} \\
    \${par_small_rna:+--small_rna} \\
    \${par_consider_already_trimmed:+--consider_already_trimmed "\${par_consider_already_trimmed}"} \\
    \${par_max_length:+--max_length "\${par_max_length}"} \\
    \${par_stringency:+--stringency "\${par_stringency}"} \\
    \${par_error_rate:+-e "\${par_error_rate}"} \\
    \${par_gzip:+--gzip} \\
    \${par_dont_gzip:+--dont_gzip} \\
    \${par_length:+--length "\${par_length}"} \\
    \${par_max_n:+--max_n "\${par_max_n}"} \\
    \${par_trim_n:+--trim-n "\${par_trim_n}"} \\
    \${par_no_report_file:+--no_report_file} \\
    \${par_suppress_warn:+--suppress_warn} \\
    \${par_clip_R1:+--clip_R1 "\${par_clip_R1}"} \\
    \${par_clip_R2:+--clip_R2 "\${par_clip_R2}"} \\
    \${par_three_prime_clip_R1:+--three_prime_clip_R1 "\${par_three_prime_clip_R1}"} \\
    \${par_three_prime_clip_R2:+--three_prime_clip_R2 "\${par_three_prime_clip_R2}"} \\
    \${par_nextseq:+--nextseq "\${par_nextseq}"} \\
    \${par_basename:+-basename "\${par_basename}"} \\
    \${par_hardtrim5:+--hardtrim5 "\${par_hardtrim5}"} \\
    \${par_hardtrim3:+--hardtrim3 "\${par_hardtrim3}"} \\
    \${par_clock:+--clock} \\
    \${par_polyA:+--polyA} \\
    \${par_implicon:+--implicon "\${par_implicon}"} \\
    \${par_rrbs:+--rrbs} \\
    \${par_non_directional:+--non_directional} \\
    \${par_keep:+--keep} \\
    \${par_paired:+--paired} \\
    \${par_retain_unpaired:+--retain_unpaired} \\
    \${par_length_1:+-r1 "\${par_length_1}"} \\
    \${par_length_2:+-r2 "\${par_length_2}"} \\
    \${meta_cpus:+-j "\${meta_cpus}"} \\
    -o \$par_output_dir \\
    \${input[*]}

if [ \$par_paired == "true" ]; then     

    input_r1=\$(basename -- "\${input[0]}")
    input_r2=\$(basename -- "\${input[1]}")
    [[ ! -z "\$par_trimmed_r1" ]] && mv \$par_output_dir/*val_1.f*q* \$par_trimmed_r1
    [[ ! -z "\$par_trimmed_r2" ]] && mv \$par_output_dir/*val_2.f*q* \$par_trimmed_r2
    [[ ! -z "\$par_trimming_report_r1" ]] && mv \$par_output_dir/\${input_r1}_trimming_report.txt \$par_trimming_report_r1
    [[ ! -z "\$par_trimming_report_r2" ]] && mv \$par_output_dir/\${input_r2}_trimming_report.txt \$par_trimming_report_r2
    
    if [ "\$par_fastqc" == "true" ]; then 
        [[ ! -z "\$par_trimmed_fastqc_html_1" ]] && mv \$par_output_dir/*val_1_fastqc.html \$par_trimmed_fastqc_html_1
        [[ ! -z "\$par_trimmed_fastqc_html_2" ]] && mv \$par_output_dir/*val_2_fastqc.html \$par_trimmed_fastqc_html_2
        [[ ! -z "\$par_trimmed_fastqc_zip_1" ]] && mv \$par_output_dir/*val_1_fastqc.zip \$par_trimmed_fastqc_zip_1
        [[ ! -z "\$par_trimmed_fastqc_zip_2" ]] && mv \$par_output_dir/*val_2_fastqc.zip \$par_trimmed_fastqc_zip_2
    fi
    
    if [ "\$par_retain_unpaired" == "true" ]; then
        [[ ! -z "\$par_unpaired_r1" ]] && mv \$par_output_dir/*.unpaired_1.f*q* \$par_unpaired_r1
        [[ ! -z "\$par_unpaired_r2" ]] && mv \$par_output_dir/*.unpaired_2.f*q* \$par_unpaired_r2
    fi

else
    
    input_r1=\$(basename -- "\${input[0]}")
    [[ ! -z "\$par_trimmed_r1" ]] && mv \$par_output_dir/*_trimmed.fq* \$par_trimmed_r1
    [[ ! -z "\$par_trimming_report_r1" ]] && mv \$par_output_dir/\${input_r1}_trimming_report.txt \$par_trimming_report_r1
    
    if [ "\$par_fastqc" == "true" ]; then 
        [[ ! -z "\$par_trimmed_fastqc_html_1" ]] && mv \$par_output_dir/*_trimmed_fastqc.html \$par_trimmed_fastqc_html_1
        [[ ! -z "\$par_trimmed_fastqc_zip_1" ]] && mv \$par_output_dir/*_trimmed_fastqc.zip \$par_trimmed_fastqc_zip_1
    fi

fi
VIASHMAIN
bash "\$tempscript" &
wait "\$!"

VIASHEOF


if [[ "$VIASH_ENGINE_TYPE" == "docker" ]]; then
  # strip viash automount from file paths
  
  if [ ! -z "$VIASH_PAR_INPUT" ]; then
    unset VIASH_TEST_INPUT
    IFS=';'
    for var in $VIASH_PAR_INPUT; do
      unset IFS
      if [ -z "$VIASH_TEST_INPUT" ]; then
      VIASH_TEST_INPUT="$(ViashDockerStripAutomount "$var")"
    else
      VIASH_TEST_INPUT="$VIASH_TEST_INPUT;""$(ViashDockerStripAutomount "$var")"
    fi
    done
    VIASH_PAR_INPUT="$VIASH_TEST_INPUT"
  fi
  if [ ! -z "$VIASH_PAR_FASTQC_CONTAMINANTS" ]; then
    VIASH_PAR_FASTQC_CONTAMINANTS=$(ViashDockerStripAutomount "$VIASH_PAR_FASTQC_CONTAMINANTS")
  fi
  if [ ! -z "$VIASH_PAR_FASTQC_ADAPTERS" ]; then
    VIASH_PAR_FASTQC_ADAPTERS=$(ViashDockerStripAutomount "$VIASH_PAR_FASTQC_ADAPTERS")
  fi
  if [ ! -z "$VIASH_PAR_FASTQC_LIMITS" ]; then
    VIASH_PAR_FASTQC_LIMITS=$(ViashDockerStripAutomount "$VIASH_PAR_FASTQC_LIMITS")
  fi
  if [ ! -z "$VIASH_PAR_OUTPUT_DIR" ]; then
    VIASH_PAR_OUTPUT_DIR=$(ViashDockerStripAutomount "$VIASH_PAR_OUTPUT_DIR")
  fi
  if [ ! -z "$VIASH_PAR_TRIMMED_R1" ]; then
    VIASH_PAR_TRIMMED_R1=$(ViashDockerStripAutomount "$VIASH_PAR_TRIMMED_R1")
  fi
  if [ ! -z "$VIASH_PAR_TRIMMED_R2" ]; then
    VIASH_PAR_TRIMMED_R2=$(ViashDockerStripAutomount "$VIASH_PAR_TRIMMED_R2")
  fi
  if [ ! -z "$VIASH_PAR_TRIMMING_REPORT_R1" ]; then
    VIASH_PAR_TRIMMING_REPORT_R1=$(ViashDockerStripAutomount "$VIASH_PAR_TRIMMING_REPORT_R1")
  fi
  if [ ! -z "$VIASH_PAR_TRIMMING_REPORT_R2" ]; then
    VIASH_PAR_TRIMMING_REPORT_R2=$(ViashDockerStripAutomount "$VIASH_PAR_TRIMMING_REPORT_R2")
  fi
  if [ ! -z "$VIASH_PAR_TRIMMED_FASTQC_HTML_1" ]; then
    VIASH_PAR_TRIMMED_FASTQC_HTML_1=$(ViashDockerStripAutomount "$VIASH_PAR_TRIMMED_FASTQC_HTML_1")
  fi
  if [ ! -z "$VIASH_PAR_TRIMMED_FASTQC_HTML_2" ]; then
    VIASH_PAR_TRIMMED_FASTQC_HTML_2=$(ViashDockerStripAutomount "$VIASH_PAR_TRIMMED_FASTQC_HTML_2")
  fi
  if [ ! -z "$VIASH_PAR_TRIMMED_FASTQC_ZIP_1" ]; then
    VIASH_PAR_TRIMMED_FASTQC_ZIP_1=$(ViashDockerStripAutomount "$VIASH_PAR_TRIMMED_FASTQC_ZIP_1")
  fi
  if [ ! -z "$VIASH_PAR_TRIMMED_FASTQC_ZIP_2" ]; then
    VIASH_PAR_TRIMMED_FASTQC_ZIP_2=$(ViashDockerStripAutomount "$VIASH_PAR_TRIMMED_FASTQC_ZIP_2")
  fi
  if [ ! -z "$VIASH_PAR_UNPAIRED_R1" ]; then
    VIASH_PAR_UNPAIRED_R1=$(ViashDockerStripAutomount "$VIASH_PAR_UNPAIRED_R1")
  fi
  if [ ! -z "$VIASH_PAR_UNPAIRED_R2" ]; then
    VIASH_PAR_UNPAIRED_R2=$(ViashDockerStripAutomount "$VIASH_PAR_UNPAIRED_R2")
  fi
  if [ ! -z "$VIASH_META_RESOURCES_DIR" ]; then
    VIASH_META_RESOURCES_DIR=$(ViashDockerStripAutomount "$VIASH_META_RESOURCES_DIR")
  fi
  if [ ! -z "$VIASH_META_EXECUTABLE" ]; then
    VIASH_META_EXECUTABLE=$(ViashDockerStripAutomount "$VIASH_META_EXECUTABLE")
  fi
  if [ ! -z "$VIASH_META_CONFIG" ]; then
    VIASH_META_CONFIG=$(ViashDockerStripAutomount "$VIASH_META_CONFIG")
  fi
  if [ ! -z "$VIASH_META_TEMP_DIR" ]; then
    VIASH_META_TEMP_DIR=$(ViashDockerStripAutomount "$VIASH_META_TEMP_DIR")
  fi
fi


# check whether required files exist
if [ ! -z "$VIASH_PAR_OUTPUT_DIR" ] && [ ! -e "$VIASH_PAR_OUTPUT_DIR" ]; then
  ViashError "Output file '$VIASH_PAR_OUTPUT_DIR' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_TRIMMED_R1" ] && [ ! -e "$VIASH_PAR_TRIMMED_R1" ]; then
  ViashError "Output file '$VIASH_PAR_TRIMMED_R1' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_TRIMMED_R2" ] && [ ! -e "$VIASH_PAR_TRIMMED_R2" ]; then
  ViashError "Output file '$VIASH_PAR_TRIMMED_R2' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_TRIMMING_REPORT_R1" ] && [ ! -e "$VIASH_PAR_TRIMMING_REPORT_R1" ]; then
  ViashError "Output file '$VIASH_PAR_TRIMMING_REPORT_R1' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_TRIMMING_REPORT_R2" ] && [ ! -e "$VIASH_PAR_TRIMMING_REPORT_R2" ]; then
  ViashError "Output file '$VIASH_PAR_TRIMMING_REPORT_R2' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_TRIMMED_FASTQC_HTML_1" ] && [ ! -e "$VIASH_PAR_TRIMMED_FASTQC_HTML_1" ]; then
  ViashError "Output file '$VIASH_PAR_TRIMMED_FASTQC_HTML_1' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_TRIMMED_FASTQC_HTML_2" ] && [ ! -e "$VIASH_PAR_TRIMMED_FASTQC_HTML_2" ]; then
  ViashError "Output file '$VIASH_PAR_TRIMMED_FASTQC_HTML_2' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_TRIMMED_FASTQC_ZIP_1" ] && [ ! -e "$VIASH_PAR_TRIMMED_FASTQC_ZIP_1" ]; then
  ViashError "Output file '$VIASH_PAR_TRIMMED_FASTQC_ZIP_1' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_TRIMMED_FASTQC_ZIP_2" ] && [ ! -e "$VIASH_PAR_TRIMMED_FASTQC_ZIP_2" ]; then
  ViashError "Output file '$VIASH_PAR_TRIMMED_FASTQC_ZIP_2' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_UNPAIRED_R1" ] && [ ! -e "$VIASH_PAR_UNPAIRED_R1" ]; then
  ViashError "Output file '$VIASH_PAR_UNPAIRED_R1' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_UNPAIRED_R2" ] && [ ! -e "$VIASH_PAR_UNPAIRED_R2" ]; then
  ViashError "Output file '$VIASH_PAR_UNPAIRED_R2' does not exist."
  exit 1
fi


exit 0
