/
Taxonomy.pm
352 lines (257 loc) · 9.31 KB
/
Taxonomy.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#
# BioPerl module for Bio::DB::Taxonomy
#
# Please direct questions and support issues to <bioperl-l@bioperl.org>
#
# Cared for by Jason Stajich <jason-at-bioperl.org>
#
# Copyright Jason Stajich
#
# You may distribute this module under the same terms as perl itself
# POD documentation - main docs before the code
=head1 NAME
Bio::DB::Taxonomy - Access to a taxonomy database
=head1 SYNOPSIS
use Bio::DB::Taxonomy;
my $db = Bio::DB::Taxonomy->new(-source => 'entrez');
# use NCBI Entrez over HTTP
my $taxonid = $db->get_taxonid('Homo sapiens');
# get a taxon
my $taxon = $db->get_taxon(-taxonid => $taxonid);
=head1 DESCRIPTION
This is a front end module for access to a taxonomy database.
=head1 FEEDBACK
=head2 Mailing Lists
User feedback is an integral part of the evolution of this and other
Bioperl modules. Send your comments and suggestions preferably to
the Bioperl mailing list. Your participation is much appreciated.
bioperl-l@bioperl.org - General discussion
http://bioperl.org/wiki/Mailing_lists - About the mailing lists
=head2 Support
Please direct usage questions or support issues to the mailing list:
I<bioperl-l@bioperl.org>
rather than to the module maintainer directly. Many experienced and
reponsive experts will be able look at the problem and quickly
address it. Please include a thorough description of the problem
with code and data examples if at all possible.
=head2 Reporting Bugs
Report bugs to the Bioperl bug tracking system to help us keep track
of the bugs and their resolution. Bug reports can be submitted via
the web:
https://redmine.open-bio.org/projects/bioperl/
=head1 AUTHOR - Jason Stajich
Email jason-at-bioperl.org
=head1 CONTRIBUTORS
Sendu Bala: bix@sendu.me.uk
=head1 APPENDIX
The rest of the documentation details each of the object methods.
Internal methods are usually preceded with a _
=cut
# Let the code begin...
package Bio::DB::Taxonomy;
use vars qw($DefaultSource $TAXON_IIDS);
use strict;
use Bio::Tree::Tree;
use base qw(Bio::Root::Root);
$DefaultSource = 'entrez';
$TAXON_IIDS = {};
=head2 new
Title : new
Usage : my $obj = Bio::DB::Taxonomy->new(-source => 'entrez');
Function: Builds a new Bio::DB::Taxonomy object.
Returns : an instance of Bio::DB::Taxonomy
Args : -source => which database source 'entrez' (NCBI taxonomy online),
'flatfile' (local NCBI taxonomy), 'greengenes' (local
GreenGenes taxonomy), 'silva' (local Silva taxonomy), or
'list' (Do-It-Yourself taxonomy)
=cut
sub new {
my($class,@args) = @_;
if( $class =~ /Bio::DB::Taxonomy::(\S+)/ ) {
my ($self) = $class->SUPER::new(@args);
$self->_initialize(@args);
return $self;
} else {
my %param = @args;
@param{ map { lc $_ } keys %param } = values %param; # lowercase keys
my $source = $param{'-source'} || $DefaultSource;
$source = "\L$source"; # normalize capitalization to lower case
# normalize capitalization
return unless( $class->_load_tax_module($source) );
return "Bio::DB::Taxonomy::$source"->new(@args);
}
}
# empty for now
sub _initialize { }
=head2 get_num_taxa
Title : get_num_taxa
Usage : my $num = $db->get_num_taxa();
Function: Get the number of taxa stored in the database.
Returns : A number
Args : None
=cut
sub get_num_taxa {
shift->throw_not_implemented();
}
=head2 get_taxon
Title : get_taxon
Usage : my $taxon = $db->get_taxon(-taxonid => $taxonid);
Function: Get a Bio::Taxon object from the database.
Returns : Bio::Taxon object
Args : just a single value which is the database id, OR named args:
-taxonid => taxonomy id (to query by taxonid)
OR
-name => string (to query by a taxonomy name: common name,
scientific name, etc)
=cut
sub get_taxon {
shift->throw_not_implemented();
}
*get_Taxonomy_Node = \&get_taxon;
=head2 get_taxonids
Title : get_taxonids
Usage : my @taxonids = $db->get_taxonids('Homo sapiens');
Function: Searches for a taxonid (typically ncbi_taxon_id) based on a query
string. Note that multiple taxonids can match to the same supplied
name.
Returns : array of integer ids in list context, one of these in scalar context
Args : string representing taxon's name
=cut
sub get_taxonids {
shift->throw_not_implemented();
}
*get_taxonid = \&get_taxonids;
*get_taxaid = \&get_taxonids;
=head2 get_tree
Title : get_tree
Usage : my $tree = $db->get_tree(@species_names);
Function: Generate a tree comprised of the full lineages of all the supplied
species names. The nodes for the requested species are given
name('supplied') values corresponding to the supplied name, such that
they can be identified if the real species name in the database
(stored under node_name()) is different. The nodes are also given an
arbitrary branch length of 1.
Returns : Bio::Tree::Tree
Args : a list of species names (strings)
=cut
sub get_tree {
my ($self, @species_names) = @_;
# the full lineages of the species are merged into a single tree
my $tree;
for my $name (@species_names) {
my @ids = $self->get_taxonids($name);
if (not scalar @ids) {
$self->throw("No taxonomy database node for species ".$name);
}
for my $id (@ids) {
my $node = $self->get_taxon(-taxonid => $id);
$node->name('supplied', $name);
if ($tree) {
$tree->merge_lineage($node);
} else {
$tree = Bio::Tree::Tree->new(-verbose => $self->verbose, -node => $node);
}
}
}
# add arbitrary branch length
for my $node ($tree->get_nodes) {
$node->branch_length(1);
}
return $tree;
}
=head2 ancestor
Title : ancestor
Usage : my $ancestor_taxon = $db->ancestor($taxon);
Function: Retrieve the full ancestor taxon of a supplied Taxon from the
database.
Returns : Bio::Taxon
Args : Bio::Taxon (that was retrieved from this database)
=cut
sub ancestor {
shift->throw_not_implemented();
}
=head2 each_Descendent
Title : each_Descendent
Usage : my @taxa = $db->each_Descendent($taxon);
Function: Get all the descendents of the supplied Taxon (but not their
descendents, ie. not a recursive fetchall).
Returns : Array of Bio::Taxon objects
Args : Bio::Taxon (that was retrieved from this database)
=cut
sub each_Descendent {
shift->throw_not_implemented();
}
=head2 get_all_Descendents
Title : get_all_Descendents
Usage : my @taxa = $db->get_all_Descendents($taxon);
Function: Like each_Descendent(), but do a recursive fetchall
Returns : Array of Bio::Taxon objects
Args : Bio::Taxon (that was retrieved from this database)
=cut
sub get_all_Descendents {
my ($self, $taxon) = @_;
my @taxa;
foreach my $desc_taxon ($self->each_Descendent($taxon)) {
push @taxa, ($desc_taxon, $self->get_all_Descendents($desc_taxon));
}
return @taxa;
}
=head2 _load_tax_module
Title : _load_tax_module
Usage : *INTERNAL Bio::DB::Taxonomy stuff*
Function: Loads up (like use) a module at run time on demand
=cut
sub _load_tax_module {
my ($self, $source) = @_;
my $module = "Bio::DB::Taxonomy::" . $source;
my $ok;
eval { $ok = $self->_load_module($module) };
if ( $@ ) {
print STDERR $@;
print STDERR <<END;
$self: $source cannot be found
Exception $@
For more information about the Bio::DB::Taxonomy system please see
the Bio::DB::Taxonomy docs. This includes ways of checking for
formats at compile time, not run time.
END
;
}
return $ok;
}
=head2 _handle_internal_id
Title : _handle_internal_id
Usage : *INTERNAL Bio::DB::Taxonomy stuff*
Function: Tries to ensure that when a taxon is requested from any database,
the Taxon object returned will have the same internal id regardless
of database.
Args : Bio::Taxon, and optionally true value to try and do the job using
scientific name & rank if your ids aren't comparable to other dbs.
=cut
sub _handle_internal_id {
my ($self, $taxon, $try_name) = @_;
$self->throw("Must supply a Bio::Taxon") unless ref($taxon) && $taxon->isa('Bio::Taxon');
my $taxid = $taxon->id || return;
my $sci_name = $taxon->scientific_name || '';
my $rank = $taxon->rank || 'no rank';
if ($try_name && $sci_name && defined $TAXON_IIDS->{names}->{$sci_name}) {
if (defined $TAXON_IIDS->{names}->{$sci_name}->{$rank}) {
$TAXON_IIDS->{taxids}->{$taxid} = $TAXON_IIDS->{names}->{$sci_name}->{$rank};
}
elsif ($rank eq 'no rank') {
# pick the internal id of one named rank taxa at random
my ($iid) = values %{$TAXON_IIDS->{names}->{$sci_name}};
$TAXON_IIDS->{taxids}->{$taxid} = $iid;
}
}
if (defined $TAXON_IIDS->{taxids}->{$taxid}) {
# a little dangerous to use this internal method of Bio::Tree::Node;
# but it is how internal_id() is set
$taxon->_creation_id($TAXON_IIDS->{taxids}->{$taxid});
}
else {
$TAXON_IIDS->{taxids}->{$taxid} = $taxon->internal_id;
$TAXON_IIDS->{names}->{$sci_name}->{$rank} = $taxon->internal_id if $sci_name;
}
}
1;