marfabe has asked for the wisdom of the Perl Monks concerning the following question:
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!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: calculate average in sliding windows
by moritz (Cardinal) on May 23, 2012 at 09:45 UTC | |
Re: calculate average in sliding windows
by BrowserUk (Patriarch) on May 23, 2012 at 09:51 UTC | |
by Eliya (Vicar) on May 23, 2012 at 11:47 UTC | |
Re: calculate average in sliding windows
by jwkrahn (Abbot) on May 23, 2012 at 11:48 UTC | |
by perlygapes (Beadle) on Jun 06, 2018 at 13:39 UTC | |
Re: calculate average in sliding windows
by marfabe (Initiate) on May 23, 2012 at 13:58 UTC | |
by Eliya (Vicar) on May 23, 2012 at 14:27 UTC | |
by marfabe (Initiate) on May 23, 2012 at 14:43 UTC | |
by Eliya (Vicar) on May 23, 2012 at 15:05 UTC | |
by marfabe (Initiate) on May 23, 2012 at 15:31 UTC |