#! /usr/bin/perl

if ($#ARGV != -1) {
    print << 'EOF';
Usage: msf2string < infile > outfile
 
Converts a MSF-format style alignment file to a plain string file

(c) by Goetz Schwandtner <goetz@informatik.uni-mainz.de> 09/2001
EOF

exit -1;
}

$numofseq=0;

# eat up lines until double-dash line found. then alignment starts
while (($line=<>) && ($line !~ /\/\//) ) {
    # count lines containing description of a sequence
    $numofseq++ if ($line =~ /Name:/);
}

#now that we know how many lines we need, read the alignment
# $i gives the number of the next sequence to be read
$i=0;

while ($line=<>) {
    chop($line); # avoid difficulties ...
    # does line start with a sequence name?
    # i.e. \w - word char or special chars (-, _)
    if ($line =~ /^([\w-_]+)\s*(.*)$/) {
	$s = $2;
	# remove internal whitespaces in alignment if neccessary
	$s =~ s/\s//g;
	$align[$i] .= $s;
	$i++;
	$i=0 if ($i >= $numofseq);
    }
}

# now everything is collected - print alignment

for ($i=0; $i<$numofseq; $i++) {
    print $align[$i]."\n";
}
