#!/usr/bin/perl -w
# split RNAfold output into separate .mfe files. E.g:
# split.pl RNAfold.out   or RNAfold < sequences | split.pl

use strict;

sub usage {
   die "Usage: $0 RNAfold.out\n";
}

usage() if eof();

my $max_name=30;  # current clustalw cuts names at 30 characters
while (<>) {
  next unless (/^>/); #skip junk (e.g. from part_func folding)
  chomp;
  my ($blah, $name) = split;
  my $seq=<>;
  $_ = <>;
  my $ext = (/\d+$/) ? '.lfold' : '.mfe';
  
  my $fname = substr($name,0,$max_name) . $ext;
  open(OUT, ">$fname");
  print OUT "> $name\n", $seq, $_;
  # handle lfold output
  if (/\d+$/) {
      while (<>) {
	  print OUT $_;
	  last unless /\d+$/;
      }
  }
  close(OUT);
}
