SeqFeature/Store/DBI/SQLite.pm

filter: Statementstmt 10.1% 59 / 581 Branchbran 0.0% 0 / 242 Conditioncond 0.0% 0 / 66 MC/DCmcdc 0.0% 0 / 37 Subroutinesub 27.5% 16 / 58 Podpod 18.1% 2 / 11 totaltotal 7.7% 77 / 995 CC 166 SCAR 57.2
CC
166
Cov
7%
_features
1482.0
_fetch_sequence
240.0
init
156.0
CRAP 305.9
« SeqFeature/Store/FeatureFileLoader.pm ↑ Summary SeqFeature/Store/DBI/mysql.pm »
1 package Bio::DB::SeqFeature::Store::DBI::SQLite;
2 1 $Bio::DB::SeqFeature::Store::DBI::SQLite::VERSION = '1.7.3';
3 #$Id$
4  
5 =head1 NAME
6  
7 Bio::DB::SeqFeature::Store::DBI::SQLite -- SQLite implementation of Bio::DB::SeqFeature::Store
8  
9 =head1 SYNOPSIS
10  
11 use Bio::DB::SeqFeature::Store;
12  
13 # Open the sequence database
14 my $db = Bio::DB::SeqFeature::Store->new(-adaptor => 'DBI::SQLite',
15 -dsn => '/path/to/database.db');
16  
17 # get a feature from somewhere
18 my $feature = Bio::SeqFeature::Generic->new(...);
19  
20 # store it
21 $db->store($feature) or die "Couldn't store!";
22  
23 # primary ID of the feature is changed to indicate its primary ID
24 # in the database...
25 my $id = $feature->primary_id;
26  
27 # get the feature back out
28 my $f = $db->fetch($id);
29  
30 # change the feature and update it
31 $f->start(100);
32 $db->update($f) or die "Couldn't update!";
33  
34 # searching...
35 # ...by id
36 my @features = $db->fetch_many(@list_of_ids);
37  
38 # ...by name
39 @features = $db->get_features_by_name('ZK909');
40  
41 # ...by alias
42 @features = $db->get_features_by_alias('sma-3');
43  
44 # ...by type
45 @features = $db->get_features_by_name('gene');
46  
47 # ...by location
48 @features = $db->get_features_by_location(-seq_id=>'Chr1',-start=>4000,-end=>600000);
49  
50 # ...by attribute
51 @features = $db->get_features_by_attribute({description => 'protein kinase'})
52  
53 # ...by the GFF "Note" field
54 @result_list = $db->search_notes('kinase');
55  
56 # ...by arbitrary combinations of selectors
57 @features = $db->features(-name => $name,
58 -type => $types,
59 -seq_id => $seqid,
60 -start => $start,
61 -end => $end,
62 -attributes => $attributes);
63  
64 # ...using an iterator
65 my $iterator = $db->get_seq_stream(-name => $name,
66 -type => $types,
67 -seq_id => $seqid,
68 -start => $start,
69 -end => $end,
70 -attributes => $attributes);
71  
72 while (my $feature = $iterator->next_seq) {
73 # do something with the feature
74 }
75  
76 # ...limiting the search to a particular region
77 my $segment = $db->segment('Chr1',5000=>6000);
78 my @features = $segment->features(-type=>['mRNA','match']);
79  
80 # getting & storing sequence information
81 # Warning: this returns a string, and not a PrimarySeq object
82 $db->insert_sequence('Chr1','GATCCCCCGGGATTCCAAAA...');
83 my $sequence = $db->fetch_sequence('Chr1',5000=>6000);
84  
85 # what feature types are defined in the database?
86 my @types = $db->types;
87  
88 # create a new feature in the database
89 my $feature = $db->new_feature(-primary_tag => 'mRNA',
90 -seq_id => 'chr3',
91 -start => 10000,
92 -end => 11000);
93  
94 =head1 DESCRIPTION
95  
96 Bio::DB::SeqFeature::Store::SQLite is the SQLite adaptor for
97 Bio::DB::SeqFeature::Store. You will not create it directly, but
98 instead use Bio::DB::SeqFeature::Store-E<gt>new() to do so.
99  
100 See L<Bio::DB::SeqFeature::Store> for complete usage instructions.
101  
102 =head2 Using the SQLite adaptor
103  
104 To establish a connection to the database, call
105 Bio::DB::SeqFeature::Store-E<gt>new(-adaptor=E<gt>'DBI::SQLite',@more_args). The
106 additional arguments are as follows:
107  
108 Argument name Description
109 ------------- -----------
110  
111 -dsn The path to the SQLite database file.
112  
113 -namespace A prefix to attach to each table. This allows you
114 to have several virtual databases in the same
115 physical database.
116  
117 -temp Boolean flag. If true, a temporary database
118 will be created and destroyed as soon as
119 the Store object goes out of scope. (synonym -temporary)
120  
121 -autoindex Boolean flag. If true, features in the database will be
122 reindexed every time they change. This is the default.
123  
124 -fts Boolean flag. If true, when the -create flag is true, the
125 attribute table will be created and indexed index for
126 full-text search using the most recent FTS extension
127 supported by DBD::SQLite.
128  
129 -tmpdir Directory in which to place temporary files during "fast" loading.
130 Defaults to File::Spec->tmpdir(). (synonyms -dump_dir, -dumpdir, -tmp)
131  
132 -dbi_options A hashref to pass to DBI->connect's 4th argument, the "attributes."
133 (synonyms -options, -dbi_attr)
134  
135 -write Pass true to open database for writing or updating.
136  
137 If successful, a new instance of
138 Bio::DB::SeqFeature::Store::DBI::SQLite will be returned.
139  
140 In addition to the standard methods supported by all well-behaved
141 Bio::DB::SeqFeature::Store databases, several following
142 adaptor-specific methods are provided. These are described in the next
143 sections.
144  
145 =cut
146  
147 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
148  
149 1 ▶use base 'Bio::DB::SeqFeature::Store::DBI::mysql';
Subroutine BEGIN: called CC=1 SCAR=0.0
150 1 ▶use Bio::DB::SeqFeature::Store::DBI::Iterator;
Subroutine BEGIN: called CC=1 SCAR=0.0
151 1 ▶use DBD::SQLite;
Subroutine BEGIN: called CC=1 SCAR=0.0
152 1 ▶use DBI qw(:sql_types);
Subroutine BEGIN: called CC=1 SCAR=0.0
153 1 ▶use Memoize;
Subroutine BEGIN: called CC=1 SCAR=0.0
154 1 ▶use Cwd qw(abs_path getcwd);
Subroutine BEGIN: called CC=1 SCAR=0.0
155 1 ▶use Bio::DB::GFF::Util::Rearrange 'rearrange';
Subroutine BEGIN: called CC=1 SCAR=0.0
156 1 ▶use Bio::SeqFeature::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
157 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
158 1 ▶use constant DEBUG=>0;
Subroutine BEGIN: called CC=1 SCAR=0.0
159 1 ▶use constant EXPERIMENTAL_COVERAGE=>1;
Subroutine BEGIN: called CC=1 SCAR=0.0
160  
161 # Using same limits as MySQL adaptor so I don't have to make something up.
162 1 ▶use constant MAX_INT => 2_147_483_647;
Subroutine BEGIN: called CC=1 SCAR=0.0
163 1 ▶use constant MIN_INT => -2_147_483_648;
Subroutine BEGIN: called CC=1 SCAR=0.0
164 1 ▶use constant SUMMARY_BIN_SIZE => 1000; # we checkpoint coverage this often, about 20 meg overhead per feature type on hg
Subroutine BEGIN: called CC=1 SCAR=0.0
165 1 ▶use constant USE_SPATIAL=>0;
Subroutine BEGIN: called CC=1 SCAR=0.0
166  
167 # The binning scheme places each feature into a bin.
168 # Bins are variably sized as powers of two. For example,
169 # there are 585 bins of size 2**17 (131072 bases)
170 1 my (@BINS,%BINS);
171 {
172 1 @BINS = map {2**$_} (17, 20, 23, 26, 29); # TO DO: experiment with different bin sizes
173 1 my $start=0;
174 1 for my $b (sort {$b<=>$a} @BINS) {
175 5 $BINS{$b} = $start;
176 5 $start += $BINS[-1]/$b;
177 }
178 }
179  
180  
181 # my %BINS = (
182 # 2**11 => 37449,
183 # 2**14 => 4681,
184 # 2**17 => 585,
185 # 2**20 => 73,
186 # 2**23 => 9,
187 # 2**26 => 1,
188 # 2**29 => 0
189 # );
190 # my @BINS = sort {$a<=>$b} keys %BINS;
191  
192 sub calculate_bin {
193 0 ▶ my $self = shift;
Subroutine calculate_bin: not called CC=4 SCAR=30.0
Pod undocumented
194 0 my ($start,$end) = @_;
195  
196 0 my $len = $end - $start;
197 0 for my $bin (@BINS) {
198 0 ▶ next if $len > $bin;
Branch: 1 branch
BranchTrueFalseTotal
if $len > $bin 0 0 0
199 # possibly fits here
200 0 my $binstart = int $start/$bin;
201 0 my $binend = int $end/$bin;
202 0 ▶ return $binstart+$BINS{$bin} if $binstart == $binend;
Branch: 1 branch
BranchTrueFalseTotal
if $binstart == $binend 0 0 0
203 }
204  
205 0 die "unreasonable coordinates ",$start+1,"..$end";
206 }
207  
208 sub search_bins {
209 0 ▶ my $self = shift;
Subroutine search_bins: not called CC=2 SCAR=17.9
Pod undocumented
210 0 my ($start,$end) = @_;
211 0 my @results;
212  
213 0 for my $bin (@BINS) {
214 0 my $binstart = int $start/$bin;
215 0 my $binend = int $end/$bin;
216 0 push @results,$binstart+$BINS{$bin}..$binend+$BINS{$bin};
217 }
218 0 return @results;
219 }
220  
221  
222 ###
223 # object initialization
224 #
225 sub init {
226 0 ▶ my $self = shift;
Subroutine init: not called CC=12 SCAR=50.5
Pod documented
227 0 my ($dsn,
228 $is_temporary,
229 $autoindex,
230 $namespace,
231 $dump_dir,
232 $user,
233 $pass,
234 $dbi_options,
235 $writeable,
236 $fts,
237 $create,
238 ) = rearrange(['DSN',
239 ['TEMP','TEMPORARY'],
240 'AUTOINDEX',
241 'NAMESPACE',
242 ['DUMP_DIR','DUMPDIR','TMP','TMPDIR'],
243 'USER',
244 ['PASS','PASSWD','PASSWORD'],
245 ['OPTIONS','DBI_OPTIONS','DBI_ATTR'],
246 ['WRITE','WRITEABLE'],
247 'FTS',
248 'CREATE',
249 ],@_);
250 0 ▶ $dbi_options ||= {};
Condition0%0/2
$dbi_options ||= {}
l!l
00
MC/DC0%0/1
$dbi_options ||= {}
$dbi_options
Truth table0%0/2
A
A= $dbi_options
Aresult
11
00
251 0 ▶ $writeable = 1 if $is_temporary or $dump_dir;
Branch: 1 branch
BranchTrueFalseTotal
if $is_temporary or $dump_dir 0 0 0
Condition0%0/3
$is_temporary or $dump_dir
l!l&&r!l&&!r
000
MC/DC0%0/2
$is_temporary or $dump_dir
$is_temporary $dump_dir
Truth table0%0/3
A or B
A= $is_temporaryB= $dump_dir
ABresult
1X1
011
000
252  
253 0 ▶ $dsn or $self->throw("Usage: ".__PACKAGE__."->init(-dsn => \$dbh || \$dsn)");
Branch: 1 branch
BranchTrueFalseTotal
$dsn or $self->throw("Usage: Bio::DB::SeqFeature::Store::DBI::SQLite->init(-dsn => \$dbh || \$dsn)") 0 0 0
254  
255 0 my $dbh;
256 0 ▶ if (ref $dsn) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $dsn) { } 0 0 0
257 0 $dbh = $dsn;
258 } else {
259 0 ▶ $dsn = "dbi:SQLite:$dsn" unless $dsn =~ /^dbi:/;
Branch: 1 branch
BranchTrueFalseTotal
unless $dsn =~ /^dbi:/ 0 0 0
260 0 ▶ $dbh = DBI->connect($dsn,$user,$pass,$dbi_options) or $self->throw($DBI::errstr);
Branch: 1 branch
BranchTrueFalseTotal
$dbh = "DBI"->connect($dsn, $user, $pass, $dbi_options) or $self->throw($DBI::errstr) 0 0 0
261 0 $dbh->do("PRAGMA synchronous = OFF;"); # makes writes much faster
262 0 $dbh->do("PRAGMA temp_store = MEMORY;"); # less disk I/O; some speedup
263 0 $dbh->do("PRAGMA cache_size = 20000;"); # less disk I/O; some speedup
264 # Keep track of database file location
265 0 my $cwd = getcwd;
266 0 my ($db_file) = ($dsn =~ m/(?:db(?:name)?|database)=(.+)$/);
267 0 $self->{dbh_file} = "$cwd/$db_file";
268 }
269 0 $self->{dbh} = $dbh;
270 0 $self->{fts} = $fts;
271 0 $self->{is_temp} = $is_temporary;
272 0 $self->{namespace} = $namespace;
273 0 $self->{writeable} = $writeable;
274  
275 0 $self->default_settings;
276 0 ▶ $self->autoindex($autoindex) if defined $autoindex;
Branch: 1 branch
BranchTrueFalseTotal
if defined $autoindex 0 0 0
277 0 ▶ $self->dumpdir($dump_dir) if $dump_dir;
Branch: 1 branch
BranchTrueFalseTotal
if $dump_dir 0 0 0
278 0 ▶ if ($self->is_temp) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->is_temp) { } 0 0 0
elsif ($create) { } 0 0 0
279 0 $self->init_tmp_database();
280 } elsif ($create) {
281 0 $self->init_database('erase');
282 }
283 }
284  
285 sub table_definitions {
286 0 ▶ my $self = shift;
Subroutine table_definitions: not called CC=3 SCAR=24.8
Pod undocumented
287 0 my $defs =
288 {
289 feature => <<END,
290 (
291 id integer primary key autoincrement,
292 typeid integer not null,
293 strand integer default 0,
294 "indexed" integer default 1,
295 object blob not null
296 )
297 END
298  
299 locationlist => <<END,
300 (
301 id integer primary key autoincrement,
302 seqname text not null
303 );
304 create index index_locationlist on locationlist (seqname);
305 END
306  
307 typelist => <<END,
308 (
309 id integer primary key autoincrement,
310 tag text not null collate nocase
311 );
312 create index index_typelist on typelist (tag);
313 END
314 name => <<END,
315 (
316 id integer not null,
317 name text not null collate nocase,
318 display_name integer default 0
319 );
320 create index index_name_id on name(id);
321 create index index_name_name on name(name);
322 END
323  
324 attribute => <<END,
325 (
326 id integer not null,
327 attribute_id integer not null,
328 attribute_value text collate nocase
329 );
330 create index index_attribute_id on attribute(attribute_id);
331 create index index_attribute_value on attribute(attribute_value);
332 END
333  
334 attributelist => <<END,
335 (
336 id integer primary key autoincrement,
337 tag text not null
338 );
339 create index index_attributelist_tag on attributelist(tag);
340 END
341 parent2child => <<END,
342 (
343 id integer,
344 child integer,
345 primary key(id, child)
346 ) without rowid;
347 END
348  
349 meta => <<END,
350 (
351 name text primary key,
352 value text not null
353 )
354 END
355 sequence => <<END,
356 (
357 id integer not null,
358 offset integer not null,
359 sequence blob,
360 primary key(id,offset)
361 )
362 END
363 };
364  
365 0 ▶ if ($self->{'fts'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"fts"}) 0 0 0
366 0 delete($defs->{attribute});
367 }
368  
369 0 ▶ unless ($self->_has_spatial_index) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_has_spatial_index) 0 0 0
370 0 $defs->{feature_location} = <<END;
371 (
372 id int(10) primary key,
373 seqid int(10),
374 bin int,
375 start int,
376 end int
377 );
378 create index index_feature_location on feature_location(seqid,bin,start,end);
379 END
380  
381 }
382  
383 0 if (EXPERIMENTAL_COVERAGE) {
384 0 $defs->{interval_stats} = <<END;
385 (
386 typeid integer not null,
387 seqid integer not null,
388 bin integer not null,
389 cum_count integer not null,
390 primary key (typeid,seqid,bin)
391 ) without rowid;
392 END
393 }
394 0 return $defs;
395 }
396  
397 sub _init_database {
398 0 ▶ my $self = shift;
Subroutine _init_database: not called CC=1 SCAR=6.9
399  
400 # must do this first before calling table_definitions
401 0 $self->_create_spatial_index;
402 0 $self->_create_attribute_fts;
403 0 $self->SUPER::_init_database(@_);
404 }
405  
406 # FIXME: ensure this works with _create_attribute_fts...
407 sub init_tmp_database {
408 0 ▶ my $self = shift;
Subroutine init_tmp_database: not called CC=1 SCAR=6.9
Pod undocumented
409 0 my $erase = shift;
410 0 $self->_create_spatial_index;
411 0 $self->_create_attribute_fts;
412 0 $self->SUPER::init_tmp_database(@_);
413 }
414  
415 sub _create_spatial_index{
416 0 ▶ my $self = shift;
Subroutine _create_spatial_index: not called CC=1 SCAR=6.9
417 0 my $dbh = $self->dbh;
418 0 local $dbh->{PrintError} = 0;
419 0 $dbh->do("DROP TABLE IF EXISTS feature_index"); # spatial index
420 0 if (USE_SPATIAL) {
421 $dbh->do("CREATE VIRTUAL TABLE feature_index USING RTREE(id,seqid,bin,start,end)");
422 }
423 }
424  
425 sub _create_attribute_fts{
426 0 ▶ my $self = shift;
Subroutine _create_attribute_fts: not called CC=4 SCAR=30.0
427 0 my $dbh = $self->dbh;
428 0 ▶ if ($self->{'fts'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"fts"}) 0 0 0
429 0 my @fts_versions;
430 0 for (@fts_versions = grep(/^ENABLE_FTS[0-9]+$/, DBD::SQLite::compile_options)) { s/ENABLE_// }
431 # use the latest supported FTS version.
432 # DBD::SQLite::compile_options appears to be sorted
433 # alphabetically, so this should work through version FTS9.
434 0 ▶ die 'fts not supported by this version of DBD::SQLite' if (!@fts_versions);
Branch: 1 branch
BranchTrueFalseTotal
unless @fts_versions 0 0 0
435 0 $dbh->do("DROP TABLE IF EXISTS attribute");
436 0 $dbh->do("CREATE VIRTUAL TABLE "
437 . $self->_attribute_table
438 . " USING " . $fts_versions[-1]
439 . "(id, attribute_id, attribute_value)");
440 }
441 }
442  
443 ###
444 # return 1 if an existing attribute table in the connected database is an FTS
445 # table, else 0
446 #
447 sub _has_fts {
448 0 ▶ my $self = shift;
Subroutine _has_fts: not called CC=2 SCAR=17.9
449 0 ▶ if (!defined($self->{'has_fts'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"has_fts"}) 0 0 0
450 # If the attribute table is a virtual table, assume it is an FTS
451 # table. Per http://www.sqlite.org/fileformat2.html:
452 # For (sqlite_master) rows that define views, triggers, and virtual
453 # tables, the rootpage column is 0 or NULL.
454 0 ($self->{'has_fts'}) = $self->dbh->selectrow_array("select count(*) from sqlite_master where type = 'table' and name = '"
455 . $self->_attribute_table
456 . "' and (rootpage = 0 or rootpage is null);");
457 }
458 0 return $self->{'has_fts'};
459 }
460  
461 sub _has_spatial_index {
462 0 ▶ my $self = shift;
Subroutine _has_spatial_index: not called CC=2 SCAR=17.9
463 0 ▶ return $self->{'_has_spatial_index'} if exists $self->{'_has_spatial_index'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_has_spatial_index"} 0 0 0
464 0 my $dbh = $self->dbh;
465 0 my ($count) = $dbh->selectrow_array("select count(*) from sqlite_master where name='feature_index'");
466 0 return $self->{'_has_spatial_index'} = $count;
467 }
468  
469 sub _finish_bulk_update {
470 0 ▶ my $self = shift;
Subroutine _finish_bulk_update: not called CC=10 SCAR=47.0
471 0 my $dbh = $self->dbh;
472 0 ▶ my $dir = $self->{dumpdir} || '.';
Condition0%0/2
$self->{'dumpdir'} || '.'
l!l
00
MC/DC0%0/1
$self->{'dumpdir'} || '.'
$self->{'dumpdir'}
Truth table0%0/2
A
A= $self->{'dumpdir'}
Aresult
11
00
473  
474 0 $self->begin_work; # making this a transaction greatly improves performance
475  
476 0 for my $table ('feature', $self->index_tables) {
477 0 my $fh = $self->dump_filehandle($table);
478 0 my $path = $self->dump_path($table);
479 0 $fh->close;
480  
481 0 ▶ open $fh, '<', $path or $self->throw("Could not read file '$path': $!");
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $path or $self->throw("Could not read file '${path}': $!") 0 0 0
482 0 my $qualified_table = $self->_qualify($table);
483  
484 0 my $sth;
485 0 ▶ if ($table =~ /feature$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($table =~ /feature$/) { } 0 0 0
486 0 $sth = $dbh->prepare("REPLACE INTO $qualified_table VALUES (?,?,?,?,?)");
487  
488 0 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
489 0 chomp();
490 0 my ($id,$typeid,$strand,$indexed,$obj) = split(/\t/);
491 0 $sth->bind_param(1, $id);
492 0 $sth->bind_param(2, $typeid);
493 0 $sth->bind_param(3, $strand);
494 0 $sth->bind_param(4, $indexed);
495 0 $sth->bind_param(5, pack('H*',$obj), {TYPE => SQL_BLOB});
496 0 $sth->execute();
497 }
498 } else {
499 0 my $feature_index = $self->_feature_index_table;
500 0 ▶ if ($table =~ /parent2child$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($table =~ /parent2child$/) { } 0 0 0
elsif ($table =~ /$feature_index$/) { } 0 0 0
501 0 $sth = $dbh->prepare("REPLACE INTO $qualified_table VALUES (?,?)");
502 } elsif ($table =~ /$feature_index$/) {
503 0 ▶ $sth = $dbh->prepare(
Branch: 1 branch
BranchTrueFalseTotal
$self->_has_spatial_index ? : 0 0 0
504 $self->_has_spatial_index ?"REPLACE INTO $qualified_table VALUES (?,?,?,?,?)"
505 :"REPLACE INTO $qualified_table (id,seqid,bin,start,end) VALUES (?,?,?,?,?)"
506 );
507 } else { # attribute or name
508 0 $sth = $dbh->prepare("REPLACE INTO $qualified_table VALUES (?,?,?)");
509 }
510  
511 0 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
512 0 chomp();
513 0 $sth->execute(split(/\t/));
514 }
515 }
516 0 $fh->close();
517 0 unlink $path;
518 }
519 0 $self->commit; # commit the transaction
520 0 delete $self->{bulk_update_in_progress};
521 0 delete $self->{filehandles};
522 }
523  
524 sub index_tables {
525 0 ▶ my $self = shift;
Subroutine index_tables: not called CC=1 SCAR=6.9
Pod undocumented
526 0 my @t = $self->SUPER::index_tables;
527 0 return (@t,$self->_feature_index_table);
528 }
529  
530 ▶sub _enable_keys { } # nullop
Subroutine _enable_keys: not called CC=1 SCAR=0.0
531 ▶sub _disable_keys { } # nullop
Subroutine _disable_keys: not called CC=1 SCAR=0.0
532  
533 sub _fetch_indexed_features_sql {
534 0 ▶ my $self = shift;
Subroutine _fetch_indexed_features_sql: not called CC=1 SCAR=6.9
535 0 my $location_table = $self->_qualify('feature_location');
536 0 my $feature_table = $self->_qualify('feature');
537 0 return <<END;
538 SELECT typeid,seqid,start-1,end
539 FROM $location_table as l,$feature_table as f
540 WHERE l.id=f.id AND f.\"indexed\"=1
541 ORDER BY typeid,seqid,start
542 END
543 }
544  
545 ###
546 # get primary sequence between start and end
547 #
548 sub _fetch_sequence {
549 0 ▶ my $self = shift;
Subroutine _fetch_sequence: not called CC=15 SCAR=54.8
550 0 my ($seqid,$start,$end) = @_;
551  
552 # backward compatibility to the old days when I liked reverse complementing
553 # dna by specifying $start > $end
554 0 my $reversed;
555 0 ▶ if (defined $start && defined $end && $start > $end) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $start and defined $end and $start > $end) 0 0 0
Condition0%0/6
defined $start and defined $end and $start > $end
!ll&&!rl&&r
000
defined $start and defined $end
!ll&&!rl&&r
000
MC/DC0%0/3
defined $start and defined $end and $start > $end
defined $start defined $end $start > $end
Truth table0%0/4
A and B and C
A= defined $startB= defined $endC= $start > $end
ABCresult
0XX0
10X0
1100
1111
556 0 $reversed++;
557 0 ($start,$end) = ($end,$start);
558 }
559 0 ▶ $start-- if defined $start;
Branch: 1 branch
BranchTrueFalseTotal
if defined $start 0 0 0
560 0 ▶ $end-- if defined $end;
Branch: 1 branch
BranchTrueFalseTotal
if defined $end 0 0 0
561  
562 0 ▶ my $offset1 = $self->_offset_boundary($seqid,$start || 'left');
Condition0%0/2
$start || 'left'
l!l
00
MC/DC0%0/1
$start || 'left'
$start
Truth table0%0/2
A
A= $start
Aresult
11
00
563 0 ▶ my $offset2 = $self->_offset_boundary($seqid,$end || 'right');
Condition0%0/2
$end || 'right'
l!l
00
MC/DC0%0/1
$end || 'right'
$end
Truth table0%0/2
A
A= $end
Aresult
11
00
564 0 my $sequence_table = $self->_sequence_table;
565 0 my $locationlist_table = $self->_locationlist_table;
566  
567 # CROSS JOIN gives a hint to the SQLite query optimizer -- mucho speedup!
568 0 my $sth = $self->_prepare(<<END);
569 SELECT sequence,offset
570 FROM $locationlist_table as ll CROSS JOIN $sequence_table as s
571 WHERE ll.id=s.id
572 AND ll.seqname= ?
573 AND offset >= ?
574 AND offset <= ?
575 ORDER BY offset
576 END
577  
578 0 my $seq = '';
579 0 ▶ $sth->execute($seqid,$offset1,$offset2) or $self->throw($sth->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute($seqid, $offset1, $offset2) or $self->throw($sth->errstr) 0 0 0
580  
581 0 ▶ while (my($frag,$offset) = $sth->fetchrow_array) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $sth->fetchrow_array) 0 0 0
582 0 ▶ substr($frag,0,$start-$offset) = '' if defined $start && $start > $offset;
Branch: 1 branch
BranchTrueFalseTotal
if defined $start and $start > $offset 0 0 0
Condition0%0/3
defined $start and $start > $offset
!ll&&!rl&&r
000
MC/DC0%0/2
defined $start and $start > $offset
defined $start $start > $offset
Truth table0%0/3
A and B
A= defined $startB= $start > $offset
ABresult
0X0
100
111
583 0 $seq .= $frag;
584 }
585 0 ▶ substr($seq,$end-$start+1) = '' if defined $end && $end-$start+1 < length($seq);
Branch: 1 branch
BranchTrueFalseTotal
if defined $end and $end - $start + 1 < length $seq 0 0 0
Condition0%0/3
defined $end and $end - $start + 1 < length $seq
!ll&&!rl&&r
000
MC/DC0%0/2
defined $end and $end - $start + 1 < length $seq
defined $end $end - $start + 1 < length $seq
Truth table0%0/3
A and B
A= defined $endB= $end - $start + 1 < length $seq
ABresult
0X0
100
111
586 0 ▶ if ($reversed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reversed) 0 0 0
587 0 $seq = reverse $seq;
588 0 $seq =~ tr/gatcGATC/ctagCTAG/;
589 }
590 0 $sth->finish;
591 0 $seq;
592 }
593  
594 sub _offset_boundary {
595 0 ▶ my $self = shift;
Subroutine _offset_boundary: not called CC=5 SCAR=34.0
596 0 my ($seqid,$position) = @_;
597  
598 0 my $sequence_table = $self->_sequence_table;
599 0 my $locationlist_table = $self->_locationlist_table;
600  
601 0 my $sql;
602 # use "CROSS JOIN" to give a hint to the SQLite query optimizer.
603 0 ▶ $sql = $position eq 'left' ? "SELECT min(offset) FROM $locationlist_table as ll CROSS JOIN $sequence_table as s ON ll.id=s.id WHERE ll.seqname=?"
Branch: 2 branches
BranchTrueFalseTotal
$position eq 'left' ? : 0 0 0
$position eq 'right' ? : 0 0 0
604 :$position eq 'right' ? "SELECT max(offset) FROM $locationlist_table as ll CROSS JOIN $sequence_table as s ON ll.id=s.id WHERE ll.seqname=?"
605 :"SELECT max(offset) FROM $locationlist_table as ll CROSS JOIN $sequence_table as s ON ll.id=s.id WHERE ll.seqname=? AND offset<=?";
606 0 my $sth = $self->_prepare($sql);
607 0 ▶ my @args = $position =~ /^-?\d+$/ ? ($seqid,$position) : ($seqid);
Branch: 1 branch
BranchTrueFalseTotal
$position =~ /^-?\d+$/ ? : 0 0 0
608 0 ▶ $sth->execute(@args) or $self->throw($sth->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute(@args) or $self->throw($sth->errstr) 0 0 0
609 0 my $boundary = $sth->fetchall_arrayref->[0][0];
610 0 $sth->finish;
611 0 return $boundary;
612 }
613  
614 ###
615 # Efficiently fetch a series of IDs from the database
616 # Can pass an array or an array ref
617 #
618 sub _fetch_many {
619 0 ▶ my $self = shift;
Subroutine _fetch_many: not called CC=5 SCAR=34.0
620 0 ▶ @_ or $self->throw('usage: fetch_many($id1,$id2,$id3...)');
Branch: 1 branch
BranchTrueFalseTotal
@_ or $self->throw("usage: fetch_many(\$id1,\$id2,\$id3...)") 0 0 0
621 0 ▶ my $ids = join ',',map {ref($_) ? @$_ : $_} @_ or return;
Branch: 2 branches
BranchTrueFalseTotal
my $ids = join(",", map({ref $_ ? @$_ : $_;} @_)) or return 0 0 0
ref $_ ? : 0 0 0
622 0 my $features = $self->_feature_table;
623  
624 0 my $sth = $self->_prepare(<<END);
625 SELECT id,object FROM $features WHERE id IN ($ids)
626 END
627 0 ▶ $sth->execute() or $self->throw($sth->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or $self->throw($sth->errstr) 0 0 0
628 0 return $self->_sth2objs($sth);
629 }
630  
631 sub _features {
632 0 ▶ my $self = shift;
Subroutine _features: not called CC=38 SCAR=73.0
633 0 my ($seq_id,$start,$end,$strand,
634 $name,$class,$allow_aliases,
635 $types,
636 $attributes,
637 $range_type,
638 $fromtable,
639 $iterator,
640 $sources
641 ) = rearrange([['SEQID','SEQ_ID','REF'],'START',['STOP','END'],'STRAND',
642 'NAME','CLASS','ALIASES',
643 ['TYPES','TYPE','PRIMARY_TAG'],
644 ['ATTRIBUTES','ATTRIBUTE'],
645 'RANGE_TYPE',
646 'FROM_TABLE',
647 'ITERATOR',
648 ['SOURCE','SOURCES']
649 ],@_);
650  
651 0 my (@from,@where,@args,@group);
652 0 ▶ $range_type ||= 'overlaps';
Condition0%0/2
$range_type ||= "overlaps"
l!l
00
MC/DC0%0/1
$range_type ||= "overlaps"
$range_type
Truth table0%0/2
A
A= $range_type
Aresult
11
00
653  
654 0 my $feature_table = $self->_feature_table;
655 0 @from = "$feature_table as f";
656  
657 0 ▶ if (defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) 0 0 0
658 # hacky backward compatibility workaround
659 0 ▶ undef $class if $class && $class eq 'Sequence';
Branch: 1 branch
BranchTrueFalseTotal
if $class and $class eq "Sequence" 0 0 0
Condition0%0/3
$class and $class eq "Sequence"
!ll&&!rl&&r
000
MC/DC0%0/2
$class and $class eq "Sequence"
$class $class eq "Sequence"
Truth table0%0/3
A and B
A= $classB= $class eq "Sequence"
ABresult
0X0
100
111
660 0 ▶ $name = "$class:$name" if defined $class && length $class > 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $class and length $class > 0 0 0 0
Condition0%0/3
defined $class and length $class > 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $class and length $class > 0
defined $class length $class > 0
Truth table0%0/3
A and B
A= defined $classB= length $class > 0
ABresult
0X0
100
111
661 # last argument is the join field
662 0 my ($from,$where,$group,@a) = $self->_name_sql($name,$allow_aliases,'f.id');
663 0 ▶ push @from,$from if $from;
Branch: 1 branch
BranchTrueFalseTotal
if $from 0 0 0
664 0 ▶ push @where,$where if $where;
Branch: 1 branch
BranchTrueFalseTotal
if $where 0 0 0
665 0 ▶ push @group,$group if $group;
Branch: 1 branch
BranchTrueFalseTotal
if $group 0 0 0
666 0 push @args,@a;
667 }
668  
669 0 ▶ if (defined $seq_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $seq_id) 0 0 0
670 # last argument is the name of the features table
671 0 my ($from,$where,$group,@a) = $self->_location_sql($seq_id,$start,$end,$range_type,$strand,'f');
672 0 ▶ push @from,$from if $from;
Branch: 1 branch
BranchTrueFalseTotal
if $from 0 0 0
673 0 ▶ push @where,$where if $where;
Branch: 1 branch
BranchTrueFalseTotal
if $where 0 0 0
674 0 ▶ push @group,$group if $group;
Branch: 1 branch
BranchTrueFalseTotal
if $group 0 0 0
675 0 push @args,@a;
676 }
677  
678 0 ▶ if (defined($sources)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sources) 0 0 0
679 0 ▶ my @sources = ref($sources) eq 'ARRAY' ? @{$sources} : ($sources);
Branch: 1 branch
BranchTrueFalseTotal
ref $sources eq 'ARRAY' ? : 0 0 0
680 0 ▶ if (defined($types)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $types) { } 0 0 0
681 0 ▶ my @types = ref($types) eq 'ARRAY' ? @{$types} : ($types);
Branch: 1 branch
BranchTrueFalseTotal
ref $types eq 'ARRAY' ? : 0 0 0
682 0 my @final_types;
683 0 foreach my $type (@types) {
684 # *** not sure what to do if user supplies both -source and -type
685 # where the type includes a source!
686 0 ▶ if ($type =~ /:/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /:/) { } 0 0 0
687 0 push(@final_types, $type);
688 }
689 else {
690 0 foreach my $source (@sources) {
691 0 push(@final_types, $type.':'.$source);
692 }
693 }
694 }
695 0 $types = \@final_types;
696 }
697 else {
698 0 $types = [map { ':'.$_ } @sources];
699 }
700 }
701 0 ▶ if (defined($types)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $types) 0 0 0
702 # last argument is the name of the features table
703 0 my ($from,$where,$group,@a) = $self->_types_sql($types,'f');
704 0 ▶ push @from,$from if $from;
Branch: 1 branch
BranchTrueFalseTotal
if $from 0 0 0
705 0 ▶ push @where,$where if $where;
Branch: 1 branch
BranchTrueFalseTotal
if $where 0 0 0
706 0 ▶ push @group,$group if $group;
Branch: 1 branch
BranchTrueFalseTotal
if $group 0 0 0
707 0 push @args,@a;
708 }
709  
710 0 ▶ if (defined $attributes) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $attributes) 0 0 0
711 # last argument is the join field
712 0 my ($from,$where,$group,@a) = $self->_attributes_sql($attributes,'f.id');
713 0 ▶ push @from,$from if $from;
Branch: 1 branch
BranchTrueFalseTotal
if $from 0 0 0
714 0 ▶ push @where,$where if $where;
Branch: 1 branch
BranchTrueFalseTotal
if $where 0 0 0
715 0 ▶ push @group,$group if $group;
Branch: 1 branch
BranchTrueFalseTotal
if $group 0 0 0
716 0 push @args,@a;
717 }
718  
719 0 ▶ if (defined $fromtable) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fromtable) 0 0 0
720 # last argument is the join field
721 0 my ($from,$where,$group,@a) = $self->_from_table_sql($fromtable,'f.id');
722 0 ▶ push @from,$from if $from;
Branch: 1 branch
BranchTrueFalseTotal
if $from 0 0 0
723 0 ▶ push @where,$where if $where;
Branch: 1 branch
BranchTrueFalseTotal
if $where 0 0 0
724 0 ▶ push @group,$group if $group;
Branch: 1 branch
BranchTrueFalseTotal
if $group 0 0 0
725 0 push @args,@a;
726 }
727  
728 # if no other criteria are specified, then
729 # only fetch indexed (i.e. top level objects)
730 0 ▶ @where = '"indexed"=1' unless @where;
Branch: 1 branch
BranchTrueFalseTotal
unless @where 0 0 0
731  
732 0 my $from = join ', ',@from;
733 0 my $where = join ' AND ',map {"($_)"} @where;
734 0 my $group = join ', ',@group;
735 0 ▶ $group = "GROUP BY $group" if @group;
Branch: 1 branch
BranchTrueFalseTotal
if @group 0 0 0
736  
737 0 my $query = <<END;
738 SELECT f.id,f.object
739 FROM $from
740 WHERE $where
741 $group
742 END
743  
744 0 ▶ $self->_print_query($query,@args) if DEBUG || $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
745  
746 0 my $sth = $self->_prepare($query);
747 0 ▶ $sth->execute(@args) or $self->throw($sth->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute(@args) or $self->throw($sth->errstr) 0 0 0
748 0 ▶ return $iterator ? Bio::DB::SeqFeature::Store::DBI::Iterator->new($sth,$self) : $self->_sth2objs($sth);
Branch: 1 branch
BranchTrueFalseTotal
$iterator ? : 0 0 0
749 }
750  
751 sub _make_attribute_group {
752 0 ▶ my $self = shift;
Subroutine _make_attribute_group: not called CC=2 SCAR=17.9
753 0 my ($table_name,$attributes) = @_;
754 0 ▶ my $key_count = keys %$attributes or return;
Branch: 1 branch
BranchTrueFalseTotal
my $key_count = keys %$attributes or return 0 0 0
755 0 my $count = $key_count-1;
756 0 return "f.id HAVING count(f.id)>$count";
757 }
758  
759 sub _location_sql {
760 0 ▶ my $self = shift;
Subroutine _location_sql: not called CC=11 SCAR=48.8
761 0 my ($seq_id,$start,$end,$range_type,$strand,$location) = @_;
762  
763 # the additional join on the location_list table badly impacts performance
764 # so we build a copy of the table in memory
765 0 ▶ my $seqid = $self->_locationid_nocreate($seq_id) || 0; # zero is an invalid primary ID, so will return empty
Condition0%0/2
$self->_locationid_nocreate($seq_id) || 0
l!l
00
MC/DC0%0/1
$self->_locationid_nocreate($seq_id) || 0
$self->_locationid_nocreate($seq_id)
Truth table0%0/2
A
A= $self->_locationid_nocreate($seq_id)
Aresult
11
00
766  
767 0 my $feature_index = $self->_feature_index_table;
768 0 my $from = "$feature_index as fi";
769  
770 0 my ($bin_where,@bin_args);
771 0 ▶ if (defined $start && defined $end && !$self->_has_spatial_index) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $start and defined $end and not $self->_has_spatial_index) 0 0 0
Condition0%0/6
defined $start and defined $end and not $self->_has_spatial_index
!ll&&!rl&&r
000
defined $start and defined $end
!ll&&!rl&&r
000
MC/DC0%0/3
defined $start and defined $end and not $self->_has_spatial_index
defined $start defined $end not $self->_has_spatial_index
Truth table0%0/4
A and B and not(C)
A= defined $startB= defined $endC= not $self->_has_spatial_index
ABCresult
0XX0
10X0
1100
1111
772 0 my @bins = $self->search_bins($start,$end);
773 0 $bin_where = ' AND bin in ('.join(',',@bins).')';
774 }
775  
776 0 ▶ $start = MIN_INT unless defined $start;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $start 0 0 0
777 0 ▶ $end = MAX_INT unless defined $end;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $end 0 0 0
778  
779 0 my ($range,@range_args);
780 0 ▶ if ($range_type eq 'overlaps') {
Branch: 3 branches
BranchTrueFalseTotal
if ($range_type eq 'overlaps') { } 0 0 0
elsif ($range_type eq 'contains') { } 0 0 0
elsif ($range_type eq 'contained_in') { } 0 0 0
781 0 $range = "fi.end>=? AND fi.start<=?".$bin_where;
782 0 @range_args = ($start,$end,@bin_args);
783 } elsif ($range_type eq 'contains') {
784 0 $range = "fi.start>=? AND fi.end<=?".$bin_where;
785 0 @range_args = ($start,$end,@bin_args);
786 } elsif ($range_type eq 'contained_in') {
787 0 $range = "fi.start<=? AND fi.end>=?";
788 0 @range_args = ($start,$end);
789 } else {
790 0 $self->throw("range_type must be one of 'overlaps', 'contains' or 'contained_in'");
791 }
792  
793 0 ▶ if (defined $strand) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $strand) 0 0 0
794 0 $range .= " AND strand=?";
795 0 push @range_args,$strand;
796 }
797  
798 0 my $where = <<END;
799 fi.seqid=?
800 AND $location.id=fi.id
801 AND $range
802 END
803 ;
804 0 my $group = '';
805  
806 0 my @args = ($seqid,@range_args);
807 0 return ($from,$where,$group,@args);
808 }
809  
810 sub _feature_index_table {
811 0 ▶ my $self = shift;
Subroutine _feature_index_table: not called CC=2 SCAR=17.9
812 0 ▶ return $self->_has_spatial_index ? $self->_qualify('feature_index')
Branch: 1 branch
BranchTrueFalseTotal
$self->_has_spatial_index ? : 0 0 0
813 : $self->_qualify('feature_location') }
814  
815 # Do a case-insensitive search a la the PostgreSQL adaptor
816 sub _name_sql {
817 0 ▶ my $self = shift;
Subroutine _name_sql: not called CC=2 SCAR=17.9
818 0 my ($name,$allow_aliases,$join) = @_;
819 0 my $name_table = $self->_name_table;
820  
821 0 my $from = "$name_table as n";
822 0 my ($match,$string) = $self->_match_sql($name);
823  
824 0 my $where = "n.id=$join AND n.name $match COLLATE NOCASE";
825 0 ▶ $where .= " AND n.display_name>0" unless $allow_aliases;
Branch: 1 branch
BranchTrueFalseTotal
unless $allow_aliases 0 0 0
826 0 return ($from,$where,'',$string);
827 }
828  
829 sub _search_attributes {
830 0 ▶ my $self = shift;
Subroutine _search_attributes: not called CC=6 SCAR=37.4
831 0 my ($search_string,$attribute_names,$limit) = @_;
832 0 my @words = map {quotemeta($_)} split /\s+/,$search_string;
833  
834 0 my $name_table = $self->_name_table;
835 0 my $attribute_table = $self->_attribute_table;
836 0 my $attributelist_table = $self->_attributelist_table;
837 0 my $type_table = $self->_type_table;
838 0 my $typelist_table = $self->_typelist_table;
839 0 my $has_fts = $self->_has_fts;
840  
841 0 my @tags = @$attribute_names;
842 0 my $tag_sql = join ' OR ',("al.tag=?") x @tags;
843  
844 0 my $perl_regexp = join '|',@words;
845  
846 0 my $sql_regexp;
847 my @wild_card_words;
848 0 ▶ if ($has_fts) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_fts) { } 0 0 0
849 0 $sql_regexp = "a.attribute_value MATCH ?";
850 0 @wild_card_words = join(' OR ', @words);
851 } else {
852 0 $sql_regexp = join ' OR ',("a.attribute_value LIKE ?") x @words;
853 0 @wild_card_words = map { "%$_%" } @words;
854 }
855 # CROSS JOIN hinders performance with FTS attribute table for DBD::SQLite 1.42
856 0 my $sql = <<END;
857 SELECT name,attribute_value,tl.tag,n.id
858 FROM $attributelist_table AS al
859 JOIN $attribute_table AS a ON al.id = a.attribute_id
860 JOIN $name_table AS n ON n.id = a.id
861 JOIN $type_table AS t ON t.id = n.id
862 JOIN $typelist_table AS tl ON tl.id = t.typeid
863 WHERE ($tag_sql)
864 AND ($sql_regexp)
865 AND n.display_name=1
866 END
867 0 ▶ $sql .= "LIMIT $limit" if defined $limit;
Branch: 1 branch
BranchTrueFalseTotal
if defined $limit 0 0 0
868 0 ▶ $self->_print_query($sql,@tags,@wild_card_words) if DEBUG || $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
869 0 my $sth = $self->_prepare($sql);
870 0 ▶ $sth->execute(@tags, @wild_card_words) or $self->throw($sth->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute(@tags, @wild_card_words) or $self->throw($sth->errstr) 0 0 0
871  
872 0 my @results;
873 0 ▶ while (my($name,$value,$type,$id) = $sth->fetchrow_array) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $sth->fetchrow_array) 0 0 0
874 0 my (@hits) = $value =~ /$perl_regexp/ig;
875 0 my @words_in_row = split /\b/,$value;
876 0 my $score = int(@hits*100/@words/@words_in_row);
877 0 push @results,[$name,$value,$score,$type,$id];
878 }
879 0 $sth->finish;
880 0 @results = sort {$b->[2]<=>$a->[2]} @results;
881 0 return @results;
882 }
883  
884 sub _match_sql {
885 0 ▶ my $self = shift;
Subroutine _match_sql: not called CC=2 SCAR=17.9
886 0 my $name = shift;
887  
888 0 my ($match,$string);
889 0 ▶ if ($name =~ /(?:^|[^\\])[*?]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /(?:^|[^\\])[*?]/) { } 0 0 0
890 0 $name =~ s/(^|[^\\])([%_])/$1\\$2/g;
891 0 $name =~ s/(^|[^\\])\*/$1%/g;
892 0 $name =~ s/(^|[^\\])\?/$1_/g;
893 0 $match = "LIKE ?";
894 0 $string = $name;
895 } else {
896 0 $match = "= ? COLLATE NOCASE";
897 0 $string = $name;
898 }
899 0 return ($match,$string);
900 }
901  
902 sub _attributes_sql {
903 0 ▶ my $self = shift;
Subroutine _attributes_sql: not called CC=3 SCAR=24.8
904 0 my ($attributes,$join) = @_;
905  
906 0 my ($wf,@bind_args) = $self->_make_attribute_where('a','al',$attributes);
907 0 my ($group_by,@group_args)= $self->_make_attribute_group('a',$attributes);
908  
909 0 my $attribute_table = $self->_attribute_table;
910 0 my $attributelist_table = $self->_attributelist_table;
911  
912 0 ▶ my $from = "$attribute_table AS a" . ($self->_has_fts
Branch: 1 branch
BranchTrueFalseTotal
$self->_has_fts ? : 0 0 0
913 ? ''
914 : " INDEXED BY index_attribute_id") . ", $attributelist_table AS al";
915 0 ▶ my $a_al_join = $self->_has_fts ? 'a.attribute_id MATCH al.id' : 'a.attribute_id=al.id';
Branch: 1 branch
BranchTrueFalseTotal
$self->_has_fts ? : 0 0 0
916  
917 0 my $where = <<END;
918 a.id=$join
919 AND $a_al_join
920 AND ($wf)
921 END
922  
923 0 my $group = $group_by;
924  
925 0 my @args = (@bind_args,@group_args);
926 0 return ($from,$where,$group,@args);
927 }
928  
929 # overridden because of case-sensitivity of matches
930 sub _types_sql {
931 0 ▶ my $self = shift;
Subroutine _types_sql: not called CC=6 SCAR=37.4
932 0 my ($types,$type_table) = @_;
933 0 my ($primary_tag,$source_tag);
934  
935 0 ▶ my @types = ref $types eq 'ARRAY' ? @$types : $types;
Branch: 1 branch
BranchTrueFalseTotal
ref $types eq 'ARRAY' ? : 0 0 0
936  
937 0 my $typelist = $self->_typelist_table;
938 0 my $from = "$typelist AS tl";
939  
940 0 my (@matches,@args);
941  
942 0 for my $type (@types) {
943  
944 0 ▶ if (ref $type && $type->isa('Bio::DB::GFF::Typename')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $type and $type->isa('Bio::DB::GFF::Typename')) { } 0 0 0
Condition0%0/3
ref $type and $type->isa("Bio::DB::GFF::Typename")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $type and $type->isa("Bio::DB::GFF::Typename")
ref $type $type->isa("Bio::DB::GFF::Typename")
Truth table0%0/3
A and B
A= ref $typeB= $type->isa("Bio::DB::GFF::Typename")
ABresult
0X0
100
111
945 0 $primary_tag = $type->method;
946 0 $source_tag = $type->source;
947 } else {
948 0 ($primary_tag,$source_tag) = split ':',$type,2;
949 }
950  
951 0 ▶ if (length $source_tag) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $source_tag) { } 0 0 0
952 0 push @matches,"tl.tag=? COLLATE NOCASE";
953 0 push @args,"$primary_tag:$source_tag";
954 } else {
955 0 push @matches,"tl.tag LIKE ?";
956 0 push @args,"$primary_tag:%";
957 }
958 }
959 0 my $matches = join ' OR ',@matches;
960  
961 0 my $where = <<END;
962 tl.id=$type_table.typeid
963 AND ($matches)
964 END
965  
966 0 return ($from,$where,'',@args);
967 }
968  
969 sub optimize {
970 0 ▶ my $self = shift;
Subroutine optimize: not called CC=2 SCAR=17.9
Pod undocumented
971 0 $self->dbh->do("ANALYZE $_") foreach $self->index_tables;
972 }
973  
974 ###
975 # Replace Bio::SeqFeatureI into database.
976 #
977 sub replace {
978 0 ▶ my $self = shift;
Subroutine replace: not called CC=9 SCAR=45.0
Pod undocumented
979 0 my $object = shift;
980 0 ▶ my $index_flag = shift || undef;
Condition0%0/2
shift() || undef
l!l
00
MC/DC0%0/1
shift() || undef
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
981  
982 # ?? shouldn't need to do this
983 # $self->_load_class($object);
984 0 my $id = $object->primary_id;
985 0 my $features = $self->_feature_table;
986  
987 0 my $sth = $self->_prepare(<<END);
988 REPLACE INTO $features (id,object,"indexed",strand,typeid) VALUES (?,?,?,?,?)
989 END
990  
991 0 ▶ my ($seqid,$start,$end,$strand,$bin) = $index_flag ? $self->_get_location_and_bin($object) : (undef)x6;
Branch: 1 branch
BranchTrueFalseTotal
$index_flag ? : 0 0 0
992  
993 0 my $primary_tag = $object->primary_tag;
994 0 ▶ my $source_tag = $object->source_tag || '';
Condition0%0/2
$object->source_tag || ''
l!l
00
MC/DC0%0/1
$object->source_tag || ''
$object->source_tag
Truth table0%0/2
A
A= $object->source_tag
Aresult
11
00
995 0 $primary_tag .= ":$source_tag";
996 0 my $typeid = $self->_typeid($primary_tag,1);
997  
998 0 ▶ my $frozen = $self->no_blobs() ? 0 : $self->freeze($object);
Branch: 1 branch
BranchTrueFalseTotal
$self->no_blobs ? : 0 0 0
999  
1000 0 $sth->bind_param(1, $id);
1001 0 $sth->bind_param(2, $frozen, {TYPE => SQL_BLOB});
1002 0 ▶ $sth->bind_param(3, $index_flag||0);
Condition0%0/2
$index_flag || 0
l!l
00
MC/DC0%0/1
$index_flag || 0
$index_flag
Truth table0%0/2
A
A= $index_flag
Aresult
11
00
1003 0 $sth->bind_param(4, $strand);
1004 0 $sth->bind_param(5, $typeid);
1005  
1006 0 ▶ $sth->execute() or $self->throw($sth->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or $self->throw($sth->errstr) 0 0 0
1007  
1008 0 my $dbh = $self->dbh;
1009 0 ▶ $object->primary_id($dbh->func('last_insert_rowid')) unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 0 0
1010  
1011 0 ▶ $self->flag_for_indexing($dbh->func('last_insert_rowid')) if $self->{bulk_update_in_progress};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"bulk_update_in_progress"} 0 0 0
1012 }
1013  
1014 # doesn't work with this schema, since we have to update name and attribute
1015 # tables which need object ids, which we can only know by replacing feats in
1016 # the feature table one by one
1017 sub bulk_replace {
1018 0 ▶ my $self = shift;
Subroutine bulk_replace: not called CC=8 SCAR=42.8
Pod undocumented
1019 0 ▶ my $index_flag = shift || undef;
Condition0%0/2
shift() || undef
l!l
00
MC/DC0%0/1
shift() || undef
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
1020 0 my @objects = @_;
1021  
1022 0 my $features = $self->_feature_table;
1023  
1024 0 my @insert_values;
1025 0 foreach my $object (@objects) {
1026 0 my $id = $object->primary_id;
1027 0 ▶ my (undef,undef,undef,$strand) = $index_flag ? $self->_get_location_and_bin($object) : (undef)x4;
Branch: 1 branch
BranchTrueFalseTotal
$index_flag ? : 0 0 0
1028 0 my $primary_tag = $object->primary_tag;
1029 0 ▶ my $source_tag = $object->source_tag || '';
Condition0%0/2
$object->source_tag || ''
l!l
00
MC/DC0%0/1
$object->source_tag || ''
$object->source_tag
Truth table0%0/2
A
A= $object->source_tag
Aresult
11
00
1030 0 $primary_tag .= ":$source_tag";
1031 0 my $typeid = $self->_typeid($primary_tag,1);
1032  
1033 0 ▶ push(@insert_values, ($id,0,$index_flag||0,$strand,$typeid));
Condition0%0/2
$index_flag || 0
l!l
00
MC/DC0%0/1
$index_flag || 0
$index_flag
Truth table0%0/2
A
A= $index_flag
Aresult
11
00
1034 }
1035  
1036 0 my @value_blocks;
1037 0 for (1..@objects) {
1038 0 push(@value_blocks, '(?,?,?,?,?)');
1039 }
1040 0 my $value_blocks = join(',', @value_blocks);
1041 0 my $sql = qq{REPLACE INTO $features (id,object,"indexed",strand,typeid) VALUES $value_blocks};
1042  
1043 0 my $sth = $self->_prepare($sql);
1044 0 ▶ $sth->execute(@insert_values) or $self->throw($sth->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute(@insert_values) or $self->throw($sth->errstr) 0 0 0
1045 }
1046  
1047 sub _get_location_and_bin {
1048 0 ▶ my $self = shift;
Subroutine _get_location_and_bin: not called CC=2 SCAR=17.9
1049 0 my $obj = shift;
1050 0 ▶ my $seqid = $self->_locationid($obj->seq_id||'');
Condition0%0/2
$obj->seq_id || ''
l!l
00
MC/DC0%0/1
$obj->seq_id || ''
$obj->seq_id
Truth table0%0/2
A
A= $obj->seq_id
Aresult
11
00
1051 0 my $start = $obj->start;
1052 0 my $end = $obj->end;
1053 0 my $strand = $obj->strand;
1054 0 return ($seqid,$start,$end,$strand,$self->calculate_bin($start,$end));
1055 }
1056  
1057  
1058  
1059 ###
1060 # Insert one Bio::SeqFeatureI into database. primary_id must be undef
1061 #
1062 sub insert {
1063 0 ▶ my $self = shift;
Subroutine insert: not called CC=5 SCAR=34.0
Pod undocumented
1064 0 my $object = shift;
1065 0 ▶ my $index_flag = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
1066  
1067 0 $self->_load_class($object);
1068 0 ▶ defined $object->primary_id and $self->throw("$object already has a primary id");
Branch: 1 branch
BranchTrueFalseTotal
defined $object->primary_id and $self->throw("$object already has a primary id") 0 0 0
1069  
1070 0 my $features = $self->_feature_table;
1071 0 my $sth = $self->_prepare(<<END);
1072 INSERT INTO $features (id,object,"indexed") VALUES (?,?,?)
1073 END
1074 0 ▶ $sth->execute(undef,$self->freeze($object),$index_flag) or $self->throw($sth->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute(undef, $self->freeze($object), $index_flag) or $self->throw($sth->errstr) 0 0 0
1075 0 my $dbh = $self->dbh;
1076 0 $object->primary_id($dbh->func('last_insert_rowid'));
1077 0 ▶ $self->flag_for_indexing($dbh->func('last_insert_rowid')) if $self->{bulk_update_in_progress};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"bulk_update_in_progress"} 0 0 0
1078 }
1079  
1080 =head2 toplevel_types
1081  
1082 Title : toplevel_types
1083 Usage : @type_list = $db->toplevel_types
1084 Function: Get the toplevel types in the database
1085 Returns : array of Bio::DB::GFF::Typename objects
1086 Args : none
1087 Status : public
1088  
1089 This is similar to types() but only returns the types of
1090 INDEXED (toplevel) features.
1091  
1092 =cut
1093  
1094 sub toplevel_types {
1095 0 ▶ my $self = shift;
Subroutine toplevel_types: not called CC=5 SCAR=34.0
Pod documented
1096 0 ▶ eval "require Bio::DB::GFF::Typename"
Branch: 1 branch
BranchTrueFalseTotal
unless "Bio::DB::GFF::Typename"->can("new") 0 0 0
1097 unless Bio::DB::GFF::Typename->can('new');
1098 0 my $typelist_table = $self->_typelist_table;
1099 0 my $feature_table = $self->_feature_table;
1100 0 my $sql = <<END;
1101 SELECT distinct(tag) from $typelist_table as tl,$feature_table as f
1102 WHERE tl.id=f.typeid
1103 AND f."indexed"=1
1104 END
1105 ;
1106 0 ▶ $self->_print_query($sql) if DEBUG || $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
1107 0 my $sth = $self->_prepare($sql);
1108 0 ▶ $sth->execute() or $self->throw($sth->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or $self->throw($sth->errstr) 0 0 0
1109  
1110 0 my @results;
1111 0 ▶ while (my($tag) = $sth->fetchrow_array) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($tag) = $sth->fetchrow_array) 0 0 0
1112 0 push @results,Bio::DB::GFF::Typename->new($tag);
1113 }
1114 0 $sth->finish;
1115 0 return @results;
1116 }
1117  
1118 sub _genericid {
1119 0 ▶ my $self = shift;
Subroutine _genericid: not called CC=5 SCAR=34.0
1120 0 my ($table,$namefield,$name,$add_if_missing) = @_;
1121 0 my $qualified_table = $self->_qualify($table);
1122 0 my $sth = $self->_prepare(<<END);
1123 SELECT id FROM $qualified_table WHERE $namefield=? COLLATE NOCASE
1124 END
1125 0 ▶ $sth->execute($name) or die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute($name) or die $sth->errstr 0 0 0
1126 0 my ($id) = $sth->fetchrow_array;
1127 0 $sth->finish;
1128 0 ▶ return $id if defined $id;
Branch: 1 branch
BranchTrueFalseTotal
if defined $id 0 0 0
1129 0 ▶ return unless $add_if_missing;
Branch: 1 branch
BranchTrueFalseTotal
unless $add_if_missing 0 0 0
1130  
1131 0 $sth = $self->_prepare(<<END);
1132 INSERT INTO $qualified_table ($namefield) VALUES (?)
1133 END
1134 0 ▶ $sth->execute($name) or die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute($name) or die $sth->errstr 0 0 0
1135 0 my $dbh = $self->dbh;
1136 0 return $dbh->func('last_insert_rowid');
1137 }
1138 ###
1139 # special-purpose store for bulk loading - write to a file rather than to the db
1140 #
1141 sub _dump_store {
1142 0 ▶ my $self = shift;
Subroutine _dump_store: not called CC=8 SCAR=42.8
1143 0 my $indexed = shift;
1144  
1145 0 my $count = 0;
1146 0 my $store_fh = $self->dump_filehandle('feature');
1147 0 my $dbh = $self->dbh;
1148  
1149 0 my $autoindex = $self->autoindex;
1150  
1151 0 for my $obj (@_) {
1152 0 my $id = $self->next_id;
1153 0 ▶ my ($seqid,$start,$end,$strand) = $indexed ? $self->_get_location_and_bin($obj) : (undef)x4;
Branch: 1 branch
BranchTrueFalseTotal
$indexed ? : 0 0 0
1154 0 my $primary_tag = $obj->primary_tag;
1155 0 ▶ my $source_tag = $obj->source_tag || '';
Condition0%0/2
$obj->source_tag || ''
l!l
00
MC/DC0%0/1
$obj->source_tag || ''
$obj->source_tag
Truth table0%0/2
A
A= $obj->source_tag
Aresult
11
00
1156 0 $primary_tag .= ":$source_tag";
1157 0 my $typeid = $self->_typeid($primary_tag,1);
1158  
1159 # Encode BLOB in hex so we can more easily import it into SQLite
1160 0 print $store_fh
1161 join("\t",$id,$typeid,$strand,$indexed,
1162 unpack('H*', $self->freeze($obj))),"\n";
1163 0 $obj->primary_id($id);
1164 0 ▶ $self->_update_indexes($obj) if $indexed && $autoindex;
Branch: 1 branch
BranchTrueFalseTotal
if $indexed and $autoindex 0 0 0
Condition0%0/3
$indexed and $autoindex
!ll&&!rl&&r
000
MC/DC0%0/2
$indexed and $autoindex
$indexed $autoindex
Truth table0%0/3
A and B
A= $indexedB= $autoindex
ABresult
0X0
100
111
1165 0 $count++;
1166 }
1167  
1168 # remember whether we are have ever stored a non-indexed feature
1169 0 ▶ unless ($indexed or $self->{indexed_flag}++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($indexed or $self->{"indexed_flag"}++) 0 0 0
Condition0%0/3
$indexed or $self->{"indexed_flag"}++
l!l&&r!l&&!r
000
MC/DC0%0/2
$indexed or $self->{"indexed_flag"}++
$indexed $self->{"indexed_flag"}++
Truth table0%0/3
A or B
A= $indexedB= $self->{"indexed_flag"}++
ABresult
1X1
011
000
1170 0 $self->subfeatures_are_indexed(0);
1171 }
1172 0 $count;
1173 }
1174  
1175 sub _dump_update_name_index {
1176 0 ▶ my $self = shift;
Subroutine _dump_update_name_index: not called CC=3 SCAR=24.8
1177 0 my ($obj,$id) = @_;
1178 0 my $fh = $self->dump_filehandle('name');
1179 0 my $dbh = $self->dbh;
1180 0 my ($names,$aliases) = $self->feature_names($obj);
1181 # unlike DBI::mysql, don't quote, as quotes will be quoted when loaded
1182 0 print $fh join("\t",$id,$_,1),"\n" foreach @$names;
1183 0 print $fh join("\t",$id,$_,0),"\n" foreach @$aliases;
1184 }
1185  
1186 sub _update_name_index {
1187 0 ▶ my $self = shift;
Subroutine _update_name_index: not called CC=5 SCAR=34.0
1188 0 my ($obj,$id) = @_;
1189 0 my $name = $self->_name_table;
1190 0 my $primary_id = $obj->primary_id;
1191  
1192 0 $self->_delete_index($name,$id);
1193 0 my ($names,$aliases) = $self->feature_names($obj);
1194  
1195 0 my $sth = $self->_prepare("INSERT INTO $name (id,name,display_name) VALUES (?,?,?)");
1196  
1197 0 ▶ $sth->execute($id,$_,1) or $self->throw($sth->errstr) foreach @$names;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute($id, $_, 1) or $self->throw($sth->errstr) 0 0 0
1198 0 ▶ $sth->execute($id,$_,0) or $self->throw($sth->errstr) foreach @$aliases;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute($id, $_, 0) or $self->throw($sth->errstr) 0 0 0
1199 0 $sth->finish;
1200 }
1201  
1202  
1203 sub _dump_update_attribute_index {
1204 0 ▶ my $self = shift;
Subroutine _dump_update_attribute_index: not called CC=3 SCAR=24.8
1205 0 my ($obj,$id) = @_;
1206 0 my $fh = $self->dump_filehandle('attribute');
1207 0 my $dbh = $self->dbh;
1208 0 for my $tag ($obj->all_tags) {
1209 0 my $tagid = $self->_attributeid($tag);
1210 0 for my $value ($obj->each_tag_value($tag)) {
1211 # unlike DBI::mysql, don't quote, as quotes will be quoted when loaded
1212 0 print $fh join("\t",$id,$tagid,$value),"\n";
1213 }
1214 }
1215 }
1216  
1217 sub _update_indexes {
1218 0 ▶ my $self = shift;
Subroutine _update_indexes: not called CC=3 SCAR=24.8
1219 0 my $obj = shift;
1220 0 ▶ defined (my $id = $obj->primary_id) or return;
Branch: 1 branch
BranchTrueFalseTotal
defined(my $id = $obj->primary_id) or return 0 0 0
1221 0 $self->SUPER::_update_indexes($obj);
1222  
1223 0 ▶ if ($self->{bulk_update_in_progress}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'bulk_update_in_progress'}) { } 0 0 0
1224 0 $self->_dump_update_location_index($obj,$id);
1225 } else {
1226 0 $self->_update_location_index($obj,$id);
1227 }
1228 }
1229  
1230 sub _update_location_index {
1231 0 ▶ my $self = shift;
Subroutine _update_location_index: not called CC=2 SCAR=17.9
1232 0 my ($obj,$id) = @_;
1233 0 my ($seqid,$start,$end,$strand,$bin) = $self->_get_location_and_bin($obj);
1234  
1235 0 my $table = $self->_feature_index_table;
1236 0 $self->_delete_index($table,$id);
1237  
1238 0 my ($sql,@args);
1239  
1240 0 ▶ if ($self->_has_spatial_index) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_spatial_index) { } 0 0 0
1241 0 $sql = "INSERT INTO $table (id,seqid,bin,start,end) values (?,?,?,?,?)";
1242 0 @args = ($id,$seqid,$bin,$start,$end);
1243 } else {
1244 0 $sql = "INSERT INTO $table (id,seqid,bin,start,end) values (?,?,?,?,?)";
1245 0 @args = ($id,$seqid,$bin,$start,$end);
1246 }
1247  
1248 0 my $sth = $self->_prepare($sql);
1249 0 $sth->execute(@args);
1250 0 $sth->finish;
1251 }
1252  
1253 sub _dump_update_location_index {
1254 0 ▶ my $self = shift;
Subroutine _dump_update_location_index: not called CC=2 SCAR=17.9
1255 0 my ($obj,$id) = @_;
1256 0 my $table = $self->_feature_index_table;
1257 0 my $fh = $self->dump_filehandle($table);
1258 0 my $dbh = $self->dbh;
1259 0 my ($seqid,$start,$end,$strand,$bin) = $self->_get_location_and_bin($obj);
1260 0 ▶ my @args = $self->_has_spatial_index ? ($id,$seqid,$bin,$start,$end)
Branch: 1 branch
BranchTrueFalseTotal
$self->_has_spatial_index ? : 0 0 0
1261 : ($id,$seqid,$bin,$start,$end);
1262 0 print $fh join("\t",@args),"\n";
1263 }
1264  
1265 sub DESTROY {
1266 0 ▶ my $self = shift;
Subroutine DESTROY: not called CC=2 SCAR=17.9
1267 # Remove filehandles, so temporal files can be properly deleted
1268 0 ▶ if (%DBI::installed_drh) {
Branch: 1 branch
BranchTrueFalseTotal
if (%DBI::installed_drh) 0 0 0
1269 0 DBI->disconnect_all;
1270 0 %DBI::installed_drh = ();
1271 }
1272 0 undef $self->{dbh};
1273 }
1274  
1275 1 1;
1276  
1277 =head1 AUTHOR
1278  
1279 Nathan Weeks - Nathan.Weeks@ars.usda.gov
1280  
1281 Copyright (c) 2009 Nathan Weeks
1282  
1283 Modified 2010 to support cumulative statistics by Lincoln Stein
1284 <lincoln.stein@gmail.com>.
1285  
1286 This library is free software; you can redistribute it and/or modify
1287 it under the same terms as Perl itself. See the Bioperl license for
1288 more details.
1289  
1290 =cut
« SeqFeature/Store/FeatureFileLoader.pm ↑ Summary SeqFeature/Store/DBI/mysql.pm »