#!/usr/bin/perl -w

use strict;

my $j;
my $llarg;
my $nom;
my %hash;
my $p = 0;
my $pes1=0;
my $total=0;
my $log1;my $log2;
my $linies;
my $a1=0;my $c1=0;my $t1=0;my $g1=0;
my @vseq;
my $x=0;
my $index=0;
my $y=0;
my $max=-10000;
my $suma=0;
my $l=0;
my $i = 0;
my $pos=0;
my $car;
my $pvalue;
my $suma1=0;
my $max1=-10000;
my $com=0;
my @vran;
my $max2=-10000;

open (FH1, "< $ARGV[1]");
$x=<FH1>;
chomp ($x);

@vseq = split (//,$x);

while ($index<scalar(@vseq)){
    if ($vseq[$index] eq "A" || $vseq[$index] eq "a"){
	$a1=$a1+1;}
    if ($vseq[$index] eq "C" || $vseq[$index] eq "c"){
	$c1=$c1+1;}
    if ($vseq[$index] eq "T" || $vseq[$index] eq"t"){
	$t1=$t1+1;}
    if ($vseq[$index] eq "G" || $vseq[$index] eq "g"){
	$g1=$g1+1;}
    $index=$index+1;
}
open (FH, "< $ARGV[0]");

$linies=<FH>;
$linies=<FH>;
while (<FH>) {
    $max=-10000;
    if (m/(FA)/){
	$nom = $_;
    }
    if (m/(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+/){
	$hash {"A"}[$p] = $2;
	$hash {"C"}[$p] = $3;
	$hash {"G"}[$p] = $4;
	$hash {"T"}[$p] = $5;
	$p = $p + 1;
    }
    my @k = keys(%hash);
    if (m/(\/\/)\n/){
	$llarg=$p;
	$p = 0;
	$total= $hash{"A"}[0]+ $hash {"C"}[0]+ $hash{"G"}[0]+ $hash{"T"}[0];
	$suma=0;
	$i=0;
	while ($i < scalar(@k)) {
	    $j = 0;

	    while ($j < $llarg) {
		if ($hash{$k[$i]}[$j] == 0){
		    $pes1=-999;
		    $hash{$k[$i]}[$j]=$pes1;
		}
		else{
		    $log1=log($hash{$k[$i]}[$j]/$total)/log(10);

		    if ($k[$i] eq "T") {
			$log2=log($t1/scalar(@vseq))/log(10);
			$pes1=$log1-$log2;
			$hash{$k[$i]}[$j]=$pes1;
		    }
		    if ($k[$i] eq "C") {
			$log2=log($c1/scalar(@vseq))/log(10);
			$pes1=$log1-$log2;
			$hash{$k[$i]}[$j]=$pes1;
		    }
		    if ($k[$i] eq "A") {
			$log2=log($a1/scalar(@vseq))/log(10);
			$pes1=$log1-$log2;
			$hash{$k[$i]}[$j]=$pes1;
		    }
		    if ($k[$i] eq "G") {
			$log2=log($g1/scalar(@vseq))/log(10);
			$pes1=$log1-$log2;
			$hash{$k[$i]}[$j]=$pes1;
		    }
		}
		$j = $j + 1;
	    }
	    $i = $i + 1;
	}
	$l=0;
	$y=0;
	while ($l < (scalar(@vseq)-$llarg+1)){
	    
	    while ($y<$llarg){
		$car=$vseq[$l+$y];
		if ($car eq "A" || $car eq "a"){
		    $suma=$suma+$hash{"A"}[$y];
		}
		if ($car eq "C" || $car eq "c"){
		   $suma=$suma+$hash{"C"}[$y];
	       }
		if ($car eq "T" || $car eq "t"){
		    $suma=$suma+$hash{"T"}[$y];
		}
		if ($car eq "G" || $car eq "g"){
		    $suma=$suma+$hash{"G"}[$y];
		}
		$y=$y+1;
	       
	    }
	    
	    if ($max<$suma){
		$max=$suma;
		$pos=$l;
	    }
	    $suma=0;
	    $y=0;
	    $l=$l+1;
	}
	
	
	print"\n";
	print"\t";
	print"Pel factor de transcripcio ",$nom,"";
	print"\t";
	print"L score maxim obtingut es de ",$max,"";
	print"\n";
   	print" a la posicio ",$pos,"";
	print" i la sequencia corresponent es ";
	$y=0;
	while($y<$llarg){
	    print"",$vseq[$pos+$y],"";
	    $y=$y+1;
	}
	print"\n";
	
	
	my $c=0;
	
	while ($com<100){
	    
	    $max1=-10000;
	    @vran=@vseq;
	    my $n = scalar(@vran);
	    my $ii = $n - 1;
	    while ($ii >= 0) {
		my $jj = int(rand($ii+1));
		if ($ii != $j) {
		    my $tmp = $vran[$ii];
		    $vran[$ii] = $vran[$jj];
		    $vran[$jj] = $tmp;
		}
		$ii = $ii - 1;
	    }
	    $l=0;
	    $y=0;  
	    while ($l < (scalar(@vran)-$llarg+1)){
		$suma1=0;
		$y=0;
		while ($y < $llarg){
		    $car=$vran[$l+$y];
		    if ($car eq "A" || $car eq "a"){
			$suma1=$suma1+$hash{"A"}[$y];
		    }
		    if ($car eq "C" || $car eq "c"){
			$suma1=$suma1+$hash{"C"}[$y];
		    }
		    if ($car eq "T" || $car eq "t"){
			$suma1=$suma1+$hash{"T"}[$y];
		    }
		    if ($car eq "G" || $car eq "g"){
			$suma1=$suma1+$hash{"G"}[$y];
		    }
		    $y=$y+1;
		}
		
		if ($max1<$suma1){
		    $max1=$suma1;
		}
		$suma1=0;
		$y=0;
		$l=$l+1;
	    }
	    if ($max1 >= $max) {
		$c=$c+1;
	    }
	    
	    
	    $com=$com+1;
	    
	}
	$pvalue=$c/100;
	print"\t";
	print"El pvalue es: ";
	print"$pvalue\n";
	$c=0;
	$com=0;
	
    }
}
close(FH1);
close(FH);
 
