blib/lib/Bio/LITE/Taxonomy/NCBI.pm

filter: Statementstmt 73.2% 63 / 86 Branchbran 50.0% 18 / 36 Conditioncond 27.2% 3 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 70.5% 12 / 17 Podpod 100% 4 / 4 totaltotal 62.1% 100 / 161 CC 23 SCAR 17.6
CC
23
Cov
63%
get_term_at_level_from_gi
12.0
_build_taxonomy
8.6
_name_nodes
7.3
CRAP 5.8
↑ Summary
1 package Bio::LITE::Taxonomy::NCBI;
2  
3 =head1 NAME
4  
5 Bio::LITE::Taxonomy::NCBI - Lightweight and efficient NCBI taxonomic manager
6  
7 =head1 SYNOPSIS
8  
9 use Bio::LITE::Taxonomy::NCBI;
10  
11 my $taxDB = Bio::LITE::Taxonomy::NCBI->new (
12 db=>"NCBI",
13 names=> "/path/to/names.dmp",
14 nodes=>"/path/to/nodes.dmp"
15 );
16  
17 my $tax = $taxDB->get_taxonomy(1442); # 1442 is a Taxid
18 my $taxid = $taxDB->get_taxid_from_name("Bacteroidetes");
19 my $term = $taxDB->get_term_at_level(1442,"family");
20  
21 my $taxDB2 = Bio::LITE::Taxonomy::NCBI-> new (
22 db=>"NCBI",
23 names=> "/path/to/names.dmp",
24 nodes=>"/path/to/nodes.dmp",
25 dict=>"/path/to/dictionary/file",
26 );
27 my $tax2 = $taxDB2->get_taxonomy_from_gi(12553);
28  
29 # Methods from Bio::LITE::Taxonomy::NCBI::Gi2taxid
30 # can also be called directly:
31  
32 my $taxid2 = $taxDB2->get_taxid(12553);
33  
34  
35 =head1 DESCRIPTION
36  
37 This module provides easy and efficient access to the NCBI taxonomy with minimal dependencies and without intermediary databases.
38  
39 This module is not part of the Bioperl bundle. For bioperl alternatives see the L</"SEE ALSO"> section of this document.
40  
41 =head1 CONSTRUCTOR
42  
43 =head2 C<new (%ARGS)>
44  
45 Creates a Bio::LITE::Taxonomy::NCBI object.
46  
47 The following parameters are needed
48  
49 =over 4
50  
51 =item names
52  
53 The location of the I<names.dmp> file. Filehandles are also allowed. I<Mandatory>.
54  
55 =item nodes
56  
57 The location of the I<nodes.dmp> file. Filehandles are also allowed. I<Mandatory>.
58  
59 =item dict
60  
61 You can query the tree using C<GI>s directly instead of C<Taxid>s. For doing this, you should provide the NCBIs GI to Taxid mapper in binary format as explained in L<Bio::LITE::Taxonomy::NCBI::Gi2taxid>. I<Optional>
62  
63 =item save_mem
64  
65 Use this option to avoid to load the binary dictionary (GI to Taxid) into memory. This will save almost 1GB of system memory but looking up for Taxids will be ~20% slower. This parameter is optional, only makes sense if you are using the C<GI> to C<Taxid> dictionary and is I<off> by default.
66  
67 =back
68  
69 =head1 METHODS
70  
71 This module inherits from L<Bio::LITE::Taxonomy> so all the methods explained there are accessible.
72 These methods are also available:
73  
74 =over 4
75  
76 =item get_taxonomy_from_gi
77  
78 Accepts a C<GI> as input and returns an array with its ascendants ordered from top to bottom.
79  
80 my @tax = $tax->get_taxonomy_from_gi($gi);
81 print "$_\n" for (@tax);
82  
83 If called in scalar context, returns an array reference instead of the array.
84 See L<Bio::LITE::Taxonomy>::get_taxonomy
85  
86 =item get_taxonomy_with_levels_from_gi
87  
88 The same as get_taxonomy_from_gi but instead of getting the ascendants returns an array of array references. Each array reference has the ascendant and its taxonomic level (at positions 0 and 1 respectively). This is simpler than it sounds. Check L<Bio::LITE::Taxonomy>::get_taxonomy_with_levels for more information.
89  
90 If called in scalar context, returns an array reference instead of the array.
91  
92 =item get_term_at_level_from_gi
93  
94 Given a gi and a taxonomic level as input, returns the taxon. For example,
95  
96 my $taxon = $tax->get_term_at_level_from_gi($gi,"family");
97  
98 See L<Bio::LITE::Taxonomy>::get_term_at_level.
99  
100 =back
101  
102 =head1 SEE ALSO
103  
104 L<Bio::Gi2taxid> - Module to convert NCBIs GIs to Taxids
105  
106 L<Bio::LITE::Taxonomy>
107  
108 L<Bio::Taxonomy::RDP>
109  
110 L<Bio::DB::Taxonomy::*> - Bioperl alternative for NCBI taxonomies.
111  
112  
113 =head1 AUTHOR
114  
115 Miguel Pignatelli
116  
117 Any comments or suggestions should be addressed to emepyc@gmail.com
118  
119 =head1 LICENSE
120  
121 Copyright 2009 Miguel Pignatelli, all rights reserved.
122  
123 This library is free software; you may redistribute it and/or modify it under the same terms as Perl itself.
124  
125 =cut
126  
127 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
128 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
129 3 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
130  
131 #use Bio::LITE::Taxonomy;
132 #if (do {(sprintf "%vd",$^V) =~ /5\.(\d\d)/; $1} >= 10}) {
133 # import base qw(Taxonomy);
134 #} else {
135 # import parent qw(Taxonomy);
136 #}
137 3 ▶use base qw(Bio::LITE::Taxonomy);
Subroutine BEGIN: called CC=1 SCAR=0.0
138  
139 3 our $VERSION = 0.09;
140  
141 3 ▶use constant FS => '\t\|\t';
Subroutine BEGIN: called CC=1 SCAR=0.0
142 3 ▶use constant RS => '\t\|\n';
Subroutine BEGIN: called CC=1 SCAR=0.0
143  
144 sub new {
145 1 ▶ my ($class,%args) = @_;
Subroutine new: called CC=5 SCAR=18.3
Pod documented
146 1 my %opts;
147  
148 1 ▶ defined $args{'nodes'} or croak "Need the file nodes.dmp";
Branch: 1 branch
BranchTrueFalseTotal
defined $args{"nodes"} or croak("Need the file nodes.dmp") 0 1 1
149 1 ▶ defined $args{'names'} or croak "Need the file names.dmp";
Branch: 1 branch
BranchTrueFalseTotal
defined $args{"names"} or croak("Need the file names.dmp") 0 1 1
150  
151 1 @opts{qw/nodesFile namesFile/} = @args{qw/nodes names/};
152  
153 1 my $self = bless \%opts, $class;
154 1 $self->_build_taxonomy();
155 1 ▶ if (defined $args{dict}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"dict"}) 0 1 1
156 0 require Bio::LITE::Taxonomy::NCBI::Gi2taxid;
157 0 ▶ $self->{dict} = Bio::LITE::Taxonomy::NCBI::Gi2taxid->new(dict=>$args{dict},save_mem=>$args{save_mem} || 0);
Condition0%0/2
$args{'save_mem'} || 0
l!l
00
MC/DC0%0/1
$args{'save_mem'} || 0
$args{'save_mem'}
Truth table0%0/2
A
A= $args{'save_mem'}
Aresult
11
00
158 }
159 1 return $self;
160 }
161  
162 sub _build_taxonomy {
163 1 ▶ my ($self) = @_;
Subroutine _build_taxonomy: called CC=7 SCAR=21.5
164 1 my $nodesFile = $self->{nodesFile};
165 1 my $tax;
166 1 ▶ if ((UNIVERSAL::isa($nodesFile, 'GLOB')) or (ref \$nodesFile eq 'GLOB')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($nodesFile, 'GLOB') or ref \$nodesFile eq 'GLOB') { } 0 1 1
Condition33%1/3
UNIVERSAL::isa($nodesFile, "GLOB") or ref \$nodesFile eq "GLOB"
l!l&&r!l&&!r
001
MC/DC0%0/2
UNIVERSAL::isa($nodesFile, "GLOB") or ref \$nodesFile eq "GLOB"
UNIVERSAL::isa($nodesFile, "GLOB") ref \$nodesFile eq "GLOB"
Truth table33%1/3
A or B
A= UNIVERSAL::isa($nodesFile, "GLOB")B= ref \$nodesFile eq "GLOB"
ABresult
1X1
011
000
167 0 $tax = $nodesFile;
168 } else {
169 1 ▶ open $tax, "<", $nodesFile or croak "$!";
Branch: 1 branch
BranchTrueFalseTotal
open $tax, "<", $nodesFile or croak("$!") 0 1 1
170 }
171 1 ▶ while (<$tax>){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $tax)) 560790 1 560791
172 560790 chomp;
173 560790 $self -> _create_node(_parse_tax_rec($_));
174 }
175 1 $self -> _name_nodes();
176 1 ▶ close $tax unless ((UNIVERSAL::isa($nodesFile, 'GLOB')) or (ref \$nodesFile eq 'GLOB'));
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($nodesFile, "GLOB") or ref \$nodesFile eq "GLOB" 1 0 1
Condition33%1/3
UNIVERSAL::isa($nodesFile, "GLOB") or ref \$nodesFile eq "GLOB"
l!l&&r!l&&!r
001
MC/DC0%0/2
UNIVERSAL::isa($nodesFile, "GLOB") or ref \$nodesFile eq "GLOB"
UNIVERSAL::isa($nodesFile, "GLOB") ref \$nodesFile eq "GLOB"
Truth table33%1/3
A or B
A= UNIVERSAL::isa($nodesFile, "GLOB")B= ref \$nodesFile eq "GLOB"
ABresult
1X1
011
000
177 }
178  
179 sub _create_node {
180 560790 ▶ my ($self,$node,$parent,$level) = @_;
Subroutine _create_node: called CC=2 SCAR=6.9
181 560790 ▶ $self->{allowed_levels}{$level} = 1 if (! defined $self->{allowed_levels}{$level});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"allowed_levels"}{$level} 29 560761 560790
182 560790 @{$self->{nodes}->{$node}}{qw/parent level/} = ($parent,$level);
183 }
184  
185 sub _name_nodes {
186 1 ▶ my ($self) = @_;
Subroutine _name_nodes: called CC=7 SCAR=19.8
187 1 my $namesFile = $self->{namesFile};
188 1 my $nodesNames;
189 1 ▶ if ((UNIVERSAL::isa($namesFile, 'GLOB')) or (ref \$namesFile eq 'GLOB')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($namesFile, 'GLOB') or ref \$namesFile eq 'GLOB') { } 0 1 1
Condition33%1/3
UNIVERSAL::isa($namesFile, "GLOB") or ref \$namesFile eq "GLOB"
l!l&&r!l&&!r
001
MC/DC0%0/2
UNIVERSAL::isa($namesFile, "GLOB") or ref \$namesFile eq "GLOB"
UNIVERSAL::isa($namesFile, "GLOB") ref \$namesFile eq "GLOB"
Truth table33%1/3
A or B
A= UNIVERSAL::isa($namesFile, "GLOB")B= ref \$namesFile eq "GLOB"
ABresult
1X1
011
000
190 0 $nodesNames = $namesFile;
191 } else {
192 1 ▶ open $nodesNames, "<", $namesFile or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
open $nodesNames, "<", $namesFile or croak($!) 0 1 1
193 }
194 1 ▶ while (<$nodesNames>){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $nodesNames)) 806059 1 806060
195 806059 chomp;
196 806059 my ($taxId,$taxName,$comment) = _process_tax_name ($_);
197 806059 ▶ if ($comment eq "scientific name"){
Branch: 2 branches
BranchTrueFalseTotal
if ($comment eq 'scientific name') { } 560790 245269 806059
elsif ($comment eq 'synonym') { } 138436 106833 245269
198 560790 ${$self->{nodes}->{$taxId}}{name} = $taxName;
199 560790 $self->{names}->{$taxName} = $taxId;
200 } elsif ($comment eq "synonym") {
201 138436 $self->{names}->{$taxName} = $taxId;
202 }
203 }
204 1 close $nodesNames;
205 }
206  
207 sub _parse_tax_rec {
208 560790 ▶ my $line = shift @_;
Subroutine _parse_tax_rec: called CC=1 SCAR=0.0
209 560790 return (split FS,$line)[0,1,2];
210 }
211  
212  
213 sub _process_tax_name {
214 806059 ▶ my $line = shift @_;
Subroutine _process_tax_name: called CC=1 SCAR=0.0
215 806059 my @fields = split FS, $line;
216 806059 $fields[3] =~ s/\t\|$//;
217 806059 return ($fields[0],$fields[1],$fields[3]);
218 }
219  
220 sub get_taxonomy_from_gi {
221 0 ▶ my ($self,$gi) = @_;
Subroutine get_taxonomy_from_gi: not called CC=2 SCAR=17.9
Pod documented
222 0 ▶ croak "Undefined GI\n" unless (defined $gi);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $gi 0 0 0
223 0 my $taxid = $self->{dict}->get_taxid($gi);
224 0 return $self->get_taxonomy($taxid);
225 }
226  
227 sub get_taxonomy_with_levels_from_gi {
228 0 ▶ my ($self,$gi) = @_;
Subroutine get_taxonomy_with_levels_from_gi: not called CC=2 SCAR=17.9
Pod documented
229 0 ▶ croak "Undefined GI\n" unless (defined $gi);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $gi 0 0 0
230 0 my $taxid = $self->{dict}->get_taxid($gi);
231 0 return $self->get_taxonomy_with_levels($taxid);
232 }
233  
234 sub get_term_at_level_from_gi {
235 0 ▶ my ($self,$gi,$level) = @_;
Subroutine get_term_at_level_from_gi: not called CC=3 SCAR=24.8
Pod documented
236 0 ▶ croak "Undefined GI\n" unless (defined $gi);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $gi 0 0 0
237 0 ▶ croak "Undefined Level\n" unless (defined $level);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $level 0 0 0
238 0 my $taxid = $self->{dict}->get_taxid($gi);
239 0 return $self->get_term_at_level($taxid,$level);
240 }
241  
242 # Note: Use methods in Gi2taxid as if they were from here
243 sub AUTOLOAD {
244 0 ▶ my ($self,$args) = @_;
Subroutine AUTOLOAD: not called CC=2 SCAR=17.9
245 0 our $AUTOLOAD;
246 0 my $method = $AUTOLOAD;
247 0 $method =~ s/.*:://;
248 0 ▶ $self->{dict}->can($method) or croak "$method not defined in package __PACKAGE__\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"dict"}->can($method) or croak("$method not defined in package __PACKAGE__\n") 0 0 0
249 0 return $self->{dict}->$method($args);
250 }
251  
252 # Needed to dont call AUTLOAD on object destruction
253 ▶sub DESTROY { }
Subroutine DESTROY: not called CC=1 SCAR=0.0
254  
255 3 1;
↑ Summary