use strict;
use SeedEnv;

use Data::Dumper;

use SAPserver;
use ANNOserver;

my $sap = SAPserver->new();
my $anno = ANNOserver->new();

my $genomes = $sap->all_genomes(-prokaryotic => 1);

my @vibrio_genome_ids = ();

foreach my $genome_id (keys %$genomes)
{
    my $genome_name = $genomes->{$genome_id};
    if ($genome_name =~ /^Vibrio/)
    {
	push(@vibrio_genome_ids, $genome_id);
	print "We will recall $genome_name\n";
    }
}

foreach my $genome_id (@vibrio_genome_ids)
{
    print "Process $genome_id\n";
    
    my $protein_data = $sap->all_proteins({ -id => $genome_id });

    my @seq_entries;

    my @id_list;
    foreach my $prot_id (keys %$protein_data)
    {
	my $protein = $protein_data->{$prot_id};
	push(@seq_entries, [$prot_id, undef, $protein]);
	push(@id_list, $prot_id);
    }

    my $functions = $sap->ids_to_functions(-ids => \@id_list);

    my $resultHandle = $anno->assign_function_to_prot(-input => \@seq_entries,
						      -kmer => 8,
						      -scoreThreshold => 3,
						      -hitThreshold => 5,
						      -seqHitThreshold => 3,
						      -detailed => 1);

    while (my $result = $resultHandle->get_next())
    {
	my($id, $function, $otu, $score, $non_overlap_count, $overlap_count, $details) = @$result;

	my $old_function = $functions->{$id};

	if ($old_function ne $function)
	{
	    print "$id\n";
	    print "\t$old_function\n";
	    print "\t$function\n";
	    for my $detail (@$details)
	    {
		my($offset, $oligo, $func, $otu) = @$detail;
		print "\t\t$offset\t$oligo\t$func\n";
	    }
	    # print "$id\t$old_function\t$function\n";
	}
    }

    exit;
}



exit 0;
