Taxonomy/entrez.pm

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