#!/usr/bin/env perl

=head1 NAME

fasta2AlignerDb.pl

=head1 SYNOPSIS
 
fasta2AlignerDb.pl [options] -o 'options' -f <fasta>
 
  Options:
  -h    this help message
  -if   fasta 
  -o    command line options for formatdb

Location of blastall must be defined in environmental
variable "BLAST_LOC". This script constructs the command line
and executes it with a system call. Creates teh database at
the location of the input fasta file.


=head1 DESCRIPTION

Creates a blast database of the input Fasta where the fasta file is located.
Uses formatdb, the location of which is defined by the environmental variable
BLAST_LOC.  

=head1 VERSION

$Revision: 1.3 $

$Date: 2009-08-26 17:18:14 $

=head1 AUTHOR(S)

Kurt M. LaButti

=head1 HISTORY

=over

=item *

K.LaButti 2008/10/23 Creation

=back

=cut

use strict;
use warnings;
use Pod::Usage;
use FindBin qw($RealBin);
use lib "$RealBin/../lib";
use PGF::Utilities::Properties;  
use Getopt::Long;
use vars qw( $optHelp $optInputOptions $optInputFasta);
use Carp;


#============================================================================#
# INPUT VALIDATION
#============================================================================#
if( !GetOptions(
		"h"      =>\$optHelp,
                "o=s"    =>\$optInputOptions,
		"if=s"   =>\$optInputFasta,
		)
    ) {
    printhelp();
}

if ( defined $optHelp ) {
    printhelp();
}

printhelp() if !$optInputFasta;
printhelp() if !$optInputOptions;



#============================================================================#
# INITIALIZE VARIABLES
#============================================================================#
unless (-s $optInputFasta){
    print STDERR "Can't find $optInputFasta ($!)\n";
    exit 1;
}

my $blastAllLoc;
if ( defined $ENV{BLAST_LOC} ) {
    $blastAllLoc = $ENV{BLAST_LOC};
} else { 
    confess "Can't find the location of blastall using BLAST_LOC environmental variable\n";
}

my $formatDbCommand = "$blastAllLoc/formatdb $optInputOptions $optInputFasta";



#============================================================================#
# MAIN
#============================================================================#
print "==$formatDbCommand==\n";
my $success = system("$formatDbCommand");

confess "Reference database creation failed ($success)\n" if $success != 0;






#============================================================================#
# SUBROUTINES
#============================================================================#
sub printhelp {
    pod2usage(-verbose=>1);
    exit 1;
}
