#!/usr/bin/perl

$args=@ARGV;

if ($args != 2){
  print "##########################################################################################\n";
  print "#                               extract_miRNAs_names.pl                                  #\n";
  print "##########################################################################################\n";  
  print "# This script finds the names of the microRNAs in the pvalues_sorted file                #\n";
  print "#                                                                                        #\n"; 
  print "# execution: >extract_miRNAs_names.pl [pvalues_sorted file] [miRNA file]               #\n";
  print "#                                                                                        #\n";
  print "# Output: the output is in the standard output                                           #\n";
  print "#                                                                                        #\n";
  print "##########################################################################################\n";
  print "\n";
}else{

  @mirs=readFileToArray("$ARGV[1]", ">");
  shift(@mirs);

  $i=0;
  for $mir (@mirs){
    $i++;
    @gene=split("\n", $mir);
    @title=split(" ", $gene[0]);
    $mir_name=$title[0];
    $MIRNA{$i} = $mir_name;
  }
  open(PSORTED, "$ARGV[0]");
  while ($prediction = <PSORTED>) {
    @Line=split(" ", $prediction);
    if ($Line[3] > 0){
      $line=join("\t", @Line);
      print "$line\t$MIRNA{$Line[2]}\n";
    }
  }
  close(PSORTED);
}


sub readFileToArray {
  my ($file_name, $register_separator) = @_;
  open(FILE, "$file_name");
  local $/;
  $file1=<FILE>;
  close(FILE);
  @File1 = split("$register_separator", $file1);
  return @File1;
}
