3

bl
#!/bin/bash
#
# This is a front-end to BLAST+ blastn
# It will not run without two input files,
# one the target sequence, the other,
# the blast database. These can occur in
# that order without prefices, or can be
# preceeded, respectively, by "-in=" and
# "-in2=". Other parameters are optional:
# "-beg=" and "-end=" refer to the beginning
# and ending addresses of the actual target
# in the file named by "-in=". "-out="
# names an alternate output file. The
# default output file will be called by
# the base name of the input file with
# extension ".blast". Options do not require
# the "=" symbol. If a space follows the
# option flag, whether or not "=" is
# present, and if the next word is not
# preceeded by "-", that word becomes the
# the value. Otherwise, the parameter is
# set to the null string.
#
# Eric Kofoid, 8/12/2010
#

#
# "infile" is the input target file
# "dfile" is the input database file
# "outf" is the output file
# "loc1" is the start position
# "loc2" is the end position
#

rparms="" # remaining parameters, will be passed to program in a lump
if [[ -z $* ]] ; then # quit as there is nothing to work with
     echo "there is no input file";
     exit;
else
     while [[ "$*" ]] ; do
     parm=$1; # get next parameter from argument list
     shift; # pop pop it off list
     if [[ -z ${parm##-*} ]] ; then # this is a hyphenated option
              flag=$(echo $parm | sed 's/^-//' | sed 's/=.*$//');
         suff=$(echo $parm | sed 's/^[^=]*//' | sed 's/=//'); # blank if unattached
         if [[ -z $suff ]] ; then # value not attached
         if [[ ${1##-*} ]] ; then # the next word is not hyphenated
             suff=$1; #...so it becomes the value
             shift;
         fi
         fi
     else # unhyphenated, therefore an infile value
         flag="anInFile";
         ifile=$parm ;
     fi
     case $flag in
         i | in | infile)
         infile=$suff;
         ;;
         i2 | in2 | infile2)
         dfile=$suff;
         ;;
         o | out | outf | outfile)
             outf=$suff;
         ;;
         b | beg | begin)
         loc1=$suff;
         ;;
         e | end)
         loc2=$suff;
         ;;
         anInFile)
         if [[ $infile ]] ; then # target already set
             dfile=$ifile; # ...so set database file
         else
             infile=$ifile; # ...otherwise, set target file
         fi
         ;;
         *)
         rparms=$rparms$parm;
         ;;
     esac
     if [[ -z $outf ]] ; then # just set it locally to "foo.blast"
         outf=`pwd`"/foo.blast"
     fi
     if [[ -z $loc1 ]] ; then
         loc1=1
     fi
     if [[ -z $loc2 ]] ; then
         loc2=10000
     fi
     done
     echo "Target file:" $infile;
     echo "Database file:" $dfile;
     echo "Outfile:" $outf;
     echo "Target start:" $loc1;
     echo "Target stop:" $loc2;
     if [[ $rparms ]] ; then
     echo "The following extra options were passed to the program:" $rparms;
     fi
     exec blastn -task blastn -dust no -db $dfile -query $infile -query_loc $loc1-$loc2 -out $outf $rparms
fi