EEDB/ChromChunk.pm untested

filter: Statementstmt 0.0% 0 / 218 Branchbran 0.0% 0 / 72 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 0.0% 0 / 34 Podpod 0.0% 0 / 31 totaltotal 0.0% 0 / 389 CC 37 SCAR 26.2
CC
37
Cov
0%
chrom
30.0
sequence
30.0
store
30.0
CRAP 13.8
1 =head1 NAME - EEDB::ChromChunk
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_chromchunk_global_should_cache = 0;
46 my $__riken_gsc_chromchunk_global_id_cache = {};
47  
48 $VERSION = 0.953;
49  
50 package EEDB::ChromChunk;
51  
52 use strict;
53 use Time::HiRes qw(time gettimeofday tv_interval);
54  
55 use MQdb::MappedQuery;
56 our @ISA = qw(MQdb::MappedQuery);
57  
58 #################################################
59 # Class methods
60 #################################################
61  
62 sub class { return "ChromChunk"; }
63  
64 sub set_cache_behaviour {
65 my $class = shift;
66 my $mode = shift;
67
68 $__riken_gsc_chromchunk_global_should_cache = $mode;
69
70 if(defined($mode) and ($mode eq '0')) {
71 #if turning off caching, then flush the caches
72 $__riken_gsc_chromchunk_global_id_cache = {};
73 }
74 }
75  
76 #################################################
77 # Instance methods
78 #################################################
79  
80 sub init {
81 my $self = shift;
82 $self->SUPER::init;
83
84 $self->{'chrom'} = undef;
85 $self->{'assembly_name'} = undef;
86 $self->{'chrom_name'} = undef;
87 $self->{'chrom_start'} = undef;
88 $self->{'chrom_end'} = undef;
89 $self->{'_sequence'} = undef; #Bio::Seq object
90
91 return $self;
92 }
93  
94  
95 ##########################
96 #
97 # getter/setter methods of data which is stored in database
98 #
99 ##########################
100  
101 sub chrom {
102 my ($self, $chrom) = @_;
103 if($chrom) {
104 unless(defined($chrom) && $chrom->isa('EEDB::Chrom')) {
105 die('chrom param must be a EEDB::Chrom');
106 }
107 $self->{'chrom'} = $chrom;
108 }
109
110 #lazy load from database if possible
111 if(!defined($self->{'chrom'}) and
112 defined($self->database) and
113 defined($self->{'chrom_id'}))
114 {
115 #printf("LAZY LOAD chrom_id=%d\n", $self->{'_chrom_id'});
116 my $chrom = EEDB::Chrom->fetch_by_id($self->database, $self->{'chrom_id'});
117 if(defined($chrom)) { $self->{'chrom'} = $chrom; }
118 }
119 return $self->{'chrom'};
120 }
121  
122  
123 sub assembly_name {
124 my $self = shift;
125 return $self->{'assembly_name'} = shift if(@_);
126 $self->{'assembly_name'}='' unless(defined($self->{'assembly_name'}));
127 if($self->chrom) {
128 return $self->chrom->assembly->ucsc_name;
129 } else {
130 return $self->{'assembly_name'};
131 }
132 }
133  
134 sub chrom_name {
135 my $self = shift;
136 return $self->{'chrom_name'} = shift if(@_);
137 $self->{'chrom_name'}='' unless(defined($self->{'chrom_name'}));
138 if($self->chrom) {
139 return $self->chrom->chrom_name;
140 } else {
141 return $self->{'chrom_name'};
142 }
143 }
144  
145 sub chrom_id {
146 my $self = shift;
147 return $self->{'chrom_id'} = shift if(@_);
148 $self->{'chrom_id'}='' unless(defined($self->{'chrom_id'}));
149 if($self->chrom) {
150 return $self->chrom->id;
151 } else {
152 return $self->{'chrom_id'};
153 }
154 }
155  
156 sub chrom_start {
157 my $self = shift;
158 return $self->{'chrom_start'} = shift if(@_);
159 $self->{'chrom_start'}=0 unless(defined($self->{'chrom_start'}));
160 return $self->{'chrom_start'};
161 }
162  
163 sub chrom_end {
164 my $self = shift;
165 return $self->{'chrom_end'} = shift if(@_);
166 $self->{'chrom_end'}=0 unless(defined($self->{'chrom_end'}));
167 return $self->{'chrom_end'};
168 }
169  
170 sub seq_length {
171 my $self = shift;
172 return $self->chrom_end - $self->chrom_start + 1;
173 }
174  
175 sub chunk_name {
176 my $self = shift;
177 return sprintf("chunk%d-%s_%s:%d..%d", $self->id, $self->assembly_name, $self->chrom_name, $self->chrom_start, $self->chrom_end);
178 }
179  
180 =head2 sequence
181  
182 Args : none
183 Example : my $bioseq = $chunk->sequence;
184 Description: returns named sequence as a Bio::Seq object
185 Returntype : Bio::Seq object
186 Exceptions : none
187 Caller : general
188  
189 =cut
190  
191 sub sequence {
192 my $self = shift;
193 if(@_) {
194 my $seq = shift;
195 unless(defined($seq) && $seq->isa('Bio::Seq')) {
196 die('sequence argument must be a Bio::Seq');
197 }
198 $self->{'_sequence'} = $seq;
199 }
200 return $self->{'_sequence'} if(defined($self->{'_sequence'}));
201  
202 #lazy load the sequence if sequence_id is set
203 if(!defined($self->{'_sequence'}) and defined($self->database())) {
204 $self->_fetch_sequence();
205 }
206 return $self->{'_sequence'};
207 }
208  
209  
210 sub get_subsequence {
211 my $self = shift;
212 my $chrom_start = shift;
213 my $chrom_end = shift;
214 my $strand = shift;
215
216 $strand = "+" unless(defined($strand));
217
218 my $offset = $chrom_start - $self->chrom_start + 1;
219 my $length = $chrom_end - $chrom_start + 1;
220 my $seq = $self->_fetch_sub_sequence($offset, $length);
221 my $name = sprintf("chunk%d_%s_%s:%d..%d%s", $self->id, $self->assembly_name, $self->chrom_name, $chrom_start, $chrom_end, $strand);
222 my $bioseq = Bio::Seq->new(-id=>$name, -seq=>$seq);
223 if($strand eq '-') { $bioseq = $bioseq->revcom; }
224 return $bioseq;
225 }
226  
227  
228 sub display_desc {
229 my $self = shift;
230 return sprintf("ChromChunk(db %s ) %s %s : %d - %d",
231 $self->id,
232 $self->assembly_name,
233 $self->chrom_name,
234 $self->chrom_start, $self->chrom_end);
235 }
236  
237  
238 sub xml {
239 my $self = shift;
240 my $str = sprintf("<chrom_chunk id=\"%d\" assembly=\"%s\" chr=\"%s\" start=\"%d\" end=\"%d\" />\n",
241 $self->id,
242 $self->assembly_name,
243 $self->chrom_name,
244 $self->chrom_start,
245 $self->chrom_end);
246 return $str;
247 }
248  
249  
250 sub dump_to_fasta_file {
251 my $self = shift;
252 my $fastafile = shift;
253
254 my $bioseq = $self->sequence;
255 unless(defined($fastafile)) {
256 $fastafile = $bioseq->id . ".fa";
257 }
258  
259 #printf(" writing chunk %s\n", $self->display_id);
260 open(OUTSEQ, ">$fastafile")
261 or $self->die("Error opening $fastafile for write");
262 my $output_seq = Bio::SeqIO->new( -fh =>\*OUTSEQ, -format => 'fasta');
263 $output_seq->write_seq($bioseq);
264 close OUTSEQ;
265  
266 return $self;
267 }
268  
269 #################################################
270 #
271 # DBObject override methods
272 #
273 #################################################
274  
275 sub store {
276 my $self = shift;
277 my $db = shift;
278
279 if($db) { $self->database($db); }
280  
281 if(!defined($self->chrom_id)) {
282 $self->_fetch_chrom_id_for_store();
283 }
284 return undef unless($self->chrom_id);
285
286 my $dbh = $self->database->get_connection;
287 my $sql = "INSERT ignore INTO chrom_chunk (
288 chrom_id,
289 chrom_start,
290 chrom_end,
291 chunk_len)
292 VALUES(?,?,?,?)";
293 my $sth = $dbh->prepare($sql);
294 $sth->execute($self->chrom_id,
295 $self->chrom_start,
296 $self->chrom_end,
297 $self->seq_length);
298  
299 my $dbID = $sth->{'mysql_insertid'};
300 $sth->finish;
301 unless($dbID) {
302 $sql = "select chrom_chunk_id from chrom_chunk where chrom_id=? and chrom_start=? and chrom_end=?";
303 $dbID = $self->fetch_col_value($self->database,
304 $sql,
305 $self->chrom_id,
306 $self->chrom_start,
307 $self->chrom_end);
308 }
309 $self->primary_id($dbID);
310
311 #now store the sequence
312 $self->store_seq();
313 }
314  
315  
316 sub check_exists_db {
317 my $self = shift;
318 my $db = shift;
319
320 return undef unless($db);
321 my $sql = "select chrom_chunk_id from chrom_chunk where chrom_id=? and chrom_start=? and chrom_end=?";
322 my $dbID = $db->fetch_col_value($sql, $self->chrom_id, $self->chrom_start, $self->chrom_end);
323 if($dbID) {
324 $self->primary_id($dbID);
325 $self->database($db);
326 return $self;
327 } else {
328 return undef;
329 }
330 }
331  
332 sub store_seq {
333 my $self = shift;
334
335 return unless(defined($self->{'_sequence'}));
336  
337 my $dbh = $self->database->get_connection;
338 my $sql = "INSERT ignore INTO chrom_chunk_seq (chrom_chunk_id, sequence) VALUES(?,?)";
339 my $sth = $dbh->prepare($sql);
340 $sth->execute($self->primary_id, $self->sequence->seq);
341 $sth->finish;
342 }
343  
344 ##### DBObject instance override methods #####
345  
346 sub mapRow {
347 my $self = shift;
348 my $rowHash = shift;
349 my $dbh = shift;
350  
351 $self->primary_id($rowHash->{'chrom_chunk_id'});
352 $self->assembly_name($rowHash->{'ucsc_name'});
353 $self->chrom_id($rowHash->{'chrom_id'});
354 $self->chrom_name($rowHash->{'chrom_name'});
355 $self->chrom_start($rowHash->{'chrom_start'});
356 $self->chrom_end($rowHash->{'chrom_end'});
357
358 if($__riken_gsc_chromchunk_global_should_cache != 0) {
359 $__riken_gsc_chromchunk_global_id_cache->{$self->database() . $self->id} = $self;
360 #printf("@@@@@ caching ChromChunk for %d\n", $self->id);
361 }
362
363 return $self;
364 }
365  
366  
367 ##### public class methods for fetching by utilizing DBObject framework methods #####
368  
369 sub fetch_by_id {
370 my $class = shift;
371 my $db = shift;
372 my $id = shift;
373  
374 if($__riken_gsc_chromchunk_global_should_cache != 0) {
375 my $chunk = $__riken_gsc_chromchunk_global_id_cache->{$db . $id};
376 if(defined($chunk)) {
377 #printf("##### YEAH using the chromchunk cache for %d\n", $id);
378 return $chunk;
379 }
380 }
381
382 my $sql = "SELECT * FROM chrom_chunk join chrom using(chrom_id) join assembly using(assembly_id) WHERE chrom_chunk_id=?";
383 return $class->fetch_single($db, $sql, $id);
384 }
385  
386 sub fetch_all {
387 my $class = shift;
388 my $db = shift;
389
390 my $sql = "SELECT * FROM chrom_chunk join chrom using(chrom_id) join assembly using(assembly_id)";
391 return $class->fetch_multiple($db, $sql);
392 }
393  
394 sub fetch_all_for_feature {
395 my $class = shift;
396 my $db = shift;
397 my $feature = shift; #Feature object
398
399 my $sql = "SELECT * FROM feature_2_chunk JOIN chrom_chunk using(chrom_chunk_id) ".
400 "JOIN chrom using(chrom_id) JOIN assembly using(assembly_id) ".
401 "WHERE feature_id=?";
402 return $class->fetch_multiple($db, $sql, $feature->id);
403 }
404  
405 sub fetch_by_id_range {
406 my $class = shift;
407 my $db = shift;
408 my $id_range = shift; #ruby style Range but as string "(1..800)" which needs parsing
409
410 $id_range =~ /\((\d+)\.\.(\d+)\)/;
411 my($start, $end) = ($1, $2);
412 #printf("fetch_by_id_range : %s : %d %d\n", $id_range, $start, $end);
413 my $sql = "SELECT * FROM chrom_chunk JOIN chrom USING(chrom_id) JOIN assembly USING(assembly_id) ".
414 "WHERE chrom_chunk_id>=? AND chrom_chunk_id<=? ORDER BY chrom_chunk_id";
415 return $class->fetch_multiple($db, $sql, $start, $end);
416 }
417  
418 sub fetch_all_by_assembly_name {
419 my $class = shift;
420 my $db = shift;
421 my $assembly_name = shift;
422
423 my $sql = "SELECT * FROM chrom_chunk ".
424 "JOIN chrom USING(chrom_id) ".
425 "JOIN assembly USING(assembly_id) ".
426 "WHERE (ncbi_version=? or ucsc_name=?) ORDER BY chrom_chunk_id";
427 return $class->fetch_multiple($db, $sql, $assembly_name, $assembly_name);
428 }
429  
430 sub fetch_all_named_region {
431 my $class = shift;
432 my $db = shift;
433 my $assembly_name = shift;
434 my $chrom_name = shift;
435 my $chrom_start = shift;
436 my $chrom_end = shift;
437
438 my $sql = "SELECT * FROM chrom_chunk ".
439 "JOIN chrom USING(chrom_id) ".
440 "JOIN assembly USING(assembly_id) ".
441 "WHERE (ncbi_version=? or ucsc_name=?) ".
442 "AND chrom_name = ? AND chrom_start <= ? AND chrom_end >= ? ".
443 "ORDER BY chrom_start";
444 return $class->fetch_multiple($db, $sql, $assembly_name, $assembly_name, $chrom_name, $chrom_end, $chrom_start);
445 }
446  
447 sub fetch_all_by_chrom {
448 my $class = shift;
449 my $chrom = shift; #Chrom object
450
451 my $sql = "SELECT * FROM chrom_chunk WHERE chrom_id=? ".
452 "ORDER BY chrom_start";
453 return $class->fetch_multiple($chrom->database, $sql, $chrom->id);
454 }
455  
456 sub fetch_all_by_chrom_range {
457 my $class = shift;
458 my $chrom = shift; #Chrom object, also source of database
459 my $chrom_start = shift;
460 my $chrom_end = shift;
461
462 my $sql = "SELECT * FROM chrom_chunk ".
463 "WHERE chrom_id = ? AND chrom_start <= ? AND chrom_end >= ? ".
464 "ORDER BY chrom_start";
465 return $class->fetch_multiple($chrom->database, $sql, $chrom->id, $chrom_end, $chrom_start);
466 }
467  
468  
469 ####### internal DB methods #######
470  
471 sub _fetch_sequence {
472 my $self = shift;
473  
474 my $sql = "SELECT sequence FROM chrom_chunk_seq WHERE chrom_chunk_id=?";
475 my $seq = $self->fetch_col_value($self->database, $sql, $self->primary_id);
476 return unless(defined($seq));
477 my $name = sprintf("chunk%d-%s-%s-%d", $self->id, $self->assembly_name, $self->chrom_name, $self->chrom_start);
478 my $bioseq = Bio::Seq->new(-id=>$name, -seq=>$seq);
479 $self->sequence($bioseq);
480 }
481  
482 sub _fetch_sub_sequence {
483 my $self = shift;
484 my $offset = shift;
485 my $length = shift;
486  
487 my $sql = sprintf("SELECT substr(sequence, %s, %s) FROM chrom_chunk_seq WHERE chrom_chunk_id=?", $offset, $length);
488 my $seq = $self->fetch_col_value($self->database, $sql, $self->primary_id);
489 return $seq;
490 }
491  
492 sub _fetch_chrom_id_for_store {
493 my $self = shift;
494  
495 my $sql = "SELECT chrom_id FROM chrom join assembly using(assembly_id) WHERE chrom_name=? and (ncbi_version=? or ucsc_name=?)";
496 my $chrom_id = $self->fetch_col_value($self->database, $sql, $self->chrom_name, $self->assembly_name, $self->assembly_name);
497 $self->chrom_id($chrom_id);
498 }
499  
500 1;
501