sync w/ main trunk
[bioperl-live.git] / examples / root / lib / Bio / Seq.pm
blobac35162133fe8143293b2f9877665a25fe0552a6
1 # $Id$
3 # BioPerl module for Bio::Seq
5 # Please direct questions and support issues to <bioperl-l@bioperl.org>
7 # Cared for by Ewan Birney <birney@ebi.ac.uk>
9 # Copyright Ewan Birney
11 # You may distribute this module under the same terms as perl itself
13 # POD documentation - main docs before the code
15 =head1 NAME
17 Bio::Seq - Sequence object, with features
19 =head1 SYNOPSIS
21 # This is the main sequence object in Bioperl
23 # gets a sequence from a file
24 $seqio = Bio::SeqIO->new( '-format' => 'embl' , -file => 'myfile.dat');
25 $seqobj = $seqio->next_seq();
27 # SeqIO can both read and write sequences; see Bio::SeqIO
28 # for more information and examples
30 # get from database
31 $db = Bio::DB::GenBank->new();
32 $seqobj = $db->get_Seq_by_acc('X78121');
34 # make from strings in script
35 $seqobj = Bio::Seq->new( -display_id => 'my_id',
36 -seq => $sequence_as_string);
38 # gets sequence as a string from sequence object
39 $seqstr = $seqobj->seq(); # actual sequence as a string
40 $seqstr = $seqobj->subseq(10,50); # slice in biological coordinates
42 # retrieves information from the sequence
43 # features must implement Bio::SeqFeatureI interface
45 @features = $seqobj->get_SeqFeatures(); # just top level
46 foreach my $feat ( @features ) {
47 print "Feature ",$feat->primary_tag," starts ",$feat->start," ends ",
48 $feat->end," strand ",$feat->strand,"\n";
50 # features retain link to underlying sequence object
51 print "Feature sequence is ",$feat->seq->seq(),"\n"
54 # sequences may have a species
56 if( defined $seq->species ) {
57 print "Sequence is from ",$species->binomial_name," [",$species->common_name,"]\n";
60 # annotation objects are Bio::AnnotationCollectionI's
61 $ann = $seqobj->annotation(); # annotation object
63 # references is one type of annotations to get. Also get
64 # comment and dblink. Look at Bio::AnnotationCollection for
65 # more information
67 foreach my $ref ( $ann->get_Annotations('reference') ) {
68 print "Reference ",$ref->title,"\n";
71 # you can get truncations, translations and reverse complements, these
72 # all give back Bio::Seq objects themselves, though currently with no
73 # features transfered
75 my $trunc = $seqobj->trunc(100,200);
76 my $rev = $seqobj->revcom();
78 # there are many options to translate - check out the docs
79 my $trans = $seqobj->translate();
81 # these functions can be chained together
83 my $trans_trunc_rev = $seqobj->trunc(100,200)->revcom->translate();
87 =head1 DESCRIPTION
89 A Seq object is a sequence with sequence features placed on it. The
90 Seq object contains a PrimarySeq object for the actual sequence and
91 also implements its interface.
93 In Bioperl we have 3 main players that people are going to use frequently
95 Bio::PrimarySeq - just the sequence and its names, nothing else.
96 Bio::SeqFeatureI - a location on a sequence, potentially with a sequence
97 and annotation.
98 Bio::Seq - A sequence and a collection of sequence features
99 (an aggregate) with its own annotation.
101 Although Bioperl is not tied heavily to file formats these distinctions do
102 map to file formats sensibly and for some bioinformaticians this might help
104 Bio::PrimarySeq - Fasta file of a sequence
105 Bio::SeqFeatureI - A single entry in an EMBL/GenBank/DDBJ feature table
106 Bio::Seq - A single EMBL/GenBank/DDBJ entry
108 By having this split we avoid a lot of nasty circular references
109 (sequence features can hold a reference to a sequence without the sequence
110 holding a reference to the sequence feature). See L<Bio::PrimarySeq> and
111 L<Bio::SeqFeatureI> for more information.
113 Ian Korf really helped in the design of the Seq and SeqFeature system.
115 =head1 EXAMPLES
117 A simple and fundamental block of code
119 use Bio::SeqIO;
121 my $seqIOobj = Bio::SeqIO->new(-file=>"1.fa"); # create a SeqIO object
122 my $seqobj = $seqIOobj->next_seq; # get a Seq object
124 With the Seq object in hand one has access to a powerful set of Bioperl
125 methods and Bioperl objects. This next script will take a file of sequences
126 in EMBL format and create a file of the reverse-complemented sequences
127 in Fasta format using Seq objects. It also prints out details about the
128 exons it finds as sequence features in Genbank Flat File format.
130 use Bio::Seq;
131 use Bio::SeqIO;
133 $seqin = Bio::SeqIO->new( -format => 'EMBL' , -file => 'myfile.dat');
134 $seqout= Bio::SeqIO->new( -format => 'Fasta', -file => '>output.fa');
136 while((my $seqobj = $seqin->next_seq())) {
137 print "Seen sequence ",$seqobj->display_id,", start of seq ",
138 substr($seqobj->seq,1,10),"\n";
139 if( $seqobj->alphabet eq 'dna') {
140 $rev = $seqobj->revcom;
141 $id = $seqobj->display_id();
142 $id = "$id.rev";
143 $rev->display_id($id);
144 $seqout->write_seq($rev);
147 foreach $feat ( $seqobj->get_SeqFeatures() ) {
148 if( $feat->primary_tag eq 'exon' ) {
149 print STDOUT "Location ",$feat->start,":",
150 $feat->end," GFF[",$feat->gff_string,"]\n";
155 Let's examine the script. The lines below import the Bioperl modules.
156 Seq is the main Bioperl sequence object and SeqIO is the Bioperl support
157 for reading sequences from files and to files
159 use Bio::Seq;
160 use Bio::SeqIO;
162 These two lines create two SeqIO streams: one for reading in sequences
163 and one for outputting sequences:
165 $seqin = Bio::SeqIO->new( -format => 'EMBL' , -file => 'myfile.dat');
166 $seqout= Bio::SeqIO->new( -format => 'Fasta', -file => '>output.fa');
168 Notice that in the "$seqout" case there is a greater-than sign,
169 indicating the file is being opened for writing.
171 Using the
173 '-argument' => value
175 syntax is common in Bioperl. The file argument is like an argument
176 to open() . You can also pass in filehandles or FileHandle objects by
177 using the -fh argument (see L<Bio::SeqIO> documentation for details).
178 Many formats in Bioperl are handled, including Fasta, EMBL, GenBank,
179 Swissprot (swiss), PIR, and GCG.
181 $seqin = Bio::SeqIO->new( -format => 'EMBL' , -file => 'myfile.dat');
182 $seqout= Bio::SeqIO->new( -format => 'Fasta', -file => '>output.fa');
184 This is the main loop which will loop progressively through sequences
185 in a file, and each call to $seqio-E<gt>next_seq() provides a new Seq
186 object from the file:
188 while((my $seqobj = $seqio->next_seq())) {
190 This print line below accesses fields in the Seq object directly. The
191 $seqobj-E<gt>display_id is the way to access the display_id attribute
192 of the Seq object. The $seqobj-E<gt>seq method gets the actual
193 sequence out as string. Then you can do manipulation of this if
194 you want to (there are however easy ways of doing truncation,
195 reverse-complement and translation).
197 print "Seen sequence ",$seqobj->display_id,", start of seq ",
198 substr($seqobj->seq,1,10),"\n";
200 Bioperl has to guess the alphabet of the sequence, being either 'dna',
201 'rna', or 'protein'. The alphabet attribute is one of these three
202 possibilities.
204 if( $seqobj->alphabet eq 'dna') {
206 The $seqobj-E<gt>revcom method provides the reverse complement of the Seq
207 object as another Seq object. Thus, the $rev variable is a reference to
208 another Seq object. For example, one could repeat the above print line
209 for this Seq object (putting $rev in place of $seqobj). In this
210 case we are going to output the object into the file stream we built
211 earlier on.
213 $rev = $seqobj->revcom;
215 When we output it, we want the id of the outputted object
216 to be changed to "$id.rev", ie, with .rev on the end of the name. The
217 following lines retrieve the id of the sequence object, add .rev
218 to this and then set the display_id of the rev sequence object to
219 this. Notice that to set the display_id attribute you just need
220 call the same method, display_id(), with the new value as an argument.
221 Getting and setting values with the same method is common in Bioperl.
223 $id = $seqobj->display_id();
224 $id = "$id.rev";
225 $rev->display_id($id);
227 The write_seq method on the SeqIO output object, $seqout, writes the
228 $rev object to the filestream we built at the top of the script.
229 The filestream knows that it is outputting in fasta format, and
230 so it provides fasta output.
232 $seqout->write_seq($rev);
234 This block of code loops over sequence features in the sequence
235 object, trying to find ones who have been tagged as 'exon'.
236 Features have start and end attributes and can be outputted
237 in Genbank Flat File format, GFF, a standarized format for sequence
238 features.
240 foreach $feat ( $seqobj->get_SeqFeatures() ) {
241 if( $feat->primary_tag eq 'exon' ) {
242 print STDOUT "Location ",$feat->start,":",
243 $feat->end," GFF[",$feat->gff_string,"]\n";
247 The code above shows how a few Bio::Seq methods suffice to read, parse,
248 reformat and analyze sequences from a file. A full list of methods
249 available to Bio::Seq objects is shown below. Bear in mind that some of
250 these methods come from PrimarySeq objects, which are simpler
251 than Seq objects, stripped of features (see L<Bio::PrimarySeq> for
252 more information).
254 # these methods return strings, and accept strings in some cases:
256 $seqobj->seq(); # string of sequence
257 $seqobj->subseq(5,10); # part of the sequence as a string
258 $seqobj->accession_number(); # when there, the accession number
259 $seqobj->alphabet(); # one of 'dna','rna',or 'protein'
260 $seqobj->seq_version() # when there, the version
261 $seqobj->keywords(); # when there, the Keywords line
262 $seqobj->length() # length
263 $seqobj->desc(); # description
264 $seqobj->primary_id(); # a unique id for this sequence regardless
265 # of its display_id or accession number
266 $seqobj->display_id(); # the human readable id of the sequence
268 Some of these values map to fields in common formats. For example, The
269 display_id() method returns the LOCUS name of a Genbank entry,
270 the (\S+) following the E<gt> character in a Fasta file, the ID from
271 a SwissProt file, and so on. The desc() method will return the DEFINITION
272 line of a Genbank file, the description following the display_id in a
273 Fasta file, and the DE field in a SwissProt file.
275 # the following methods return new Seq objects, but
276 # do not transfer features across to the new object:
278 $seqobj->trunc(5,10) # truncation from 5 to 10 as new object
279 $seqobj->revcom # reverse complements sequence
280 $seqobj->translate # translation of the sequence
282 # if new() can be called this method returns 1, else 0
284 $seqobj->can_call_new
286 # the following method determines if the given string will be accepted
287 # by the seq() method - if the string is acceptable then validate()
288 # returns 1, or 0 if not
290 $seqobj->validate_seq($string)
292 # the following method returns or accepts a Species object:
294 $seqobj->species();
296 Please see L<Bio::Species> for more information on this object.
298 # the following method returns or accepts an Annotation object
299 # which in turn allows access to Annotation::Reference
300 # and Annotation::Comment objects:
302 $seqobj->annotation();
304 These annotations typically refer to entire sequences, unlike
305 features. See L<Bio::AnnotationCollectionI>,
306 L<Bio::Annotation::Collection>, L<Bio::Annotation::Reference>, and
307 L<Bio::Annotation::Comment> for details.
309 It is also important to be able to describe defined portions of a
310 sequence. The combination of some description and the corresponding
311 sub-sequence is called a feature - an exon and its coordinates within
312 a gene is an example of a feature, or a domain within a protein.
314 # the following methods return an array of SeqFeatureI objects:
316 $seqobj->get_SeqFeatures # The 'top level' sequence features
317 $seqobj->get_all_SeqFeatures # All sequence features, including sub-seq
318 # features, such as features in an exon
320 # to find out the number of features use:
322 $seqobj->feature_count
324 Here are just some of the methods available to SeqFeatureI objects:
326 # these methods return numbers:
328 $feat->start # start position (1 is the first base)
329 $feat->end # end position (2 is the second base)
330 $feat->strand # 1 means forward, -1 reverse, 0 not relevant
332 # these methods return or accept strings:
334 $feat->primary_tag # the name of the sequence feature, eg
335 # 'exon', 'glycoslyation site', 'TM domain'
336 $feat->source_tag # where the feature comes from, eg, 'EMBL_GenBank',
337 # or 'BLAST'
339 # this method returns the more austere PrimarySeq object, not a
340 # Seq object - the main difference is that PrimarySeq objects do not
341 # themselves contain sequence features
343 $feat->seq # the sequence between start,end on the
344 # correct strand of the sequence
346 See L<Bio::PrimarySeq> for more details on PrimarySeq objects.
348 # useful methods for feature comparisons, for start/end points
350 $feat->overlaps($other) # do $feat and $other overlap?
351 $feat->contains($other) # is $other completely within $feat?
352 $feat->equals($other) # do $feat and $other completely agree?
354 # one can also add features
356 $seqobj->add_SeqFeature($feat) # returns 1 if successful
357 $seqobj->add_SeqFeature(@features) # returns 1 if successful
359 # sub features. For complex join() statements, the feature
360 # is one sequence feature with many sub SeqFeatures
362 $feat->sub_SeqFeature # returns array of sub seq features
364 Please see L<Bio::SeqFeatureI> and L<Bio::SeqFeature::Generic>,
365 for more information on sequence features.
367 It is worth mentioning that one can also retrieve the start and end
368 positions of a feature using a Bio::LocationI object:
370 $location = $feat->location # $location is a Bio::LocationI object
371 $location->start; # start position
372 $location->end; # end position
374 This is useful because one needs a Bio::Location::SplitLocationI object
375 in order to retrieve the coordinates inside the Genbank or EMBL join()
376 statements (e.g. "CDS join(51..142,273..495,1346..1474)"):
378 if ( $feat->location->isa('Bio::Location::SplitLocationI') &&
379 $feat->primary_tag eq 'CDS' ) {
380 foreach $loc ( $feat->location->sub_Location ) {
381 print $loc->start . ".." . $loc->end . "\n";
385 See L<Bio::LocationI> and L<Bio::Location::SplitLocationI> for more
386 information.
388 =head1 Implemented Interfaces
390 This class implements the following interfaces.
392 =over 4
394 =item Bio::SeqI
396 Note that this includes implementing Bio::PrimarySeqI.
398 =item Bio::IdentifiableI
400 =item Bio::DescribableI
402 =item Bio::AnnotatableI
404 =item Bio::FeatureHolderI
406 =back
408 =head1 FEEDBACK
411 =head2 Mailing Lists
413 User feedback is an integral part of the evolution of this and other
414 Bioperl modules. Send your comments and suggestions preferably to one
415 of the Bioperl mailing lists. Your participation is much appreciated.
417 bioperl-l@bioperl.org - General discussion
418 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
420 =head2 Support
422 Please direct usage questions or support issues to the mailing list:
424 L<bioperl-l@bioperl.org>
426 rather than to the module maintainer directly. Many experienced and
427 reponsive experts will be able look at the problem and quickly
428 address it. Please include a thorough description of the problem
429 with code and data examples if at all possible.
431 =head2 Reporting Bugs
433 Report bugs to the Bioperl bug tracking system to help us keep track
434 the bugs and their resolution. Bug reports can be submitted via the
435 web:
437 http://bugzilla.open-bio.org/
439 =head1 AUTHOR - Ewan Birney, inspired by Ian Korf objects
441 Email birney@ebi.ac.uk
443 =head1 CONTRIBUTORS
445 Jason Stajich E<lt>jason@bioperl.orgE<gt>
447 =head1 APPENDIX
450 The rest of the documentation details each of the object
451 methods. Internal methods are usually preceded with a "_".
453 =cut
456 # Let the code begin...
459 package Bio::Seq;
460 use strict;
463 # Object preamble - inherits from Bio::Root::Object
465 use Bio::Annotation::Collection;
466 use Bio::PrimarySeq;
468 use base qw(Bio::Root::Root Bio::SeqI
469 Bio::IdentifiableI Bio::DescribableI
470 Bio::AnnotatableI Bio::FeatureHolderI);
472 =head2 new
474 Title : new
475 Usage : $seq = Bio::Seq->new( -seq => 'ATGGGGGTGGTGGTACCCT',
476 -id => 'human_id',
477 -accession_number => 'AL000012',
480 Function: Returns a new Seq object from
481 basic constructors, being a string for the sequence
482 and strings for id and accession_number
483 Returns : a new Bio::Seq object
485 =cut
487 sub new {
488 my($caller,@args) = @_;
490 if( $caller ne 'Bio::Seq') {
491 $caller = ref($caller) if ref($caller);
494 # we know our inherietance heirarchy
495 my $self = Bio::Root::Root->new(@args);
496 bless $self,$caller;
498 # this is way too sneaky probably. We delegate the construction of
499 # the Seq object onto PrimarySeq and then pop primary_seq into
500 # our primary_seq slot
502 my $pseq = Bio::PrimarySeq->new(@args);
504 # as we have just made this, we know it is ok to set hash directly
505 # rather than going through the method
507 $self->{'primary_seq'} = $pseq;
509 # setting this array is now delayed until the final
510 # moment, again speed ups for non feature containing things
511 # $self->{'_as_feat'} = [];
514 my ($ann, $pid,$feat,$species) = &Bio::Root::RootI::_rearrange($self,[qw(ANNOTATION PRIMARY_ID FEATURES SPECIES)], @args);
516 # for a number of cases - reading fasta files - these are never set. This
517 # gives a quick optimisation around testing things later on
519 if( defined $ann || defined $pid || defined $feat || defined $species ) {
520 $pid && $self->primary_id($pid);
521 $species && $self->species($species);
522 $ann && $self->annotation($ann);
524 if( defined $feat ) {
525 if( ref($feat) !~ /ARRAY/i ) {
526 if( ref($feat) && $feat->isa('Bio::SeqFeatureI') ) {
527 $self->add_SeqFeature($feat);
528 } else {
529 $self->warn("Must specify a valid Bio::SeqFeatureI or ArrayRef of Bio::SeqFeatureI's with the -features init parameter for ".ref($self));
531 } else {
532 foreach my $feature ( @$feat ) {
533 $self->add_SeqFeature($feature);
539 return $self;
542 =head1 PrimarySeq interface
545 The PrimarySeq interface provides the basic sequence getting
546 and setting methods for on all sequences.
548 These methods implement the Bio::PrimarySeq interface by delegating
549 to the primary_seq inside the object. This means that you
550 can use a Seq object wherever there is a PrimarySeq, and
551 of course, you are free to use these functions anyway.
553 =cut
555 =head2 seq
557 Title : seq
558 Usage : $string = $obj->seq()
559 Function: Returns the sequence as a string of letters. The
560 case of the letters is left up to the implementer.
561 Suggested cases are upper case for proteins and lower case for
562 DNA sequence (IUPAC standard),
563 but implementations are suggested to keep an open mind about
564 case (some users... want mixed case!)
565 Returns : A scalar
566 Args : None
568 =cut
570 sub seq {
571 return shift->primary_seq()->seq(@_);
574 =head2 validate_seq
576 Title : validate_seq
577 Usage : if(! $seq->validate_seq($seq_str) ) {
578 print "sequence $seq_str is not valid for an object of type ",
579 ref($seq), "\n";
581 Function: Validates a given sequence string. A validating sequence string
582 must be accepted by seq(). A string that does not validate will
583 lead to an exception if passed to seq().
585 The implementation provided here does not take alphabet() into
586 account. Allowed are all letters (A-Z) and '-','.', and '*'.
588 Example :
589 Returns : 1 if the supplied sequence string is valid for the object, and
590 0 otherwise.
591 Args : The sequence string to be validated.
594 =cut
596 sub validate_seq {
597 return shift->primary_seq()->validate_seq(@_);
600 =head2 length
602 Title : length
603 Usage : $len = $seq->length()
604 Function:
605 Example :
606 Returns : Integer representing the length of the sequence.
607 Args : None
609 =cut
611 sub length {
612 return shift->primary_seq()->length(@_);
615 =head1 Methods from the Bio::PrimarySeqI interface
617 =cut
619 =head2 subseq
621 Title : subseq
622 Usage : $substring = $obj->subseq(10,40);
623 Function: Returns the subseq from start to end, where the first base
624 is 1 and the number is inclusive, ie 1-2 are the first two
625 bases of the sequence
627 Start cannot be larger than end but can be equal
629 Returns : A string
630 Args : 2 integers
633 =cut
635 sub subseq {
636 return shift->primary_seq()->subseq(@_);
639 =head2 display_id
641 Title : display_id
642 Usage : $id = $obj->display_id or $obj->display_id($newid);
643 Function: Gets or sets the display id, also known as the common name of
644 the Seq object.
646 The semantics of this is that it is the most likely string
647 to be used as an identifier of the sequence, and likely to
648 have "human" readability. The id is equivalent to the LOCUS
649 field of the GenBank/EMBL databanks and the ID field of the
650 Swissprot/sptrembl database. In fasta format, the >(\S+) is
651 presumed to be the id, though some people overload the id
652 to embed other information. Bioperl does not use any
653 embedded information in the ID field, and people are
654 encouraged to use other mechanisms (accession field for
655 example, or extending the sequence object) to solve this.
657 Notice that $seq->id() maps to this function, mainly for
658 legacy/convenience issues.
659 Returns : A string
660 Args : None or a new id
663 =cut
665 sub display_id {
666 return shift->primary_seq->display_id(@_);
671 =head2 accession_number
673 Title : accession_number
674 Usage : $unique_biological_key = $obj->accession_number;
675 Function: Returns the unique biological id for a sequence, commonly
676 called the accession_number. For sequences from established
677 databases, the implementors should try to use the correct
678 accession number. Notice that primary_id() provides the
679 unique id for the implemetation, allowing multiple objects
680 to have the same accession number in a particular implementation.
682 For sequences with no accession number, this method should return
683 "unknown".
685 Can also be used to set the accession number.
686 Example : $key = $seq->accession_number or $seq->accession_number($key)
687 Returns : A string
688 Args : None or an accession number
691 =cut
693 sub accession_number {
694 return shift->primary_seq->accession_number(@_);
697 =head2 desc
699 Title : desc
700 Usage : $seqobj->desc($string) or $seqobj->desc()
701 Function: Sets or gets the description of the sequence
702 Example :
703 Returns : The description
704 Args : The description or none
707 =cut
709 sub desc {
710 return shift->primary_seq->desc(@_);
713 =head2 primary_id
715 Title : primary_id
716 Usage : $unique_implementation_key = $obj->primary_id;
717 Function: Returns the unique id for this object in this
718 implementation. This allows implementations to manage
719 their own object ids in a way the implementation can control
720 clients can expect one id to map to one object.
722 For sequences with no natural id, this method should return
723 a stringified memory location.
725 Can also be used to set the primary_id.
727 Also notice that this method is not delegated to the
728 internal Bio::PrimarySeq object
730 [Note this method name is likely to change in 1.3]
732 Example : $id = $seq->primary_id or $seq->primary_id($id)
733 Returns : A string
734 Args : None or an id
737 =cut
739 sub primary_id {
740 my ($obj,$value) = @_;
742 if( defined $value) {
743 $obj->{'primary_id'} = $value;
745 if( ! exists $obj->{'primary_id'} ) {
746 return "$obj";
748 return $obj->{'primary_id'};
751 =head2 can_call_new
753 Title : can_call_new
754 Usage : if ( $obj->can_call_new ) {
755 $newobj = $obj->new( %param );
757 Function: can_call_new returns 1 or 0 depending
758 on whether an implementation allows new
759 constructor to be called. If a new constructor
760 is allowed, then it should take the followed hashed
761 constructor list.
763 $myobject->new( -seq => $sequence_as_string,
764 -display_id => $id
765 -accession_number => $accession
766 -alphabet => 'dna',
768 Example :
769 Returns : 1 or 0
770 Args : None
773 =cut
775 sub can_call_new {
776 return 1;
779 =head2 alphabet
781 Title : alphabet
782 Usage : if ( $obj->alphabet eq 'dna' ) { /Do Something/ }
783 Function: Returns the type of sequence being one of
784 'dna', 'rna' or 'protein'. This is case sensitive.
786 This is not called <type> because this would cause
787 upgrade problems from the 0.5 and earlier Seq objects.
789 Returns : A string either 'dna','rna','protein'. NB - the object must
790 make a call of the type - if there is no type specified it
791 has to guess.
792 Args : None
795 =cut
797 sub alphabet {
798 my $self = shift;
799 return $self->primary_seq->alphabet(@_) if @_ && defined $_[0];
800 return $self->primary_seq->alphabet();
803 =head1 Methods for Bio::IdentifiableI compliance
805 =cut
807 =head2 object_id
809 Title : object_id
810 Usage : $string = $obj->object_id()
811 Function: a string which represents the stable primary identifier
812 in this namespace of this object. For DNA sequences this
813 is its accession_number, similarly for protein sequences
815 This is aliased to accession_number().
816 Returns : A scalar
819 =cut
821 sub object_id {
822 return shift->accession_number(@_);
825 =head2 version
827 Title : version
828 Usage : $version = $obj->version()
829 Function: a number which differentiates between versions of
830 the same object. Higher numbers are considered to be
831 later and more relevant, but a single object described
832 the same identifier should represent the same concept
834 Returns : A number
836 =cut
838 sub version{
839 return shift->primary_seq->version(@_);
843 =head2 authority
845 Title : authority
846 Usage : $authority = $obj->authority()
847 Function: a string which represents the organisation which
848 granted the namespace, written as the DNS name for
849 organisation (eg, wormbase.org)
851 Returns : A scalar
853 =cut
855 sub authority {
856 return shift->primary_seq()->authority(@_);
859 =head2 namespace
861 Title : namespace
862 Usage : $string = $obj->namespace()
863 Function: A string representing the name space this identifier
864 is valid in, often the database name or the name
865 describing the collection
867 Returns : A scalar
870 =cut
872 sub namespace{
873 return shift->primary_seq()->namespace(@_);
876 =head1 Methods for Bio::DescribableI compliance
878 =cut
880 =head2 display_name
882 Title : display_name
883 Usage : $string = $obj->display_name()
884 Function: A string which is what should be displayed to the user
885 the string should have no spaces (ideally, though a cautious
886 user of this interface would not assumme this) and should be
887 less than thirty characters (though again, double checking
888 this is a good idea)
890 This is aliased to display_id().
891 Returns : A scalar
893 =cut
895 sub display_name {
896 return shift->display_id(@_);
899 =head2 description
901 Title : description
902 Usage : $string = $obj->description()
903 Function: A text string suitable for displaying to the user a
904 description. This string is likely to have spaces, but
905 should not have any newlines or formatting - just plain
906 text. The string should not be greater than 255 characters
907 and clients can feel justified at truncating strings at 255
908 characters for the purposes of display
910 This is aliased to desc().
911 Returns : A scalar
913 =cut
915 sub description {
916 return shift->desc(@_);
919 =head1 Methods for implementing Bio::AnnotatableI
921 =cut
923 =head2 annotation
925 Title : annotation
926 Usage : $ann = $seq->annotation or $seq->annotation($annotation)
927 Function: Gets or sets the annotation
928 Returns : L<Bio::AnnotationCollectionI> object
929 Args : None or L<Bio::AnnotationCollectionI> object
931 See L<Bio::AnnotationCollectionI> and L<Bio::Annotation::Collection>
932 for more information
934 =cut
936 sub annotation {
937 my ($obj,$value) = @_;
938 if( defined $value ) {
939 $obj->throw("object of class ".ref($value)." does not implement ".
940 "Bio::AnnotationCollectionI. Too bad.")
941 unless $value->isa("Bio::AnnotationCollectionI");
942 $obj->{'_annotation'} = $value;
943 } elsif( ! defined $obj->{'_annotation'}) {
944 $obj->{'_annotation'} = Bio::Annotation::Collection->new();
946 return $obj->{'_annotation'};
949 =head1 Methods to implement Bio::FeatureHolderI
951 This includes methods for retrieving, adding, and removing features.
953 =cut
955 =head2 get_SeqFeatures
957 Title : get_SeqFeatures
958 Usage :
959 Function: Get the feature objects held by this feature holder.
961 Features which are not top-level are subfeatures of one or
962 more of the returned feature objects, which means that you
963 must traverse the subfeature arrays of each top-level
964 feature object in order to traverse all features associated
965 with this sequence.
967 Use get_all_SeqFeatures() if you want the feature tree
968 flattened into one single array.
970 Example :
971 Returns : an array of Bio::SeqFeatureI implementing objects
972 Args : none
974 At some day we may want to expand this method to allow for a feature
975 filter to be passed in.
977 =cut
979 sub get_SeqFeatures{
980 my $self = shift;
982 if( !defined $self->{'_as_feat'} ) {
983 $self->{'_as_feat'} = [];
986 return @{$self->{'_as_feat'}};
989 =head2 get_all_SeqFeatures
991 Title : get_all_SeqFeatures
992 Usage : @feat_ary = $seq->get_all_SeqFeatures();
993 Function: Returns the tree of feature objects attached to this
994 sequence object flattened into one single array. Top-level
995 features will still contain their subfeature-arrays, which
996 means that you will encounter subfeatures twice if you
997 traverse the subfeature tree of the returned objects.
999 Use get_SeqFeatures() if you want the array to contain only
1000 the top-level features.
1002 Returns : An array of Bio::SeqFeatureI implementing objects.
1003 Args : None
1006 =cut
1008 # this implementation is inherited from FeatureHolderI
1010 =head2 feature_count
1012 Title : feature_count
1013 Usage : $seq->feature_count()
1014 Function: Return the number of SeqFeatures attached to a sequence
1015 Returns : integer representing the number of SeqFeatures
1016 Args : None
1019 =cut
1021 sub feature_count {
1022 my ($self) = @_;
1024 if (defined($self->{'_as_feat'})) {
1025 return ($#{$self->{'_as_feat'}} + 1);
1026 } else {
1027 return 0;
1031 =head2 add_SeqFeature
1033 Title : add_SeqFeature
1034 Usage : $seq->add_SeqFeature($feat);
1035 $seq->add_SeqFeature(@feat);
1036 Function: Adds the given feature object (or each of an array of feature
1037 objects to the feature array of this
1038 sequence. The object passed is required to implement the
1039 Bio::SeqFeatureI interface.
1040 Returns : 1 on success
1041 Args : A Bio::SeqFeatureI implementing object, or an array of such objects.
1042 Throws : Bio::Root::BadParameter if any of the supplied arguments do
1043 not derive from Bio::SeqFeatureI.
1046 =cut
1048 sub add_SeqFeature {
1049 my ($self,@feat) = @_;
1051 $self->{'_as_feat'} = [] unless $self->{'_as_feat'};
1053 foreach my $feat ( @feat ) {
1054 if( !$feat->isa("Bio::SeqFeatureI") ) {
1055 $self->throw(-class => 'Bio::Root::BadParameter',
1056 -text =>"$feat is not a Bio::SeqFeatureI and that's what we expect...",
1057 -value => $feat);
1060 # make sure we attach ourselves to the feature if the feature wants it
1061 my $aseq = $self->primary_seq;
1062 $feat->attach_seq($aseq) if $aseq;
1064 push(@{$self->{'_as_feat'}},$feat);
1066 return 1;
1069 =head2 remove_SeqFeatures
1071 Title : remove_SeqFeatures
1072 Usage : $seq->remove_SeqFeatures();
1073 Function: Flushes all attached SeqFeatureI objects.
1075 To remove individual feature objects, delete those from the returned
1076 array and re-add the rest.
1077 Example :
1078 Returns : The array of Bio::SeqFeatureI objects removed from this seq.
1079 Args : None
1082 =cut
1084 sub remove_SeqFeatures {
1085 my $self = shift;
1087 return () unless $self->{'_as_feat'};
1088 my @feats = @{$self->{'_as_feat'}};
1089 $self->{'_as_feat'} = [];
1090 return @feats;
1093 =head1 Methods provided in the Bio::PrimarySeqI interface
1096 These methods are inherited from the PrimarySeq interface
1097 and work as one expects, building new Bio::Seq objects
1098 or other information as expected. See L<Bio::PrimarySeq>
1099 for more information.
1101 Sequence Features are B<not> transfered to the new objects.
1102 This is possibly a mistake. Anyone who feels the urge in
1103 dealing with this is welcome to give it a go.
1105 =head2 revcom
1107 Title : revcom
1108 Usage : $rev = $seq->revcom()
1109 Function: Produces a new Bio::Seq object which
1110 is the reversed complement of the sequence. For protein
1111 sequences this throws an exception of "Sequence is a protein.
1112 Cannot revcom"
1114 The id is the same id as the original sequence, and the
1115 accession number is also identical. If someone wants to track
1116 that this sequence has be reversed, it needs to define its own
1117 extensions
1119 To do an in-place edit of an object you can go:
1121 $seq = $seq->revcom();
1123 This of course, causes Perl to handle the garbage collection of
1124 the old object, but it is roughly speaking as efficient as an
1125 in-place edit.
1127 Returns : A new (fresh) Bio::Seq object
1128 Args : None
1131 =cut
1133 =head2 trunc
1135 Title : trunc
1136 Usage : $subseq = $myseq->trunc(10,100);
1137 Function: Provides a truncation of a sequence
1139 Example :
1140 Returns : A fresh Seq object
1141 Args : A Seq object
1144 =cut
1146 =head2 id
1148 Title : id
1149 Usage : $id = $seq->id()
1150 Function: This is mapped on display_id
1151 Returns : value of display_id()
1152 Args : [optional] value to update display_id
1155 =cut
1157 sub id {
1158 return shift->display_id(@_);
1162 =head1 Seq only methods
1165 These methods are specific to the Bio::Seq object, and not
1166 found on the Bio::PrimarySeq object
1168 =head2 primary_seq
1170 Title : primary_seq
1171 Usage : $seq->primary_seq or $seq->primary_seq($newval)
1172 Function: Get or set a PrimarySeq object
1173 Example :
1174 Returns : PrimarySeq object
1175 Args : None or PrimarySeq object
1176 Throws : Bio::Root::BadParameter if the supplied argument does
1177 not derive from Bio::PrimarySeqI.
1180 =cut
1182 sub primary_seq {
1183 my ($obj,$value) = @_;
1185 if( defined $value) {
1186 if( ! ref $value || ! $value->isa('Bio::PrimarySeqI') ) {
1187 $obj->throw(-class => 'Bio::Root::BadParameter',
1188 -text => "$value is not a Bio::PrimarySeqI compliant object",
1189 -value => $value );
1192 $obj->{'primary_seq'} = $value;
1193 # descend down over all seqfeature objects, seeing whether they
1194 # want an attached seq.
1196 foreach my $sf ( $obj->get_SeqFeatures() ) {
1197 $sf->attach_seq($value);
1201 return $obj->{'primary_seq'};
1205 =head2 species
1207 Title : species
1208 Usage : $species = $seq->species() or $seq->species($species)
1209 Function: Gets or sets the species
1210 Returns : L<Bio::Species> object
1211 Args : None or L<Bio::Species> object
1213 See L<Bio::Species> for more information
1215 =cut
1217 sub species {
1218 my ($self, $species) = @_;
1219 if ($species) {
1220 $self->{'species'} = $species;
1221 } else {
1222 return $self->{'species'};
1226 =head1 Internal methods
1228 =cut
1230 # keep AUTOLOAD happy
1231 sub DESTROY { }
1233 ############################################################################
1234 # aliases due to name changes or to compensate for our lack of consistency #
1235 ############################################################################
1237 # in all other modules we use the object in the singular --
1238 # lack of consistency sucks
1239 *flush_SeqFeature = \&remove_SeqFeatures;
1240 *flush_SeqFeatures = \&remove_SeqFeatures;
1242 # this is now get_SeqFeatures() (from FeatureHolderI)
1243 *top_SeqFeatures = \&get_SeqFeatures;
1245 # this is now get_all_SeqFeatures() in FeatureHolderI
1246 sub all_SeqFeatures{
1247 return shift->get_all_SeqFeatures(@_);
1250 sub accession {
1251 my $self = shift;
1252 $self->warn(ref($self)."::accession is deprecated, ".
1253 "use accession_number() instead");
1254 return $self->accession_number(@_);