Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

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

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://998900]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2017-01-22 00:37 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (186 votes). Check out past polls.