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

echo "Start falco sh"

## ----------
## Variables setup
## ----------
#bam_file=$1
#bam_name=$2
#html_out=$3
#vcf_out=$4
#out_path=$5
source $1
JAR_PATH=$2
#JAR_PATH=$3

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

## ----------
## Create extra init script
## ----------
#if [ $design_file != 'None' && $design_file != '' ]; then BED_PATH=$design_file; fi
#if [ $manifest_file != 'None' && $manifest_file != '' ]; then MAN_PATH=$manifest_file; fi
if [ $design_file != 'None' ] # Galaxy default is "None"
then 
	BED_PATH=$design_file
else
	BED_PATH="$TOOLDIR/tool-data/GALAXY_TSACP.bed"
fi

if [ $manifest_file != 'None' ] # Galaxy default is "None"
then 
	MAN_PATH=$manifest_file
else
	MAN_PATH="$TOOLDIR/tool-data/GALAXY_TruSeq_Amplicon_Cancer_Panel_Manifest_AFP1_PN15032433.txt"	
fi

echo "BED:"$design_file
echo "MAN:"$manifest_file
echo "BED chosen:"$BED_PATH
echo "MAN chosen:"$MAN_PATH
#echo "VARSCAN_PATH1:"$SNPEFF_JAR_PATH
#echo "VARSCAN_PATH2:"\$SNPEFF_JAR_PATH
echo "JAR_PATH1:"$JAR_PATH
#echo "JAR_PATH2:"$jar_path2

init_file="galaxy_init.sh"
echo '#!/bin/bash' > $init_file

## executables
#echo "snpEff=/ccagc/lib/snpEff/snpEff_v3_4" >> $init_file
echo "snpEff=\$SNPEFF_JAR_PATH/" >> $init_file
#echo "snpSift=/ccagc/lib/SnpSift" >> $init_file
echo "snpSift=\$SNPEFF_JAR_PATH/" >> $init_file
#echo "varscan=/ccagc/lib/VarScan/VarScan.v2.2.11.jar" >> $init_file
echo "varscan=\$JAVA_JAR_PATH/VarScan.v2.3.5.jar" >> $init_file

## files
echo "targetBed=$BED_PATH" >> $init_file
echo "manifest=$MAN_PATH" >> $init_file

## ----------
## Status / debug
## ----------
echo "BAM: "$bam_file
echo "OUT: "$bam_name
echo "BAMBASE: "$bam_base
echo "OUTPATH: "$out_path
echo "HTMLOUT: "$html_out
echo "VCFOUT : "$vcf_out
echo "INIT   : "$init_file

source $init_file

echo "--- sam ---"
samtools
echo "--- bcf ---"
bcftools
echo "--- snpsift ---"
java -jar $snpSift/SnpSift.jar
echo "--- snpeff ---"
java -jar $snpEff/snpeff.jar
echo "--- varscan ---"
java -jar $varScan

exit 0

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

## ----------
## running analysis
## ----------
echo "Starting VcAn"
$TOOLDIR/falco/amplicon-VcAn-TSACP.sh $bam_file $bam_base
#$TOOLDIR/falco/amplicon-AlVcAn-TSACP.sh ## this contains more

echo "Starting amplicon-Report"
$TOOLDIR/falco/amplicon-Report.sh $bam_base

echo "Starting QC"
$TOOLDIR/falco/amplicon-QC.sh $bam_file

#echo "Starting plots.R"
#$TOOLDIR/falco/misc/plots.R $bam_base.qc.ann.txt $bam_base.qc.targets.txt $bam_base.pdf

echo "Starting mkHtmlReport"
$TOOLDIR/falco/misc/mkHtmlReport.pl ./ ./



## ----------
## 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 "	<a href=\"index.html\">HTML</a>" >> $html_out
echo "	<table><tbody>" >> $html_out
#for file in *.vcf *.txt *.tsv
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: " `head $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'.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 *.tsv $out_path; cp *.txt $out_path; cp *.html $out_path

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