blib/lib/Bio/Tools/Run/Ensembl.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« blib/lib/Bio/Tools/Run/AssemblerBase.pm ↑ Summary blib/lib/Bio/DB/ESoap.pm »
1 #
2 # BioPerl module for Bio::Tools::Run::Ensembl
3 #
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
5 #
6 # Cared for by Sendu Bala <bix@sendu.me.uk>
7 #
8 # Copyright Sendu Bala
9 #
10 # You may distribute this module under the same terms as perl itself
11  
12 # POD documentation - main docs before the code
13  
14 =head1 NAME
15  
16 Bio::Tools::Run::Ensembl - A simplified front-end for setting up the registry
17 for, and then using an Ensembl database with the
18 Ensembl Perl API.
19  
20 =head1 SYNOPSIS
21  
22 use Bio::Tools::Run::Ensembl;
23
24 # get a Bio::EnsEMBL::Gene for agene of interest
25 my $gene = Bio::Tools::Run::Ensembl->get_gene_by_name(-species => 'human',
26 -name => 'BRCA2');
27  
28 =head1 DESCRIPTION
29  
30 This is a simple way of accessing the Ensembl database to retrieve gene
31 information. Rather than learn the whole Ensembl Perl API, you only need to
32 install it (that is, check it out from CVS:
33 http://www.ensembl.org/info/docs/api/api_installation.html
34 - ignore the information about BioPerl version) and then you can get information
35 about a gene using get_gene_by_name().
36  
37 For gene retrieval it is especially useful compared to direct Ensembl Perl API
38 usage since it can use information from alternate data sources (orthologues,
39 Swissprot, Entrez) to get your gene.
40  
41 =head1 FEEDBACK
42  
43 =head2 Mailing Lists
44  
45 User feedback is an integral part of the evolution of this and other
46 Bioperl modules. Send your comments and suggestions preferably to the
47 Bioperl mailing list. Your participation is much appreciated.
48  
49 bioperl-l@bioperl.org - General discussion
50 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
51  
52 =head2 Support
53  
54 Please direct usage questions or support issues to the mailing list:
55  
56 I<bioperl-l@bioperl.org>
57  
58 rather than to the module maintainer directly. Many experienced and
59 reponsive experts will be able look at the problem and quickly
60 address it. Please include a thorough description of the problem
61 with code and data examples if at all possible.
62  
63 =head2 Reporting Bugs
64  
65 Report bugs to the Bioperl bug tracking system to help us keep track
66 of the bugs and their resolution. Bug reports can be submitted via the
67 web:
68  
69 http://redmine.open-bio.org/projects/bioperl/
70  
71 =head1 AUTHOR - Sendu Bala
72  
73 Email bix@sendu.me.uk
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 # Let the code begin...
83  
84 package Bio::Tools::Run::Ensembl;
85 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
86  
87 1 ▶use Bio::WebAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
88 1 ▶use Bio::DB::EUtilities;
Subroutine BEGIN: called CC=1 SCAR=2.6
89  
90 use base qw(Bio::Root::Root);
91  
92 our $ENSEMBL_INSTALLED;
93 our $NODB;
94 our $LOADED_STR;
95 our $TOTAL = 0;
96 our $ORTHS = 0;
97 our $SWISS = 0;
98 our $NCBI = 0;
99 our $BAD = 0;
100 our $GOOD = 0;
101  
102 BEGIN {
103 eval {
104 require Bio::EnsEMBL::Registry;
105 };
106 $ENSEMBL_INSTALLED = ! $@;
107 $NODB = 0;
108 $LOADED_STR = '';
109 }
110  
111 =head2 registry_setup
112  
113 Title : registry_setup
114 Usage : Bio::Tools::Run::Ensembl->registry_setup(-host => $host,
115 -user => $user);
116 if (Bio::Tools::Run::Ensembl->registry_setup) {...}
117 Function: Configure the ensembl registy to use a certain database.
118 The database must be an Ensembl database compatible with the
119 Ensembl Perl API, and you must have that API installed for this
120 method to return true. Defaults to anonymous access to
121 ensembldb.ensembl.org
122 Or just ask if the registry is setup and the database ready to use.
123 Returns : boolean (true if Registry loaded and ready to use)
124 Args : -host => host name (defaults to 'ensembldb.ensembl.org')
125 -user => username (defaults to 'anonymous')
126 -pass => password (no default)
127 -port => port (defaults to 3306)
128 -db_version => version of ensembl database to use, if different from
129 your installed Ensembl modules
130 -verbose => boolean (1 to print messages during database connection)
131 -no_database => boolean (1 to disable database access, causing this
132 method to always return false)
133  
134 =cut
135  
136 sub registry_setup {
137 return 0 unless $ENSEMBL_INSTALLED;
138 my $class = shift;
139 my ($host, $user, $pass, $port, $verbose, $no_db, $db_version) =
140 $class->_rearrange([qw(HOST USER PASS PORT VERBOSE NO_DATABASE DB_VERSION)], @_);
141 $host ||= 'ensembldb.ensembl.org';
142 $user ||= 'anonymous';
143
144 $NODB = $no_db if defined($no_db);
145 return 0 if $NODB;
146
147 my $load_str = $host.$user. (defined $pass ? $pass : '') . (defined $port ? $port : '');
148 unless ($LOADED_STR eq $load_str) {
149 Bio::EnsEMBL::Registry->load_registry_from_db(-host => $host,
150 -user => $user,
151 defined $pass ? (-pass => $pass) : (),
152 defined $port ? (-port => $port) : (),
153 defined $db_version ? (-db_version => $db_version) : (),
154 -verbose => $verbose);
155 $LOADED_STR = $load_str;
156 }
157
158 return 1;
159 }
160  
161 =head2 get_adaptor
162  
163 Title : get_adaptor
164 Usage : my $adaptor = Bio::Tools::Run::Ensembl->get_adaptor($species, $type);
165 Function: Get a species-specific 'core' database adaptor, optionally of a
166 certain type.
167 Returns : Bio::EnsEMBL::DBSQL::DBAdaptor, OR if a certain type requested, a
168 Bio::EnsEMBL::DBSQL::${type}Adaptor
169 Args : Bio::Species or string (species name) (REQUIRED), AND optionally
170 string (the type of adaptor, eg. 'Gene' or 'Slice').
171  
172 =cut
173  
174 sub get_adaptor {
175 my ($class, $species, $type) = @_;
176 return unless $class->registry_setup;
177 return unless $species;
178
179 if (ref($species)) {
180 $species = $species->scientific_name;
181 }
182
183 return Bio::EnsEMBL::Registry->get_adaptor($species, 'core', $type) if $type;
184 return Bio::EnsEMBL::Registry->get_DBAdaptor($species, 'core');
185 }
186  
187 =head2 get_gene_by_name
188  
189 Title : get_gene_by_name
190 Usage : my $gene = Bio::Tools::Run::Ensembl->get_gene_by_name();
191 Function: Get a gene given species and a gene name. If multiple genes match
192 this combination, tries to pick the 'best' match.
193 Returns : Bio::EnsEMBL::Gene
194 Args : -species => Bio::Species or string (species name), REQUIRED
195 -name => string: gene name, REQUIRED
196  
197 If searching for the supplied gene name in the supplied species
198 results in no genes, or more than one, you can choose what else is
199 attempted in order to find just one gene:
200  
201 -use_orthologues => Bio::Species or string (species name), or array
202 ref of such things: see if any of these
203 supplied species have (unambiguously) a gene
204 with the supplied gene name and if a
205 (one-to-one) orthologue of that gene in that
206 species is present in the main desired species
207 supplied to -species, returns that orthologous
208 gene. (default: none, do not use orthologues)
209 -use_swiss_lookup => boolean: queries swissprot at expasy and if a
210 suitable match is found, queries ensembl with
211 the swissprot id. (default: 0, do not use
212 swiss)
213 -use_entrez_lookup => boolean: queries entrez at the NCBI server if
214 (only) a single gene could not be found by any
215 other method, then query ensembl with the
216 entrez gene id. (default: 0, do not use NCBI)
217  
218 (Attempts proceed in this order and return as soon as one method is
219 successful.)
220  
221 -strict => boolean: return undef with no warnings if more than one,
222 or zero genes were found. (default: 0, warnings are issued
223 and if many genes were found, one of them is returned)
224  
225 =cut
226  
227 sub get_gene_by_name {
228 my $class = shift;
229 return unless $class->registry_setup;
230
231 my ($species, $gene_name, $use_swiss, $use_orth, $use_entrez, $strict) =
232 $class->_rearrange([qw(SPECIES NAME
233 USE_SWISS_LOOKUP USE_ORTHOLOGUES
234 USE_ENTREZ_LOOKUP STRICT)], @_);
235 $species || $class->throw("You must supply a -species");
236 $gene_name || $class->throw("You must supply a -name");
237
238 my $taxid;
239 if (ref($species)) {
240 $taxid = $species->id;
241 $species = $species->scientific_name;
242 }
243
244 $TOTAL++;
245 #print ". ";
246
247 my $gene_adaptor = $class->get_adaptor($species, 'Gene') || return;
248
249 # get the first gene that matches our query, warn if more than one did
250 my @genes = @{$gene_adaptor->fetch_all_by_external_name($gene_name)};
251 my $gene = shift(@genes);
252
253 # if not good enough, try again using orthologues
254 if ($use_orth && (! $gene || @genes > 0)) {
255 my @tests;
256 if (ref($use_orth) && ref($use_orth) eq 'ARRAY') {
257 @tests = @{$use_orth};
258 }
259 else {
260 @tests = ($use_orth);
261 }
262
263 my $alias_species = Bio::EnsEMBL::Registry->get_alias($species);
264
265 foreach my $test_species (@tests) {
266 $test_species = $test_species->scientific_name if ref($test_species);
267 $test_species eq $species and next;
268
269 my $test_gene = $class->get_gene_by_name(-species => $test_species,
270 -name => $gene_name,
271 -strict => 1) || next;
272 my $homologue_results_ref = $test_gene->get_all_homologous_Genes();
273
274 # get the species and gene id of each homologue
275 foreach my $result_ref (@{$homologue_results_ref}) {
276 my ($homolog_gene, $homology, $homolog_species) = @{$result_ref};
277
278 # get_alias returns lower case, underscored version of what we get here
279 $homolog_species = lc($homolog_species);
280 $homolog_species =~ s/ /_/g;
281 $homolog_species eq $alias_species or next;
282
283 $homology->description eq 'UBRH' or next;
284
285 $gene = $homolog_gene;
286 $ORTHS++;
287 last;
288 }
289
290 $gene and last;
291 }
292 }
293
294 # if not good enough, try again using swissprot
295 if ($use_swiss && (! $gene || @genes > 0)) {
296 my $swiss_id;
297
298 #*** swiss look up should be farmed out to some dedicated class
299 my $swiss_name = lc($gene_name);
300 my $swiss_species = lc($species);
301 $swiss_species =~ s/\s/+/g;
302 my $url = "http://www.expasy.org/cgi-bin/get-entries?db=sp&db=tr&DE=&GNc=AND&GN=$swiss_name&OC=$swiss_species&view=&num=100";
303 my $web_agent = Bio::WebAgent->new();
304 $web_agent->url($url);
305 my $rq = HTTP::Request->new(GET=>$url);
306 my $reply = $web_agent->request($rq);
307 if ($reply->is_error) {
308 $class->throw($reply->as_string()."\nError getting for url $url!\n");
309 }
310 my $content = $reply->content;
311 if ($content && $content !~ /No entries have been found/) {
312 my @possibles = split("<tr><td><input type=checkbox name=ac value=", $content);
313 shift(@possibles);
314
315 my @good_ids;
316 foreach my $poss (@possibles) {
317 my ($id, $desc) = $poss =~ /^.+?<td>(.+?)<\/td>.+?<b>.+?<b>(.+?)<\/td>/;
318 unless ($desc =~ /Fragment/) {
319 push(@good_ids, $id);
320 }
321 }
322
323 if (@good_ids == 1) {
324 $swiss_id = shift(@good_ids);
325 }
326 }
327
328 if ($swiss_id) {
329 @genes = @{$gene_adaptor->fetch_all_by_external_name($swiss_id)};
330 $gene = shift(@genes);
331 $SWISS++ if ($gene && @genes == 0);
332 }
333 }
334
335 # if not good enough, try again with search for the gene name at ncbi
336 if ($use_entrez && (! $gene || @genes > 0)) {
337 my $esearch = Bio::DB::EUtilities->new(-eutil => 'esearch',
338 -db => 'gene',
339 -term => $taxid ? "$gene_name ${taxid}[taxid]" : "$gene_name \"$species\"[Organism]",
340 -usehistory => 'y',
341 -verbose => -1);
342 my $esummary = Bio::DB::EUtilities->new(-eutil => 'esummary',
343 -history => $esearch->next_History);
344 eval {$esummary->parse_data;};
345 if (!$@) {
346 my $ncbi_id;
347 while (my $docsum = $esummary->next_DocSum) {
348 my $item = $docsum->get_Item_by_name('Name');
349 if (lc($item->get_content) eq lc($gene_name)) {
350 $ncbi_id = $docsum->get_id;
351 last;
352 }
353 }
354
355 if ($ncbi_id) {
356 @genes = @{$gene_adaptor->fetch_all_by_external_name($ncbi_id)};
357 $gene = shift(@genes);
358 $NCBI++ if ($gene && @genes == 0);
359 }
360 }
361 }
362
363 if (@genes > 0) {
364 return if $strict;
365 #$class->warn("Species '$species' had multiple matches to gene '$gene_name', using first gene '".$gene->display_id."'");
366 }
367 unless ($gene) {
368 return if $strict;
369 $BAD++;
370 #$class->warn("Species '$species' didn't have gene '$gene_name'");
371 return;
372 }
373
374 $GOOD++;
375 return $gene;
376 }
377  
378 sub _stats {
379 print "$TOTAL | $ORTHS | $SWISS | $NCBI | good vs bad = $GOOD vs $BAD\n";
380 }
381  
382 1;
« blib/lib/Bio/Tools/Run/AssemblerBase.pm ↑ Summary blib/lib/Bio/DB/ESoap.pm »