2 # BioPerl module for Bio::Map::Relative
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
6 # Cared for by Sendu Bala <bix@sendu.me.uk>
10 # You may distribute this module under the same terms as perl itself
12 # POD documentation - main docs before the code
16 Bio::Map::Relative - Represents what a Position's coordiantes are relative to.
20 # Get a Bio::Map::PositionI somehow
21 my $pos = Bio::Map::Position->new(-value => 100);
23 # its co-ordinates are implicitly relative to the start of its map
24 my $implicit_relative = $pos->relative;
25 my $type = $implicit_relative->type; # $type eq 'map'
26 my $value = $implicit_relative->$type(); # $value == 0
28 # make its co-ordinates relative to another Position
29 my $pos_we_are_relative_to = Bio::Map::Position->new(-value => 200);
30 my $relative = Bio::Map::Relative->new(-position => $pos_we_are_relative_to);
31 $pos->relative($relative);
33 # Get the start co-ordinate of $pos relative to $pos_we_are_relative_to
34 my $start = $pos->start; # $start == 100
36 # Get the start co-ordinate of $pos relative to the start of the map
37 my $abs_start = $relative->absolute_conversion($pos); # $abs_start == 300
40 my $abs_start = $pos->start; # $abs_start == 300
43 # Get the start co-ordinate of $pos relative to a third Position
44 my $pos_frame_of_reference = Bio::Map::Position->new(-value => 10);
45 my $relative2 = Bio::Map::Relative->new(-position => $pos_frame_of_reference);
46 my $start = $pos->start($relative2); # $start == 290
50 A Relative object is used to describe what the co-ordinates (numerical(),
51 start(), end()) of a Position are relative to. By default they are
52 implicitly assumed to be relative to the start of the map the Position is on.
53 But setting the relative() of a Position to one of these objects lets us
60 User feedback is an integral part of the evolution of this and other
61 Bioperl modules. Send your comments and suggestions preferably to
62 the Bioperl mailing list. Your participation is much appreciated.
64 bioperl-l@bioperl.org - General discussion
65 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
69 Please direct usage questions or support issues to the mailing list:
71 I<bioperl-l@bioperl.org>
73 rather than to the module maintainer directly. Many experienced and
74 reponsive experts will be able look at the problem and quickly
75 address it. Please include a thorough description of the problem
76 with code and data examples if at all possible.
80 Report bugs to the Bioperl bug tracking system to help us keep track
81 of the bugs and their resolution. Bug reports can be submitted via the
84 https://github.com/bioperl/bioperl-live/issues
86 =head1 AUTHOR - Sendu Bala
92 The rest of the documentation details each of the object methods.
93 Internal methods are usually preceded with a _
97 # Let the code begin...
99 package Bio
::Map
::Relative
;
101 use Scalar
::Util
qw(looks_like_number);
103 use base
qw(Bio::Root::Root Bio::Map::RelativeI);
108 Usage : my $relative = Bio::Map::Relative->new();
109 Function: Build a new Bio::Map::Relative object.
110 Returns : Bio::Map::Relative object
111 Args : -map => int : coordinates are relative to this point on the
112 Position's map [default is map => 0, ie.
113 relative to the start of the map],
114 -element => Mappable : or relative to this element's (a
115 Bio::Map::MappableI) position in the map
116 (only works if the given element has only one
117 position in the map the Position belongs to),
118 -position => Position : or relative to this other Position (a
119 Bio::Map::PositionI, fails if the other
120 Position is on a different map to this map)
122 -description => string: Free text description of what this relative
125 (To say a Position is relative to something and upstream of it,
126 the Position's start() co-ordinate should be set negative)
131 my ($class, @args) = @_;
132 my $self = $class->SUPER::new
(@args);
134 my ($map, $element, $position, $desc) =
135 $self->_rearrange([qw( MAP ELEMENT POSITION DESCRIPTION )], @args);
137 if (defined($map) + defined($element) + defined($position) > 1) {
138 $self->throw("-map, -element and -position are mutually exclusive");
141 defined($map) && $self->map($map);
142 $element && $self->element($element);
143 $position && $self->position($position);
144 $desc && $self->description($desc);
149 =head2 absolute_conversion
151 Title : absolute_conversion
152 Usage : my $absolute_coord = $relative->absolute_conversion($pos);
153 Function: Convert the start co-ordinate of the supplied position into a number
154 relative to the start of its map.
155 Returns : scalar number
156 Args : Bio::Map::PositionI object
160 sub absolute_conversion
{
161 my ($self, $pos) = @_;
162 $self->throw("Must supply an object") unless ref($pos);
163 $self->throw("This is [$pos], not a Bio::Map::PositionI") unless $pos->isa('Bio::Map::PositionI');
165 # get the raw start position of our position
166 my $prior_abs = $pos->absolute;
167 $pos->absolute(0) if $prior_abs;
168 my $raw = $pos->start;
169 $pos->absolute($prior_abs) if $prior_abs;
170 $self->throw("Can't convert co-ordinates when start isn't set") unless defined($raw); #*** needed? return undef?
172 # what are we relative to?
173 my $type = $self->type;
174 my $value = $self->$type;
175 $self->throw("Details not yet set for this Relative, cannot convert") unless $type && defined($value);
177 # get the absolute start of the thing we're relative to
179 if ($type eq 'element') {
180 $self->throw("Relative to a Mappable, but the Position has no map") unless $map;
181 my @positions = $value->get_positions($map);
182 $value = shift(@positions);
183 $self->throw("Relative to a Mappable, but this Mappable has no positions on the supplied Position's map") unless $value;
187 my $rel = $value->relative;
188 $value = $rel->absolute_conversion($value);
191 if (defined($value)) {
192 return $value + $raw;
200 Usage : my $type = $relative->type();
201 Function: Get the type of thing we are relative to. The types correspond
202 to a method name, so the value of what we are relative to can
203 subsequently be found by $value = $relative->$type;
205 Note that type is set by the last method that was set, or during
208 Returns : the string 'map', 'element' or 'position', or undef
215 return $self->{_use
} || return;
221 Usage : my $int = $relative->map();
222 $relative->map($int);
223 Function: Get/set the distance from the start of the map that the Position's
224 co-ordiantes are relative to.
226 Args : none to get, OR
227 int to set; a value of 0 means relative to the start of the map.
232 my ($self, $num) = @_;
234 $self->throw("This is [$num], not a number") unless looks_like_number
($num);
235 $self->{_use
} = 'map';
236 $self->{_map
} = $num;
238 return defined($self->{_map
}) ?
$self->{_map
} : return;
244 Usage : my $element = $relative->element();
245 $relative->element($element);
246 Function: Get/set the map element (Mappable) the Position is relative to. If
247 the Mappable has more than one Position on the Position's map, we
248 will be relative to the Mappable's first Position on the map.
249 Returns : Bio::Map::MappableI
250 Args : none to get, OR
251 Bio::Map::MappableI to set
256 my ($self, $element) = @_;
258 $self->throw("Must supply an object") unless ref($element);
259 $self->throw("This is [$element], not a Bio::Map::MappableI") unless $element->isa('Bio::Map::MappableI');
260 $self->{_use
} = 'element';
261 $self->{_element
} = $element;
263 return $self->{_element
} || return;
269 Usage : my $position = $relative->position();
270 $relative->position($position);
271 Function: Get/set the Position your Position is relative to. Your Position
272 will be made relative to the start of this supplied Position. It
273 makes no difference what maps the Positions are on.
274 Returns : Bio::Map::PositionI
275 Args : none to get, OR
276 Bio::Map::PositionI to set
281 my ($self, $pos) = @_;
283 $self->throw("Must supply an object") unless ref($pos);
284 $self->throw("This is [$pos], not a Bio::Map::PositionI") unless $pos->isa('Bio::Map::PositionI');
285 $self->{_use
} = 'position';
286 $self->{_position
} = $pos;
288 return $self->{_position
} || return;
294 Usage : my $description = $relative->description();
295 $relative->description($description);
296 Function: Get/set a textual description of what this relative describes.
298 Args : none to get, OR
305 if (@_) { $self->{desc
} = shift }
306 return $self->{desc
} || '';