0
|
1 # $Id: Range.pm,v 1.17 2002/10/22 07:38:24 lapp Exp $
|
|
2 #
|
|
3 # BioPerl module for Bio::Range
|
|
4 #
|
|
5 # Cared for by Heikki Lehvaslaiho <heikki@ebi.ac.uk>
|
|
6 #
|
|
7 # Copywright Matthew Pocock
|
|
8 #
|
|
9 # You may distribute this module under the same terms as perl itself
|
|
10 #
|
|
11 # POD documentation - main docs before the code
|
|
12 #
|
|
13
|
|
14 =head1 NAME
|
|
15
|
|
16 Bio::Range - Pure perl RangeI implementation
|
|
17
|
|
18 =head1 SYNOPSIS
|
|
19
|
|
20 $range = new Bio::Range(-start=>10, -end=>30, -strand=>+1);
|
|
21 $r2 = new Bio::Range(-start=>15, -end=>200, -strand=>+1);
|
|
22
|
|
23 print join(', ', $range->union($r2), "\n";
|
|
24 print join(', ', $range->intersection($r2), "\n";
|
|
25
|
|
26 print $range->overlaps($r2), "\n";
|
|
27 print $range->contains($r2), "\n";
|
|
28
|
|
29 =head1 DESCRIPTION
|
|
30
|
|
31 This provides a pure perl implementation of the BioPerl range
|
|
32 interface.
|
|
33
|
|
34 Ranges are modeled as having (start, end, length, strand). They use
|
|
35 Bio-coordinates - all points E<gt>= start and E<lt>= end are within the
|
|
36 range. End is always greater-than or equal-to start, and length is
|
|
37 greather than or equal to 1. The behaviour of a range is undefined if
|
|
38 ranges with negative numbers or zero are used.
|
|
39
|
|
40 So, in summary:
|
|
41
|
|
42 length = end - start + 1
|
|
43 end >= start
|
|
44 strand = (-1 | 0 | +1)
|
|
45
|
|
46 =head1 FEEDBACK
|
|
47
|
|
48 =head2 Mailing Lists
|
|
49
|
|
50 User feedback is an integral part of the evolution of this and other
|
|
51 Bioperl modules. Send your comments and suggestions preferably to one
|
|
52 of the Bioperl mailing lists. Your participation is much appreciated.
|
|
53
|
|
54 bioperl-l@bioperl.org - General discussion
|
|
55 http://bio.perl.org/MailList.html - About the mailing lists
|
|
56
|
|
57 =head2 Reporting Bugs
|
|
58
|
|
59 Report bugs to the Bioperl bug tracking system to help us keep track
|
|
60 the bugs and their resolution. Bug reports can be submitted via email
|
|
61 or the web:
|
|
62
|
|
63 bioperl-bugs@bio.perl.org
|
|
64 http://bugzilla.bioperl.org/
|
|
65
|
|
66 =head1 AUTHOR - Heikki Lehvaslaiho
|
|
67
|
|
68 Email heikki@ebi.ac.uk
|
|
69
|
|
70 =head1 APPENDIX
|
|
71
|
|
72 The rest of the documentation details each of the object
|
|
73 methods. Internal methods are usually preceded with a _
|
|
74
|
|
75 =cut
|
|
76
|
|
77 package Bio::Range;
|
|
78
|
|
79 use strict;
|
|
80 use Carp;
|
|
81 use integer;
|
|
82 use Bio::RangeI;
|
|
83 use Bio::Root::Root;
|
|
84
|
|
85 use vars qw(@ISA);
|
|
86
|
|
87 @ISA = qw(Bio::Root::Root Bio::RangeI);
|
|
88
|
|
89 =head1 Constructors
|
|
90
|
|
91 =head2 new
|
|
92
|
|
93 Title : new
|
|
94 Usage : $range = Bio::Range->new(-start => 100, -end=> 200, -strand = +1);
|
|
95 Function: generates a new Bio::Range
|
|
96 Returns : a new range
|
|
97 Args : two of (-start, -end, '-length') - the third is calculated
|
|
98 : -strand (defaults to 0)
|
|
99
|
|
100 =cut
|
|
101
|
|
102 sub new {
|
|
103 my ($caller, @args) = @_;
|
|
104 my $self = $caller->SUPER::new(@args);
|
|
105 my ($strand, $start, $end, $length) =
|
|
106 $self->_rearrange([qw(STRAND
|
|
107 START
|
|
108 END
|
|
109 LENGTH
|
|
110 )],@args);
|
|
111 $self->strand($strand || 0);
|
|
112
|
|
113 if(defined $start ) {
|
|
114 $self->start($start);
|
|
115 if(defined $end) {
|
|
116 $self->end($end);
|
|
117 } elsif(defined $length) {
|
|
118 $self->end($self->start()+ $length - 1);
|
|
119 }
|
|
120 } elsif(defined $end && defined $length ) {
|
|
121 $self->end($end);
|
|
122 $self->start($self->end() - $length + 1);
|
|
123 }
|
|
124 return $self;
|
|
125 }
|
|
126
|
|
127 =head1 Member variable access
|
|
128
|
|
129 These methods let you get at and set the member variables
|
|
130
|
|
131 =head2 start
|
|
132
|
|
133 Title : start
|
|
134 Function : return or set the start co-ordinate
|
|
135 Example : $s = $range->start(); $range->start(7);
|
|
136 Returns : the value of the start co-ordinate
|
|
137 Args : optionally, the new start co-ordinate
|
|
138 Overrides: Bio::RangeI::start
|
|
139
|
|
140 =cut
|
|
141
|
|
142 sub start {
|
|
143 my ($self,$value) = @_;
|
|
144 if( defined $value) {
|
|
145 $self->throw("'$value' is not an integer.\n")
|
|
146 unless $value =~ /^[-+]?\d+$/;
|
|
147 $self->{'start'} = $value;
|
|
148 }
|
|
149 return $self->{'start'};
|
|
150 }
|
|
151
|
|
152 =head2 end
|
|
153
|
|
154 Title : end
|
|
155 Function : return or set the end co-ordinate
|
|
156 Example : $e = $range->end(); $range->end(2000);
|
|
157 Returns : the value of the end co-ordinate
|
|
158 Args : optionally, the new end co-ordinate
|
|
159 Overrides: Bio::RangeI::end
|
|
160
|
|
161 =cut
|
|
162
|
|
163 sub end {
|
|
164
|
|
165 my ($self,$value) = @_;
|
|
166 if( defined $value) {
|
|
167 $self->throw("'$value' is not an integer.\n")
|
|
168 unless $value =~ /^[-+]?\d+$/;
|
|
169 $self->{'end'} = $value;
|
|
170 }
|
|
171 return $self->{'end'};
|
|
172 }
|
|
173
|
|
174 =head2 strand
|
|
175
|
|
176 Title : strand
|
|
177 Function : return or set the strandidness
|
|
178 Example : $st = $range->strand(); $range->strand(-1);
|
|
179 Returns : the value of the strandedness (-1, 0 or 1)
|
|
180 Args : optionaly, the new strand - (-1, 0, 1) or (-, ., +).
|
|
181 Overrides: Bio::RangeI::Strand
|
|
182
|
|
183 =cut
|
|
184
|
|
185 sub strand {
|
|
186 my $self = shift;
|
|
187 if(@_) {
|
|
188 my $val = shift;
|
|
189 $val =~ tr/+/1/;
|
|
190 $val =~ tr/-/-1/;
|
|
191 $val =~ tr/./0/;
|
|
192 if($val == -1 || $val == 0 || $val == 1 ) {
|
|
193 $self->{'strand'} = $val;
|
|
194 }
|
|
195 }
|
|
196 return $self->{'strand'};
|
|
197 }
|
|
198
|
|
199 =head2 length
|
|
200
|
|
201 Title : length
|
|
202 Function : returns the length of this range
|
|
203 Example : $length = $range->length();
|
|
204 Returns : the length of this range, equal to end - start + 1
|
|
205 Args : if you attempt to set the length, and exeption will be thrown
|
|
206 Overrides: Bio::RangeI::Length
|
|
207
|
|
208 =cut
|
|
209
|
|
210 sub length {
|
|
211 my $self = shift;
|
|
212 if(@_) {
|
|
213 confess ref($self), "->length() is read-only";
|
|
214 }
|
|
215 return $self->end() - $self->start() + 1;
|
|
216 }
|
|
217
|
|
218 =head2 toString
|
|
219
|
|
220 Title : toString
|
|
221 Function: stringifies this range
|
|
222 Example : print $range->toString(), "\n";
|
|
223 Returns : a string representation of this range
|
|
224
|
|
225 =cut
|
|
226
|
|
227 sub toString {
|
|
228 my $self = shift;
|
|
229 return "(${\$self->start}, ${\$self->end}) strand=${\$self->strand}";
|
|
230 }
|
|
231
|
|
232 =head1 Boolean Methods
|
|
233
|
|
234 These methods return true or false.
|
|
235
|
|
236 $range->overlaps($otherRange) && print "Ranges overlap\n";
|
|
237
|
|
238 =head2 overlaps
|
|
239
|
|
240 Title : overlaps
|
|
241 Usage : if($r1->overlaps($r2)) { do stuff }
|
|
242 Function : tests if $r2 overlaps $r1
|
|
243 Args : a range to test for overlap with
|
|
244 Returns : true if the ranges overlap, false otherwise
|
|
245 Inherited: Bio::RangeI
|
|
246
|
|
247 =head2 contains
|
|
248
|
|
249 Title : contains
|
|
250 Usage : if($r1->contains($r2) { do stuff }
|
|
251 Function : tests wether $r1 totaly contains $r2
|
|
252 Args : a range to test for being contained
|
|
253 Returns : true if the argument is totaly contained within this range
|
|
254 Inherited: Bio::RangeI
|
|
255
|
|
256 =head2 equals
|
|
257
|
|
258 Title : equals
|
|
259 Usage : if($r1->equals($r2))
|
|
260 Function : test whether $r1 has the same start, end, length as $r2
|
|
261 Args : a range to test for equality
|
|
262 Returns : true if they are describing the same range
|
|
263 Inherited: Bio::RangeI
|
|
264
|
|
265 =head1 Geometrical methods
|
|
266
|
|
267 These methods do things to the geometry of ranges, and return
|
|
268 triplets (start, end, strand) from which new ranges could be built.
|
|
269
|
|
270 =head2 intersection
|
|
271
|
|
272 Title : intersection
|
|
273 Usage : ($start, $stop, $strand) = $r1->intersection($r2)
|
|
274 Function : gives the range that is contained by both ranges
|
|
275 Args : a range to compare this one to
|
|
276 Returns : nothing if they do not overlap, or the range that they do overlap
|
|
277 Inherited: Bio::RangeI::intersection
|
|
278
|
|
279 =cut
|
|
280
|
|
281 =head2 union
|
|
282
|
|
283 Title : union
|
|
284 Usage : ($start, $stop, $strand) = $r1->union($r2);
|
|
285 : ($start, $stop, $strand) = Bio::Range->union(@ranges);
|
|
286 Function : finds the minimal range that contains all of the ranges
|
|
287 Args : a range or list of ranges to find the union of
|
|
288 Returns : the range containing all of the ranges
|
|
289 Inherited: Bio::RangeI::union
|
|
290
|
|
291 =cut
|
|
292
|
|
293 1;
|