#!/bin/bash
set -e
inputFiles=($1)
outputDir=$3
outputFile=$3/index.html #$1
clonalType=$4
species=$5
locus=$6
filterproductive=$7

html=$2
dir="$(cd "$(dirname "$0")" && pwd)"
array=("$@")
echo "<html><h3>Progress</h3><table><tr><td>info</td></tr>" > $html
echo "<tr><td>-----------------------------------</td></tr>" >> $html

mkdir $PWD/igblastdatabase
unzip $dir/database.zip -d $PWD/igblastdatabase/
export IGDATA=$PWD/igblastdatabase/

function blastAndParse {
	echo "<tr><td>Starting blast of sample $3 of patient $2</td></tr>" >> $html
	echo "igblastn -germline_db_V $PWD/igblastdatabase/database/human_gl_V -germline_db_J $PWD/igblastdatabase/database/human_gl_J -germline_db_D $PWD/igblastdatabase/database/human_gl_D -domain_system imgt -query $1 -auxiliary_data $PWD/igblastdatabase/optional_file/human_gl.aux -show_translation -outfmt 3 > $PWD/$4"
	/home/galaxy/galaxy/igblast/igblastn -germline_db_V $PWD/igblastdatabase/database/human_gl_V -germline_db_J $PWD/igblastdatabase/database/human_gl_J -germline_db_D $PWD/igblastdatabase/database/human_gl_D -domain_system imgt -query $1 -auxiliary_data $PWD/igblastdatabase/optional_file/human_gl.aux -show_translation -outfmt 3 > $PWD/$4
	echo "<tr><td>Finished blast of sample $3 of patient $2</td></tr>" >> $html
	echo "<tr><td>Starting parse of sample $3 of patient $2</td></tr>" >> $html
	perl $dir/igparse.pl $PWD/$4 0 | grep -v "D:" | cut -f2- > "$5"
	echo "<tr><td>Finished parse of sample $3 of patient $2</td></tr>" >> $html
}

function imgtConvert {
	echo "<tr><td>Starting imgt convert of sample $3 of patient $2</td></tr>" >> $html
	bash $dir/imgt_loader.sh $1 $4 $5
	echo "<tr><td>Finished conversion of sample $3 of patient $2</td></tr>" >> $html
}

id=""
forwardSlash="/"
mergerInput=()
echo "Before loop"
count=1
for current in "${inputFiles[@]}"
do
	if [[ "$current" != *"$forwardSlash"* ]]; then
			id="$current"
			mergerInput+=($id)
			count=1
			continue
	fi
	echo "working on $current"
	fileName=$(basename $current)
	parsedFileName="${fileName%.*}"
	parsedFileName="$PWD/$parsedFileName.parsed"
	f=$(file $current)
	zipType="Zip archive"
  if [[ "$f" == *"$zipType"* ]]
	then
		echo "<tr><td>Sample $count of patient $id is a zip file, using IMGT Loader</td></tr>" >> $html
	  fileName=$(basename $current)
		imgtConvert $current $id $count $parsedFileName "${id}_${count}" #&
	else
		echo "<tr><td>Sample $count of patient $id is not a zip file, using igBLASTn</td></tr>" >> $html
		blastAndParse $current $id $count $fileName $parsedFileName &
	fi
	mergerInput+=($parsedFileName)
	count=$((count+1))
done
wait

echo "after loop"

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

python $dir/experimental_design.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

echo "after ED"

$dir/r_wrapper.sh $PWD/merged.txt $2 $outputDir $clonalType $species $locus $filterproductive

