| 1 | # | ||
| 2 | # BioPerl module for FAST::Bio::DB::Taxonomy::entrez | ||
| 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::entrez - Taxonomy Entrez driver | ||
| 17 | |||
| 18 | =head1 SYNOPSIS | ||
| 19 | |||
| 20 | # Do not use this object directly, rather through the FAST::Bio::DB::Taxonomy | ||
| 21 | # interface | ||
| 22 | |||
| 23 | use FAST::Bio::DB::Taxonomy; | ||
| 24 | |||
| 25 | my $db = FAST::Bio::DB::Taxonomy->new(-source => 'entrez'); | ||
| 26 | |||
| 27 | my $taxonid = $db->get_taxonid('Homo sapiens'); | ||
| 28 | my $node = $db->get_Taxonomy_Node(-taxonid => $taxonid); | ||
| 29 | |||
| 30 | my $gi = 71836523; | ||
| 31 | my $node = $db->get_Taxonomy_Node(-gi => $gi, -db => 'protein'); | ||
| 32 | print $node->binomial, "\n"; | ||
| 33 | my ($species,$genus,$family) = $node->classification; | ||
| 34 | print "family is $family\n"; | ||
| 35 | |||
| 36 | # Can also go up 4 levels | ||
| 37 | my $p = $node; | ||
| 38 | for ( 1..4 ) { | ||
| 39 | $p = $db->get_Taxonomy_Node(-taxonid => $p->parent_id); | ||
| 40 | } | ||
| 41 | print $p->rank, " ", ($p->classification)[0], "\n"; | ||
| 42 | |||
| 43 | # could then classify a set of BLAST hits based on their GI numbers | ||
| 44 | # into taxonomic categories. | ||
| 45 | |||
| 46 | |||
| 47 | It is not currently possibly to query a node for its children so we | ||
| 48 | cannot completely replace the advantage of the flatfile | ||
| 49 | FAST::Bio::DB::Taxonomy::flatfile module. | ||
| 50 | |||
| 51 | |||
| 52 | =head1 DESCRIPTION | ||
| 53 | |||
| 54 | A driver for querying NCBI Entrez Taxonomy database. | ||
| 55 | |||
| 56 | =head1 FEEDBACK | ||
| 57 | |||
| 58 | =head2 Mailing Lists | ||
| 59 | |||
| 60 | User feedback is an integral part of the evolution of this and other | ||
| 61 | Bioperl modules. Send your comments and suggestions preferably to | ||
| 62 | the Bioperl mailing list. Your participation is much appreciated. | ||
| 63 | |||
| 64 | bioperl-l@bioperl.org - General discussion | ||
| 65 | http://bioperl.org/wiki/Mailing_lists - About the mailing lists | ||
| 66 | |||
| 67 | =head2 Support | ||
| 68 | |||
| 69 | Please direct usage questions or support issues to the mailing list: | ||
| 70 | |||
| 71 | I<bioperl-l@bioperl.org> | ||
| 72 | |||
| 73 | rather than to the module maintainer directly. Many experienced and | ||
| 74 | reponsive experts will be able look at the problem and quickly | ||
| 75 | address it. Please include a thorough description of the problem | ||
| 76 | with code and data examples if at all possible. | ||
| 77 | |||
| 78 | =head2 Reporting Bugs | ||
| 79 | |||
| 80 | Report bugs to the Bioperl bug tracking system to help us keep track | ||
| 81 | of the bugs and their resolution. Bug reports can be submitted via | ||
| 82 | the web: | ||
| 83 | |||
| 84 | https://redmine.open-bio.org/projects/bioperl/ | ||
| 85 | |||
| 86 | =head1 AUTHOR - Jason Stajich | ||
| 87 | |||
| 88 | Email jason-at-bioperl.org | ||
| 89 | |||
| 90 | =head1 CONTRIBUTORS | ||
| 91 | |||
| 92 | Sendu Bala: bix@sendu.me.uk | ||
| 93 | |||
| 94 | =head1 APPENDIX | ||
| 95 | |||
| 96 | The rest of the documentation details each of the object methods. | ||
| 97 | Internal methods are usually preceded with a _ | ||
| 98 | |||
| 99 | =cut | ||
| 100 | |||
| 101 | # Let the code begin... | ||
| 102 | |||
| 103 | package FAST::Bio::DB::Taxonomy::entrez; | ||
| 104 | use vars qw($EntrezLocation $UrlParamSeparatorValue %EntrezParams | ||
| 105 | $EntrezGet $EntrezSummary $EntrezFetch %SequenceParams | ||
| 106 | $XMLTWIG $DATA_CACHE $RELATIONS); | ||
| 107 | use strict; | ||
| 108 | |||
| 109 | use FAST::Bio::Taxon; | ||
| 110 | |||
| 111 | eval { | ||
| 112 | require XML::Twig; | ||
| 113 | $XMLTWIG = 1; | ||
| 114 | }; | ||
| 115 | if( $@ ) { | ||
| 116 | $XMLTWIG = 0; | ||
| 117 | } | ||
| 118 | |||
| 119 | use base qw(FAST::Bio::WebAgent FAST::Bio::DB::Taxonomy); | ||
| 120 | |||
| 121 | $EntrezLocation = 'http://www.ncbi.nih.gov/entrez/eutils/'; | ||
| 122 | $EntrezGet = 'esearch.fcgi'; | ||
| 123 | $EntrezFetch = 'efetch.fcgi'; | ||
| 124 | $EntrezSummary = 'esummary.fcgi'; | ||
| 125 | |||
| 126 | $DATA_CACHE = {}; | ||
| 127 | $RELATIONS = {}; | ||
| 128 | |||
| 129 | %EntrezParams = ( 'db' => 'taxonomy', | ||
| 130 | 'report' => 'xml', | ||
| 131 | 'retmode'=> 'xml', | ||
| 132 | 'tool' => 'Bioperl'); | ||
| 133 | |||
| 134 | %SequenceParams = ( 'db' => 'nucleotide', # or protein | ||
| 135 | 'retmode' => 'xml', | ||
| 136 | 'tool' => 'Bioperl'); | ||
| 137 | |||
| 138 | $UrlParamSeparatorValue = '&'; | ||
| 139 | |||
| 140 | =head2 new | ||
| 141 | |||
| 142 | Title : new | ||
| 143 | Usage : my $obj = FAST::Bio::DB::Taxonomy::entrez->new(); | ||
| 144 | Function: Builds a new FAST::Bio::DB::Taxonomy::entrez object | ||
| 145 | Returns : an instance of FAST::Bio::DB::Taxonomy::entrez | ||
| 146 | Args : -location => URL to Entrez (if you want to override the default) | ||
| 147 | -params => Hashref of URL params if you want to override the | ||
| 148 | default | ||
| 149 | |||
| 150 | =cut | ||
| 151 | |||
| 152 | sub new { | ||
| 153 | my ($class, @args) = @_; | ||
| 154 | |||
| 155 | # need to initialise FAST::Bio::WebAgent... | ||
| 156 | my ($self) = $class->SUPER::new(@args); | ||
| 157 | |||
| 158 | # ... as well as our normal FAST::Bio::DB::Taxonomy selves: | ||
| 159 | $self->_initialize(@args); | ||
| 160 | return $self; | ||
| 161 | } | ||
| 162 | |||
| 163 | sub _initialize { | ||
| 164 | my($self) = shift; | ||
| 165 | |||
| 166 | $self->SUPER::_initialize(@_); | ||
| 167 | |||
| 168 | my ($location,$params) = $self->_rearrange([qw(LOCATION PARAMS)],@_); | ||
| 169 | |||
| 170 | if( $params ) { | ||
| 171 | if( ref($params) !~ /HASH/i ) { | ||
| 172 | $self->warn("Must have provided a valid HASHref for -params"); | ||
| 173 | $params = \%EntrezParams; | ||
| 174 | } | ||
| 175 | } else { | ||
| 176 | $params = \%EntrezParams; | ||
| 177 | } | ||
| 178 | $self->entrez_params($params); | ||
| 179 | $self->entrez_url($location || $EntrezLocation ); | ||
| 180 | } | ||
| 181 | |||
| 182 | =head2 get_taxon | ||
| 183 | |||
| 184 | Title : get_taxon | ||
| 185 | Usage : my $taxon = $db->get_taxon(-taxonid => $taxonid) | ||
| 186 | Function: Get a FAST::Bio::Taxon object from the database. | ||
| 187 | Returns : FAST::Bio::Taxon object | ||
| 188 | Args : just a single value which is the database id, OR named args: | ||
| 189 | -taxonid => taxonomy id (to query by taxonid) | ||
| 190 | OR | ||
| 191 | -name => string (to query by a taxonomy name: common name, | ||
| 192 | scientific name, etc) | ||
| 193 | OR | ||
| 194 | To retrieve a taxonomy node for a GI number provide the -gi option | ||
| 195 | with the gi number and -db with either 'nucleotide' or 'protein' to | ||
| 196 | define the db. | ||
| 197 | AND optionally, | ||
| 198 | -full => 1 (to force retrieval of full information - sometimes | ||
| 199 | minimal information about your taxon may have been | ||
| 200 | cached, which is normally used to save database | ||
| 201 | accesses) | ||
| 202 | |||
| 203 | =cut | ||
| 204 | |||
| 205 | sub get_taxon { | ||
| 206 | my $self = shift; | ||
| 207 | if (! $XMLTWIG) { | ||
| 208 | eval { require XML::Twig }; | ||
| 209 | $self->throw("Could not load XML::Twig for get_taxon(): $@") if $@; | ||
| 210 | } | ||
| 211 | |||
| 212 | my %p = $self->entrez_params; | ||
| 213 | |||
| 214 | # convert input request to one or more ids | ||
| 215 | my (@taxonids, $taxonid, $want_full); | ||
| 216 | if (@_ > 1) { | ||
| 217 | my %params = @_; | ||
| 218 | if ($params{'-taxonid'}) { | ||
| 219 | $taxonid = $params{'-taxonid'}; | ||
| 220 | } | ||
| 221 | elsif ($params{'-gi'}) { | ||
| 222 | my $db = $params{'-db'}; | ||
| 223 | # we're going to do all the work here and then redirect | ||
| 224 | # the call based on the TaxId | ||
| 225 | my %p = %SequenceParams; | ||
| 226 | my %items; | ||
| 227 | if( ref($params{'-gi'}) =~ /ARRAY/i ) { | ||
| 228 | $p{'id'} = join(',', @{$params{'-gi'}}); | ||
| 229 | } else { | ||
| 230 | $p{'id'} = $params{'-gi'}; | ||
| 231 | } | ||
| 232 | $p{'db'} = $db if defined $db; | ||
| 233 | my $params = join($UrlParamSeparatorValue, map { "$_=".$p{$_} } keys %p); | ||
| 234 | my $url = sprintf("%s%s?%s",$self->entrez_url,$EntrezSummary,$params); | ||
| 235 | $self->debug("url is $url\n"); | ||
| 236 | |||
| 237 | my @ids; | ||
| 238 | if (exists $DATA_CACHE->{gi_to_ids}->{$url}) { | ||
| 239 | @ids = @{$DATA_CACHE->{gi_to_ids}->{$url}}; | ||
| 240 | } | ||
| 241 | else { | ||
| 242 | my $response = $self->get($url); | ||
| 243 | if ($response->is_success) { | ||
| 244 | $response = $response->content; | ||
| 245 | } | ||
| 246 | else { | ||
| 247 | $self->throw("Can't query website: ".$response->status_line); | ||
| 248 | } | ||
| 249 | |||
| 250 | $self->debug("resp is $response\n"); | ||
| 251 | my $twig = XML::Twig->new; | ||
| 252 | $twig->parse($response); | ||
| 253 | my $root = $twig->root; | ||
| 254 | |||
| 255 | for my $topnode ( $root->children('DocSum') ) { | ||
| 256 | for my $child ( $topnode->children('Item') ) { | ||
| 257 | if( uc($child->{att}->{'Name'}) eq 'TAXID' ) { | ||
| 258 | push @ids, $child->text; | ||
| 259 | } | ||
| 260 | } | ||
| 261 | } | ||
| 262 | |||
| 263 | $DATA_CACHE->{gi_to_ids}->{$url} = \@ids; | ||
| 264 | } | ||
| 265 | |||
| 266 | return $self->get_taxon(-taxonid => \@ids); | ||
| 267 | } | ||
| 268 | elsif ($params{'-name'}) { | ||
| 269 | @taxonids = $self->get_taxonid($params{'-name'}); | ||
| 270 | } | ||
| 271 | else { | ||
| 272 | $self->warn("Need to have provided either a -taxonid or -name value to get_taxon"); | ||
| 273 | } | ||
| 274 | |||
| 275 | if ($params{'-full'}) { | ||
| 276 | $want_full = 1; | ||
| 277 | } | ||
| 278 | } | ||
| 279 | else { | ||
| 280 | $taxonid = shift; | ||
| 281 | } | ||
| 282 | |||
| 283 | if (ref($taxonid) =~ /ARRAY/i ) { | ||
| 284 | @taxonids = @{$taxonid}; | ||
| 285 | } | ||
| 286 | else { | ||
| 287 | push(@taxonids, $taxonid) if $taxonid; | ||
| 288 | } | ||
| 289 | |||
| 290 | # return answer(s) from the cache if possible | ||
| 291 | my @results; | ||
| 292 | my @uncached; | ||
| 293 | foreach my $taxonid (@taxonids) { | ||
| 294 | $taxonid || $self->throw("In taxonids list one was undef! '@taxonids'\n"); | ||
| 295 | if (defined $DATA_CACHE->{full_info}->{$taxonid}) { | ||
| 296 | push(@results, $self->_make_taxon($DATA_CACHE->{full_info}->{$taxonid})); | ||
| 297 | } | ||
| 298 | elsif (! $want_full && defined $DATA_CACHE->{minimal_info}->{$taxonid}) { | ||
| 299 | push(@results, $self->_make_taxon($DATA_CACHE->{minimal_info}->{$taxonid})); | ||
| 300 | } | ||
| 301 | else { | ||
| 302 | push(@uncached, $taxonid); | ||
| 303 | } | ||
| 304 | } | ||
| 305 | |||
| 306 | if (@uncached > 0) { | ||
| 307 | $taxonid = join(',', @uncached); | ||
| 308 | |||
| 309 | $p{'id'} = $taxonid; | ||
| 310 | $self->debug("id is $taxonid\n"); | ||
| 311 | my $params = join($UrlParamSeparatorValue, map { "$_=".$p{$_} } keys %p); | ||
| 312 | |||
| 313 | my $url = sprintf("%s%s?%s",$self->entrez_url,$EntrezFetch,$params); | ||
| 314 | $self->debug("url is $url\n"); | ||
| 315 | my $response = $self->get($url); | ||
| 316 | if ($response->is_success) { | ||
| 317 | $response = $response->content; | ||
| 318 | } | ||
| 319 | else { | ||
| 320 | $self->throw("Can't query website: ".$response->status_line); | ||
| 321 | } | ||
| 322 | $self->debug("resp is $response\n"); | ||
| 323 | |||
| 324 | my $twig = XML::Twig->new; | ||
| 325 | $twig->parse($response); | ||
| 326 | |||
| 327 | my $root = $twig->root; | ||
| 328 | for my $taxon ( $root->children('Taxon') ) { | ||
| 329 | my $taxid = $taxon->first_child_text('TaxId'); | ||
| 330 | $self->throw("Got a result with no TaxId!") unless $taxid; | ||
| 331 | |||
| 332 | my $data = {}; | ||
| 333 | if (exists $DATA_CACHE->{minimal_info}->{$taxid}) { | ||
| 334 | $data = $DATA_CACHE->{minimal_info}->{$taxid}; | ||
| 335 | } | ||
| 336 | |||
| 337 | $data->{id} = $taxid; | ||
| 338 | $data->{rank} = $taxon->first_child_text('Rank'); | ||
| 339 | |||
| 340 | my $other_names = $taxon->first_child('OtherNames'); | ||
| 341 | my @other_names = $other_names->children_text() if $other_names; | ||
| 342 | my $sci_name = $taxon->first_child_text('ScientificName'); | ||
| 343 | my $orig_sci_name = $sci_name; | ||
| 344 | $sci_name =~ s/ \(class\)$//; | ||
| 345 | push(@other_names, $orig_sci_name) if $orig_sci_name ne $sci_name; | ||
| 346 | $data->{scientific_name} = $sci_name; | ||
| 347 | $data->{common_names} = \@other_names; | ||
| 348 | |||
| 349 | $data->{division} = $taxon->first_child_text('Division'); | ||
| 350 | $data->{genetic_code} = $taxon->first_child('GeneticCode')->first_child_text('GCId'); | ||
| 351 | $data->{mitochondrial_genetic_code} = $taxon->first_child('MitoGeneticCode')->first_child_text('MGCId'); | ||
| 352 | $data->{create_date} = $taxon->first_child_text('CreateDate'); | ||
| 353 | $data->{update_date} = $taxon->first_child_text('UpdateDate'); | ||
| 354 | $data->{pub_date} = $taxon->first_child_text('PubDate'); | ||
| 355 | |||
| 356 | # since we have some information about all the ancestors of our | ||
| 357 | # requested node, we may as well cache data for the ancestors to | ||
| 358 | # reduce the number of accesses to website in future | ||
| 359 | my $lineage_ex = $taxon->first_child('LineageEx'); | ||
| 360 | my ($ancestor, $lineage_data, @taxa); | ||
| 361 | foreach my $lineage_taxon ($lineage_ex->children) { | ||
| 362 | my $lineage_taxid = $lineage_taxon->first_child_text('TaxId'); | ||
| 363 | |||
| 364 | if (exists $DATA_CACHE->{minimal_info}->{$lineage_taxid} || exists $DATA_CACHE->{full_info}->{$lineage_taxid}) { | ||
| 365 | $lineage_data = $DATA_CACHE->{minimal_info}->{$lineage_taxid} || $DATA_CACHE->{full_info}->{$lineage_taxid}; | ||
| 366 | next; | ||
| 367 | } | ||
| 368 | else { | ||
| 369 | $lineage_data = {}; | ||
| 370 | } | ||
| 371 | |||
| 372 | $lineage_data->{id} = $lineage_taxid; | ||
| 373 | $lineage_data->{scientific_name} = $lineage_taxon->first_child_text('ScientificName'); | ||
| 374 | $lineage_data->{rank} = $lineage_taxon->first_child_text('Rank'); | ||
| 375 | |||
| 376 | $RELATIONS->{ancestors}->{$lineage_taxid} = $ancestor->{id} if $ancestor; | ||
| 377 | |||
| 378 | $DATA_CACHE->{minimal_info}->{$lineage_taxid} = $lineage_data; | ||
| 379 | } continue { $ancestor = $lineage_data; unshift(@taxa, $lineage_data); } | ||
| 380 | |||
| 381 | $RELATIONS->{ancestors}->{$taxid} = $ancestor->{id} if $ancestor; | ||
| 382 | |||
| 383 | # go through the lineage in reverse so we can remember the children | ||
| 384 | my $child = $data; | ||
| 385 | foreach my $lineage_data (@taxa) { | ||
| 386 | $RELATIONS->{children}->{$lineage_data->{id}}->{$child->{id}} = 1; | ||
| 387 | } continue { $child = $lineage_data; } | ||
| 388 | |||
| 389 | delete $DATA_CACHE->{minimal_info}->{$taxid}; | ||
| 390 | $DATA_CACHE->{full_info}->{$taxid} = $data; | ||
| 391 | push(@results, $self->_make_taxon($data)); | ||
| 392 | } | ||
| 393 | } | ||
| 394 | |||
| 395 | wantarray() ? @results : shift @results; | ||
| 396 | } | ||
| 397 | |||
| 398 | *get_Taxonomy_Node = \&get_taxon; | ||
| 399 | |||
| 400 | =head2 get_taxonids | ||
| 401 | |||
| 402 | Title : get_taxonids | ||
| 403 | Usage : my $taxonid = $db->get_taxonids('Homo sapiens'); | ||
| 404 | Function: Searches for a taxonid (typically ncbi_taxon_id) based on a query | ||
| 405 | string. Note that multiple taxonids can match to the same supplied | ||
| 406 | name. | ||
| 407 | Returns : array of integer ids in list context, one of these in scalar context | ||
| 408 | Args : string representing taxon's name | ||
| 409 | |||
| 410 | =cut | ||
| 411 | |||
| 412 | sub get_taxonids { | ||
| 413 | my ($self,$query) = @_; | ||
| 414 | my %p = $self->entrez_params; | ||
| 415 | |||
| 416 | # queries don't work correctly with special characters, so get rid of them. | ||
| 417 | if ($query =~ /<.+>/) { | ||
| 418 | # queries with <something> will fail, so workaround by removing, doing | ||
| 419 | # the query, getting multiple taxonids, then picking the one id that | ||
| 420 | # has a parent node with a scientific_name() or common_names() | ||
| 421 | # case-insensitive matching to the word(s) within <> | ||
| 422 | $query =~ s/ <(.+?)>//; | ||
| 423 | my $desired_parent_name = lc($1); | ||
| 424 | |||
| 425 | ID: foreach my $start_id ($self->get_taxonids($query)) { | ||
| 426 | my $node = $self->get_taxon($start_id) || next ID; | ||
| 427 | |||
| 428 | # walk up the parents until we hit a node with a named rank | ||
| 429 | while (1) { | ||
| 430 | my $parent_node = $self->ancestor($node) || next ID; | ||
| 431 | my $parent_sci_name = $parent_node->scientific_name || next ID; | ||
| 432 | my @parent_common_names = $parent_node->common_names; | ||
| 433 | unless (@parent_common_names) { | ||
| 434 | # ensure we're not using a minimal-info cached version | ||
| 435 | $parent_node = $self->get_taxon(-taxonid => $parent_node->id, -full => 1); | ||
| 436 | @parent_common_names = $parent_node->common_names; | ||
| 437 | } | ||
| 438 | |||
| 439 | foreach my $name ($parent_sci_name, @parent_common_names) { | ||
| 440 | if (lc($name) eq $desired_parent_name) { | ||
| 441 | return wantarray() ? ($start_id) : $start_id; | ||
| 442 | } | ||
| 443 | } | ||
| 444 | |||
| 445 | my $parent_rank = $parent_node->rank || 'no rank'; | ||
| 446 | if ($parent_rank ne 'no rank') { | ||
| 447 | last; | ||
| 448 | } | ||
| 449 | else { | ||
| 450 | $node = $parent_node; | ||
| 451 | } | ||
| 452 | } | ||
| 453 | } | ||
| 454 | return; | ||
| 455 | } | ||
| 456 | $query =~ s/[\"\(\)]//g; # not an exhaustive list; these are just the ones I know cause problems | ||
| 457 | $query =~ s/\s/+/g; | ||
| 458 | |||
| 459 | my @data; | ||
| 460 | if (defined $DATA_CACHE->{name_to_id}->{$query}) { | ||
| 461 | @data = @{$DATA_CACHE->{name_to_id}->{$query}}; | ||
| 462 | } | ||
| 463 | else { | ||
| 464 | $p{'term'} = $query; | ||
| 465 | my $params = join($UrlParamSeparatorValue, map { "$_=".$p{$_} } keys %p); | ||
| 466 | my $url = sprintf("%s%s?%s",$self->entrez_url,$EntrezGet,$params); | ||
| 467 | my $response = $self->get($url); | ||
| 468 | if ($response->is_success) { | ||
| 469 | $response = $response->content; | ||
| 470 | } | ||
| 471 | else { | ||
| 472 | $self->throw("Can't query website: ".$response->status_line); | ||
| 473 | } | ||
| 474 | $self->debug("response is $response\n"); | ||
| 475 | my $twig = XML::Twig->new; | ||
| 476 | $twig->parse($response); | ||
| 477 | my $root = $twig->root; | ||
| 478 | my $list = $root->first_child('IdList'); | ||
| 479 | @data = map { $_->text } $list->children('Id'); | ||
| 480 | |||
| 481 | $DATA_CACHE->{name_to_id}->{$query} = [@data]; | ||
| 482 | } | ||
| 483 | |||
| 484 | wantarray() ? @data : shift @data; | ||
| 485 | } | ||
| 486 | |||
| 487 | *get_taxonid = \&get_taxonids; | ||
| 488 | |||
| 489 | =head2 ancestor | ||
| 490 | |||
| 491 | Title : ancestor | ||
| 492 | Usage : my $ancestor_taxon = $db->ancestor($taxon) | ||
| 493 | Function: Retrieve the ancestor taxon of a supplied Taxon from the database. | ||
| 494 | |||
| 495 | Note that unless the ancestor has previously been directly | ||
| 496 | requested with get_taxon(), the returned Taxon object will only have | ||
| 497 | a minimal amount of information. | ||
| 498 | |||
| 499 | Returns : FAST::Bio::Taxon | ||
| 500 | Args : FAST::Bio::Taxon (that was retrieved from this database) | ||
| 501 | |||
| 502 | =cut | ||
| 503 | |||
| 504 | sub ancestor { | ||
| 505 | my ($self, $taxon) = @_; | ||
| 506 | $self->throw("Must supply a FAST::Bio::Taxon") unless ref($taxon) && $taxon->isa('FAST::Bio::Taxon'); | ||
| 507 | $self->throw("The supplied Taxon must belong to this database") unless $taxon->db_handle && $taxon->db_handle eq $self; | ||
| 508 | my $id = $taxon->id || $self->throw("The supplied Taxon is missing its id!"); | ||
| 509 | |||
| 510 | my $ancestor_id = $RELATIONS->{ancestors}->{$id} || return; | ||
| 511 | return $self->_make_taxon($DATA_CACHE->{full_info}->{$ancestor_id} || $DATA_CACHE->{minimal_info}->{$ancestor_id}); | ||
| 512 | } | ||
| 513 | |||
| 514 | =head2 each_Descendent | ||
| 515 | |||
| 516 | Title : each_Descendent | ||
| 517 | Usage : my @taxa = $db->each_Descendent($taxon); | ||
| 518 | Function: Get all the descendents of the supplied Taxon (but not their | ||
| 519 | descendents, ie. not a recursive fetchall). | ||
| 520 | |||
| 521 | Note that this implementation is unable to return a taxon that | ||
| 522 | hasn't previously been directly fetched with get_taxon(), or wasn't | ||
| 523 | an ancestor of such a fetch. | ||
| 524 | |||
| 525 | Returns : Array of FAST::Bio::Taxon objects | ||
| 526 | Args : FAST::Bio::Taxon (that was retrieved from this database) | ||
| 527 | |||
| 528 | =cut | ||
| 529 | |||
| 530 | sub each_Descendent { | ||
| 531 | my ($self, $taxon) = @_; | ||
| 532 | $self->throw("Must supply a FAST::Bio::Taxon") unless ref($taxon) && $taxon->isa('FAST::Bio::Taxon'); | ||
| 533 | $self->throw("The supplied Taxon must belong to this database") unless $taxon->db_handle && $taxon->db_handle eq $self; | ||
| 534 | my $id = $taxon->id || $self->throw("The supplied Taxon is missing its id!"); | ||
| 535 | |||
| 536 | my @children_ids = keys %{$RELATIONS->{children}->{$id} || {}}; | ||
| 537 | my @children; | ||
| 538 | foreach my $child_id (@children_ids) { | ||
| 539 | push(@children, $self->_make_taxon($DATA_CACHE->{full_info}->{$child_id} || $DATA_CACHE->{minimal_info}->{$child_id})); | ||
| 540 | } | ||
| 541 | |||
| 542 | return @children; | ||
| 543 | } | ||
| 544 | |||
| 545 | =head2 Some Get/Setter methods | ||
| 546 | |||
| 547 | =cut | ||
| 548 | |||
| 549 | =head2 entrez_url | ||
| 550 | |||
| 551 | Title : entrez_url | ||
| 552 | Usage : $obj->entrez_url($newval) | ||
| 553 | Function: Get/set entrez URL | ||
| 554 | Returns : value of entrez url (a scalar) | ||
| 555 | Args : on set, new value (a scalar or undef, optional) | ||
| 556 | |||
| 557 | =cut | ||
| 558 | |||
| 559 | sub entrez_url{ | ||
| 560 | my $self = shift; | ||
| 561 | |||
| 562 | return $self->{'_entrez_url'} = shift if @_; | ||
| 563 | return $self->{'_entrez_url'}; | ||
| 564 | } | ||
| 565 | |||
| 566 | =head2 entrez_params | ||
| 567 | |||
| 568 | Title : entrez_params | ||
| 569 | Usage : $obj->entrez_params($newval) | ||
| 570 | Function: Get/set entrez params | ||
| 571 | Returns : value of entrez_params (a hashref) | ||
| 572 | Args : on set, new value Hashref | ||
| 573 | |||
| 574 | =cut | ||
| 575 | |||
| 576 | sub entrez_params{ | ||
| 577 | my $self = shift; | ||
| 578 | my $f; | ||
| 579 | if( @_ ) { | ||
| 580 | $f = $self->{'_entrez_params'} = shift; | ||
| 581 | } else { | ||
| 582 | $f = $self->{'_entrez_params'}; | ||
| 583 | } | ||
| 584 | return %$f; | ||
| 585 | } | ||
| 586 | |||
| 587 | =head2 FAST::Bio::DB::WebBase methods | ||
| 588 | |||
| 589 | =cut | ||
| 590 | |||
| 591 | =head2 proxy_string | ||
| 592 | |||
| 593 | Title : proxy_string | ||
| 594 | Usage : my $proxy_string = $self->proxy_string($protocol) | ||
| 595 | Function: Get the proxy string (plus user/pass ) | ||
| 596 | Returns : string | ||
| 597 | Args : protocol ('http' or 'ftp'), default 'http' | ||
| 598 | |||
| 599 | =head2 proxy | ||
| 600 | |||
| 601 | Title : proxy | ||
| 602 | Usage : $httpproxy = $db->proxy('http') or | ||
| 603 | $db->proxy(['http','ftp'], 'http://myproxy' ) | ||
| 604 | Function: Get/Set a proxy for use of proxy | ||
| 605 | Returns : a string indicating the proxy | ||
| 606 | Args : $protocol : an array ref of the protocol(s) to set/get | ||
| 607 | $proxyurl : url of the proxy to use for the specified protocol | ||
| 608 | $username : username (if proxy requires authentication) | ||
| 609 | $password : password (if proxy requires authentication) | ||
| 610 | |||
| 611 | =head2 authentication | ||
| 612 | |||
| 613 | Title : authentication | ||
| 614 | Usage : $db->authentication($user,$pass) | ||
| 615 | Function: Get/Set authentication credentials | ||
| 616 | Returns : Array of user/pass | ||
| 617 | Args : Array or user/pass | ||
| 618 | |||
| 619 | =cut | ||
| 620 | |||
| 621 | # make a Taxon object from data hash ref | ||
| 622 | sub _make_taxon { | ||
| 623 | my ($self, $data) = @_; | ||
| 624 | |||
| 625 | my $taxon = FAST::Bio::Taxon->new(); | ||
| 626 | |||
| 627 | my $taxid; | ||
| 628 | while (my ($method, $value) = each %{$data}) { | ||
| 629 | if ($method eq 'id') { | ||
| 630 | $method = 'ncbi_taxid'; # since this is a real ncbi taxid, explicitly set it as one | ||
| 631 | $taxid = $value; | ||
| 632 | } | ||
| 633 | $taxon->$method(ref($value) eq 'ARRAY' ? @{$value} : $value); | ||
| 634 | } | ||
| 635 | |||
| 636 | # we can't use -dbh or the db_handle() method ourselves or we'll go | ||
| 637 | # infinite on the merge attempt | ||
| 638 | $taxon->{'db_handle'} = $self; | ||
| 639 | |||
| 640 | $self->_handle_internal_id($taxon); | ||
| 641 | |||
| 642 | return $taxon; | ||
| 643 | } | ||
| 644 | |||
| 645 | 1; |