#!/usr/bin/perl-w
use strict;
my $file;
my $string;
my $num_aa;
my $tm;
my $name;
my @TH;
my @Opm;
my %TM;
my %opmTM;
my $pdb = 0;
my $tm_num=0;
my $i=0;
my $j=0;
my $length = 0;
$file = "hmhmm.txt";
open FILE, "<$file" or die ("can't open file!\n");
#get the protein ID
#------------------
while($string=<FILE>) {
  if( $string =~m/^#\s(\w+_\w+)\sLength.\s(\d+)/ )  {
    $name = $1;
    $length = $2;
    last;
  }
}
#  get TM 
#---------
while($string=<FILE>) {
  if( $string =~m/^$name\s+\w+.\w+\s+(\w+)\s+(\d+)\s+(\d+)/ )  {
        if( $1 eq "TMhelix" )  {
             
            $tm_num += $3-$2+1;
            $TH[$i][$j]=$2;
            #print"$IN[$i][$j]!!\n";
            $j++;
            $TH[$i][$j]=$3;
            #print"$IN[$i][$j]!\n";
            $i++;
            $j=0;
            
        }
  }
}
close FILE or die ("can't close file!\n");
$file = "opm.txt";
open FILE, "<$file" or die ("can't open file!\n");
while($string =<FILE>) {
  if( $string =~m/^view\sof\simage\s.\w+.\w+.(\w+).\w+./ ) {
      $pdb = $1;
  }
  elsif($string =~m/\d+\stransmembrane\ssubunits/)  {
    $string=<FILE>;
    if($string =~m/\*\w\*/)  {
      $tm=$string;
      $string=<FILE>;
      $tm=$tm.$string;
      $string=<FILE>;
      if(!($string=~m/\*\w\*/))  {
        $tm=$tm.$string;
        $string=<FILE>;
        next;
      }
    }
  }
}
#print"$tm"; 
#print("pdb = $pdb\n");
close FILE or die ("can't close file!\n");
$i=0;
$j=0;
open FILE, ">result.txt" or die("can't open result.txt\n");

if($tm=~m/\sSegments:\s..\s(\d+)-\s(\d+)..\s..\s(\d+)-\s(\d+)..\s..\s(\d+)-\s(\d+)..\s..\s(\d+)-\s(\d+)./)  {
  #print("$1, $2, $3, $4, $5, $6, $7, $8\n");
  $Opm[$i][$j]=$1; $j++;
  $Opm[$i][$j]=$2; $j=0; $i++; 
  $Opm[$i][$j]=$3; $j++;
  $Opm[$i][$j]=$4; $j=0; $i++;
  $Opm[$i][$j]=$5; $j++;
  $Opm[$i][$j]=$6; $j=0; $i++;
  $Opm[$i][$j]=$7; $j++;
  $Opm[$i][$j]=$8; 
    
}
 
my $TP = 0;
my $TN = 0;
my $FN = 0;
my $FP = 0;
print"amount aa $length\n";
print"tm aa $tm_num\n";
for($i=0; $i < 4; $i++ ){
	$j=0;
 	if( ($TH[$i][$j] > $Opm[$i][$j]) && ($TH[$i][$j+1] < $Opm[$i][$j+1]) ) {
		$TP+=($TH[$i][$j+1] - $TH[$i][$j] + 1);
		$FN+=($Opm[$i][$j+1] - $TH[$i][$j+1] + $TH[$i][$j] - $Opm[$i][$j]);
		#$TN+=($length-$Opm[$i][$j+1] + $Opm[$i][$j]);
	}
        elsif( ($TH[$i][$j] < $Opm[$i][$j]) && ($TH[$i][$j+1] < $Opm[$i][$j+1]) ) {
        	$TP+=($TH[$i][$j+1] - $Opm[$i][$j] + 1);
		$FN+=($Opm[$i][$j+1] - $TH[$i][$j+1]);
		$FP+=($Opm[$i][$j] - $TH[$i][$j]);
		#$TN+=($length-$Opm[$i][$j+1] + $TH[$i][$j]);
	}
	elsif( ($TH[$i][$j] < $Opm[$i][$j]) && ($TH[$i][$j+1] > $Opm[$i][$j+1]) ) {
        	$TP+=($Opm[$i][$j+1] - $Opm[$i][$j] + 1);
		$FP+=($TH[$i][$j+1] - $Opm[$i][$j+1] + $Opm[$i][$j] - $TH[$i][$j]);
		#$TN+=($length-$TH[$i][$j+1] + $TH[$i][$j]);
	}
       elsif( ($TH[$i][$j] > $Opm[$i][$j]) && ($TH[$i][$j+1] > $Opm[$i][$j+1]) ) {
       	$TP+=($Opm[$i][$j+1] - $TH[$i][$j] + 1);
		$FP+=($TH[$i][$j+1] - $Opm[$i][$j+1]);
		$FN+=($TH[$i][$j] - $Opm[$i][$j] );
		#$TN+=($length-$TH[$i][$j+1] + $Opm[$i][$j]);
	} 
}
$TN = $length-$TP-$FP-$FN;
	
my $sens = $TP/($TP+$FN)*100;
my $spec = $TN/($TN+$FP)*100;
my $prec = $TP/($TP+$FP)*100;
my $over = $FP/($TP+$FP)*100;
my $under = $FN/($TN+$FN)*100;
print("TP $TP\nFP $FP\nTN $TN\nFN $FN\nsensivity $sens\nspecificity $spec\nprecision $prec\n
overprediction $over\nunderprediction $under\n");
close FILE or die("can't close result.txt\n");









