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

filter: Statementstmt 91.1% 82 / 90 Branchbran 63.1% 24 / 38 Conditioncond 50.0% 7 / 14 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 100% 12 / 12 Podpod 100% 3 / 3 totaltotal 78.3% 130 / 166 SCAR 34.1
CC
25
Cov
80%
new_dict
15.8
new
9.2
_direct_lookup
3.4
CRAP 30.3
↑ Summary
1 package Bio::LITE::Taxonomy::NCBI::Gi2taxid;
2  
3 =head1 NAME
4  
5 Bio::LITE::Taxonomy::NCBI::Gi2taxid - Mappings of NCBI GI's to Taxids fast and with very low memory footprint.
6  
7 =head1 SYNOPSIS
8  
9 Creation of a new Taxid to GI dictionary (binary mapping file):
10  
11 use Bio::LITE::Taxonomy::NCBI::Gi2taxid qw/new_dict/;
12  
13 new_dict (in => "gi_taxid_prot.dmp",
14 out => "gi_taxid_prot.bin");
15  
16 Usage of the dictionary:
17  
18 use Bio::LITE::Taxonomy::NCBI::Gi2taxid;
19  
20 my $dict = Bio::LITE::Taxonomy::NCBI::Gi2taxid->new(dict=>"gi_taxid_prot.bin");
21 my $taxid = $dict->get_taxid(12553);
22  
23 =head1 DESCRIPTION
24  
25 The NCBI site offers a file to map gene and protein sequences (GIs) with their corresponding taxon of origin (Taxids). If you want to use this information inside a Perl script you will find that (given the high amount of sequences available) it is fairly inefficient to store this information in, for example, a regular hash. Only for creating such a hash you will need more than 10 GBs of system memory.
26  
27 This is a very simple module that has been designed to efficiently map NCBI GIs to Taxids with speed as the primary goal. It is designed to retrieve taxids from GIs very fast and with low memory usage. It is even faster than using a SQL database to retrieve the mappings or using a local DBHash.
28  
29 To achieve this, it uses a binary index that can be created with the function C<new_dict>. This index has to be created one time for each mapping file.
30  
31 The original mapping files can be downloaded from the NCBI site at the following address: L<ftp://ftp.ncbi.nih.gov/pub/taxonomy/>.
32  
33 =head1 FUNCTIONS
34  
35 =head2 C<new_dict>
36  
37 This function creates a new binary dictionary from the NCBI mapping file. The file should be uncompressed before being passed to the script. The function accepts the following parameters:
38  
39 *WARNING* version 0.05 uses a more compacted memory file. This means that binary files created with earlier versions will not work with this one and vice-versa. You need to create the new binary db with this version.
40  
41 =over 4
42  
43 =item in
44  
45 This is the uncompressed mapping file from the NCBI. The function accepts a filename or a filehandle
46  
47 =item out
48  
49 Optional. Where the binary dictionary is going to be printed. The function accepts a filename or a filehandle (that should be opened with writing permissions). If absent STDOUT will be assumed.
50  
51 =back
52  
53 =head1 CONSTRUCTOR
54  
55 =head2 C<new>
56  
57 Once the binary dictionary is created it can be used as an object using this constructor. It accepts the following parameters
58  
59 =over 4
60  
61 =item dict
62  
63 This is the binary dictionary obtained with the C<new_dict> function. The name of the file or a filehandle is accepted.
64  
65 =item save_mem
66  
67 Optional. Use this option to avoid to load the binary dictionary into memory. This will save almost 1GB of system memory but looking up for Taxids will be ~20% slower. This option of I<off> by default.
68  
69 =back
70  
71 =head1 METHODS
72  
73 =head2 C<get_taxid>
74  
75 This method receives a GI and returns the corresponding Taxid.
76  
77 =head1 SEE ALSO
78  
79 L<DBHash>
80 L<Bio::DB::Taxonomy>
81  
82 =head1 AUTHOR
83  
84 Miguel Pignatelli
85  
86 Any comments should be addressed to emepyc@gmail.com
87  
88 =head1 LICENSE
89  
90 Copyright 2013 Miguel Pignatelli, all rights reserved.
91  
92 This library is free software; you may redistribute it and/or modify it under the same terms as Perl itself.
93  
94  
95 =cut
96  
97 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
98 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
99 4 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
100 4 ▶use File::Tail;
Subroutine BEGIN: called CC=1 SCAR=0.0
101  
102 4 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
103 4 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
104 4 @ISA = qw(Exporter);
105  
106 4 @EXPORT = (); # Only qualified exports are allowed
107 4 @EXPORT_OK = qw(new_dict);
108 4 $VERSION = '0.11';
109  
110 sub new
111 {
112 3 ▶ my ($class, %args) = @_;
Subroutine new: called CC=8 SCAR=22.2
Pod documented
113 3 my %opts;
114  
115 3 ▶ $args{'dict'} or croak "Need the dictionary file";
Branch: 1 branch
BranchTrueFalseTotal
$args{"dict"} or croak("Need the dictionary file") 0 3 3
116 3 ▶ croak "\nERROR\n$args{dict}: File not found\n" unless -e $args{dict};
Branch: 1 branch
BranchTrueFalseTotal
unless -e $args{"dict"} 0 3 3
117  
118 3 ▶ my $save_mem = $args{'save_mem'} || 0;
Condition50%1/2
$args{'save_mem'} || 0
l!l
03
MC/DC0%0/1
$args{'save_mem'} || 0
$args{'save_mem'}
Truth table50%1/2
A
A= $args{'save_mem'}
Aresult
11
00
119 3 my $dictfh;
120 3 ▶ if ((UNIVERSAL::isa($args{dict}, 'GLOB')) or (ref \$args{dict} eq 'GLOB')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($args{'dict'}, 'GLOB') or ref \$args{'dict'} eq 'GLOB') { } 0 3 3
Condition33%1/3
UNIVERSAL::isa($args{"dict"}, "GLOB") or ref \$args{"dict"} eq "GLOB"
l!l&&r!l&&!r
003
MC/DC0%0/2
UNIVERSAL::isa($args{"dict"}, "GLOB") or ref \$args{"dict"} eq "GLOB"
UNIVERSAL::isa($args{"dict"}, "GLOB") ref \$args{"dict"} eq "GLOB"
Truth table33%1/3
A or B
A= UNIVERSAL::isa($args{"dict"}, "GLOB")B= ref \$args{"dict"} eq "GLOB"
ABresult
1X1
011
000
121 0 $dictfh = $args{dict}; # TO DO: Test flags, the filehandle must be readable
122 } else {
123 3 ▶ croak "\nERROR\nThe file containing the gi <-> taxid correspondences must be converted to binary format. See the documentation of this module for more details." unless (-B $args{dict});
Branch: 1 branch
BranchTrueFalseTotal
unless -B $args{"dict"} 1 2 3
124 2 ▶ open $dictfh, "<:raw", $args{dict} or croak "$!: $args{dict}";
Branch: 1 branch
BranchTrueFalseTotal
open $dictfh, "<:raw", $args{"dict"} or croak(qq[$!: $args{"dict"}]) 0 2 2
125 }
126  
127 2 $opts{fh} = $dictfh;
128 2 $opts{save_mem} = $save_mem;
129 2 my $self = bless \%opts;
130  
131 2 $self -> _build_dict();
132 2 return $self;
133 }
134  
135 sub _build_dict
136 {
137 2 ▶ my ($self) = @_;
Subroutine _build_dict: called CC=3 SCAR=11.2
138 2 my $data;
139 2 ▶ $self->{save_mem} && return;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"save_mem"} and return 0 2 2
140 2 my $n = sysread( $self->{fh}, $data, -s $self->{fh} );
141 2 ▶ croak "Can't read input file" unless ($n);
Branch: 1 branch
BranchTrueFalseTotal
unless $n 0 2 2
142 2 $self->{dict} = \$data;
143 }
144  
145 sub get_taxid
146 {
147 4 ▶ my ($self, $gi) = @_;
Subroutine get_taxid: called CC=1 SCAR=0.0
Pod documented
148 4 return $self->_direct_lookup ($gi);
149 }
150  
151 sub _direct_lookup {
152 4 ▶ my ($self,$gi) = @_;
Subroutine _direct_lookup: called CC=3 SCAR=12.2
153 4 ▶ if ($self->{save_mem}){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'save_mem'}) { } 0 4 4
154 0 my $taxidBytes;
155 0 sysseek ($self->{fh},$gi*3,0);
156 0 sysread($self->{fh},$taxidBytes,3);
157 0 my ($taxid1, $taxid2, $taxid3) = unpack "CCC", $taxidBytes;
158 0 return $taxid3 | $taxid2 << 8 | $taxid1 << 16 | 0 << 24 ;
159 } else {
160 4 ▶ no warnings qw/substr/;
Subroutine BEGIN: called CC=1 SCAR=0.0
161 4 my $v = substr(${$self->{dict}}, $gi*3, 3);
162 4 ▶ return 0 unless ($v);
Branch: 1 branch
BranchTrueFalseTotal
unless $v 0 4 4
163 4 my ($taxid1, $taxid2, $taxid3) = unpack "CCC", $v;
164 4 my $taxid = $taxid3 | $taxid2 << 8 | $taxid1 << 16 | 0 << 24 ;
165 4 return $taxid;
166 }
167 }
168  
169 sub new_dict {
170 3 ▶ my (%args) = @_;
Subroutine new_dict: called CC=14 SCAR=27.6
Pod documented
171  
172 # TO DO -> Multiple inputs should be allowed
173 3 ▶ defined $args{in} or croak "No input dictionary file provided";
Branch: 1 branch
BranchTrueFalseTotal
defined $args{"in"} or croak("No input dictionary file provided") 1 2 3
174  
175 2 my $outfh;
176 2 ▶ if (! defined $args{out}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $args{'out'}) { } 0 2 2
elsif (UNIVERSAL::isa($args{'out'}, 'GLOB') or ref \$args{'out'} eq 'GLOB') { } 1 1 2
177 0 $outfh = \*STDOUT
178 ▶ } elsif ((UNIVERSAL::isa($args{out}, 'GLOB')) or (ref \$args{out} eq 'GLOB')) {
Condition66%2/3
UNIVERSAL::isa($args{"out"}, "GLOB") or ref \$args{"out"} eq "GLOB"
l!l&&r!l&&!r
101
MC/DC50%1/2
UNIVERSAL::isa($args{"out"}, "GLOB") or ref \$args{"out"} eq "GLOB"
UNIVERSAL::isa($args{"out"}, "GLOB") ref \$args{"out"} eq "GLOB"
Truth table66%2/3
A or B
A= UNIVERSAL::isa($args{"out"}, "GLOB")B= ref \$args{"out"} eq "GLOB"
ABresult
1X1
011
000
179 1 $outfh = $args{out}; ## TO DO: Test flags, the filehandle must be writeable
180 } else {
181 1 ▶ open $outfh, ">", $args{out} or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
open $outfh, ">", $args{"out"} or croak($!) 0 1 1
182 }
183  
184 2 my $infh;
185 2 ▶ if ((UNIVERSAL::isa($args{in}, 'GLOB')) or (ref \$args{in} eq 'GLOB')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($args{'in'}, 'GLOB') or ref \$args{'in'} eq 'GLOB') { } 0 2 2
Condition33%1/3
UNIVERSAL::isa($args{"in"}, "GLOB") or ref \$args{"in"} eq "GLOB"
l!l&&r!l&&!r
002
MC/DC0%0/2
UNIVERSAL::isa($args{"in"}, "GLOB") or ref \$args{"in"} eq "GLOB"
UNIVERSAL::isa($args{"in"}, "GLOB") ref \$args{"in"} eq "GLOB"
Truth table33%1/3
A or B
A= UNIVERSAL::isa($args{"in"}, "GLOB")B= ref \$args{"in"} eq "GLOB"
ABresult
1X1
011
000
186 0 $infh = $args{in}; ## TO DO: Test flags, the filehandle must be readable
187 } else {
188 2 ▶ open $infh, "<", $args{in} or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
open $infh, "<", $args{"in"} or croak($!) 0 2 2
189 }
190  
191 2 ▶ my $ftail = File::Tail->new(name=>$args{in},tail=>1) or croak "$!: $args{in}";
Branch: 1 branch
BranchTrueFalseTotal
my $ftail = "File::Tail"->new("name", $args{"in"}, "tail", 1) or croak(qq[$!: $args{"in"}]) 0 2 2
192 2 my $last_line = $ftail->read();
193 2 ▶ croak "$args{in} is empty" unless (defined $last_line);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $last_line 0 2 2
194 2 my ($last_val) = split /\t/, $last_line;
195  
196 2 ▶ while (<$infh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $infh)) 10 2 12
197 10 chomp;
198 10 my ($key,$val) = split /\t/;
199 10 my ($taxid1, $taxid2, $taxid3, $taxid4) = unpack("CCCC", pack("N", $val));
200 10 sysseek($outfh, $key*3, 0);
201 10 syswrite($outfh, pack("C", $taxid2), 1);
202 10 sysseek($outfh, $key*3 + 1, 0);
203 10 syswrite($outfh, pack("C", $taxid3), 1);
204 10 sysseek($outfh, $key*3 + 2, 0);
205 10 syswrite($outfh, pack("C", $taxid4), 1);
206 }
207 2 close ($infh);
208 2 ▶ if (defined $args{out} && ref \$args{out} eq "SCALAR") {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"out"} and ref \$args{"out"} eq "SCALAR") 1 1 2
Condition66%2/3
defined $args{"out"} and ref \$args{"out"} eq "SCALAR"
!ll&&!rl&&r
011
MC/DC50%1/2
defined $args{"out"} and ref \$args{"out"} eq "SCALAR"
defined $args{"out"} ref \$args{"out"} eq "SCALAR"
Truth table66%2/3
A and B
A= defined $args{"out"}B= ref \$args{"out"} eq "SCALAR"
ABresult
0X0
100
111
209 1 close($outfh)}
210 }
211  
212 4 1;
↑ Summary