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

filter: Statementstmt 92.3% 97 / 105 Branchbran 65.0% 26 / 40 Conditioncond 50.0% 8 / 16 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 100% 13 / 13 Podpod 100% 3 / 3 totaltotal 79.6% 149 / 187 CC 27 SCAR 22.8
CC
27
Cov
81%
new_dict
17.4
new
9.2
_direct_lookup
3.4
CRAP 9.8
↑ 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 *NOTE* From version 0.05, the lib 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 =item chunk_size
52  
53 Optional. While bin conversion, the lib stores chunks of data in memory to speed up the conversion. This number specifies the size of the chunks. By default 30Mb is used. The whole chunk is stored in a Perl scalar so be careful not to overflow the scalar capacity.
54  
55 =back
56  
57 =head1 CONSTRUCTOR
58  
59 =head2 C<new>
60  
61 Once the binary dictionary is created it can be used as an object using this constructor. It accepts the following parameters
62  
63 =over 4
64  
65 =item dict
66  
67 This is the binary dictionary obtained with the C<new_dict> function. The name of the file or a filehandle is accepted.
68  
69 =item save_mem
70  
71 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 is I<off> by default.
72  
73 =back
74  
75 =head1 METHODS
76  
77 =head2 C<get_taxid>
78  
79 This method receives a GI and returns the corresponding Taxid.
80  
81 =head1 SEE ALSO
82  
83 L<DBHash>
84 L<Bio::DB::Taxonomy>
85  
86 =head1 AUTHOR
87  
88 Miguel Pignatelli
89  
90 Any comments should be addressed to emepyc@gmail.com
91  
92 =head1 LICENSE
93  
94 Copyright 2013 Miguel Pignatelli, all rights reserved.
95  
96 This library is free software; you may redistribute it and/or modify it under the same terms as Perl itself.
97  
98  
99 =cut
100  
101 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
102 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
103 4 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
104 4 ▶use File::Tail;
Subroutine BEGIN: called CC=1 SCAR=0.0
105 4 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
106  
107 4 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
108 4 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
109 4 @ISA = qw(Exporter);
110  
111 4 @EXPORT = (); # Only qualified exports are allowed
112 4 @EXPORT_OK = qw(new_dict);
113 4 $VERSION = '0.12';
114  
115 sub new
116 {
117 3 ▶ my ($class, %args) = @_;
Subroutine new: called CC=8 SCAR=22.2
Pod documented
118 3 my %opts;
119  
120 3 ▶ $args{'dict'} or croak "Need the dictionary file";
Branch: 1 branch
BranchTrueFalseTotal
$args{"dict"} or croak("Need the dictionary file") 0 3 3
121 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
122  
123 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
124 3 my $dictfh;
125 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
126 0 $dictfh = $args{dict}; # TO DO: Test flags, the filehandle must be readable
127 } else {
128 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
129 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
130 }
131  
132 2 $opts{fh} = $dictfh;
133 2 $opts{save_mem} = $save_mem;
134 2 my $self = bless \%opts;
135  
136 2 $self -> _build_dict();
137 2 return $self;
138 }
139  
140 sub _build_dict
141 {
142 2 ▶ my ($self) = @_;
Subroutine _build_dict: called CC=3 SCAR=11.2
143 2 my $data;
144 2 ▶ $self->{save_mem} && return;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"save_mem"} and return 0 2 2
145 2 my $n = sysread( $self->{fh}, $data, -s $self->{fh} );
146 2 ▶ croak "Can't read input file" unless ($n);
Branch: 1 branch
BranchTrueFalseTotal
unless $n 0 2 2
147 2 $self->{dict} = \$data;
148 }
149  
150 sub get_taxid
151 {
152 4 ▶ my ($self, $gi) = @_;
Subroutine get_taxid: called CC=1 SCAR=0.0
Pod documented
153 4 return $self->_direct_lookup ($gi);
154 }
155  
156 sub _direct_lookup {
157 4 ▶ my ($self,$gi) = @_;
Subroutine _direct_lookup: called CC=3 SCAR=12.2
158 4 ▶ if ($self->{save_mem}){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'save_mem'}) { } 0 4 4
159 0 my $taxidBytes;
160 0 sysseek ($self->{fh},$gi*3,0);
161 0 sysread($self->{fh},$taxidBytes,3);
162 0 my ($taxid1, $taxid2, $taxid3) = unpack "CCC", $taxidBytes;
163 0 return $taxid3 | $taxid2 << 8 | $taxid1 << 16 | 0 << 24 ;
164 } else {
165 4 ▶ no warnings qw/substr/;
Subroutine BEGIN: called CC=1 SCAR=0.0
166 4 my $v = substr(${$self->{dict}}, $gi*3, 3);
167 4 ▶ return 0 unless ($v);
Branch: 1 branch
BranchTrueFalseTotal
unless $v 0 4 4
168 4 my ($taxid1, $taxid2, $taxid3) = unpack "CCC", $v;
169 4 my $taxid = $taxid3 | $taxid2 << 8 | $taxid1 << 16 | 0 << 24 ;
170 4 return $taxid;
171 }
172 }
173  
174 sub new_dict {
175 3 ▶ my (%args) = @_;
Subroutine new_dict: called CC=16 SCAR=28.6
Pod documented
176  
177 # TO DO -> Multiple inputs should be allowed
178 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
179  
180 2 my $outfh;
181 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
182 0 $outfh = \*STDOUT
183 ▶ } 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
184 1 $outfh = $args{out}; ## TO DO: Test flags, the filehandle must be writeable
185 } else {
186 1 ▶ open $outfh, ">", $args{out} or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
open $outfh, ">", $args{"out"} or croak($!) 0 1 1
187 }
188  
189 2 my $infh;
190 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
191 0 $infh = $args{in}; ## TO DO: Test flags, the filehandle must be readable
192 } else {
193 2 ▶ open $infh, "<", $args{in} or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
open $infh, "<", $args{"in"} or croak($!) 0 2 2
194 }
195  
196 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
197 2 my $last_line = $ftail->read();
198 2 ▶ croak "$args{in} is empty" unless (defined $last_line);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $last_line 0 2 2
199 2 my ($last_val) = split /\t/, $last_line;
200  
201 2 ▶ my $chunk_size = $args{chunk_size} || 30000000; # 30Mb -- 10 million records per chunk
Condition50%1/2
$args{'chunk_size'} || 30000000
l!l
20
MC/DC0%0/1
$args{'chunk_size'} || 30000000
$args{'chunk_size'}
Truth table50%1/2
A
A= $args{'chunk_size'}
Aresult
11
00
202 2 $chunk_size = floor($chunk_size / 3);
203  
204 2 my $bin_chunk = "\0" x (3 * ($chunk_size));
205 2 my $n_chunk = 0;
206 2 my $chunk_pos = $chunk_size * $n_chunk;
207 2 my $chunk_limit = $chunk_size * ($n_chunk + 1);
208 2 ▶ while (<$infh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $infh)) 24 2 26
209 24 chomp;
210 24 my ($key,$val) = split /\t/;
211 24 my ($taxid1, $taxid2, $taxid3, $taxid4) = unpack("CCCC", pack("N", $val));
212 24 ▶ if ($key >= $chunk_limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key >= $chunk_limit) 8 16 24
213 8 print {$outfh} $bin_chunk;
214 8 $bin_chunk = "\0" x (3 * ($chunk_size));
215 8 $n_chunk++;
216 8 $chunk_pos = $chunk_size * $n_chunk;
217 8 $chunk_limit = $chunk_size * ($n_chunk + 1);
218 }
219 24 substr($bin_chunk, ($key-$chunk_pos)*3, 1, pack("C", $taxid2));
220 24 substr($bin_chunk, ($key-$chunk_pos)*3+1, 1, pack("C", $taxid3));
221 24 substr($bin_chunk, ($key-$chunk_pos)*3+2, 1, pack("C", $taxid4));
222 }
223 2 print {$outfh} $bin_chunk;
224  
225 2 close ($infh);
226 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
227 1 close($outfh)}
228 }
229  
230 4 1;
↑ Summary