#!/usr/bin/perl -w

use strict;


my %hash = (  "A" => [0, 0, 0, 0, 0, 0, 0, 0, 0],
	      "C" => [0, 0, 0, 0, 0, 0, 0, 0, 0],
	      "G" => [0, 0, 0, 0, 0, 0, 0, 0, 0],
	      "T" => [0, 0, 0, 0, 0, 0, 0, 0, 0]);            

my $compta=0;
my $entrada;
my $posicio = 0;
my @total;
my $z = 0;
my $suma = 0;
my $w = 0;
my $lloc = 0;
my $seq;
my @seq;
my @seqsencera;

open (FH,"< $ARGV[1]");

while (<FH>){

    $seq = $_;
    chomp($seq);

    @seq = split(//,$seq);
    
    push(@seqsencera,@seq);

}

close (FH);

my $q = 0;
my $sumaA = 0;
my $sumaC = 0;
my $sumaT = 0;
my $sumaG = 0;
my @logaritmes;
my $nom;

my $longseq = scalar(@seqsencera);

while ($q < $longseq){

    if ($seqsencera[$q] eq 'A' || $seqsencera[$q] eq 'a'){
	
	$sumaA = $sumaA + 1;
    }
	
    elsif ($seqsencera[$q] eq 'C' || $seqsencera[$q] eq 'c'){

	$sumaC = $sumaC + 1;
    }

    elsif ($seqsencera[$q] eq 'G' || $seqsencera[$q] eq 'g'){

	$sumaG = $sumaG + 1;
    }

    elsif ($seqsencera[$q] eq 'T' || $seqsencera[$q] eq 't'){

	$sumaT = $sumaT + 1;
    }
   
    $q = $q + 1;

}
   
$sumaA = log($sumaA/$longseq)/log 10;
$sumaC = log($sumaC/$longseq)/log 10;
$sumaG = log($sumaG/$longseq)/log 10;
$sumaT = log($sumaT/$longseq)/log 10;

@logaritmes = ($sumaA,$sumaC,$sumaG,$sumaT);



open (MATRIUS,"< $ARGV[0]");

while (<MATRIUS>){

    chomp ($_);
    $entrada = $_;
	
	if ($entrada =~/(FA)\s(.+)/){
			$nom = $2;
			#print $nom,"\n";

		}
	
	elsif ($entrada =~/[0-9]+\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)$/){
	
		$hash{"A"}[$posicio] = $1; 
	    	$hash{"C"}[$posicio] = $2; 
		$hash{"G"}[$posicio] = $3; 
		$hash{"T"}[$posicio] = $4; 
	
	    $posicio = $posicio +1;
	    $compta=$compta+1;


	 @total = ($1,$2,$3,$4);
	    
	    while ($z < 4){
		
		$suma = $suma + $total[$z];
		
		$z = $z + 1;
	    }
	    
	    $z = 0;

	    while ($w < 4){

		if ($total[$w] == 0){

		    $total[$w] = -999;
		}
		
		else{
		   
		    $total[$w]=((log($total[$w]/$suma))/log 10) - ($logaritmes[$w]);
		    	       
		}
		$w = $w +1;
	    }

	    $w = 0;

		$hash{"A"}[$lloc] = $total[0]; 
	    	$hash{"C"}[$lloc] = $total[1];
		$hash{"G"}[$lloc] = $total[2];
		$hash{"T"}[$lloc] = $total[3];
		
	    $lloc = $lloc+1;
	 
	    $suma = 0;
	    
	}

	
	
	elsif (($entrada = ~/(\/\/)\Z/) && ($lloc > 1 )) {
	
	#	my @k = keys(%hash);
	#	my $i = 0;
	#	@k = sort(@k);
	#	while ($i < scalar(@k)) {
			
	#		my $b =0;
	#		print "$k[$i]";
			
	#		while ($b < $compta) {
				
	#			print "\t", $hash{$k[$i]}[$b];
				
	#			$b = $b +1;
	#		}

	#		print "\n";
	
	#		$i = $i + 1;
	#	}	

my $score = 0;
my $scoremax = -999;
my $posiciomillor = 0; 
my $pmotiu; 
my $f = 0; 
my $m = 0;

	while ($m < $longseq-($compta + 1)) {
	    
	 $f = 0;
	 $score = 0;
	 $pmotiu = $m;

	    while ($f < $compta) { 
		
		if ($seqsencera[$pmotiu] eq 'A' || $seqsencera[$pmotiu] eq 'a' ){

		    $score = $score + $hash{"A"}[$f];
		}

		if ($seqsencera[$pmotiu] eq 'C' || $seqsencera[$pmotiu] eq 'c' ){

		    $score = $score + $hash{"C"}[$f];
		} 

		if ($seqsencera[$pmotiu] eq 'G' || $seqsencera[$pmotiu] eq 'g' ){

		    $score = $score + $hash{"G"}[$f];
		}

		if ($seqsencera[$pmotiu] eq 'T' || $seqsencera[$pmotiu] eq 't' ){

		    $score = $score + $hash{"T"}[$f];
		}
		
		$pmotiu = $pmotiu + 1;
		$f = $f + 1; 
	      	
	    }	
	    

	    if ($score > $scoremax) {
		$scoremax = $score;
		$posiciomillor = $m;
	    }


	    $m = $m + 1;
	      
	}
  
	#if ($scoremax == -999){
	#print "$nom no s'uneix a la seqüència promotora\n";
#}
#	else {
#	    print "La $posiciomillor es la millor posicio i te un putuacio de: $scoremax\n";
#}	
	

my $voltes = 0; 
my $scorerandom = -999; 
my $c = 0;
my $pvalue;

	while ($voltes < 100 ){

	    my $n = scalar(@seqsencera);
	    my $y = $n - 1;

	    while ($y >= 0) {

		my $j = int(rand($y+1));

		if ($y != $j) {

		    my $tmp = $seqsencera[$y];

		    $seqsencera[$y] = $seqsencera[$j];
		    $seqsencera[$j] = $tmp;
		}
		$y = $y - 1;
	    }

	    $m = 0;
	    $posiciomillor = 0;

	    while ($m < $longseq-($compta)+1) {
	    
		$f = 0;
		$score = 0;
		$pmotiu = $m;
	
		while ($pmotiu < $compta) { 
		
		    if ($seqsencera[$pmotiu] eq 'A' || $seqsencera[$pmotiu] eq 'a' ){

			$score = $score + $hash{"A"}[$f];
		    }

		    if ($seqsencera[$pmotiu] eq 'C' || $seqsencera[$pmotiu] eq 'c' ){
			
			$score = $score + $hash{"C"}[$f];
		    } 

		    if ($seqsencera[$pmotiu] eq 'G' || $seqsencera[$pmotiu] eq 'g' ){

			$score = $score + $hash{"G"}[$f];
		    }

		    if ($seqsencera[$pmotiu] eq 'T' || $seqsencera[$pmotiu] eq 't' ){

			$score = $score + $hash{"T"}[$f];
		    }
		
		    $pmotiu = $pmotiu + 1;
		    $f = $f + 1; 
	      	
		}	
	    

	    if ($score > $scorerandom) {
		$scorerandom = $score;
		$posiciomillor = $m;
	    }


	    $m = $m + 1;
	      
	    }
  
	    if ($scorerandom >= $scoremax){
		$c = $c + 1;
	    }
	
	    $voltes = $voltes + 1;
	}
	if ($c == 0) {
	    
	    print "el p-value del factor de trasncripció $nom és 0,0\n";
	}
	else {

	$pvalue = $c/100;

	print "el p-value del factor de transcripció $nom és $pvalue \n";
      }


%hash = ( "A" => [0, 0, 0, 0, 0, 0, 0, 0, 0],
	  "C" => [0, 0, 0, 0, 0, 0, 0, 0, 0],
	  "G" => [0, 0, 0, 0, 0, 0, 0, 0, 0],
	  "T" => [0, 0, 0, 0, 0, 0, 0, 0, 0]);
	

	$lloc = 0;
	$compta = 0;
	
	}



}		
close (MATRIUS);
