#!/usr/bin/perl -w

use strict;

# read matrix

my $i      = 0;
my @pssm   = ();
my $infile = 'matrix3.txt';
open MATRIX, $infile or die "Could not open matrix file $infile\n";
while (<MATRIX>) {
    chomp;
    my @array = split;
    for ( my $j = 0 ; $j < 15 ; $j++ ) { $pssm[$i][$j] = $array[$j]; }
    $i++;
}
close MATRIX;

# read sequence to be analyzed

$infile = 'amyloid.fa';
open IN, $infile or die "Could not open $infile\n";
my $seq = '';
while (<IN>) {

    unless (/>/) {
        chomp;
        $seq .= $_;
    }
}
close IN;

print "pos\tscore\n";    # print header 

$seq = uc($seq);
my @bases = ( 'A', 'T', 'C', 'G' );

# score with the matrix

for ( my $k = 0 ; $k < length($seq) - 15 ; $k++ ) {
    my $test = substr( $seq, $k, 15 );
    my $score = 0;
    for ( my $j = 0 ; $j < 15 ; $j++ ) {
        my $base = substr( $test, $j, 1 );
        for ( my $b = 0 ; $b < 4 ; $b++ ) {
            if ( $bases[$b] eq $base ) {
                $score += $pssm[$b][$j];
            }
        }
    }
    $score = 2**$score;    # convert log2 to real values

    my $pos = $k + 12;     # We want to print a position
                           # next to the exon-intron
                           # junction
    print "$pos\t$score\n";

}

