#!/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='Teri Solow <tms45@cornell.edu>';

#local packages to use
use runtime;
use db_link;
use projects;

if (!$ARGV[0] || $ARGV[0] eq "help") {
    print <<EOF;

    Quick script to return a tab-delimited file for the specified unigene 
	build with the following information on each line:
	<est_id> <unigene_id> <direction> <start trim position> <end trim position> <start position in unigene> <end position in unigene>
	
	Enter 'all' as the build id to output one file for each build.
	
    Usage: $0 -p <project> -b <build_id | all> [ -o <outfile prefix> ]

EOF
    exit -1;
}

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

my %args=();

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

my $project=$args{'p'};
my $build=$args{'b'};
my $out_prefix=$args{'o'};

$project or die "Please specify a project (cgn, fgn, pgn) using the -p flag\n";
$build or die "No build specified, try '-b build_id (-l all to process all builds)'\n";
$out_prefix||="/tmp";

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

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

my $start_time=time;

# 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);

# fill an array with each library which will be processed (this only 
# matters if 'all' was entered)
my @builds;
if ($build eq 'all') {
	my $ql = $dbh->prepare("select unigene_build_id from unigene_build;");
	$ql->execute();
	while(my ($build_id) = $ql->fetchrow_array()) {
		push @builds, $build_id;
	}
}
else {
	push @builds, $build;
}

foreach (@builds) {
	my $out_file = $out_prefix."/unigene-est-".$_.".txt";
	open OUT, ">$out_file" or die "Can't open $out_file for writing: $!\n";
	# first get info from assemblies
	my $qa = $dbh->prepare("select distinct c.seq_id, u.unigene_id, c.read_direction, c.start_trim, c.end_trim, c.start_location, c.end_location from unigenes as u left join unigene_assembly_component as c on c.unigene_assembly_id=u.unigene_element_id left join other_identifier as o on o.local_db_id=c.seq_id where u.unigene_build_id='$_' and u.unigene_element_type_id='1' order by u.unigene_id");
	$qa->execute();
	while(my ($eid, $uid, $dir, $st, $et, $sp, $ep) = $qa->fetchrow_array()) {
		print OUT "$eid\t$uid\t$dir\t$st\t$et\t$sp\t$ep\n";
	}
	# then from singletons
	my $qs = $dbh->prepare("select distinct s.seq_id, u.unigene_id, e.read_direction, q.front_trim_end, q.tail_trim_start, length(t.sequence_data) from unigenes as u left join unigene_singleton as s on s.unigene_singleton_id=u.unigene_element_id left join est_info as e on s.seq_id=e.seq_id left join trimmed_sequence as t on s.seq_id=t.seq_id left join quality_trim as q on s.seq_id=q.seq_id left join other_identifier as o on o.local_db_id=s.seq_id where u.unigene_build_id='$_' and u.unigene_element_type_id='2' order by u.unigene_id");
	$qs->execute();
	while(my ($eid, $uid, $dir, $st, $et, $length) = $qs->fetchrow_array()) {
		print OUT "$eid\t$uid\t$dir\t$st\t$et\t0\t$length\n";
	}
	close OUT;
}

#close the database link
db_link::disconnect_db($dbh);

runtime::runtime_print($start_time, "Unigene dump for $build");
