Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Find overlap

by remiah (Hermit)
on Oct 14, 2012 at 02:05 UTC ( #998900=note: print w/ replies, xml ) Need Help??


in reply to Find overlap

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.


Comment on Re: Find overlap
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://998900]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (9)
As of 2015-07-29 04:59 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 (260 votes), past polls