comparison variant_effect_predictor/Bio/Annotation/AnnotationFactory.pm @ 0:21066c0abaf5 draft

Uploaded
author willmclaren
date Fri, 03 Aug 2012 10:04:48 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:21066c0abaf5
1 # $Id: AnnotationFactory.pm,v 1.1 2002/10/31 09:45:39 lapp Exp $
2 #
3 # BioPerl module for Bio::Annotation::AnnotationFactory
4 #
5 # Cared for by Hilmar Lapp <hlapp at gmx.net>
6 #
7 # Copyright Hilmar Lapp
8 #
9 # You may distribute this module under the same terms as perl itself
10
11 #
12 # (c) Hilmar Lapp, hlapp at gmx.net, 2002.
13 # (c) GNF, Genomics Institute of the Novartis Research Foundation, 2002.
14 #
15 # You may distribute this module under the same terms as perl itself.
16 # Refer to the Perl Artistic License (see the license accompanying this
17 # software package, or see http://www.perl.com/language/misc/Artistic.html)
18 # for the terms under which you may use, modify, and redistribute this module.
19 #
20 # THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
21 # WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
22 # MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 #
24
25 # POD documentation - main docs before the code
26
27 =head1 NAME
28
29 Bio::Annotation::AnnotationFactory - Instantiates a new Bio::AnnotationI (or derived class) through a factory
30
31 =head1 SYNOPSIS
32
33 use Bio::Annotation::AnnotationFactory;
34 #
35 my $factory = new Bio::Annotation::AnnotationFactory(-type => 'Bio::Annotation::SimpleValue');
36 my $ann = $factory->create_object(-value => 'peroxisome',
37 -tagname => 'cellular component');
38
39
40 =head1 DESCRIPTION
41
42 This object will build L<Bio::AnnotationI> objects generically.
43
44 =head1 FEEDBACK
45
46 =head2 Mailing Lists
47
48 User feedback is an integral part of the evolution of this and other
49 Bioperl modules. Send your comments and suggestions preferably to
50 the Bioperl mailing list. Your participation is much appreciated.
51
52 bioperl-l@bioperl.org - General discussion
53 http://bioperl.org/MailList.shtml - About the mailing lists
54
55 =head2 Reporting Bugs
56
57 Report bugs to the Bioperl bug tracking system to help us keep track
58 of the bugs and their resolution. Bug reports can be submitted via
59 email or the web:
60
61 bioperl-bugs@bioperl.org
62 http://bugzilla.bioperl.org/
63
64 =head1 AUTHOR - Hilmar Lapp
65
66 Email hlapp at gmx.net
67
68
69 =head1 CONTRIBUTORS
70
71 This is mostly copy-and-paste with subsequent adaptation from
72 Bio::Seq::SeqFactory by Jason Stajich. Most credits should in fact go
73 to him.
74
75 =head1 APPENDIX
76
77 The rest of the documentation details each of the object methods.
78 Internal methods are usually preceded with a _
79
80 =cut
81
82
83 # Let the code begin...
84
85
86 package Bio::Annotation::AnnotationFactory;
87 use vars qw(@ISA);
88 use strict;
89
90 use Bio::Root::Root;
91 use Bio::Factory::ObjectFactoryI;
92
93 @ISA = qw(Bio::Root::Root Bio::Factory::ObjectFactoryI);
94
95 =head2 new
96
97 Title : new
98 Usage : my $obj = new Bio::Annotation::AnnotationFactory();
99 Function: Builds a new Bio::Annotation::AnnotationFactory object
100 Returns : Bio::Annotation::AnnotationFactory
101 Args : -type => string, name of a L<Bio::AnnotationI> derived class.
102 The default is L<Bio::Ontology::Term>.
103
104 =cut
105
106 sub new {
107 my($class,@args) = @_;
108
109 my $self = $class->SUPER::new(@args);
110
111 my ($type) = $self->_rearrange([qw(TYPE)], @args);
112
113 $self->{'_loaded_types'} = {};
114 $self->type($type) if $type;
115
116 return $self;
117 }
118
119
120 =head2 create_object
121
122 Title : create_object
123 Usage : my $seq = $factory->create_object(<named parameters>);
124 Function: Instantiates new Bio::AnnotationI (or one of its child classes)
125
126 This object allows us to genericize the instantiation of
127 cluster objects.
128
129 Returns : L<Bio::AnnotationI> compliant object
130 The return type is configurable using new(-type =>"...").
131 Args : initialization parameters specific to the type of annotation
132 object we want.
133
134 =cut
135
136 sub create_object {
137 my ($self,@args) = @_;
138
139 my $type = $self->type();
140 if(! $type) {
141 # we need to guess this
142 $type = $self->_guess_type(@args);
143 if(! $type) {
144 $self->throw("No annotation type set and unable to guess.");
145 }
146 # load dynamically if it hasn't been loaded yet
147 if(! $self->{'_loaded_types'}->{$type}) {
148 eval {
149 $self->_load_module($type);
150 $self->{'_loaded_types'}->{$type} = 1;
151 };
152 if($@) {
153 $self->throw("Bio::AnnotationI implementation $type ".
154 "failed to load: ".$@);
155 }
156 }
157 }
158 return $type->new(-verbose => $self->verbose, @args);
159 }
160
161 =head2 type
162
163 Title : type
164 Usage : $obj->type($newval)
165 Function: Get/set the type of L<Bio::AnnotationI> object to be created.
166
167 This may be changed at any time during the lifetime of this
168 factory.
169
170 Returns : value of type
171 Args : newvalue (optional)
172
173
174 =cut
175
176 sub type{
177 my $self = shift;
178
179 if(@_) {
180 my $type = shift;
181 if($type && (! $self->{'_loaded_types'}->{$type})) {
182 eval {
183 $self->_load_module($type);
184 };
185 if( $@ ) {
186 $self->throw("Annotation class '$type' failed to load: ".
187 $@);
188 }
189 my $a = bless {},$type;
190 if( ! $a->isa('Bio::AnnotationI') ) {
191 $self->throw("'$type' does not implement Bio::AnnotationI. ".
192 "Too bad.");
193 }
194 $self->{'_loaded_types'}->{$type} = 1;
195 }
196 return $self->{'type'} = $type;
197 }
198 return $self->{'type'};
199 }
200
201 =head2 _guess_type
202
203 Title : _guess_type
204 Usage :
205 Function: Guesses the right type of L<Bio::AnnotationI> implementation
206 based on initialization parameters for the prospective
207 object.
208 Example :
209 Returns : the type (a string, the module name)
210 Args : initialization parameters to be passed to the prospective
211 cluster object
212
213
214 =cut
215
216 sub _guess_type{
217 my ($self,@args) = @_;
218 my $type;
219
220 # we can only guess from a certain number of arguments
221 my ($val,$db,$text,$name,$authors) =
222 $self->_rearrange([qw(VALUE
223 DATABASE
224 TEXT
225 NAME
226 AUTHORS
227 )], @args);
228 SWITCH: {
229 $val && do { $type = "SimpleValue"; last SWITCH; };
230 $authors && do { $type = "Reference"; last SWITCH; };
231 $db && do { $type = "DBLink"; last SWITCH; };
232 $text && do { $type = "Comment"; last SWITCH; };
233 $name && do { $type = "OntologyTerm"; last SWITCH; };
234 # what else could we look for?
235 }
236 $type = "Bio::Annotation::".$type;
237
238 return $type;
239 }
240
241 #####################################################################
242 # aliases for naming consistency or other reasons #
243 #####################################################################
244
245 *create = \&create_object;
246
247 1;