t/AlignIO/AlignIO.t: fix number of tests in plan (fixup c523e6bed866)
[bioperl-live.git] / Bio / LiveSeq / AARange.pm
blobfafe69a64905e109411046ad67c5c22848e78670
2 # bioperl module for Bio::LiveSeq::AARange
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
6 # Cared for by Joseph Insana <insana@ebi.ac.uk> <jinsana@gmx.net>
8 # Copyright Joseph Insana
10 # You may distribute this module under the same terms as perl itself
12 # POD documentation - main docs before the code
14 =head1 NAME
16 Bio::LiveSeq::AARange - AARange abstract class for LiveSeq
18 =head1 SYNOPSIS
20 #documentation needed
22 =head1 DESCRIPTION
24 This is used as possible parent for aminoacid range object classes.
25 Or it can be used straight away to define aminoacid ranges. The idea
26 is that the ranges defined are attached to a Translation object and
27 they refer to its coordinate-system when they are first created (via
28 the new() method). When they are created they are anyway linked to
29 the underlying DNA LiveSeq by way of the LiveSeq labels. This allows
30 to preserve the ranges even if the numbering changes in the
31 Translation due to deletions or insertions.
33 The protein sequence associated with the AARange can be accessed via
34 the usual seq() or subseq() methods.
36 The start and end of the AARange in protein coordinate system can be
37 fetched with aa_start() and aa_end() methods. Note: the behaviour of
38 these methods would be influenced by the coordinate_start set in the
39 corresponding Translation object. This can be desirable but can also
40 lead to confusion if the coordinate_start had been changed and the
41 original position of the AARange was to be retrieved.
43 start() and end() methods of the AARange will point to the labels
44 identifying the first nucleotide of the first and last triplet coding
45 for the start and end of the AminoAcidRange.
47 The underlying nucleotide sequence of the AARange can be retrieved
48 with the labelsubseq() method. This would retrieve the whole DNA
49 sequence, including possible introns. This is called "DNA_sequence".
51 To fetch the nucleotide sequence of the Transcript, without introns,
52 the labelsubseq() of the attached Transcript (the Transcript the
53 Translation comes from) has to be accessed. This is called
54 "cDNA_sequence".
56 Here are the operations to retrieve these latter two kinds of
57 sequences:
59 $startlabel=$AARange->start;
60 $endtripletlabel=$AARange->end;
61 $endlabel=$AARange->{'seq'}->label(3,$endtripletlabel,$AARange->strand);
63 $dnaseq=$AARange->labelsubseq($startlabel,undef,$endlabel));
65 $cdnaseq=$AARange->get_Transcript->labelsubseq($startlabel,undef,$endlabel);
67 To simplify, these operations have been included in two additional
68 methods: dna_seq() and cdna_seq().
70 These would return the whole sequence, as in the examples above. But
71 the above general scheme can be used by specifying different labels,
72 to retrieve hypothetical subsequences of interest.
74 =head1 AUTHOR - Joseph A.L. Insana
76 Email: Insana@ebi.ac.uk, jinsana@gmx.net
78 =head1 APPENDIX
80 The rest of the documentation details each of the object
81 methods. Internal methods are usually preceded with a _
83 =cut
85 # Let the code begin...
87 package Bio::LiveSeq::AARange;
89 use strict;
90 use base qw(Bio::LiveSeq::SeqI);
92 =head2 new
94 Title : new
95 Usage : $aarange = Bio::LiveSeq::AARange->new(-translation => $obj_ref,
96 -start => $beginaa,
97 -end => $endaa,
98 -name => "ABCD",
99 -description => "DCBA",
100 -translength => $length);
102 Function: generates a new AminoAcidRange LiveSeq object
103 Returns : reference to a new object of class AARange
104 Errorcode -1
105 Args : two positions in AminoAcid coordinate numbering
106 an object reference specifying to which translation the aminoacid
107 ranges refer to
108 a name and a description (optional)
109 an optional "translength" argument: this can be given when
110 a lot of AARanges are to be created at the same time for the same
111 Translation object, calculating it with $translation->length
112 This would increase the speed, avoiding the new() function to
113 calculate everytime the same length again and again for every obj.
115 =cut
117 sub new {
118 my ($thing, %args) = @_;
119 my $class = ref($thing) || $thing;
120 my ($obj,%range);
122 $obj = \%range;
123 $obj = bless $obj, $class;
124 my $self=$obj;
126 my ($translation,$start,$end,$name,$description,$translength)=($args{-translation},$args{-start},$args{-end},$args{-name},$args{-description},$args{-translength});
128 unless (($translation)&&(ref($translation) eq "Bio::LiveSeq::Translation")) {
129 $self->warn("No -translation or wrong type given");
130 return (-1);
132 unless ($translength) { # if it's not given, fetch it
133 $translength=$translation->length;
135 my $seq=$translation->{'seq'};
137 if (($start < 1)&&($start > $translength)) {
138 $self->warn("$class not initialised because start aminoacid position not valid");
139 return (-1);
141 if (($end < 1)&&($end > $translength)) {
142 $self->warn("$class not initialised because end aminoacid position not valid");
143 return (-1);
145 if ($start > $end) {
146 $self->warn("$class not initialised because start position > end position!");
147 return (-1);
150 my ($starttripletlabel,$endtripletlabel);
151 if ($start == $end) { # trick to increase speed
152 $starttripletlabel=$endtripletlabel=$translation->label($start);
153 } else {
154 ($starttripletlabel,$endtripletlabel)=($translation->label($start),$translation->label($end));
156 unless (($starttripletlabel > 0)&&($endtripletlabel > 0)) {
157 $self->warn("$class not initialised because of problems in retrieving start or end label!");
158 return (-1);
161 # unsure if needed:
162 #my $endlabel=$seq->label(3,$endtripletlabel); # to get the real end
163 #unless ($endlabel > 0) {
164 #carp "$class not initialised because of problems retrieving the last nucleotide of the triplet coding for the end aminoacid";
165 #return (-1);
167 $self->{'seq'}=$seq;
168 $self->{'start'}=$starttripletlabel;
169 $self->{'end'}=$endtripletlabel;
170 $self->{'strand'}=$translation->strand;
171 $self->{'translation'}=$translation;
172 $self->{'name'}=$name;
173 $self->{'description'}=$description;
174 $self->{'alphabet'}="protein";
176 return $obj;
179 sub coordinate_start {
180 my $self=shift;
181 $self->warn("Cannot perform this operation in an AminoAcidRange object!");
182 return (-1);
185 sub all_labels {
186 my $self=shift;
187 $self->warn("Cannot perform this operation in an AminoAcidRange object!");
188 return (-1);
191 sub valid {
192 my $self=shift;
193 $self->warn("Cannot perform this operation in an AminoAcidRange object!");
194 return (-1);
197 =head2 get_Transcript
199 Title : valid
200 Usage : $transcript = $obj->get_Transcript()
201 Function: retrieves the reference to the object of class Transcript (if any)
202 attached to a LiveSeq object
203 Returns : object reference
204 Args : none
206 =cut
208 sub get_Transcript {
209 my $self=shift;
210 return ($self->get_Translation->get_Transcript);
213 =head2 get_Translation
215 Title : valid
216 Usage : $translation = $obj->get_Translation()
217 Function: retrieves the reference to the object of class Translation (if any)
218 attached to a LiveSeq object
219 Returns : object reference
220 Args : none
222 =cut
224 sub get_Translation {
225 my $self=shift;
226 return ($self->{'translation'});
229 sub change {
230 my $self=shift;
231 $self->warn("Cannot change an AminoAcidRange object!");
232 return (-1);
234 sub positionchange {
235 my $self=shift;
236 $self->warn("Cannot change an AminoAcidRange object!");
237 return (-1);
239 sub labelchange {
240 my $self=shift;
241 $self->warn("Cannot change an AminoAcidRange object!");
242 return (-1);
245 sub subseq {
246 my ($self,$pos1,$pos2,$length) = @_;
247 if (defined ($length)) {
248 if ($length < 1) {
249 $self->warn("No sense asking for a subseq of length < 1");
250 return (-1);
253 unless (defined ($pos1)) {
254 $pos1=1;
255 } elsif ($pos1 < 1) { # if position out of boundaries
256 $self->warn("Starting position for AARange cannot be < 1!"); return (-1);
257 if ((defined ($pos2))&&($pos1>$pos2)) {
258 $self->warn("1st position($pos1) cannot be > 2nd position($pos2)!"); return (-1);
261 my $seq=$self->seq;
262 my $objlength=length($seq);
263 unless (defined ($length)) {
264 $length=$objlength-$pos1+1;
266 if (defined ($pos2)) {
267 if ($pos2 > $objlength) { # if position out of boundaries
268 $self->warn("Ending position for AARange cannot be > length of AARange!"); return (-1);
270 $length=$pos2-$pos1+1;
271 if ((defined ($pos1))&&($pos1>$pos2)) {
272 $self->warn("1st position($pos1) cannot be > 2nd position($pos2)!"); return (-1);
275 my $str=substr($seq,$pos1-1,$length);
276 if (length($str) < $length) {
277 $self->warn("Attention, cannot return the length requested for subseq",1);
279 return $str;
282 sub seq {
283 my $self=shift;
284 my ($aa_start,$aa_end)=($self->aa_start,$self->aa_end);
285 unless (($aa_start)&&($aa_end)) { # they must both exist
286 $self->warn("Not able to find start or end of the AminoAcid Range");
287 return (0);
289 my $translseq=$self->get_Translation->seq;
290 return substr($translseq,$aa_start-1,$aa_end-$aa_start+1);
291 # Note: it will return "undef" if the translation stops before the start
292 # of the aarange (because of upstream nonsense mutation creating STOP).
293 # For the same reason it would return uncomplete (up to the STOP) string
294 # if the stop happens in between aarange's start and stop
297 sub length {
298 my $self=shift;
299 my $seq=$self->seq;
300 my $length=length($seq);
301 return $length;
304 sub label {
305 my ($self,$position)=@_;
306 my $translation=$self->get_Translation;
307 my $origstart=$translation->coordinate_start; # preserve it
308 $translation->coordinate_start($self->start); # change it
309 my $label=$translation->label($position);
310 $translation->coordinate_start($origstart); # restore it
311 return ($label);
314 sub position {
315 my ($self,$label)=@_;
316 my $translation=$self->get_Translation;
317 my $origstart=$translation->coordinate_start; # preserve it
318 $translation->coordinate_start($self->start); # change it
319 my $position=$translation->position($label);
320 $translation->coordinate_start($origstart); # restore it
321 return ($position);
324 =head2 aa_start
326 Title : aa_start
327 Usage : $end = $aarange->aa_start()
328 Returns : integer (position, according to Translation coordinate system) of
329 the start of an AminoAcidRange object
330 Args : none
332 =cut
334 sub aa_start {
335 my $self=shift;
336 my $aastart=$self->get_Translation->position($self->{'start'});
339 =head2 aa_end
341 Title : aa_end
342 Usage : $end = $aarange->aa_end()
343 Returns : integer (position, according to Translation coordinate system) of
344 the end of an AminoAcidRange object
345 Args : none
347 =cut
349 sub aa_end {
350 my $self=shift;
351 my $aastart=$self->get_Translation->position($self->{'end'});
354 =head2 dna_seq
356 Title : dna_seq
357 Usage : $end = $aarange->dna_seq()
358 Returns : the sequence at DNA level of the entire AminoAcidRange
359 this would include introns (if present)
360 Args : none
362 =cut
364 sub dna_seq {
365 my $self=shift;
366 my $startlabel=$self->start;
367 my $endtripletlabel=$self->end;
368 my $endlabel=$self->{'seq'}->label(3,$endtripletlabel,$self->strand);
369 return ($self->labelsubseq($startlabel,undef,$endlabel));
372 =head2 cdna_seq
374 Title : cdna_seq
375 Usage : $end = $aarange->cdna_seq()
376 Returns : the sequence at cDNA level of the entire AminoAcidRange
377 i.e. this is the part of the Transcript that codes for the
378 AminoAcidRange. It would be composed just of exonic DNA.
379 Args : none
381 =cut
383 sub cdna_seq {
384 my $self=shift;
385 my $startlabel=$self->start;
386 my $endtripletlabel=$self->end;
387 my $endlabel=$self->{'seq'}->label(3,$endtripletlabel,$self->strand);
388 return ($self->get_Transcript->labelsubseq($startlabel,undef,$endlabel));
391 # this checks if the attached Transcript has a Gene object attached
392 sub gene {
393 my ($self,$value) = @_;
394 if (defined $value) {
395 $self->{'gene'} = $value;
397 unless (exists $self->{'gene'}) {
398 unless (exists $self->get_Transcript->{'gene'}) {
399 return (0);
400 } else {
401 return ($self->get_Transcript->{'gene'});
403 } else {
404 return $self->{'gene'};