PP.pm 7.63 KB
Newer Older
1 2 3
=head1 LICENSE

Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
4
Copyright [2016-2019] EMBL-European Bioinformatics Institute
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

=cut


=head1 CONTACT

  Please email comments or questions to the public Ensembl
  developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.

  Questions may also be sent to the Ensembl help desk at
  <http://www.ensembl.org/Help/Contact>.

=cut

=head1 NAME

33
Bio::EnsEMBL::Utils::Tree::Interval::Mutable::PP
34 35 36 37 38 39 40 41 42 43

=head1 DESCRIPTION

Pure Perl fall back implementation of a mutable interval tree, uses
augmented AVL binary balanced trees.
 
=head1 METHODS

=cut

44
package Bio::EnsEMBL::Utils::Tree::Interval::Mutable::PP;
45 46

use strict;
47
use Carp;
48

49 50
use Bio::EnsEMBL::Utils::Tree::Interval::Mutable::Node;
use Bio::EnsEMBL::Utils::Interval;
51
use Bio::EnsEMBL::Utils::Exception qw(throw);
52

53 54
=head2 new

55 56 57 58 59 60 61
  Arg []      : none
  Example     : my $tree = Bio::EnsEMBL::Utils::Tree::Mutable();
  Description : Constructor. Creates a new mutable tree instance
  Returntype  : Bio::EnsEMBL::Utils::Tree::Interval::Mutable::PP
  Exceptions  : none
  Caller      : general

62 63 64 65 66 67 68
=cut

sub new {
  my $caller = shift;
  my $class = ref($caller) || $caller;

  # mmhh, should probably return just the hash ref
69
  return bless({ _root => undef, _size => 0 }, $class);
70 71
}

72 73
=head2 root

74 75 76 77 78 79 80
  Arg []      : none
  Example     : my $root = $tree->root;
  Description : Returns the tree top node
  Returntype  : Bio::EnsEMBL::Utils::Tree::Interval::Mutable::Node
  Exceptions  : none
  Caller      : general

81 82
=cut

83
sub root {
84
  my $self = shift;
85 86 87 88 89
  $self->{_root} = shift if( @_ );
  
  return $self->{_root};
}

90 91
=head2 size

92 93 94 95 96 97 98
  Arg []      : none
  Example     : print "Tree size is ", $tree->size, "\n";
  Description : Return the size of the tree, i.e. the number of nodes
  Returntype  : scalar
  Exceptions  : none
  Caller      : general

99 100 101 102 103 104
=cut

sub size {
  return shift->{_size};
}

105 106
=head2 insert

107 108 109 110 111
  Arg [1]     : Bio::EnsEMBL::Utils::Interval
                Interval to insert
  Example     : $tree->insert(Bio::EnsEMBL::Utils::Interval->new(10, 20, 'data'));
  Description : Insert an interval in the tree
  Returntype  : scalar (1), upon success
112
  Exceptions  : thrown if Interval spans origin (has start > end)
113
  Caller      : general
114 115 116

=cut

117 118
sub insert {
  my ($self, $i) = @_;
119
  
120 121 122
  if ($i->spans_origin) {
    throw "Cannot insert an interval that spans the origin into a mutable tree";
  }
123 124
  # base case: empty tree, assign new node to root
  unless (defined $self->root) {
125
    $self->root(Bio::EnsEMBL::Utils::Tree::Interval::Mutable::Node->new($self, $i));
126
    $self->{_size}++;
127 128 129
    
    return 1;
  }
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

  # check if node exists with the same key
  my $node = $self->root->search_by_key($i->start);
  if ($node) {
    # check the node's intervals if there's already one
    # which is the same as the interval we're trying to insert
    map { return 0 if $_->start == $i->start and $_->end == $i->end } @{$node->intervals};

    # add the interval to the node
    $node->add_interval($i);

    # update max of node and its ancestors if necessary
    if ($node->max < $i->end) {
      $node->max($i->end);
      $node->_update_parents_max if $node->parent;
    }

147 148
    $self->{_size}++;
    
149 150 151 152 153
    return 1;
  } else {
    # node with the interval's key doesn't exist
    # insert from root node
    $self->root->insert($i);
154 155
    $self->{_size}++;
    
156 157 158 159
    return 1;
  }

  croak "Shouldn't be here";
160 161
}

162 163
=head2 search

164 165 166 167 168 169 170 171 172 173
  Arg [1]     : scalar, $start
                the starting point of the interval to search
  Arg [2]     : scalar, $end
                the end point of the interval to search
  Example     : my $result = $tree->search(85, 100);
  Description : Search the intervals in the tree overlapping the query
  Returntype  : Arrayref of Bio::EnsEMBL::Utils::Interval instances
  Exceptions  : none
  Caller      : general

174 175 176
=cut

sub search {
177
  my ($self, $start, $end) = @_;
178 179
  
  return unless $self->root; # empty tree
180
  return $self->root->search(Bio::EnsEMBL::Utils::Interval->new($start, $end));
181 182 183 184 185
  
}

=head2 remove

186 187 188 189 190 191 192 193
  Arg [1]     : Bio::EnsEMBL::Utils::Interval
                The interval to remove from the tree
  Example     : $tree->remove($i);
  Description : Remove an interval from the tree
  Returntype  : scalar, 1 if removal is successful, 0 otherwise
  Exceptions  : none
  Caller      : general

194 195 196 197 198 199 200 201 202 203 204 205 206 207
=cut

sub remove {
  my ($self, $i) = @_;

  return 0 unless $self->root; # empty tree

  my $node = $self->root->search_by_key($i->start);
  return 0 unless $node;
  
  my $node_intervals = $node->intervals;
  if (scalar @{$node_intervals} > 1) {
    # node with this key has more than this interval. Find it and remove
    my $removed = 0;
208 209 210
    foreach my $j (0 .. $#{$node_intervals}) {
      if ($node_intervals->[$j]->start == $i->start and $node_intervals->[$j]->end == $i->end) {
	splice @{$node_intervals}, $j, 1;
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
	$removed = 1;
	last;
      }
    }

    if ($removed) {
      $removed = 0;

      # update max of node and its ancestors, if necessary
      if ($i->end == $node->max) {
	my $node_highest_end = $node->_highest_end;
	if ($node->left and $node->right) {
	  $node->{max} = max $node->left->max, $node->rigth->max, $node_highest_end;
	} elsif ($node->left and not $node->right) {
	  $node->{max} = max $node->left->max, $node_highest_end;
	} elsif ($node->right and not $node->left) {
	  $node->{max} = max $node->right->max, $node_highest_end;
	} else {
	  $node->{max} = $node_highest_end;
	}

	$node->parent->_update_parents_max if $node->parent;
      }
234 235

      $self->{_size}--;
236 237 238 239 240 241 242 243 244 245 246 247 248 249
      
      return 1;
    } else {
      return 0;
    }
  } elsif (scalar @{$node_intervals}) {
    # node with this key has only this interval
    # check if the remaining node's interval is the one we want to remove
    if ($node_intervals->[0]->start == $i->start and $node_intervals->[0]->end == $i->end) {
      # remove the whole node from the tree
      if ($node->key == $self->root->key) {
	# we're removing the root node
	# create dummy node temporarily taking root's parent node
	my $root_parent =
250 251
	  Bio::EnsEMBL::Utils::Tree::Interval::Mutable::Node->new($self,
								  Bio::EnsEMBL::Utils::Interval->new($i->start, $i->end));
252
	$root_parent->left($self->root);
253 254 255 256 257 258 259 260
	$self->root->parent($root_parent);
	
	my $removed_node = $self->root->remove($node);
	$self->root($root_parent->left);

	$self->root->parent(undef) if $self->root;
	if ($removed_node) {
	  $removed_node = undef;
261 262
	  $self->{_size}--;
	  
263 264 265 266 267 268
	  return 1;
	} else {
	  return 0;
	}
      } else {
	my $removed_node = $self->root->remove($node);
269 270 271 272 273 274 275 276
	if ($removed_node) {
	  $removed_node = undef;
	  $self->{_size}--;
	  
	  return 1;
	} else {
	  return 0;
	}
277 278 279 280 281 282 283 284 285 286
      }
    } else {
      # the remaining record is not the one we want to remove
      return 0;
    }
    
  } else {
    # no records at all in this node, shouldn't happen
    croak "This should not happen";
  }
287 288
}

289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
sub _in_order_traversal_delete {
  my ($self, $node) = @_;

  return unless $node;

  $self->_in_order_traversal_delete($node->left);
  $node->left(undef);
  $self->_in_order_traversal_delete($node->right);
  $node->right(undef);

  $node->parent(undef);

}

sub DESTROY {
  my $self = shift;

  $self->_in_order_traversal_delete($self->root);
}

309 310
1;