* sync with trunk
[bioperl-live.git] / Bio / LiveSeq / AARange.pm
blob3f12091d7f87c02ebaad027bd47768104ea9b490
1 # $Id$
3 # bioperl module for Bio::LiveSeq::AARange
5 # Cared for by Joseph Insana <insana@ebi.ac.uk> <jinsana@gmx.net>
7 # Copyright Joseph Insana
9 # You may distribute this module under the same terms as perl itself
11 # POD documentation - main docs before the code
13 =head1 NAME
15 Bio::LiveSeq::AARange - AARange abstract class for LiveSeq
17 =head1 SYNOPSIS
19 #documentation needed
21 =head1 DESCRIPTION
23 This is used as possible parent for aminoacid range object classes.
24 Or it can be used straight away to define aminoacid ranges. The idea
25 is that the ranges defined are attached to a Translation object and
26 they refer to its coordinate-system when they are first created (via
27 the new() method). When they are created they are anyway linked to
28 the underlying DNA LiveSeq by way of the LiveSeq labels. This allows
29 to preserve the ranges even if the numbering changes in the
30 Translation due to deletions or insertions.
32 The protein sequence associated with the AARange can be accessed via
33 the usual seq() or subseq() methods.
35 The start and end of the AARange in protein coordinate system can be
36 fetched with aa_start() and aa_end() methods. Note: the behaviour of
37 these methods would be influenced by the coordinate_start set in the
38 corresponding Translation object. This can be desirable but can also
39 lead to confusion if the coordinate_start had been changed and the
40 original position of the AARange was to be retrieved.
42 start() and end() methods of the AARange will point to the labels
43 identifying the first nucleotide of the first and last triplet coding
44 for the start and end of the AminoAcidRange.
46 The underlying nucleotide sequence of the AARange can be retrieved
47 with the labelsubseq() method. This would retrieve the whole DNA
48 sequence, including possible introns. This is called "DNA_sequence".
50 To fetch the nucleotide sequence of the Transcript, without introns,
51 the labelsubseq() of the attached Transcript (the Transcript the
52 Translation comes from) has to be accessed. This is called
53 "cDNA_sequence".
55 Here are the operations to retrieve these latter two kinds of
56 sequences:
58 $startlabel=$AARange->start;
59 $endtripletlabel=$AARange->end;
60 $endlabel=$AARange->{'seq'}->label(3,$endtripletlabel,$AARange->strand);
62 $dnaseq=$AARange->labelsubseq($startlabel,undef,$endlabel));
64 $cdnaseq=$AARange->get_Transcript->labelsubseq($startlabel,undef,$endlabel);
66 To simplify, these operations have been included in two additional
67 methods: dna_seq() and cdna_seq().
69 These would return the whole sequence, as in the examples above. But
70 the above general scheme can be used by specifying different labels,
71 to retrieve hypothetical subsequences of interest.
73 =head1 AUTHOR - Joseph A.L. Insana
75 Email: Insana@ebi.ac.uk, jinsana@gmx.net
77 =head1 APPENDIX
79 The rest of the documentation details each of the object
80 methods. Internal methods are usually preceded with a _
82 =cut
84 # Let the code begin...
86 package Bio::LiveSeq::AARange;
88 use strict;
89 use base qw(Bio::LiveSeq::SeqI);
91 =head2 new
93 Title : new
94 Usage : $aarange = Bio::LiveSeq::AARange->new(-translation => $obj_ref,
95 -start => $beginaa,
96 -end => $endaa,
97 -name => "ABCD",
98 -description => "DCBA",
99 -translength => $length);
101 Function: generates a new AminoAcidRange LiveSeq object
102 Returns : reference to a new object of class AARange
103 Errorcode -1
104 Args : two positions in AminoAcid coordinate numbering
105 an object reference specifying to which translation the aminoacid
106 ranges refer to
107 a name and a description (optional)
108 an optional "translength" argument: this can be given when
109 a lot of AARanges are to be created at the same time for the same
110 Translation object, calculating it with $translation->length
111 This would increase the speed, avoiding the new() function to
112 calculate everytime the same length again and again for every obj.
114 =cut
116 sub new {
117 my ($thing, %args) = @_;
118 my $class = ref($thing) || $thing;
119 my ($obj,%range);
121 $obj = \%range;
122 $obj = bless $obj, $class;
123 my $self=$obj;
125 my ($translation,$start,$end,$name,$description,$translength)=($args{-translation},$args{-start},$args{-end},$args{-name},$args{-description},$args{-translength});
127 unless (($translation)&&(ref($translation) eq "Bio::LiveSeq::Translation")) {
128 $self->warn("No -translation or wrong type given");
129 return (-1);
131 unless ($translength) { # if it's not given, fetch it
132 $translength=$translation->length;
134 my $seq=$translation->{'seq'};
136 if (($start < 1)&&($start > $translength)) {
137 $self->warn("$class not initialised because start aminoacid position not valid");
138 return (-1);
140 if (($end < 1)&&($end > $translength)) {
141 $self->warn("$class not initialised because end aminoacid position not valid");
142 return (-1);
144 if ($start > $end) {
145 $self->warn("$class not initialised because start position > end position!");
146 return (-1);
149 my ($starttripletlabel,$endtripletlabel);
150 if ($start == $end) { # trick to increase speed
151 $starttripletlabel=$endtripletlabel=$translation->label($start);
152 } else {
153 ($starttripletlabel,$endtripletlabel)=($translation->label($start),$translation->label($end));
155 unless (($starttripletlabel > 0)&&($endtripletlabel > 0)) {
156 $self->warn("$class not initialised because of problems in retrieving start or end label!");
157 return (-1);
160 # unsure if needed:
161 #my $endlabel=$seq->label(3,$endtripletlabel); # to get the real end
162 #unless ($endlabel > 0) {
163 #carp "$class not initialised because of problems retrieving the last nucleotide of the triplet coding for the end aminoacid";
164 #return (-1);
166 $self->{'seq'}=$seq;
167 $self->{'start'}=$starttripletlabel;
168 $self->{'end'}=$endtripletlabel;
169 $self->{'strand'}=$translation->strand;
170 $self->{'translation'}=$translation;
171 $self->{'name'}=$name;
172 $self->{'description'}=$description;
173 $self->{'alphabet'}="protein";
175 return $obj;
178 sub coordinate_start {
179 my $self=shift;
180 $self->warn("Cannot perform this operation in an AminoAcidRange object!");
181 return (-1);
184 sub all_labels {
185 my $self=shift;
186 $self->warn("Cannot perform this operation in an AminoAcidRange object!");
187 return (-1);
190 sub valid {
191 my $self=shift;
192 $self->warn("Cannot perform this operation in an AminoAcidRange object!");
193 return (-1);
196 =head2 get_Transcript
198 Title : valid
199 Usage : $transcript = $obj->get_Transcript()
200 Function: retrieves the reference to the object of class Transcript (if any)
201 attached to a LiveSeq object
202 Returns : object reference
203 Args : none
205 =cut
207 sub get_Transcript {
208 my $self=shift;
209 return ($self->get_Translation->get_Transcript);
212 =head2 get_Translation
214 Title : valid
215 Usage : $translation = $obj->get_Translation()
216 Function: retrieves the reference to the object of class Translation (if any)
217 attached to a LiveSeq object
218 Returns : object reference
219 Args : none
221 =cut
223 sub get_Translation {
224 my $self=shift;
225 return ($self->{'translation'});
228 sub change {
229 my $self=shift;
230 $self->warn("Cannot change an AminoAcidRange object!");
231 return (-1);
233 sub positionchange {
234 my $self=shift;
235 $self->warn("Cannot change an AminoAcidRange object!");
236 return (-1);
238 sub labelchange {
239 my $self=shift;
240 $self->warn("Cannot change an AminoAcidRange object!");
241 return (-1);
244 sub subseq {
245 my ($self,$pos1,$pos2,$length) = @_;
246 if (defined ($length)) {
247 if ($length < 1) {
248 $self->warn("No sense asking for a subseq of length < 1");
249 return (-1);
252 unless (defined ($pos1)) {
253 $pos1=1;
254 } elsif ($pos1 < 1) { # if position out of boundaries
255 $self->warn("Starting position for AARange cannot be < 1!"); return (-1);
256 if ((defined ($pos2))&&($pos1>$pos2)) {
257 $self->warn("1st position($pos1) cannot be > 2nd position($pos2)!"); return (-1);
260 my $seq=$self->seq;
261 my $objlength=length($seq);
262 unless (defined ($length)) {
263 $length=$objlength-$pos1+1;
265 if (defined ($pos2)) {
266 if ($pos2 > $objlength) { # if position out of boundaries
267 $self->warn("Ending position for AARange cannot be > length of AARange!"); return (-1);
269 $length=$pos2-$pos1+1;
270 if ((defined ($pos1))&&($pos1>$pos2)) {
271 $self->warn("1st position($pos1) cannot be > 2nd position($pos2)!"); return (-1);
274 my $str=substr($seq,$pos1-1,$length);
275 if (length($str) < $length) {
276 $self->warn("Attention, cannot return the length requested for subseq",1);
278 return $str;
281 sub seq {
282 my $self=shift;
283 my ($aa_start,$aa_end)=($self->aa_start,$self->aa_end);
284 unless (($aa_start)&&($aa_end)) { # they must both exist
285 $self->warn("Not able to find start or end of the AminoAcid Range");
286 return (0);
288 my $translseq=$self->get_Translation->seq;
289 return substr($translseq,$aa_start-1,$aa_end-$aa_start+1);
290 # Note: it will return "undef" if the translation stops before the start
291 # of the aarange (because of upstream nonsense mutation creating STOP).
292 # For the same reason it would return uncomplete (up to the STOP) string
293 # if the stop happens in between aarange's start and stop
296 sub length {
297 my $self=shift;
298 my $seq=$self->seq;
299 my $length=length($seq);
300 return $length;
303 sub label {
304 my ($self,$position)=@_;
305 my $translation=$self->get_Translation;
306 my $origstart=$translation->coordinate_start; # preserve it
307 $translation->coordinate_start($self->start); # change it
308 my $label=$translation->label($position);
309 $translation->coordinate_start($origstart); # restore it
310 return ($label);
313 sub position {
314 my ($self,$label)=@_;
315 my $translation=$self->get_Translation;
316 my $origstart=$translation->coordinate_start; # preserve it
317 $translation->coordinate_start($self->start); # change it
318 my $position=$translation->position($label);
319 $translation->coordinate_start($origstart); # restore it
320 return ($position);
323 =head2 aa_start
325 Title : aa_start
326 Usage : $end = $aarange->aa_start()
327 Returns : integer (position, according to Translation coordinate system) of
328 the start of an AminoAcidRange object
329 Args : none
331 =cut
333 sub aa_start {
334 my $self=shift;
335 my $aastart=$self->get_Translation->position($self->{'start'});
338 =head2 aa_end
340 Title : aa_end
341 Usage : $end = $aarange->aa_end()
342 Returns : integer (position, according to Translation coordinate system) of
343 the end of an AminoAcidRange object
344 Args : none
346 =cut
348 sub aa_end {
349 my $self=shift;
350 my $aastart=$self->get_Translation->position($self->{'end'});
353 =head2 dna_seq
355 Title : dna_seq
356 Usage : $end = $aarange->dna_seq()
357 Returns : the sequence at DNA level of the entire AminoAcidRange
358 this would include introns (if present)
359 Args : none
361 =cut
363 sub dna_seq {
364 my $self=shift;
365 my $startlabel=$self->start;
366 my $endtripletlabel=$self->end;
367 my $endlabel=$self->{'seq'}->label(3,$endtripletlabel,$self->strand);
368 return ($self->labelsubseq($startlabel,undef,$endlabel));
371 =head2 cdna_seq
373 Title : cdna_seq
374 Usage : $end = $aarange->cdna_seq()
375 Returns : the sequence at cDNA level of the entire AminoAcidRange
376 i.e. this is the part of the Transcript that codes for the
377 AminoAcidRange. It would be composed just of exonic DNA.
378 Args : none
380 =cut
382 sub cdna_seq {
383 my $self=shift;
384 my $startlabel=$self->start;
385 my $endtripletlabel=$self->end;
386 my $endlabel=$self->{'seq'}->label(3,$endtripletlabel,$self->strand);
387 return ($self->get_Transcript->labelsubseq($startlabel,undef,$endlabel));
390 # this checks if the attached Transcript has a Gene object attached
391 sub gene {
392 my ($self,$value) = @_;
393 if (defined $value) {
394 $self->{'gene'} = $value;
396 unless (exists $self->{'gene'}) {
397 unless (exists $self->get_Transcript->{'gene'}) {
398 return (0);
399 } else {
400 return ($self->get_Transcript->{'gene'});
402 } else {
403 return $self->{'gene'};