#!/usr/bin/perl

$fname   = $ARGV[0];
$ofname  = $ARGV[1];
$resfile = $ARGV[2];

# check syntax from commandline
if( @ARGV  ne 3 )
  {
    print "Wrong number of arguments!\n";
    print "Syntax: ./orf2exp.pl small.ffn expert_infile.txt results_XXX.txt\n";
    exit( 1 );
  }

open( MYFILE, $fname )       or die "Cannot open  $fname : $!";
open( OUTFILE, ">>$ofname" ) or die "Cannot write to file: $!";
system( "echo > $resfile" );

$counter = 0;
@list_of_names;
$prev_name = "";

while ( <MYFILE> )
{
  $a = $_;

  if( $a =~ /\>/ )
    {
      $a =~ s/\[//;
      $a =~ s/\]//;
      $a =~ s/\>//;
      @parts = split( " " , $a);

      $name = $parts[0];
      $name =~ s/\_/\_uORF/;
      $name =~ s/\-/x/;

      @p = split( "_",$name );
      $gname = $p[0];

      if( $prev_name ne $gname &&  $prev_name ne "" )
	{
	  print OUTFILE "$prev_name instance of utr with\n";
	  print OUTFILE "number_of_UORFs: $counter\n";
	  print OUTFILE "intergenic_sequence_length: $nt_length\n";
	  print OUTFILE "uORFs: [$list_of_names[0]";
	  for( $i=1; $i < @list_of_names; ++$i )
	    {
	      print OUTFILE  ", $list_of_names[$i]";
	    }
	  print OUTFILE "];\n\n";
	  for( $i=0; $i < @list_of_names; ++$i )
	    {
	      print OUTFILE "add frame $list_of_names[$i];\n";
	    }
	  print OUTFILE "add frame $prev_name;\n\n";
	  @list_of_names = "";
	  $counter = 0;
	  addRules();

	  system( "bash commands.bash 2> /dev/null | grep functionality  >> $resfile " );
	  close( OUTFILE );
          open( OUTFILE, ">$ofname" ) or die "Cannot open  $ofname: $!";
	  addHeader();
	  close( OUTFILE );
          open( OUTFILE, ">>$ofname" ) or die "Cannot open  $ofname: $!";
	}

      $list_of_names[ $counter++ ] = $name;

      $nt_length = $parts[8];
      $dist = $parts[8]-$parts[1]+1;
      $length = ($parts[3]-$parts[1]+4)/3;

      print OUTFILE "$name instance of uORF with\n";
      print OUTFILE "distance_from_start_codon: $dist\n";
      print OUTFILE "length_in_codons: $length;\n\n";
#      for ( $i = 0; $i < @parts; ++$i )
#	{
#	  print OUTFILE  "Substring $i: $parts[$i]\n";
#	}

      $prev_name = $gname;
    }
}


print OUTFILE "$prev_name instance of utr with\n";
print OUTFILE "number_of_UORFs: $counter\n";
print OUTFILE "intergenic_sequence_length: $nt_length\n";
print OUTFILE "uORFs: [$list_of_names[0]";
for( $i=1; $i < @list_of_names; ++$i )
  {
    print OUTFILE  ", $list_of_names[$i]";
  }
print OUTFILE "];\n\n";
for( $i=0; $i < @list_of_names; ++$i )
  {
    print OUTFILE "add frame $list_of_names[$i];\n";
  }
print OUTFILE "add frame $prev_name;\n\n";
@list_of_names = "";
$counter = 0;
addRules();




sub addHeader()
  {
    print OUTFILE "%-------\n";
    print OUTFILE "% Frames\n";
    print OUTFILE "%-------\n\n";
  }

sub addRules()
  {
    open( MYFILE2, "rules.txt" ) or die "Cannot open  $rules.txt : $!";
    while( <MYFILE2> )
      {
	print OUTFILE "$_";
      }
    print OUTFILE "%----------\n";
    print OUTFILE "% End rules\n";
    print OUTFILE "%----------\n\n";
  }
