#!/bin/bash

# Get command line options
READFILE=$1 
shift
ARGUMENTS=$*

RETCODE="0"

# Check for readpair files.
READLIST=`bl_seqreadlist.py --tsv "$READFILE" --outtype transratecom`
if [[ -z $READLIST ]]
then
    RETCODE="2"
    MESSAGE='You must select one or more pairs of read files before running Transrate.'
    echo $MESSAGE >> transrate.log
    java -jar $BIRCH/script/ErrorBox2.jar "$MESSAGE"
else
    # Check for gzip-compressed files
    # Assumes that the first argument is the name of the input file
    # with a list of read files to be checked for the .gz extension
    if [[ -z `grep '.gz' $READFILE` ]]
    then
        RETCODE="0"
    else
        RETCODE="1"
        MESSAGE='Transrate cannot read compressed (.gz) files. Decompress files with gunzip *.gz'
        echo $MESSAGE >> transrate.log
        java -jar $BIRCH/script/ErrorBox2.jar "$MESSAGE"
    fi
fi

# If any of the files are compressed, open up an error message.
# Otherwise, run transrate
if [ "$RETCODE" -eq "0" ]
then
    nice transrate $READLIST "$ARGUMENTS" 2>> transrate.log
else
    exit 2 # returns an error code
fi

