#!/bin/bash -x
# Bismark-ENCODE-WGBS 0.0.1
# Generated by dx-app-wizard.
#
# Basic execution pattern: Your app will run on a single machine from
# beginning to end.
#
# Your job's input variables (if any) will be loaded as environment
# variables before this script runs.  Any array inputs will be loaded
# as bash arrays.
#
# Any code outside of main() (or any entry point you may add) is
# ALWAYS executed, followed by running the entry point itself.
#
# See https://wiki.dnanexus.com/Developer-Portal for tutorials on how
# to modify this file.

main() {

    echo "Value of  pair 1 reads: '${pair1_reads[@]}"
    echo "Value of  pair 2 reads: '${pair2_reads[@]}"

    # The following line(s) use the dx command-line tool to download your file
    # inputs to the local file system using variable names for the filenames. To
    # recover the original filenames, you can use the output of "dx describe
    # "$variable" --name".

    mkdir r1
    mkdir r2
    allreads1=""
    allreads2=""
    for i in ${!pair1_reads[@]}
    do
        filename=`dx describe "${pair1_reads[$i]}" --name | cut -d'.' -f1`
        dx download "${pair1_reads[$i]}" -o - | gunzip > r1/"$filename".fq
        allreads1="$allreads1""$filename"
    done

    for i in ${!pair2_reads[@]}
    do
        filename=`dx describe "${pair2_reads[$i]}" --name | cut -d'.' -f1`
        dx download "${pair2_reads[$i]}" -o - | gunzip > r2/"$filename".fq
        allreads2="$allreads2""$filename"
    done
    echo "Reads  downloaded"
    cat r1/*.fq > "$allreads1".fq
    cat r2/*.fq > "$allreads2".fq

    mkdir input
    outfile1="$allreads1".trimmed-reads.1.fq
    outfile2="$allreads2".trimmed-reads.2.fq
    mott-trim.py -q 3 -m 30 -t sanger $outfile1,$outfile2 "$allreads1".fq,"$allreads2".fq
    gzip *trimmed-reads*.fq


    echo `ls /home/dnanexus`
    trimmed_reads1=$(dx upload /home/dnanexus/$outfile1.gz --brief)
    trimmed_reads2=$(dx upload /home/dnanexus/$outfile2.gz --brief)

    # The following line(s) use the utility dx-jobutil-add-output to format and
    # add output variables to your job's output as appropriate for the output
    # class.  Run "dx-jobutil-add-output -h" for more information on what it
    # does.

    dx-jobutil-add-output trimmed_reads1 "$trimmed_reads1" --class=file
    dx-jobutil-add-output trimmed_reads2 "$trimmed_reads2" --class=file
}
