Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re^5: average a column in tab-delimited file

by Cristoforo (Deacon)
on Feb 02, 2012 at 01:09 UTC ( #951322=note: print w/replies, xml ) Need Help??

in reply to Re^4: average a column in tab-delimited file
in thread average a column in tab-delimited file

it goes wrong on the very last line of the file

Between the 2 foreach blocks, add these 2 lines of code.

$pval{$prev_join} = $prev_pval/$i; $fst{$prev_join} = $prev_fst/$i;

There were alot of warnings about some of your variables not being initialized. To get rid of the warnings, I initialized them with values from the first line in the file. Posting some of that code here.

open INFILE, "<$infile" or die $!; chomp(my @lines = <INFILE>); close INFILE or die $!; my ($chr, $prev_loc, $prev_pval, $prev_fst) = (split /\t/, $lines[0])[ +1,2,5,6]; my $prev_join = "$chr-$prev_loc"; my $i=1; my %pval; my %fst; foreach (@lines[1 .. $#lines]){ my ($name, $chr, $location, $gen, $dom, $pval, $fst) = split /\t/; my $join = "$chr-$location"; if ($location == $prev_loc){ $pval = $pval + $prev_pval; $fst = $fst + $prev_fst; $i++; } else { $pval{$prev_join} = $prev_pval / $i; $fst{$prev_join} = $prev_fst / $i; $i = 1; } $prev_loc = $location; $prev_pval = $pval; $prev_fst = $fst; $prev_join = $join; } $pval{$prev_join} = $prev_pval/$i; $fst{$prev_join} = $prev_fst/$i; foreach (@lines){ my ($name, $chr, $location, $gen, $dom, $pval, $fst) = split /\t+/ +; 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"; }

There are 2 different criteria about what you use to create a group to average. In your code, a group to average is determined by the code, if ($location == $prev_loc) .... But you are also joining the $chr and $location keys for the 2 different hashes here. I believe this leaves the door open for difficult to trace behaviors. It works without error with the sample data provided, but it could break down with an actual file.

Would you want a line like if ($join eq $prev_join)...?

Why join the chr location columns. Can the same location have different 'chr' columns? If not, there is no advantage to combining them like that. They could be more simply using the location for the 2 hashes here and the keys are simple to sort, the keys being 'location' alone - a floating point number.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://951322]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (10)
As of 2017-05-23 08:05 GMT
Find Nodes?
    Voting Booth?