Bio::Root::RootI: deprecate deprecated() method (issue #294)
[bioperl-live.git] / lib / Bio / Seq.pm
blobd2938366ff8336a49e6027f12290d6be44fc0b77
2 # BioPerl module for Bio::Seq
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
6 # Cared for by Ewan Birney <birney@ebi.ac.uk>
8 # Copyright Ewan Birney
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::Seq - Sequence object, with features
18 =head1 SYNOPSIS
20 # This is the main sequence object in Bioperl
22 # gets a sequence from a file
23 $seqio = Bio::SeqIO->new( '-format' => 'embl' , -file => 'myfile.dat');
24 $seqobj = $seqio->next_seq();
26 # SeqIO can both read and write sequences; see Bio::SeqIO
27 # for more information and examples
29 # get from database
30 $db = Bio::DB::GenBank->new();
31 $seqobj = $db->get_Seq_by_acc('X78121');
33 # make from strings in script
34 $seqobj = Bio::Seq->new( -display_id => 'my_id',
35 -seq => $sequence_as_string);
37 # gets sequence as a string from sequence object
38 $seqstr = $seqobj->seq(); # actual sequence as a string
39 $seqstr = $seqobj->subseq(10,50); # slice in biological coordinates
41 # retrieves information from the sequence
42 # features must implement Bio::SeqFeatureI interface
44 @features = $seqobj->get_SeqFeatures(); # just top level
45 foreach my $feat ( @features ) {
46 print "Feature ",$feat->primary_tag," starts ",$feat->start," ends ",
47 $feat->end," strand ",$feat->strand,"\n";
49 # features retain link to underlying sequence object
50 print "Feature sequence is ",$feat->seq->seq(),"\n"
53 # sequences may have a species
55 if( defined $seq->species ) {
56 print "Sequence is from ",$species->binomial," [",$species->common_name,"]\n";
59 # annotation objects are Bio::AnnotationCollectionI's
60 $ann = $seqobj->annotation(); # annotation object
62 # references is one type of annotations to get. Also get
63 # comment and dblink. Look at Bio::AnnotationCollection for
64 # more information
66 foreach my $ref ( $ann->get_Annotations('reference') ) {
67 print "Reference ",$ref->title,"\n";
70 # you can get truncations, translations and reverse complements, these
71 # all give back Bio::Seq objects themselves, though currently with no
72 # features transferred
74 my $trunc = $seqobj->trunc(100,200);
75 my $rev = $seqobj->revcom();
77 # there are many options to translate - check out the docs
78 my $trans = $seqobj->translate();
80 # these functions can be chained together
82 my $trans_trunc_rev = $seqobj->trunc(100,200)->revcom->translate();
86 =head1 DESCRIPTION
88 A Seq object is a sequence with sequence features placed on it. The
89 Seq object contains a PrimarySeq object for the actual sequence and
90 also implements its interface.
92 In Bioperl we have 3 main players that people are going to use frequently
94 Bio::PrimarySeq - just the sequence and its names, nothing else.
95 Bio::SeqFeatureI - a feature on a sequence, potentially with a sequence
96 and a location and annotation.
97 Bio::Seq - A sequence and a collection of sequence features
98 (an aggregate) with its own annotation.
100 Although Bioperl is not tied heavily to file formats these distinctions do
101 map to file formats sensibly and for some bioinformaticians this might help
103 Bio::PrimarySeq - Fasta file of a sequence
104 Bio::SeqFeatureI - A single entry in an EMBL/GenBank/DDBJ feature table
105 Bio::Seq - A single EMBL/GenBank/DDBJ entry
107 By having this split we avoid a lot of nasty circular references
108 (sequence features can hold a reference to a sequence without the sequence
109 holding a reference to the sequence feature). See L<Bio::PrimarySeq> and
110 L<Bio::SeqFeatureI> for more information.
112 Ian Korf really helped in the design of the Seq and SeqFeature system.
114 =head2 Examples
116 A simple and fundamental block of code:
118 use Bio::SeqIO;
120 my $seqIOobj = Bio::SeqIO->new(-file=>"1.fa"); # create a SeqIO object
121 my $seqobj = $seqIOobj->next_seq; # get a Seq object
123 With the Seq object in hand one has access to a powerful set of Bioperl
124 methods and related Bioperl objects. This next script will take a file of sequences
125 in EMBL format and create a file of the reverse-complemented sequences
126 in Fasta format using Seq objects. It also prints out details about the
127 exons it finds as sequence features in Genbank Flat File format.
129 use Bio::Seq;
130 use Bio::SeqIO;
132 $seqin = Bio::SeqIO->new( -format => 'EMBL' , -file => 'myfile.dat');
133 $seqout= Bio::SeqIO->new( -format => 'Fasta', -file => '>output.fa');
135 while((my $seqobj = $seqin->next_seq())) {
136 print "Seen sequence ",$seqobj->display_id,", start of seq ",
137 substr($seqobj->seq,1,10),"\n";
138 if( $seqobj->alphabet eq 'dna') {
139 $rev = $seqobj->revcom;
140 $id = $seqobj->display_id();
141 $id = "$id.rev";
142 $rev->display_id($id);
143 $seqout->write_seq($rev);
146 foreach $feat ( $seqobj->get_SeqFeatures() ) {
147 if( $feat->primary_tag eq 'exon' ) {
148 print STDOUT "Location ",$feat->start,":",
149 $feat->end," GFF[",$feat->gff_string,"]\n";
154 Let's examine the script. The lines below import the Bioperl modules.
155 Seq is the main Bioperl sequence object and SeqIO is the Bioperl support
156 for reading sequences from files and to files
158 use Bio::Seq;
159 use Bio::SeqIO;
161 These two lines create two SeqIO streams: one for reading in sequences
162 and one for outputting sequences:
164 $seqin = Bio::SeqIO->new( -format => 'EMBL' , -file => 'myfile.dat');
165 $seqout= Bio::SeqIO->new( -format => 'Fasta', -file => '>output.fa');
167 Notice that in the "$seqout" case there is a greater-than sign,
168 indicating the file is being opened for writing.
170 Using the
172 '-argument' => value
174 syntax is common in Bioperl. The file argument is like an argument
175 to open() . You can also pass in filehandles or FileHandle objects by
176 using the -fh argument (see L<Bio::SeqIO> documentation for details).
177 Many formats in Bioperl are handled, including Fasta, EMBL, GenBank,
178 Swissprot (swiss), PIR, and GCG.
180 $seqin = Bio::SeqIO->new( -format => 'EMBL' , -file => 'myfile.dat');
181 $seqout= Bio::SeqIO->new( -format => 'Fasta', -file => '>output.fa');
183 This is the main loop which will loop progressively through sequences
184 in a file, and each call to $seqio-E<gt>next_seq() provides a new Seq
185 object from the file:
187 while((my $seqobj = $seqio->next_seq())) {
189 This print line below accesses fields in the Seq object directly. The
190 $seqobj-E<gt>display_id is the way to access the display_id attribute
191 of the Seq object. The $seqobj-E<gt>seq method gets the actual
192 sequence out as string. Then you can do manipulation of this if
193 you want to (there are however easy ways of doing truncation,
194 reverse-complement and translation).
196 print "Seen sequence ",$seqobj->display_id,", start of seq ",
197 substr($seqobj->seq,1,10),"\n";
199 Bioperl has to guess the alphabet of the sequence, being either 'dna',
200 'rna', or 'protein'. The alphabet attribute is one of these three
201 possibilities.
203 if( $seqobj->alphabet eq 'dna') {
205 The $seqobj-E<gt>revcom method provides the reverse complement of the Seq
206 object as another Seq object. Thus, the $rev variable is a reference to
207 another Seq object. For example, one could repeat the above print line
208 for this Seq object (putting $rev in place of $seqobj). In this
209 case we are going to output the object into the file stream we built
210 earlier on.
212 $rev = $seqobj->revcom;
214 When we output it, we want the id of the outputted object
215 to be changed to "$id.rev", ie, with .rev on the end of the name. The
216 following lines retrieve the id of the sequence object, add .rev
217 to this and then set the display_id of the rev sequence object to
218 this. Notice that to set the display_id attribute you just need
219 call the same method, display_id(), with the new value as an argument.
220 Getting and setting values with the same method is common in Bioperl.
222 $id = $seqobj->display_id();
223 $id = "$id.rev";
224 $rev->display_id($id);
226 The write_seq method on the SeqIO output object, $seqout, writes the
227 $rev object to the filestream we built at the top of the script.
228 The filestream knows that it is outputting in fasta format, and
229 so it provides fasta output.
231 $seqout->write_seq($rev);
233 This block of code loops over sequence features in the sequence
234 object, trying to find ones who have been tagged as 'exon'.
235 Features have start and end attributes and can be outputted
236 in Genbank Flat File format, GFF, a standarized format for sequence
237 features.
239 foreach $feat ( $seqobj->get_SeqFeatures() ) {
240 if( $feat->primary_tag eq 'exon' ) {
241 print STDOUT "Location ",$feat->start,":",
242 $feat->end," GFF[",$feat->gff_string,"]\n";
246 The code above shows how a few Bio::Seq methods suffice to read, parse,
247 reformat and analyze sequences from a file. A full list of methods
248 available to Bio::Seq objects is shown below. Bear in mind that some of
249 these methods come from PrimarySeq objects, which are simpler
250 than Seq objects, stripped of features (see L<Bio::PrimarySeq> for
251 more information).
253 # these methods return strings, and accept strings in some cases:
255 $seqobj->seq(); # string of sequence
256 $seqobj->subseq(5,10); # part of the sequence as a string
257 $seqobj->accession_number(); # when there, the accession number
258 $seqobj->alphabet(); # one of 'dna','rna',or 'protein'
259 $seqobj->version() # when there, the version
260 $seqobj->keywords(); # when there, the Keywords line
261 $seqobj->length() # length
262 $seqobj->desc(); # description
263 $seqobj->primary_id(); # a unique id for this sequence regardless
264 # of its display_id or accession number
265 $seqobj->display_id(); # the human readable id of the sequence
267 Some of these values map to fields in common formats. For example, The
268 display_id() method returns the LOCUS name of a Genbank entry,
269 the (\S+) following the E<gt> character in a Fasta file, the ID from
270 a SwissProt file, and so on. The desc() method will return the DEFINITION
271 line of a Genbank file, the description following the display_id in a
272 Fasta file, and the DE field in a SwissProt file.
274 # the following methods return new Seq objects, but
275 # do not transfer features across to the new object:
277 $seqobj->trunc(5,10) # truncation from 5 to 10 as new object
278 $seqobj->revcom # reverse complements sequence
279 $seqobj->translate # translation of the sequence
281 # if new() can be called this method returns 1, else 0
283 $seqobj->can_call_new
285 # the following method determines if the given string will be accepted
286 # by the seq() method - if the string is acceptable then validate()
287 # returns 1, or 0 if not
289 $seqobj->validate_seq($string)
291 # the following method returns or accepts a Species object:
293 $seqobj->species();
295 Please see L<Bio::Species> for more information on this object.
297 # the following method returns or accepts an Annotation object
298 # which in turn allows access to Annotation::Reference
299 # and Annotation::Comment objects:
301 $seqobj->annotation();
303 These annotations typically refer to entire sequences, unlike
304 features. See L<Bio::AnnotationCollectionI>,
305 L<Bio::Annotation::Collection>, L<Bio::Annotation::Reference>, and
306 L<Bio::Annotation::Comment> for details.
308 It is also important to be able to describe defined portions of a
309 sequence. The combination of some description and the corresponding
310 sub-sequence is called a feature - an exon and its coordinates within
311 a gene is an example of a feature, or a domain within a protein.
313 # the following methods return an array of SeqFeatureI objects:
315 $seqobj->get_SeqFeatures # The 'top level' sequence features
316 $seqobj->get_all_SeqFeatures # All sequence features, including sub-seq
317 # features, such as features in an exon
319 # to find out the number of features use:
321 $seqobj->feature_count
323 Here are just some of the methods available to SeqFeatureI objects:
325 # these methods return numbers:
327 $feat->start # start position (1 is the first base)
328 $feat->end # end position (2 is the second base)
329 $feat->strand # 1 means forward, -1 reverse, 0 not relevant
331 # these methods return or accept strings:
333 $feat->primary_tag # the name of the sequence feature, eg
334 # 'exon', 'glycoslyation site', 'TM domain'
335 $feat->source_tag # where the feature comes from, eg, 'EMBL_GenBank',
336 # or 'BLAST'
338 # this method returns the more austere PrimarySeq object, not a
339 # Seq object - the main difference is that PrimarySeq objects do not
340 # themselves contain sequence features
342 $feat->seq # the sequence between start,end on the
343 # correct strand of the sequence
345 See L<Bio::PrimarySeq> for more details on PrimarySeq objects.
347 # useful methods for feature comparisons, for start/end points
349 $feat->overlaps($other) # do $feat and $other overlap?
350 $feat->contains($other) # is $other completely within $feat?
351 $feat->equals($other) # do $feat and $other completely agree?
353 # one can also add features
355 $seqobj->add_SeqFeature($feat) # returns 1 if successful
357 # sub features. For complex join() statements, the feature
358 # is one sequence feature with many sub SeqFeatures
360 $feat->sub_SeqFeature # returns array of sub seq features
362 Please see L<Bio::SeqFeatureI> and L<Bio::SeqFeature::Generic>,
363 for more information on sequence features.
365 It is worth mentioning that one can also retrieve the start and end
366 positions of a feature using a Bio::LocationI object:
368 $location = $feat->location # $location is a Bio::LocationI object
369 $location->start; # start position
370 $location->end; # end position
372 This is useful because one needs a Bio::Location::SplitLocationI object
373 in order to retrieve the coordinates inside the Genbank or EMBL join()
374 statements (e.g. "CDS join(51..142,273..495,1346..1474)"):
376 if ( $feat->location->isa('Bio::Location::SplitLocationI') &&
377 $feat->primary_tag eq 'CDS' ) {
378 foreach $loc ( $feat->location->sub_Location ) {
379 print $loc->start . ".." . $loc->end . "\n";
383 See L<Bio::LocationI> and L<Bio::Location::SplitLocationI> for more
384 information.
386 =head1 Implemented Interfaces
388 This class implements the following interfaces.
390 =over 4
392 =item Bio::SeqI
394 Note that this includes implementing Bio::PrimarySeqI.
396 =item Bio::IdentifiableI
398 =item Bio::DescribableI
400 =item Bio::AnnotatableI
402 =item Bio::FeatureHolderI
404 =back
406 =head1 FEEDBACK
409 =head2 Mailing Lists
411 User feedback is an integral part of the evolution of this and other
412 Bioperl modules. Send your comments and suggestions preferably to one
413 of the Bioperl mailing lists. Your participation is much appreciated.
415 bioperl-l@bioperl.org - General discussion
416 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
418 =head2 Support
420 Please direct usage questions or support issues to the mailing list:
422 I<bioperl-l@bioperl.org>
424 rather than to the module maintainer directly. Many experienced and
425 reponsive experts will be able look at the problem and quickly
426 address it. Please include a thorough description of the problem
427 with code and data examples if at all possible.
429 =head2 Reporting Bugs
431 Report bugs to the Bioperl bug tracking system to help us keep track
432 the bugs and their resolution. Bug reports can be submitted via the
433 web:
435 https://github.com/bioperl/bioperl-live/issues
437 =head1 AUTHOR - Ewan Birney, inspired by Ian Korf objects
439 Email birney@ebi.ac.uk
441 =head1 CONTRIBUTORS
443 Jason Stajich E<lt>jason@bioperl.orgE<gt>
444 Mark A. Jensen maj -at- fortinbras -dot- us
446 =head1 APPENDIX
449 The rest of the documentation details each of the object
450 methods. Internal methods are usually preceded with a "_".
452 =cut
455 # Let the code begin...
458 package Bio::Seq;
459 use strict;
461 use Carp;
463 use Bio::Annotation::Collection;
464 use Bio::PrimarySeq;
466 use base qw(Bio::Root::Root Bio::SeqI Bio::IdentifiableI Bio::DescribableI Bio::AnnotatableI Bio::FeatureHolderI Bio::AnnotationCollectionI);
468 =head2 new
470 Title : new
471 Usage : $seq = Bio::Seq->new( -seq => 'ATGGGGGTGGTGGTACCCT',
472 -id => 'human_id',
473 -accession_number => 'AL000012',
476 Function: Returns a new Seq object from
477 basic constructors, being a string for the sequence
478 and strings for id and accession_number
479 Returns : a new Bio::Seq object
481 =cut
483 sub new {
484 my($caller,@args) = @_;
486 if( $caller ne 'Bio::Seq') {
487 $caller = ref($caller) if ref($caller);
490 # we know our inherietance hierarchy
491 my $self = Bio::Root::Root->new(@args);
492 bless $self,$caller;
494 # this is way too sneaky probably. We delegate the construction of
495 # the Seq object onto PrimarySeq and then pop primary_seq into
496 # our primary_seq slot
498 my $pseq = Bio::PrimarySeq->new(@args);
500 # as we have just made this, we know it is ok to set hash directly
501 # rather than going through the method
503 $self->{'primary_seq'} = $pseq;
505 # setting this array is now delayed until the final
506 # moment, again speed ups for non feature containing things
507 # $self->{'_as_feat'} = [];
510 my ($ann, $pid,$feat,$species) = &Bio::Root::RootI::_rearrange($self,[qw(ANNOTATION PRIMARY_ID FEATURES SPECIES)], @args);
512 # for a number of cases - reading fasta files - these are never set. This
513 # gives a quick optimisation around testing things later on
515 if( defined $ann || defined $pid || defined $feat || defined $species ) {
516 $pid && $self->primary_id($pid);
517 $species && $self->species($species);
518 $ann && $self->annotation($ann);
520 if( defined $feat ) {
521 if( ref($feat) !~ /ARRAY/i ) {
522 if( ref($feat) && $feat->isa('Bio::SeqFeatureI') ) {
523 $self->add_SeqFeature($feat);
524 } else {
525 $self->warn("Must specify a valid Bio::SeqFeatureI or ArrayRef of Bio::SeqFeatureI's with the -features init parameter for ".ref($self));
527 } else {
528 foreach my $feature ( @$feat ) {
529 $self->add_SeqFeature($feature);
535 return $self;
539 =head1 PrimarySeq interface
542 The PrimarySeq interface provides the basic sequence getting
543 and setting methods for on all sequences.
545 These methods implement the Bio::PrimarySeq interface by delegating
546 to the primary_seq inside the object. This means that you
547 can use a Seq object wherever there is a PrimarySeq, and
548 of course, you are free to use these functions anyway.
550 =cut
552 =head2 seq
554 Title : seq
555 Usage : $string = $obj->seq()
556 Function: Get/Set the sequence as a string of letters. The
557 case of the letters is left up to the implementer.
558 Suggested cases are upper case for proteins and lower case for
559 DNA sequence (IUPAC standard),
560 but implementations are suggested to keep an open mind about
561 case (some users... want mixed case!)
562 Returns : A scalar
563 Args : Optionally on set the new value (a string). An optional second
564 argument presets the alphabet (otherwise it will be guessed).
565 Both parameters may also be given in named parameter style
566 with -seq and -alphabet being the names.
568 =cut
570 sub seq {
571 return shift->primary_seq()->seq(@_);
575 =head2 validate_seq
577 Title : validate_seq
578 Usage : if(! $seqobj->validate_seq($seq_str) ) {
579 print "sequence $seq_str is not valid for an object of
580 alphabet ",$seqobj->alphabet, "\n";
582 Function: Test that the given sequence is valid, i.e. contains only valid
583 characters. The allowed characters are all letters (A-Z) and '-','.',
584 '*','?','=' and '~'. Spaces are not valid. Note that this
585 implementation does not take alphabet() into account.
586 Returns : 1 if the supplied sequence string is valid, 0 otherwise.
587 Args : - Sequence string to be validated
588 - Boolean to throw an error if the sequence is invalid
590 =cut
592 sub validate_seq {
593 return shift->primary_seq()->validate_seq(@_);
597 =head2 length
599 Title : length
600 Usage : $len = $seq->length()
601 Function:
602 Example :
603 Returns : Integer representing the length of the sequence.
604 Args : None
606 =cut
608 sub length {
609 return shift->primary_seq()->length(@_);
613 =head1 Methods from the Bio::PrimarySeqI interface
615 =head2 subseq
617 Title : subseq
618 Usage : $substring = $obj->subseq(10,40);
619 Function: Returns the subseq from start to end, where the first base
620 is 1 and the number is inclusive, ie 1-2 are the first two
621 bases of the sequence
623 Start cannot be larger than end but can be equal
625 Returns : A string
626 Args : 2 integers
629 =cut
631 sub subseq {
632 return shift->primary_seq()->subseq(@_);
636 =head2 display_id
638 Title : display_id
639 Usage : $id = $obj->display_id or $obj->display_id($newid);
640 Function: Gets or sets the display id, also known as the common name of
641 the Seq object.
643 The semantics of this is that it is the most likely string
644 to be used as an identifier of the sequence, and likely to
645 have "human" readability. The id is equivalent to the LOCUS
646 field of the GenBank/EMBL databanks and the ID field of the
647 Swissprot/sptrembl database. In fasta format, the >(\S+) is
648 presumed to be the id, though some people overload the id
649 to embed other information. Bioperl does not use any
650 embedded information in the ID field, and people are
651 encouraged to use other mechanisms (accession field for
652 example, or extending the sequence object) to solve this.
654 Notice that $seq->id() maps to this function, mainly for
655 legacy/convenience issues.
656 Returns : A string
657 Args : None or a new id
659 =cut
661 sub display_id {
662 return shift->primary_seq->display_id(@_);
666 =head2 accession_number
668 Title : accession_number
669 Usage : $unique_biological_key = $obj->accession_number;
670 Function: Returns the unique biological id for a sequence, commonly
671 called the accession_number. For sequences from established
672 databases, the implementors should try to use the correct
673 accession number. Notice that primary_id() provides the
674 unique id for the implementation, allowing multiple objects
675 to have the same accession number in a particular implementation.
677 For sequences with no accession number, this method should return
678 "unknown".
680 Can also be used to set the accession number.
681 Example : $key = $seq->accession_number or $seq->accession_number($key)
682 Returns : A string
683 Args : None or an accession number
685 =cut
687 sub accession_number {
688 return shift->primary_seq->accession_number(@_);
692 =head2 desc
694 Title : desc
695 Usage : $seqobj->desc($string) or $seqobj->desc()
696 Function: Sets or gets the description of the sequence
697 Example :
698 Returns : The description
699 Args : The description or none
701 =cut
703 sub desc {
704 return shift->primary_seq->desc(@_);
708 =head2 primary_id
710 Title : primary_id
711 Usage : $unique_implementation_key = $obj->primary_id;
712 Function: Returns the unique id for this object in this
713 implementation. This allows implementations to manage
714 their own object ids in a way the implementation can control
715 clients can expect one id to map to one object.
717 For sequences with no natural id, this method should return
718 a stringified memory location.
720 Can also be used to set the primary_id (or unset to undef).
722 [Note this method name is likely to change in 1.3]
724 Example : $id = $seq->primary_id or $seq->primary_id($id)
725 Returns : A string
726 Args : None or an id, or undef to unset the primary id.
728 =cut
730 sub primary_id {
731 # Note: this used to not delegate to the primary seq. This is
732 # really bad in very subtle ways. E.g., if you created the object
733 # with a primary id given to the constructor and then later you
734 # change the primary id, if this method wouldn't delegate you'd
735 # have different values for primary id in the PrimarySeq object
736 # compared to this instance. Not good.
738 # I can't remember why not delegating was ever deemed
739 # advantageous, but I hereby claim that its problems far outweigh
740 # its advantages, if there are any. Convince me otherwise if you
741 # disagree. HL 2004/08/05
743 return shift->primary_seq->primary_id(@_);
747 =head2 can_call_new
749 Title : can_call_new
750 Usage : if ( $obj->can_call_new ) {
751 $newobj = $obj->new( %param );
753 Function: can_call_new returns 1 or 0 depending
754 on whether an implementation allows new
755 constructor to be called. If a new constructor
756 is allowed, then it should take the followed hashed
757 constructor list.
759 $myobject->new( -seq => $sequence_as_string,
760 -display_id => $id
761 -accession_number => $accession
762 -alphabet => 'dna',
764 Example :
765 Returns : 1 or 0
766 Args : None
768 =cut
770 sub can_call_new {
771 return 1;
775 =head2 alphabet
777 Title : alphabet
778 Usage : if ( $obj->alphabet eq 'dna' ) { /Do Something/ }
779 Function: Get/Set the type of sequence being one of
780 'dna', 'rna' or 'protein'. This is case sensitive.
782 This is not called <type> because this would cause
783 upgrade problems from the 0.5 and earlier Seq objects.
785 Returns : A string either 'dna','rna','protein'. NB - the object must
786 make a call of the type - if there is no type specified it
787 has to guess.
788 Args : optional string to set : 'dna' | 'rna' | 'protein'
790 =cut
792 sub alphabet {
793 my $self = shift;
794 return $self->primary_seq->alphabet(@_) if @_ && defined $_[0];
795 return $self->primary_seq->alphabet();
799 =head2 is_circular
801 Title : is_circular
802 Usage : if( $obj->is_circular) { /Do Something/ }
803 Function: Returns true if the molecule is circular
804 Returns : Boolean value
805 Args : none
807 =cut
809 sub is_circular {
810 return shift->primary_seq()->is_circular(@_);
814 =head1 Methods for Bio::IdentifiableI compliance
816 =head2 object_id
818 Title : object_id
819 Usage : $string = $obj->object_id()
820 Function: a string which represents the stable primary identifier
821 in this namespace of this object. For DNA sequences this
822 is its accession_number, similarly for protein sequences
824 This is aliased to accession_number().
825 Returns : A scalar
827 =cut
829 sub object_id {
830 return shift->accession_number(@_);
834 =head2 version
836 Title : version
837 Usage : $version = $obj->version()
838 Function: a number which differentiates between versions of
839 the same object. Higher numbers are considered to be
840 later and more relevant, but a single object described
841 the same identifier should represent the same concept
843 Returns : A number
845 =cut
847 sub version{
848 return shift->primary_seq->version(@_);
852 =head2 authority
854 Title : authority
855 Usage : $authority = $obj->authority()
856 Function: a string which represents the organisation which
857 granted the namespace, written as the DNS name for
858 organisation (eg, wormbase.org)
860 Returns : A scalar
862 =cut
864 sub authority {
865 return shift->primary_seq()->authority(@_);
869 =head2 namespace
871 Title : namespace
872 Usage : $string = $obj->namespace()
873 Function: A string representing the name space this identifier
874 is valid in, often the database name or the name
875 describing the collection
877 Returns : A scalar
879 =cut
881 sub namespace{
882 return shift->primary_seq()->namespace(@_);
886 =head1 Methods for Bio::DescribableI compliance
888 =head2 display_name
890 Title : display_name
891 Usage : $string = $obj->display_name()
892 Function: A string which is what should be displayed to the user
893 the string should have no spaces (ideally, though a cautious
894 user of this interface would not assume this) and should be
895 less than thirty characters (though again, double checking
896 this is a good idea)
898 This is aliased to display_id().
899 Returns : A scalar
901 =cut
903 sub display_name {
904 return shift->display_id(@_);
907 =head2 description
909 Title : description
910 Usage : $string = $obj->description()
911 Function: A text string suitable for displaying to the user a
912 description. This string is likely to have spaces, but
913 should not have any newlines or formatting - just plain
914 text. The string should not be greater than 255 characters
915 and clients can feel justified at truncating strings at 255
916 characters for the purposes of display
918 This is aliased to desc().
919 Returns : A scalar
921 =cut
923 sub description {
924 return shift->desc(@_);
928 =head1 Methods for implementing Bio::AnnotatableI
930 =head2 annotation
932 Title : annotation
933 Usage : $ann = $seq->annotation or
934 $seq->annotation($ann)
935 Function: Gets or sets the annotation
936 Returns : Bio::AnnotationCollectionI object
937 Args : None or Bio::AnnotationCollectionI object
939 See L<Bio::AnnotationCollectionI> and L<Bio::Annotation::Collection>
940 for more information
942 =cut
944 sub annotation {
945 my ($obj,$value) = @_;
946 if( defined $value ) {
947 $obj->throw("object of class ".ref($value)." does not implement ".
948 "Bio::AnnotationCollectionI. Too bad.")
949 unless $value->isa("Bio::AnnotationCollectionI");
950 $obj->{'_annotation'} = $value;
951 } elsif( ! defined $obj->{'_annotation'}) {
952 $obj->{'_annotation'} = Bio::Annotation::Collection->new();
954 return $obj->{'_annotation'};
958 =head1 Methods for delegating Bio::AnnotationCollectionI
960 =head2 get_Annotations()
962 Usage : my @annotations = $seq->get_Annotations('key')
963 Function: Retrieves all the Bio::AnnotationI objects for a specific key
964 for this object
965 Returns : list of Bio::AnnotationI - empty if no objects stored for a key
966 Args : string which is key for annotations
968 =cut
970 sub get_Annotations { shift->annotation->get_Annotations(@_); }
973 =head2 add_Annotation()
975 Usage : $seq->add_Annotation('reference',$object);
976 $seq->add_Annotation($object,'Bio::MyInterface::DiseaseI');
977 $seq->add_Annotation($object);
978 $seq->add_Annotation('disease',$object,'Bio::MyInterface::DiseaseI');
979 Function: Adds an annotation for a specific key for this sequence object.
981 If the key is omitted, the object to be added must provide a value
982 via its tagname().
984 If the archetype is provided, this and future objects added under
985 that tag have to comply with the archetype and will be rejected
986 otherwise.
988 Returns : none
989 Args : annotation key ('disease', 'dblink', ...)
990 object to store (must be Bio::AnnotationI compliant)
991 [optional] object archetype to map future storage of object
992 of these types to
994 =cut
996 sub add_Annotation { shift->annotation->add_Annotation(@_) }
999 =head2 remove_Annotations()
1001 Usage : $seq->remove_Annotations()
1002 Function: Remove the annotations for the specified key from this sequence
1003 object
1004 Returns : an list of Bio::AnnotationI compliant objects which were stored
1005 under the given key(s) for this sequence object
1006 Args : the key(s) (tag name(s), one or more strings) for which to
1007 remove annotations (optional; if none given, flushes all
1008 annotations)
1010 =cut
1012 sub remove_Annotations { shift->annotation->remove_Annotations(@_) }
1015 =head2 get_num_of_annotations()
1017 Usage : my $count = $seq->get_num_of_annotations()
1018 Alias : num_Annotations
1019 Function: Returns the count of all annotations stored for this sequence
1020 object
1021 Returns : integer
1022 Args : none
1024 =cut
1026 sub get_num_of_annotations { shift->annotation->get_num_of_annotations(@_) }
1027 sub num_Annotations { shift->get_num_of_annotations }; #DWYM
1030 =head1 Methods to implement Bio::FeatureHolderI
1032 This includes methods for retrieving, adding, and removing features.
1034 =cut
1036 =head2 get_SeqFeatures
1038 Title : get_SeqFeatures
1039 Usage :
1040 Function: Get the feature objects held by this feature holder.
1042 Features which are not top-level are subfeatures of one or
1043 more of the returned feature objects, which means that you
1044 must traverse the subfeature arrays of each top-level
1045 feature object in order to traverse all features associated
1046 with this sequence.
1048 Specific features can be obtained by primary tag, specified in
1049 the argument.
1051 Use get_all_SeqFeatures() if you want the feature tree
1052 flattened into one single array.
1054 Example : my @feats = $seq->get_SeqFeatures or
1055 my @genefeats = $seq->get_SeqFeatures('gene')
1056 Returns : an array of Bio::SeqFeatureI implementing objects
1057 Args : [optional] string (feature tag)
1059 =cut
1061 sub get_SeqFeatures{
1062 my $self = shift;
1063 my $tag = shift;
1065 if( !defined $self->{'_as_feat'} ) {
1066 $self->{'_as_feat'} = [];
1068 if ($tag) {
1069 return map { $_->primary_tag eq $tag ? $_ : () } @{$self->{'_as_feat'}};
1071 else {
1072 return @{$self->{'_as_feat'}};
1077 =head2 get_all_SeqFeatures
1079 Title : get_all_SeqFeatures
1080 Usage : @feat_ary = $seq->get_all_SeqFeatures();
1081 Function: Returns the tree of feature objects attached to this
1082 sequence object flattened into one single array. Top-level
1083 features will still contain their subfeature-arrays, which
1084 means that you will encounter subfeatures twice if you
1085 traverse the subfeature tree of the returned objects.
1087 Use get_SeqFeatures() if you want the array to contain only
1088 the top-level features.
1090 Returns : An array of Bio::SeqFeatureI implementing objects.
1091 Args : None
1093 =cut
1095 # this implementation is inherited from FeatureHolderI
1097 =head2 feature_count
1099 Title : feature_count
1100 Usage : $seq->feature_count()
1101 Function: Return the number of SeqFeatures attached to a sequence
1102 Returns : integer representing the number of SeqFeatures
1103 Args : None
1105 =cut
1107 sub feature_count {
1108 my ($self) = @_;
1110 if (defined($self->{'_as_feat'})) {
1111 return ($#{$self->{'_as_feat'}} + 1);
1112 } else {
1113 return 0;
1118 =head2 add_SeqFeature
1120 Title : add_SeqFeature
1121 Usage : $seq->add_SeqFeature($feat);
1122 Function: Adds the given feature object to the feature array of this
1123 sequence. The object passed is required to implement the
1124 Bio::SeqFeatureI interface.
1125 The 'EXPAND' qualifier (see L<Bio::FeatureHolderI>) is supported, but
1126 has no effect,
1127 Returns : 1 on success
1128 Args : A Bio::SeqFeatureI implementing object.
1130 =cut
1132 sub add_SeqFeature {
1133 my ($self, @feat) = @_;
1135 $self->{'_as_feat'} = [] unless $self->{'_as_feat'};
1137 if (scalar @feat > 1) {
1138 Carp::carp('Providing an array of features to Bio::Seq add_SeqFeature()'
1139 . ' is deprecated and will be removed in a future version.'
1140 . ' Add a single feature at a time instead.');
1143 for my $feat ( @feat ) {
1145 next if $feat eq 'EXPAND'; # Need to support it for FeatureHolderI compliance
1147 if( !$feat->isa("Bio::SeqFeatureI") ) {
1148 $self->throw("Expected a Bio::SeqFeatureI object, but got a $feat.");
1151 # make sure we attach ourselves to the feature if the feature wants it
1152 my $aseq = $self->primary_seq;
1153 $feat->attach_seq($aseq) if $aseq;
1155 push(@{$self->{'_as_feat'}},$feat);
1157 return 1;
1161 =head2 remove_SeqFeatures
1163 Title : remove_SeqFeatures
1164 Usage : $seq->remove_SeqFeatures();
1165 Function: Removes all attached SeqFeatureI objects or those with the
1166 specified primary tag
1167 Example : my @gene_feats = $seq->remove_seqFeatures('gene') or
1168 my @feats = $seq->remove_seqFeatures()
1169 Returns : The array of Bio::SeqFeatureI objects removed from the sequence
1170 Args : None, or a feature primary tag
1172 =cut
1174 sub remove_SeqFeatures {
1175 my ( $self, $tag ) = @_;
1176 return () unless $self->{'_as_feat'};
1178 if ( $tag ) {
1179 my @selected_feats = grep { $_->primary_tag eq $tag } @{ $self->{'_as_feat'} };
1180 my @unselected_feats = grep { $_->primary_tag ne $tag } @{ $self->{'_as_feat'} };
1181 $self->{'_as_feat'} = \@unselected_feats;
1182 return @selected_feats;
1184 else {
1185 my @all_feats = @{ $self->{'_as_feat'} };
1186 $self->{'_as_feat'} = [];
1187 return @all_feats;
1191 =head1 Methods provided in the Bio::PrimarySeqI interface
1193 These methods are inherited from the PrimarySeq interface
1194 and work as one expects, building new Bio::Seq objects
1195 or other information as expected. See L<Bio::PrimarySeq>
1196 for more information.
1198 Sequence Features are B<not> transferred to the new objects.
1199 To reverse complement and include the features use
1200 L<Bio::SeqUtils::revcom_with_features>.
1202 =head2 revcom
1204 Title : revcom
1205 Usage : $rev = $seq->revcom()
1206 Function: Produces a new Bio::Seq object which
1207 is the reversed complement of the sequence. For protein
1208 sequences this throws an exception of "Sequence is a protein.
1209 Cannot revcom"
1211 The id is the same id as the original sequence, and the
1212 accession number is also identical. If someone wants to track
1213 that this sequence has be reversed, it needs to define its own
1214 extensions
1216 To do an in-place edit of an object you can go:
1218 $seq = $seq->revcom();
1220 This of course, causes Perl to handle the garbage collection of
1221 the old object, but it is roughly speaking as efficient as an
1222 in-place edit.
1224 Returns : A new (fresh) Bio::Seq object
1225 Args : None
1227 =head2 trunc
1229 Title : trunc
1230 Usage : $subseq = $myseq->trunc(10,100);
1231 Function: Provides a truncation of a sequence
1233 Example :
1234 Returns : A fresh Seq object
1235 Args : A Seq object
1237 =head2 id
1239 Title : id
1240 Usage : $id = $seq->id()
1241 Function: This is mapped on display_id
1242 Returns : value of display_id()
1243 Args : [optional] value to update display_id
1245 =cut
1247 sub id {
1248 return shift->display_id(@_);
1252 =head1 Seq only methods
1254 These methods are specific to the Bio::Seq object, and not
1255 found on the Bio::PrimarySeq object
1257 =head2 primary_seq
1259 Title : primary_seq
1260 Usage : $seq->primary_seq or $seq->primary_seq($newval)
1261 Function: Get or set a PrimarySeq object
1262 Example :
1263 Returns : PrimarySeq object
1264 Args : None or PrimarySeq object
1266 =cut
1268 sub primary_seq {
1269 my ($obj,$value) = @_;
1271 if( defined $value) {
1272 if( ! ref $value || ! $value->isa('Bio::PrimarySeqI') ) {
1273 $obj->throw("$value is not a Bio::PrimarySeq compliant object");
1276 $obj->{'primary_seq'} = $value;
1277 # descend down over all seqfeature objects, seeing whether they
1278 # want an attached seq.
1280 foreach my $sf ( $obj->get_SeqFeatures() ) {
1281 $sf->attach_seq($value);
1285 return $obj->{'primary_seq'};
1290 =head2 species
1292 Title : species
1293 Usage : $species = $seq->species() or $seq->species($species)
1294 Function: Gets or sets the species
1295 Returns : L<Bio::Species> object
1296 Args : None or L<Bio::Species> object
1298 See L<Bio::Species> for more information
1300 =cut
1302 sub species {
1303 my ($self, $species) = @_;
1304 if ($species) {
1305 $self->{'species'} = $species;
1306 } else {
1307 return $self->{'species'};
1312 # Internal methods follow...
1314 # keep AUTOLOAD happy
1315 sub DESTROY { }
1317 ############################################################################
1318 # aliases due to name changes or to compensate for our lack of consistency #
1319 ############################################################################
1321 # in all other modules we use the object in the singular --
1322 # lack of consistency sucks
1323 *flush_SeqFeature = \&remove_SeqFeatures;
1324 *flush_SeqFeatures = \&remove_SeqFeatures;
1326 # this is now get_SeqFeatures() (from FeatureHolderI)
1327 *top_SeqFeatures = \&get_SeqFeatures;
1329 # this is now get_all_SeqFeatures() in FeatureHolderI
1330 sub all_SeqFeatures{
1331 return shift->get_all_SeqFeatures(@_);
1334 sub accession {
1335 my $self = shift;
1336 $self->warn(ref($self)."::accession is deprecated, ".
1337 "use accession_number() instead");
1338 return $self->accession_number(@_);