#! /bin/bash

while getopts "q:s:m:k:e:o:z:" optkey; do
        case $optkey in
	     m) mapper=$OPTARG;;
	     k) kmer=$OPTARG;;
	     e) mapbamfile=$OPTARG;;
	     o) sdifile=$OPTARG;;
	     q) mapqcutoff=$OPTARG;;
	     s) phredcutoff=$OPTARG;;
	     z) imrextraopt=$OPTARG;;
        esac
done
shift $((OPTIND-1))

if [ $# -lt 1 ]; then
	echo "usage: imrdenom <project_description_file>"
	exit 1
fi
Bin="$( readlink -f -- "${0%/*}" )"
des=$1

echo "running IMR"
strmapper=""
if [ "$mapper" ]; then
	strmapper=" -m $mapper "   
fi

if [ "$mapbamfile" ]; then
	strmapbam=" -e $mapbamfile"
fi

if [ "$imrextraopt" ]; then
	imropt=" $imrextraopt "
fi
$Bin/imr easyrun $imropt $strmapper $strmapbam $des 

echo "running DENOM"
strkmer=""
if [ "$kmer" ]; then
	strkmer=" -k $kmer " 
fi
$Bin/denom soapinterface $strkmer $des 


wait
echo "running MCMERGE"
mcmeopt=""
if [ "$sdifile" ]; then
	mcmeopt=" -o $sdifile "   
fi
if [ "$mapqcutoff" ]; then
	mcmeopt = " -q $mapqcutoff $mcmeopt"
fi
if [ "$phredcutoff" ]; then
	mcmeopt = " -s $mapqcutoff $mcmeopt"
fi
$Bin/mcmerge configrun $mcmeopt $des
