#!/usr/bin/perl -w
use strict;

my $OPM;
my $TMHMM;
my @TM_start;#по результатам TMHMM
my @TM_end;
my @TM1_start;#по данным OPM
my @TM1_end;
my %table;


sub file_check () #запрашивает имена файлов, проверяет их существование
  {
  print "Enter the name of the file with data from OPM\n";
  $OPM=<STDIN>;
  chomp ($OPM);
  while ()
    {
    if (-e $OPM) {last;}  
    else
      {
      print "The file does not exist. Enter another filename.\n";
      $OPM=<STDIN>;
      chomp ($OPM);
      next;
      }
    }
   print "Enter the name of the file with TMHMM results.\n";
   $TMHMM=<STDIN>;
   chomp ($TMHMM);
   while ()
    {
    if (-e $TMHMM) {last;}  
    else
      {
      print "The file does not exist. Enter another filename.\n";
      $TMHMM=<STDIN>;
      chomp ($TMHMM);
      next;
      }
    }
  }
  




sub read_TMHMM ()  
  {
  open FILE, "<$TMHMM"; 
  my $i=0;
  $table{TM_predicted}=0;
  while (my $line=<FILE>)
    {
    if ($line=~m/.+Length:\s(\d+)\s*/)
      {
      $table{AA_total}=$1;
      last;
      }
    }
  while (my $line=<FILE>)
    {
    if ($line=~m/.+TMhelix\s*(\d+)\s*(\d+)\s*/)
      {
      push (@TM_start, $1);
      push (@TM_end, $2);
      $table{TM_predicted}+=$2-$1+1;
      }
    }      
  close FILE;
  } 
  
sub read_OPM () 
  {
  open FILE, "<$OPM"; 
  while (my $line=<FILE>)
    {
    #print $line;
    if ($line=~m/.*Segments:(.+)\s+/)
      {
      #print "$line \n";
      @TM1_start=split (',',$1);
     # foreach my $i (@TM1_start) {print "start=$i\n";}
      #print "o=$TM1_start[0]\n";
      for(my $i=0; $i<@TM1_start; $i++)
        {
        $TM1_start[$i]=~s/.*\(\s*(\d+)\D+(\d+)\).*/$1/;
        $TM1_end[$i]=$2;
        }
        last;
      }
    }
   close FILE;
   } 
sub count ()
    {
    my $i=0;
    my $j=0;
    my $FN=0;
    my $FP=0;
    my $TP=0;
    
    while (($i<@TM1_start) && ($j<@TM_start))
      {
          #    $table{FN}+=$FN;
        #print "fn=$table{FN}\n";
       # $table{FP}+=$FP;
        #$table{TP}+=$TP;
        #print "FP=$table{FP}\n";
       # print "TP=$table{TP}\n";
      if ($TM1_start[$i] < $TM_start[$j])
        {
        if ($TM1_end[$i] < $TM_start[$j])
          {
          $FN=$TM1_end[$i]-$TM1_start[$i]+1;
          $i++;
          $table{FN}+=$FN;
          next;
          }
        else
          {
          $FN=$TM_start[$j]-$TM1_start[$i]+1;
          $TM1_start[$i]=$TM_start[$j];
          $table{FN}+=$FN;
          }  
        
        }
      else 
        {
        if ($TM1_start[$i] <= $TM_end[$j])
          {
          $FP=$TM1_start[$i] - $TM_start[$j];
          if ($TM1_end[$i] <= $TM_end[$j])
            {
            $FP+=$TM_end[$j]-$TM1_end[$i];
            $TP=$TM1_end[$i]-$TM1_start[$i]+1;
            $TM_start[$j]=$TM1_end[$i]+1;
            if ($TM1_end[$i] == $TM_end[$j]) {$j++;}
            $i++;
            $table{FP}+=$FP;
            $table{TP}+=$TP;
            next;
            }
          else
            {
            $TP=$TM_end[$j] - $TM1_start[$i]+1;
            $TM1_start[$i]=$TM_end[$j]+1;
            $j++;
            $table{TP}+=$TP;
            next;
            }  
          
          }
        else
          {
          $FP=$TM_end[$j]-$TM_start[$j]+1;
          $table{FP}+=$FP;
          $j++;
          next;
          }
        }

      }
      $table{TN}=$table{AA_total}-$table{TP}-$table{FP}-$table{FN};
      $table{Sensivity}=$table{TP}/($table{TP}+$table{FN});
      $table{Specificity}=$table{TN}/($table{FP}+$table{TN});
      $table{Precision}=$table{TP}/($table{TP}+$table{FP});
      $table{Overprediction}=$table{FP}/($table{TP}+$table{FP});
      $table{Underprediction}=$table{FN}/($table{TN}+$table{FN});
    }
  
  
file_check ();
read_TMHMM (); 
#foreach my $i (@TM_start) {print "start=$i\n";}
#foreach my $i (@TM_end) {print "end=$i\n";}
read_OPM (); 
#foreach my $i (@TM1_start) {print "start=$i\n";}
#foreach my $i (@TM1_end) {print "end=$i\n";}
count ();
foreach my $i (keys %table){print "$i = $table{$i}\n";}
my $v=<STDIN>;  #пауза