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

#####Считаем, сколько всего а.о.
#####Пишем в переменную $length

my $length;
print "Enter the TMHMM file name\n";
my $tmname=<STDIN>;
chomp($tmname);
open(TMHMM,"<$tmname") || die "Error\n";
my @tm=<TMHMM>;
close(TMHMM);
if ($tm[0]=~m/Length:\s(\d+)/){$length=$1;print "LENGTH ".$length."\n\n";}



#####Создаем массив с номерами остатков в спиралях ТМНММ
#####Массив @tmhelix

my @tmhelix=();
my $temp1;
for (my $i=1;$i<=$#tm;$i++)
{	
	if ($tm[$i]=~m/TMhelix\s+(\d+)\s+(\d+)/)
	{	
		push @tmhelix, $1;
		$temp1=$1;                 
		while ($temp1<$2)
		{
			$temp1++;
			push @tmhelix, $temp1;
		}  
	}
	else{next;}
}



#####Создаем массив с номерами остатков по данным ОРМ
print "Enter the OPM file name\n";
my $opmname=<STDIN>;
chomp($opmname);
open(OPM,"<$opmname") || die "Error\n";
my $opm=<OPM>;
close(OPM);      
$opm=substr($opm,10);                         
my @opm = split ', ', $opm, 10;
my @opmhelix=();
my $temp2;
for (my $j=0;$j<=$#opm;$j++)
{	
	if ($opm[$j]=~m/\((\d+)\-(\d+)\)/)
	{	                   
		push @opmhelix, $1;
		$temp2=$1;                 
		while ($temp2<$2)
		{
			$temp2++;
			push @opmhelix, $temp2;
		}  
	}
	else{next;}                           
}



#####Считаем все, что нужно
my $tp=0;
my $fp;
my $tn;
my $fn;
my $sens;
my $spec;
my $prec;
my $over;
my $under;

for (my $n=0; $n<=$#tmhelix; $n++)
{
	for (my $m=0; $m<=$#opmhelix; $m++)
	{
		if ($tmhelix[$n]==$opmhelix[$m]){$tp++;last;}
		else{next;}
	}
}

$fp=$#tmhelix-$tp;
$fn=$#opmhelix-$tp;
$tn=$length-$tp-$fp-$fn;
$sens=$tp/($tp+$fn);
$spec=$tn/($tn+$fp);
$prec=$tp/($tp+$fp);
$over=$fp/($fp+$tp);
$under=$fn/($tn+$fn);

                                                         
open(RESULT,">count_results.txt") || die "Error creating output file\n";                
print RESULT 'The estimation of EMHMM membrane protein topology prediction.'."\n\n".'The OPM data: '.$opmname."\n".'The TMHMM prediction results: '.$tmname."\n\n".'Amino residues: '.$length."\n".'Predicted membrane helix residues: '.$#tmhelix."\n".'TP: '.$tp."\n".'FP: '.$fp."\n".'FN: '.$fn."\n".'TN: '.$tn."\n".'Sensitivity: '.$sens."\n".'Specificity: '.$spec."\n".'Precision: '.$prec."\n".'Overprediction: '.$over."\n".'Underprediction: '.$under."\n";
close(RESULT);

print "\nThe output file count_results.txt was created.\n";













close(RESULT);
