perl_paduan

It appears that you want to have three lists for each hash key, but your code is intermingling the data into a single list. You could split the lists out by name like this:

push( @{ $gene_hash{$gene_key}{min_start} }, $start ); push( @{ $gene_hash{$gene_key}{max_end} }, $end ); push( @{ $gene_hash{$gene_key}{max_ex} }, $ex );

Then, of course, you'd have to change your report loop a little:

my $Low=min( @ {$gene_hash{$key}{min_start} } ); my $High=max( @ {$gene_hash{$key}{max_end} } ); my $High_ex=max( @ {$gene_hash{$key}{max_ex} } );

However, since your report appears to give only one line per CHR:GENE, I'd suggest doing your data aggregation while reading, rather than trying to do it in your output loop. Also, your data structure uses hardcoded array positions for different data elements, which will be confusing when you change your program. So I'd suggest using a HoH, where the second level of your hash contains the name of the item you're aggregating, something like this (untested):

while (<DATA>) { chomp; my ($chr, $start, $end, $gene, $ex) = split(/\t/, $_); my $gene_key = $chr.":".$gene; my $cur_gene = $gene_hash{$gene_key}; $$cur_gene{min_start} = $start unless $$cur_gene{min_start}<$start +; $$cur_gene{max_end} = $end unless $$cur_gene{max_end}>$end; $$cur_gene{max_ex} = $ex unless $$cur_gene{max_ex}>$ex; $gene_hash{$gene_key} = $cur_gene; }

This code, as it reads each CHR:GENE combination will update the min_start, max_end and max_ex values. This way, at the end of your input loop, you already have the values you want, and you can simply retrieve your values in the output loop like this:

my $Low=$gene_hash{$key}{min_start}; my $High=$gene_hash{$key}{max_end}; my $High_ex=$gene_hash{$key}{max_ex};

...roboticus


In reply to Re: How to group by a column and calculate max/min on another by roboticus
in thread How to group by a column and calculate max/min on another by perl_paduan

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.