package BLAST::M9FileIO;
BEGIN{ push(@INC, $ENV{LOCUS_PERL_MODULES}); }
use Carp;
use warnings;
use strict;
use BLAST::M9Hit;

###################################################
sub parse {
    my $filename = shift or croak "Input file name is not specified.";
    my $hits = [];
    open IN, $filename or croak "Can't open $filename to read.";
    while(<IN>){
	chomp;
	next if(/^#/);
	my @cols = split("\t", $_);
	if( @cols == 12 ){
	    my $hit = BLAST::M9Hit->new();
	    $hit->setAll( $_ );
	    push( @$hits, $hit );
	}
    }
    close IN;
    return $hits;
}

###################################################
sub save {
    my $filename = shift or croak "Input file name is not specified.";
    my $hits = shift;
    open OUT, ">$filename" or croak "Can't open $filename to write.";
    foreach my $hit (@$hits){
	print OUT ""
	    . $hit->getQueryID()         . "\t"
	    . $hit->getSubjectID()       . "\t"
	    . $hit->getPercentIdentity() . "\t"
	    . $hit->getAlignmentLength() . "\t"
	    . $hit->getNumMismatches()   . "\t"
	    . $hit->getNumGapOpens()     . "\t"
	    . $hit->getQueryStart()      . "\t"
	    . $hit->getQueryEnd()        . "\t"
	    . $hit->getSubjectStart()    . "\t"
	    . $hit->getSubjectEnd()      . "\t"
	    . $hit->getEValue()          . "\t"
	    . $hit->getBitScore()        . "\n";
    }
    close OUT;
}

1;
