rnaeye has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks,
Once again I need your kind help. I am trying to calculate sum of column-3 by window size of column-2 in the following example. In this example I would like to divide positions in column-2 into 20 base pair wide windows as below:
window_of_col-2 positions(col-2) sum of col-3 (coverage) based +on window --------------- -------- --------------- 1-20 1,4,7 9 21-40 22,24,38 21 41-60 44,50,57,60 85 61-80 65 30 And, I want to print the results as below: window SUM 20 9 40 21 60 85 80 30
I spent a good amount of time on this, and I feel pretty stupid already. I tried range operator and tried to add a counter for window size, but could not make it work. Only the thing I can do is following that calculates sum of col-3 but not by window size. I would appriciate any help or pointers. Thanks.
#!/usr/bin/perl use warnings; use strict; use 5.010; my $total = 0; while (<DATA>){ chomp; my ($chr, $pos, $coverage) = split /\t/; $total += $coverage; } say $total; #------------------------- #data format: #chr positon_on_DNA coverage __DATA__ chr 1 2 chr 4 2 chr 7 5 chr 22 5 chr 24 6 chr 38 10 chr 44 10 chr 50 20 chr 57 25 chr 60 30 chr 65 30
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to calculate sum of a column by window size based on another column
by GrandFather (Saint) on Jun 27, 2013 at 01:29 UTC | |
|
Re: How to calculate sum of a column by window size based on another column
by Cristoforo (Curate) on Jun 27, 2013 at 04:53 UTC | |
|
Re: How to calculate sum of a column by window size based on another column
by rnaeye (Friar) on Jun 27, 2013 at 02:27 UTC |