Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-04-18 05:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found