#!/usr/bin/perl -w


my $fname     = $ARGV[0];
my $smallfile = $ARGV[1];
my $minlength = $ARGV[2];
my $maxlength = $ARGV[3];


# check syntax from commandline
if( @ARGV  ne 4 )
  {
    print "Wrong number of arguments!\n";
    print "Syntax: ./splitOutputFromGetorf.pl out.ffn results.ffn 300 350 \n";
    exit( 1 );
  }


open( MYFILE, $fname )            or die "Cannot open: $!";
open( SMLFILE, ">$smallfile" )    or die "Cannot write to: $!";


my $genename =  <MYFILE>;
my $sequence = "";
while ( <MYFILE> )
{
  my $line = $_;
  if( $line =~ /\>/ )
    {
      my @parts = split " ", $genename;
      my $seqlength = $parts[8];
      if($seqlength >= $minlength && $maxlength >= $seqlength )
	{
	  print SMLFILE $genename;
	  print SMLFILE $sequence;
	}
      $sequence = "";
      $genename = $line;
      next;
    }
  $sequence = $sequence.$line;
}
