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