EEDB/Experiment.pm untested

filter: Statementstmt 0.0% 0 / 196 Branchbran 0.0% 0 / 68 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 27 Podpod 0.0% 0 / 24 totaltotal 0.0% 0 / 330 CC 35 SCAR 25.9
CC
35
Cov
0%
fetch_all_by_filter_search
42.0
mapRow
20.0
display_name
12.0
CRAP 13.3
1 =head1 NAME - EEDB::Experiment
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_EEDB_experiment_global_should_cache = 1;
46 my $__riken_EEDB_experiment_global_id_cache = {};
47 my $__riken_EEDB_experiment_global_name_cache = {};
48  
49 $VERSION = 0.953;
50  
51 package EEDB::Experiment;
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 "Experiment"; }
64  
65 sub set_cache_behaviour {
66 my $class = shift;
67 my $mode = shift;
68  
69 $__riken_EEDB_experiment_global_should_cache = $mode;
70  
71 if(defined($mode) and ($mode eq '0')) {
72 #if turning off caching, then flush the caches
73 $__riken_EEDB_experiment_global_id_cache = {};
74 $__riken_EEDB_experiment_global_name_cache = {};
75 }
76 }
77  
78  
79  
80 #################################################
81 # Instance methods
82 #################################################
83  
84 sub init {
85 my $self = shift;
86 $self->SUPER::init;
87
88 $self->{'series_name'} = undef;
89 $self->{'exp_accession'} = undef;
90 $self->{'series_point'} = undef;
91
92 return $self;
93 }
94  
95  
96 ##########################
97 #
98 # getter/setter methods of data which is stored in database
99 #
100 ##########################
101  
102 sub exp_accession {
103 my $self = shift;
104 return $self->{'exp_accession'} = shift if(@_);
105 return $self->{'exp_accession'};
106 }
107  
108 sub display_name {
109 my $self = shift;
110 return $self->{'display_name'} = shift if(@_);
111 if(!defined($self->{'display_name'}) and defined($self->{'exp_accession'})) {
112 return $self->{'exp_accession'};
113 } else {
114 return $self->{'display_name'};
115 }
116 }
117  
118 sub platform {
119 my $self = shift;
120 return $self->{'platform'} = shift if(@_);
121 $self->{'platform'}='' unless(defined($self->{'platform'}));
122 return $self->{'platform'};
123 }
124  
125 sub series_name {
126 my $self = shift;
127 return $self->{'series_name'} = shift if(@_);
128 $self->{'series_name'}='' unless(defined($self->{'series_name'}));
129 return $self->{'series_name'};
130 }
131  
132 sub series_point {
133 my $self = shift;
134 return $self->{'series_point'} = shift if(@_);
135 $self->{'series_point'}=0 unless(defined($self->{'series_point'}));
136 return $self->{'series_point'};
137 }
138  
139 sub is_active {
140 my $self = shift;
141 return $self->{'is_active'} = shift if(@_);
142 $self->{'is_active'}='' unless(defined($self->{'is_active'}));
143 return $self->{'is_active'};
144 }
145  
146 sub metadataset {
147 my $self = shift;
148
149 if(!defined($self->{'metadataset'})) {
150 $self->{'metadataset'} = new EEDB::MetadataSet;
151  
152 if($self->database) {
153 my $symbols = EEDB::Symbol->fetch_all_by_experiment_id($self->database, $self->id);
154 $self->{'metadataset'}->add_metadata(@$symbols);
155  
156 my $mdata = EEDB::Metadata->fetch_all_by_experiment_id($self->database, $self->id);
157 $self->{'metadataset'}->add_metadata(@$mdata);
158 }
159 }
160 return $self->{'metadataset'};
161 }
162  
163 ####################################
164  
165 sub display_desc
166 {
167 my $self = shift;
168 my $str = sprintf("Experiment(%s) [%s] %s : (%s, %s) : %s",
169 $self->id,
170 $self->platform,
171 $self->display_name,
172 $self->series_name,
173 $self->series_point,
174 $self->exp_accession);
175 return $str;
176 }
177  
178 sub display_contents {
179 my $self = shift;
180 my $str = $self->display_desc;
181 $str .= "\n". $self->metadataset->display_contents;
182 return $str;
183 }
184  
185 ####################################
186  
187 sub xml_start {
188 my $self = shift;
189 my $str = sprintf("<experiment id=\"%d\" platform=\"%s\" name=\"%s\" exp_acc=\"%s\" ",
190 $self->id,
191 $self->platform,
192 $self->display_name,
193 $self->exp_accession);
194 $str .= sprintf("series_name=\"%s\" ", $self->series_name) if($self->series_name);
195 $str .= sprintf("series_point=\"%s\" ", $self->series_point) if(defined($self->series_point));
196 $str .= ">\n";
197 return $str;
198 }
199  
200 sub xml_end {
201 my $self = shift;
202 return "</experiment>\n";
203 }
204  
205 sub xml {
206 my $self = shift;
207
208 my $str = $self->xml_start;
209  
210 $str .= $self->metadataset->xml;
211
212 $str .= $self->xml_end;
213 return $str;
214 }
215  
216  
217 #################################################
218 #
219 # DBObject override methods
220 #
221 #################################################
222  
223 sub store {
224 my $self = shift;
225 my $db = shift;
226
227 if($db) { $self->database($db); }
228 my $dbh = $self->database->get_connection;
229 my $sql = "INSERT ignore INTO experiment ".
230 "(exp_accession, display_name, platform, series_name, series_point, is_active) ".
231 "VALUES(?,?,?,?,?,?)";
232 my $sth = $dbh->prepare($sql);
233 $sth->execute($self->exp_accession,
234 $self->display_name,
235 $self->platform,
236 $self->series_name,
237 $self->series_point,
238 $self->is_active
239 );
240  
241 my $dbID = $sth->{'mysql_insertid'};
242 $sth->finish;
243 unless($dbID) {
244 $sql = "select experiment_id from experiment where exp_accession=?";
245 $dbID = $self->fetch_col_value($self->database, $sql, $self->exp_accession);
246 }
247 $self->primary_id($dbID);
248
249 #now do the symbols and metadata
250 $self->store_metadata;
251  
252 return $self;
253 }
254  
255 sub store_metadata {
256 my $self = shift;
257 die("error no database to store metadata\n") unless($self->database);
258  
259 my $mdata_list = $self->metadataset->metadata_list;
260 foreach my $mdata (@$mdata_list) {
261 if(!defined($mdata->primary_id)) {
262 $mdata->store($self->database);
263 $mdata->store_link_to_experiment($self);
264 }
265 }
266 }
267  
268 sub update {
269 my $self = shift;
270  
271 die("error no database set\n") unless($self->database);
272 dies("Experiment with undef id") unless(defined($self->primary_id));
273
274 my $dbc = $self->database->get_connection;
275 my $sql = "UPDATE experiment SET display_name=? WHERE experiment_id=?";
276 my $sth = $dbc->prepare($sql);
277 $sth->execute($self->display_name,
278 $self->id);
279 $sth->finish;
280 }
281  
282  
283  
284 ##### DBObject instance override methods #####
285  
286 sub mapRow {
287 my $self = shift;
288 my $rowHash = shift;
289 my $dbh = shift;
290  
291 my $dbID = $rowHash->{'experiment_id'};
292 if(($__riken_EEDB_experiment_global_should_cache != 0) and ($self->database())) {
293 my $cached_self = $__riken_EEDB_experiment_global_id_cache->{$self->database() . $dbID};
294 if(defined($cached_self)) {
295 return $cached_self;
296 }
297 }
298  
299 $self->primary_id($rowHash->{'experiment_id'});
300 $self->series_name($rowHash->{'series_name'});
301 $self->exp_accession($rowHash->{'exp_accession'});
302 $self->display_name($rowHash->{'display_name'});
303 $self->platform($rowHash->{'platform'});
304 $self->series_point($rowHash->{'series_point'});
305 $self->is_active($rowHash->{'is_active'});
306
307 if($__riken_EEDB_experiment_global_should_cache != 0) {
308 $__riken_EEDB_experiment_global_id_cache->{$self->database() . $self->id} = $self;
309 $__riken_EEDB_experiment_global_name_cache->{$self->database() . $self->exp_accession} = $self;
310 }
311  
312 return $self;
313 }
314  
315  
316 ##### public class methods for fetching by utilizing DBObject framework methods #####
317  
318 sub fetch_by_id {
319 my $class = shift;
320 my $db = shift;
321 my $id = shift;
322  
323 if($__riken_EEDB_experiment_global_should_cache != 0) {
324 my $feature = $__riken_EEDB_experiment_global_id_cache->{$db . $id};
325 if(defined($feature)) { return $feature; }
326 }
327  
328 my $sql = "SELECT * FROM experiment WHERE experiment_id=?";
329 return $class->fetch_single($db, $sql, $id);
330 }
331  
332 sub fetch_all {
333 my $class = shift;
334 my $db = shift;
335  
336 my $sql = "SELECT * FROM experiment ORDER BY platform, series_name, series_point, exp_accession";
337 return $class->fetch_multiple($db, $sql);
338 }
339  
340 sub fetch_all_by_platform {
341 my $class = shift;
342 my $db = shift;
343 my $platform = shift;
344  
345 my $sql = "SELECT * FROM experiment WHERE platform=? ORDER BY platform, series_name, series_point, exp_accession";
346 return $class->fetch_multiple($db, $sql, $platform);
347 }
348  
349 sub fetch_by_exp_accession {
350 my $class = shift;
351 my $db = shift;
352 my $exp_accession = shift;
353  
354 if($__riken_EEDB_experiment_global_should_cache != 0) {
355 my $feature = $__riken_EEDB_experiment_global_name_cache->{$db . $exp_accession};
356 if(defined($feature)) { return $feature; }
357 }
358  
359 my $sql = "SELECT * FROM experiment WHERE exp_accession=?";
360 return $class->fetch_single($db, $sql, $exp_accession);
361 }
362  
363  
364 sub fetch_all_by_filter_search {
365 my $class = shift;
366 my $db = shift;
367 my $filter = shift;
368 my $response_limit = shift; #optional
369
370 my $filter_sql = "(SELECT distinct t1.experiment_id FROM \n";
371  
372 my @clauses = split (/and/, $filter);
373 my $and_count=0;
374 foreach my $and_clause (@clauses) {
375 my $or_sql = "(SELECT distinct experiment_id FROM experiment_2_symbol join symbol using(symbol_id) WHERE ";
376 #sym_value like "RIKEN1%" or sym_value like 'RIKEN3%' or sym_value like 'CAGE%')t1
377 #printf("or block :: %s\n", $and_clause);
378 my @toks = split (/or/, $and_clause);
379 my $or_count=0;
380 foreach my $tok (@toks) {
381 $or_count++;
382 $tok =~ s/^\s+//g;
383 $tok =~ s/\s+$//g;
384 $tok =~ s/_/\\_/g;
385 $tok =~ s/\"/\\\"/g;
386 $tok =~ s/\'/\\\'/g; #'
387 if($or_count>1) { $or_sql .= " or "; }
388 $or_sql .= sprintf(" sym_value like \"%s%%\" ", $tok);
389
390 }
391 $and_count++;
392 $or_sql .= sprintf(") t%d", $and_count);
393 if($and_count>1) { $filter_sql .= ",\n"; }
394 $filter_sql .= " ". $or_sql;
395 }
396 if($and_count > 1) {
397 $filter_sql .= "\nWHERE ";
398 my $x=1;
399 while($x < $and_count) {
400 if($x>1) { $filter_sql .= " AND "; }
401 $filter_sql .= sprintf(" t%d.experiment_id = t%d.experiment_id ", $x, $x+1);
402 $x++;
403 }
404 }
405  
406 $filter_sql .= ")";
407
408 my $sql = sprintf("SELECT e.* FROM experiment e join %s filter where e.experiment_id = filter.experiment_id", $filter_sql);
409 $sql .= sprintf(" LIMIT %d", $response_limit) if($response_limit);
410  
411 #print($sql, "\n", );
412 return $class->fetch_multiple($db, $sql);
413 #return [];
414 }
415  
416  
417 1;
418