# -*- perl -*-

######################### We start with some black magic to print on failure.

# Change 1..1 below to 1..last_test_to_print .
# (It may become useful if the test is moved to ./t subdirectory.)

BEGIN { $| = 1; print "1..12\n"; }
END {print "not ok 1\n" unless $loaded;}
use Align::NW;
$loaded = 1;
print "ok 1\n";

######################### End of black magic.

my $N = 2;
sub Not { print "not " }
sub OK  { print "ok ", $N++, "\n" }


my $T1 = { a        => 'abcde',
	   b        => 'abcde',
	   score    => 10,
	   expected => <<END };
abcde
|||||
abcde
END

my $T2 = { a        => 'abcdefgh',
	   b        => 'abcxefgh',
	   score    => 12,
	   expected => <<END };
abcdefgh
||| ||||
abcxefgh
END

my $T3 = { a        => 'abcdefgh',
	   b        => 'bcdefghi',
	   score    => 14,
	   expected => <<END };
abcdefgh
 |||||||
 bcdefghi
END

my $T4 = { a        => 'abcdefghijklmnopqrst',
	   b        => 'abcdijklqrst',
	   score    => 6,
	   expected => <<END };
abcdefghijklmnopqrst
||||    ||||    ||||
abcd....ijkl....qrst
END

my $T5 = { a        => 'cgatcaaacaaccgat',
	   b        => 'cgatcaaccgat',
	   score    => 16,
	   expected => <<END };
cgatcaaacaaccgat
    | | ||||||||
    cgatcaaccgat
END


my @Tests = ($T1, $T2, $T3, $T4, $T5);
	
my $Payoff = { match      =>  2,
	       mismatch   => -2,
	       gap_open   => -5,
	       gap_extend => -1 };
	

for my $test (@Tests)
{
    my $nw = new Align::NW $test->{a}, $test->{b}, $Payoff;
    $nw->score;
    $nw->align;
    my $score  = $nw->get_score;
    my $actual = $nw->dump_align;
    print "Score $score\n";
    print $actual;
    $score  eq $test->{score   } or Not; OK;
    $actual eq $test->{expected} or Not; OK;
}


align_files('t/a.seq', 't/b.seq', $Payoff);
OK;


sub align_files
{
    my($fileA, $fileB, $payoff) = @_;

    my $a = slurp($fileA);
    my $b = slurp($fileB);

    $a = substr($a, 0, 200);
    $b = substr($b, 0, 200);

    my $start = time;

    my $nw = new Align::NW $a, $b, $Payoff;
    $nw->score;
    $nw->align;

    my $end = time;
    print $end-$start, " seconds\n";
    $nw->print_align;
}


sub slurp
{
    my $file = shift;
    open(FILE, $file) or die "Can't open $file: $!\n";
    local $/;
    undef $/;
    <FILE>
}

