Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Hello lunseyr.

I guess maybe something like this ...Input <DATA> may be diffrent from your files. This example prints

overlaps:9,200
no overlaps for chr2
I just thought packages may be good for adding constraints to your items.

Here I used Moose, but I am feeling something not good. I hope someone points me for better design.
use strict; use warnings; { #packages to use package Item; use Any::Moose; has "file" => (isa =>"Str", required=>1, is=>"rw"); has "bgn" => (isa =>"Int", required=>1, is=>"rw"); has "end" => (isa =>"Int", required=>1, is=>"rw"); 1; package overlaps; use Any::Moose; has "items" => (isa =>"ArrayRef[Item]", is=>"rw"); sub find_overlaps { my $self=shift; my $cur_bgn=undef; my $cur_end=undef; for (@{ $self->items }){ #printf "%s,%d,%d\n", $_->file, $_->bgn,$_->end; if (! defined($cur_bgn) || ! defined($cur_end) ){ $cur_bgn =$_->bgn; $cur_end =$_->end; next; } if ( ( $_->bgn >= $cur_bgn && $_->bgn <= $cur_end ) or ( $_->end >= $cur_bgn && $_->end <= $cur_end ) ){ #ok }else { return undef; } $cur_bgn = $_->bgn if ( $_->bgn < $cur_bgn ); $cur_end = $_->end if ( $_->end > $cur_end ); } return [$cur_bgn, $cur_end]; } 1; } #end packages to use my %h; #read data to hash $/=""; while(<DATA>){ my @rec=split /\n/; $rec[0] =~ s/^file:\s*//; my($k,$bgn,$end)=split /\s+/, $rec[1]; push @{$h{$k}} , Item->new(file=>$rec[0],bgn=>$bgn,end=>$end); } #check overlaps foreach my $k (keys %h){ my $o=overlaps->new(items=>$h{$k}); my $ret=$o->find_overlaps; if ($ret){ print "overlaps:", join(',', @$ret) ,"\n"; } else { print "no overlaps for $k\n"; } } __DATA__ file: 148N chr1 10 50 file: 162N chr1 9 40 file: 174N chr1 12 60 file: 175N chr1 30 45 file: test chr1 10 200 file: test2 chr2 1 3 file: test3 chr2 5 10
regards.


In reply to Re: Find overlap by remiah
in thread Find overlap by linseyr

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!
  • 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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (5)
    As of 2015-07-04 00:31 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (57 votes), past polls