#! /usr/bin/perl -w

# some parameters. Change if desired!
$MATCH= 0;
$MISMATCH= 1;
$GAP= 2;

if ($#ARGV != -1) {
    print << "EOF";
Usage: $0 < infile
 
Calculates SP-score of alignment in infile (which should be string-format)
Score used: match= $MATCH, mismatch= $MISMATCH, gap= $GAP

gaps in infile must be represented as '.'
EOF
    print << 'EOF';
(c) by Goetz Schwandtner <goetz@informatik.uni-mainz.de> 09/2001
EOF

exit -1;
}

# get sequences from inputfile
# at the end $nos holds number of sequences
for ($nos=0; $line=<STDIN>; $nos++) {
    chop($line);
    $align[$nos] = $line;
}

# now we have the lines, add up the score columnwise
# to do this we remove the first column in each step and add its score

$numgg= 0;
$numgap= 0;
$nummatch= 0;
$nummismatch= 0;

# all aligned sequences are of the same length, so check first line
# whether the whole alignment is already scored
while ($align[0] ne "") {
    for ($i=0; $i < $nos; $i++) {
	$align[$i] =~ s/^(.)//;
	$alcol[$i] = $1;
    }

    for ($i=0; $i < $nos; $i++) {
	for ($j=0; $j < $i; $j++) {
	    if ( ($alcol[$i] eq ".") && ($alcol[$j] eq ".") ) {
		$numgg++;
	    } elsif ( ($alcol[$i] eq ".") || ($alcol[$j] eq ".") ) {
		# only one is a gap, add gap penalty !
		$numgap++;
	    } elsif ( $alcol[$i] eq $alcol[$j] ) {
		# a match occurred
		$nummatch++;
	    } else {
		# this must be a mismatch
		$nummismatch++;
	    }
	}
    }
}

$score= $nummatch * $MATCH + $nummismatch * $MISMATCH + $numgap * $GAP;

print << "EOF";
SP-Score for $nos sequences: $score
Statistics:
gap-gap:     ( score 0 ) $numgg
gaps:        ( score $GAP ) $numgap
mismatches:  ( score $MISMATCH ) $nummismatch
matches:     ( score $MATCH ) $nummatch

EOF
