| 1 | package Bio::Gonzales::Range::Overlap; | ||
| 2 | |||
| 3 | use Mouse; | ||
| 4 | |||
| 5 | use warnings; | ||
| 6 | use strict; | ||
| 7 | use Carp; | ||
| 8 | use Set::IntervalTree; | ||
| 9 | |||
| 10 | use 5.010; | ||
| 11 | |||
| 12 | our $VERSION = '0.058'; # VERSION | ||
| 13 | |||
| 14 | has _tree => ( is => 'rw', default => sub { Set::IntervalTree->new } ); | ||
| 15 | has keep_coords => ( is => 'rw', default => 1 ); | ||
| 16 | |||
| 17 | sub BUILD { | ||
| 18 | my $self = shift; | ||
| 19 | my $args = shift; | ||
| 20 | |||
| 21 | if ( $args->{ranges} ) { | ||
| 22 | $self->insert( $args->{ranges} ); | ||
| 23 | } | ||
| 24 | } | ||
| 25 | |||
| 26 | sub insert { | ||
| 27 | my $self = shift; | ||
| 28 | |||
| 29 | if ( @_ == 1 && ref $_[0] eq 'ARRAY' ) { | ||
| 30 | for my $r ( @{ $_[0] } ) { | ||
| 31 | $self->_insert(@$r); | ||
| 32 | } | ||
| 33 | } else { | ||
| 34 | $self->_insert(@_); | ||
| 35 | } | ||
| 36 | |||
| 37 | return; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub _insert { | ||
| 41 | my ( $self, $start, $end, @rest ) = @_; | ||
| 42 | |||
| 43 | ( $start, $end ) = ( $end, $start ) | ||
| 44 | if ( $start > $end ); | ||
| 45 | |||
| 46 | my $obj; | ||
| 47 | if ( $self->keep_coords ) { | ||
| 48 | $obj = [ $start, $end, @rest ]; | ||
| 49 | } elsif ( @rest > 1 ) { | ||
| 50 | $obj = \@rest; | ||
| 51 | } else { | ||
| 52 | $obj = $rest[0]; | ||
| 53 | } | ||
| 54 | |||
| 55 | $self->_tree->insert( $obj, $start, $end ); | ||
| 56 | |||
| 57 | return; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub contained_in { | ||
| 61 | my ( $self, $from, $to ) = @_; | ||
| 62 | |||
| 63 | ( $from, $to ) = ( $to, $from ) | ||
| 64 | if ( $from > $to ); | ||
| 65 | #$to--; | ||
| 66 | #$from++; | ||
| 67 | |||
| 68 | return $self->_tree->fetch_window( $from, $to ); | ||
| 69 | } | ||
| 70 | |||
| 71 | sub overlaps_with { | ||
| 72 | my ( $self, $from, $to ) = @_; | ||
| 73 | |||
| 74 | ( $from, $to ) = ( $to, $from ) | ||
| 75 | if ( $from > $to ); | ||
| 76 | $from--; | ||
| 77 | $to++; | ||
| 78 | |||
| 79 | return $self->_tree->fetch( $from, $to ); | ||
| 80 | } | ||
| 81 | |||
| 82 | __PACKAGE__->meta->make_immutable(); | ||
| 83 | |||
| 84 | __END__ | ||
| 85 | |||
| 86 | =head1 NAME | ||
| 87 | |||
| 88 | Bio::Gonzales::Range::Overlap - find overlapping ranges | ||
| 89 | |||
| 90 | =head1 SYNOPSIS | ||
| 91 | |||
| 92 | use 5.010; | ||
| 93 | use Bio::Gonzales::Range::Overlap; | ||
| 94 | use Data::Dumper; | ||
| 95 | |||
| 96 | my @ranges1 = ( | ||
| 97 | [ 0, 5, 'some', 'information' ], | ||
| 98 | [ 6, 8, 'some', 'other', 'information' ], | ||
| 99 | [ 7, 10, 'nonsense', 'information' ], | ||
| 100 | [ 11, 100, { 'very' => 'complicated', "data" => 'structure' } ], | ||
| 101 | ); | ||
| 102 | |||
| 103 | my $ro = Bio::Gonzales::Range::Overlap->new; | ||
| 104 | |||
| 105 | #build query db from 1st set of intervals | ||
| 106 | for my $r (@ranges1) { | ||
| 107 | $ro->insert(@$r); | ||
| 108 | } | ||
| 109 | |||
| 110 | # in this case (from and to are elements 0 and 1) | ||
| 111 | # insert could be called with all ranges | ||
| 112 | #$ro->insert(\@ranges1); | ||
| 113 | |||
| 114 | my @ranges2 = ( [ 8, 10 ], [ 1, 3 ], [99,200],); | ||
| 115 | |||
| 116 | # query the db with ranges | ||
| 117 | for my $r (@ranges2) { | ||
| 118 | say "Range (" . join(",", @$r) . ") overlaps with:"; | ||
| 119 | say Dumper $ro->overlaps_with(@$r); | ||
| 120 | } | ||
| 121 | |||
| 122 | =head1 DESCRIPTION | ||
| 123 | |||
| 124 | A C<@range> has the form C<($from, $to, @additional elements)>. Lists of | ||
| 125 | ranges have the form C<([$from, $to, @add_elems], [$from, $to, @add_elems], ...)>. | ||
| 126 | |||
| 127 | =head1 OPTIONS | ||
| 128 | |||
| 129 | =head1 METHODS | ||
| 130 | |||
| 131 | =over 4 | ||
| 132 | |||
| 133 | =item B<< $ro->insert(@range) >> | ||
| 134 | |||
| 135 | =item B<< $ro->insert(\@list_of_ranges) >> | ||
| 136 | |||
| 137 | =item B<< \@ranges_contained_in_given_range = $ro->contained_in(@range) >> | ||
| 138 | |||
| 139 | =item B<< \@ranges_that_overlap_with_given_range = $ro->overlaps_with(@range) >> | ||
| 140 | |||
| 141 | =back | ||
| 142 | |||
| 143 | =head1 SEE ALSO | ||
| 144 | |||
| 145 | =over 4 | ||
| 146 | |||
| 147 | =item L<Bio::Gonzales::Matrix::IO> for reading in ranges from files | ||
| 148 | |||
| 149 | =item L<Bio::Gonzales::Range::GroupedOverlap> for grouped ranges such as genes | ||
| 150 | that are grouped by chromosomes. | ||
| 151 | |||
| 152 | =back | ||
| 153 | |||
| 154 | =head1 AUTHOR | ||
| 155 | |||
| 156 | jw bargsten, C<< <jwb at cpan dot org> >> | ||
| 157 | |||
| 158 | =cut |