$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.
|