64
65
# Let the code begin...
67
67
package Bio::DB::Taxonomy;
68
use vars qw(@ISA $DefaultSource);
68
use vars qw($DefaultSource $TAXON_IIDS);
71
use Bio::Root::HTTPget;
72
use base qw(Bio::Root::Root);
72
74
$DefaultSource = 'entrez';
74
@ISA = qw(Bio::Root::HTTPget);
79
80
Usage : my $obj = new Bio::DB::Taxonomy(-source => 'entrez');
80
Function: Builds a new Bio::DB::Taxonomy object
81
Function: Builds a new Bio::DB::Taxonomy object.
81
82
Returns : an instance of Bio::DB::Taxonomy
82
Args : -source => which database source 'entrez' or 'localfile'
83
Args : -source => which database source 'entrez' or 'flatfile' or 'list'
97
96
@param{ map { lc $_ } keys %param } = values %param; # lowercase keys
98
97
my $source = $param{'-source'} || $DefaultSource;
100
99
$source = "\L$source"; # normalize capitalization to lower case
102
101
# normalize capitalization
103
return undef unless( $class->_load_tax_module($source) );
102
return unless( $class->_load_tax_module($source) );
104
103
return "Bio::DB::Taxonomy::$source"->new(@args);
109
108
sub _initialize { }
112
=head2 get_Taxonomy_Node
114
Title : get_Taxonomy_Node
115
Usage : my $species = $db->get_Taxonomy_Node(-taxonid => $taxaid)
116
Function: Get a Bio::Taxonomy::Taxon object for a taxonid
117
Returns : Bio::Taxonomy::Taxon object
118
Args : -taxonid => taxonomy id (to query by taxonid)
113
Usage : my $taxon = $db->get_taxon(-taxonid => $taxonid)
114
Function: Get a Bio::Taxon object from the database.
115
Returns : Bio::Taxon object
116
Args : just a single value which is the database id, OR named args:
117
-taxonid => taxonomy id (to query by taxonid)
120
-name => string (to query by a taxonomy name: common name,
126
sub get_Taxonomy_Node{
129
$self->throw_not_implemented();
136
Usage : my $taxonid = $db->get_taxonid('Homo sapiens');
137
Function: Searches for a taxonid (typically ncbi_taxon_id)
138
based on a query string
140
Args : String representing species/node name
145
*get_taxaid = \&get_taxonid;
150
$self->throw_not_implemented();
119
-name => string (to query by a taxonomy name: common name,
120
scientific name, etc)
125
shift->throw_not_implemented();
128
*get_Taxonomy_Node = \&get_taxon;
133
Usage : my @taxonids = $db->get_taxonids('Homo sapiens');
134
Function: Searches for a taxonid (typically ncbi_taxon_id) based on a query
135
string. Note that multiple taxonids can match to the same supplied
137
Returns : array of integer ids in list context, one of these in scalar context
138
Args : string representing taxon's name
143
shift->throw_not_implemented();
146
*get_taxonid = \&get_taxonids;
147
*get_taxaid = \&get_taxonids;
152
Usage : my $ancestor_taxon = $db->ancestor($taxon)
153
Function: Retrieve the full ancestor taxon of a supplied Taxon from the
156
Args : Bio::Taxon (that was retrieved from this database)
161
shift->throw_not_implemented();
164
=head2 each_Descendent
166
Title : each_Descendent
167
Usage : my @taxa = $db->each_Descendent($taxon);
168
Function: Get all the descendents of the supplied Taxon (but not their
169
descendents, ie. not a recursive fetchall).
170
Returns : Array of Bio::Taxon objects
171
Args : Bio::Taxon (that was retrieved from this database)
175
sub each_Descendent {
176
shift->throw_not_implemented();
179
=head2 get_all_Descendents
181
Title : get_all_Descendents
182
Usage : my @taxa = $db->get_all_Descendents($taxon);
183
Function: Like each_Descendent(), but do a recursive fetchall
184
Returns : Array of Bio::Taxon objects
185
Args : Bio::Taxon (that was retrieved from this database)
189
sub get_all_Descendents {
190
my ($self, $taxon) = @_;
192
foreach my $desc_taxon ($self->each_Descendent($taxon)) {
193
push @taxa, ($desc_taxon, $self->get_all_Descendents($desc_taxon));
153
198
=head2 _load_tax_module
226
=head2 _handle_internal_id
228
Title : _handle_internal_id
229
Usage : *INTERNAL Bio::DB::Taxonomy stuff*
230
Function: Tries to ensure that when a taxon is requested from any database,
231
the Taxon object returned will have the same internal id regardless
233
Args : Bio::Taxon, and optionally true value to try and do the job using
234
scientific name & rank if your ids aren't comparable to other dbs.
238
sub _handle_internal_id {
239
my ($self, $taxon, $try_name) = @_;
240
$self->throw("Must supply a Bio::Taxon") unless ref($taxon) && $taxon->isa('Bio::Taxon');
241
my $taxid = $taxon->id || return;
242
my $sci_name = $taxon->scientific_name || '';
243
my $rank = $taxon->rank || 'no rank';
245
if ($try_name && $sci_name && defined $TAXON_IIDS->{names}->{$sci_name}) {
246
if (defined $TAXON_IIDS->{names}->{$sci_name}->{$rank}) {
247
$TAXON_IIDS->{taxids}->{$taxid} = $TAXON_IIDS->{names}->{$sci_name}->{$rank};
249
elsif ($rank eq 'no rank') {
250
# pick the internal id of one named rank taxa at random
251
my ($iid) = values %{$TAXON_IIDS->{names}->{$sci_name}};
252
$TAXON_IIDS->{taxids}->{$taxid} = $iid;
256
if (defined $TAXON_IIDS->{taxids}->{$taxid}) {
257
# a little dangerous to use this internal method of Bio::Tree::Node;
258
# but it is how internal_id() is set
259
$taxon->_creation_id($TAXON_IIDS->{taxids}->{$taxid});
262
$TAXON_IIDS->{taxids}->{$taxid} = $taxon->internal_id;
263
$TAXON_IIDS->{names}->{$sci_name}->{$rank} = $taxon->internal_id if $sci_name;