#!/bin/bash
TOOLDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

echo "Started FALCO calling"

## ----------
## Variables setup
## ----------
source $1

bam_base=`echo $bam_name | sed 's#.bam$##' - ` 

## ----------
## Create extra init script
## ----------
if [[ $filter_file != 'None' && $filter_file != '' ]] # Galaxy default somehow is "None"
then 
	FILTER_PARAM=" --filter "$filter_file
else
	FILTER_PARAM=""
fi

if [[ $manifest_file != 'None' && $manifest_file != 'None' ]] # Galaxy default is "None"
then 
	MANIFEST_PARAM=" --manifest "$manifest_file
else
	MANIFEST_PARAM=""
fi

## ----------
## Status / debug
## ----------
DEBUG=1
if [ $DEBUG ]
then
	DBS="[INFO] "
	echo $DBS"FILTER:  "$filter_file
	echo $DBS"MANIFES: "$manifest_file
	echo $DBS"REFFILE: "$REF_FILE
	echo $DBS"DB_KEY:  "$DB_KEY
	echo $DBS"REF_SRC: "$REF_SOURCE
	echo $DBS"BAMFILE: "$bam_file
	echo $DBS"BAMNAME: "$bam_name
	echo $DBS"BAMBASE: "$bam_base
	echo $DBS"OUTPATH: "$out_path
fi

## ----------
## create output files dir
## ----------
mkdir $out_path

## ----------
## running analysis
## ----------
echo "Starting variant calling"
CALL_STRING="$TOOLDIR/falco/bin/falco --bam $bam_file --output $bam_base --ref $REF_FILE $FILTER_PARAM $MANIFEST_PARAM"
echo "[INFO] "$CALL_STRING
perl $CALL_STRING
echo "...done with variant calling"


## ----------
## create index html for main galaxy output
## ----------
echo "<!DOCTYPE html>" >> $html_out
echo "<html>" >> $html_out
echo "<head>" >> $html_out
echo "<style>" >> $html_out
echo "    body{ padding: 0px 20px; }" >> $html_out
echo "    h1{ color: red; }" >> $html_out
echo "    table{ border: 1px solid black; padding: 5px }" >> $html_out
echo "</style>" >> $html_out
echo "</head>" >> $html_out
echo "<body>" >> $html_out
echo "	<h1>FALCO</h1>" >> $html_out
echo "	<p>This page is way to get output files that are not implemented in galaxy history, it is not intended to be a user-friendly way of displaying anything ;)</p>" >> $html_out
#echo "	<a href=\"index.html\">HTML</a>" >> $html_out
echo "	<table><tbody>" >> $html_out
for file in *.vcf *.txt *stderr *stdout
#for file in *
do
	lineCount=`wc -l $file | cut -f 1 -d " "`
	echo "	<tr><td><a href=\"$file\">$file</a> has $lineCount lines</td></tr>" >> $html_out
	echo "  <tr><td> --> " `head -1 $file` "</td></tr>" >> $html_out
done
echo "	</tbody></table>" >> $html_out
echo "</body>" >> $html_out
echo "</html>" >> $html_out

## ----------
## creating galaxy history outputs
## ----------
#cp 'index.html' $html_out # this is the overview of samples html
#cp $bam_base'.html' $out_path/'out.html' # this is the sample html
cp $bam_base'.falco.vcf' $vcf_out

## ----------
## copy files to keep to output path
## ----------
#cp -r ./$bam_base/*png $out_path/$bam_base/
#cp -r ./* $out_path
cp *.vcf $out_path; cp *.txt $out_path; cp *_std* $out_path

## ----------
echo "END falco sh"
exit 0