EEDB/Tools/BEDLoader.pm untested

filter: Statementstmt 0.0% 0 / 287 Branchbran 0.0% 0 / 128 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 26 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 495 CC 65 SCAR 60.4
CC
65
Cov
0%
load_features
1122.0
feature_source
30.0
sync_importdates
30.0
CRAP 420.9
1 =head1 NAME - EEDB::Tools::BEDLoader
2  
3 =head1 SYNOPSIS
4  
5 =head1 DESCRIPTION
6  
7 This is a simple class to wrap the functionality of BED file parsing and
8 conversion into eeDB objects into a nice modular component.
9  
10 =head1 CONTACT
11  
12 Jessica Severin <severin@gsc.riken.jp>
13  
14 =head1 LICENSE
15  
16 * Software License Agreement (BSD License)
17 * EdgeExpressDB [eeDB] system
18 * copyright (c) 2007-2009 Jessica Severin RIKEN OSC
19 * All rights reserved.
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions are met:
22 * * Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * * Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
27 * * Neither the name of Jessica Severin RIKEN OSC nor the
28 * names of its contributors may be used to endorse or promote products
29 * derived from this software without specific prior written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
32 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
33 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
34 * DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS BE LIABLE FOR ANY
35 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
38 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
40 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41  
42 =head1 APPENDIX
43  
44 The rest of the documentation details each of the object methods. Internal methods are usually preceded with a _
45  
46 =cut
47  
48 $VERSION = 0.953;
49  
50 package EEDB::Tools::BEDLoader;
51  
52 use strict;
53 use EEDB::FeatureSource;
54 use EEDB::Feature;
55 use EEDB::Tools::MultiLoader;
56  
57 use Time::HiRes qw(time gettimeofday tv_interval);
58 use Compress::Zlib;
59  
60 use MQdb::DBObject;
61 our @ISA = qw(MQdb::DBObject);
62  
63 #################################################
64 # Class methods
65 #################################################
66  
67 sub class { return "EEDB::Tools::BEDLoader"; }
68  
69 #################################################
70 # Instance methods
71 #################################################
72  
73 sub init {
74 my $self = shift;
75 $self->SUPER::init;
76
77 $self->{'assembly'} = undef;
78 $self->{'category'} = "";
79 $self->{'store'} = 0;
80 $self->{'debug'} = 0;
81 $self->{"import_blocks"} = 0;
82 $self->{'display_interval'} = 250;
83 $self->{'create_chroms'} = 1;
84  
85 return $self;
86 }
87  
88  
89 ##########################
90 #
91 # getter/setter methods of data which is stored in database
92 #
93 ##########################
94  
95 sub database {
96 #overide
97 my $self = shift;
98 my $db = shift;
99 if(defined($db)) {
100 if(!($db->isa('MQdb::Database'))) { die("$db is not a MQdb::Database"); }
101 if($self->{'_database'} and ($db ne $self->{'_database'})) {
102 die("ERROR using multiple databases on one BEDLoader is not allowed");
103 }
104 $self->{'_database'} = $db;
105 }
106 return $self->{'_database'};
107 }
108  
109 sub assembly {
110 my ($self, $assembly) = @_;
111 if($assembly) {
112 unless(defined($assembly) && $assembly->isa('EEDB::Assembly')) {
113 die('assembly param must be a EEDB::Assembly');
114 }
115 $self->{'assembly'} = $assembly;
116 }
117 return $self->{'assembly'};
118 }
119  
120 sub source_name {
121 my $self = shift;
122 my $name = shift;
123
124 $self->{"category"} = "";
125 $self->{"src_name"} = $name;
126 if($name =~ /(\w+)\:\:(.+)/) {
127 $self->{"category"} = $1;
128 $self->{"src_name"} = $2;
129 }
130 return $self->{"src_name"};
131 }
132  
133 sub do_store {
134 my $self = shift;
135 return $self->{'store'} = shift if(@_);
136 $self->{'store'}=1 unless(defined($self->{'store'}));
137 return $self->{'store'};
138 }
139  
140 sub debug {
141 my $self = shift;
142 return $self->{'debug'} = shift if(@_);
143 return $self->{'debug'};
144 }
145  
146 sub import_blocks {
147 my $self = shift;
148 return $self->{'import_blocks'} = shift if(@_);
149 return $self->{'import_blocks'};
150 }
151  
152 sub create_chroms {
153 my $self = shift;
154 return $self->{'create_chroms'} = shift if(@_);
155 return $self->{'create_chroms'};
156 }
157  
158 sub display_interval {
159 my $self = shift;
160 return $self->{'display_interval'} = shift if(@_);
161 return $self->{'display_interval'};
162 }
163  
164 ###########
165  
166 sub display_desc {
167 #override superclass method
168 my $self = shift;
169 return sprintf("BEDLoader:: ");
170 }
171  
172 sub display_contents {
173 my $self = shift;
174 }
175  
176  
177 ###############################################################
178 # dynamic fetch/creation of sources as needed
179 #
180  
181 sub feature_source {
182 my $self = shift;
183 if(!defined($self->{"fsrc"})) {
184 if($self->{"category"}) {
185 $self->{"fsrc"} = EEDB::FeatureSource->fetch_by_category_name($self->database, $self->{"category"}, $self->{"src_name"});
186 } else {
187 $self->{"fsrc"} = EEDB::FeatureSource->fetch_by_name($self->database, $self->{"src_name"});
188 }
189 unless($self->{"fsrc"}){
190 $self->{"fsrc"} = new EEDB::FeatureSource;
191 $self->{"fsrc"}->name($self->{"src_name"});
192 $self->{"fsrc"}->category($self->{"category"});
193 $self->{"fsrc"}->import_source("");
194 $self->{"fsrc"}->is_active("y");
195 $self->{"fsrc"}->is_visible("y");
196 $self->{"fsrc"}->store($self->database) if($self->{'store'});
197 printf("Needed to create:: ");
198 }
199 $self->{"fsrc"}->display_info;
200 }
201 return $self->{"fsrc"};
202 }
203  
204 sub exon_source {
205 my $self = shift;
206 if(!defined($self->{"exon_fsrc"})) {
207 my $exon_name = $self->{"src_name"} . "_exon";
208 $self->{"exon_fsrc"} = EEDB::FeatureSource->fetch_by_category_name($self->database, "exon", $exon_name);
209 unless($self->{"exon_fsrc"}){
210 $self->{"exon_fsrc"} = new EEDB::FeatureSource;
211 $self->{"exon_fsrc"}->category("exon");
212 $self->{"exon_fsrc"}->name($exon_name);
213 $self->{"exon_fsrc"}->import_source("");
214 $self->{"exon_fsrc"}->is_active("y");
215 $self->{"exon_fsrc"}->is_visible("y");
216 $self->{"exon_fsrc"}->store($self->database) if($self->{'store'});
217 printf("Needed to create:: ");
218 }
219 $self->{"exon_fsrc"}->display_info;
220 }
221 return $self->{"exon_fsrc"};
222 }
223  
224 sub sublink_source {
225 my $self = shift;
226 if(!defined($self->{'subfeature_lsrc'})) {
227 my $link_name = $self->{"src_name"} . "_subfeature";
228 $self->{'subfeature_lsrc'} = EEDB::EdgeSource->fetch_by_name($self->database, $link_name);
229 unless($self->{'subfeature_lsrc'}){
230 $self->{'subfeature_lsrc'} = new EEDB::EdgeSource;
231 $self->{'subfeature_lsrc'}->category("subfeature");
232 $self->{'subfeature_lsrc'}->name($link_name);
233 $self->{"subfeature_lsrc"}->is_active("y");
234 $self->{"subfeature_lsrc"}->is_visible("y");
235 $self->{'subfeature_lsrc'}->store($self->database) if($self->{'store'});
236 printf("Needed to create:: ");
237 }
238 $self->{'subfeature_lsrc'}->display_info;
239 }
240 return $self->{'subfeature_lsrc'};
241 }
242  
243 sub utr3_source {
244 my $self = shift;
245 if(!defined($self->{"utr3_fsrc"})) {
246 my $utr3_name = $self->{"src_name"} . "_3utr";
247 $self->{"utr3_fsrc"} = EEDB::FeatureSource->fetch_by_category_name($self->database, "3utr", $utr3_name);
248 unless($self->{"utr3_fsrc"}){
249 $self->{"utr3_fsrc"} = new EEDB::FeatureSource;
250 $self->{"utr3_fsrc"}->category("3utr");
251 $self->{"utr3_fsrc"}->name($utr3_name);
252 $self->{"utr3_fsrc"}->import_source("");
253 $self->{"utr3_fsrc"}->is_active("y");
254 $self->{"utr3_fsrc"}->is_visible("y");
255 $self->{"utr3_fsrc"}->store($self->database) if($self->{'store'});
256 printf("Needed to create:: ");
257 }
258 $self->{"utr3_fsrc"}->display_info;
259 }
260 return $self->{"utr3_fsrc"};
261 }
262  
263 sub utr5_source {
264 my $self = shift;
265 if(!defined($self->{"utr5_fsrc"})) {
266 my $utr5_name = $self->{"src_name"} . "_5utr";
267 $self->{"utr5_fsrc"} = EEDB::FeatureSource->fetch_by_category_name($self->database, "5utr", $utr5_name);
268 unless($self->{"utr5_fsrc"}){
269 $self->{"utr5_fsrc"} = new EEDB::FeatureSource;
270 $self->{"utr5_fsrc"}->category("5utr");
271 $self->{"utr5_fsrc"}->name($utr5_name);
272 $self->{"utr5_fsrc"}->import_source("");
273 $self->{"utr5_fsrc"}->is_active("y");
274 $self->{"utr5_fsrc"}->is_visible("y");
275 $self->{"utr5_fsrc"}->store($self->database) if($self->{'store'});
276 printf("Needed to create:: ");
277 }
278 $self->{"utr5_fsrc"}->display_info;
279 }
280 return $self->{"utr5_fsrc"};
281 }
282  
283 sub sync_importdates {
284 my $self = shift;
285
286 if(defined($self->{"fsrc"})) { $self->{"fsrc"}->sync_importdate_to_features; }
287 if(defined($self->{"exon_fsrc"})) { $self->{"exon_fsrc"}->sync_importdate_to_features; }
288 if(defined($self->{"utr3_fsrc"})) { $self->{"utr3_fsrc"}->sync_importdate_to_features; }
289 if(defined($self->{"utr5_fsrc"})) { $self->{"utr5_fsrc"}->sync_importdate_to_features; }
290 }
291  
292 #########################################################################################
293  
294 sub load_features {
295 my $self = shift;
296 my $file = shift;
297
298 printf("\n==============\n");
299 my $starttime = time();
300 my $linecount=0;
301
302 my $multiLoad = new EEDB::Tools::MultiLoader;
303 $multiLoad->database($self->database);
304 $multiLoad->do_store($self->{'store'});
305  
306 my $gz = gzopen($file, "rb") ;
307 my $line;
308 while(my $bytesread = $gz->gzreadline($line)) {
309 chomp($line);
310 next if($line =~ /^\#/);
311 $linecount++;
312 $line =~ s/\r//g;
313 printf("LINE: $line\n") if($self->debug>1);
314  
315 #chr1 134212714 134230065 NM_028778 0 + 134212806 134228958 0 7 335,121,152,66,120,133,2168, 0,8815,11559,11993,13820,14421,15183,
316  
317 #1. chrom - The name of the chromosome (e.g. chr3, chrY, chr2_random) or scaffold (e.g. scaffold10671).
318 #2. chromStart - The starting position of the feature in the chromosome or scaffold. The first base in a chromosome is numbered 0.
319 #3. chromEnd - The ending position of the feature in the chromosome or scaffold. The chromEnd base is not included in the display of the feature. For example, the first 100 bases of a chromosome are defined as chromStart=0, chromEnd=100, and span the bases numbered 0-99.
320 #4. name - Defines the name of the BED line. This label is displayed to the left of the BED line in the Genome Browser window when the track is open to full display mode or directly to the left of the item in pack mode.
321 #5. score - A score between 0 and 1000. If the track line useScore attribute is set to 1 for this annotation data set, the score value will determine the level of gray in which this feature is displayed (higher numbers = darker gray).
322 #6. strand - Defines the strand - either '+' or '-'.
323 #7. thickStart - The starting position at which the feature is drawn thickly (for example, the start codon in gene displays).
324 #8. thickEnd - The ending position at which the feature is drawn thickly (for example, the stop codon in gene displays).
325 #9. itemRgb - An RGB value of the form R,G,B (e.g. 255,0,0). If the track line itemRgb attribute is set to "On", this RBG value will determine the display color of the data contained in this BED line. NOTE: It is recommended that a simple color scheme (eight colors or less) be used with this attribute to avoid overwhelming the color resources of the Genome Browser and your Internet browser.
326 #10. blockCount - The number of blocks (exons) in the BED line.
327 #11. blockSizes - A comma-separated list of the block sizes. The number of items in this list should correspond to blockCount.
328 #12. blockStarts - A comma-separated list of block starts. All of the blockStart positions should be calculated relative to chromStart. The number of items in this list should correspond to blockCount.
329  
330 my ($chrname, $start, $end, $name, $score, $strand, $thickStart, $thickEnd, $rgb, $blockCount, $blockSizes, $blockStarts) = split(/\t/, $line);
331
332 my @block_size_array = split(/,/, $blockSizes) if($blockCount);
333 my @block_start_array = split(/,/, $blockStarts) if($blockCount);
334
335 if($start>$end) {
336 my $t=$start;
337 $start = $end;
338 $end = $t;
339 }
340 #bed format is 0 reference and eeDB is 1 referenced
341 #because bed is not-inclusive, but eeDB is inclusive I do not need to +1 to the end
342 $start += 1;
343 $thickStart += 1;
344
345 my $chrom = EEDB::Chrom->fetch_by_assembly_chrname($self->assembly, $chrname);
346 unless($chrom and $self->{'create_chroms'}) { #create the chromosome;
347 $chrom = new EEDB::Chrom;
348 $chrom->chrom_name($chrname);
349 $chrom->assembly($self->assembly);
350 $chrom->chrom_type('chromosome');
351 $chrom->store($self->database) if($self->{'store'});
352 printf("need to create chromosome :: %s\n", $chrom->display_desc);
353 }
354 if(!$chrom) { next; }
355
356 my $feature = new EEDB::Feature;
357 $feature->feature_source($self->feature_source);
358 $feature->chrom($chrom);
359 $feature->chrom_start($start);
360 $feature->chrom_end($end);
361 #optional columns now
362 if($strand) { $feature->strand($strand); }
363 if($score and ($score ne '.')) { $feature->significance($score); }
364 if($name) {
365 $feature->primary_name($name);
366 $feature->metadataset->add_tag_symbol($self->feature_source->category, $name);
367 }
368  
369 $multiLoad->store_feature($feature);
370 $feature->display_info if($self->debug);
371
372 if($blockCount and $self->{"import_blocks"}) {
373 for(my $i=0; $i<$blockCount; $i++) {
374 my $bstart = $block_start_array[$i];
375 my $bsize = $block_size_array[$i];
376
377 my $subfeat = new EEDB::Feature;
378 $subfeat->feature_source($self->exon_source);
379 $subfeat->primary_name($name . "_block". ($i+1));
380 $subfeat->chrom($chrom);
381 $subfeat->chrom_start($start + $bstart);
382 $subfeat->chrom_end($start + $bstart + $bsize - 1);
383 $subfeat->strand($strand);
384 $multiLoad->store_feature($subfeat);
385 printf(" %s\n", $subfeat->display_desc) if($self->debug);
386  
387 my $edge = new EEDB::Edge;
388 $edge->edge_source($self->sublink_source);
389 $edge->feature1($subfeat);
390 $edge->feature2($feature);
391 $multiLoad->store_edge($edge);
392 printf(" %s\n", $edge->display_desc) if($self->debug>2);
393
394 if(($thickStart > $start) and ($thickStart >= $subfeat->chrom_start)) {
395 my $uend = $subfeat->chrom_end;
396 if($thickStart < $uend) { $uend = $thickStart; }
397  
398 my $utr = new EEDB::Feature;
399 $utr->chrom($chrom);
400 $utr->chrom_start($subfeat->chrom_start);
401 $utr->chrom_end($uend);
402 $utr->strand($strand) if($strand);
403 if($strand eq '-') {
404 $utr->feature_source($self->utr3_source);
405 $utr->primary_name($name . "_3utr") if($name);
406 } else {
407 $utr->feature_source($self->utr5_source);
408 $utr->primary_name($name . "_5utr") if($name);
409 }
410 $multiLoad->store_feature($utr);
411 printf(" %s\n", $utr->display_desc) if($self->debug);
412
413 my $edge = new EEDB::Edge;
414 $edge->edge_source($self->sublink_source);
415 $edge->feature1($utr);
416 $edge->feature2($feature);
417 $multiLoad->store_edge($edge);
418 printf(" %s\n", $edge->display_desc) if($self->debug>2);
419 }
420 if(($thickEnd < $end) and ($thickEnd <= $subfeat->chrom_end)) {
421 my $ustart = $subfeat->chrom_start;
422 if($thickEnd > $ustart) { $ustart = $thickEnd; }
423  
424 my $utr = new EEDB::Feature;
425 $utr->chrom($chrom);
426 $utr->chrom_start($ustart);
427 $utr->chrom_end($subfeat->chrom_end);
428 $utr->strand($strand) if($strand);
429 if($strand eq '-') {
430 $utr->feature_source($self->utr5_source);
431 $utr->primary_name($name . "_5utr") if($name);
432 } else {
433 $utr->feature_source($self->utr3_source);
434 $utr->primary_name($name . "_3utr") if($name);
435 }
436 $multiLoad->store_feature($utr);
437 printf(" %s\n", $utr->display_desc) if($self->debug);
438  
439 my $edge = new EEDB::Edge;
440 $edge->edge_source($self->sublink_source);
441 $edge->feature1($utr);
442 $edge->feature2($feature);
443 $multiLoad->store_edge($edge);
444 printf(" %s\n", $edge->display_desc) if($self->debug>2);
445 }
446
447 }
448 }
449  
450  
451 if($linecount % $self->{"display_interval"} == 0) {
452 my $rate = $linecount / (time() - $starttime);
453 printf("%10d (%1.2f x/sec): %s\n", $linecount, $rate, $feature->display_desc);
454 }
455 #last;
456 }
457 $gz->gzclose();
458
459 $multiLoad->flush_buffers();
460
461 $self->sync_importdates;
462  
463 my $total_time = time() - $starttime;
464 my $rate = $linecount / $total_time;
465 printf("TOTAL: %10d :: %1.3f min :: %1.2f x/sec\n", $linecount, $total_time/60.0, $rate);
466  
467 }
468  
469  
470 1;
471