#/usr/bin/perl


#set the location for custom packages used by the scripts
use lib '/soldb/www/perllib';
use runtime;
use db_link;
use projects;


@ARGV or print "No input parameters, proceeding with default.\n";

my @arg_pairs = split (/\-/, (join ' ', @ARGV));

my %args=();

foreach (@arg_pairs){

    $_ or next;
    my ($flag, $val)=split /\s+/;
    $args{$flag}=$val;
}

my $filein = $args{'i'};
my $project = $args{'p'};
my $target_id = $args{'t'};


$project or die "Please specify a project (cgn, fgn, pgn) using the -p flag\n";
$target_id or die "Please specify the blast target database id using the -t flag\n";


my ($db, $usr) = @{projects::get_db_info($project)};
$db or die "No known database for project $project";

my %annotated_clones=();


#parse the cleaned blast results
open FILEIN, $filein;

my $clone;
my $hit_summary;
my $hit_details;
my $detail;

while(<FILEIN>){

    chomp;

#skip blank lines
    if (/^\s*$/){
	next;
    }

#set the current clone name
    if(/^Query=\s+([^\-]+\-[^\-]+\-[^\-]+)\-/){
	$clone = $1;
	next;
    }

#check to see if we are in the summary or details section
    if (/^Sequences\s/){
	$hit_summary = 'true';
	next;
    }

    if (/^>/){
	$hit_summary = 'false';
	$hit_details = 'true';
    }

    if (/^---/){
	$hit_details='false';
    }


#parse the summary
    if ($hit_summary eq 'true'){
	my ($defline, $spacer1, $score, $spacer2, $evalue) = unpack ("A67a3A3a3A5", $_);

#prepare for database load
	$score=~s/\'/\\\'/g;
	$score=~s/\"/\\\"/g;
	$evalue=~s/\'/\\\'/g;
	$evalue=~s/\"/\\\"/g;

	push @{$annotated_clones{$clone}{'summary'}}, [$score, $evalue];
	next;
    }


#parse the detail
    if ($hit_details eq 'true'){
	if (/^>(.+)$/){

	    if($detail){

#cleanup spacing issues
		$detail =~ s/\s{3,}/  /g;
		$detail =~ s/\'/\\\'/g;
		$detail =~ s/\"/\\\"/g;
		push @{$annotated_clones{$clone}{'detail'}}, $detail;
	    }

	    $detail = $1;
	}
	else{
	    $detail .= $_;
	}
	next;
    }

}

close FILEIN;


#    foreach (keys %annotated_clones){
#	print "$_:\n";
#	my $i;
#	for ($i=0;$i<@{$annotated_clones{$_}{'summary'}};$i++){
#	    print "$i:\tScore=" . $annotated_clones{$_}{'summary'}[$i] . "\tEvalue=" . $annotated_clones{$_}{'summary'}[$i] . "\n";
#	    print "Detail: " . $annotated_clones{$_}{'detail'}[$i] . "\n";
#	}
#	print '='x80 . "\n"; 
#    }




# try to open the database
my $dbh = db_link::connect_db($db, $usr) or die "couldn't open database link\n";
my ($stm, $sth, $rv, $rc);

foreach (keys %annotated_clones){

#get the sequence id
    my $seq_id;
    $stm="select local_db_id from other_identifier where external_id='$_'";
    $sth = $dbh->prepare($stm) 
	|| die "Can't prepare statement: $DBI::errstr";
    $rv = $sth->execute
	|| die "Can't execute statement: $DBI::errstr";
    $rc = $sth->bind_columns(\$seq_id);
    unless($sth->fetch){
	print "couldn't find id for $_\n";
	next;
    }


#get the trimmed sequence id
    my $trimmed_seq_id;
    $stm="select trimmed_seq_id from trimmed_sequence where seq_id='$seq_id'";
    $sth = $dbh->prepare($stm) 
	|| die "Can't prepare statement: $DBI::errstr";
    $rv = $sth->execute
	|| die "Can't execute statement: $DBI::errstr";
    $rc = $sth->bind_columns(\$trimmed_seq_id);
    unless($sth->fetch){
	print "couldn't find trimmed id for $_\n";
	next;
    }

    my $i;
    for ($i=0;$i<@{$annotated_clones{$_}{'summary'}};$i++){
	$stm="insert into blast_result (seq_id, trimmed_seq_id, blast_target_id, match_description, match_score, evalue) values ('$seq_id', '$trimmed_seq_id', '$target_id', '$annotated_clones{$_}{detail}[$i]', '$annotated_clones{$_}{summary}[$i][0]', '$annotated_clones{$_}{summary}[$i][1]')";
    $sth = $dbh->prepare($stm) 
	|| die "Can't prepare statement: $DBI::errstr";
    $rv = $sth->execute
	|| die "Can't execute statement: $DBI::errstr";

    }

}

#close the db link

db_link::disconnect_db($dbh);

