package MetaGene::Status;
BEGIN{ push(@INC, $ENV{LOCAL_PERL_MODULES}); }
use Carp;
use warnings;
use strict;
use MetaGene::ORF;
use Utils::Calc;

###################################################
sub stats {
    my $file = shift;
    my $orfs = shift;
    my $stats = calcStats( $orfs );
    open OUT, ">$file" or die "Can't open $file to write.";
    print OUT "\n"
	. "#. genes           = " . $stats->{numGenes} . "\n"
	. "#. complete genes  = " . $stats->{numComplete}  . " ( " . $stats->{perComplete}  . " %)" . "\n"
	. "#. lack 3'-end     = " . $stats->{numPartial3}  . " ( " . $stats->{perPartial3}  . " %)" . "\n"
	. "#. lack 5'-end     = " . $stats->{numPartial5}  . " ( " . $stats->{perPartial5}  . " %)" . "\n"
	. "#. lack both end   = " . $stats->{numPartial53} . " ( " . $stats->{perPartial53} . " %)" . "\n"
	. "\n";
}

###################################################
sub printStats {
    my $stats = shift;
    print ""
	. "#. genes           = " . $stats->{numGenes} . "\n"
	. "#. complete genes  = " . $stats->{numComplete}  . " ( " . $stats->{perComplete}  . " %)" . "\n"
	. "#. lack 3'-end     = " . $stats->{numPartial3}  . " ( " . $stats->{perPartial3}  . " %)" . "\n"
	. "#. lack 5'-end     = " . $stats->{numPartial5}  . " ( " . $stats->{perPartial5}  . " %)" . "\n"
	. "#. lack both end   = " . $stats->{numPartial53} . " ( " . $stats->{perPartial53} . " %)" . "\n";
}

###################################################
sub calcStats {
    my $orfs = shift;
    my $stats = {
	numComplete  => 0,
	numPartial3  => 0, 
	numPartial5  => 0, 
	numPartial53 => 0, 
	numGenes     => 0, 
	perComplete  => 0, 
	perPartial3  => 0, 
	perPartial5  => 0, 
	perPartial53 => 0
    };
    foreach my $orf (@$orfs){
	my $status = $orf->getStatus();
	++$stats->{numGenes};
	if( $status eq 'complete' ){
	    ++$stats->{numComplete};
 	} elsif( $status eq 'partial3' ) {
	    ++$stats->{numPartial3};
	} elsif( $status eq 'partial5' ) {
	    ++$stats->{numPartial5};
	} elsif( $status eq 'partial53' ) {
	    ++$stats->{numPartial53};
	}
    }
    $stats->{perComplete}  = Utils::Calc::round( $stats->{numComplete} /$stats->{numGenes}*100, 2 );
    $stats->{perPartial3}  = Utils::Calc::round( $stats->{numPartial3} /$stats->{numGenes}*100, 2 );
    $stats->{perPartial5}  = Utils::Calc::round( $stats->{numPartial5} /$stats->{numGenes}*100, 2 );
    $stats->{perPartial53} = Utils::Calc::round( $stats->{numPartial53}/$stats->{numGenes}*100, 2 );
    return $stats;
}

1;
