#!/usr/bin/perl -w

use strict;
use lib "/usr2/people/khuang/bin/module";
use lib "/usr2/people/khuang/bin/Genomics/Perl/modules";
use GenomicsUtils;
use myGenome;

my $helpStr = qq{

    critica2vimss.sql <tax ID> <genome fna> <genome source> <model outfile> <model source>

	<model source>: "Critica"
	<genome fna>: the exact same genome fna file that was used to predict models

	For example: Geobacter metallireducens from ORNL and Critica models
	
	critica2vimss.sql 28232 genome.fna ORNL models.critica Critica

};

unless (@ARGV==5){die "$helpStr";}
my $hostname = "lut";
connect($hostname);
my ($taxId, $genomeF, $gSource, $modelF, $mSource) = @ARGV;
unless ($mSource eq "Critica"){die "$helpStr";}

#get the hash of scaffolds in the genome fna file
my $scaffoldsR = myGenome::fasta2hash($genomeF);
print "done===\n";
#get the hash of scaffolds with arrays of models for each scaffold
my $modelsR = myGenome::model2hash($modelF, $mSource, $scaffoldsR);
print "done===\n";
#get the last used scaffoldId and locusId

my $tempR = GenomicsUtils::query(qq{SELECT scaffoldId FROM Scaffold ORDER BY scaffoldId DESC LIMIT 1});
my $lastScaffoldId = $tempR->[0][0];
print "done===\n";
$tempR = GenomicsUtils::query(qq{SELECT locusId FROM Locus ORDER BY locusId DESC LIMIT 1});
my $lastOrfId = $tempR->[0][0];
print "done===\n";
$tempR = GenomicsUtils::query(qq{SELECT posId FROM Position ORDER BY posId DESC LIMIT 1});
my $lastPosId = $tempR->[0][0];
print "done===\n";
open SCAFFOLD, ">Scaffold.$taxId.$gSource.txt" or die "Can't write to Scaffold.$taxId.$gSource.txt";
open SCAFFOLDSEQ, ">ScaffoldSeq.$taxId.$gSource.txt" or die "Can't write to ScaffoldSeq.$gSource.txt";
open POSITION, ">Position.$taxId.$gSource.$mSource.txt" or die "Can't write to Position.$taxId.$gSource.$mSource.txt";
open Locus, ">Locus.$taxId.$gSource.$mSource.txt" or die "Can't write to Locus.$taxId.$gSource.$mSource.txt";
open LocusSEQ, ">LocusSeq.$taxId.$gSource.$mSource.txt" or die "Can't write to LocusSeq.$taxId.$gSource.$mSource.txt";
open AASEQ, ">AASeq.$taxId.$gSource.$mSource.txt" or die "Can't write to AASeq.$taxId.$gSource.$mSource.txt";
foreach my $key (keys %$modelsR){
    my $sId = $lastScaffoldId + 1;
    print $sId."===\n";
## some default values that might need to be altered:
    my $chr_num = 1;
    my $isCircular = 1;
    my $sLength = length($scaffoldsR->{$key});
    my $sFile = $gSource;
    my $isGenomic = 1;
    my $gi = "";
    my $comment = "";
    my $isActive = 1;
    my $isPartial = 1;

    my $version = 1;
    my $priority = 1;
    my $created=`date '+%Y-%m-%d'`;
    $created =~ s/\n//;
    my $evidence = $mSource;
###########################


    print SCAFFOLD join "\t", $sId, $chr_num, $isCircular, $sLength, $sFile, $isGenomic, $gi, $taxId, $comment, $isActive, $isPartial."\n"; 
    print SCAFFOLDSEQ join "\t", $sId, $scaffoldsR->{$key}."\n";
    my @models = sort {$a<=>$b} keys %{$modelsR->{$key}};
    foreach (@models){
	my $locusId = $lastOrfId + 1;
	my $posId = $lastPosId + 1;
	my ($strand, $start, $end, $dna, $prot) = split /\t/, $modelsR->{$key}{$_};
	print POSITION join "\t", $posId, $strand, $start, $end, $sId, "1\n";
	
	print Locus join "\t", $locusId, $version, $priority, $created, $posId, $evidence, $sId."\n";
	print LocusSEQ join "\t", $locusId, $version, $dna."\n";
	print AASEQ join "\t", $locusId, $version, $prot."\n";
	$lastOrfId = $locusId;
	$lastPosId = $posId; 
    }
    $lastScaffoldId = $sId;
}
close SCAFFOLD;
close SCAFFOLDSEQ;
close POSITION;
close Locus;
close LocusSEQ;
close AASEQ;
disconnect();
exit;
