speelink fixes, patch courtesy Charles Plessy, fixes #3256
[bioperl-run.git] / lib / Bio / Tools / Run / Ensembl.pm
blob26011312a8c7814914ef144ab1abf98204aa0646
1 # $Id: Ensembl.pm,v 1.6 2006/07/17 14:16:53 sendu Exp $
3 # BioPerl module for Bio::Tools::Run::Ensembl
5 # Please direct questions and support issues to <bioperl-l@bioperl.org>
7 # Cared for by Sendu Bala <bix@sendu.me.uk>
9 # Copyright Sendu Bala
11 # You may distribute this module under the same terms as perl itself
13 # POD documentation - main docs before the code
15 =head1 NAME
17 Bio::Tools::Run::Ensembl - A simplified front-end for setting up the registry
18 for, and then using an Ensembl database with the
19 Ensembl Perl API.
21 =head1 SYNOPSIS
23 use Bio::Tools::Run::Ensembl;
25 # get a Bio::EnsEMBL::Gene for agene of interest
26 my $gene = Bio::Tools::Run::Ensembl->get_gene_by_name(-species => 'human',
27 -name => 'BRCA2');
29 =head1 DESCRIPTION
31 This is a simple way of accessing the Ensembl database to retrieve gene
32 information. Rather than learn the whole Ensembl Perl API, you only need to
33 install it (that is, check it out from CVS:
34 http://www.ensembl.org/info/docs/api/api_installation.html
35 - ignore the information about BioPerl version) and then you can get information
36 about a gene using get_gene_by_name().
38 For gene retrieval it is especially useful compared to direct Ensembl Perl API
39 usage since it can use information from alternate data sources (orthologues,
40 Swissprot, Entrez) to get your gene.
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 the
48 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 Support
55 Please direct usage questions or support issues to the mailing list:
57 I<bioperl-l@bioperl.org>
59 rather than to the module maintainer directly. Many experienced and
60 reponsive experts will be able look at the problem and quickly
61 address it. Please include a thorough description of the problem
62 with code and data examples if at all possible.
64 =head2 Reporting Bugs
66 Report bugs to the Bioperl bug tracking system to help us keep track
67 of the bugs and their resolution. Bug reports can be submitted via the
68 web:
70 http://redmine.open-bio.org/projects/bioperl/
72 =head1 AUTHOR - Sendu Bala
74 Email bix@sendu.me.uk
76 =head1 APPENDIX
78 The rest of the documentation details each of the object methods.
79 Internal methods are usually preceded with a _
81 =cut
83 # Let the code begin...
85 package Bio::Tools::Run::Ensembl;
86 use strict;
88 use Bio::WebAgent;
89 use Bio::DB::EUtilities;
91 use base qw(Bio::Root::Root);
93 our $ENSEMBL_INSTALLED;
94 our $NODB;
95 our $LOADED_STR;
96 our $TOTAL = 0;
97 our $ORTHS = 0;
98 our $SWISS = 0;
99 our $NCBI = 0;
100 our $BAD = 0;
101 our $GOOD = 0;
103 BEGIN {
104 eval {
105 require Bio::EnsEMBL::Registry;
107 $ENSEMBL_INSTALLED = ! $@;
108 $NODB = 0;
109 $LOADED_STR = '';
112 =head2 registry_setup
114 Title : registry_setup
115 Usage : Bio::Tools::Run::Ensembl->registry_setup(-host => $host,
116 -user => $user);
117 if (Bio::Tools::Run::Ensembl->registry_setup) {...}
118 Function: Configure the ensembl registy to use a certain database.
119 The database must be an Ensembl database compatible with the
120 Ensembl Perl API, and you must have that API installed for this
121 method to return true. Defaults to anonymous access to
122 ensembldb.ensembl.org
123 Or just ask if the registry is setup and the database ready to use.
124 Returns : boolean (true if Registry loaded and ready to use)
125 Args : -host => host name (defaults to 'ensembldb.ensembl.org')
126 -user => username (defaults to 'anonymous')
127 -pass => password (no default)
128 -port => port (defaults to 3306)
129 -db_version => version of ensembl database to use, if different from
130 your installed Ensembl modules
131 -verbose => boolean (1 to print messages during database connection)
132 -no_database => boolean (1 to disable database access, causing this
133 method to always return false)
135 =cut
137 sub registry_setup {
138 return 0 unless $ENSEMBL_INSTALLED;
139 my $class = shift;
140 my ($host, $user, $pass, $port, $verbose, $no_db, $db_version) =
141 $class->_rearrange([qw(HOST USER PASS PORT VERBOSE NO_DATABASE DB_VERSION)], @_);
142 $host ||= 'ensembldb.ensembl.org';
143 $user ||= 'anonymous';
145 $NODB = $no_db if defined($no_db);
146 return 0 if $NODB;
148 my $load_str = $host.$user. (defined $pass ? $pass : '') . (defined $port ? $port : '');
149 unless ($LOADED_STR eq $load_str) {
150 Bio::EnsEMBL::Registry->load_registry_from_db(-host => $host,
151 -user => $user,
152 defined $pass ? (-pass => $pass) : (),
153 defined $port ? (-port => $port) : (),
154 defined $db_version ? (-db_version => $db_version) : (),
155 -verbose => $verbose);
156 $LOADED_STR = $load_str;
159 return 1;
162 =head2 get_adaptor
164 Title : get_adaptor
165 Usage : my $adaptor = Bio::Tools::Run::Ensembl->get_adaptor($species, $type);
166 Function: Get a species-specific 'core' database adaptor, optionally of a
167 certain type.
168 Returns : Bio::EnsEMBL::DBSQL::DBAdaptor, OR if a certain type requested, a
169 Bio::EnsEMBL::DBSQL::${type}Adaptor
170 Args : Bio::Species or string (species name) (REQUIRED), AND optionally
171 string (the type of adaptor, eg. 'Gene' or 'Slice').
173 =cut
175 sub get_adaptor {
176 my ($class, $species, $type) = @_;
177 return unless $class->registry_setup;
178 return unless $species;
180 if (ref($species)) {
181 $species = $species->scientific_name;
184 return Bio::EnsEMBL::Registry->get_adaptor($species, 'core', $type) if $type;
185 return Bio::EnsEMBL::Registry->get_DBAdaptor($species, 'core');
188 =head2 get_gene_by_name
190 Title : get_gene_by_name
191 Usage : my $gene = Bio::Tools::Run::Ensembl->get_gene_by_name();
192 Function: Get a gene given species and a gene name. If multiple genes match
193 this combination, tries to pick the 'best' match.
194 Returns : Bio::EnsEMBL::Gene
195 Args : -species => Bio::Species or string (species name), REQUIRED
196 -name => string: gene name, REQUIRED
198 If searching for the supplied gene name in the supplied species
199 results in no genes, or more than one, you can choose what else is
200 attempted in order to find just one gene:
202 -use_orthologues => Bio::Species or string (species name), or array
203 ref of such things: see if any of these
204 supplied species have (unambiguously) a gene
205 with the supplied gene name and if a
206 (one-to-one) orthologue of that gene in that
207 species is present in the main desired species
208 supplied to -species, returns that orthologous
209 gene. (default: none, do not use orthologues)
210 -use_swiss_lookup => boolean: queries swissprot at expasy and if a
211 suitable match is found, queries ensembl with
212 the swissprot id. (default: 0, do not use
213 swiss)
214 -use_entrez_lookup => boolean: queries entrez at the NCBI server if
215 (only) a single gene could not be found by any
216 other method, then query ensembl with the
217 entrez gene id. (default: 0, do not use NCBI)
219 (Attempts proceed in this order and return as soon as one method is
220 successful.)
222 -strict => boolean: return undef with no warnings if more than one,
223 or zero genes were found. (default: 0, warnings are issued
224 and if many genes were found, one of them is returned)
226 =cut
228 sub get_gene_by_name {
229 my $class = shift;
230 return unless $class->registry_setup;
232 my ($species, $gene_name, $use_swiss, $use_orth, $use_entrez, $strict) =
233 $class->_rearrange([qw(SPECIES NAME
234 USE_SWISS_LOOKUP USE_ORTHOLOGUES
235 USE_ENTREZ_LOOKUP STRICT)], @_);
236 $species || $class->throw("You must supply a -species");
237 $gene_name || $class->throw("You must supply a -name");
239 my $taxid;
240 if (ref($species)) {
241 $taxid = $species->id;
242 $species = $species->scientific_name;
245 $TOTAL++;
246 #print ". ";
248 my $gene_adaptor = $class->get_adaptor($species, 'Gene') || return;
250 # get the first gene that matches our query, warn if more than one did
251 my @genes = @{$gene_adaptor->fetch_all_by_external_name($gene_name)};
252 my $gene = shift(@genes);
254 # if not good enough, try again using orthologues
255 if ($use_orth && (! $gene || @genes > 0)) {
256 my @tests;
257 if (ref($use_orth) && ref($use_orth) eq 'ARRAY') {
258 @tests = @{$use_orth};
260 else {
261 @tests = ($use_orth);
264 my $alias_species = Bio::EnsEMBL::Registry->get_alias($species);
266 foreach my $test_species (@tests) {
267 $test_species = $test_species->scientific_name if ref($test_species);
268 $test_species eq $species and next;
270 my $test_gene = $class->get_gene_by_name(-species => $test_species,
271 -name => $gene_name,
272 -strict => 1) || next;
273 my $homologue_results_ref = $test_gene->get_all_homologous_Genes();
275 # get the species and gene id of each homologue
276 foreach my $result_ref (@{$homologue_results_ref}) {
277 my ($homolog_gene, $homology, $homolog_species) = @{$result_ref};
279 # get_alias returns lower case, underscored version of what we get here
280 $homolog_species = lc($homolog_species);
281 $homolog_species =~ s/ /_/g;
282 $homolog_species eq $alias_species or next;
284 $homology->description eq 'UBRH' or next;
286 $gene = $homolog_gene;
287 $ORTHS++;
288 last;
291 $gene and last;
295 # if not good enough, try again using swissprot
296 if ($use_swiss && (! $gene || @genes > 0)) {
297 my $swiss_id;
299 #*** swiss look up should be farmed out to some dedicated class
300 my $swiss_name = lc($gene_name);
301 my $swiss_species = lc($species);
302 $swiss_species =~ s/\s/+/g;
303 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";
304 my $web_agent = Bio::WebAgent->new();
305 $web_agent->url($url);
306 my $rq = HTTP::Request->new(GET=>$url);
307 my $reply = $web_agent->request($rq);
308 if ($reply->is_error) {
309 $class->throw($reply->as_string()."\nError getting for url $url!\n");
311 my $content = $reply->content;
312 if ($content && $content !~ /No entries have been found/) {
313 my @possibles = split("<tr><td><input type=checkbox name=ac value=", $content);
314 shift(@possibles);
316 my @good_ids;
317 foreach my $poss (@possibles) {
318 my ($id, $desc) = $poss =~ /^.+?<td>(.+?)<\/td>.+?<b>.+?<b>(.+?)<\/td>/;
319 unless ($desc =~ /Fragment/) {
320 push(@good_ids, $id);
324 if (@good_ids == 1) {
325 $swiss_id = shift(@good_ids);
329 if ($swiss_id) {
330 @genes = @{$gene_adaptor->fetch_all_by_external_name($swiss_id)};
331 $gene = shift(@genes);
332 $SWISS++ if ($gene && @genes == 0);
336 # if not good enough, try again with search for the gene name at ncbi
337 if ($use_entrez && (! $gene || @genes > 0)) {
338 my $esearch = Bio::DB::EUtilities->new(-eutil => 'esearch',
339 -db => 'gene',
340 -term => $taxid ? "$gene_name ${taxid}[taxid]" : "$gene_name \"$species\"[Organism]",
341 -usehistory => 'y',
342 -verbose => -1);
343 my $esummary = Bio::DB::EUtilities->new(-eutil => 'esummary',
344 -history => $esearch->next_History);
345 eval {$esummary->parse_data;};
346 if (!$@) {
347 my $ncbi_id;
348 while (my $docsum = $esummary->next_DocSum) {
349 my $item = $docsum->get_Item_by_name('Name');
350 if (lc($item->get_content) eq lc($gene_name)) {
351 $ncbi_id = $docsum->get_id;
352 last;
356 if ($ncbi_id) {
357 @genes = @{$gene_adaptor->fetch_all_by_external_name($ncbi_id)};
358 $gene = shift(@genes);
359 $NCBI++ if ($gene && @genes == 0);
364 if (@genes > 0) {
365 return if $strict;
366 #$class->warn("Species '$species' had multiple matches to gene '$gene_name', using first gene '".$gene->display_id."'");
368 unless ($gene) {
369 return if $strict;
370 $BAD++;
371 #$class->warn("Species '$species' didn't have gene '$gene_name'");
372 return;
375 $GOOD++;
376 return $gene;
379 sub _stats {
380 print "$TOTAL | $ORTHS | $SWISS | $NCBI | good vs bad = $GOOD vs $BAD\n";