sync w/ main trunk
[bioperl-live.git] / Bio / Map / MappableI.pm
blob056a90cba42c6407fc56bf2cbf0b17aaac83214d
1 # $Id$
3 # BioPerl module for Bio::Map::MappableI
5 # Please direct questions and support issues to <bioperl-l@bioperl.org>
7 # Cared for by Sendu Bala <bix@sendu.me.uk>
9 # Copyright Jason Stajich
11 # You may distribute this module under the same terms as perl itself
13 # POD documentation - main docs before the code
15 =head1 NAME
17 Bio::Map::MappableI - An object that can be placed in a map
19 =head1 SYNOPSIS
21 # do not use this module directly
22 # See Bio::Map::Mappable for an example of
23 # implementation.
25 =head1 DESCRIPTION
27 This object handles the generic notion of an element placed on a
28 (linear) Map. A Mappable can have multiple positions in multiple maps, such as
29 is the case of Restriction enzyme cut sites on sequence maps. For exact
30 information about a mappable's position in a map one must query the associate
31 PositionI objects which are accessible through the get_positions() method.
33 =head1 FEEDBACK
35 =head2 Mailing Lists
37 User feedback is an integral part of the evolution of this and other
38 Bioperl modules. Send your comments and suggestions preferably to
39 the Bioperl mailing list. Your participation is much appreciated.
41 bioperl-l@bioperl.org - General discussion
42 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
44 =head2 Support
46 Please direct usage questions or support issues to the mailing list:
48 L<bioperl-l@bioperl.org>
50 rather than to the module maintainer directly. Many experienced and
51 reponsive experts will be able look at the problem and quickly
52 address it. Please include a thorough description of the problem
53 with code and data examples if at all possible.
55 =head2 Reporting Bugs
57 Report bugs to the Bioperl bug tracking system to help us keep track
58 of the bugs and their resolution. Bug reports can be submitted via the
59 web:
61 http://bugzilla.open-bio.org/
63 =head1 AUTHOR - Jason Stajich
65 Email jason@bioperl.org
67 =head1 CONTRIBUTORS
69 Heikki Lehvaslaiho heikki-at-bioperl-dot-org
70 Lincoln Stein lstein@cshl.org
71 Sendu Bala bix@sendu.me.uk
73 =head1 APPENDIX
75 The rest of the documentation details each of the object methods.
76 Internal methods are usually preceded with a _
78 =cut
80 # Let the code begin...
82 package Bio::Map::MappableI;
83 use strict;
84 use Bio::Map::PositionHandler;
86 use base qw(Bio::Map::EntityI Bio::AnnotatableI);
88 =head2 EntityI methods
90 These are fundamental to coordination of Mappables and other entities, so are
91 implemented at the interface level
93 =cut
95 =head2 get_position_handler
97 Title : get_position_handler
98 Usage : my $position_handler = $entity->get_position_handler();
99 Function: Gets a PositionHandlerI that $entity is registered with.
100 Returns : Bio::Map::PositionHandlerI object
101 Args : none
103 =cut
105 sub get_position_handler {
106 my $self = shift;
107 unless (defined $self->{_eh}) {
108 my $ph = Bio::Map::PositionHandler->new(-self => $self);
109 $self->{_eh} = $ph;
110 $ph->register;
112 return $self->{_eh};
115 =head2 PositionHandlerI-related methods
117 These are fundamental to coordination of Mappables and other entities, so are
118 implemented at the interface level
120 =cut
122 =head2 add_position
124 Title : add_position
125 Usage : $mappable->add_position($position);
126 Function: Add a position to this mappable (defining where on which map it is).
127 Returns : n/a
128 Args : L<Bio::Map::PositionI> object
130 =cut
132 sub add_position {
133 my $self = shift;
134 # actually, we allow multiple positions to be set at once
135 $self->get_position_handler->add_positions(@_);
138 =head2 get_positions
140 Title : get_positions
141 Usage : my @positions = $mappable->get_positions();
142 Function: Get all the Positions of this Mappable (sorted).
143 Returns : Array of L<Bio::Map::PositionI> objects
144 Args : none for all, OR
145 L<Bio::Map::MapI> object for positions on the given map, AND/OR some
146 other true value to avoid sorting
148 =cut
150 sub get_positions {
151 my ($self, $thing, $no_sort) = @_;
152 my $map;
153 if (ref($thing) && $thing->isa('Bio::Map::MapI')) {
154 $map = $thing;
156 else {
157 $no_sort = $thing;
159 my @positions = $self->get_position_handler->get_positions($map);
160 return @positions if @positions == 1;
162 unless ($no_sort) {
163 # don't do
164 # @positions = sort { $a->sortable <=> $b->sortable } @positions;
165 # directly since sortable() can result in the call of another sort
166 # routine and cause problems; pre-compute sortable values instead
167 # (which is also more efficient)
168 @positions = map { $_->[1] }
169 sort { $a->[0] <=> $b->[0] }
170 map { [$_->sortable, $_] }
171 @positions;
173 return @positions;
176 =head2 each_position
178 Title : each_position
179 Function: Synonym of the get_positions() method.
180 Status : deprecated, will be removed in next version
182 =cut
184 *each_position = \&get_positions;
186 =head2 purge_positions
188 Title : purge_positions
189 Usage : $mappable->purge_positions();
190 Function: Remove positions from this mappable.
191 Returns : n/a
192 Args : none to remove all positions, OR
193 L<Bio::Map::PositionI> object to remove just that Position, OR
194 L<Bio::Map::MapI> object to remove only those positions on the given
197 =cut
199 sub purge_positions {
200 my ($self, $thing) = @_;
201 $self->get_position_handler->purge_positions($thing);
204 =head2 known_maps
206 Title : known_maps
207 Usage : my @maps = $marker->known_maps()
208 Function: Returns the maps that this mappable is found on
209 Returns : Array of L<Bio::Map::MapI> objects
210 Args : none
212 =cut
214 sub known_maps {
215 my $self = shift;
216 return $self->get_position_handler->get_other_entities;
219 =head2 MappableI-specific methods
221 =cut
223 =head2 name
225 Title : name
226 Usage : my $name = $marker->name();
227 $marker->name($new_name);
228 Function: Get/Set the name for this Mappable.
229 Returns : A scalar representing the current name of this Mappable
230 Args : none to get
231 string to set
233 =cut
235 sub name {
236 my $self = shift;
237 $self->throw_not_implemented();
240 =head2 id
242 Title : id
243 Usage : my $id = $marker->id();
244 $marker->id($new_id);
245 Function: Get/Set the id for this Mappable.
246 Returns : A scalar representing the current id of this Mappable
247 Args : none to get
248 string to set
250 =cut
252 sub id {
253 my $self = shift;
254 $self->throw_not_implemented();
257 =head2 in_map
259 Title : in_map
260 Usage : if ($marker->in_map($map)) {...}
261 Function: Tests if this mappable is found on a specific map
262 Returns : boolean
263 Args : L<Bio::Map::MapI>
265 =cut
267 sub in_map {
268 my $self = shift;
269 $self->throw_not_implemented();
272 =head1 RangeI-related Methods
274 They throw an error if start and end are not defined in the Positions of the
275 Mappables supplied.
277 =cut
279 =head2 equals
281 Title : equals
282 Usage : if ($mappable->equals($other_mappable)) {...}
283 my @equal_positions = $mappable->equals($other_mappable);
284 Function: Finds the positions in this mappable that are equal to any
285 comparison positions.
286 Returns : array of L<Bio::Map::PositionI> objects
287 Args : arg #1 = L<Bio::Map::MappableI> OR L<Bio::Map::PositionI> to compare
288 this one to (mandatory)
289 arg #2 = optionally, the key => value pairs below
290 -map => Bio::Map::MapI : optionally a Map to only consider
291 positions on the given map
292 -relative => Bio::Map::RelativeI : optionally a Relative to ask if
293 the Positions equal in terms of
294 their relative position to the
295 thing described by that Relative
297 =cut
299 sub equals {
300 my $self = shift;
301 $self->throw_not_implemented();
304 =head2 overlaps
306 Title : overlaps
307 Usage : if ($mappable->overlaps($other_mappable)) {...}
308 my @overlapping_positions = $mappable->overlaps($other_mappable);
309 Function: Finds the positions in this mappable that overlap with any
310 comparison positions.
311 Returns : array of L<Bio::Map::PositionI> objects
312 Args : arg #1 = L<Bio::Map::MappableI> OR L<Bio::Map::PositionI> to compare
313 this one to (mandatory)
314 arg #2 = optionally, the key => value pairs below
315 -map => Bio::Map::MapI : optionally a Map to only consider
316 positions on the given map
317 -relative => Bio::Map::RelativeI : optionally a Relative to ask if
318 the Positions overlap in terms of
319 their relative position to the
320 thing described by that Relative
322 =cut
324 sub overlaps {
325 my $self = shift;
326 $self->throw_not_implemented();
329 =head2 contains
331 Title : contains
332 Usage : if ($mappable->contains($other_mappable)) {...}
333 my @container_positions = $mappable->contains($other_mappable);
334 Function: Finds the positions in this mappable that contain any comparison
335 positions.
336 Returns : array of L<Bio::Map::PositionI> objects
337 Args : arg #1 = L<Bio::Map::MappableI> OR L<Bio::Map::PositionI> to compare
338 this one to (mandatory)
339 arg #2 = optionally, the key => value pairs below
340 -map => Bio::Map::MapI : optionally a Map to only consider
341 positions on the given map
342 -relative => Bio::Map::RelativeI : optionally a Relative to ask if
343 the Positions contains in terms of
344 their relative position to the
345 thing described by that Relative
347 =cut
349 sub contains {
350 my $self = shift;
351 $self->throw_not_implemented();
354 =head2 intersection
356 Title : intersection
357 Usage : my $position = $mappable->intersection($other_mappable);
358 my $position = Bio::Map::Mappable->intersection(\@mappables);
359 Function: Make the position that is at the intersection of all positions of all
360 supplied mappables.
361 Returns : L<Bio::Map::PositionI> object or undef (if not all positions overlap)
362 Args : arg #1 = L<Bio::Map::MappableI> OR L<Bio::Map::PositionI> to compare
363 this one to, or an array ref of such objects (mandatory)
364 arg #2 = optionally, the key => value pairs below
365 -map => Bio::Map::MapI : optionally a Map to only consider
366 positions on the given map
367 -relative => Bio::Map::RelativeI : optionally a Relative to to ask
368 how the Positions intersect in
369 terms of their relative position
370 to the thing described by that
371 Relative
373 =cut
375 sub intersection {
376 my $self = shift;
377 $self->throw_not_implemented();
380 =head2 union
382 Title : union
383 Usage : my $position = $mappable->union($other_mappable);
384 my $position = Bio::Map::Mappable->union(@mappables);
385 Function: Make the minimal position that contains all of the positions of all
386 supplied mappables.
387 Returns : L<Bio::Map::PositionI> object or undef (if not all positions overlap)
388 Args : arg #1 = L<Bio::Map::MappableI> OR L<Bio::Map::PositionI> to compare
389 this one to, or an array ref of such objects (mandatory)
390 arg #2 = optionally, the key => value pairs below
391 -map => Bio::Map::MapI : optionally a Map to only consider
392 positions on the given map
393 -relative => Bio::Map::RelativeI : optionally a Relative to to ask
394 if the union of the Positions in
395 terms of their relative position
396 to the thing described by that
397 Relative
399 =cut
401 sub union {
402 my $self = shift;
403 $self->throw_not_implemented();