#!/usr/bin/perl -w


### typical file would look like that (coma separated):
### gene_name_1,ranking_value_1
### ...
### gene_name_n,ranking_value_n
### a title line with gene names as columns
### condition_name1,value_gene_1,...,value_gene_n
### ...
### condition_name_m,value_gene_1,...,value_gene_n

###	Gene Name,M-Value,,,,,,,,,,
###	gapd,0.789003111,,,,,,,,,,
###	tub,0.650735595,,,,,,,,,,
###	ubq,0.62048636,,,,,,,,,,
###	h3,0.580001331,,,,,,,,,,
###	sand,0.532036061,,,,,,,,,,
###	pex,0.505854815,,,,,,,,,,
###	ef1,0.485996857,,,,,,,,,,
###	cul,0.466653562,,,,,,,,,,
###	apt,0.417903432,,,,,,,,,,
###	act,0.364232703,,,,,,,,,,
###	elf5,0.364232703,,,,,,,,,,
###	mean,act,apt,cul,ef1,elf5,gapd,h3,pex,sand,tub,ubq
###	gamb 17d,0.7256802,0.5655303,0.3679363,0.6429478,0.4477618,0.1702996,0.1520090,0.3097799,0.4402024,0.7419557,0.5456361
###	low N 17d,0.7193139,0.7463430,1.0000000,0.7795587,0.4832248,1.0000000,0.3120064,0.9375561,0.6432495,0.1766346,0.5611696
###	high N 17d,0.6178211,0.7254442,0.5936718,0.6764160,0.5655146,0.4890596,0.3236532,0.4913143,0.6008294,0.3013992,0.2934497
###	soil 17d,0.7056823,0.7972146,0.6644886,0.7831425,0.6799765,0.0819153,0.5627858,0.5074043,0.7188483,0.4134570,0.2493714
###	gamb 24d,0.7661184,1.0000000,0.6942459,0.8144842,0.7924170,0.0609783,0.1966419,0.5242920,0.5930368,0.3872969,0.6463174
###	soil 24d,0.8134993,0.8296919,0.6893051,0.7820285,0.7745306,0.1300274,0.3982714,0.5253837,0.7571154,0.4342143,0.4681179
###	high N 17d -> low N 24h,0.5541644,0.6271833,0.6001130,0.7441143,0.5334926,0.1836092,0.3431228,0.5718402,0.6702126,0.4223004,0.5202134
###	high N 17d -> high N 24h,0.3948556,0.7400247,0.4087920,0.5266601,0.3242503,0.1144436,0.3745454,0.3804182,0.4319581,0.3464067,0.4023886
###	cold 17d,1.0000000,0.8809984,0.8158856,0.8406042,0.8659109,0.0683866,0.1886298,0.4176980,0.6507459,0.8569031,1.0000000
###	gemmae,0.1805579,0.3812205,0.2719867,0.5439176,0.3128601,0.4838367,0.0905743,0.3654534,0.7317068,0.0924592,0.1186552
###	antheridiophore,0.4710501,0.4313783,0.9754552,0.7627097,0.4374533,0.0151336,1.0000000,1.0000000,0.9022355,1.0000000,0.2331234
###	archegoniophore,0.5214672,0.5659920,0.8002280,1.0000000,0.3768413,0.1890805,0.4427394,0.6556732,1.0000000,0.6428763,0.4965495
###	no ABA 17d,0.6958283,0.5760694,0.4192327,0.5156263,0.5615459,0.1323627,0.2528191,0.4316189,0.6461055,0.3025486,0.2410713
###	ABA 17d,0.3073190,0.3383048,0.3253314,0.3113465,0.3594728,0.2109371,0.1533020,0.3713031,0.4164315,0.2249489,0.1503999
###	no hor 17d -> no hor 24h,0.7746582,0.5518308,0.5219696,0.4484100,0.8380830,0.3958630,0.2247817,0.5972110,0.8315504,0.4391396,0.5443186
###	no hor 17d -> NAA 24h,0.6267987,0.5921991,0.4718037,0.4316798,0.6520532,0.1253435,0.2374211,0.4256159,0.5239225,0.4284684,0.3709998
###	no hor 17d -> ABA 24h,0.5396815,0.5251403,0.4280188,0.4691259,0.6021263,0.2966906,0.2329044,0.4754352,0.5419417,0.2847023,0.2608492
###	no hor 17d -> GR24 24h,0.9375617,0.8454586,0.7201772,0.8036730,1.0000000,0.1746125,0.3314604,0.5890146,0.6931669,0.5948717,0.6786401
###	low P 17d,0.2215482,0.4077500,0.3399191,0.1694243,0.1303249,0.2509033,0.2082642,0.2270573,0.2406004,0.3099704,0.3916339
###	high P 17d,0.3511340,0.4422522,0.2958036,0.2267300,0.2685404,0.1009860,0.2323521,0.2609601,0.2388235,0.2655960,0.2148862
###	no NAA 17d,0.6245300,0.5842262,0.4373772,0.3904580,0.3777922,0.1488360,0.2912044,0.2931871,0.2427235,0.5880916,0.5924456
###	NAA 17d,0.3552174,0.3671591,0.2172974,0.2113384,0.2803691,0.1427348,0.1294668,0.1201018,0.0901331,0.1930422,0.2289742

use strict;





my (%ranking, @order, @values, %conditions);
my $file = shift;


open IN, '<', $file or die;
<IN>;
my $values;
while (my $line = <IN>) {
	$line =~ s/\r\n$//;
	my @line = split /,/, $line;
	
	if ($line[0] =~ /mean/) {
		++$values;
		shift @line;
		foreach (@line) {
			push @order, $_;
		}
	} elsif ($values) {
		my $cond = shift @line;
		%{$conditions{$cond}} = ();
		my $i = 0;
		while ($i < @line) {
			$conditions{$cond}{$order[$i]} = $line[$i];
			++$i;
		}
	} else {
		$ranking{$line[0]} = $line[1];
	}
	
}
close IN;

my @ranked = sort {$ranking{$a} <=> $ranking{$b}} keys %ranking;

#foreach my $cond (keys %conditions) {
#	print "$cond:\n";
#	foreach my $gene (keys %{$conditions{$cond}}) {
#		print "\t$gene: $conditions{$cond}{$gene}\n";
#	}
#}



my $out = 'genorm_Ngene' . $file;
open OUT, '>', $out or die;


my $n = 1;
while ($n < @ranked) {
	my @Ajk;
	
	foreach my $cond (keys %conditions) {
		print "$n / cond: $cond\n";
		my $NFn = &arraymult($n, $cond)**(1/$n);
		print "NFn: $NFn\n";
		my $NFnp1 = &arraymult($n + 1, $cond)**(1/($n + 1));
		print "NFn+1: $NFnp1\n";
		my $A = &log2($NFn / $NFnp1);
		print "A: $A\n";
		push @Ajk, $A;
	}
	
	my ($mean, $sigma) = &getstats(\@Ajk);
	my $np1 = $n + 1;
	print OUT "V$n/$np1,$sigma\n";
	print "V$n/$np1,$sigma\n";
	
	++$n;
}

close OUT;

sub log2 {
	my $num = shift;
	return log($num) / log(2);
}

sub arraymult {
	my $n = shift;
	my $cond = shift;
	my $res = shift;
	$res = 1 unless $res;
	
	$res = &arraymult($n - 1, $cond, $res) if $n > 1;
	my $gene = $ranked[$n - 1];
	$res *= $conditions{$cond}{$gene};
	
	return $res;
}

sub getstats {
	my $input = shift;
	my $mean = 0;
	my $sigma = 0;
	
	# mean
	foreach (@$input) {
		$mean += $_;
	}
	$mean /= @$input;
	
	# sigma
	unless (@$input <= 1) {
		foreach (@$input) {
			$sigma += ($_ - $mean) ** 2;
		}
		$sigma /= @$input - 1;
		$sigma = sqrt $sigma;
	}
	
	return $mean, $sigma;
}


exit 0
