#!/usr/bin/env bash

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

set -e

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

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

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

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

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

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

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

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

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

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

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

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

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

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



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


# ViashHelp: Display helpful explanation about this executable
function ViashHelp {
  echo "post_processing v0.3.0"
  echo ""
  echo "A viash sub-workflow for the post-processing stage of nf-core/rnaseq pipeline."
  echo ""
  echo "Input:"
  echo "    --id"
  echo "        type: string, required parameter"
  echo "        example: foo"
  echo "        ID of the sample."
  echo ""
  echo "    --strandedness"
  echo "        type: string"
  echo "        default: auto"
  echo "        Sample strand-specificity. Must be one of unstranded, forward, reverse"
  echo "        or auto"
  echo ""
  echo "    --paired"
  echo "        type: boolean"
  echo "        Paired fastq files or not?"
  echo ""
  echo "    --fasta"
  echo "        type: file, required parameter, file must exist"
  echo "        Path to FASTA genome file."
  echo ""
  echo "    --fai"
  echo "        type: file, required parameter, file must exist"
  echo "        Path to FASTA index"
  echo ""
  echo "    --gtf"
  echo "        type: file, file must exist"
  echo "        GTF file"
  echo ""
  echo "    --genome_bam"
  echo "        type: file, file must exist"
  echo "        Genome BAM file"
  echo ""
  echo "    --chrom_sizes"
  echo "        type: file, file must exist"
  echo "        File containing chromosome lengths"
  echo ""
  echo "    --star_multiqc"
  echo "        type: file, file must exist"
  echo "        STAR align log file."
  echo ""
  echo "    --extra_picard_args"
  echo "        type: string"
  echo "        default:"
  echo "        Extra arguments to pass to picard MarkDuplicates command in addition to"
  echo "        defaults defined by the pipeline."
  echo ""
  echo "    --extra_stringtie_args"
  echo "        type: string"
  echo "        default:"
  echo "        Extra arguments to pass to stringtie command in addition to defaults"
  echo "        defined by the pipeline."
  echo ""
  echo "    --stringtie_ignore_gtf"
  echo "        type: boolean"
  echo "        Perform reference-guided de novo assembly of transcripts using"
  echo "        StringTie, i.e. don't restrict to those in GTF file."
  echo ""
  echo "    --bam_csi_index"
  echo "        type: boolean"
  echo "        default: false"
  echo "        Create a CSI index for BAM files instead of the traditional BAI index."
  echo "        This will be required for genomes with larger chromosome sizes."
  echo ""
  echo "    --min_mapped_reads"
  echo "        type: integer"
  echo "        Minimum percentage of uniquely mapped reads below which samples are"
  echo "        removed from further processing."
  echo ""
  echo "    --with_umi"
  echo "        type: boolean"
  echo "        default: false"
  echo "        Enable UMI-based read deduplication."
  echo ""
  echo "    --skip_qc"
  echo "        type: boolean"
  echo ""
  echo "    --skip_markduplicates"
  echo "        type: boolean"
  echo ""
  echo "    --skip_stringtie"
  echo "        type: boolean"
  echo ""
  echo "    --skip_bigwig"
  echo "        type: boolean"
  echo ""
  echo "Output:"
  echo "    --processed_genome_bam"
  echo "        type: file, output, file must exist"
  echo "        default: \$id.genome.bam"
  echo ""
  echo "    --genome_bam_index"
  echo "        type: file, output, file must exist"
  echo "        default: \$id.genome.bam.bai"
  echo ""
  echo "    --genome_bam_stats"
  echo "        type: file, output, file must exist"
  echo "        default: \$id.genome.stats"
  echo ""
  echo "    --genome_bam_flagstat"
  echo "        type: file, output, file must exist"
  echo "        default: \$id.genome.flagstat"
  echo ""
  echo "    --genome_bam_idxstats"
  echo "        type: file, output, file must exist"
  echo "        default: \$id.genome.idxstats"
  echo ""
  echo "    --markduplicates_metrics"
  echo "        type: file, output, file must exist"
  echo "        default: \$id.MarkDuplicates.metrics.txt"
  echo ""
  echo "    --stringtie_transcript_gtf"
  echo "        type: file, output, file must exist"
  echo "        default: \$id.stringtie.transcripts.gtf"
  echo ""
  echo "    --stringtie_coverage_gtf"
  echo "        type: file, output, file must exist"
  echo "        default: \$id.stringtie.coverage.gtf"
  echo ""
  echo "    --stringtie_abundance"
  echo "        type: file, output, file must exist"
  echo "        default: \$id.stringtie.gene_abundance.txt"
  echo ""
  echo "    --stringtie_ballgown"
  echo "        type: file, output, file must exist"
  echo "        default: \$id.stringtie.ballgown"
  echo ""
  echo "    --bedgraph_forward"
  echo "        type: file, output, file must exist"
  echo "        default: \$id.forward.bedgraph"
  echo ""
  echo "    --bedgraph_reverse"
  echo "        type: file, output, file must exist"
  echo "        default: \$id.reverse.bedgraph"
  echo ""
  echo "    --bigwig_forward"
  echo "        type: file, output, file must exist"
  echo "        default: \$id.forward.bigwig"
  echo ""
  echo "    --bigwig_reverse"
  echo "        type: file, output, file must exist"
  echo "        default: \$id.reverse.bigwig"
  echo ""
  echo "Viash built in Computational Requirements:"
  echo "    ---cpus=INT"
  echo "        Number of CPUs to use"
  echo "    ---memory=STRING"
  echo "        Amount of memory to use. Examples: 4GB, 3MiB."
  echo ""
  echo "Viash built in Engines:"
  echo "    ---engine=ENGINE_ID"
  echo "        Specify the engine to use. Options are: native."
  echo "        Default: native"
}

# initialise array
VIASH_POSITIONAL_ARGS=''

while [[ $# -gt 0 ]]; do
    case "$1" in
        -h|--help)
            ViashHelp
            exit
            ;;
        ---v|---verbose)
            let "VIASH_VERBOSITY=VIASH_VERBOSITY+1"
            shift 1
            ;;
        ---verbosity)
            VIASH_VERBOSITY="$2"
            shift 2
            ;;
        ---verbosity=*)
            VIASH_VERBOSITY="$(ViashRemoveFlags "$1")"
            shift 1
            ;;
        --version)
            echo "post_processing v0.3.0"
            exit
            ;;
        --id)
            [ -n "$VIASH_PAR_ID" ] && ViashError Bad arguments for option \'--id\': \'$VIASH_PAR_ID\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_ID="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --id. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --id=*)
            [ -n "$VIASH_PAR_ID" ] && ViashError Bad arguments for option \'--id=*\': \'$VIASH_PAR_ID\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_ID=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --strandedness)
            [ -n "$VIASH_PAR_STRANDEDNESS" ] && ViashError Bad arguments for option \'--strandedness\': \'$VIASH_PAR_STRANDEDNESS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_STRANDEDNESS="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --strandedness. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --strandedness=*)
            [ -n "$VIASH_PAR_STRANDEDNESS" ] && ViashError Bad arguments for option \'--strandedness=*\': \'$VIASH_PAR_STRANDEDNESS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_STRANDEDNESS=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --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="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --paired. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --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=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --fasta)
            [ -n "$VIASH_PAR_FASTA" ] && ViashError Bad arguments for option \'--fasta\': \'$VIASH_PAR_FASTA\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_FASTA="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --fasta. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --fasta=*)
            [ -n "$VIASH_PAR_FASTA" ] && ViashError Bad arguments for option \'--fasta=*\': \'$VIASH_PAR_FASTA\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_FASTA=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --fai)
            [ -n "$VIASH_PAR_FAI" ] && ViashError Bad arguments for option \'--fai\': \'$VIASH_PAR_FAI\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_FAI="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --fai. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --fai=*)
            [ -n "$VIASH_PAR_FAI" ] && ViashError Bad arguments for option \'--fai=*\': \'$VIASH_PAR_FAI\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_FAI=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --gtf)
            [ -n "$VIASH_PAR_GTF" ] && ViashError Bad arguments for option \'--gtf\': \'$VIASH_PAR_GTF\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_GTF="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --gtf. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --gtf=*)
            [ -n "$VIASH_PAR_GTF" ] && ViashError Bad arguments for option \'--gtf=*\': \'$VIASH_PAR_GTF\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_GTF=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --genome_bam)
            [ -n "$VIASH_PAR_GENOME_BAM" ] && ViashError Bad arguments for option \'--genome_bam\': \'$VIASH_PAR_GENOME_BAM\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_GENOME_BAM="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --genome_bam. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --genome_bam=*)
            [ -n "$VIASH_PAR_GENOME_BAM" ] && ViashError Bad arguments for option \'--genome_bam=*\': \'$VIASH_PAR_GENOME_BAM\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_GENOME_BAM=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --chrom_sizes)
            [ -n "$VIASH_PAR_CHROM_SIZES" ] && ViashError Bad arguments for option \'--chrom_sizes\': \'$VIASH_PAR_CHROM_SIZES\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_CHROM_SIZES="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --chrom_sizes. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --chrom_sizes=*)
            [ -n "$VIASH_PAR_CHROM_SIZES" ] && ViashError Bad arguments for option \'--chrom_sizes=*\': \'$VIASH_PAR_CHROM_SIZES\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_CHROM_SIZES=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --star_multiqc)
            [ -n "$VIASH_PAR_STAR_MULTIQC" ] && ViashError Bad arguments for option \'--star_multiqc\': \'$VIASH_PAR_STAR_MULTIQC\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_STAR_MULTIQC="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --star_multiqc. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --star_multiqc=*)
            [ -n "$VIASH_PAR_STAR_MULTIQC" ] && ViashError Bad arguments for option \'--star_multiqc=*\': \'$VIASH_PAR_STAR_MULTIQC\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_STAR_MULTIQC=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --extra_picard_args)
            [ -n "$VIASH_PAR_EXTRA_PICARD_ARGS" ] && ViashError Bad arguments for option \'--extra_picard_args\': \'$VIASH_PAR_EXTRA_PICARD_ARGS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_EXTRA_PICARD_ARGS="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --extra_picard_args. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --extra_picard_args=*)
            [ -n "$VIASH_PAR_EXTRA_PICARD_ARGS" ] && ViashError Bad arguments for option \'--extra_picard_args=*\': \'$VIASH_PAR_EXTRA_PICARD_ARGS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_EXTRA_PICARD_ARGS=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --extra_stringtie_args)
            [ -n "$VIASH_PAR_EXTRA_STRINGTIE_ARGS" ] && ViashError Bad arguments for option \'--extra_stringtie_args\': \'$VIASH_PAR_EXTRA_STRINGTIE_ARGS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_EXTRA_STRINGTIE_ARGS="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --extra_stringtie_args. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --extra_stringtie_args=*)
            [ -n "$VIASH_PAR_EXTRA_STRINGTIE_ARGS" ] && ViashError Bad arguments for option \'--extra_stringtie_args=*\': \'$VIASH_PAR_EXTRA_STRINGTIE_ARGS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_EXTRA_STRINGTIE_ARGS=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --stringtie_ignore_gtf)
            [ -n "$VIASH_PAR_STRINGTIE_IGNORE_GTF" ] && ViashError Bad arguments for option \'--stringtie_ignore_gtf\': \'$VIASH_PAR_STRINGTIE_IGNORE_GTF\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_STRINGTIE_IGNORE_GTF="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --stringtie_ignore_gtf. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --stringtie_ignore_gtf=*)
            [ -n "$VIASH_PAR_STRINGTIE_IGNORE_GTF" ] && ViashError Bad arguments for option \'--stringtie_ignore_gtf=*\': \'$VIASH_PAR_STRINGTIE_IGNORE_GTF\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_STRINGTIE_IGNORE_GTF=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --bam_csi_index)
            [ -n "$VIASH_PAR_BAM_CSI_INDEX" ] && ViashError Bad arguments for option \'--bam_csi_index\': \'$VIASH_PAR_BAM_CSI_INDEX\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_BAM_CSI_INDEX="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --bam_csi_index. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --bam_csi_index=*)
            [ -n "$VIASH_PAR_BAM_CSI_INDEX" ] && ViashError Bad arguments for option \'--bam_csi_index=*\': \'$VIASH_PAR_BAM_CSI_INDEX\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_BAM_CSI_INDEX=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --min_mapped_reads)
            [ -n "$VIASH_PAR_MIN_MAPPED_READS" ] && ViashError Bad arguments for option \'--min_mapped_reads\': \'$VIASH_PAR_MIN_MAPPED_READS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_MIN_MAPPED_READS="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --min_mapped_reads. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --min_mapped_reads=*)
            [ -n "$VIASH_PAR_MIN_MAPPED_READS" ] && ViashError Bad arguments for option \'--min_mapped_reads=*\': \'$VIASH_PAR_MIN_MAPPED_READS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_MIN_MAPPED_READS=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --with_umi)
            [ -n "$VIASH_PAR_WITH_UMI" ] && ViashError Bad arguments for option \'--with_umi\': \'$VIASH_PAR_WITH_UMI\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_WITH_UMI="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --with_umi. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --with_umi=*)
            [ -n "$VIASH_PAR_WITH_UMI" ] && ViashError Bad arguments for option \'--with_umi=*\': \'$VIASH_PAR_WITH_UMI\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_WITH_UMI=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --skip_qc)
            [ -n "$VIASH_PAR_SKIP_QC" ] && ViashError Bad arguments for option \'--skip_qc\': \'$VIASH_PAR_SKIP_QC\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_SKIP_QC="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --skip_qc. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --skip_qc=*)
            [ -n "$VIASH_PAR_SKIP_QC" ] && ViashError Bad arguments for option \'--skip_qc=*\': \'$VIASH_PAR_SKIP_QC\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_SKIP_QC=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --skip_markduplicates)
            [ -n "$VIASH_PAR_SKIP_MARKDUPLICATES" ] && ViashError Bad arguments for option \'--skip_markduplicates\': \'$VIASH_PAR_SKIP_MARKDUPLICATES\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_SKIP_MARKDUPLICATES="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --skip_markduplicates. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --skip_markduplicates=*)
            [ -n "$VIASH_PAR_SKIP_MARKDUPLICATES" ] && ViashError Bad arguments for option \'--skip_markduplicates=*\': \'$VIASH_PAR_SKIP_MARKDUPLICATES\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_SKIP_MARKDUPLICATES=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --skip_stringtie)
            [ -n "$VIASH_PAR_SKIP_STRINGTIE" ] && ViashError Bad arguments for option \'--skip_stringtie\': \'$VIASH_PAR_SKIP_STRINGTIE\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_SKIP_STRINGTIE="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --skip_stringtie. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --skip_stringtie=*)
            [ -n "$VIASH_PAR_SKIP_STRINGTIE" ] && ViashError Bad arguments for option \'--skip_stringtie=*\': \'$VIASH_PAR_SKIP_STRINGTIE\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_SKIP_STRINGTIE=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --skip_bigwig)
            [ -n "$VIASH_PAR_SKIP_BIGWIG" ] && ViashError Bad arguments for option \'--skip_bigwig\': \'$VIASH_PAR_SKIP_BIGWIG\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_SKIP_BIGWIG="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --skip_bigwig. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --skip_bigwig=*)
            [ -n "$VIASH_PAR_SKIP_BIGWIG" ] && ViashError Bad arguments for option \'--skip_bigwig=*\': \'$VIASH_PAR_SKIP_BIGWIG\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_SKIP_BIGWIG=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --processed_genome_bam)
            [ -n "$VIASH_PAR_PROCESSED_GENOME_BAM" ] && ViashError Bad arguments for option \'--processed_genome_bam\': \'$VIASH_PAR_PROCESSED_GENOME_BAM\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_PROCESSED_GENOME_BAM="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --processed_genome_bam. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --processed_genome_bam=*)
            [ -n "$VIASH_PAR_PROCESSED_GENOME_BAM" ] && ViashError Bad arguments for option \'--processed_genome_bam=*\': \'$VIASH_PAR_PROCESSED_GENOME_BAM\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_PROCESSED_GENOME_BAM=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --genome_bam_index)
            [ -n "$VIASH_PAR_GENOME_BAM_INDEX" ] && ViashError Bad arguments for option \'--genome_bam_index\': \'$VIASH_PAR_GENOME_BAM_INDEX\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_GENOME_BAM_INDEX="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --genome_bam_index. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --genome_bam_index=*)
            [ -n "$VIASH_PAR_GENOME_BAM_INDEX" ] && ViashError Bad arguments for option \'--genome_bam_index=*\': \'$VIASH_PAR_GENOME_BAM_INDEX\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_GENOME_BAM_INDEX=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --genome_bam_stats)
            [ -n "$VIASH_PAR_GENOME_BAM_STATS" ] && ViashError Bad arguments for option \'--genome_bam_stats\': \'$VIASH_PAR_GENOME_BAM_STATS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_GENOME_BAM_STATS="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --genome_bam_stats. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --genome_bam_stats=*)
            [ -n "$VIASH_PAR_GENOME_BAM_STATS" ] && ViashError Bad arguments for option \'--genome_bam_stats=*\': \'$VIASH_PAR_GENOME_BAM_STATS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_GENOME_BAM_STATS=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --genome_bam_flagstat)
            [ -n "$VIASH_PAR_GENOME_BAM_FLAGSTAT" ] && ViashError Bad arguments for option \'--genome_bam_flagstat\': \'$VIASH_PAR_GENOME_BAM_FLAGSTAT\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_GENOME_BAM_FLAGSTAT="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --genome_bam_flagstat. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --genome_bam_flagstat=*)
            [ -n "$VIASH_PAR_GENOME_BAM_FLAGSTAT" ] && ViashError Bad arguments for option \'--genome_bam_flagstat=*\': \'$VIASH_PAR_GENOME_BAM_FLAGSTAT\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_GENOME_BAM_FLAGSTAT=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --genome_bam_idxstats)
            [ -n "$VIASH_PAR_GENOME_BAM_IDXSTATS" ] && ViashError Bad arguments for option \'--genome_bam_idxstats\': \'$VIASH_PAR_GENOME_BAM_IDXSTATS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_GENOME_BAM_IDXSTATS="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --genome_bam_idxstats. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --genome_bam_idxstats=*)
            [ -n "$VIASH_PAR_GENOME_BAM_IDXSTATS" ] && ViashError Bad arguments for option \'--genome_bam_idxstats=*\': \'$VIASH_PAR_GENOME_BAM_IDXSTATS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_GENOME_BAM_IDXSTATS=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --markduplicates_metrics)
            [ -n "$VIASH_PAR_MARKDUPLICATES_METRICS" ] && ViashError Bad arguments for option \'--markduplicates_metrics\': \'$VIASH_PAR_MARKDUPLICATES_METRICS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_MARKDUPLICATES_METRICS="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --markduplicates_metrics. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --markduplicates_metrics=*)
            [ -n "$VIASH_PAR_MARKDUPLICATES_METRICS" ] && ViashError Bad arguments for option \'--markduplicates_metrics=*\': \'$VIASH_PAR_MARKDUPLICATES_METRICS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_MARKDUPLICATES_METRICS=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --stringtie_transcript_gtf)
            [ -n "$VIASH_PAR_STRINGTIE_TRANSCRIPT_GTF" ] && ViashError Bad arguments for option \'--stringtie_transcript_gtf\': \'$VIASH_PAR_STRINGTIE_TRANSCRIPT_GTF\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_STRINGTIE_TRANSCRIPT_GTF="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --stringtie_transcript_gtf. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --stringtie_transcript_gtf=*)
            [ -n "$VIASH_PAR_STRINGTIE_TRANSCRIPT_GTF" ] && ViashError Bad arguments for option \'--stringtie_transcript_gtf=*\': \'$VIASH_PAR_STRINGTIE_TRANSCRIPT_GTF\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_STRINGTIE_TRANSCRIPT_GTF=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --stringtie_coverage_gtf)
            [ -n "$VIASH_PAR_STRINGTIE_COVERAGE_GTF" ] && ViashError Bad arguments for option \'--stringtie_coverage_gtf\': \'$VIASH_PAR_STRINGTIE_COVERAGE_GTF\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_STRINGTIE_COVERAGE_GTF="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --stringtie_coverage_gtf. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --stringtie_coverage_gtf=*)
            [ -n "$VIASH_PAR_STRINGTIE_COVERAGE_GTF" ] && ViashError Bad arguments for option \'--stringtie_coverage_gtf=*\': \'$VIASH_PAR_STRINGTIE_COVERAGE_GTF\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_STRINGTIE_COVERAGE_GTF=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --stringtie_abundance)
            [ -n "$VIASH_PAR_STRINGTIE_ABUNDANCE" ] && ViashError Bad arguments for option \'--stringtie_abundance\': \'$VIASH_PAR_STRINGTIE_ABUNDANCE\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_STRINGTIE_ABUNDANCE="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --stringtie_abundance. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --stringtie_abundance=*)
            [ -n "$VIASH_PAR_STRINGTIE_ABUNDANCE" ] && ViashError Bad arguments for option \'--stringtie_abundance=*\': \'$VIASH_PAR_STRINGTIE_ABUNDANCE\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_STRINGTIE_ABUNDANCE=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --stringtie_ballgown)
            [ -n "$VIASH_PAR_STRINGTIE_BALLGOWN" ] && ViashError Bad arguments for option \'--stringtie_ballgown\': \'$VIASH_PAR_STRINGTIE_BALLGOWN\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_STRINGTIE_BALLGOWN="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --stringtie_ballgown. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --stringtie_ballgown=*)
            [ -n "$VIASH_PAR_STRINGTIE_BALLGOWN" ] && ViashError Bad arguments for option \'--stringtie_ballgown=*\': \'$VIASH_PAR_STRINGTIE_BALLGOWN\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_STRINGTIE_BALLGOWN=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --bedgraph_forward)
            [ -n "$VIASH_PAR_BEDGRAPH_FORWARD" ] && ViashError Bad arguments for option \'--bedgraph_forward\': \'$VIASH_PAR_BEDGRAPH_FORWARD\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_BEDGRAPH_FORWARD="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --bedgraph_forward. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --bedgraph_forward=*)
            [ -n "$VIASH_PAR_BEDGRAPH_FORWARD" ] && ViashError Bad arguments for option \'--bedgraph_forward=*\': \'$VIASH_PAR_BEDGRAPH_FORWARD\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_BEDGRAPH_FORWARD=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --bedgraph_reverse)
            [ -n "$VIASH_PAR_BEDGRAPH_REVERSE" ] && ViashError Bad arguments for option \'--bedgraph_reverse\': \'$VIASH_PAR_BEDGRAPH_REVERSE\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_BEDGRAPH_REVERSE="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --bedgraph_reverse. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --bedgraph_reverse=*)
            [ -n "$VIASH_PAR_BEDGRAPH_REVERSE" ] && ViashError Bad arguments for option \'--bedgraph_reverse=*\': \'$VIASH_PAR_BEDGRAPH_REVERSE\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_BEDGRAPH_REVERSE=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --bigwig_forward)
            [ -n "$VIASH_PAR_BIGWIG_FORWARD" ] && ViashError Bad arguments for option \'--bigwig_forward\': \'$VIASH_PAR_BIGWIG_FORWARD\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_BIGWIG_FORWARD="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --bigwig_forward. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --bigwig_forward=*)
            [ -n "$VIASH_PAR_BIGWIG_FORWARD" ] && ViashError Bad arguments for option \'--bigwig_forward=*\': \'$VIASH_PAR_BIGWIG_FORWARD\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_BIGWIG_FORWARD=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        --bigwig_reverse)
            [ -n "$VIASH_PAR_BIGWIG_REVERSE" ] && ViashError Bad arguments for option \'--bigwig_reverse\': \'$VIASH_PAR_BIGWIG_REVERSE\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_BIGWIG_REVERSE="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to --bigwig_reverse. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        --bigwig_reverse=*)
            [ -n "$VIASH_PAR_BIGWIG_REVERSE" ] && ViashError Bad arguments for option \'--bigwig_reverse=*\': \'$VIASH_PAR_BIGWIG_REVERSE\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_PAR_BIGWIG_REVERSE=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        ---engine)
            VIASH_ENGINE_ID="$2"
            shift 2
            ;;
        ---engine=*)
            VIASH_ENGINE_ID="$(ViashRemoveFlags "$1")"
            shift 1
            ;;
        ---cpus)
            [ -n "$VIASH_META_CPUS" ] && ViashError Bad arguments for option \'---cpus\': \'$VIASH_META_CPUS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_META_CPUS="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to ---cpus. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        ---cpus=*)
            [ -n "$VIASH_META_CPUS" ] && ViashError Bad arguments for option \'---cpus=*\': \'$VIASH_META_CPUS\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_META_CPUS=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        ---memory)
            [ -n "$VIASH_META_MEMORY" ] && ViashError Bad arguments for option \'---memory\': \'$VIASH_META_MEMORY\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_META_MEMORY="$2"
            [ $# -lt 2 ] && ViashError Not enough arguments passed to ---memory. Use "--help" to get more information on the parameters. && exit 1
            shift 2
            ;;
        ---memory=*)
            [ -n "$VIASH_META_MEMORY" ] && ViashError Bad arguments for option \'---memory=*\': \'$VIASH_META_MEMORY\' \& \'$2\' - you should provide exactly one argument for this option. && exit 1
            VIASH_META_MEMORY=$(ViashRemoveFlags "$1")
            shift 1
            ;;
        *)  # positional arg or unknown option
            # since the positional args will be eval'd, can we always quote, instead of using ViashQuote
            VIASH_POSITIONAL_ARGS="$VIASH_POSITIONAL_ARGS '$1'"
            [[ $1 == -* ]] && ViashWarning $1 looks like a parameter but is not a defined parameter and will instead be treated as a positional argument. Use "--help" to get more information on the parameters.
            shift # past argument
            ;;
    esac
done

# parse positional parameters
eval set -- $VIASH_POSITIONAL_ARGS


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

# setting computational defaults

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


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

# filling in defaults
if [ -z ${VIASH_PAR_STRANDEDNESS+x} ]; then
  VIASH_PAR_STRANDEDNESS="auto"
fi
if [ -z ${VIASH_PAR_EXTRA_PICARD_ARGS+x} ]; then
  VIASH_PAR_EXTRA_PICARD_ARGS=""
fi
if [ -z ${VIASH_PAR_EXTRA_STRINGTIE_ARGS+x} ]; then
  VIASH_PAR_EXTRA_STRINGTIE_ARGS=""
fi
if [ -z ${VIASH_PAR_BAM_CSI_INDEX+x} ]; then
  VIASH_PAR_BAM_CSI_INDEX="false"
fi
if [ -z ${VIASH_PAR_WITH_UMI+x} ]; then
  VIASH_PAR_WITH_UMI="false"
fi
if [ -z ${VIASH_PAR_PROCESSED_GENOME_BAM+x} ]; then
  VIASH_PAR_PROCESSED_GENOME_BAM="\$id.genome.bam"
fi
if [ -z ${VIASH_PAR_GENOME_BAM_INDEX+x} ]; then
  VIASH_PAR_GENOME_BAM_INDEX="\$id.genome.bam.bai"
fi
if [ -z ${VIASH_PAR_GENOME_BAM_STATS+x} ]; then
  VIASH_PAR_GENOME_BAM_STATS="\$id.genome.stats"
fi
if [ -z ${VIASH_PAR_GENOME_BAM_FLAGSTAT+x} ]; then
  VIASH_PAR_GENOME_BAM_FLAGSTAT="\$id.genome.flagstat"
fi
if [ -z ${VIASH_PAR_GENOME_BAM_IDXSTATS+x} ]; then
  VIASH_PAR_GENOME_BAM_IDXSTATS="\$id.genome.idxstats"
fi
if [ -z ${VIASH_PAR_MARKDUPLICATES_METRICS+x} ]; then
  VIASH_PAR_MARKDUPLICATES_METRICS="\$id.MarkDuplicates.metrics.txt"
fi
if [ -z ${VIASH_PAR_STRINGTIE_TRANSCRIPT_GTF+x} ]; then
  VIASH_PAR_STRINGTIE_TRANSCRIPT_GTF="\$id.stringtie.transcripts.gtf"
fi
if [ -z ${VIASH_PAR_STRINGTIE_COVERAGE_GTF+x} ]; then
  VIASH_PAR_STRINGTIE_COVERAGE_GTF="\$id.stringtie.coverage.gtf"
fi
if [ -z ${VIASH_PAR_STRINGTIE_ABUNDANCE+x} ]; then
  VIASH_PAR_STRINGTIE_ABUNDANCE="\$id.stringtie.gene_abundance.txt"
fi
if [ -z ${VIASH_PAR_STRINGTIE_BALLGOWN+x} ]; then
  VIASH_PAR_STRINGTIE_BALLGOWN="\$id.stringtie.ballgown"
fi
if [ -z ${VIASH_PAR_BEDGRAPH_FORWARD+x} ]; then
  VIASH_PAR_BEDGRAPH_FORWARD="\$id.forward.bedgraph"
fi
if [ -z ${VIASH_PAR_BEDGRAPH_REVERSE+x} ]; then
  VIASH_PAR_BEDGRAPH_REVERSE="\$id.reverse.bedgraph"
fi
if [ -z ${VIASH_PAR_BIGWIG_FORWARD+x} ]; then
  VIASH_PAR_BIGWIG_FORWARD="\$id.forward.bigwig"
fi
if [ -z ${VIASH_PAR_BIGWIG_REVERSE+x} ]; then
  VIASH_PAR_BIGWIG_REVERSE="\$id.reverse.bigwig"
fi

# check whether required files exist
if [ ! -z "$VIASH_PAR_FASTA" ] && [ ! -e "$VIASH_PAR_FASTA" ]; then
  ViashError "Input file '$VIASH_PAR_FASTA' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_FAI" ] && [ ! -e "$VIASH_PAR_FAI" ]; then
  ViashError "Input file '$VIASH_PAR_FAI' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_GTF" ] && [ ! -e "$VIASH_PAR_GTF" ]; then
  ViashError "Input file '$VIASH_PAR_GTF' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_GENOME_BAM" ] && [ ! -e "$VIASH_PAR_GENOME_BAM" ]; then
  ViashError "Input file '$VIASH_PAR_GENOME_BAM' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_CHROM_SIZES" ] && [ ! -e "$VIASH_PAR_CHROM_SIZES" ]; then
  ViashError "Input file '$VIASH_PAR_CHROM_SIZES' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_STAR_MULTIQC" ] && [ ! -e "$VIASH_PAR_STAR_MULTIQC" ]; then
  ViashError "Input file '$VIASH_PAR_STAR_MULTIQC' does not exist."
  exit 1
fi

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

# create parent directories of output files, if so desired
if [ ! -z "$VIASH_PAR_PROCESSED_GENOME_BAM" ] && [ ! -d "$(dirname "$VIASH_PAR_PROCESSED_GENOME_BAM")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_PROCESSED_GENOME_BAM")"
fi
if [ ! -z "$VIASH_PAR_GENOME_BAM_INDEX" ] && [ ! -d "$(dirname "$VIASH_PAR_GENOME_BAM_INDEX")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_GENOME_BAM_INDEX")"
fi
if [ ! -z "$VIASH_PAR_GENOME_BAM_STATS" ] && [ ! -d "$(dirname "$VIASH_PAR_GENOME_BAM_STATS")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_GENOME_BAM_STATS")"
fi
if [ ! -z "$VIASH_PAR_GENOME_BAM_FLAGSTAT" ] && [ ! -d "$(dirname "$VIASH_PAR_GENOME_BAM_FLAGSTAT")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_GENOME_BAM_FLAGSTAT")"
fi
if [ ! -z "$VIASH_PAR_GENOME_BAM_IDXSTATS" ] && [ ! -d "$(dirname "$VIASH_PAR_GENOME_BAM_IDXSTATS")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_GENOME_BAM_IDXSTATS")"
fi
if [ ! -z "$VIASH_PAR_MARKDUPLICATES_METRICS" ] && [ ! -d "$(dirname "$VIASH_PAR_MARKDUPLICATES_METRICS")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_MARKDUPLICATES_METRICS")"
fi
if [ ! -z "$VIASH_PAR_STRINGTIE_TRANSCRIPT_GTF" ] && [ ! -d "$(dirname "$VIASH_PAR_STRINGTIE_TRANSCRIPT_GTF")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_STRINGTIE_TRANSCRIPT_GTF")"
fi
if [ ! -z "$VIASH_PAR_STRINGTIE_COVERAGE_GTF" ] && [ ! -d "$(dirname "$VIASH_PAR_STRINGTIE_COVERAGE_GTF")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_STRINGTIE_COVERAGE_GTF")"
fi
if [ ! -z "$VIASH_PAR_STRINGTIE_ABUNDANCE" ] && [ ! -d "$(dirname "$VIASH_PAR_STRINGTIE_ABUNDANCE")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_STRINGTIE_ABUNDANCE")"
fi
if [ ! -z "$VIASH_PAR_STRINGTIE_BALLGOWN" ] && [ ! -d "$(dirname "$VIASH_PAR_STRINGTIE_BALLGOWN")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_STRINGTIE_BALLGOWN")"
fi
if [ ! -z "$VIASH_PAR_BEDGRAPH_FORWARD" ] && [ ! -d "$(dirname "$VIASH_PAR_BEDGRAPH_FORWARD")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_BEDGRAPH_FORWARD")"
fi
if [ ! -z "$VIASH_PAR_BEDGRAPH_REVERSE" ] && [ ! -d "$(dirname "$VIASH_PAR_BEDGRAPH_REVERSE")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_BEDGRAPH_REVERSE")"
fi
if [ ! -z "$VIASH_PAR_BIGWIG_FORWARD" ] && [ ! -d "$(dirname "$VIASH_PAR_BIGWIG_FORWARD")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_BIGWIG_FORWARD")"
fi
if [ ! -z "$VIASH_PAR_BIGWIG_REVERSE" ] && [ ! -d "$(dirname "$VIASH_PAR_BIGWIG_REVERSE")" ]; then
  mkdir -p "$(dirname "$VIASH_PAR_BIGWIG_REVERSE")"
fi

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


# set dependency paths
VIASH_DEP_PICARD_MARKDUPLICATES="$VIASH_META_RESOURCES_DIR/../../../nextflow/picard_markduplicates/main.nf"
VIASH_DEP_STRINGTIE="$VIASH_META_RESOURCES_DIR/../../../nextflow/stringtie/main.nf"
VIASH_DEP_UCSC_BEDCLIP="$VIASH_META_RESOURCES_DIR/../../../nextflow/ucsc/bedclip/main.nf"
VIASH_DEP_UCSC_BEDGRAPHTOBIGWIG="$VIASH_META_RESOURCES_DIR/../../../nextflow/ucsc/bedgraphtobigwig/main.nf"
VIASH_DEP_SAMTOOLS_SAMTOOLS_SORT="$VIASH_TARGET_DIR/dependencies/vsh/vsh/biobox/v0.3.1/nextflow/samtools/samtools_sort/main.nf"
VIASH_DEP_SAMTOOLS_SAMTOOLS_INDEX="$VIASH_TARGET_DIR/dependencies/vsh/vsh/biobox/v0.3.1/nextflow/samtools/samtools_index/main.nf"
VIASH_DEP_SAMTOOLS_SAMTOOLS_STATS="$VIASH_TARGET_DIR/dependencies/vsh/vsh/biobox/v0.3.1/nextflow/samtools/samtools_stats/main.nf"
VIASH_DEP_SAMTOOLS_SAMTOOLS_FLAGSTAT="$VIASH_TARGET_DIR/dependencies/vsh/vsh/biobox/v0.3.1/nextflow/samtools/samtools_flagstat/main.nf"
VIASH_DEP_SAMTOOLS_SAMTOOLS_IDXSTATS="$VIASH_TARGET_DIR/dependencies/vsh/vsh/biobox/v0.3.1/nextflow/samtools/samtools_idxstats/main.nf"
VIASH_DEP_BEDTOOLS_BEDTOOLS_GENOMECOV="$VIASH_TARGET_DIR/dependencies/vsh/vsh/biobox/v0.3.1/nextflow/bedtools/bedtools_genomecov/main.nf"

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

//// VIASH END
// Note: some helper functionality is added at the end of this file

workflow run_wf {
  take:
    input_ch

  main:

    output_ch = input_ch     

      | picard_markduplicates.run (
        runIf: { id, state -> !state.skip_markduplicates && !state.with_umi }, 
        fromState: [
            "bam": "genome_bam",
            "fasta": "fasta",
            "fai": "fai",
            "extra_picard_args": "extra_picard_args"
        ], 
        toState: [
            "processed_genome_bam": "output_bam",
            "markduplicates_metrics": "metrics"
        ],
        directives: [ label: [ "midmem", "midcpu" ] ]
      )
      | samtools_sort.run (
        runIf: { id, state -> !state.skip_markduplicates && !state.with_umi },
        fromState: [ "input": "processed_genome_bam" ],
        toState: [ "processed_genome_bam": "output" ],
        key: "genome_sorted_MarkDuplicates",
        directives: [ label: [ "midmem", "midcpu" ] ]
      )
      | samtools_index.run (
        runIf: { id, state -> !state.skip_markduplicates && !state.with_umi },
        fromState: [
            "input": "processed_genome_bam", 
            "csi": "bam_csi_index"
        ],
        toState: [ "genome_bam_index": "output" ],
        key: "genome_sorted_MarkDuplicates",
        directives: [ label: [ "midmem", "midcpu" ] ]
      )
      | samtools_stats.run (
        runIf: { id, state -> !state.skip_markduplicates && !state.with_umi }, 
        fromState: [
            "input": "processed_genome_bam", 
            "bai": "genome_bam_index" 
        ],
        toState: [ "genome_bam_stats": "output" ],
        key: "MarkDuplicates_stats",
        directives: [ label: [ "midmem", "midcpu" ] ]
      )
      | samtools_flagstat.run (
        runIf: { id, state -> !state.skip_markduplicates && !state.with_umi }, 
        fromState: [
            "bam": "processed_genome_bam", 
            "bai": "genome_bam_index"
        ],
        toState: [ "genome_bam_flagstat": "output" ],
        key: "MarkDuplicates_flagstat",
        directives: [ label: [ "midmem", "midcpu" ] ]
      )
      | samtools_idxstats.run(
        runIf: { id, state -> !state.skip_markduplicates && !state.with_umi }, 
        fromState: [
          "bam": "processed_genome_bam", 
          "bai": "genome_bam_index"
        ],
        toState: [ "genome_bam_idxstats": "output" ],
        key: "MarkDuplicates_idxstats",
        directives: [ label: [ "midmem", "midcpu" ] ]
      ) 

      | stringtie.run (
        runIf: { id, state -> !state.skip_stringtie }, 
        fromState: [
          "strandedness": "strandedness", 
          "bam": "processed_genome_bam",
          "annotation_gtf": "gtf",
          "extra_stringtie_args": "extra_stringtie_args"
        ], 
        toState: [
          "stringtie_transcript_gtf": "transcript_gtf",
          "stringtie_coverage_gtf": "coverage_gtf",
          "stringtie_abundance": "abundance",
          "stringtie_ballgown": "ballgown"
        ],
        directives: [ label: [ "midmem", "midcpu" ] ]
      )

      // Genome-wide coverage with BEDTools

      | bedtools_genomecov.run (
        runIf: { id, state -> !state.skip_bigwig },
        fromState: [
          "input_bam": "processed_genome_bam",
        ],
        args: [
          split: true, 
          du: true, 
          bed_graph: true, 
          strand: "+"
        ],
        toState: [ "bedgraph_forward": "output" ], 
        key: "bedtools_genomecov_forward",
        directives: [ label: [ "midmem", "midcpu" ] ]
      )

      | bedtools_genomecov.run (
        runIf: { id, state -> !state.skip_bigwig },
        fromState: [
          "input_bam": "processed_genome_bam",
        ],
        args: [
          split: true, 
          du: true, 
          bed_graph: true, 
          strand: "-"
        ],
        toState: [ "bedgraph_reverse": "output" ],
        key: "bedtools_genomecov_reverse",
        directives: [ label: [ "midmem", "midcpu" ] ]
      )

      | bedclip.run (
        runIf: { id, state -> !state.skip_bigwig },
        fromState: [
          "input_bedgraph": "bedgraph_forward", 
          "sizes": "chrom_sizes" 
        ],
        toState: [ "bedgraph_forward": "output_bedgraph" ], 
        key: "bedclip_forward",
        directives: [ label: [ "midmem", "midcpu" ] ]
      )

      | bedgraphtobigwig.run (
        runIf: { id, state -> !state.skip_bigwig },
        fromState: [
            "bedgraph": "bedgraph_forward", 
            "sizes": "chrom_sizes" 
        ],
        toState: [ "bigwig_forward": "bigwig" ], 
        key: "bedgraphtobigwig_forward",
        directives: [ label: [ "midmem", "midcpu" ] ]
      )

      | bedclip.run (
        runIf: { id, state -> !state.skip_bigwig },
        fromState: [
          "input_bedgraph": "bedgraph_reverse", 
          "sizes": "chrom_sizes", 
        ],
        toState: [ "bedgraph_reverse": "output_bedgraph" ], 
        key: "bedclip_reverse",
        directives: [ label: [ "midmem", "midcpu" ] ]
      )

      | bedgraphtobigwig.run (
        runIf: { id, state -> !state.skip_bigwig },
        fromState: [
          "bedgraph": "bedgraph_reverse", 
          "sizes": "chrom_sizes" 
        ],
        toState: [ "bigwig_reverse": "bigwig" ], 
        key: "bedgraphtobigwig_reverse",
        directives: [ label: [ "midmem", "midcpu" ] ]
      )

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

      | setState (
        "processed_genome_bam": "processed_genome_bam", 
        "genome_bam_index": "genome_bam_index",
        "genome_bam_stats": "genome_bam_stats",
        "genome_bam_flagstat": "genome_bam_flagstat", 
        "genome_bam_idxstats": "genome_bam_idxstats", 
        "markduplicates_metrics": "markduplicates_metrics", 
        "stringtie_transcript_gtf": "stringtie_transcript_gtf",
        "stringtie_coverage_gtf": "stringtie_coverage_gtf",
        "stringtie_abundance": "stringtie_abundance",
        "stringtie_ballgown": "stringtie_ballgown", 
        "bedgraph_forward": "bedgraph_forward",
        "bedgraph_reverse": "bedgraph_reverse",
        "bigwig_forward": "bigwig_forward",
        "bigwig_reverse": "bigwig_reverse"
      )

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

VIASHEOF


# check whether required files exist
if [ ! -z "$VIASH_PAR_PROCESSED_GENOME_BAM" ] && [ ! -e "$VIASH_PAR_PROCESSED_GENOME_BAM" ]; then
  ViashError "Output file '$VIASH_PAR_PROCESSED_GENOME_BAM' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_GENOME_BAM_INDEX" ] && [ ! -e "$VIASH_PAR_GENOME_BAM_INDEX" ]; then
  ViashError "Output file '$VIASH_PAR_GENOME_BAM_INDEX' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_GENOME_BAM_STATS" ] && [ ! -e "$VIASH_PAR_GENOME_BAM_STATS" ]; then
  ViashError "Output file '$VIASH_PAR_GENOME_BAM_STATS' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_GENOME_BAM_FLAGSTAT" ] && [ ! -e "$VIASH_PAR_GENOME_BAM_FLAGSTAT" ]; then
  ViashError "Output file '$VIASH_PAR_GENOME_BAM_FLAGSTAT' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_GENOME_BAM_IDXSTATS" ] && [ ! -e "$VIASH_PAR_GENOME_BAM_IDXSTATS" ]; then
  ViashError "Output file '$VIASH_PAR_GENOME_BAM_IDXSTATS' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_MARKDUPLICATES_METRICS" ] && [ ! -e "$VIASH_PAR_MARKDUPLICATES_METRICS" ]; then
  ViashError "Output file '$VIASH_PAR_MARKDUPLICATES_METRICS' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_STRINGTIE_TRANSCRIPT_GTF" ] && [ ! -e "$VIASH_PAR_STRINGTIE_TRANSCRIPT_GTF" ]; then
  ViashError "Output file '$VIASH_PAR_STRINGTIE_TRANSCRIPT_GTF' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_STRINGTIE_COVERAGE_GTF" ] && [ ! -e "$VIASH_PAR_STRINGTIE_COVERAGE_GTF" ]; then
  ViashError "Output file '$VIASH_PAR_STRINGTIE_COVERAGE_GTF' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_STRINGTIE_ABUNDANCE" ] && [ ! -e "$VIASH_PAR_STRINGTIE_ABUNDANCE" ]; then
  ViashError "Output file '$VIASH_PAR_STRINGTIE_ABUNDANCE' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_STRINGTIE_BALLGOWN" ] && [ ! -e "$VIASH_PAR_STRINGTIE_BALLGOWN" ]; then
  ViashError "Output file '$VIASH_PAR_STRINGTIE_BALLGOWN' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_BEDGRAPH_FORWARD" ] && [ ! -e "$VIASH_PAR_BEDGRAPH_FORWARD" ]; then
  ViashError "Output file '$VIASH_PAR_BEDGRAPH_FORWARD' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_BEDGRAPH_REVERSE" ] && [ ! -e "$VIASH_PAR_BEDGRAPH_REVERSE" ]; then
  ViashError "Output file '$VIASH_PAR_BEDGRAPH_REVERSE' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_BIGWIG_FORWARD" ] && [ ! -e "$VIASH_PAR_BIGWIG_FORWARD" ]; then
  ViashError "Output file '$VIASH_PAR_BIGWIG_FORWARD' does not exist."
  exit 1
fi
if [ ! -z "$VIASH_PAR_BIGWIG_REVERSE" ] && [ ! -e "$VIASH_PAR_BIGWIG_REVERSE" ]; then
  ViashError "Output file '$VIASH_PAR_BIGWIG_REVERSE' does not exist."
  exit 1
fi


exit 0
