EEDB/Chrom.pm untested

filter: Statementstmt 0.0% 0 / 222 Branchbran 0.0% 0 / 70 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 30 Podpod 0.0% 0 / 26 totaltotal 0.0% 0 / 383 CC 36 SCAR 35.9
CC
36
Cov
0%
get_subsequence
156.0
fetch_by_assembly_chrname
20.0
chrom_name
12.0
CRAP 36.1
1 =head1 NAME - EEDB::Chrom
2  
3 =head1 SYNOPSIS
4  
5 An object to encapsulate a Chromosome within an Assembly.
6  
7 =head1 DESCRIPTION
8  
9 An object that corresponds to specific chromosomes within an assembly. Because Chrom is tied to a specific
10 assembly, an instance of a Chrom identifies not only the chromosome, but also the assembly and species
11  
12 As with all objects in EEDB, Chrom interits from MQdb::DBObject and MQdb::MappedQuery.
13 Please refer to these documents for all superclass methods
14  
15 =head1 CONTACT
16  
17 Jessica Severin <severin@gsc.riken.jp>
18  
19 =head1 LICENSE
20  
21 * Software License Agreement (BSD License)
22 * EdgeExpressDB [eeDB] system
23 * copyright (c) 2007-2009 Jessica Severin RIKEN OSC
24 * All rights reserved.
25 * Redistribution and use in source and binary forms, with or without
26 * modification, are permitted provided that the following conditions are met:
27 * * Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * * Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
32 * * Neither the name of Jessica Severin RIKEN OSC nor the
33 * names of its contributors may be used to endorse or promote products
34 * derived from this software without specific prior written permission.
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
37 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
38 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39 * DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS BE LIABLE FOR ANY
40 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
41 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
45 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46  
47 =head1 APPENDIX
48  
49 The rest of the documentation details each of the object methods. Internal methods are usually preceded with a _
50  
51 =cut
52  
53 my $__riken_gsc_chrom_global_should_cache = 1;
54 my $__riken_gsc_chrom_global_id_cache = {};
55 my $__riken_gsc_chrom_global_nameasm_cache = {};
56  
57 $VERSION = 0.953;
58  
59 package EEDB::Chrom;
60  
61 use strict;
62 use Time::HiRes qw(time gettimeofday tv_interval);
63  
64 use EEDB::Assembly;
65  
66 use MQdb::MappedQuery;
67 our @ISA = qw(MQdb::MappedQuery);
68  
69 #################################################
70 # Class methods
71 #################################################
72  
73 sub class { return "Chrom"; }
74  
75 =head2 set_cache_behaviour
76  
77 Description : class level method to turn on/off global caching of Chromsome objects
78 since these are very small and used heavily, it is on my default
79 and it is recommended to not turn it off.
80 Parameter[1] : scalar 0/1 to turn the caching off/on
81 Returntype : none
82 Exceptions : none
83  
84 =cut
85  
86 sub set_cache_behaviour {
87 my $class = shift;
88 my $mode = shift;
89
90 $__riken_gsc_chrom_global_should_cache = $mode;
91
92 if(defined($mode) and ($mode eq '0')) {
93 #if turning off caching, then flush the caches
94 $__riken_gsc_chrom_global_id_cache = {};
95 $__riken_gsc_chrom_global_nameasm_cache = {};
96 }
97 }
98  
99 =head2 get_cache_size
100  
101 Description : get count of Chroms currently in the memory cache
102 Returntype : scalar count
103 Exceptions : none
104  
105 =cut
106  
107 sub get_cache_size {
108 return scalar(keys(%{$__riken_gsc_chrom_global_nameasm_cache}));
109 }
110  
111 #################################################
112 # Instance methods
113 #################################################
114  
115 =head2 init
116  
117 Description: initialize a new instance of this object.
118 generally not needed for users to call this method
119 Returntype : $self
120 Exceptions : none
121  
122 =cut
123  
124 sub init {
125 my $self = shift;
126 $self->SUPER::init;
127
128 $self->{'chrom_name'} = undef;
129 $self->{'assembly'} = undef;
130 $self->{'chrom_length'} = undef;
131 $self->{'chrom_type'} = '';
132
133 return $self;
134 }
135  
136  
137 ##########################
138 #
139 # getter/setter methods of data which is stored in database
140 #
141 ##########################
142  
143 =head2 chrom_name
144  
145 Description : simple getter/setter method for the chromosome name
146 Parameter[1] : <optional> if specififed it will set the chromsome name
147 Returntype : string
148 Exceptions : none
149  
150 =cut
151  
152 sub chrom_name {
153 my $self = shift;
154 return $self->{'chrom_name'} = shift if(@_);
155 $self->{'chrom_name'}='' unless(defined($self->{'chrom_name'}));
156 return $self->{'chrom_name'};
157 }
158  
159 =head2 chrom_type
160  
161 Description : simple getter/setter method for the chromosome type
162 Parameter[1] : <optional> if specififed it will set the chromsome type
163 Returntype : string
164 Exceptions : none
165  
166 =cut
167  
168 sub chrom_type {
169 my $self = shift;
170 return $self->{'chrom_type'} = shift if(@_);
171 $self->{'chrom_type'}='' unless(defined($self->{'chrom_type'}));
172 return $self->{'chrom_type'};
173 }
174  
175 =head2 description
176  
177 Description : simple getter/setter method for the chromosome description
178 Parameter[1] : <optional> if specififed it will set the chromsome description
179 Returntype : string
180 Exceptions : none
181  
182 =cut
183  
184 sub description {
185 my $self = shift;
186 return $self->{'description'} = shift if(@_);
187 $self->{'description'}='' unless(defined($self->{'description'}));
188 return $self->{'description'};
189 }
190  
191 =head2 assembly
192  
193 Description : simple getter/setter method for the Assembly of this Chrom
194 Parameter[1] : <optional> of type EEDB::Assembly. if specififed it will set the assembly
195 Returntype : EEDB::Assembly instance
196 Exceptions : none
197  
198 =cut
199  
200 sub assembly {
201 my $self = shift;
202 if(@_) {
203 my $assembly = shift;
204 if(defined($assembly) && !($assembly->isa('EEDB::Assembly'))) {
205 die('assembly param must be a EEDB::Assembly');
206 }
207 return $self->{'assembly'} = $assembly;
208 }
209 return $self->{'assembly'};
210 }
211  
212 =head2 chrom_length
213  
214 Description : simple getter/setter method for the chromosome length
215 Parameter[1] : <optional scalar> if specififed it will set the chromsome length
216 Returntype : scalar
217 Exceptions : none
218  
219 =cut
220  
221 sub chrom_length {
222 my $self = shift;
223 return $self->{'chrom_length'} = shift if(@_);
224 $self->{'chrom_length'}=-1 unless(defined($self->{'chrom_length'}));
225 return $self->{'chrom_length'};
226 }
227  
228 =head2 display_desc
229  
230 Description : overrides the superclass method.
231 returns a debugging description of this instance.
232 calling display_info() will print this display_desc to STDOUT
233 Returntype : string
234 Exceptions : none
235  
236 =cut
237  
238 sub display_desc {
239 my $self = shift;
240 return sprintf("Chrom(db %s ) %s %s %s : len %d :: %s",
241 $self->id,
242 $self->assembly->ucsc_name,
243 $self->chrom_type,
244 $self->chrom_name,
245 $self->chrom_length,
246 $self->description
247 );
248 }
249  
250 =head2 xml_start
251  
252 Description : overrides the superclass method.
253 returns the start of the XML description of this instance
254 Returntype : string
255 Exceptions : none
256  
257 =cut
258  
259 sub xml_start {
260 my $self = shift;
261
262 my $str = sprintf("<chrom id=\"%s\" peer=\"%s\" chr=\"%s\" length=\"%d\" ",
263 $self->id,
264 $self->database->alias,
265 $self->chrom_name,
266 $self->chrom_length);
267 my $assembly = $self->assembly;
268 $str .= sprintf(" taxon_id=\"%s\" ncbi_asm=\"%s\" asm=\"%s\" ",
269 $assembly->taxon_id,
270 $assembly->ncbi_version,
271 $assembly->ucsc_name);
272 $str .= ">";
273 return $str;
274 }
275  
276 =head2 xml_end
277  
278 Description : overrides the superclass method.
279 returns the end tag of the XML description of this instance
280 Returntype : string
281 Exceptions : none
282  
283 =cut
284  
285 sub xml_end {
286 my $self = shift;
287 return "</chrom>\n";
288 }
289  
290 =head2 xml
291  
292 Description : overrides the superclass method.
293 returns the complete XML description of this instance
294 Returntype : string
295 Exceptions : none
296  
297 =cut
298  
299 sub xml {
300 my $self = shift;
301 return $self->simple_xml;
302 }
303  
304 =head2 get_subsequence
305  
306 Description : uses ChromChunk objects and the sequence in the database to
307 return the actual sequence in this region. Since the
308 Chrom is assigned to a specific Assembly one only needs to specify
309 the start/end and an optional strand to fetch the sequence.
310 Parameter[1] : chrom_start
311 the chromosome start of the region to fetch
312 Parameter[2] : chrom_end
313 the chromosome end of the region to fetch
314 Parameter[3] : strand <optional> as "-" or "+"
315 the strand of the sequence. if "-" then it will return the sequence
316 on the reverse strand by reverse complementing the sequence
317 Returntype : Bio::Seq instance or undef if a data error happens
318 Errors : if the region is not valid or if data is not present it will return undef
319 Exceptions : none
320  
321 =cut
322  
323 sub get_subsequence {
324 my $self = shift;
325 my $start = shift;
326 my $end = shift;
327 my $strand = shift; #optional
328
329 #must provide valid coordinates
330 if(!defined($start) or !defined($end)) { return undef; }
331 if($start < 0) { return undef; }
332 if($end < 0) { return undef; }
333 if($end > $self->chrom_length) { return undef; }
334 if($start > $end) { return undef; }
335
336 if(!defined($strand)) { $strand = "+"; }
337 my $asm_name = $self->assembly->ucsc_name;
338  
339 #fetch_all_named_region returns chunks in sorted order by chrom_start
340 my @chunks = @{EEDB::ChromChunk->fetch_all_named_region($self->database, $asm_name, $self->chrom_name, $start, $end)};
341 if(scalar(@chunks) == 0) { return undef; }
342  
343 my $name = sprintf("seq_%s::%s:%d..%d%s", $asm_name, $self->chrom_name, $start, $end, $strand);
344  
345 #first see if region fits entirely inside one chunk
346 #coord compare is much faster than string concats
347 for(my $i=0; $i<scalar(@chunks); $i++) {
348 my $chunk = $chunks[$i];
349 if(($start >= $chunk->chrom_start) and ($end <= $chunk->chrom_end)) {
350 my $bioseq = $chunk->get_subsequence($start, $end, $strand);
351 $bioseq->id($name);
352 return $bioseq;
353 }
354 }
355  
356 ## more than one chunk required to cover entire region so concat
357 ## keep track of how much has already been concatonated by $ts and loop
358 ## then do reverse complement at end
359 my $seq = "";
360 my $ts = $start; #keep running track of position
361 for(my $i=0; $i<scalar(@chunks); $i++) {
362 my $chunk = $chunks[$i];
363 my $te = $chunk->chrom_end;
364 if($end<$te) { $te = $end; }
365 $seq .= $chunk->get_subsequence($ts, $te)->seq;
366 $ts = $te+1;
367 if($ts > $end) { last; } #we are done
368 }
369  
370 my $bioseq = Bio::Seq->new(-id=>$name, -seq=>$seq);
371 if($strand eq '-') { $bioseq = $bioseq->revcom; }
372 return $bioseq;
373 }
374  
375  
376 #################################################
377 #
378 # DBObject override methods
379 #
380 #################################################
381  
382 =head2 store
383  
384 Description : store this instance into an EEDB database
385 on return the instance will have the primary_id() set.
386 Parameter[1] : a MQdb::Database to store into
387 Returntype : $self or undef if a problem occurred
388 Exceptions : none
389  
390 =cut
391  
392 sub store {
393 my $self = shift;
394 my $db = shift;
395
396 if($db) { $self->database($db); }
397 my $dbh = $self->database->get_connection;
398 my $sql = "INSERT ignore INTO chrom (
399 chrom_name,
400 chrom_type,
401 assembly_id,
402 chrom_length)
403 VALUES(?,?,?,?)";
404 my $sth = $dbh->prepare($sql);
405 $sth->execute($self->chrom_name,
406 $self->chrom_type,
407 $self->assembly->id,
408 $self->chrom_length);
409  
410 my $dbID = $sth->{'mysql_insertid'};
411 $sth->finish;
412 return undef unless($dbID);
413 $self->primary_id($dbID);
414 return $self;
415 }
416  
417 =head2 update
418  
419 Description : updates the data of this instance. require the Chrom to have been
420 fetched from database. It must have database() and primary_id()
421 Returntype : $self
422 Exceptions : none
423  
424 =cut
425  
426 sub update {
427 my $self = shift;
428
429 return undef unless($self->database and $self->id);
430
431 my $dbh = $self->database->get_connection;
432 my $sql = "UPDATE chrom set chrom_length=?, description=?, chrom_type=? where chrom_id=?";
433 my $sth = $dbh->prepare($sql);
434 $sth->execute($self->chrom_length,
435 $self->description,
436 $self->chrom_type,
437 $self->id);
438 return $self;
439 }
440  
441 ##### DBObject instance override methods #####
442  
443 #mapRow is an internal method used by the MappedQuery template machinery
444 sub mapRow {
445 my $self = shift;
446 my $rowHash = shift;
447 my $dbh = shift;
448  
449 $self->primary_id($rowHash->{'chrom_id'});
450 $self->chrom_name($rowHash->{'chrom_name'});
451 $self->chrom_length($rowHash->{'chrom_length'});
452 $self->chrom_type($rowHash->{'chrom_type'});
453
454 my $assembly = EEDB::Assembly->fetch_by_id($self->database, $rowHash->{'assembly_id'});
455 $self->assembly($assembly);
456
457 if($__riken_gsc_chrom_global_should_cache != 0) {
458 $__riken_gsc_chrom_global_id_cache->{$self->database() . $self->id} = $self;
459 $__riken_gsc_chrom_global_nameasm_cache->{$self->database() . $self->chrom_name. $rowHash->{'assembly_id'}} = $self;
460 }
461
462 return $self;
463 }
464  
465  
466 ##### public class methods for fetching by utilizing DBObject framework methods #####
467  
468 sub fetch_by_id {
469 my $class = shift;
470 my $db = shift;
471 my $id = shift;
472  
473 if($__riken_gsc_chrom_global_should_cache != 0) {
474 my $chrom = $__riken_gsc_chrom_global_id_cache->{$db . $id};
475 if(defined($chrom)) { return $chrom; }
476 }
477 my $sql = "SELECT * FROM chrom WHERE chrom_id=?";
478 return $class->fetch_single($db, $sql, $id);
479 }
480  
481 sub fetch_all {
482 my $class = shift;
483 my $db = shift;
484
485 my $sql = "SELECT * FROM chrom";
486 return $class->fetch_multiple($db, $sql);
487 }
488  
489 sub fetch_all_by_assembly {
490 my $class = shift;
491 my $assembly = shift; #Assembly object
492  
493 unless(defined($assembly) && $assembly->isa('EEDB::Assembly')) {
494 print('ERROR fetch_all_by_assembly() param must be a EEDB::Assembly');
495 return undef;
496 }
497 return undef unless($assembly->database);
498 my $sql = "SELECT * FROM chrom WHERE assembly_id=?";
499 return $class->fetch_multiple($assembly->database, $sql, $assembly->id);
500 }
501  
502 sub fetch_all_by_assembly_id {
503 my $class = shift;
504 my $db = shift;
505 my $assembly_id = shift;
506  
507 my $sql = "SELECT * FROM chrom WHERE assembly_id=?";
508 return $class->fetch_multiple($db, $sql, $assembly_id);
509 }
510  
511 sub fetch_all_by_assembly_name {
512 my $class = shift;
513 my $db = shift;
514 my $assembly_name = shift;
515  
516 my $sql = "SELECT * FROM chrom JOIN assembly USING(assembly_id) ".
517 "WHERE (ncbi_version=? or ucsc_name=?) ORDER BY chrom_length";
518 return $class->fetch_multiple($db, $sql, $assembly_name, $assembly_name);
519 }
520  
521 sub fetch_by_assembly_chrname {
522 my $class = shift;
523 my $assembly = shift; #Assembly object
524 my $chrname = shift;
525  
526 if(!defined($assembly) or !($assembly->isa('EEDB::Assembly'))) {
527 print('fetch_by_assembly_chrname() param must be a EEDB::Assembly');
528 return undef;
529 }
530  
531 my $db = $assembly->database;
532 if($__riken_gsc_chrom_global_should_cache != 0) {
533 my $chrom = $__riken_gsc_chrom_global_nameasm_cache->{$db . $chrname . $assembly->id};
534 if(defined($chrom)) { return $chrom; }
535 }
536  
537 my $sql = "SELECT * FROM chrom WHERE chrom_name=? and assembly_id=?";
538 return $class->fetch_single($db, $sql, $chrname, $assembly->id);
539 }
540  
541 sub fetch_by_name_assembly_id {
542 my $class = shift;
543 my $db = shift;
544 my $name = shift;
545 my $assembly_id = shift;
546  
547 if($__riken_gsc_chrom_global_should_cache != 0) {
548 my $chrom = $__riken_gsc_chrom_global_nameasm_cache->{$db . $name . $assembly_id};
549 if(defined($chrom)) { return $chrom; }
550 }
551  
552 my $sql = "SELECT * FROM chrom WHERE chrom_name=? and assembly_id=?";
553 return $class->fetch_single($db, $sql, $name, $assembly_id);
554 }
555  
556 sub fetch_by_name {
557 my $class = shift;
558 my $db = shift;
559 my $assembly_name = shift;
560 my $chrom_name = shift;
561  
562 my $sql = "SELECT * FROM chrom JOIN assembly USING(assembly_id) ".
563 "WHERE chrom_name=? and (ncbi_version=? or ucsc_name=?)";
564 return $class->fetch_single($db, $sql, $chrom_name, $assembly_name, $assembly_name);
565 }
566  
567 sub fetch_all_by_feature_source {
568 my $class = shift;
569 my $fsource = shift;
570  
571 my $chrom_hash = {};
572 my $stream = EEDB::Feature->stream_all_by_source($fsource);
573 while(my $feature = $stream->next_in_stream) {
574 #no need to worry about federated IDs here since the source must be on one database
575 my $chrom = $feature->chrom;
576 $chrom_hash->{$chrom->id} = $chrom;
577 }
578 my @chroms = values(%{$chrom_hash});
579 return \@chroms;
580 }
581  
582  
583 1;
584  
585