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

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

use lib '/soldb/website/perllib';

#local packages to use
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 $out_dir=$args{'o'};
my $in_dir=$args{'i'};
my $source_project=$args{'s'};
my $verbose=$args{'v'};

$source_project or die  "Please specify a project (cgn, fgn, pgn) using the -s flag\n";
my ($db, $usr) = @{projects::get_db_info($source_project)};

$db or die "No known database for project $source_project";


#set defaults for io, script filename, etc
$out_dir ||= "/tmp/";
$in_dir ||= "/tmp/";

#uniform trailing slashes
$out_dir=~/\/$/ or $out_dir.='/';
$in_dir=~/\/$/ or $in_dir.='/';

my $target_db='pgn';

my $minmatch=12;
my $minscore=20;

my $script_file=__FILE__;

my $load_date="20021003000000";


#main body of script
#####################

my $start_time=time;

#get the sequences from the database

$verbose and print "Getting sequences\n";

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


#get the raw sequences that haven't been uploaded yet
my %raw_seq=();
my ($seq_id, $seq_data, $data_type, $data_format, $last_mod);
$stm = "select seq_id, sequence_data, data_type, data_format, last_modified from raw_sequence where last_modified < $load_date";
$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, \$seq_data, \$data_type, \$data_format, \$last_mod);

while($sth->fetch){
    $raw_seq{$seq_id}{'seq'} = [$seq_data, $data_type, $data_format, $last_mod];
}

$verbose and print "Sequences retrieved, getting quality data.\n";

#get the raw quality values
my %raw_qual=();
my ($qual_val);

foreach (keys %raw_seq){
    $stm = "select quality_values from raw_sequence_quality where 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(\$qual_val);
    while($sth->fetch){
	$raw_seq{$_}{'qual'} = $qual_val;
    }
}


  db_link::disconnect_db($dbh);

#my $dbh_target = db_link::connect_db($target_db, $usr) or die "couldn't open database link to $target_db\n";
#my ($stm2, $sth2, $rv2, $rc2);

$verbose and print "Quality values retrieved, outputing to new db.\n";
   
    my $count=0;
    foreach (keys %raw_seq){
	print "$_\n";
	$count++;
    } 

    print "$count total \n";


 runtime::runtime_print($start_time, "PGN load");

