#! /usr/bin/perl 

if ($#ARGV != 0) {
    print << "EOF";
Usage: $0 headerfile < string-file > msf-file

    Converts a string type alignment file to a msf-file using a msf-file as
	header file for the output file

(c) Goetz Schwandtner 2001
EOF

    exit -1;
}

# get header and sequence names from header file 

open( HEADER, "<$ARGV[0]");

$numofseq=0;

# eat up lines until double-dash line found. then alignment starts
while (($line=<HEADER>) && ($line !~ /\/\//) ) {
    if ($line =~ /Name:\s*(\w*)/) {
	$names[$numofseq] = $1;
	$numofseq++;
    }
    print $line;  
}

print "//\n";

# get sequences from inputfile
for ($i=0; $line=<STDIN>; $i++) {
    chop($line);
    $align[$i] = $line;
}

die "Wrong sequence count: Header: $numofseq, Alignment: $i"
    if ($i != $numofseq);

# write down sequences. since all are same length use first for stop
while ($align[0] ne "") {
    print "\n\n";
    for ($i=0; $i < $numofseq; $i++) {
	$align[$i] =~ s/^(.{1,60})//;
	print $names[$i]."           ".$1."\n";
    } 
}
