#!/usr/bin/perl

$args=@ARGV;
# $BIN_PACMIT="/home/ray/Documents/Research/MicroRNA/Code/Pacmit_public_v2";
# 25 June 2011
$BIN_PACMIT="/projects/remc_bigdata/miR/PACMIT/code/20110622/Pacmit_public_v2";

if ($args ne 6){
  print "##########################################################################################\n";
  print "#                                        pacmit.pl                                       #\n";
  print "##########################################################################################\n";
  print "# Use: Find microRNA targets among 3'UTRs.                                               #\n";
  print "#                                                                                        #\n";
  print "# execution: >pacmit.pl [fasta 3'UTR file] [fasta miRNA file] [seed start] [seed end]    #\n";
  print "#                       [accessibility=0/1] [P_cutoff]                                   #\n";
  print "#                                                                                        #\n";
  print "# Output: the output is in folder ./[miRNA file]/. The results directory has the same    #\n";
  print "#         name as the miRNA file.                                                        #\n";
  print "#                                                                                        #\n";
  print "# Input:                                                                                 #\n";
  print "# Argument 1 => 3'UTR file: e.g. example_utr.fa.                                         #\n";
  print "#               Maximum UTR length allowed: 20,000 nucleotides                           #\n";
  print "# Argument 2 => miRNA file: must end in .fa, e.g. example_mirna.fa.                      #\n";
  print "#               Maximum number of miRNAs allowed: 10,000                                 #\n";
  print "# Argument 3 => seed start: first position of the miRNA seed in 5' to 3' sense, e.g. 2.  #\n";
  print "# Argument 4 => seed end: last position of the miRNA seed in 5' to 3' sense, e.g. 8.     #\n";
  print "#               The scan for perfect seed matches will only consider the sequence        #\n";
  print "#               between positions 'seed start' and 'seed end'                            #\n";
  print "# Argument 5 => Depending of which properties of the UTR you want to consider:           #\n";
  print "#               0 => only the sequence, no accessibility (PACMIT-0.0)                    #\n";
  print "#               1 => sequence and accessibility (PACMIT-[P_cutoff])                      #\n";
  print "# Argument 6 => P_cutoff: Probability of 4-mers to be unpaired                           #\n";
  print "##########################################################################################\n";
  print "\n";
}else{
  $time=`date`;
  $utrs=$ARGV[0];
  $mirs=$ARGV[1];
  $seed_start=$ARGV[2];
  $seed_end=$ARGV[3];
  $accessibility=$ARGV[4];
  $p_cutoff=$ARGV[5];

  print "\npacmit.pl started at $time\n";
  print "3'UTR fasta file: $utrs\n";
  print "miRNA fasta file: $mirs\n";
  print "miRNA seed: $seed_start-$seed_end\n";
  print "Accessibility: $accessibility\n";
  print "P_cutoff: $p_cutoff\n";

  # Setting up variables
  if($accessibility eq 0){
    $properties = 1;
  }else{
    $properties = 3;
  }
  $file_cap = join("_", $seed_start, $seed_end, $accessibility, $p_cutoff);
  @mirna_route=split("/", $mirs);
  $output_dir=$mirna_route[$#mirna_route];
  $output_dir =~ s/.fa//;
  system("mkdir -p $output_dir");

  $seeds=`$BIN_PACMIT/bind_hex $mirs $seed_start $seed_end`;
  @Seeds=split(" ", $seeds);
  $x = $#Seeds + 1;
  $y = $x + 3;
  $z = (2*$x) + 3;

  # Single hypothesis analysis.
  system("$BIN_PACMIT/oligo_mm $utrs 2 $properties 0 1 1 4 $p_cutoff 0 0 $seeds > $output_dir/pvalues_$file_cap");
  system("gawk -f $BIN_PACMIT/single_pair.awk < $output_dir/pvalues_$file_cap | sort -n -k 6 > $output_dir/pvalues_sorted_$file_cap");
  system("echo \"#Sequence ID\tt_access\tNo. miRNA\tc_access\tExpected c_access\tlog(P_SH)\tmiRNA name\" > $output_dir/predictions_$file_cap");
  system("$BIN_PACMIT/extract_miRNAs_names.pl $output_dir/pvalues_sorted_$file_cap $mirs >> $output_dir/predictions_$file_cap");
  system("rm $output_dir/pvalues_sorted_$file_cap $output_dir/pvalues_$file_cap");

  $time=`date`;
  print "\npacmit.pl finished at $time\n";
  print "Output file written to: ./$output_dir/predictions_$file_cap\n\n";
}
