#!/usr/bin/perl use strict; ############# #usage stuff# ############# if (@ARGV != 2) { die "\nUsage: perl normalize.pl \n\n"; } ################################################################################################### # Get the first file name and open an instream to it. Open an outstream to the second file name.# #Also get the user input for thresholds. # ################################################################################################### my $infile = shift @ARGV; my $outfile = shift @ARGV; open (IN, "<$infile") or die "couldn't open file: $infile\n"; open (OUT, ">$outfile") or die "couldn't write to $outfile\n"; ################################################### #Keep track of index (which) number gene we're on.# ################################################### my $index = 0; my @name; # 1D array of names my @Data; # 2D array for genes and their expression values. #################################### #look in each line of the instream.# #################################### while (my $line = ) { ########################################### #Unless the line begins with Y, ignore it.# ########################################### if ($line =~ /^(Y\S+)\s+/){ ############################################################################# #Split the line into an array. Shift off the first element of the array and# #store it in the array name. ############################################################################# my @array = split /\s+/, $line; $name[$index] = shift @array; ########################################################################## #Get the length of what is left of the array and store it in the 2D array# #called Data. ########################################################################## @{$Data[$index]} = @array; # # Alternate, but messier way of accomplishing the same # # my $length = @array; # for($i=0;$i<$length;$i++){ # $Data[$index][$i]=$array[$i]; # } # $index++; } } ################## #close the infile# ################## close IN; ######################################################### #Since index started at 0, $index = the number of genes.# ######################################################### my $number_genes = $index; ################## #For each gene...# ################## for(my $i=0;$i<$number_genes;$i++){ my $length = scalar (@{$Data[$i]}); ################################################################ #Print the name of the gene (now you see why I saved the names)# ################################################################ print OUT $name[$i]; ######################################################################################## #The normalization factor is sqrt($Data[0]^2 + $Data[1]^2 + ... + $Data[$length - 1]^2)# #So lets do that. ######################################################################################## my $total = 0; for(my $j=0;$j<$length;$j++){ $total = $total + $Data[$i][$j]**2; } my $norm = sqrt($total); ############################################## #Print the normalized values to the OUT file.# ############################################## for(my $j=0;$j<$length;$j++){ print OUT "\t".$Data[$i][$j]/$norm; } print OUT "\n"; } ################### #close the outfile# ################### close OUT;