$normalize = 1;
$no_unique = 1;
$nr_headers = 23;
open DATAFILE, 'Man_processed_reactome_output.txt';
$i = 1;
while (<DATAFILE>) 
{
	chomp;
	@x = split '	',$_;
	for ($j=1;$j<=$#x+1;$j++) 
		{
			$datafile[$i][$j]=$x[$j-1]
		}; 
	$i += 1;
};
close DATAFILE;
$nr_col = $#x+1;
$nr_lines = $i-1;
print "Nr col: ",$nr_col," and nr lines: ",$nr_lines,"\n";
$tot_nr_genes = 0;      	

for ($i=1;$i<=$nr_lines;$i++)
{
    @y = split ';',$datafile[$i][$nr_headers];
    $nr_genes[$i]=$#y+1;
    for ($j=1;$j<=$nr_genes[$i];$j++)
    {
        $gene_found = 0;
        $k = 1;
        while ($gene_found == 0 && $k <= $tot_nr_genes)
        {
            if ($y[$j-1] eq $name_gene[$k])
            {
                $datafile[$i][$nr_headers+$k] = 1;
                $gene_found = 1;
            };
            $k+=1;
        };
        if ($gene_found == 0)
        {
            $tot_nr_genes += 1;
            $name_gene[$tot_nr_genes] = $y[$j-1];
            $datafile[0][$nr_headers+$tot_nr_genes] = $y[$j-1];
            for ($l=1;$l<=$nr_lines;$l++)
            {
                $datafile[$l][$nr_headers+$tot_nr_genes] = 0;
            };
            $datafile[$i][$nr_headers+$tot_nr_genes] = 1;
        };
    };
};

if ($no_unique == 1)
{
    for ($i=1;$i<=$nr_lines;$i++)
    {
                $datafile[0][$nr_headers+$tot_nr_genes+$i] = "Extra";
                for ($k=1;$k<=$nr_lines;$k++)
                {
                      $datafile[$k][$nr_headers+$tot_nr_genes+$i] = 0;   
                };
                $datafile[$i][$nr_headers+$tot_nr_genes+$i] = 1;
    };
};

if ($normalize == 1)
{
    #for ($i = 1;$i<=$tot_nr_genes;$i++)
	for ($i = 1;$i<=$tot_nr_genes+$nr_lines;$i++)
    {
        $mean = 0;
        for ($j=1;$j<=$nr_lines;$j++)
        {
               $mean += $datafile[$j][$nr_headers+$i]
        };
        $mean = $mean/$nr_lines;
        $var = 0;
        for ($j=1;$j<=$nr_lines;$j++)
        {
               $var += ($datafile[$j][$nr_headers+$i]-$mean)**2
        };
        $var = $var/($nr_lines-1);
        $sd = sqrt($var);
        
        for ($j=1;$j<=$nr_lines;$j++)
        {
               $datafile[$j][$nr_headers+$i] = ($datafile[$j][$nr_headers+$i]-$mean)/$sd;
        };
    };
};




open RESULTS, '>Reactome_prep.out';
print RESULTS "EXP	TYPE	TYPE_2	COLX	CLUSTER	R_NR	R_TERM	R_TERM2	CODE1	CODE2	CODE3	CODE4	COLY	TERM1	TERM2	TERM3	TERM4	TERM5	TERM6	TERM7	TERM8	P_VAL	ALL_GENES";
for ($i=1;$i<=$tot_nr_genes+$nr_lines;$i++)
#for ($i=1;$i<=$tot_nr_genes;$i++)
{
    print RESULTS "	",$datafile[0][$nr_headers+$i];
};
#print RESULTS "	NR_GENES\n";
print RESULTS "\n";
for ($i=1;$i<=$nr_lines;$i++)
{
    print RESULTS $datafile[$i][1];
    for ($j=2;$j<=$nr_headers+$tot_nr_genes;$j++)
    {
        print RESULTS "	",$datafile[$i][$j];
    };
    for ($j=1;$j<=$nr_lines;$j++)
    {
        print RESULTS "	",$datafile[$i][$nr_headers+$tot_nr_genes+$j];
    };
    #print RESULTS "	",$nr_genes[$i],"\n";
	print RESULTS "\n";
};

close RESULTS;
print "Number of genes is ",$tot_nr_genes,"\n";
print "Hope you like the results ... \n";
 