in reply to Re^3: average a column in tab-delimited file
in thread average a column in tab-delimited file
well I've written something that almost does the job, except it goes wrong on the very last line of the file. I read the file into an array and calculate the averages which I then put into a hash and then iterate through the array and lookup values from the hashes. I'm sure there's a more elegant way to do it though....
#!/usr/bin/perl # Usage: perl average_table.pl -i <input file> use strict; use warnings; use Getopt::Long; my $infile; my @fields; my @array; my @values; my %hash; my $add_fst; my @out; my @count; my $i=1; my @final; my %pval; my %fst; my $prev_loc; my $prev_pval; my $prev_fst; my $prev_join; GetOptions ( "infile=s" => \$infile, ); open INFILE, "<$infile" or die $!; @fields = <INFILE>; foreach (@fields){ @array = split(/\t+/, $_); chomp (@array); my ($name, $chr, $location, $gen, $dom, $pval, $fst) = @array[ +0..6]; my $join = "$chr-$location"; push (@values, $join, $pval); if ($location == $prev_loc){ $pval = $pval + $prev_pval; $fst = $fst + $prev_fst; $i++; } elsif ($location != $prev_loc) { my $pval_average = ($prev_pval / $i); my $fst_average = ($prev_fst / $i); my $join2 = "$prev_join\t$pval_average\t$fst_average"; $pval{$prev_join} = $pval_average; $fst{$prev_join} = $fst_average; push (@final, $join2); $i = 1; } $prev_loc = $location; $prev_pval = $pval; $prev_fst = $fst; $prev_join = $join; } foreach (@fields){ @out = split(/\t+/, $_); chomp (@out); my ($name, $chr, $location, $gen, $dom, $pval, $fst) = @ou +t[0..6]; my $join = "$chr-$location"; my $new_pval = $pval{$join}; my $new_fst = $fst{$join}; print $name."\t".$chr."\t".$location."\t".$gen."\t".$dom."\t". +$new_pval."\t".$new_fst."\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: average a column in tab-delimited file
by Cristoforo (Curate) on Feb 02, 2012 at 01:09 UTC | |
|
Re^5: average a column in tab-delimited file
by Cristoforo (Curate) on Feb 04, 2012 at 20:50 UTC | |
|
Re^5: average a column in tab-delimited file
by Anonymous Monk on Feb 05, 2012 at 16:22 UTC |