~/www/Lesson5/parseGE.pl.html #! /usr/bin/perl -w
use strict;

die "Usage: parseGE.pl <GE-file>\n" if (@ARGV != 1);

# reading the GE file:
open(I,$ARGV[0]) or die "cuoldn't open file\n";
my %logRatio;
while(<I>){
    chomp;
    my ($probe,$gene,$LR) = split "\t";
    $logRatio{$gene} = [] unless ( exists $logRatio{$gene});
    push(@{$logRatio{$gene}},$LR]);
}
close(I);


# Print genes with an average fold change > 2:
while(my($g,$ref) = each(%logRatio)){
    my $LR = average($ref);
    if ($LR>2){
        print "Up-regulated Gene: $g\n"
    }
    elsif ($LR < 0.5) {
        print "Down-regulated Gene: $g\n"
    }
}

sub average {
    my $arrayref = shift;
    my $sum = 0;
    foreach my $a (@$arrayref){
        $sum+=$a;
    }
    return $sum/scalar(@$arrayref);
}