#!/bin/bash -e
# Translate pDNA SAM file to BLASTX
# Daniel Huson, 9.2012

if [ $# != "4" ] && [ $# != "2" ]
then
	echo "Usage: z5_sam2blastx.sh DNA-file SAM-file BLASTX-file pauda-index"
	echo "	DNA-file: file containing original DNA reads in FastA format"
	echo "	SAM-file: file generated by bowtie containing pDNA matches"
	echo "	BLASTX-file: Name of new BLASTX file"
	echo "	pauda-index: location of pauda-index"
	echo "Usage: z5_sam2blastx.sh listfile index-dir"
	echo "	listfile: file containing list of DNA-file SAM-file BLASTX-file triplets"
	echo "	pauda-index: location of pauda-index"
	exit 1
fi

bin_dir=`dirname "$0"`
bin_dir=`cd "$bin_dir" && pwd`

if [ $# == 2 ]
then
	index_dir=$2
else
	index_dir=$4
fi

if [ ! -e $index_dir ]
then
	echo "PAUDA index not found: $index_dir"
	exit 1
fi

original=$index_dir/ref.faa
map1=$index_dir/ref.map1
map2=$index_dir/ref.map2

min_bitscore=30
blosum=BLOSUM62
gap_open=11
gap_extend=1

bin_dir=`dirname $0`       
bin_dir=`cd $bin_dir && pwd` 
jar=$bin_dir/../lib/Pauda.jar

if [ $# ==  2 ]
then
	list=$1
	java -Xmx8G  -Duser.language=en -Duser.region=US -cp "$jar" megan.pauda.SAM2BlastX -r "$original"  -m1 "$map1" -m2 "$map2" -L "$list" -min $min_bitscore  -bm $blosum -gop $gap_open -gep $gap_extend
	exit 0
elif [ $# == 4 ]
then
	dnafile=$1
	samfile=$2
	blastxfile=$3
	name=`echo $samfile|sed "s/.sam//"`

	java -Xmx8G  -Duser.language=en -Duser.region=US -cp "$jar" megan.pauda.SAM2BlastX -r $original  -m1 "$map1" -m2 "$map2" -i "$dnafile" -b "$samfile" -o $blastxfile -min $min_bitscore -bm $blosum -gop $gap_open -gep $gap_extend
	exit 0
fi
