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

In reply to How to calculate sum of a column by window size based on another column by rnaeye

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.