sync from trunk
[bioperl-live.git] / Bio / FeatureHolderI.pm
blob71372aef7e99b5b5ebcf7d5e746bf18dee5d99c4
1 # $Id$
3 # BioPerl module for Bio::FeatureHolderI
5 # Cared for by Hilmar Lapp <hlapp at gmx.net>
7 # Copyright Hilmar Lapp
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::FeatureHolderI - the base interface an object with features must implement
17 =head1 SYNOPSIS
19 use Bio::SeqIO;
20 # get a feature-holding object somehow: for example, Bio::SeqI objects
21 # have features
22 my $seqio = Bio::SeqIO->new(-fh => \*STDIN, -format => 'genbank');
23 while (my $seq = $seqio->next_seq()) {
24 # $seq is-a Bio::FeatureHolderI, hence:
25 my @feas = $seq->get_SeqFeatures();
26 # each element is-a Bio::SeqFeatureI
27 foreach my $fea (@feas) {
28 # do something with the feature objects
32 =head1 DESCRIPTION
34 This is the base interface that all feature-holding objects must
35 implement.
37 Popular feature-holders are for instance L<Bio::Seq> objects. Since
38 L<Bio::SeqFeatureI> defines a sub_SeqFeature() method, most
39 Bio::SeqFeatureI implementations like L<Bio::SeqFeature::Generic> will
40 implement the feature holder interface as well.
42 =head1 FEEDBACK
44 =head2 Mailing Lists
46 User feedback is an integral part of the evolution of this and other
47 Bioperl modules. Send your comments and suggestions preferably to
48 the Bioperl mailing list. Your participation is much appreciated.
50 bioperl-l@bioperl.org - General discussion
51 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
53 =head2 Reporting Bugs
55 Report bugs to the Bioperl bug tracking system to help us keep track
56 of the bugs and their resolution. Bug reports can be submitted via the
57 web:
59 http://bugzilla.open-bio.org/
61 =head1 AUTHOR - Hilmar Lapp
63 Email hlapp at gmx.net
65 =head1 CONTRIBUTORS
67 Steffen Grossmann [SG], grossman-at-molgen.mpg.de
69 =head1 APPENDIX
71 The rest of the documentation details each of the object methods.
72 Internal methods are usually preceded with a _
74 =cut
77 # Let the code begin...
81 package Bio::FeatureHolderI;
82 use strict;
83 use Carp;
85 use base qw(Bio::Root::RootI);
87 =head2 get_SeqFeatures()
89 Usage :
90 Function: Get the feature objects held by this feature holder.
91 Example :
92 Returns : an array of Bio::SeqFeatureI implementing objects
93 Args : none
95 At some day we may want to expand this method to allow for a feature
96 filter to be passed in.
98 =cut
100 sub get_SeqFeatures {
101 shift->throw_not_implemented();
104 =head2 add_SeqFeature()
106 Usage : $feat->add_SeqFeature($subfeat);
107 $feat->add_SeqFeature($subfeat,'EXPAND')
108 Function: adds a SeqFeature into the subSeqFeature array.
109 with no 'EXPAND' qualifer, subfeat will be tested
110 as to whether it lies inside the parent, and throw
111 an exception if not.
113 If EXPAND is used, the parent''s start/end/strand will
114 be adjusted so that it grows to accommodate the new
115 subFeature
116 Example :
117 Returns : nothing
118 Args : a Bio::SeqFeatureI object
120 =cut
122 sub add_SeqFeature {
123 shift->throw_not_implemented();
127 =head2 remove_SeqFeatures()
129 Usage : $obj->remove_SeqFeatures
130 Function: Removes all sub SeqFeatures. If you want to remove only a subset,
131 remove that subset from the returned array, and add back the rest.
132 Returns : The array of Bio::SeqFeatureI implementing sub-features that was
133 deleted from this feature.
134 Args : none
136 =cut
138 sub remove_SeqFeatures {
139 shift->throw_not_implemented();
142 =head2 feature_count
144 Title : feature_count
145 Usage : $obj->feature_count()
146 Function: Return the number of SeqFeatures attached to a feature holder.
148 This is before flattening a possible sub-feature tree.
150 We provide a default implementation here that just counts
151 the number of objects returned by get_SeqFeatures().
152 Implementors may want to override this with a more
153 efficient implementation.
155 Returns : integer representing the number of SeqFeatures
156 Args : None
158 At some day we may want to expand this method to allow for a feature
159 filter to be passed in.
161 Our default implementation allows for any number of additional
162 arguments and will pass them on to get_SeqFeatures(). I.e., in order to
163 support filter arguments, just support them in get_SeqFeatures().
165 =cut
167 sub feature_count {
168 return scalar(shift->get_SeqFeatures(@_));
171 =head2 get_all_SeqFeatures
173 Title : get_all_SeqFeatures
174 Usage :
175 Function: Get the flattened tree of feature objects held by this
176 feature holder. The difference to get_SeqFeatures is that
177 the entire tree of sub-features will be flattened out.
179 We provide a default implementation here, so implementors
180 don''t necessarily need to implement this method.
182 Example :
183 Returns : an array of Bio::SeqFeatureI implementing objects
184 Args : none
186 At some day we may want to expand this method to allow for a feature
187 filter to be passed in.
189 Our default implementation allows for any number of additional
190 arguments and will pass them on to any invocation of
191 get_SeqFeatures(), wherever a component of the tree implements
192 FeatureHolderI. I.e., in order to support filter arguments, just
193 support them in get_SeqFeatures().
195 =cut
197 sub get_all_SeqFeatures{
198 my $self = shift;
199 my @flatarr;
201 foreach my $feat ( $self->get_SeqFeatures(@_) ){
202 push(@flatarr,$feat);
203 &_add_flattened_SeqFeatures(\@flatarr,$feat,@_);
206 # needed to deal with subfeatures which appear more than once in the hierarchy [SG]
207 my %seen = ();
208 my @uniq_flatarr = ();
209 foreach my $feat (@flatarr) {
210 push(@uniq_flatarr, $feat) unless $seen{$feat}++;
212 return @uniq_flatarr;
215 sub _add_flattened_SeqFeatures {
216 my ($arrayref,$feat,@args) = @_;
217 my @subs = ();
219 if($feat->isa("Bio::FeatureHolderI")) {
220 @subs = $feat->get_SeqFeatures(@args);
221 } elsif($feat->isa("Bio::SeqFeatureI")) {
222 @subs = $feat->sub_SeqFeature();
223 } else {
224 confess ref($feat)." is neither a FeatureHolderI nor a SeqFeatureI. ".
225 "Don't know how to flatten.";
227 foreach my $sub (@subs) {
228 push(@$arrayref,$sub);
229 &_add_flattened_SeqFeatures($arrayref,$sub);
234 sub set_ParentIDs_from_hierarchy(){
235 # DEPRECATED - use IDHandler
236 my $self = shift;
237 require "Bio/SeqFeature/Tools/IDHandler.pm";
238 Bio::SeqFeature::Tools::IDHandler->new->set_ParentIDs_from_hierarchy($self);
241 sub create_hierarchy_from_ParentIDs(){
242 # DEPRECATED - use IDHandler
243 my $self = shift;
244 require "Bio/SeqFeature/Tools/IDHandler.pm";
245 Bio::SeqFeature::Tools::IDHandler->new->create_hierarchy_from_ParentIDs($self);