Taxonomy/entrez.pm

filter: Statementstmt 27.7% 70 / 252 Branchbran 8.0% 8 / 100 Conditioncond 2.0% 1 / 48 MC/DCmcdc 0.0% 0 / 30 Subroutinesub 70.5% 12 / 17 Podpod 100% 8 / 8 totaltotal 21.7% 99 / 455 CC 78 SCAR 63.8
CC
78
Cov
20%
get_taxon
1332.0
get_taxonids
342.0
each_Descendent
90.0
CRAP 591.8
↑ Summary NCBIHelper.pm »
1 #
2 # BioPerl module for 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 Bio::DB::Taxonomy::entrez - Taxonomy Entrez driver
17  
18 =head1 SYNOPSIS
19  
20 # Do not use this object directly, rather through the Bio::DB::Taxonomy
21 # interface
22  
23 use Bio::DB::Taxonomy;
24  
25 my $db = 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 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://github.com/bioperl/bioperl-live/issues
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 Bio::DB::Taxonomy::entrez;
104 2 $Bio::DB::Taxonomy::entrez::VERSION = '1.7.8';
105 2 ▶use vars qw($EntrezLocation $UrlParamSeparatorValue %EntrezParams
Subroutine BEGIN: called CC=1 SCAR=0.0
106 $EntrezGet $EntrezSummary $EntrezFetch %SequenceParams
107 2 $XMLTWIG $DATA_CACHE $RELATIONS);
108 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
109 2 ▶use Bio::Taxon;
Subroutine BEGIN: called CC=1 SCAR=0.0
110 2 ▶use Bio::DB::NCBIHelper;
Subroutine BEGIN: called CC=1 SCAR=0.0
111  
112 2 eval {
113 2 require XML::Twig;
114 0 $XMLTWIG = 1;
115 };
116 2 ▶if( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 2 0 2
117 2 $XMLTWIG = 0;
118 }
119  
120 2 ▶use base qw(Bio::WebAgent Bio::DB::Taxonomy);
Subroutine BEGIN: called CC=1 SCAR=0.0
121  
122 2 $EntrezLocation = $Bio::DB::NCBIHelper::HOSTBASE . '/entrez/eutils/';
123 2 $EntrezGet = 'esearch.fcgi';
124 2 $EntrezFetch = 'efetch.fcgi';
125 2 $EntrezSummary = 'esummary.fcgi';
126  
127 2 $DATA_CACHE = {};
128 2 $RELATIONS = {};
129  
130 2 %EntrezParams = ( 'db' => 'taxonomy',
131 'report' => 'xml',
132 'retmode'=> 'xml',
133 'tool' => 'Bioperl');
134  
135 2 %SequenceParams = ( 'db' => 'nucleotide', # or protein
136 'retmode' => 'xml',
137 'tool' => 'Bioperl');
138  
139 2 $UrlParamSeparatorValue = '&';
140  
141  
142 =head2 new
143  
144 Title : new
145 Usage : my $obj = Bio::DB::Taxonomy::entrez->new();
146 Function: Builds a new Bio::DB::Taxonomy::entrez object
147 Returns : an instance of Bio::DB::Taxonomy::entrez
148 Args : -location => URL to Entrez (if you want to override the default)
149 -params => Hashref of URL params if you want to override the
150 default
151  
152 =cut
153  
154 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
155 2 my ($class, @args) = @_;
156  
157 # need to initialise Bio::WebAgent...
158 2 my ($self) = $class->SUPER::new(@args);
159  
160 # ... as well as our normal Bio::DB::Taxonomy selves:
161 2 $self->_initialize(@args);
162 2 return $self;
163 }
164  
165  
166 2 ▶sub _initialize {
Subroutine _initialize: called CC=5 SCAR=20.4
167 2 my($self) = shift;
168  
169 2 $self->SUPER::_initialize(@_);
170  
171 2 my ($location,$params,$email) = $self->_rearrange([qw(LOCATION PARAMS EMAIL)],@_);
172  
173 2 ▶ if( $params ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params) { } 0 2 2
174 0 ▶ if( ref($params) !~ /HASH/i ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref($params) =~ /HASH/i) 0 0 0
175 0 $self->warn("Must have provided a valid HASHref for -params");
176 0 $params = \%EntrezParams;
177 }
178 } else {
179 2 $params = \%EntrezParams;
180 }
181 2 ▶ if ($email) {
Branch: 1 branch
BranchTrueFalseTotal
if ($email) 0 2 2
182 0 $params->{email} = $email;
183 }
184 2 $self->entrez_params($params);
185 2 ▶ $self->entrez_url($location || $EntrezLocation );
Condition33%1/3
$location || $Bio::DB::Taxonomy::entrez::EntrezLocation
l!l&&r!l&&!r
020
MC/DC0%0/2
$location || $Bio::DB::Taxonomy::entrez::EntrezLocation
$location $Bio::DB::Taxonomy::entrez::EntrezLocation
Truth table33%1/3
A || B
A= $locationB= $Bio::DB::Taxonomy::entrez::EntrezLocation
ABresult
1X1
011
000
186 }
187  
188  
189 =head2 get_num_taxa
190  
191 Title : get_num_taxa
192 Usage : my $num = $db->get_num_taxa();
193 Function: Get the number of taxa stored in the database.
194 Returns : A number
195 Args : None
196  
197 =cut
198  
199 1 ▶sub get_num_taxa {
Subroutine get_num_taxa: called CC=1 SCAR=0.4
Pod documented
200 1 my ($self) = @_;
201 # Use this URL query to get the ID of all the taxa in the NCBI Taxonomy database:
202 # https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=taxonomy&term=all[Filter]
203 # Only the first 20 taxa IDs are returned (good because the list is long),
204 # and the total number is reported as well (which is what we are interested
205 # in).
206 1 my %p = $self->entrez_params;
207 1 $p{'term'} = 'all[Filter]';
208 1 my $twig = $self->_run_query($self->_build_url($EntrezGet, \%p));
209 0 my $count = $twig->root->first_child('Count')->first_child->text;
210 0 return $count;
211 }
212  
213  
214 =head2 get_taxon
215  
216 Title : get_taxon
217 Usage : my $taxon = $db->get_taxon(-taxonid => $taxonid)
218 Function: Get a Bio::Taxon object from the database.
219 Returns : Bio::Taxon object
220 Args : just a single value which is the database id, OR named args:
221 -taxonid => taxonomy id (to query by taxonid)
222 OR
223 -name => string (to query by a taxonomy name: common name,
224 scientific name, etc)
225 OR
226 To retrieve a taxonomy node for a GI number provide the -gi option
227 with the gi number and -db with either 'nucleotide' or 'protein' to
228 define the db.
229 AND optionally,
230 -full => 1 (to force retrieval of full information - sometimes
231 minimal information about your taxon may have been
232 cached, which is normally used to save database
233 accesses)
234  
235 =cut
236  
237 0 ▶sub get_taxon {
Subroutine get_taxon: not called CC=36 SCAR=71.9
Pod documented
238 0 my $self = shift;
239 0 ▶ if (! $XMLTWIG) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Bio::DB::Taxonomy::entrez::XMLTWIG) 0 0 0
240 0 eval { require XML::Twig };
241 0 ▶ $self->throw("Could not load XML::Twig for get_taxon(): $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
242 }
243  
244 0 my %p = $self->entrez_params;
245  
246 # convert input request to one or more ids
247 0 my (@taxonids, $taxonid, $want_full);
248 0 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) { } 0 0 0
249 0 my %params = @_;
250 0 ▶ if ($params{'-taxonid'}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($params{'-taxonid'}) { } 0 0 0
elsif ($params{'-gi'}) { } 0 0 0
elsif ($params{'-name'}) { } 0 0 0
251 0 $taxonid = $params{'-taxonid'};
252 }
253 elsif ($params{'-gi'}) {
254 0 my $db = $params{'-db'};
255 # we're going to do all the work here and then redirect
256 # the call based on the TaxId
257 0 my %p = %SequenceParams;
258 0 my %items;
259 0 ▶ if( ref($params{'-gi'}) =~ /ARRAY/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($params{'-gi'}) =~ /ARRAY/i) { } 0 0 0
260 0 $p{'id'} = join(',', @{$params{'-gi'}});
261 } else {
262 0 $p{'id'} = $params{'-gi'};
263 }
264 0 ▶ $p{'db'} = $db if defined $db;
Branch: 1 branch
BranchTrueFalseTotal
if defined $db 0 0 0
265 0 my $url = $self->_build_url($EntrezSummary, \%p);
266 0 my @ids;
267 0 ▶ if (exists $DATA_CACHE->{gi_to_ids}->{$url}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Bio::DB::Taxonomy::entrez::DATA_CACHE->{'gi_to_ids'}{$url}) { } 0 0 0
268 0 @ids = @{$DATA_CACHE->{gi_to_ids}->{$url}};
269 }
270 else {
271 0 my $twig = $self->_run_query($url);
272 0 my $root = $twig->root;
273 0 for my $topnode ( $root->children('DocSum') ) {
274 0 for my $child ( $topnode->children('Item') ) {
275 0 ▶ if( uc($child->{att}->{'Name'}) eq 'TAXID' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (uc $child->{"att"}{"Name"} eq "TAXID") 0 0 0
276 0 push @ids, $child->text;
277 }
278 }
279 }
280  
281 0 $DATA_CACHE->{gi_to_ids}->{$url} = \@ids;
282 }
283  
284 0 return $self->get_taxon(-taxonid => \@ids);
285 }
286 elsif ($params{'-name'}) {
287 0 @taxonids = $self->get_taxonid($params{'-name'});
288 }
289 else {
290 0 $self->warn("Need to have provided either a -taxonid or -name value to get_taxon");
291 }
292  
293 0 ▶ if ($params{'-full'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"-full"}) 0 0 0
294 0 $want_full = 1;
295 }
296 }
297 else {
298 0 $taxonid = shift;
299 }
300  
301 0 ▶ if (ref($taxonid) =~ /ARRAY/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($taxonid) =~ /ARRAY/i) { } 0 0 0
302 0 @taxonids = @{$taxonid};
303 }
304 else {
305 0 ▶ push(@taxonids, $taxonid) if $taxonid;
Branch: 1 branch
BranchTrueFalseTotal
if $taxonid 0 0 0
306 }
307  
308 # return answer(s) from the cache if possible
309 0 my @results;
310 my @uncached;
311 0 foreach my $taxonid (@taxonids) {
312 0 ▶ $taxonid || $self->throw("In taxonids list one was undef! '@taxonids'\n");
Branch: 1 branch
BranchTrueFalseTotal
$taxonid or $self->throw("In taxonids list one was undef! '@{taxonids}'\n") 0 0 0
313 0 ▶ if (defined $DATA_CACHE->{full_info}->{$taxonid}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $Bio::DB::Taxonomy::entrez::DATA_CACHE->{'full_info'}{$taxonid}) { } 0 0 0
elsif (not $want_full and defined $Bio::DB::Taxonomy::entrez::DATA_CACHE->{'minimal_info'}{$taxonid}) { } 0 0 0
314 0 push(@results, $self->_make_taxon($DATA_CACHE->{full_info}->{$taxonid}));
315 }
316 ▶ elsif (! $want_full && defined $DATA_CACHE->{minimal_info}->{$taxonid}) {
Condition0%0/3
not $want_full and defined $Bio::DB::Taxonomy::entrez::DATA_CACHE->{"minimal_info"}{$taxonid}
!ll&&!rl&&r
000
MC/DC0%0/2
not $want_full and defined $Bio::DB::Taxonomy::entrez::DATA_CACHE->{"minimal_info"}{$taxonid}
not $want_full defined $Bio::DB::Taxonomy::entrez::DATA_CACHE->{"minimal_info"}{$taxonid}
Truth table0%0/3
not(A) and B
A= not $want_fullB= defined $Bio::DB::Taxonomy::entrez::DATA_CACHE->{"minimal_info"}{$taxonid}
ABresult
0X0
100
111
317 0 push(@results, $self->_make_taxon($DATA_CACHE->{minimal_info}->{$taxonid}));
318 }
319 else {
320 0 push(@uncached, $taxonid);
321 }
322 }
323  
324 0 ▶ if (@uncached > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@uncached > 0) 0 0 0
325 0 $taxonid = join(',', @uncached);
326  
327 0 $p{'id'} = $taxonid;
328  
329 0 my $twig = $self->_run_query($self->_build_url($EntrezFetch, \%p));
330 0 my $root = $twig->root;
331 0 for my $taxon ( $root->children('Taxon') ) {
332 0 my $taxid = $taxon->first_child_text('TaxId');
333 0 ▶ $self->throw("Got a result with no TaxId!") unless $taxid;
Branch: 1 branch
BranchTrueFalseTotal
unless $taxid 0 0 0
334  
335 0 my $data = {};
336 0 ▶ if (exists $DATA_CACHE->{minimal_info}->{$taxid}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Bio::DB::Taxonomy::entrez::DATA_CACHE->{"minimal_info"}{$taxid}) 0 0 0
337 0 $data = $DATA_CACHE->{minimal_info}->{$taxid};
338 }
339  
340 0 $data->{id} = $taxid;
341 0 $data->{rank} = $taxon->first_child_text('Rank');
342  
343 0 my $other_names = $taxon->first_child('OtherNames');
344 0 ▶ my @other_names = $other_names->children_text() if $other_names;
Branch: 1 branch
BranchTrueFalseTotal
if $other_names 0 0 0
345 0 my $sci_name = $taxon->first_child_text('ScientificName');
346 0 my $orig_sci_name = $sci_name;
347 0 $sci_name =~ s/ \(class\)$//;
348 0 ▶ push(@other_names, $orig_sci_name) if $orig_sci_name ne $sci_name;
Branch: 1 branch
BranchTrueFalseTotal
if $orig_sci_name ne $sci_name 0 0 0
349 0 $data->{scientific_name} = $sci_name;
350 0 $data->{common_names} = \@other_names;
351  
352 0 $data->{division} = $taxon->first_child_text('Division');
353 0 $data->{genetic_code} = $taxon->first_child('GeneticCode')->first_child_text('GCId');
354 0 $data->{mitochondrial_genetic_code} = $taxon->first_child('MitoGeneticCode')->first_child_text('MGCId');
355 0 $data->{create_date} = $taxon->first_child_text('CreateDate');
356 0 $data->{update_date} = $taxon->first_child_text('UpdateDate');
357 0 $data->{pub_date} = $taxon->first_child_text('PubDate');
358  
359 # since we have some information about all the ancestors of our
360 # requested node, we may as well cache data for the ancestors to
361 # reduce the number of accesses to website in future
362 0 my $lineage_ex = $taxon->first_child('LineageEx');
363 0 ▶ if (defined $lineage_ex) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lineage_ex) 0 0 0
364 0 my ($ancestor, $lineage_data, @taxa);
365 0 foreach my $lineage_taxon ($lineage_ex->children) {
366 0 my $lineage_taxid = $lineage_taxon->first_child_text('TaxId');
367  
368 0 ▶ if (exists $DATA_CACHE->{minimal_info}->{$lineage_taxid} || exists $DATA_CACHE->{full_info}->{$lineage_taxid}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Bio::DB::Taxonomy::entrez::DATA_CACHE->{'minimal_info'}{$lineage_taxid} or exists $Bio::DB::Taxonomy::entrez::DATA_CACHE->{'full_info'}{$lineage_taxid}) { } 0 0 0
Condition0%0/3
exists $Bio::DB::Taxonomy::entrez::DATA_CACHE->{"minimal_info"}{$lineage_taxid} or exists $Bio::DB::Taxonomy::entrez::DATA_CACHE->{"full_info"}{$lineage_taxid}
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $Bio::DB::Taxonomy::entrez::DATA_CACHE->{"minimal_info"}{$lineage_taxid} or exists $Bio::DB::Taxonomy::entrez::DATA_CACHE->{"full_info"}{$lineage_taxid}
exists $Bio::DB::Taxonomy::entrez::DATA_CACHE->{"minimal_info"}{$lineage_taxid} exists $Bio::DB::Taxonomy::entrez::DATA_CACHE->{"full_info"}{$lineage_taxid}
Truth table0%0/3
A or B
A= exists $Bio::DB::Taxonomy::entrez::DATA_CACHE->{"minimal_info"}{$lineage_taxid}B= exists $Bio::DB::Taxonomy::entrez::DATA_CACHE->{"full_info"}{$lineage_taxid}
ABresult
1X1
011
000
369 0 ▶ $lineage_data = $DATA_CACHE->{minimal_info}->{$lineage_taxid} || $DATA_CACHE->{full_info}->{$lineage_taxid};
Condition0%0/3
$Bio::DB::Taxonomy::entrez::DATA_CACHE->{'minimal_info'}{$lineage_taxid} || $Bio::DB::Taxonomy::entrez::DATA_CACHE->{'full_info'}{$lineage_taxid}
l!l&&r!l&&!r
000
MC/DC0%0/2
$Bio::DB::Taxonomy::entrez::DATA_CACHE->{'minimal_info'}{$lineage_taxid} || $Bio::DB::Taxonomy::entrez::DATA_CACHE->{'full_info'}{$lineage_taxid}
$Bio::DB::Taxonomy::entrez::DATA_CACHE->{'minimal_info'}{$lineage_taxid} $Bio::DB::Taxonomy::entrez::DATA_CACHE->{'full_info'}{$lineage_taxid}
Truth table0%0/3
A || B
A= $Bio::DB::Taxonomy::entrez::DATA_CACHE->{'minimal_info'}{$lineage_taxid}B= $Bio::DB::Taxonomy::entrez::DATA_CACHE->{'full_info'}{$lineage_taxid}
ABresult
1X1
011
000
370 0 next;
371 }
372 else {
373 0 $lineage_data = {};
374 }
375  
376 0 $lineage_data->{id} = $lineage_taxid;
377 0 $lineage_data->{scientific_name} = $lineage_taxon->first_child_text('ScientificName');
378 0 $lineage_data->{rank} = $lineage_taxon->first_child_text('Rank');
379  
380 0 ▶ $RELATIONS->{ancestors}->{$lineage_taxid} = $ancestor->{id} if $ancestor;
Branch: 1 branch
BranchTrueFalseTotal
if $ancestor 0 0 0
381  
382 0 $DATA_CACHE->{minimal_info}->{$lineage_taxid} = $lineage_data;
383 0 } continue { $ancestor = $lineage_data; unshift(@taxa, $lineage_data); }
384  
385 0 ▶ $RELATIONS->{ancestors}->{$taxid} = $ancestor->{id} if $ancestor;
Branch: 1 branch
BranchTrueFalseTotal
if $ancestor 0 0 0
386  
387 # go through the lineage in reverse so we can remember the children
388 0 my $child = $data;
389 0 foreach my $lineage_data (@taxa) {
390 0 $RELATIONS->{children}->{$lineage_data->{id}}->{$child->{id}} = 1;
391 0 } continue { $child = $lineage_data; }
392 }
393  
394 0 delete $DATA_CACHE->{minimal_info}->{$taxid};
395 0 $DATA_CACHE->{full_info}->{$taxid} = $data;
396 0 push(@results, $self->_make_taxon($data));
397 }
398 }
399  
400 0 ▶ wantarray() ? @results : shift @results;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
401 }
402  
403 2 *get_Taxonomy_Node = \&get_taxon;
404  
405  
406 =head2 get_taxonids
407  
408 Title : get_taxonids
409 Usage : my $taxonid = $db->get_taxonids('Homo sapiens');
410 Function: Searches for a taxonid (typically ncbi_taxon_id) based on a query
411 string. Note that multiple taxonids can match to the same supplied
412 name.
413 Returns : array of integer ids in list context, one of these in scalar context
414 Args : string representing taxon's name
415  
416 =cut
417  
418 0 ▶sub get_taxonids {
Subroutine get_taxonids: not called CC=18 SCAR=58.3
Pod documented
419 0 my ($self,$query) = @_;
420 0 my %p = $self->entrez_params;
421  
422 # queries don't work correctly with special characters, so get rid of them.
423 0 ▶ if ($query =~ /<.+>/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($query =~ /<.+>/) 0 0 0
424 # queries with <something> will fail, so workaround by removing, doing
425 # the query, getting multiple taxonids, then picking the one id that
426 # has a parent node with a scientific_name() or common_names()
427 # case-insensitive matching to the word(s) within <>
428 0 $query =~ s/ <(.+?)>//;
429 0 my $desired_parent_name = lc($1);
430  
431 0 ID: for my $start_id ($self->get_taxonids($query)) {
432 0 ▶ my $node = $self->get_taxon($start_id) || next ID;
Condition0%0/2
$self->get_taxon($start_id) || (next ID)
l!l
00
MC/DC0%0/1
$self->get_taxon($start_id) || (next ID)
$self->get_taxon($start_id)
Truth table0%0/2
A
A= $self->get_taxon($start_id)
Aresult
11
00
433  
434 # walk up the parents until we hit a node with a named rank
435 0 while (1) {
436 0 ▶ my $parent_node = $self->ancestor($node) || next ID;
Condition0%0/2
$self->ancestor($node) || (next ID)
l!l
00
MC/DC0%0/1
$self->ancestor($node) || (next ID)
$self->ancestor($node)
Truth table0%0/2
A
A= $self->ancestor($node)
Aresult
11
00
437 0 ▶ my $parent_sci_name = $parent_node->scientific_name || next ID;
Condition0%0/2
$parent_node->scientific_name || (next ID)
l!l
00
MC/DC0%0/1
$parent_node->scientific_name || (next ID)
$parent_node->scientific_name
Truth table0%0/2
A
A= $parent_node->scientific_name
Aresult
11
00
438 0 my @parent_common_names = $parent_node->common_names;
439 0 ▶ unless (@parent_common_names) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@parent_common_names) 0 0 0
440 # ensure we're not using a minimal-info cached version
441 0 $parent_node = $self->get_taxon(-taxonid => $parent_node->id, -full => 1);
442 0 @parent_common_names = $parent_node->common_names;
443 }
444  
445 0 for my $name ($parent_sci_name, @parent_common_names) {
446 0 ▶ if (lc($name) eq $desired_parent_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $name eq $desired_parent_name) 0 0 0
447 0 ▶ return wantarray() ? ($start_id) : $start_id;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
448 }
449 }
450  
451 0 ▶ my $parent_rank = $parent_node->rank || 'no rank';
Condition0%0/2
$parent_node->rank || 'no rank'
l!l
00
MC/DC0%0/1
$parent_node->rank || 'no rank'
$parent_node->rank
Truth table0%0/2
A
A= $parent_node->rank
Aresult
11
00
452 0 ▶ if ($parent_rank ne 'no rank') {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent_rank ne 'no rank') { } 0 0 0
453 0 last;
454 }
455 else {
456 0 $node = $parent_node;
457 }
458 }
459 }
460 0 return;
461 }
462 0 $query =~ s/[\"\(\)]//g; # not an exhaustive list; these are just the ones we know cause problems
463 0 $query =~ s/\s/+/g;
464  
465 0 my @data;
466 0 ▶ if (defined $DATA_CACHE->{name_to_id}->{$query}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Bio::DB::Taxonomy::entrez::DATA_CACHE->{'name_to_id'}{$query}) { } 0 0 0
467 0 @data = @{$DATA_CACHE->{name_to_id}->{$query}};
468 }
469 else {
470 0 $p{'term'} = $query;
471  
472 0 my $twig = $self->_run_query($self->_build_url($EntrezGet, \%p));
473 0 my $root = $twig->root;
474  
475 # Check that all words in the query are found, because we do not want to
476 # match just 1 word if there are multiple words, e.g. if we query with
477 # "Homo sapiens" both "homo" and "sapiens" must be found
478 0 my $errorlist = $root->first_child('ErrorList');
479 0 ▶ if ( $errorlist ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($errorlist) 0 0 0
480 0 my @notfound = map { $_->text } $errorlist->children('PhraseNotFound');
481 0 ▶ if ( @notfound ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@notfound) 0 0 0
482 0 for my $term ( @notfound ) {
483 0 ▶ return "No hit" if $query =~ /$term/;
Branch: 1 branch
BranchTrueFalseTotal
if $query =~ /$term/ 0 0 0
484 }
485 }
486 }
487  
488 0 my $list = $root->first_child('IdList');
489 0 @data = map { $_->text } $list->children('Id');
490  
491 0 $DATA_CACHE->{name_to_id}->{$query} = [@data];
492 }
493  
494 0 ▶ return wantarray() ? @data : shift @data;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
495 }
496  
497 2 *get_taxonid = \&get_taxonids;
498  
499  
500 =head2 ancestor
501  
502 Title : ancestor
503 Usage : my $ancestor_taxon = $db->ancestor($taxon)
504 Function: Retrieve the ancestor taxon of a supplied Taxon from the database.
505  
506 Note that unless the ancestor has previously been directly
507 requested with get_taxon(), the returned Taxon object will only have
508 a minimal amount of information.
509  
510 Returns : Bio::Taxon
511 Args : Bio::Taxon (that was retrieved from this database)
512  
513 =cut
514  
515 0 ▶sub ancestor {
Subroutine ancestor: not called CC=8 SCAR=42.8
Pod documented
516 0 my ($self, $taxon) = @_;
517 0 ▶ $self->throw("Must supply a Bio::Taxon") unless ref($taxon) && $taxon->isa('Bio::Taxon');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $taxon and $taxon->isa("Bio::Taxon") 0 0 0
Condition0%0/3
ref $taxon and $taxon->isa("Bio::Taxon")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $taxon and $taxon->isa("Bio::Taxon")
ref $taxon $taxon->isa("Bio::Taxon")
Truth table0%0/3
A and B
A= ref $taxonB= $taxon->isa("Bio::Taxon")
ABresult
0X0
100
111
518 0 ▶ $self->throw("The supplied Taxon must belong to this database") unless $taxon->db_handle && $taxon->db_handle eq $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $taxon->db_handle and $taxon->db_handle eq $self 0 0 0
Condition0%0/3
$taxon->db_handle and $taxon->db_handle eq $self
!ll&&!rl&&r
000
MC/DC0%0/2
$taxon->db_handle and $taxon->db_handle eq $self
$taxon->db_handle $taxon->db_handle eq $self
Truth table0%0/3
A and B
A= $taxon->db_handleB= $taxon->db_handle eq $self
ABresult
0X0
100
111
519 0 ▶ my $id = $taxon->id || $self->throw("The supplied Taxon is missing its id!");
Condition0%0/3
$taxon->id || $self->throw('The supplied Taxon is missing its id!')
l!l&&r!l&&!r
000
MC/DC0%0/2
$taxon->id || $self->throw('The supplied Taxon is missing its id!')
$taxon->id $self->throw('The supplied Taxon is missing its id!')
Truth table0%0/3
A || B
A= $taxon->idB= $self->throw('The supplied Taxon is missing its id!')
ABresult
1X1
011
000
520  
521 0 ▶ my $ancestor_id = $RELATIONS->{ancestors}->{$id} || return;
Condition0%0/2
$Bio::DB::Taxonomy::entrez::RELATIONS->{'ancestors'}{$id} || (return)
l!l
00
MC/DC0%0/1
$Bio::DB::Taxonomy::entrez::RELATIONS->{'ancestors'}{$id} || (return)
$Bio::DB::Taxonomy::entrez::RELATIONS->{'ancestors'}{$id}
Truth table0%0/2
A
A= $Bio::DB::Taxonomy::entrez::RELATIONS->{'ancestors'}{$id}
Aresult
11
00
522 0 ▶ return $self->_make_taxon($DATA_CACHE->{full_info}->{$ancestor_id} || $DATA_CACHE->{minimal_info}->{$ancestor_id});
Condition0%0/3
$Bio::DB::Taxonomy::entrez::DATA_CACHE->{'full_info'}{$ancestor_id} || $Bio::DB::Taxonomy::entrez::DATA_CACHE->{'minimal_info'}{$ancestor_id}
l!l&&r!l&&!r
000
MC/DC0%0/2
$Bio::DB::Taxonomy::entrez::DATA_CACHE->{'full_info'}{$ancestor_id} || $Bio::DB::Taxonomy::entrez::DATA_CACHE->{'minimal_info'}{$ancestor_id}
$Bio::DB::Taxonomy::entrez::DATA_CACHE->{'full_info'}{$ancestor_id} $Bio::DB::Taxonomy::entrez::DATA_CACHE->{'minimal_info'}{$ancestor_id}
Truth table0%0/3
A || B
A= $Bio::DB::Taxonomy::entrez::DATA_CACHE->{'full_info'}{$ancestor_id}B= $Bio::DB::Taxonomy::entrez::DATA_CACHE->{'minimal_info'}{$ancestor_id}
ABresult
1X1
011
000
523 }
524  
525  
526 =head2 each_Descendent
527  
528 Title : each_Descendent
529 Usage : my @taxa = $db->each_Descendent($taxon);
530 Function: Get all the descendents of the supplied Taxon (but not their
531 descendents, ie. not a recursive fetchall).
532  
533 Note that this implementation is unable to return a taxon that
534 hasn't previously been directly fetched with get_taxon(), or wasn't
535 an ancestor of such a fetch.
536  
537 Returns : Array of Bio::Taxon objects
538 Args : Bio::Taxon (that was retrieved from this database)
539  
540 =cut
541  
542 0 ▶sub each_Descendent {
Subroutine each_Descendent: not called CC=9 SCAR=45.0
Pod documented
543 0 my ($self, $taxon) = @_;
544 0 ▶ $self->throw("Must supply a Bio::Taxon") unless ref($taxon) && $taxon->isa('Bio::Taxon');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $taxon and $taxon->isa("Bio::Taxon") 0 0 0
Condition0%0/3
ref $taxon and $taxon->isa("Bio::Taxon")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $taxon and $taxon->isa("Bio::Taxon")
ref $taxon $taxon->isa("Bio::Taxon")
Truth table0%0/3
A and B
A= ref $taxonB= $taxon->isa("Bio::Taxon")
ABresult
0X0
100
111
545 0 ▶ $self->throw("The supplied Taxon must belong to this database") unless $taxon->db_handle && $taxon->db_handle eq $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $taxon->db_handle and $taxon->db_handle eq $self 0 0 0
Condition0%0/3
$taxon->db_handle and $taxon->db_handle eq $self
!ll&&!rl&&r
000
MC/DC0%0/2
$taxon->db_handle and $taxon->db_handle eq $self
$taxon->db_handle $taxon->db_handle eq $self
Truth table0%0/3
A and B
A= $taxon->db_handleB= $taxon->db_handle eq $self
ABresult
0X0
100
111
546 0 ▶ my $id = $taxon->id || $self->throw("The supplied Taxon is missing its id!");
Condition0%0/3
$taxon->id || $self->throw('The supplied Taxon is missing its id!')
l!l&&r!l&&!r
000
MC/DC0%0/2
$taxon->id || $self->throw('The supplied Taxon is missing its id!')
$taxon->id $self->throw('The supplied Taxon is missing its id!')
Truth table0%0/3
A || B
A= $taxon->idB= $self->throw('The supplied Taxon is missing its id!')
ABresult
1X1
011
000
547  
548 0 ▶ my @children_ids = keys %{$RELATIONS->{children}->{$id} || {}};
Condition0%0/2
$Bio::DB::Taxonomy::entrez::RELATIONS->{'children'}{$id} || {}
l!l
00
MC/DC0%0/1
$Bio::DB::Taxonomy::entrez::RELATIONS->{'children'}{$id} || {}
$Bio::DB::Taxonomy::entrez::RELATIONS->{'children'}{$id}
Truth table0%0/2
A
A= $Bio::DB::Taxonomy::entrez::RELATIONS->{'children'}{$id}
Aresult
11
00
549 0 my @children;
550 0 foreach my $child_id (@children_ids) {
551 0 ▶ push(@children, $self->_make_taxon($DATA_CACHE->{full_info}->{$child_id} || $DATA_CACHE->{minimal_info}->{$child_id}));
Condition0%0/3
$Bio::DB::Taxonomy::entrez::DATA_CACHE->{'full_info'}{$child_id} || $Bio::DB::Taxonomy::entrez::DATA_CACHE->{'minimal_info'}{$child_id}
l!l&&r!l&&!r
000
MC/DC0%0/2
$Bio::DB::Taxonomy::entrez::DATA_CACHE->{'full_info'}{$child_id} || $Bio::DB::Taxonomy::entrez::DATA_CACHE->{'minimal_info'}{$child_id}
$Bio::DB::Taxonomy::entrez::DATA_CACHE->{'full_info'}{$child_id} $Bio::DB::Taxonomy::entrez::DATA_CACHE->{'minimal_info'}{$child_id}
Truth table0%0/3
A || B
A= $Bio::DB::Taxonomy::entrez::DATA_CACHE->{'full_info'}{$child_id}B= $Bio::DB::Taxonomy::entrez::DATA_CACHE->{'minimal_info'}{$child_id}
ABresult
1X1
011
000
552 }
553  
554 0 return @children;
555 }
556  
557  
558 =head2 Some Get/Setter methods
559  
560 =head2 entrez_url
561  
562 Title : entrez_url
563 Usage : $obj->entrez_url($newval)
564 Function: Get/set entrez URL
565 Returns : value of entrez url (a scalar)
566 Args : on set, new value (a scalar or undef, optional)
567  
568 =cut
569  
570 3 ▶sub entrez_url{
Subroutine entrez_url: called CC=2 SCAR=6.9
Pod documented
571 3 my $self = shift;
572  
573 3 ▶ return $self->{'_entrez_url'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 2 1 3
574 1 return $self->{'_entrez_url'};
575 }
576  
577  
578 =head2 entrez_params
579  
580 Title : entrez_params
581 Usage : $obj->entrez_params($newval)
582 Function: Get/set entrez params
583 Returns : value of entrez_params (a hashref)
584 Args : on set, new value Hashref
585  
586 =cut
587  
588 3 ▶sub entrez_params{
Subroutine entrez_params: called CC=2 SCAR=6.9
Pod documented
589 3 my $self = shift;
590 3 my $f;
591 3 ▶ if( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 2 1 3
592 2 $f = $self->{'_entrez_params'} = shift;
593 } else {
594 1 $f = $self->{'_entrez_params'};
595 }
596 3 return %$f;
597 }
598  
599 =head2 Bio::DB::WebBase methods
600  
601 =head2 proxy_string
602  
603 Title : proxy_string
604 Usage : my $proxy_string = $self->proxy_string($protocol)
605 Function: Get the proxy string (plus user/pass )
606 Returns : string
607 Args : protocol ('http' or 'ftp'), default 'http'
608  
609 =head2 proxy
610  
611 Title : proxy
612 Usage : $httpproxy = $db->proxy('http') or
613 $db->proxy(['http','ftp'], 'http://myproxy' )
614 Function: Get/Set a proxy for use of proxy
615 Returns : a string indicating the proxy
616 Args : $protocol : an array ref of the protocol(s) to set/get
617 $proxyurl : url of the proxy to use for the specified protocol
618 $username : username (if proxy requires authentication)
619 $password : password (if proxy requires authentication)
620  
621 =head2 authentication
622  
623 Title : authentication
624 Usage : $db->authentication($user,$pass)
625 Function: Get/Set authentication credentials
626 Returns : Array of user/pass
627 Args : Array or user/pass
628  
629 =cut
630  
631  
632 # make a Taxon object from data hash ref
633 0 ▶sub _make_taxon {
Subroutine _make_taxon: not called CC=4 SCAR=30.0
634 0 my ($self, $data) = @_;
635  
636 0 my $taxon = Bio::Taxon->new();
637  
638 0 my $taxid;
639 0 ▶ while (my ($method, $value) = each %{$data}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$data;}) 0 0 0
640 0 ▶ if ($method eq 'id') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "id") 0 0 0
641 0 $method = 'ncbi_taxid'; # since this is a real ncbi taxid, explicitly set it as one
642 0 $taxid = $value;
643 }
644 0 ▶ $taxon->$method(ref($value) eq 'ARRAY' ? @{$value} : $value);
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 0 0 0
645 }
646  
647 # we can't use -dbh or the db_handle() method ourselves or we'll go
648 # infinite on the merge attempt
649 0 $taxon->{'db_handle'} = $self;
650  
651 0 $self->_handle_internal_id($taxon);
652  
653 0 return $taxon;
654 }
655  
656  
657 1 ▶sub _build_url {
Subroutine _build_url: called CC=1 SCAR=0.0
658 # Given a eutility (esearch.fcgi, efetch.fcgi or esummary.fcgi) and a
659 # hashref or parameters, build a url suitable for eutil query
660 1 my ($self, $eutility, $p) = @_;
661 1 my $params = join($UrlParamSeparatorValue, map { $_.'='.$p->{$_} } keys %$p);
662 1 my $url = $self->entrez_url.$eutility.'?'.$params;
663 1 $self->debug("url is $url\n");
664 1 return $url;
665 }
666  
667  
668 1 ▶sub _run_query {
Subroutine _run_query: called CC=2 SCAR=7.6
669 # Given an eutil url, run the eutil query and parse the response into an
670 # XML Twig object
671 1 my ($self, $url) = @_;
672 1 $self->sleep();
673 1 my $response = $self->get($url);
674 1 ▶ if ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 1 0 1
675 1 $response = $response->content;
676 }else {
677 0 $self->throw("Can't query website: ".$response->status_line);
678 }
679 1 $self->debug("response is $response\n");
680 1 my $twig = XML::Twig->new;
681 0 $twig->parse($response);
682 0 return $twig;
683 }
684  
685  
686 2 1;
↑ Summary NCBIHelper.pm »