EEDB/FeatureSource.pm untested

filter: Statementstmt 0.0% 0 / 219 Branchbran 0.0% 0 / 94 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 34 Podpod 0.0% 0 / 31 totaltotal 0.0% 0 / 388 CC 48 SCAR 26.9
CC
48
Cov
0%
create_from_name
56.0
check_exists_db
20.0
store_metadata
20.0
CRAP 14.7
1 =head1 NAME - EEDB::FeatureSource
2  
3 =head1 SYNOPSIS
4  
5 =head1 DESCRIPTION
6  
7 =head1 CONTACT
8  
9 Jessica Severin <severin@gsc.riken.jp>
10  
11 =head1 LICENSE
12  
13 * Software License Agreement (BSD License)
14 * EdgeExpressDB [eeDB] system
15 * copyright (c) 2007-2009 Jessica Severin RIKEN OSC
16 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are met:
19 * * Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * * Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * * Neither the name of Jessica Severin RIKEN OSC nor the
25 * names of its contributors may be used to endorse or promote products
26 * derived from this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
29 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
30 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31 * DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS BE LIABLE FOR ANY
32 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
33 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38  
39 =head1 APPENDIX
40  
41 The rest of the documentation details each of the object methods. Internal methods are usually preceded with a _
42  
43 =cut
44  
45 my $__riken_gsc_feature_source_global_should_cache = 1;
46 my $__riken_gsc_feature_source_global_id_cache = {};
47 my $__riken_gsc_feature_source_global_name_cache = {};
48  
49 $VERSION = 0.953;
50  
51 package EEDB::FeatureSource;
52  
53 use strict;
54 use Time::HiRes qw(time gettimeofday tv_interval);
55  
56 use MQdb::MappedQuery;
57 our @ISA = qw(MQdb::MappedQuery);
58  
59 #################################################
60 # Class methods
61 #################################################
62  
63 sub class { return "FeatureSource"; }
64  
65 sub set_cache_behaviour {
66 my $class = shift;
67 my $mode = shift;
68
69 $__riken_gsc_feature_source_global_should_cache = $mode;
70
71 if(defined($mode) and ($mode eq '0')) {
72 #if turning off caching, then flush the caches
73 $__riken_gsc_feature_source_global_id_cache = {};
74 $__riken_gsc_feature_source_global_name_cache = {};
75 }
76 }
77  
78 sub create_from_name {
79 #if $db parameter is supplied then it will check the database first
80 #otherwise it will create one and store it (if $db is provided)
81 my $class = shift;
82 my $fsrc_name = shift;
83 my $db = shift; #optional
84
85 if(!$fsrc_name) { return undef; }
86
87 my $fsrc = undef;
88 my $category = undef;
89 if($fsrc_name =~ /(\w+)\:\:(.+)/) {
90 $category = $1;
91 $fsrc_name = $2;
92 $fsrc = EEDB::FeatureSource->fetch_by_category_name($db, $1, $2) if($db);
93 } else {
94 $fsrc = EEDB::FeatureSource->fetch_by_name($db, $fsrc_name) if($db);
95 }
96 unless($fsrc){
97 $fsrc = new EEDB::FeatureSource;
98 $fsrc->name($fsrc_name);
99 $fsrc->category($category);
100 $fsrc->import_source("");
101 $fsrc->primary_id(-1); #for debugging
102 $fsrc->store($db) if($db);
103 }
104 return $fsrc;
105 }
106  
107  
108 #################################################
109 # Instance methods
110 #################################################
111  
112 sub init {
113 my $self = shift;
114 $self->SUPER::init;
115
116 return $self;
117 }
118  
119 sub name {
120 my $self = shift;
121 return $self->{'name'} = shift if(@_);
122 $self->{'name'}='' unless(defined($self->{'name'}));
123 return $self->{'name'};
124 }
125  
126 sub category {
127 #like 'class', basically a high level controlled vocabulary of types
128 #but not implemented as any constraint in the database. Up to the data
129 #user to control the vocabular prior to data import. Not the DB's job to
130 #babysit the vocabular checks
131 my $self = shift;
132 return $self->{'category'} = shift if(@_);
133 $self->{'category'}='' unless(defined($self->{'category'}));
134 return $self->{'category'};
135 }
136  
137 sub import_source {
138 my $self = shift;
139 return $self->{'import_source'} = shift if(@_);
140 $self->{'import_source'}='' unless(defined($self->{'import_source'}));
141 return $self->{'import_source'};
142 }
143  
144 sub import_date {
145 my $self = shift;
146 return $self->{'import_date'} = shift if(@_);
147 $self->{'import_date'}='' unless(defined($self->{'import_date'}));
148 return $self->{'import_date'};
149 }
150  
151 sub comments {
152 my $self = shift;
153 return $self->{'comments'} = shift if(@_);
154 $self->{'comments'}='' unless(defined($self->{'comments'}));
155 return $self->{'comments'};
156 }
157  
158 sub is_active {
159 my $self = shift;
160 return $self->{'is_active'} = shift if(@_);
161 $self->{'is_active'}='' unless(defined($self->{'is_active'}));
162 return $self->{'is_active'};
163 }
164  
165 sub is_visible {
166 my $self = shift;
167 return $self->{'is_visible'} = shift if(@_);
168 $self->{'is_visible'}='' unless(defined($self->{'is_visible'}));
169 return $self->{'is_visible'};
170 }
171  
172 sub feature_count {
173 my $self = shift;
174 return $self->{'_feature_count'} = shift if(@_);
175 unless(defined($self->{'_feature_count'})) {
176 $self->get_feature_count();
177 }
178 return $self->{'_feature_count'};
179 }
180  
181 sub metadataset {
182 my $self = shift;
183
184 if(!defined($self->{'metadataset'})) {
185 $self->{'metadataset'} = new EEDB::MetadataSet;
186  
187 if($self->database) {
188 my $symbols = EEDB::Symbol->fetch_all_by_feature_source_id($self->database, $self->id);
189 $self->{'metadataset'}->add_metadata(@$symbols);
190  
191 my $mdata = EEDB::Metadata->fetch_all_by_feature_source_id($self->database, $self->id);
192 $self->{'metadataset'}->add_metadata(@$mdata);
193 }
194 }
195 return $self->{'metadataset'};
196 }
197  
198  
199 ################
200  
201 sub get_feature_count {
202 my $self = shift;
203
204 if(!defined($self->database)) { return 0; }
205 if(!defined($self->{'_feature_count'})) {
206 my $sql = "SELECT count(*) FROM feature WHERE feature_source_id=?";
207 $self->{'_feature_count'} = $self->fetch_col_value($self->database, $sql, $self->id);
208 }
209 return $self->{'_feature_count'};
210 }
211  
212 ################
213  
214 sub feature_id_list {
215 #returns a simple array of feature_id for all features in this source
216 #useful for remote streaming. does as direct query, no cache
217 my $self = shift;
218 my $sql = "SELECT feature_id FROM feature where feature_source_id=?";
219 return $self->fetch_multiple($self->database, $sql, $self->id);
220 }
221  
222  
223 ################
224  
225 sub display_desc {
226 my $self = shift;
227 my $str = sprintf("FeatureSource(%s) %s : %s : %s",
228 $self->id,
229 $self->name,
230 $self->category,
231 $self->import_source
232 );
233 return $str;
234 }
235  
236 sub display_contents {
237 my $self = shift;
238  
239 my $str = $self->display_desc;
240 $str .= "\n". $self->metadataset->display_contents;
241 return $str;
242 }
243  
244 sub xml {
245 my $self = shift;
246 my $str = $self->xml_start;
247 my $count = $self->feature_count;
248 if($count) { $str .= sprintf("<feature_count value=\"%d\" />\n", $count); }
249 $str .= "\n". $self->metadataset->xml;
250 $str .= $self->xml_end;
251 return $str;
252 }
253  
254 sub xml_start {
255 my $self = shift;
256 my $str = sprintf("<featuresource id=\"%s\" name=\"%s\" category=\"%s\" source=\"%s\" comments=\"%s\" ",
257 $self->id,
258 $self->name,
259 $self->category,
260 $self->import_source,
261 $self->comments
262 );
263 $str .= sprintf("peer_uuid=\"%s\" ", $self->database->uuid) if($self->database->uuid);
264 $str .= sprintf("import_date=\"%s\" ", $self->import_date) if(defined($self->import_date));
265 $str .= ">";
266  
267 return $str;
268 }
269  
270 sub xml_end {
271 my $self = shift;
272 return "</featuresource>\n";
273 }
274  
275  
276 #################################################
277 #
278 # DBObject override methods
279 #
280 #################################################
281  
282 sub check_exists_db {
283 my $self = shift;
284 my $db = shift;
285
286 unless($db) { return undef; }
287 if(defined($self->primary_id) and ($self->primary_id>0)) { return $self; }
288
289 #check if it is already in the database
290 my $dbID = $db->fetch_col_value("SELECT feature_source_id FROM feature_source where name=?", $self->name);
291 if($dbID) {
292 $self->primary_id($dbID);
293 $self->database($db);
294 return $self;
295 } else {
296 return undef;
297 }
298 }
299  
300 sub store {
301 my $self = shift;
302 my $db = shift;
303
304 if($db) { $self->database($db); }
305
306 $db->execute_sql("INSERT ignore INTO feature_source ".
307 "(name, category, is_active, is_visible, import_source, import_date) ".
308 "VALUES(?,?,?,?,?,NOW())",
309 $self->name,
310 $self->category,
311 $self->is_active,
312 $self->is_visible,
313 $self->import_source);
314  
315 $self->check_exists_db($db); #checks the database and sets the primary_id
316  
317 #now do the symbols and metadata
318 $self->store_metadata;
319
320 return $self;
321 }
322  
323 sub store_metadata {
324 my $self = shift;
325 die("error no database to store metadata\n") unless($self->database);
326 return unless($self->{'metadataset'}); #if not created then don't lazy load
327
328 my $mdata_list = $self->metadataset->metadata_list;
329 foreach my $mdata (@$mdata_list) {
330 if(!defined($mdata->primary_id)) {
331 $mdata->store($self->database);
332 $mdata->store_link_to_feature_source($self);
333 }
334 }
335 }
336  
337 sub sync_importdate_to_features {
338 my $self = shift;
339
340 my $sql = "SELECT max(last_update) FROM feature where feature_source_id=?";
341 my $lastdate = $self->fetch_col_value($self->database, $sql, $self->id);
342  
343 $sql = "UPDATE feature_source SET import_date=? WHERE feature_source_id=?";
344 $self->database->execute_sql($sql, $lastdate, $self->id);
345 }
346  
347  
348 ##### DBObject instance override methods #####
349  
350 sub mapRow {
351 my $self = shift;
352 my $rowHash = shift;
353 my $dbh = shift;
354  
355 $self->primary_id($rowHash->{'feature_source_id'});
356 $self->name($rowHash->{'name'});
357 $self->category($rowHash->{'category'});
358 $self->import_source($rowHash->{'import_source'});
359 $self->import_date($rowHash->{'import_date'});
360 $self->comments($rowHash->{'comments'});
361 $self->is_active($rowHash->{'is_active'});
362 $self->is_visible($rowHash->{'is_visible'});
363 $self->feature_count($rowHash->{'feature_count'});
364  
365 if($__riken_gsc_feature_source_global_should_cache != 0) {
366 if(defined($__riken_gsc_feature_source_global_id_cache->{$self->database() . $self->id})) {
367 $self = $__riken_gsc_feature_source_global_id_cache->{$self->database() . $self->id};
368 } else {
369 $__riken_gsc_feature_source_global_id_cache->{$self->database() . $self->id} = $self;
370 $__riken_gsc_feature_source_global_name_cache->{$self->database() . $self->category . $self->name} = $self;
371 $__riken_gsc_feature_source_global_name_cache->{$self->database() . $self->name} = $self;
372 }
373 }
374
375 return $self;
376 }
377  
378  
379 ##### public class methods for fetching by utilizing DBObject framework methods #####
380  
381 sub fetch_all {
382 my $class = shift;
383 my $db = shift;
384 my $sql = "SELECT * FROM feature_source";
385 return $class->fetch_multiple($db, $sql);
386 }
387  
388 sub fetch_by_id {
389 my $class = shift;
390 my $db = shift;
391 my $id = shift;
392  
393 if($__riken_gsc_feature_source_global_should_cache != 0) {
394 my $obj = $__riken_gsc_feature_source_global_id_cache->{$db . $id};
395 if(defined($obj)) { return $obj; }
396 }
397  
398 my $sql = "SELECT * FROM feature_source WHERE feature_source_id=?";
399 return $class->fetch_single($db, $sql, $id);
400 }
401  
402 sub fetch_all_by_chrom_chunk_id {
403 my $class = shift;
404 my $db = shift;
405 my $id = shift;
406
407 #turn on the ChromChunk caching or this will explode
408 #EEDB::ChromChunk->set_cache_behaviour(1);
409  
410 my $sql = "SELECT * FROM feature_source ".
411 "WHERE chrom_chunk_id = ? ORDER BY chrom_start";
412 return $class->fetch_multiple($db, $sql, $id);
413 }
414  
415 sub fetch_by_name {
416 my $class = shift;
417 my $db = shift;
418 my $name = shift;
419
420 if($__riken_gsc_feature_source_global_should_cache != 0) {
421 my $obj = $__riken_gsc_feature_source_global_name_cache->{$db . $name};
422 if(defined($obj)) { return $obj; }
423 }
424  
425 my $sql = "SELECT * FROM feature_source WHERE name=? ";
426 return $class->fetch_single($db, $sql, $name);
427 }
428  
429 sub fetch_by_category_name {
430 my $class = shift;
431 my $db = shift;
432 my $category = shift;
433 my $name = shift;
434
435 if($__riken_gsc_feature_source_global_should_cache != 0) {
436 my $obj = $__riken_gsc_feature_source_global_name_cache->{$db . $category . $name};
437 if(defined($obj)) { return $obj; }
438 }
439  
440 my $sql = "SELECT * FROM feature_source ".
441 "WHERE name=? and category=?";
442 return $class->fetch_single($db, $sql, $name, $category);
443 }
444  
445 sub _load_symbols {
446 my $self = shift;
447
448 return if(defined($self->{'symbols'}));
449  
450 die("no database defined\n") unless($self->database);
451 my $dbc = $self->database->get_connection;
452 my $sql = "SELECT sym_type, sym_value FROM feature_metadata join symbol using(symbol_id) where feature_id = ?";
453 my $sth = $dbc->prepare($sql);
454 $sth->execute($self->primary_id);
455 while(my ($type, $value) = $sth->fetchrow_array) {
456 $self->{'symbols'}->{$type} = $value;
457 }
458 $sth->finish;
459 }
460  
461 1;
462