#!/usr/bin/perl -w

if ($#ARGV != 1) {
	print "usage: <parsed_file_name> <number_of_top_results_to_get>\n";
	exit (1);
}

print "FILE_NAME: $ARGV[0]\n\n";
open (FILE, $ARGV[0]) || die "Can't open file\n.";
$number_of_res = $ARGV[1];

$i = -1;
@run = ();
@cons1 = ();
@cons2 = ();
@maxUU = ();
@maxU  = ();
@value = ();
@best_iter = ();
$total = -1;
$j = 0;

while (<FILE>) {
   if ($_ =~ /START\sOF\sRUN/) {$i++;$run[$i]=$_; $total++;}
   if ($_ =~ /CONSENSUS/) {
      if ($j==0) {$cons1[$i] = $_; $j=1;next;}
      if ($j==1) {$cons2[$i] = $_; $j=0; next;}
   }
   if ($_ =~ /MaxUU/) {
          $maxUU[$i] = $_;
          @l = split(/:/,$_); 
          @m = split (/\s/,$l[1]);
          $value[$i] = $m[1]; 
   }
   if ($_ =~ /MaxU1/) {$maxU[$i] = $_;} 
   if (($_ =~ /Best/) && ($_ =~ /combined/)) {$best_iter[$i] = $_;}
} 

@sorted_values = sort {$a<=>$b} @value;

for ($x=$total; $x>$total-$number_of_res; $x--) {
  for ($y=$total; $y>=0; $y--) {
     if ($value[$y] == $sorted_values[$x]) {
	print "$run[$y]$cons1[$y]$cons2[$y]$best_iter[$y]$maxUU[$y]$maxU[$y]\n";}
  }
}
