Mercurial > repos > mahtabm > ensembl
comparison variant_effect_predictor/Bio/Cluster/ClusterFactory.pm @ 0:1f6dce3d34e0
Uploaded
author | mahtabm |
---|---|
date | Thu, 11 Apr 2013 02:01:53 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:1f6dce3d34e0 |
---|---|
1 # $Id: ClusterFactory.pm,v 1.2 2002/10/31 09:45:39 lapp Exp $ | |
2 # | |
3 # BioPerl module for Bio::Cluster::ClusterFactory | |
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::Cluster::ClusterFactory - Instantiates a new Bio::ClusterI (or derived class) through a factory | |
30 | |
31 =head1 SYNOPSIS | |
32 | |
33 use Bio::Cluster::ClusterFactory; | |
34 # if you don't provide a default type, the factory will try | |
35 # some guesswork based on display_id and namespace | |
36 my $factory = new Bio::Cluster::ClusterFactory(-type => 'Bio::Cluster::UniGene'); | |
37 my $clu = $factory->create_object(-description => 'NAT', | |
38 -display_id => 'Hs.2'); | |
39 | |
40 | |
41 =head1 DESCRIPTION | |
42 | |
43 This object will build L<Bio::ClusterI> objects generically. | |
44 | |
45 =head1 FEEDBACK | |
46 | |
47 =head2 Mailing Lists | |
48 | |
49 User feedback is an integral part of the evolution of this and other | |
50 Bioperl modules. Send your comments and suggestions preferably to | |
51 the Bioperl mailing list. Your participation is much appreciated. | |
52 | |
53 bioperl-l@bioperl.org - General discussion | |
54 http://bioperl.org/MailList.shtml - About the mailing lists | |
55 | |
56 =head2 Reporting Bugs | |
57 | |
58 Report bugs to the Bioperl bug tracking system to help us keep track | |
59 of the bugs and their resolution. Bug reports can be submitted via | |
60 email or the web: | |
61 | |
62 bioperl-bugs@bioperl.org | |
63 http://bugzilla.bioperl.org/ | |
64 | |
65 =head1 AUTHOR - Hilmar Lapp | |
66 | |
67 Email hlapp at gmx.net | |
68 | |
69 | |
70 =head1 CONTRIBUTORS | |
71 | |
72 This is mostly copy-and-paste with subsequent adaptation from | |
73 Bio::Seq::SeqFactory by Jason Stajich. Most credits should in fact go | |
74 to him. | |
75 | |
76 =head1 APPENDIX | |
77 | |
78 The rest of the documentation details each of the object methods. | |
79 Internal methods are usually preceded with a _ | |
80 | |
81 =cut | |
82 | |
83 | |
84 # Let the code begin... | |
85 | |
86 | |
87 package Bio::Cluster::ClusterFactory; | |
88 use vars qw(@ISA); | |
89 use strict; | |
90 | |
91 use Bio::Root::Root; | |
92 use Bio::Factory::ObjectFactoryI; | |
93 | |
94 @ISA = qw(Bio::Root::Root Bio::Factory::ObjectFactoryI); | |
95 | |
96 =head2 new | |
97 | |
98 Title : new | |
99 Usage : my $obj = new Bio::Cluster::ClusterFactory(); | |
100 Function: Builds a new Bio::Cluster::ClusterFactory object | |
101 Returns : Bio::Cluster::ClusterFactory | |
102 Args : -type => string, name of a ClusterI derived class. | |
103 If not provided, the factory will have to guess | |
104 from ID and namespace, which may or may not be | |
105 successful. | |
106 | |
107 =cut | |
108 | |
109 sub new { | |
110 my($class,@args) = @_; | |
111 | |
112 my $self = $class->SUPER::new(@args); | |
113 | |
114 my ($type) = $self->_rearrange([qw(TYPE)], @args); | |
115 | |
116 $self->{'_loaded_types'} = {}; | |
117 $self->type($type) if $type; | |
118 | |
119 return $self; | |
120 } | |
121 | |
122 | |
123 =head2 create_object | |
124 | |
125 Title : create_object | |
126 Usage : my $seq = $factory->create_object(<named parameters>); | |
127 Function: Instantiates new Bio::ClusterI (or one of its child classes) | |
128 | |
129 This object allows us to genericize the instantiation of | |
130 cluster objects. | |
131 | |
132 Returns : L<Bio::ClusterI> compliant object | |
133 The return type is configurable using new(-type =>"..."). | |
134 Args : initialization parameters specific to the type of cluster | |
135 object we want. Typically | |
136 -display_id => $name | |
137 -description => description of the cluster | |
138 -members => arrayref, members of the cluster | |
139 | |
140 =cut | |
141 | |
142 sub create_object { | |
143 my ($self,@args) = @_; | |
144 | |
145 my $type = $self->type(); | |
146 if(! $type) { | |
147 # we need to guess this | |
148 $type = $self->_guess_type(@args); | |
149 if(! $type) { | |
150 $self->throw("No cluster type set and unable to guess."); | |
151 } | |
152 # load dynamically if it hasn't been loaded yet | |
153 if(! $self->{'_loaded_types'}->{$type}) { | |
154 eval { | |
155 $self->_load_module($type); | |
156 $self->{'_loaded_types'}->{$type} = 1; | |
157 }; | |
158 if($@) { | |
159 $self->throw("Bio::ClusterI implementation $type ". | |
160 "failed to load: ".$@); | |
161 } | |
162 } | |
163 } | |
164 return $type->new(-verbose => $self->verbose, @args); | |
165 } | |
166 | |
167 =head2 type | |
168 | |
169 Title : type | |
170 Usage : $obj->type($newval) | |
171 Function: Get/set the type of L<Bio::ClusterI> object to be created. | |
172 | |
173 This may be changed at any time during the lifetime of this | |
174 factory. | |
175 | |
176 Returns : value of type | |
177 Args : newvalue (optional) | |
178 | |
179 | |
180 =cut | |
181 | |
182 sub type{ | |
183 my $self = shift; | |
184 | |
185 if(@_) { | |
186 my $type = shift; | |
187 if($type && (! $self->{'_loaded_types'}->{$type})) { | |
188 eval { | |
189 $self->_load_module($type); | |
190 }; | |
191 if( $@ ) { | |
192 $self->throw("Cluster implementation '$type' failed to load: ". | |
193 $@); | |
194 } | |
195 my $a = bless {},$type; | |
196 if( ! $a->isa('Bio::ClusterI') ) { | |
197 $self->throw("'$type' does not implement Bio::ClusterI. ". | |
198 "Too bad."); | |
199 } | |
200 $self->{'_loaded_types'}->{$type} = 1; | |
201 } | |
202 return $self->{'type'} = $type; | |
203 } | |
204 return $self->{'type'}; | |
205 } | |
206 | |
207 =head2 _guess_type | |
208 | |
209 Title : _guess_type | |
210 Usage : | |
211 Function: Guesses the right type of L<Bio::ClusterI> implementation | |
212 based on initialization parameters for the prospective | |
213 object. | |
214 Example : | |
215 Returns : the type (a string, the module name) | |
216 Args : initialization parameters to be passed to the prospective | |
217 cluster object | |
218 | |
219 | |
220 =cut | |
221 | |
222 sub _guess_type{ | |
223 my ($self,@args) = @_; | |
224 my $type; | |
225 | |
226 # we can only guess from a certain number of arguments | |
227 my ($dispid, $ns, $members) = | |
228 $self->_rearrange([qw(DISPLAY_ID | |
229 NAMESPACE | |
230 MEMBERS | |
231 )], @args); | |
232 # Unigene namespace or ID? | |
233 if($ns && (lc($ns) eq "unigene")) { | |
234 $type = 'Bio::Cluster::UniGene'; | |
235 } elsif($dispid && ($dispid =~ /^Hs\.[0-9]/)) { | |
236 $type = 'Bio::Cluster::UniGene'; | |
237 } | |
238 # what else could we look for? | |
239 return $type; | |
240 } | |
241 | |
242 ##################################################################### | |
243 # aliases for naming consistency or other reasons # | |
244 ##################################################################### | |
245 | |
246 *create = \&create_object; | |
247 | |
248 1; |