#!/usr/bin/env sh
#
# This file is part of the EukCC (https://github.com/openpaul/eukcc).
# Copyright (c) 2019 Paul Saary
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# provides all file operation functions
# used inthis package
# simple wrapper for GMEs
# allowing to provide an output directory


# A POSIX variable
OPTIND=1         # Reset in case getopts has been used previously in the shell.

# Initialize our own variables:
output_file="./output/"
input_file=""
ncores="1"
key="$(pwd)/.gm_key"

while getopts "h?i:o:n:k:" opt; do
    case "$opt" in
    h|\?)
        show_help
        exit 0
        ;;
    i)  input_file=$OPTARG
        ;;
    o)  output_file=$OPTARG
        ;;
    n)  ncores=$OPTARG
        ;;
    esac
done

# cd into output dir
cd $(dirname ${output_file})
GTF="$(basename $input_file).gtf"
if [ -f "genemark.gtf" ]; then
    echo "Did not rerun gmes as genemark.gtf exists"
    echo "Delete the file to rerun gmes"
else
    gmes_petap.pl --v --fungus --ES --cores $ncores --min_contig 5000 --sequence $input_file > runGMES.log 2>&1 
fi
# check if GeneMark-ES was succesfull
# if so extract proteins
if [ -f "genemark.gtf" ]; then
    get_sequence_from_GTF.pl genemark.gtf $input_file
fi
