ensembl-hive  2.7.0
Mutable.pm
Go to the documentation of this file.
1 =head1 LICENSE
2 
3 See the NOTICE file distributed with this work for additional information
4 regarding copyright ownership.
5 
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9 
10  http://www.apache.org/licenses/LICENSE-2.0
11 
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 
18 =cut
19 
20 
21 =head1 CONTACT
22 
23  Please email comments or questions to the public Ensembl
24  developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.
25 
26  Questions may also be sent to the Ensembl help desk at
27  <http://www.ensembl.org/Help/Contact>.
28 
29 =cut
30 
31 =head1 NAME
32 
34 
35 =head1 SYNOPSIS
36 
37  # start with an empty tree
39 
40  # add a few intervals (i.e. Bio::EnsEMBL::Utils::Interval)
41  $tree->insert($i1);
42  $tree->insert($i2);
43  $tree->insert($i3);
44 
45  # query the tree
46  my $result = $tree->search(85, 100);
47  if (scalar @{$result}) {
48  print "Found overlapping interval: [", $result->[0]->start, ', ', $result->[0]->end, "\n";
49  }
50 
51 =head1 DESCRIPTION
52 
53 Class representing a dynamic, i.e. mutable, interval tree implemented as an augmented AVL balanced binary tree.
54 
55 This module is a wrapper around two possible implementations: one using the Perl extension (XS) mechanisms, and
56 a pure Perl (PP) one.
57 
58 The module is capable of detecting whether the XS module is available and it loads it in that
59 case; it falls back to the PP implementation otherwise.
60 
61 =head1 METHODS
62 
63 =cut
64 
65 package Bio::EnsEMBL::Utils::Tree::Interval::Mutable;
66 
67 use strict;
68 
69 use Bio::EnsEMBL::Utils::Scalar qw(assert_ref);
70 use Bio::EnsEMBL::Utils::Exception qw(throw warning info);
71 
72 # the modules providing the underlying implementation,
73 # either XS or pure perl fallback
74 my $XS = 'Bio::EnsEMBL::XS::Utils::Tree::Interval::Mutable';
75 my $PP = 'Bio::EnsEMBL::Utils::Tree::Interval::Mutable::PP';
76 
77 # if XS is used, version at least 1.3.1 is required (provides the interval tree library)
78 my $VERSION_XS = '1.3.1';
79 
80 my @public_methods = qw/ insert search remove size /;
81 
82 # import either XS or PP methods into namespace
83 unless ($Bio::EnsEMBL::Utils::Tree::Interval::Mutable::IMPL) {
84  # first check if XS is available and try to load it,
85  # otherwise fall back to PP implementation
86  _load_xs() or _load_pp() or throw "Couldn't load implementation: $@";
87 }
88 
89 
90 =head2 new
91 
92  Arg [] : none
93  Example : my $tree = Bio::EnsEMBL::Utils::Tree::Mutable();
94  Description : Constructor. Creates a new mutable tree instance
95  Returntype : Bio::EnsEMBL::Utils::Tree::Interval::Mutable
96  Exceptions : none
97  Caller : general
98 
99 =cut
100 
101 sub new {
102  my $caller = shift;
103  my $class = ref($caller) || $caller;
104 
105  # for ($XS|$PP)::new(0);
106  return eval qq| $Bio::EnsEMBL::Utils::Tree::Interval::Mutable::IMPL\::new( \$caller ) | unless $caller; ## no critic
107 
108  if (my $self = $Bio::EnsEMBL::Utils::Tree::Interval::Mutable::IMPL->new(@_)) {
109  $self->{_IMPL} = $Bio::EnsEMBL::Utils::Tree::Interval::Mutable::IMPL;
110  bless($self, $class);
111  return $self
112  }
113 
114  return;
115 }
116 
117 sub _load_xs {
118  _load($XS, $VERSION_XS);
119 }
120 
121 sub _load_pp {
122  _load($PP);
123 }
124 
125 sub _load {
126  my ($module, $version) = @_;
127  $version ||= '';
128 
129  eval qq| use $module $version |; ## no critic
130  info(sprintf("Cannot load %s interval tree implementation", $module eq $XS?'XS':'PP'), 2000)
131  and return if $@;
132 
133  push @Bio::EnsEMBL::Utils::Tree::Interval::Mutable::ISA, $module;
134  $Bio::EnsEMBL::Utils::Tree::Interval::Mutable::IMPL = $module;
135 
136  local $^W;
137  no strict qw(refs); ## no critic
138 
139  for my $method (@public_methods) {
140  *{"Bio::EnsEMBL::Utils::Tree::Interval::Mutable::$method"} = \&{"$module\::$method"};
141  }
142 
143  return 1;
144 }
145 
146 1;
147 
Bio::EnsEMBL::Utils::Interval
Definition: Interval.pm:41
EnsEMBL
Definition: Filter.pm:1
Bio::EnsEMBL::Utils::Tree::Interval::Mutable
Definition: Node.pm:10
Bio::EnsEMBL::Utils::Scalar
Definition: Scalar.pm:66
info
public info()
Bio
Definition: AltAlleleGroup.pm:4
Bio::EnsEMBL::Utils::Exception
Definition: Exception.pm:68