Hi all!
I have a 2 columns file (position, value) and I want to create a sliding window of 10000 and calculate the average for each window.
My data is
position value 12498248 1.033560 12512575 0.566400 12513687 0.632940 12520106 0.232530 12523984 0.247300 12525383 1.487640
So far, I tried this code...but it doesn't work..
#!usr/bin/perl use warnings; use strict; use List::Util 'sum'; print ' ############### Establish windows of 10kb and calculate the average Recomb Rate ############### '; my $recomb_all = $ARGV[0]; my @recomb_all = @{get_contents ($recomb_all)}; #Testing content print "line 1 column 2 ::: $recomb_all[0][1]\n"; my $length = (scalar @recomb_all) - 1; my @window; my $window_size = 10000; my $window_step = 10000; my @average; for (my $i = 0; ($i + $window_size - $window_step) <= $recomb_all[$len +gth][0]; $i += $window_step) { my $e = $i + $window_size; if ($e > $recomb_all[$length][0]) { $e = $recomb_all[$length][0]; push (@window, $recomb_all[$length][1]); print "@window\n"; push (@average, $recomb_all[$length][0], my $av = average (@wi +ndow)); } } #print "@window\n"; #print "@average\n"; exit; ####################### #SUBROUTINES ####################### sub get_contents { my ( $file ) = @_; my @contents; open (FH, "< $file") or die "I cannot open $file file: $!\n"; my $x = 0; while (<FH>) { next unless /\S/; next if /^\s*[A-Z]+/i; @{$contents[$x]} = split /\s+/; ++$x; } return (\@contents); } # ---------- end of subroutine get_contents ---------- sub average { my @array = @_; return sum(@array) / @array; }
I would really appreciate any help! Thanks!
In reply to calculate average in sliding windows by marfabe
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |