#!/bin/bash

export IGDATA=/home/david/Galaxy/galaxy-dist/toolsheddependencies/igBlastn/1.0.0/davidvanzessen/igblast_human/1c64c977624e/ncbi-igblast-1.0.0/;

clonalType=${@:(-3):1}
html=${@:(-2):1}
imageDir=${@:(-1):1}
dir="$(cd "$(dirname "$0")" && pwd)"
fileCount=`expr $# - 3`
array=("$@")
echo "<html><h3>Progress</h3><table><tr><td>info</td></tr>" > $html
for var in "$@"
do
	echo "$var" >> $html
done
echo "<tr><td>-----------------------------------</td></tr>" >> $html
limit=`expr $fileCount / 2`

function blastAndParse {
	echo "<tr><td>Starting blast of $2</td></tr>" >> $html
	fileName=$(basename $1)
	$IGDATA/bin/igblastn -germline_db_V $IGDATA/database/human_gl_V -germline_db_J $IGDATA/database/human_gl_J -germline_db_D $IGDATA/database/human_gl_D -domain_system imgt -query $1 -auxiliary_data $IGDATA/optional_file/human_gl.aux -show_translation -outfmt 3 > $PWD/$fileName
	echo "<tr><td>Finished blast of $2</td></tr>" >> $html
	
	echo "<tr><td>Starting parse of $2</td></tr>" >> $html
	parsedFileName="${fileName%.*}"
	parsedFileName="$parsedFileName.parsed"
	perl $dir/igparse.pl $PWD/$fileName 0 | grep -v "D:" | cut -f2- > $parsedFileName
	echo "<tr><td>Finished parse of $2</td></tr>" >> $html
}

for ((i=0;i<$fileCount;i=$((i+2))))
do
	next=$((i+1))
	blastAndParse ${array[$i]} ${array[$next]} &
done
wait



echo "<tr><td>-----------------------------------</td></tr>" >> $html
echo "<tr><td>merging</td></tr>" >> $html

count=0
for ((i=0;i<$fileCount;i=$((i+2))))
do
	id=$((i+1))
	place=$((count+limit))
	fn=$(basename ${array[$i]})
	fn="${fn%.*}"
	mergeInputs[$count]="$PWD/$fn.parsed"
	mergeIDs[$place]=${array[$id]}
	count=$((count+1))
done

python $dir/igblastmerge.py --input ${mergeInputs[*]} --id ${mergeIDs[*]} --output $PWD/merged.txt

echo "<tr><td>done</td></tr>" >> $html
echo "<tr><td>-----------------------------------</td></tr>" >> $html
echo "<tr><td>plotting</td></tr>" >> $html


inputFile=$PWD/merged.txt
outputFile=$html
outputDir=$imageDir
mkdir $outputDir
Rscript --verbose $dir/RScript.r $inputFile $outputDir $outputDir $clonalType 2>&1
echo "<html>" > $outputFile
echo "<img src='VPlot.png'/>" >> $outputFile
echo "<img src='DPlot.png'/>" >> $outputFile
echo "<img src='JPlot.png'/>" >> $outputFile

samples=`cat $outputDir/samples.txt`
count=1
echo "<table border='1'>" >> $outputFile
for sample in $samples; do
	echo "<tr><td colspan='3' height='100'></td>" >> $outputFile
	echo "<tr><td colspan='3'><h1>$sample</h1></td></tr>" >> $outputFile
	echo "<tr><td><h2>V-D Heatmap:</h2></td><td><h2>V-J Heatmap:</h2></td><td><h2>D-J Heatmap:</h2></td></tr><tr>" >> $outputFile
	mv "$outputDir/HeatmapVD_$sample.png" "$outputDir/VD_$sample.png"
	echo "<td><img src='VD_$sample.png'/></td>" >> $outputFile
	mv "$outputDir/HeatmapVJ_$sample.png" "$outputDir/VJ_$sample.png"
	echo "<td><img src='VJ_$sample.png'/></td>" >> $outputFile
	mv "$outputDir/HeatmapDJ_$sample.png" "$outputDir/DJ_$sample.png"
	echo "<td><img src='DJ_$sample.png'/></td></tr>" >> $outputFile
	count=$((count+1))
done
echo "</table>" >> $outputFile
echo "<h1>$clonalType</h1>" >> $outputFile

echo "</html>" >> $2
