EEDB/Tools/PSLLoader.pm untested

filter: Statementstmt 0.0% 0 / 202 Branchbran 0.0% 0 / 76 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 337 CC 39 SCAR 45.5
CC
39
Cov
0%
load_features
306.0
feature_source
30.0
database
20.0
CRAP 94.2
1 =head1 NAME - EEDB::Tools::PSLLoader
2  
3 =head1 SYNOPSIS
4  
5 =head1 DESCRIPTION
6  
7 This is a processing class. Take two FeatureSource with features on chromosomes, streams both of them
8 and does a merge-sort-like comparison. Uses a call-out function to allow the user to 'do things'
9 with the feature pairs.
10  
11 =head1 CONTACT
12  
13 Jessica Severin <severin@gsc.riken.jp>
14  
15 =head1 LICENSE
16  
17 * Software License Agreement (BSD License)
18 * EdgeExpressDB [eeDB] system
19 * copyright (c) 2007-2009 Jessica Severin RIKEN OSC
20 * All rights reserved.
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions are met:
23 * * Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * * Redistributions in binary form must reproduce the above copyright
26 * notice, this list of conditions and the following disclaimer in the
27 * documentation and/or other materials provided with the distribution.
28 * * Neither the name of Jessica Severin RIKEN OSC nor the
29 * names of its contributors may be used to endorse or promote products
30 * derived from this software without specific prior written permission.
31 *
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
33 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
34 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
35 * DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS BE LIABLE FOR ANY
36 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
37 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
39 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
41 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42  
43 =head1 APPENDIX
44  
45 The rest of the documentation details each of the object methods. Internal methods are usually preceded with a _
46  
47 =cut
48  
49 $VERSION = 0.953;
50  
51 package EEDB::Tools::PSLLoader;
52  
53 use strict;
54 use EEDB::FeatureSource;
55 use EEDB::Feature;
56 use EEDB::Tools::MultiLoader;
57  
58 use Time::HiRes qw(time gettimeofday tv_interval);
59 use Compress::Zlib;
60  
61 use MQdb::DBObject;
62 our @ISA = qw(MQdb::DBObject);
63  
64 #################################################
65 # Class methods
66 #################################################
67  
68 sub class { return "EEDB::Tools::PSLLoader"; }
69  
70 #################################################
71 # Instance methods
72 #################################################
73  
74 sub init {
75 my $self = shift;
76 $self->SUPER::init;
77
78 $self->{'assembly'} = undef;
79 $self->{'category'} = "";
80 $self->{'store'} = 0;
81 $self->{'debug'} = 0;
82 $self->{"import_blocks"} = 0;
83 $self->{'display_interval'} = 250;
84 $self->{'create_chroms'} = 0;
85  
86 return $self;
87 }
88  
89  
90 ##########################
91 #
92 # getter/setter methods of data which is stored in database
93 #
94 ##########################
95  
96 sub database {
97 #overide
98 my $self = shift;
99 my $db = shift;
100 if(defined($db)) {
101 if(!($db->isa('MQdb::Database'))) { die("$db is not a MQdb::Database"); }
102 if($self->{'_database'} and ($db ne $self->{'_database'})) {
103 die("ERROR using multiple databases on one PSLLoader is not allowed");
104 }
105 $self->{'_database'} = $db;
106 }
107 return $self->{'_database'};
108 }
109  
110 sub assembly {
111 my ($self, $assembly) = @_;
112 if($assembly) {
113 unless(defined($assembly) && $assembly->isa('EEDB::Assembly')) {
114 die('assembly param must be a EEDB::Assembly');
115 }
116 $self->{'assembly'} = $assembly;
117 }
118 return $self->{'assembly'};
119 }
120  
121 sub source_name {
122 my $self = shift;
123 my $name = shift;
124
125 $self->{"category"} = "";
126 $self->{"src_name"} = $name;
127 if($name =~ /(\w+)\:\:(.+)/) {
128 $self->{"category"} = $1;
129 $self->{"src_name"} = $2;
130 }
131 return $self->{"src_name"};
132 }
133  
134 sub do_store {
135 my $self = shift;
136 return $self->{'store'} = shift if(@_);
137 $self->{'store'}=1 unless(defined($self->{'store'}));
138 return $self->{'store'};
139 }
140  
141 sub debug {
142 my $self = shift;
143 return $self->{'debug'} = shift if(@_);
144 return $self->{'debug'};
145 }
146  
147 sub import_blocks {
148 my $self = shift;
149 return $self->{'import_blocks'} = shift if(@_);
150 return $self->{'import_blocks'};
151 }
152  
153 sub create_chroms {
154 my $self = shift;
155 return $self->{'create_chroms'} = shift if(@_);
156 return $self->{'create_chroms'};
157 }
158  
159 sub display_interval {
160 my $self = shift;
161 return $self->{'display_interval'} = shift if(@_);
162 return $self->{'display_interval'};
163 }
164  
165 ###########
166  
167 sub display_desc {
168 #override superclass method
169 my $self = shift;
170 return sprintf("PSLLoader:: ");
171 }
172  
173 sub display_contents {
174 my $self = shift;
175 }
176  
177  
178 ###############################################################
179 # dynamic fetch/creation of sources as needed
180 #
181  
182 sub feature_source {
183 my $self = shift;
184 if(!defined($self->{"fsrc"})) {
185 if($self->{"category"}) {
186 $self->{"fsrc"} = EEDB::FeatureSource->fetch_by_category_name($self->database, $self->{"category"}, $self->{"src_name"});
187 } else {
188 $self->{"fsrc"} = EEDB::FeatureSource->fetch_by_name($self->database, $self->{"src_name"});
189 }
190 unless($self->{"fsrc"}){
191 $self->{"fsrc"} = new EEDB::FeatureSource;
192 $self->{"fsrc"}->name($self->{"src_name"});
193 $self->{"fsrc"}->category($self->{"category"});
194 $self->{"fsrc"}->import_source("");
195 $self->{"fsrc"}->store($self->database) if($self->{'store'});
196 printf("Needed to create:: ");
197 }
198 $self->{"fsrc"}->display_info;
199 }
200 return $self->{"fsrc"};
201 }
202  
203 sub block_source {
204 my $self = shift;
205 if(!defined($self->{"block_fsrc"})) {
206 my $block_name = $self->{"src_name"} . "_block";
207 $self->{"block_fsrc"} = EEDB::FeatureSource->fetch_by_category_name($self->database, "block", $block_name);
208 unless($self->{"block_fsrc"}){
209 $self->{"block_fsrc"} = new EEDB::FeatureSource;
210 $self->{"block_fsrc"}->category("block");
211 $self->{"block_fsrc"}->name($block_name);
212 $self->{"block_fsrc"}->import_source("");
213 $self->{"block_fsrc"}->store($self->database) if($self->{'store'});
214 printf("Needed to create:: ");
215 }
216 $self->{"block_fsrc"}->display_info;
217 }
218 return $self->{"block_fsrc"};
219 }
220  
221 sub sublink_source {
222 my $self = shift;
223 if(!defined($self->{'subfeature_lsrc'})) {
224 my $link_name = $self->{"src_name"} . "_subfeature";
225 $self->{'subfeature_lsrc'} = EEDB::EdgeSource->fetch_by_name($self->database, $link_name);
226 unless($self->{'subfeature_lsrc'}){
227 $self->{'subfeature_lsrc'} = new EEDB::EdgeSource;
228 $self->{'subfeature_lsrc'}->category("subfeature");
229 $self->{'subfeature_lsrc'}->name($link_name);
230 $self->{'subfeature_lsrc'}->store($self->database) if($self->{'store'});
231 printf("Needed to create:: ");
232 }
233 $self->{'subfeature_lsrc'}->display_info;
234 }
235 return $self->{'subfeature_lsrc'};
236 }
237  
238  
239 #########################################################################################
240  
241 sub load_features {
242 my $self = shift;
243 my $file = shift;
244
245 printf("\n==============\n");
246 my $starttime = time();
247 my $linecount=0;
248
249 my $multiLoad = new EEDB::Tools::MultiLoader;
250 $multiLoad->database($self->database);
251 $multiLoad->do_store($self->{'store'});
252  
253 my $gz = gzopen($file, "rb") ;
254 my $line;
255 while(my $bytesread = $gz->gzreadline($line)) {
256 chomp($line);
257 next if($line =~ /^\#/);
258 next if($line =~ /^track/);
259 $linecount++;
260 $line =~ s/\r//g;
261 printf("LINE: $line\n") if($self->debug>1);
262  
263 # PSL format columns. here we assume that the PSL is mapping some query to a 'target' genome
264 #
265 #0. matches - Number of bases that match that aren't repeats
266 #1. misMatches - Number of bases that don't match
267 #2. repMatches - Number of bases that match but are part of repeats
268 #3. nCount - Number of 'N' bases
269 #4. qNumInsert - Number of inserts in query
270 #5. qBaseInsert - Number of bases inserted in query
271 #6. tNumInsert - Number of inserts in target
272 #7. tBaseInsert - Number of bases inserted in target
273 #8. strand - '+' or '-' for query strand. For translated alignments, second '+'or '-' is for genomic strand
274 #9. qName - Query sequence name
275 #10. qSize - Query sequence size
276 #11. qStart - Alignment start position in query
277 #12. qEnd - Alignment end position in query
278 #13. tName - Target sequence name
279 #14. tSize - Target sequence size
280 #15. tStart - Alignment start position in target
281 #16. tEnd - Alignment end position in target
282 #17. blockCount - Number of blocks in the alignment (a block contains no gaps)
283 #18. blockSizes - Comma-separated list of sizes of each block
284 #19. qStarts - Comma-separated list of starting positions of each block in query
285 #20. tStarts - Comma-separated list of starting positions of each block in target (in target coords not offset)
286
287 my @columns = split(/\t/, $line);
288 my $strand = $columns[8];
289 my $name = $columns[9];
290 my $chrname = $columns[13];
291 my $start = $columns[15];
292 my $end = $columns[16];
293 my $blockCount = $columns[17];
294 my $blockSizes = $columns[18];
295 my $blockStarts = $columns[20];
296
297 my @block_size_array = split(/,/, $blockSizes) if($blockCount);
298 my @block_start_array = split(/,/, $blockStarts) if($blockCount);
299
300 if($start>$end) {
301 my $t=$start;
302 $start = $end;
303 $end = $t;
304 }
305 #PSL format is 0 reference and eeDB is 1 referenced
306 #because PSL is not-inclusive, but eeDB is inclusive I do not need to +1 to the end
307 $start += 1;
308
309 my $chrom = EEDB::Chrom->fetch_by_assembly_chrname($self->assembly, $chrname);
310 if(!$chrom and $self->{'create_chroms'}) { #create the chromosome;
311 $chrom = new EEDB::Chrom;
312 $chrom->chrom_name($chrname);
313 $chrom->assembly($self->assembly);
314 $chrom->chrom_type('chromosome');
315 $chrom->store($self->database) if($self->{'store'});
316 printf("need to create chromosome :: %s\n", $chrom->display_desc);
317 }
318 if(!$chrom) { next; }
319
320 my $feature = new EEDB::Feature;
321 $feature->feature_source($self->feature_source);
322 $feature->chrom($chrom);
323 $feature->chrom_start($start);
324 $feature->chrom_end($end);
325 #optional columns now
326 if($strand) { $feature->strand($strand); }
327 if($name) {
328 $feature->primary_name($name);
329 $feature->metadataset->add_tag_symbol($self->feature_source->category, $name);
330 }
331  
332 $multiLoad->store_feature($feature);
333 $feature->display_info if($self->debug);
334
335 if($blockCount and $self->{"import_blocks"}) {
336 for(my $i=0; $i<$blockCount; $i++) {
337 my $bstart = $block_start_array[$i] + 1;
338 my $bsize = $block_size_array[$i];
339
340 my $subfeat = new EEDB::Feature;
341 $subfeat->feature_source($self->block_source);
342 $subfeat->primary_name($name . "_block". ($i+1));
343 $subfeat->chrom($chrom);
344 $subfeat->chrom_start($bstart);
345 $subfeat->chrom_end($bstart + $bsize - 1); #eedb is inclusive to must -1 (1234..1234 is size=1)
346 $subfeat->strand($strand);
347 $multiLoad->store_feature($subfeat);
348 printf(" %s\n", $subfeat->display_desc) if($self->debug);
349  
350 my $edge = new EEDB::Edge;
351 $edge->edge_source($self->sublink_source);
352 $edge->feature1($subfeat);
353 $edge->feature2($feature);
354 $multiLoad->store_edge($edge);
355 printf(" %s\n", $edge->display_desc) if($self->debug>2);
356 }
357 }
358  
359  
360 if($linecount % $self->{"display_interval"} == 0) {
361 my $rate = $linecount / (time() - $starttime);
362 printf("%10d (%1.2f x/sec): %s\n", $linecount, $rate, $feature->display_desc);
363 }
364 #last;
365 }
366 $gz->gzclose();
367
368 #to flush the MultiLoader buffers
369 $multiLoad->store_feature();
370 $multiLoad->store_edge();
371  
372 my $total_time = time() - $starttime;
373 my $rate = $linecount / $total_time;
374 printf("TOTAL: %10d :: %1.3f min :: %1.2f x/sec\n", $linecount, $total_time/60.0, $rate);
375  
376 }
377  
378  
379 1;
380