#!/usr/bin/perl -w
use strict;
while(){
	print "Enter OPM file name\n";
	my $opm = <STDIN>;
	chomp $opm;
	if(-e $opm){
		open OPM, "<$opm";
		last;
	}else{
		print "File doesn`t exists\n";
	}
}
while(){
	print "Enter TMHMM file name\n";
	my $tmh = <STDIN>;
	chomp $tmh;
	if(-e $tmh){
		open TMH, "<$tmh";
		last;
	}else{
		print "File doesn`t exists\n";
	}
}
my @aOPM;
my $len;
while(my $lOPM=<OPM>){
	chomp $lOPM;
	if($lOPM=~m/(\d+)-\s?(\d+)/){
		push @aOPM,$1,$2;
	}
}
my @aTMH;
while(my $lTMH=<TMH>){
	chomp $lTMH;
	if($lTMH=~m/TMhelix\s+(\d+)\s+(\d+)/){
		push @aTMH,$1,$2;
	}elsif($lTMH=~m/Length:\s(\d+)/){
		$len=$1;
	}
}
my @OPMout = toArray ($len,@aOPM);
my @TMHout = toArray ($len,@aTMH);
my $tp;
my $tn;
my $fp;
my $fn;
for(my $i=1;$i<=$len;$i++){
	if($OPMout[$i]==1 && $TMHout[$i]==1){
		$tp++;
	}elsif($OPMout[$i]==0 && $TMHout[$i]==1){
		$fp++;
	}elsif($OPMout[$i]==0 && $TMHout[$i]==0){
		$tn++;
	}else{
		$fn++;
	}
}
my $tm=$tp+$fp;
my $sn=$tp/($tp+$fn);
my $sp=$tn/($tn+$fp);
my $pr=$tp/($tp+$fp);
my $oPre=$fp/($fp+$tp);
my $nPre=$fn/($tn+$fn);
print "length: $len\n";
print "ÒÌ: $tm\n";
print "tp: $tp\n";
print "fp: $fp\n";
print "tn: $tn\n";
print "fn: $fn\n";
print "sensivity: $sn\n";
print "specificity: $sp\n";
print "precision: $pr\n";
print "oPre: $oPre\n";
print "nPre: $nPre\n";
sub toArray{
my @ZO;
my $len = shift;
my $ch=1;
my $num = shift;
for(my $i=1;$i<=$len;$i++){
	if($num==$i){
		$ch=-$ch;
		if(!($num = shift)){
			$num=0;
		}
	}
	if($ch==1){
		$ZO[$i]=0;
	}elsif($ch==-1){
		$ZO[$i]=1;
	}
}
return @ZO;
}