#!/bin/bash
#
# Name:         blastn.sh
#
#
# Purpose:      This program uses blastn to match the query sequences
#		against the database and generates the blast output file  
#		in the current working directory. 
#               The filename of blast output file is the original fasta filename 
#		plus "_blast.out".
#		
# Author(s):    Umer Zeeshan Ijaz
#
# Address:      University of Glasgow
#               School of Engineering   
#               Glasgow
#               G12 8LT
#               Umer.Ijaz@glasgow.ac.uk
#
# Version:      0.1 

#store the input arguments
args=("$@")

# = Error checks =================== #
if [ $# -ne 2 ]; then
	echo "Usage: `basename $0` <fastafile> <blastdatabasename>" 
	exit 0
fi

if [ ! -f ${args[0]} ]; then
	echo "File doesn't exist. Please try again!"
	exit 0
fi

if [ `echo "${args[0]}"| sed 's/.*\.//'` != "fasta" ]; then
	echo "File doesn't have .fasta extension. Please try again!"
	exit 0
fi
# ================================== #

readFile=${args[0]}
blastDatabaseName=${args[1]}
blastFile=`echo "$(basename $readFile)" | cut -d'.' -f1`'_blast.out'
startTime=`date +%s`

blastn -db $blastDatabaseName -query $readFile -perc_identity 90 -out $blastFile -outfmt 6 -max_target_seqs 1 -evalue 0.00001 -dust no -num_threads 16

echo blastn took $(expr `date +%s` - $startTime) seconds for $readFile ...


