#!/usr/bin/perl -w
use strict;

#person in charge of this script
#this is usually the person currently in charge of scripts for the SGN project
my $script_maintainer='Dan Ilut <dci1@cornell.edu>';

#Add the working directory to @INC (array of directories with packages)
#This is a kludge to include custom packages without adding them to
#the standard @INC directories
use Cwd;
use lib cwd();

#local folders with custom packages
use lib '/soldb/local_scripts/custom_packages';

#local packages to use
use runtime;

if(!@ARGV or $ARGV[0]=~/help/){
    print "Usage:\n ./batch_blast_generic.pl -i input.fasta -o output_file -t target_db -p program -d t/f -e evalue -a alignment\n -d, -e and -a are optional, by default -d t (detail on) -e 10 and -a 0 will be used\n";
    exit;
}

my %args=();
my $i=0;
while($i<@ARGV){
    my $j=$i+1;
    $args{$ARGV[$i]}=$ARGV[$j];
    $i+=2;
}


my ($toblast_file, $output_file, $expect, $program, $blast_db, $bast_exec, $alignment, $detail);

$toblast_file=$args{-i};
$toblast_file or print "Error: no input file given.\n" and exit;

$output_file=$args{-o};
$output_file or print "Error: no output file given.\n" and exit;

my $results_file=$output_file.'.blast_results';
my $cleanresults_file=$output_file.'.blast_clean_results';

$program=$args{-p};
$program or print "Error: no program (blastn, tblastx, etc) specified.\n" and exit;

$blast_db=$args{-t};
$blast_db or print "Error: no target database specified.\n" and exit;

$detail=$args{-d};
$detail||='t';

$expect=$args{-e};
$expect||=10;

$alignment=$args{-a};
$alignment||=0;

my $blast_exec='/usr/local/sbin/blastall';
my $start_time=time;

print "Blasting $toblast_file against $blast_db using $program.\nUsing cutoff evalue of $expect.\nPrinting results to $results_file\n";

my $sys_cmd = "$blast_exec -e $expect -p $program -d $blast_db -i $toblast_file -o $results_file -m $alignment";

#### DEBUG ####
#print $sys_cmd;
###############

system($sys_cmd);


print "Cleaning up the results file $results_file.\nWriting clean results to $cleanresults_file.\n";

open (FILEIN, "$results_file");
open (FILEOUT, ">$cleanresults_file");


my $good_lines='false';
my $line;
while ($line = <FILEIN>){

    if($line=~/Query=/){
	print FILEOUT "\n" . '-'x80 . "\n$line";
    }
    if ($line=~/^Searching/){
	$good_lines='true';
	next;
    }

    if ($line=~/^>/){
	$detail eq 't' and $good_lines='true';
	$detail eq 'f' and $good_lines='false';
    }
    if ($line=~/Length =/){
	$good_lines='false';
    }

    if ($line=~/No hits/){
	print FILEOUT "$line\n";
	$good_lines='false';
    }

    unless ($good_lines eq 'false'){
	print FILEOUT "$line";
    }
}

close (FILEOUT);
close (FILEIN);

print "Done.\n";
runtime::runtime_print($start_time, 'Batch blast');
