#!/bin/bash

export IGDATA=/home/david/tmp/ncbi-igblast-1.0.0;

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

id=${inputData[0]}
forwardSlash="/"
mergerInput=()
count=0
for current in $inputData; do
    if [[ "$current" != *"$forwardSlash"* ]]; then
        id=$current
        count=0
        mergerInput+=($id)
        continue
    fi
    fileName=$(basename $current)
    convertedFileName="${fileName%.*}"
    convertedFileName="$PWD/$convertedFileName.converted"
	bash $dir/imgtconvert.sh $current $id $count $convertedFileName
	mergerInput+=($convertedFileName)
	count=$((count+1))
done



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

python $dir/igblastmerge.py ${mergerInput[*]}  --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'><caption><a href='allUnique.tsv'><h3>$clonalType</h3></a></caption>" >> $outputFile
hasReplicateColumn="$(if head -n 1 $inputFile | grep -q 'Replicate'; then echo 'Yes'; else echo 'No'; fi)"
for sample in $samples; do
	clonalityScore="$(cat $outputDir/ClonalityScore_$sample.csv)"
	echo "<tr><td colspan='3' height='100'></td></tr>" >> $outputFile
	echo "<tr><td colspan='3'><h1>$sample</h1></td></tr>" >> $outputFile

	echo "$hasReplicateColumn"
	#if its a 'new' merged file with replicate info
	if [[ "$hasReplicateColumn" == "Yes" ]] ; then
		echo "<tr><td colspan='3'><a href='clonality_$sample.tsv'><h2>Clonality Score: $clonalityScore</h2></a></td></tr>" >> $outputFile

		#replicate,reads,squared
		echo "<tr><td colspan='3'><table border='1'><tr><th>Replicate ID</th><th>Number of Reads</th><th>Reads Squared</th></tr>" >> $outputFile
		while IFS=, read replicate reads squared
		do

			echo "<tr><td><a href='clonality_${sample}_$replicate.tsv'>$replicate</a></td><td>$reads</td><td>$squared</td></tr>" >> $outputFile
		done < $outputDir/ReplicateReads_$sample.csv

		#sum of reads and reads squared
		while IFS=, read readsSum squaredSum
			do
				echo "<tr><td>Sum</td><td>$readsSum</td><td>$squaredSum</td></tr>" >> $outputFile
		done < $outputDir/ReplicateSumReads_$sample.csv

		echo "</table></td></tr>" >> $outputFile

		#overview
		echo "<tr><td colspan='3'><table border='1'><tr><th>Coincidence Type</th><th>Raw Coincidence Freq</th><th>Coincidence Weight</th><th>Coincidences, Weighted</th></tr>" >> $outputFile
		while IFS=, read type count weight weightedCount
		do
			echo "<tr><td>$type</td><td>$count</td><td>$weight</td><td>$weightedCount</td></tr>" >> $outputFile
		done < $outputDir/ClonalityOverView_$sample.csv
		echo "</table></td></tr>" >> $outputFile
	fi

	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 "</html>" >> $outputFile
