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