2 # BioPerl module for Bio::DB::Taxonomy::flatfile
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
6 # Cared for by Chris Fields <cjfields-at-cpan-dot-org>
8 # Copyright Chris Fields
10 # You may distribute this module under the same terms as perl itself
12 # POD documentation - main docs before the code
16 Bio::DB::Taxonomy::sqlite - SQLite-based implementation of Bio::DB::Taxonomy::flatfile
20 use Bio::DB::Taxonomy;
22 my $db = Bio::DB::Taxonomy->new(-source => 'sqlite' ,
23 -nodesfile => 'nodes.dmp',
24 -namesfile => 'names.dmp');
28 This is an implementation of Bio::DB::Taxonomy which stores and accesses the
29 NCBI taxonomy using a simple SQLite3 database stored locally on disk.
31 The required database files, nodes.dmp and names.dmp can be obtained from
32 ftp://ftp.ncbi.nih.gov/pub/taxonomy/taxdump.tar.gz
36 Beyond completing the implementation and optimization, this will
37 likely be rolled into a more flexible backend at some future point.
43 User feedback is an integral part of the evolution of this and other
44 Bioperl modules. Send your comments and suggestions preferably to
45 the Bioperl mailing list. Your participation is much appreciated.
47 bioperl-l@bioperl.org - General discussion
48 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
52 Please direct usage questions or support issues to the mailing list:
54 I<bioperl-l@bioperl.org>
56 rather than to the module maintainer directly. Many experienced and
57 reponsive experts will be able look at the problem and quickly
58 address it. Please include a thorough description of the problem
59 with code and data examples if at all possible.
63 Report bugs to the Bioperl bug tracking system to help us keep track
64 of the bugs and their resolution. Bug reports can be submitted via
67 https://github.com/bioperl/bioperl-live/issues
69 =head1 AUTHOR - Chris Fields
71 Email cjfields-at-cpan-dot-org
75 The rest of the documentation details each of the object methods.
76 Internal methods are usually preceded with a _
80 # Let the code begin...
82 package Bio
::DB
::Taxonomy
::sqlite
;
88 use File
::Spec
::Functions
;
92 use constant SEPARATOR
=> ':';
94 our $DEFAULT_INDEX_DIR = $Bio::Root
::IO
::TEMPDIR
; # /tmp
95 our $DEFAULT_CACHE_SIZE = 0; # /tmp
96 our $DEFAULT_DB_NAME = 'taxonomy.sqlite';
100 [qw(INV Invertebrates)],
103 [qw(PLN Plants)], # (and fungi)
107 [qw(UNA Unassigned)],
109 [qw(VRT Vertebrates)],
110 [qw(ENV 'Environmental samples')]
113 use base
qw(Bio::DB::Taxonomy);
118 Usage : my $obj = Bio::DB::Taxonomy::flatfile->new();
119 Function: Builds a new Bio::DB::Taxonomy::flatfile object
120 Returns : an instance of Bio::DB::Taxonomy::flatfile
121 Args : -directory => name of directory where index files should be created
122 -nodesfile => name of file containing nodes (nodes.dmp from NCBI)
123 -namesfile => name of the file containing names(names.dmp from NCBI)
124 -force => 1 to replace current indexes even if they exist
128 # TODO: get rid of globals!
130 my ( $class, @args ) = @_;
132 my $self = $class->SUPER::new
(@args);
134 my ( $dir, $nodesfile, $namesfile, $db, $force, $cs ) =
135 $self->_rearrange( [qw(DIRECTORY NODESFILE NAMESFILE DB FORCE CACHE)], @args );
137 $self->index_directory( $dir || $DEFAULT_INDEX_DIR );
139 $self->db_name( $db || $DEFAULT_DB_NAME );
141 $self->cache_size($cs // $DEFAULT_CACHE_SIZE);
144 $self->_build_index( $nodesfile, $namesfile, $force );
151 =head2 Bio::DB::Taxonomy interface implementation
156 Usage : my $num = $db->get_num_taxa();
157 Function: Get the number of taxa stored in the database.
166 my $ct = $self->_dbh_fetch(<<SQL);
167 SELECT COUNT(*) FROM taxon
176 Usage : my $taxon = $db->get_taxon(-taxonid => $taxonid)
177 Function: Get a Bio::Taxon object from the database.
178 Returns : Bio::Taxon object
179 Args : just a single value which is the database id, OR named args:
180 -taxonid => taxonomy id (to query by taxonid)
182 -name => string (to query by a taxonomy name: common name,
183 scientific name, etc)
189 my ( $taxonid, $name );
192 ( $taxonid, $name ) = $self->_rearrange( [qw(TAXONID NAME)], @_ );
194 ( $taxonid, my @others ) = $self->get_taxonids($name);
196 "There were multiple ids ($taxonid @others) matching '$name', using '$taxonid'"
204 return unless $taxonid;
206 $taxonid =~ /^\d+$/ || $self->throw("TaxID must be integer, got [$taxonid]");
208 my ( $parent_id, $rank, $code, $divid, $gen_code, $mito, $nm, $uniq, $class );
209 # single join or two calls?
210 my $sth = $self->_prepare_cached(<<SQL);
211 SELECT tax.parent_id, tax.rank, tax.code, tax.division_id, tax.gencode_id, tax.mito_id, names.name, names.uniq_name, names.class
212 FROM taxon as tax, names
216 names.taxon_id = tax.taxon_id
219 $sth->bind_columns(\
$parent_id, \
$rank, \
$code, \
$divid, \
$gen_code, \
$mito, \
$nm, \
$uniq, \
$class);
221 $sth->execute($taxonid) or $self->throw($sth->errstr);
223 my ($sci_name, @common_names);
225 while ($sth->fetch) {
226 if ($class eq 'scientific name') {
229 push @common_names, $nm;
233 my $taxon = Bio
::Taxon
->new(
235 -common_names
=> [@common_names],
236 -ncbi_taxid
=> $taxonid,
239 # Okay, this is a pretty goofy thing; we have the parent_id in hand
240 # but can't assign it b/c of semantics (one apparently must call
241 # ancestor() to get this, which seems roundabout if the information is
244 -parent_id
=> $parent_id,
246 -division
=> $DIVISIONS[$divid]->[1],
247 -genetic_code
=> $gen_code,
248 -mito_genetic_code
=> $mito
251 # we can't use -dbh or the db_handle() method ourselves or we'll go
252 # infinite on the merge attempt
253 $taxon->{'db_handle'} = $self;
255 $self->_handle_internal_id($taxon);
260 *get_Taxonomy_Node
= \
&get_taxon
;
265 Usage : my @taxonids = $db->get_taxonids('Homo sapiens');
266 Function: Searches for a taxonid (typically ncbi_taxon_id) based on a query
267 string. Note that multiple taxonids can match to the same supplied
269 Returns : array of integer ids in list context, one of these in scalar context
270 Args : string representing taxon's name
275 my ( $self, $query ) = @_;
277 my $taxids = $self->{dbh
}->selectcol_arrayref(<<SQL);
278 SELECT taxon_id FROM names
283 return wantarray() ? @
{$taxids} : @
{$taxids}[0];
286 *get_taxonid
= \
&get_taxonids
;
288 =head2 get_Children_Taxids
290 Title : get_Children_Taxids
291 Usage : my @childrenids = $db->get_Children_Taxids
292 Function: Get the ids of the children of a node in the taxonomy
293 Returns : Array of Ids
294 Args : Bio::Taxon or a taxon_id
295 Status : deprecated (use each_Descendent())
299 sub get_Children_Taxids
{
300 my ( $self, $node ) = @_;
301 $self->deprecated(); # ?
303 # "get_Children_Taxids is deprecated, use each_Descendent instead");
306 # if ( $node->can('object_id') ) {
307 # $id = $node->object_id;
309 # elsif ( $node->can('ncbi_taxid') ) {
310 # $id = $node->ncbi_taxid;
314 # "Don't know how to extract a taxon id from the object of type "
320 #else { $id = $node }
321 #my @vals = $self->{'_parentbtree'}->get_dup($id);
328 Usage : my $ancestor_taxon = $db->ancestor($taxon)
329 Function: Retrieve the full ancestor taxon of a supplied Taxon from the
332 Args : Bio::Taxon (that was retrieved from this database)
337 my ( $self, $taxon ) = @_;
338 $self->throw("Must supply a Bio::Taxon")
339 unless ref($taxon) && $taxon->isa('Bio::Taxon');
340 $self->throw("The supplied Taxon must belong to this database")
341 unless $taxon->db_handle && $taxon->db_handle eq $self;
343 $taxon->id || $self->throw("The supplied Taxon is missing its id!");
346 # Note here we explicitly set the parent ID, but use a separate method to
347 # check whether it is defined. Mixing back-end databases, even if from the
348 # same source, should still work (since a different backend wouldn't
349 # explicitly set the parent_id)
351 if (defined $taxon->trusted_parent_id) {
352 return $self->get_taxon($taxon->parent_id);
354 # TODO: would there be any other option?
359 =head2 each_Descendent
361 Title : each_Descendent
362 Usage : my @taxa = $db->each_Descendent($taxon);
363 Function: Get all the descendents of the supplied Taxon (but not their
364 descendents, ie. not a recursive fetchall).
365 Returns : Array of Bio::Taxon objects
366 Args : Bio::Taxon (that was retrieved from this database)
370 sub each_Descendent
{
371 my ( $self, $taxon ) = @_;
372 $self->throw("Must supply a Bio::Taxon")
373 unless ref($taxon) && $taxon->isa('Bio::Taxon');
374 $self->throw("The supplied Taxon must belong to this database")
375 unless $taxon->db_handle && $taxon->db_handle eq $self; # yikes
378 $taxon->id || $self->throw("The supplied Taxon is missing its id!");
380 #my ( $parent_id, $rank, $code, $divid, $gen_code, $mito, $nm, $uniq, $class );
381 # single join or two calls?
383 # probably not optimal, maybe set up as a cached statement with bindings?
384 my $desc_ids = $self->{dbh
}->selectcol_arrayref(<<SQL) or $self->throw($self->{dbh}->errstr);
391 return unless ref $desc_ids eq 'ARRAY';
394 foreach my $desc_id (@
$desc_ids) {
395 push( @descs, $self->get_taxon($desc_id) || next );
400 =head2 Helper methods
404 =head2 index_directory
406 Title : index_directory
407 Funtion : Get/set the location that index files are stored. (this module
408 will index the supplied database)
409 Usage : $obj->index_directory($newval)
410 Returns : value of index_directory (a scalar)
411 Args : on set, new value (a scalar or undef, optional)
412 Note : kept for backwards compatibility with older DB_File implementation
416 sub index_directory
{
418 return $self->{'index_directory'} = shift if @_;
419 return $self->{'index_directory'};
425 Funtion : Get/set the name of the SQLite3 database where data is stored
426 Usage : $obj->db_name($newval)
427 Returns : value of db_name (a scalar)
428 Args : on set, new value (a scalar or undef, optional)
432 # TODO: this may need some disambiguation w/ index_directory above; for now we
433 # assume this doesn't have a full path name (though I see no reason why this
434 # shouldn't allow that)
438 return $self->{'db_name'} = shift if @_;
439 return $self->{'db_name'};
445 Funtion : Get/set the cachesize used for loading the SQLite3 database
446 Usage : $obj->cache_size($newval)
447 Returns : value of cache_size (a scalar)
448 Args : on set, new value (a scalar or undef, optional)
449 Note : we do no checking on whether this value is an integer (SQLite does this for use)
455 return $self->{'cache_size'} = shift if defined($_[0]);
456 return $self->{'cache_size'};
459 # internal method which does the indexing
461 my ( $self, $nodesfile, $namesfile, $force ) = @_;
463 # TODO: need to disambiguate using index_directory here since we only have
464 # one file. Mayeb ignore it in favor of having full path for db_name?
465 my ($dir, $db_name) = ($self->index_directory, $self->db_name);
467 # TODO: we're ignoring index_directory for now, may add support for this
469 my $dbh = DBI
->connect("dbi:SQLite:dbname=$db_name","","") or die $!;
471 $dbh->do('PRAGMA synchronous = 0'); # Non transaction safe!!!
473 if ($self->cache_size) {
474 $dbh->do('PRAGMA cache_size = '.$self->cache_size)
477 if (! -e
$db_name || $force) {
479 $self->debug("Loading taxon table data\n");
480 $self->_init_db($dbh);
481 open my $NODES, '<', $nodesfile
482 or $self->throw("Could not read node file '$nodesfile': $!");
484 # TODO: this has the really unnecessary 'OR IGNORE' option added,
485 # apparently b.c the test data expects to handle cases where the TaxID
486 # is repeated in this table (which should never happen in this table). I
487 # will likely change this to throw under those circumstances
489 my $sth = $dbh->prepare_cached(<<SQL);
490 INSERT OR IGNORE INTO taxon (taxon_id, parent_id, rank, code, division_id, gencode_id, mito_id) VALUES (?,?,?,?,?,?,?)
496 my ($taxid,$parent,$rank,$code,$divid,undef,$gen_code,undef,$mito) = split(/\t\|\t/,$_);
502 $sth->execute($taxid, $parent, $rank, $code, $divid, $gen_code, $mito) or die $sth->errstr.": TaxID $taxid";
506 # TODO:index parent_id?
509 $self->debug("Loading name table data\n");
510 open my $NAMES, '<', $namesfile
511 or $self->throw("Could not read names file '$namesfile': $!");
513 my $sth = $dbh->prepare_cached(<<SQL) or $self->throw($dbh->errstr);
514 INSERT INTO names (taxon_id, name, uniq_name, class) VALUES (?,?,?,?)
520 my ($taxid, $name, $unique_name, $class) = split(/\t\|\t/,$_);
521 # don't include the fake root node 'root' or 'all' with id 1
524 $class =~ s/\s+\|\s*$//;
525 $sth->execute($taxid, $name, $unique_name, $class) or $self->throw($sth->errstr);
528 $dbh->do("PRAGMA foreign_keys = ON");
531 $self->{'_initialized'} = 1;
536 # connect the internal db handle
539 return if $self->{'_initialized'};
541 my ($dir, $db_name) = ($self->index_directory, $self->db_name);
543 # TODO: we're ignoring index_directory for now, may add support for this
545 $self->{dbh
} = DBI
->connect("dbi:SQLite:dbname=$db_name","","") or die $!;
546 $self->{'_initialized'} = 1;
550 my ($self, $dbh) = @_;
551 my $schema = $self->taxon_schema();
552 # TODO: set up handler parameters here
553 for my $table (sort keys %$schema) {
554 $dbh->do("DROP TABLE IF EXISTS $table") or $self->throw($dbh->errstr);
555 $dbh->do("CREATE TABLE $table ".$schema->{$table}) or $self->throw($dbh->errstr);
561 my ($self, $sql) = @_;
562 # TODO: more sanity checks
563 my $rows = $self->{dbh
}->selectrow_arrayref($sql) or $self->throw( $self->{dbh
}->errstr );
567 sub _prepare_cached
{
568 my ($self, $sql) = @_;
569 # TODO: more sanity checks
570 my $sth = $self->{dbh
}->prepare_cached($sql) or $self->throw( $self->{dbh
}->errstr );
575 # TODO: check data size, this is a ballpark estimate (could be reduced)
581 taxon_id INTEGER PRIMARY KEY NOT NULL,
590 FOREIGN KEY(parent_id) REFERENCES taxon(taxon_id)
596 name_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
599 uniq_name VARCHAR(50),
601 FOREIGN KEY(taxon_id) REFERENCES taxon(taxon_id)