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



my $count='0'; #the number of aa 
my $tmcount='0'; #The number of predicted transmembrane aa
my $tp='0';  #The number of true positives
my $fp='0';	 #The number of false positives
my $tn='0';	 #The number of true negatives
my $fn='0';  #The number of false negatives
my $coord;


print "Enter the name of the OPM file?\n";
my $fname1 = <STDIN>;
chomp $fname1;


#check if there is this file
while(!(-e $fname1))
{	
	print "File doesn't exist. Enter new name\n";
	$fname1=<STDIN>;
	chomp $fname1;
} 

open OPM, " $fname1";

my $tmopm='0'; #scalar of transmembrane parts
my @opms;  #array of transmembrane parts
 
while (my $line=<OPM>)
 {
	if ($line=~m/gria3_macfa: Segments:\s+/i)
	{
		$tmopm=$';
		chomp $tmopm;
	}

 }


 close OPM;

# for the program OPM
my $f_omp='0';   #the coord of first transmembrane aa
my $l_omp='0';   #the coord of last transmembrane aa 
my @f_omp;   #the coord of first transmembrane of all transmembrane part
my @l_omp;    #the coord of last transmembrane of all transmembrane part

@opms=split /,\s/, $tmopm;

foreach my $i (@opms)
{
	$f_omp=$i;
	$l_omp=$i;
	$f_omp=~s/\d+\(//g;
	$f_omp=~s/-\d+\)//g;
	$l_omp=~s/\d+\(\d+-//g;
	$l_omp=~s/\)//g;
	push @f_omp, $f_omp;
	push @l_omp, $l_omp;
}



print "Enter the name of the TMHMM file\n";
my $fname2 = <STDIN>;
chomp $fname2;


#check if there is this file
while(!(-e $fname2))
{	
	print "File doesn't exist. Enter new name\n";
	$fname2=<STDIN>;
	chomp $fname2;
}

open TMHMM, " $fname2";

#for the program tmhmm
my $f_tmhmm='0';  #the coord of first transmembrane aa 
my $l_tmhmm='0';  #the coord of last transmembrane aa 
my @f_tmhmm; #the coord of first transmembrane of all transmembrane part 
my @l_tmhmm;	#the coord of last transmembrane of all transmembrane part


while (my $line=<TMHMM>)
 {
	if ($line=~m/\slength:\s/i)
	{
		$count=$';
		chomp $count;
	}
  
	elsif ($line=~m/\s+tmhelix\s+/i)
	{
		$coord=$';
		chomp $coord;
		$f_tmhmm=$coord;
		$l_tmhmm=$coord;
		$f_tmhmm=~s/\s+\d+\s*//g;
		$l_tmhmm=~s/\d+\s+//g;
		$l_tmhmm=~s/\s+//g;
		push @f_tmhmm, $f_tmhmm;
		push @l_tmhmm, $l_tmhmm;
		$tmcount=$tmcount+$l_tmhmm-$f_tmhmm+1;
   }
 }           

close TMHMM;


my $tmopmcount='0';
for (my $t=0; $t<@f_omp; $t++)
{
	$tmopmcount=$tmopmcount+$l_omp[$t]-$f_omp[$t]+1;
}

#count aa that were true predicted by 2 programs
for (my $k=0; $k<@f_tmhmm; $k++)
{
	for (my $j=0; $j<@f_omp; $j++)
	{
	
		if ($f_tmhmm[$k]<$f_omp[$j] && $l_tmhmm[$k]>=$f_omp[$j])
    	{
			if ($l_tmhmm[$k]<=$l_omp[$j])
			{
				$tp=$tp+$l_tmhmm[$k]-$f_omp[$j]+1;
			}
			
			elsif ($l_tmhmm[$k]>$l_omp[$j])
			{
				$tp=$tp+$l_omp[$j]-$f_omp[$j]+1;
			}

		}
		
		elsif ($f_tmhmm[$k]>$f_omp[$j] && $f_tmhmm[$k]<=$l_omp[$j])
		{
			if ($l_tmhmm[$k]<=$l_omp[$j])
			{
				$tp=$tp+$l_tmhmm[$k]-$f_tmhmm[$k]+1;
			}
			
			elsif ($l_tmhmm[$k]>$l_omp[$j])
			{
				$tp=$tp+$l_omp[$j]-$f_tmhmm[$k]+1;
			}

		}

	}
}                

#determination of the characteristics    

$fn=$tmopmcount-$tp; 
$fp=$tmcount-$tp;
$tn=$count-$tmcount-$fn;


my $sens=sprintf("%.3f",$tp/($tp+$fn));
my $spec=sprintf("%.3f",$tn/($tn+$fp));
my $prec=sprintf("%.3f",$tp/($tp+$fp));
my $overpred=sprintf("%.3f",$fp/($fp+$tp));
my $notpred=sprintf("%.3f",$fn/($tn+$fn));


open FILE, ">result.txt";
print FILE "The length - $count\nThe number of predicted transmembrane aa - $tmcount\nTP - $tp\nFP - $fp\nTN - $tn\nFN - $fn\nSensivity - $sens\nSpecificity - $spec\nPrecision - $prec\nOverprediction - $overpred\nNot enough prediction - $notpred\n";
close FILE;

<>;