blib/lib/Bio/GenomeMap.pm

filter: Statementstmt 89.7% 175 / 195 Branchbran 67.6% 23 / 34 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 91.6% 33 / 36 Podpod 58.8% 10 / 17 totaltotal 84.4% 245 / 290 CC 20 SCAR 8.7
CC
20
Cov
86%
insert
6.0
search
5.0
seqname_to_seqid
4.1
CRAP 2.4
↑ Summary
1 package Bio::GenomeMap;
2 {
3 1 $Bio::GenomeMap::VERSION = '0.03';
4 }
5 # ABSTRACT: Data structure store and query genomically indexed data efficiently using SQLite's R*Tree.
6  
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use 5.010_000;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Carp qw/carp croak cluck/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Digest::SHA;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use File::stat;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Storable qw/freeze thaw/;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # responsibility of this class:
20 # create sqlite db if necessary. handle digest
21 # set up dbh
22 # set up statement handles
23  
24 1 has sqlite_file => ( is => 'ro' );
25 1 has ro => (is => 'ro', default => 0);
26  
27 1 has dbh => ( is => 'rw', init_arg => undef);
28 1 has sth_insert_rtree => ( is => 'rw', init_arg => undef);
29 1 has sth_insert_data => ( is => 'rw', init_arg => undef);
30 1 has sth_insert_sequence => ( is => 'rw', init_arg => undef);
31 1 has sth_select_sequence => ( is => 'rw', init_arg => undef);
32 1 has sth_select_seqid => ( is => 'rw', init_arg => undef);
33 1 has sth_select_overlap => ( is => 'rw', init_arg => undef);
34 1 has sth_select_surrounding => ( is => 'rw', init_arg => undef);
35 1 has sth_select_within => ( is => 'rw', init_arg => undef);
36 1 has sth_select_all => ( is => 'rw', init_arg => undef);
37 1 has sth_search_data => ( is => 'rw', init_arg => undef);
38  
39 2 ▶sub BUILD{
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
40 2 my ($self) = @_;
41 2 ▶ my $sqlite = $self->sqlite_file // ':memory:';
Condition100%2/2
$self->sqlite_file // ":memory:"
l!l
11
MC/DC100%1/1
$self->sqlite_file // ":memory:"
$self->sqlite_file
Truth table100%2/2
A
A= $self->sqlite_file
Aresult
11
00
42  
43 2 $self->dbh(DBI->connect("dbi:SQLite:dbname=$sqlite","","", {
44 RaiseError => 1,
45 ReadOnly => $self->ro(),
46 AutoCommit => 0}
47 ));
48  
49 2 $self->dbh->do(q{PRAGMA automatic_index = OFF});
50 2 $self->dbh->do(q{PRAGMA journal_mode = OFF});
51 2 $self->dbh->do(q{PRAGMA cache_size = 80000});
52  
53 2 $self->dbh->do(q{
54 create virtual table if not exists map using rtree_i32(
55 id,
56 seqid1,
57 seqid2,
58 start,
59 end
60 ); });
61 2 $self->dbh->do(q{
62 create table if not exists data (
63 id integer primary key,
64 is_storable integer,
65 data
66 ); });
67 2 $self->dbh->do(q{
68 create table if not exists sequence (
69 id integer primary key,
70 name
71 ); });
72  
73 2 $self->sth_insert_rtree(
74 $self->dbh->prepare(q{insert into map (seqid1, seqid2, start, end) values (?,?,?,?); })
75 );
76 2 $self->sth_insert_data(
77 $self->dbh->prepare(q{insert into data (id, is_storable, data) values (last_insert_rowid(),?,?); })
78 );
79 2 $self->sth_insert_sequence(
80 $self->dbh->prepare(q{insert into sequence (name) values (?); })
81 );
82  
83 2 $self->sth_select_sequence(
84 $self->dbh->prepare(q{select id from sequence where name = ?; })
85 );
86  
87 2 $self->sth_select_seqid(
88 $self->dbh->prepare(q{select name from sequence where id = ?; })
89 );
90  
91 2 $self->sth_select_overlap(
92 $self->dbh->prepare(
93 q{
94 select map.start, map.end, data.is_storable, data.data
95 from data, map where
96 (data.id = map.id)
97 and
98 (? <= map.end and map.start <= ?)
99 and
100 (map.seqid1 = ?)
101 order by map.start;
102 }
103 )
104 );
105  
106 2 $self->sth_select_within(
107 $self->dbh->prepare(
108 q{
109 select map.start, map.end, data.is_storable, data.data
110 from data, map where
111 (data.id = map.id)
112 and
113 (? <= map.start and map.end <= ?)
114 and
115 (map.seqid1 = ?)
116 order by map.start;
117 }
118 )
119 );
120  
121 2 $self->sth_select_surrounding(
122 $self->dbh->prepare(
123 q{
124 select map.start, map.end, data.is_storable, data.data
125 from data, map where
126 (data.id = map.id)
127 and
128 (map.start <= ? and ? <= map.end)
129 and
130 (map.seqid1 = ?)
131 order by map.start;
132 }
133 )
134 );
135  
136 2 $self->sth_select_all(
137 $self->dbh->prepare(
138 q{
139 select map.seqid1, map.start, map.end, data.is_storable, data.data
140 from data, map
141 where
142 data.id = map.id
143 order by map.seqid1, map.start
144 ;
145 }
146 )
147 );
148  
149 2 $self->sth_search_data(
150 $self->dbh->prepare(
151 q{
152 select map.seqid1, map.start, map.end, data.is_storable, data.data
153 from data, map
154 where
155 data.data like ?
156 and
157 data.id = map.id
158 order by map.seqid1, map.start
159 ;
160 }
161 )
162 );
163 }
164  
165 2 ▶sub DEMOLISH{
Subroutine DEMOLISH: called CC=2 SCAR=7.0
Pod undocumented
166 2 my $self = shift;
167 # not sure about this
168 2 ▶ if (defined $self->dbh){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->dbh) 2 0 2
169 2 $self->dbh->commit;
170 2 $self->dbh(undef);
171 }
172 }
173  
174  
175 # seqid cache so that we don't have to get the sequence from db every single
176 # insert. we also use cache to get id for SELECTION as well-- I couldn't
177 # figure out how to make the select statements to do 3 table joins using all
178 # indices... (I think the problem is that I'm using a dimension of the rtree as
179 # a foreign key (seqid1 and seqid2),
180  
181 has seqidcache => (
182 traits => ['Hash'],
183 is => 'ro',
184 isa => 'HashRef[Str]',
185 2 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
186 1 handles => {
187 set_seqidcache => 'set',
188 get_seqidcache => 'get',
189 has_seqidcache => 'exists',
190 },
191 );
192 2596 ▶sub seqname_to_seqid{
Subroutine seqname_to_seqid: called CC=4 SCAR=14.2
Pod undocumented
193 2596 my ($self, $seqname) = @_;
194 2596 ▶ if ($self->has_seqidcache($seqname)){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_seqidcache($seqname)) { } 2582 14 2596
195 2582 return $self->get_seqidcache($seqname);
196 }
197 else{
198 14 my $select = $self->sth_select_sequence();
199 14 $select->execute($seqname);
200 14 ▶ if (my $row = $select->fetchrow_arrayref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $select->fetchrow_arrayref) { } 0 14 14
201 0 $self->set_seqidcache($seqname, $row->[0]);
202 0 return $row->[0];
203 }
204 else{
205 14 my $insert = $self->sth_insert_sequence();
206 14 $insert->execute($seqname);
207 14 ▶ my $row = $self->dbh->selectrow_arrayref('select last_insert_rowid();') or croak "couldn't get sequence insert last rowid";
Branch: 1 branch
BranchTrueFalseTotal
my $row = $self->dbh->selectrow_arrayref("select last_insert_rowid();") or croak("couldn't get sequence insert last rowid") 14 0 14
208 14 $self->set_seqidcache($seqname, $row->[0]);
209 14 return $row->[0];
210 }
211 }
212 }
213  
214 has seqnamecache => (
215 traits => ['Hash'],
216 is => 'ro',
217 isa => 'HashRef[Str]',
218 2 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
219 1 handles => {
220 set_seqnamecache => 'set',
221 get_seqnamecache => 'get',
222 has_seqnamecache => 'exists',
223 },
224 );
225 2924 ▶sub seqid_to_seqname{
Subroutine seqid_to_seqname: called CC=3 SCAR=11.1
Pod undocumented
226 2924 my ($self, $seqid) = @_;
227 2924 ▶ if ($self->has_seqnamecache($seqid)){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_seqnamecache($seqid)) { } 2910 14 2924
228 2910 return $self->get_seqnamecache($seqid);
229 }
230 else{
231 14 my $select = $self->sth_select_seqid();
232 14 $select->execute($seqid);
233 14 ▶ if (my $row = $select->fetchrow_arrayref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $select->fetchrow_arrayref) { } 14 0 14
234 14 $self->set_seqnamecache($seqid, $row->[0]);
235 14 return $row->[0];
236 }
237 else{
238 0 croak "no such seqid $seqid";
239 }
240 }
241  
242 }
243  
244 2 ▶sub bulk_insert{
Subroutine bulk_insert: called CC=1 SCAR=0.1
Pod documented
245 2 my ($self, $sub) = @_;
246 2 my $dbh = $self->dbh;
247 2 my $sth_insert_rtree = $self->sth_insert_rtree;
248 2 my $sth_insert_data = $self->sth_insert_data;
249  
250 2 my $counter = 1;
251 2586 ▶ my $inserter = sub{
Subroutine __ANON__: called CC=3 SCAR=12.0
252 2586 my ($seqname, $start, $end, $data) = @_;
253 2586 my $seqid = $self->seqname_to_seqid($seqname);
254  
255 2586 $sth_insert_rtree->execute($seqid, $seqid, $start, $end);
256 2586 ▶ if (ref $data eq ''){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq '') { } 2586 0 2586
257 2586 $sth_insert_data->execute(0, $data);
258 }
259 else{
260 0 $sth_insert_data->execute(1, freeze($data));
261 }
262  
263 2586 ▶ if (++$counter % 50_000 == 0){
Branch: 1 branch
BranchTrueFalseTotal
if (++$counter % 50000 == 0) 0 2586 2586
264 0 $dbh->commit;
265 }
266 2 };
267 2 $sub->($inserter);
268 2 $dbh->commit;
269 }
270  
271 # this is a higher level insert function. Commits every time so not efficient.
272 # use bulk_insert() instead.
273 0 ▶sub insert{
Subroutine insert: not called CC=2 SCAR=17.9
Pod undocumented
274 0 my ($self, $seqname, $start, $end, $data) = @_;
275 0 my $seqid = $self->seqname_to_seqid($seqname);
276 0 $self->sth_insert_rtree->execute($seqid, $seqid, $start, $end);
277 0 ▶ if (ref $data eq ''){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq '') { } 0 0 0
278 0 $self->sth_insert_data->execute(0, $data);
279 }
280 else{
281 0 $self->sth_insert_data->execute(1, freeze($data));
282 }
283 0 $self->dbh->commit;
284 }
285  
286 1 ▶sub commit{
Subroutine commit: called CC=1 SCAR=0.0
Pod undocumented
287 1 my $self = shift;
288 1 $self->dbh->commit;
289 }
290  
291 # select
292  
293 10 ▶sub _select_iter{
Subroutine _select_iter: called CC=3 SCAR=11.2
294 10 my ($sth, $seqid, $start, $end, $code) = @_;
295  
296 10 $sth->execute( $start, $end, $seqid);
297 10 ▶ while (my $row = $sth->fetchrow_arrayref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_arrayref) 120 10 130
298 120 my ($s, $e, $is_storable, $data) = @$row;
299 120 ▶ if ($is_storable){
Branch: 1 branch
BranchTrueFalseTotal
if ($is_storable) { } 0 120 120
300 0 $code->($s, $e, thaw($data));
301 }
302 else{
303 120 $code->($s, $e, $data);
304 }
305 }
306 }
307 2 ▶sub iter_overlaps{
Subroutine iter_overlaps: called CC=1 SCAR=0.0
Pod documented
308 2 my ($self, $seqname, $start, $end, $code) = @_;
309 2 my $seqid = $self->seqname_to_seqid($seqname);
310 2 _select_iter($self->sth_select_overlap, $seqid, $start, $end, $code);
311 }
312  
313 0 ▶sub iter_surrounding{
Subroutine iter_surrounding: not called CC=1 SCAR=6.9
Pod documented
314 0 my ($self, $seqname, $start, $end, $code) = @_;
315 0 my $seqid = $self->seqname_to_seqid($seqname);
316 0 _select_iter($self->sth_select_surrounding, $seqid, $start, $end, $code);
317 }
318 0 ▶sub iter_within{
Subroutine iter_within: not called CC=1 SCAR=6.9
Pod documented
319 0 my ($self, $seqname, $start, $end, $code) = @_;
320 0 my $seqid = $self->seqname_to_seqid($seqname);
321 0 _select_iter($self->sth_select_within, $seqid, $start, $end, $code);
322 }
323  
324 2 ▶sub iter_all{
Subroutine iter_all: called CC=3 SCAR=11.1
Pod documented
325 2 my ($self, $code) = @_;
326 2 my $sth = $self->sth_select_all;
327 2 $sth->execute();
328  
329 2 ▶ while (my $row = $sth->fetchrow_arrayref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_arrayref) 2586 2 2588
330 2586 my ($seqid, $s, $e, $is_storable, $data) = @$row;
331 2586 my $seqname = $self->seqid_to_seqname($seqid);
332 2586 ▶ if ($is_storable){
Branch: 1 branch
BranchTrueFalseTotal
if ($is_storable) { } 0 2586 2586
333 0 $code->($seqname, $s, $e, thaw($data));
334 }
335 else{
336 2586 $code->($seqname, $s, $e, $data);
337 }
338 }
339 }
340  
341 # slurping
342  
343 8 ▶sub _slurp{
Subroutine _slurp: called CC=1 SCAR=0.0
344 8 my ($sth, $seqid, $start, $end) = @_;
345 8 my @accum;
346 90 ▶ _select_iter($sth, $seqid, $start, $end, sub{
Subroutine __ANON__: called CC=1 SCAR=0.0
347 90 my ($start, $end, $data) = @_;
348 90 push @accum, [$start, $end, $data]
349 8 });
350 8 return \@accum;
351 }
352  
353 4 ▶sub slurp_overlaps{
Subroutine slurp_overlaps: called CC=1 SCAR=0.0
Pod documented
354 4 my ($self, $seqname, $start, $end) = @_;
355 4 my $seqid = $self->seqname_to_seqid($seqname);
356 4 _slurp($self->sth_select_overlap, $seqid, $start, $end);
357 }
358  
359 2 ▶sub slurp_surrounding{
Subroutine slurp_surrounding: called CC=1 SCAR=0.0
Pod documented
360 2 my ($self, $seqname, $start, $end) = @_;
361 2 my $seqid = $self->seqname_to_seqid($seqname);
362 2 _slurp($self->sth_select_surrounding, $seqid, $start, $end);
363 }
364  
365 2 ▶sub slurp_within{
Subroutine slurp_within: called CC=1 SCAR=0.0
Pod documented
366 2 my ($self, $seqname, $start, $end) = @_;
367 2 my $seqid = $self->seqname_to_seqid($seqname);
368 2 _slurp($self->sth_select_within, $seqid, $start, $end);
369 }
370  
371 2 ▶sub slurp_all{
Subroutine slurp_all: called CC=1 SCAR=0.0
Pod documented
372 2 my ($self) = @_;
373 2 my @accum;
374 2586 ▶ $self->iter_all(sub{
Subroutine __ANON__: called CC=1 SCAR=0.0
375 2586 my ($seq, $start, $end, $data) = @_;
376 2586 push @accum, [$seq, $start, $end, $data]
377 2 });
378 2 return \@accum;
379 }
380  
381 2 ▶sub sequences{
Subroutine sequences: called CC=2 SCAR=6.9
Pod undocumented
382 2 my $self = shift;
383 2 my $dbh = $self->dbh;
384  
385 2 my $select_sth = $dbh->prepare(q{
386 select name from sequence order by name asc;
387 });
388 2 $select_sth->execute();
389  
390 2 my @accum;
391 2 ▶ while (my $row = $select_sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $select_sth->fetchrow_hashref) 14 2 16
392 14 push @accum, $row->{name};
393 }
394 2 return @accum;
395 }
396  
397 6 ▶sub search{
Subroutine search: called CC=5 SCAR=16.2
Pod documented
398 6 my ($self, $query, $start, $limit) = @_;
399 6 $query = '%' . $query . '%';
400 6 my $sth = $self->sth_search_data();
401 6 $sth->execute($query);
402  
403 6 my @accum;
404 6 my $counter = 0;
405 6 my $end = $start + $limit - 1;
406  
407 # select map.seqid1, map.start, map.end, data.is_storable, data.data
408 6 ▶ while (my $row = $sth->fetchrow_arrayref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_arrayref) 338 2 340
409 338 my ($seqid, $s, $e, $sto, $data) = @$row;
410 338 my $seqname = $self->seqid_to_seqname($seqid);
411 338 ▶ if ($start <= $counter && $counter <= $end){
Branch: 1 branch
BranchTrueFalseTotal
if ($start <= $counter and $counter <= $end) 338 0 338
Condition33%1/3
$start <= $counter and $counter <= $end
!ll&&!rl&&r
00338
MC/DC0%0/2
$start <= $counter and $counter <= $end
$start <= $counter $counter <= $end
Truth table33%1/3
A and B
A= $start <= $counterB= $counter <= $end
ABresult
0X0
100
111
412 338 push @accum, [$seqname, $s, $e, $data];
413 338 $counter++;
414 }
415 338 ▶ last if ($counter > $end);
Branch: 1 branch
BranchTrueFalseTotal
if $counter > $end 4 334 338
416 }
417 6 return @accum;
418 }
419  
420 1 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
421 1 __PACKAGE__->meta->make_immutable;
422  
423 1 1;
424  
425 __END__
↑ Summary