Bio/Annotation/AnnotationFactory.pm

filter: Statementstmt 84.7% 61 / 72 Branchbran 72.2% 26 / 36 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 6 / 6 Podpod 100% 3 / 3 totaltotal 82.7% 101 / 122 CC 20 SCAR 20.0
CC
20
Cov
81%
_guess_type
11.6
type
6.1
create_object
5.3
CRAP 7.4
« Bio/AlignIO/mase.pm ↑ Summary Bio/SearchIO/SearchWriterI.pm »
1 #
2 # BioPerl module for Bio::Annotation::AnnotationFactory
3 #
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
5 #
6 # Cared for by Hilmar Lapp <hlapp at gmx.net>
7 #
8 # Copyright Hilmar Lapp
9 #
10 # You may distribute this module under the same terms as perl itself
11  
12 #
13 # (c) Hilmar Lapp, hlapp at gmx.net, 2002.
14 # (c) GNF, Genomics Institute of the Novartis Research Foundation, 2002.
15 #
16 # You may distribute this module under the same terms as perl itself.
17 # Refer to the Perl Artistic License (see the license accompanying this
18 # software package, or see http://www.perl.com/language/misc/Artistic.html)
19 # for the terms under which you may use, modify, and redistribute this module.
20 #
21 # THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
22 # WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
23 # MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
24 #
25  
26 # POD documentation - main docs before the code
27  
28 =head1 NAME
29  
30 Bio::Annotation::AnnotationFactory - Instantiates a new
31 Bio::AnnotationI (or derived class) through a factory
32  
33 =head1 SYNOPSIS
34  
35 use Bio::Annotation::AnnotationFactory;
36 #
37 my $factory = Bio::Annotation::AnnotationFactory->new(
38 -type => 'Bio::Annotation::SimpleValue');
39 my $ann = $factory->create_object(-value => 'peroxisome',
40 -tagname => 'cellular component');
41  
42  
43 =head1 DESCRIPTION
44  
45 This object will build L<Bio::AnnotationI> objects generically.
46  
47 =head1 FEEDBACK
48  
49 =head2 Mailing Lists
50  
51 User feedback is an integral part of the evolution of this and other
52 Bioperl modules. Send your comments and suggestions preferably to
53 the Bioperl mailing list. Your participation is much appreciated.
54  
55 bioperl-l@bioperl.org - General discussion
56 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
57  
58 =head2 Support
59  
60 Please direct usage questions or support issues to the mailing list:
61  
62 I<bioperl-l@bioperl.org>
63  
64 rather than to the module maintainer directly. Many experienced and
65 reponsive experts will be able look at the problem and quickly
66 address it. Please include a thorough description of the problem
67 with code and data examples if at all possible.
68  
69 =head2 Reporting Bugs
70  
71 Report bugs to the Bioperl bug tracking system to help us keep track
72 of the bugs and their resolution. Bug reports can be submitted via
73 the web:
74  
75 https://github.com/bioperl/bioperl-live/issues
76  
77 =head1 AUTHOR - Hilmar Lapp
78  
79 Email hlapp at gmx.net
80  
81  
82 =head1 CONTRIBUTORS
83  
84 This is mostly copy-and-paste with subsequent adaptation from
85 Bio::Seq::SeqFactory by Jason Stajich. Most credits should in fact go
86 to him.
87  
88 =head1 APPENDIX
89  
90 The rest of the documentation details each of the object methods.
91 Internal methods are usually preceded with a _
92  
93 =cut
94  
95  
96 # Let the code begin...
97  
98  
99 package Bio::Annotation::AnnotationFactory;
100 5 $Bio::Annotation::AnnotationFactory::VERSION = '1.7.8';
101 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
102  
103  
104 5 ▶use base qw(Bio::Root::Root Bio::Factory::ObjectFactoryI);
Subroutine BEGIN: called CC=1 SCAR=0.0
105  
106 =head2 new
107  
108 Title : new
109 Usage : my $obj = Bio::Annotation::AnnotationFactory->new();
110 Function: Builds a new Bio::Annotation::AnnotationFactory object
111 Returns : Bio::Annotation::AnnotationFactory
112 Args : -type => string, name of a L<Bio::AnnotationI> derived class.
113  
114 If type is not set the module guesses it based on arguments passed to
115 method L<create_object>.
116  
117 =cut
118  
119 14 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
120 14 my($class,@args) = @_;
121  
122 14 my $self = $class->SUPER::new(@args);
123
124 14 my ($type) = $self->_rearrange([qw(TYPE)], @args);
125  
126 14 $self->{'_loaded_types'} = {};
127 14 ▶ $self->type($type) if $type;
Branch: 1 branch
BranchTrueFalseTotal
if $type 8 6 14
128  
129 14 return $self;
130 }
131  
132  
133 =head2 create_object
134  
135 Title : create_object
136 Usage : my $seq = $factory->create_object(<named parameters>);
137 Function: Instantiates new Bio::AnnotationI (or one of its child classes)
138  
139 This object allows us to genericize the instantiation of
140 cluster objects.
141  
142 Returns : L<Bio::AnnotationI> compliant object
143 The return type is configurable using new(-type =>"...").
144 Args : initialization parameters specific to the type of annotation
145 object we want.
146  
147 =cut
148  
149 62 ▶sub create_object {
Subroutine create_object: called CC=5 SCAR=16.7
Pod documented
150 62 my ($self,@args) = @_;
151  
152 62 my $type = $self->type;
153 62 ▶ if(! $type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type) 7 55 62
154 # we need to guess this
155 7 $type = $self->_guess_type(@args);
156 7 ▶ if(! $type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type) 0 7 7
157 0 $self->throw("No annotation type set and unable to guess.");
158 }
159 # load dynamically if it hasn't been loaded yet
160 7 ▶ if(! $self->{'_loaded_types'}->{$type}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_loaded_types"}{$type}) 7 0 7
161 7 eval {
162 7 $self->_load_module($type);
163 7 $self->{'_loaded_types'}->{$type} = 1;
164 };
165 7 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 7 7
166 0 $self->throw("Bio::AnnotationI implementation $type ".
167 "failed to load: ".$@);
168 }
169 }
170 }
171 62 return $type->new(-verbose => $self->verbose, @args);
172 }
173  
174 =head2 type
175  
176 Title : type
177 Usage : $obj->type($newval)
178 Function: Get/set the type of L<Bio::AnnotationI> object to be created.
179  
180 This may be changed at any time during the lifetime of this
181 factory.
182  
183 Returns : value of type
184 Args : newvalue (optional)
185  
186  
187 =cut
188  
189 89 ▶sub type{
Subroutine type: called CC=6 SCAR=18.2
Pod documented
190 89 my $self = shift;
191  
192 89 ▶ if(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 27 62 89
193 27 my $type = shift;
194 27 ▶ if($type && (! $self->{'_loaded_types'}->{$type})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type and not $self->{"_loaded_types"}{$type}) 17 10 27
Condition100%3/3
$type and not $self->{"_loaded_types"}{$type}
!ll&&!rl&&r
1917
MC/DC100%2/2
$type and not $self->{"_loaded_types"}{$type}
$type not $self->{"_loaded_types"}{$type}
Truth table100%3/3
A and not(B)
A= $typeB= not $self->{"_loaded_types"}{$type}
ABresult
0X0
100
111
195 17 eval {
196 17 $self->_load_module($type);
197 };
198 17 ▶ if( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 17 17
199 0 $self->throw("Annotation class '$type' failed to load: ".
200 $@);
201 }
202 17 my $a = bless {},$type;
203 17 ▶ if( ! $a->isa('Bio::AnnotationI') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($a->isa("Bio::AnnotationI")) 0 17 17
204 0 $self->throw("'$type' does not implement Bio::AnnotationI. ".
205 "Too bad.");
206 }
207 17 $self->{'_loaded_types'}->{$type} = 1;
208 }
209 27 return $self->{'type'} = $type;
210 }
211 62 return $self->{'type'};
212 }
213  
214 =head2 _guess_type
215  
216 Title : _guess_type
217 Usage :
218 Function: Guesses the right type of L<Bio::AnnotationI> implementation
219 based on initialization parameters for the prospective
220 object.
221 Example :
222 Returns : the type (a string, the module name)
223 Args : initialization parameters to be passed to the prospective
224 cluster object
225  
226  
227 =cut
228  
229 7 ▶sub _guess_type{
Subroutine _guess_type: called CC=10 SCAR=24.5
230 7 my ($self,@args) = @_;
231 7 my $type;
232  
233 # we can only guess from a certain number of arguments
234 7 my ($val, $db, $text, $name, $authors, $start, $tree, $node) =
235 $self->_rearrange([qw(VALUE
236 DATABASE
237 TEXT
238 NAME
239 AUTHORS
240 START
241 TREE_OBJ
242 NODE
243 )], @args);
244 SWITCH: {
245 7 ▶ $val && do { $type = ref($val) ? "TagTree" : "SimpleValue"; last SWITCH; };
Branch: 2 branches
BranchTrueFalseTotal
$val and do { $type = ref $val ? "TagTree" : "SimpleValue"; last SWITCH } 2 5 7
ref $val ? : 1 1 2
246 0 ▶ $authors && do { $type = "Reference"; last SWITCH; };
Branch: 1 branch
BranchTrueFalseTotal
$authors and do { $type = "Reference"; last SWITCH } 0 5 5
247 0 ▶ $db && do { $type = "DBLink"; last SWITCH; };
Branch: 1 branch
BranchTrueFalseTotal
$db and do { $type = "DBLink"; last SWITCH } 0 5 5
248 5 ▶ $text && do { $type = "Comment"; last SWITCH; };
Branch: 1 branch
BranchTrueFalseTotal
$text and do { $type = "Comment"; last SWITCH } 2 3 5
249 3 ▶ $name && do { $type = "OntologyTerm"; last SWITCH; };
Branch: 1 branch
BranchTrueFalseTotal
$name and do { $type = "OntologyTerm"; last SWITCH } 1 2 3
250 2 ▶ $start && do { $type = "Target"; last SWITCH; };
Branch: 1 branch
BranchTrueFalseTotal
$start and do { $type = "Target"; last SWITCH } 1 1 2
251 1 ▶ $tree && do { $type = "Tree"; last SWITCH; };
Branch: 1 branch
BranchTrueFalseTotal
$tree and do { $type = "Tree"; last SWITCH } 1 0 1
252 0 ▶ $node && do { $type = "TagTree"; last SWITCH; };
Branch: 1 branch
BranchTrueFalseTotal
$node and do { $type = "TagTree"; last SWITCH } 0 0 0
253 # what else could we look for?
254 }
255 7 $type = "Bio::Annotation::".$type;
256  
257 7 return $type;
258 }
259  
260 #####################################################################
261 # aliases for naming consistency or other reasons #
262 #####################################################################
263  
264 5 *create = \&create_object;
265  
266 5 1;
« Bio/AlignIO/mase.pm ↑ Summary Bio/SearchIO/SearchWriterI.pm »