# Stop script on any error.
set -ue

# If you set the so called BLASTDB variable
# blast will find the files there automatically.
# Makes for shorter commands.

# This will be the directory we store BLAST databases in.
export BLASTDB=~/refs/blastdb

# Make the BLAST database directory.
mkdir -p $BLASTDB

# Temporarily switch to the BLASTDB directory and run the blast database updater.
# It will only update the data if it is incomplete or has changed.
(cd $BLASTDB && update_blastdb.pl --quiet --decompress 16SMicrobial)

# Get the "mystery sequence" from the book site.
wget -q -nc http://data.biostarhandbook.com/fasta/mystery3.fa

# We can list the short database name because BLASTDB is set properly
blastn -db 16SMicrobial -query mystery3.fa > blast-1-results.txt

# If you don't set the BLASTB the same command will be:
# blastn -db ~/refs/blastdb/16SMicrobial -query mystery3.fa > blast-1-default-results.txt

# We can format the output differently.
# Look at the blast -help for information on what each field means.
blastn -db 16SMicrobial -query mystery3.fa -outfmt 6 > blast-2-results.txt

# We can also selectively output only certain columns:
# subject accession, query accession and percent identity.
# Look at blast --help for information on how to encode the format string.
blastn -db 16SMicrobial -query mystery3.fa -outfmt "6 sacc qacc pident" > blast-3-results.txt

# Generate a report of top hits.
echo "Top ten hits with highest percent identity":
cat blast-3-results.txt | sort -k3,3rn | head -10