| 1 | =head1 NAME - EEDB::Tools::OverlapCompare | ||
| 2 | |||
| 3 | =head1 SYNOPSIS | ||
| 4 | |||
| 5 | =head1 DESCRIPTION | ||
| 6 | |||
| 7 | This is a general purpose processing tool designed to do genome spatial comparisons. | ||
| 8 | It is configured with a set of feature sources and parameters and call-back functions. | ||
| 9 | It is then fed a stream of features sorted by chromsome location. given the overlaps it will | ||
| 10 | then call the functions which the user can defined to "do things". | ||
| 11 | It is based on a modified merge-sort-like comparison algorithm. | ||
| 12 | |||
| 13 | =head1 CONTACT | ||
| 14 | |||
| 15 | Jessica Severin <severin@gsc.riken.jp> | ||
| 16 | |||
| 17 | =head1 LICENSE | ||
| 18 | |||
| 19 | * Software License Agreement (BSD License) | ||
| 20 | * EdgeExpressDB [eeDB] system | ||
| 21 | * copyright (c) 2007-2009 Jessica Severin RIKEN OSC | ||
| 22 | * All rights reserved. | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions are met: | ||
| 25 | * * Redistributions of source code must retain the above copyright | ||
| 26 | * notice, this list of conditions and the following disclaimer. | ||
| 27 | * * Redistributions in binary form must reproduce the above copyright | ||
| 28 | * notice, this list of conditions and the following disclaimer in the | ||
| 29 | * documentation and/or other materials provided with the distribution. | ||
| 30 | * * Neither the name of Jessica Severin RIKEN OSC nor the | ||
| 31 | * names of its contributors may be used to endorse or promote products | ||
| 32 | * derived from this software without specific prior written permission. | ||
| 33 | * | ||
| 34 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY | ||
| 35 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
| 36 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
| 37 | * DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
| 38 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
| 39 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
| 40 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
| 41 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| 42 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
| 43 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 44 | |||
| 45 | =head1 APPENDIX | ||
| 46 | |||
| 47 | The rest of the documentation details each of the object methods. Internal methods are usually preceded with a _ | ||
| 48 | |||
| 49 | =cut | ||
| 50 | |||
| 51 | $VERSION = 0.953; | ||
| 52 | |||
| 53 | package EEDB::Tools::OverlapCompare; | ||
| 54 | |||
| 55 | use strict; | ||
| 56 | use EEDB::FeatureSource; | ||
| 57 | use EEDB::Feature; | ||
| 58 | use Time::HiRes qw(time gettimeofday tv_interval); | ||
| 59 | |||
| 60 | use MQdb::DBObject; | ||
| 61 | our @ISA = qw(MQdb::DBObject); | ||
| 62 | |||
| 63 | ################################################# | ||
| 64 | # Class methods | ||
| 65 | ################################################# | ||
| 66 | |||
| 67 | sub class { return "EEDB::Tools::OverlapCompare"; } | ||
| 68 | |||
| 69 | ################################################# | ||
| 70 | # Instance methods | ||
| 71 | ################################################# | ||
| 72 | |||
| 73 | sub init { | ||
| 74 | my $self = shift; | ||
| 75 | $self->SUPER::init; | ||
| 76 | |||
| 77 | $self->{'compare_mode'} = 'overlap'; #other is 'within' | ||
| 78 | $self->{'upstream_distance'} = 0; | ||
| 79 | $self->{'downstream_distance'} = 0; | ||
| 80 | |||
| 81 | $self->{'assembly'} = undef; | ||
| 82 | $self->{'feature_source_array'} = []; | ||
| 83 | $self->{'experiment_array'} = []; | ||
| 84 | $self->{'expression_mode_type_array'} = undef; | ||
| 85 | |||
| 86 | $self->{'current_chrom_name'}= undef; | ||
| 87 | $self->{'current_stream'}= undef; | ||
| 88 | $self->{'feature_buffer'}= []; | ||
| 89 | |||
| 90 | $self->{'debug'} = 0; | ||
| 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 database { | ||
| 103 | #overide | ||
| 104 | my $self = shift; | ||
| 105 | my $db = shift; | ||
| 106 | if(defined($db)) { | ||
| 107 | if(!($db->isa('MQdb::Database'))) { die("$db is not a MQdb::Database"); } | ||
| 108 | if($self->{'_database'} and ($db ne $self->{'_database'})) { | ||
| 109 | die("ERROR using multiple databases on one OverlapCompare is not allowed"); | ||
| 110 | } | ||
| 111 | $self->{'_database'} = $db; | ||
| 112 | } | ||
| 113 | return $self->{'_database'}; | ||
| 114 | } | ||
| 115 | |||
| 116 | sub assembly { | ||
| 117 | my ($self, $assembly) = @_; | ||
| 118 | if($assembly) { | ||
| 119 | unless(defined($assembly) && $assembly->isa('EEDB::Assembly')) { | ||
| 120 | die('assembly param must be a EEDB::Assembly'); | ||
| 121 | } | ||
| 122 | $self->{'assembly'} = $assembly; | ||
| 123 | $self->database($assembly->database); | ||
| 124 | } | ||
| 125 | return $self->{'assembly'}; | ||
| 126 | } | ||
| 127 | |||
| 128 | sub add_experiment { | ||
| 129 | my ($self, $experiment) = @_; | ||
| 130 | unless(defined($experiment) && $experiment->isa('EEDB::Experiment')) { | ||
| 131 | die('add_experiment param must be a EEDB::Experiment'); | ||
| 132 | } | ||
| 133 | push @{$self->{'experiment_array'}}, $experiment; | ||
| 134 | $self->database($experiment->database); | ||
| 135 | return $self; | ||
| 136 | } | ||
| 137 | |||
| 138 | sub add_feature_source { | ||
| 139 | my ($self, $source) = @_; | ||
| 140 | unless(defined($source) && $source->isa('EEDB::FeatureSource')) { | ||
| 141 | die('add_feature_source param must be a EEDB::FeatureSource'); | ||
| 142 | } | ||
| 143 | push @{$self->{'feature_source_array'}}, $source; | ||
| 144 | $self->database($source->database); | ||
| 145 | return $self; | ||
| 146 | } | ||
| 147 | |||
| 148 | sub expression_mode_type { | ||
| 149 | my $self = shift; | ||
| 150 | return $self->{'expression_mode_type'} = shift if(@_); | ||
| 151 | return $self->{'expression_mode_type'}; | ||
| 152 | } | ||
| 153 | |||
| 154 | sub upstream_distance { | ||
| 155 | my $self = shift; | ||
| 156 | return $self->{'upstream_distance'} = shift if(@_); | ||
| 157 | return $self->{'upstream_distance'}; | ||
| 158 | } | ||
| 159 | |||
| 160 | sub downstream_distance { | ||
| 161 | my $self = shift; | ||
| 162 | return $self->{'downstream_distance'} = shift if(@_); | ||
| 163 | return $self->{'downstream_distance'}; | ||
| 164 | } | ||
| 165 | |||
| 166 | |||
| 167 | ## to set an external function like sub my_func { .... } | ||
| 168 | ## do call_out_function(\&my_func); | ||
| 169 | sub overlap_function { | ||
| 170 | my $self = shift; | ||
| 171 | if(@_) { $self->{'overlap_function'} = shift; } | ||
| 172 | return $self->{'overlap_function'}; | ||
| 173 | } | ||
| 174 | |||
| 175 | sub between_function { | ||
| 176 | my $self = shift; | ||
| 177 | if(@_) { $self->{'between_function'} = shift; } | ||
| 178 | return $self->{'between_function'}; | ||
| 179 | } | ||
| 180 | |||
| 181 | |||
| 182 | sub display_desc { | ||
| 183 | #override superclass method | ||
| 184 | my $self = shift; | ||
| 185 | return sprintf("OverlapCompare:: "); | ||
| 186 | } | ||
| 187 | |||
| 188 | sub display_contents { | ||
| 189 | my $self = shift; | ||
| 190 | } | ||
| 191 | |||
| 192 | |||
| 193 | ############################################################### | ||
| 194 | |||
| 195 | # The input feature is expected to come in as a chrom_start sorted list | ||
| 196 | # all on the same chromosome, when the input stream changes chrom | ||
| 197 | # then the processing stream will also change | ||
| 198 | sub stream_process_feature { | ||
| 199 | my $self = shift; | ||
| 200 | my $feature1 = shift; | ||
| 201 | |||
| 202 | my $f2db = $self->database; | ||
| 203 | |||
| 204 | return undef unless($feature1->chrom); | ||
| 205 | printf("\nFEATURE: %s [%s]\n", $feature1->chrom_location, $feature1->primary_name) if($self->{'debug'}>1); | ||
| 206 | |||
| 207 | if(!defined($self->{'current_chrom_name'}) or ($feature1->chrom_name ne $self->{'current_chrom_name'})) { | ||
| 208 | #change the chrom and refresh the buffers | ||
| 209 | my $assembly2 = EEDB::Assembly->fetch_by_name($f2db, $feature1->chrom->assembly->ucsc_name); | ||
| 210 | my $chrom2 = EEDB::Chrom->fetch_by_name_assembly_id($f2db, $feature1->chrom_name, $assembly2->id); | ||
| 211 | return undef unless($chrom2); | ||
| 212 | |||
| 213 | my $stream2; | ||
| 214 | if(!defined($self->expression_mode_type)) { | ||
| 215 | my @sources = @{$self->{'feature_source_array'}}; | ||
| 216 | $stream2 = EEDB::Feature->stream_by_chrom($chrom2, sources=>\@sources); | ||
| 217 | } else { | ||
| 218 | my $type = $self->expression_mode_type; | ||
| 219 | if($type eq "all") { $type = undef; } | ||
| 220 | my @sources = @{$self->{'feature_source_array'}}; | ||
| 221 | my @exps = @{$self->{'experiment_array'}}; | ||
| 222 | $stream2 = EEDB::Expression->stream_by_chrom($chrom2, datatypes=>[$type], sources=>\@sources, experiments=>\@exps); | ||
| 223 | } | ||
| 224 | |||
| 225 | $self->{'current_chrom_name'}= $feature1->chrom_name; | ||
| 226 | $self->{'current_stream'}= $stream2; | ||
| 227 | if($self->{'debug'}) { | ||
| 228 | printf("+++++++++ %s ++++++\n", $feature1->chrom_name); | ||
| 229 | $assembly2->display_info; | ||
| 230 | $chrom2->display_info; | ||
| 231 | } | ||
| 232 | |||
| 233 | # | ||
| 234 | # first scan up stream2 until we get to around $feature1 | ||
| 235 | # | ||
| 236 | my $feature2a = $stream2->next_in_stream; | ||
| 237 | my $feature2b = $stream2->next_in_stream; | ||
| 238 | printf(" F2: %s [%s] :: preload\n", $feature2a->chrom_location, $feature2a->primary_name) if($feature2a and $self->{'debug'}>1); | ||
| 239 | printf(" F2: %s [%s] :: preload\n", $feature2b->chrom_location, $feature2b->primary_name) if($feature2b and $self->{'debug'}>1); | ||
| 240 | |||
| 241 | while($self->overlap_check($feature1, $feature2b) == 1) { | ||
| 242 | #F1 is past the end of the buffer so move the buffer forward (f1>f2) | ||
| 243 | printf(" F2: %s [%s] :: upstream, move stream2 forward\n", $feature2b->chrom_location, $feature2b->primary_name) if($self->{'debug'}>1); | ||
| 244 | $feature2a = $feature2b; | ||
| 245 | $feature2b = $stream2->next_in_stream; | ||
| 246 | } | ||
| 247 | printf(" F2: %s [%s] :: init buffer\n", $feature2a->chrom_location, $feature2a->primary_name) if($feature2a and $self->{'debug'}>1); | ||
| 248 | printf(" F2: %s [%s] :: init buffer\n", $feature2b->chrom_location, $feature2b->primary_name) if($feature2b and $self->{'debug'}>1); | ||
| 249 | $self->{'f2_buffer'}= [$feature2a, $feature2b]; | ||
| 250 | } | ||
| 251 | |||
| 252 | my $f2_buffer = $self->{'f2_buffer'}; | ||
| 253 | my $stream2 = $self->{'current_stream'}; | ||
| 254 | |||
| 255 | # | ||
| 256 | # first do work on the end of the buffer, in order to properly capture 'betweeen' events we | ||
| 257 | # need to make sure buffer goes beyond the current $feature1 | ||
| 258 | # | ||
| 259 | my $feature2 = undef; | ||
| 260 | if(@$f2_buffer) { $feature2 = $f2_buffer->[scalar(@$f2_buffer)-1]; } | ||
| 261 | while($feature2 and ($self->overlap_check($feature1, $feature2) != -1)) { | ||
| 262 | #keep going | ||
| 263 | $feature2 = $stream2->next_in_stream; | ||
| 264 | if($feature2) { | ||
| 265 | printf(" F2: %s [%s] :: buffer tail extend\n", $feature2->chrom_location, $feature2->primary_name) if($self->{'debug'}>1); | ||
| 266 | push @$f2_buffer, $feature2; | ||
| 267 | } | ||
| 268 | } | ||
| 269 | |||
| 270 | # | ||
| 271 | # then do work on the head of the buffer | ||
| 272 | # | ||
| 273 | my $feature2a = $f2_buffer->[0]; | ||
| 274 | my $feature2b = $f2_buffer->[1]; | ||
| 275 | while($feature2b and ($self->overlap_check($feature1, $feature2b) == 1)) { | ||
| 276 | #F1 past is past both f2a and f2b so trim the head | ||
| 277 | printf(" F2: %s [%s] :: buffer head trim\n", $feature2a->chrom_location, $feature2a->primary_name) if($self->{'debug'}>1); | ||
| 278 | shift @$f2_buffer; | ||
| 279 | $feature2a = $feature2b; | ||
| 280 | $feature2b = $f2_buffer->[1]; | ||
| 281 | } | ||
| 282 | |||
| 283 | if($self->{'debug'}>1) { | ||
| 284 | printf(" F1: %s [%s]\n", $feature1->chrom_location, $feature1->primary_name); | ||
| 285 | foreach my $f2 (@$f2_buffer) { | ||
| 286 | next unless(defined($f2)); | ||
| 287 | printf(" buffer F2: %s [%s]\n", $f2->chrom_location, $f2->primary_name); | ||
| 288 | } | ||
| 289 | } | ||
| 290 | |||
| 291 | my $does_overlap =0; | ||
| 292 | foreach $feature2 (@$f2_buffer) { | ||
| 293 | next unless(defined($feature2)); | ||
| 294 | if($self->overlap_check($feature1, $feature2) == 0) { | ||
| 295 | #OK real overlap here | ||
| 296 | printf(" overlap_check F2: %s [%s] :: OK do call out\n", $feature2->chrom_location, $feature2->primary_name) if($self->{'debug'}>1); | ||
| 297 | $does_overlap=1; | ||
| 298 | #call out here | ||
| 299 | if($self->overlap_function) { | ||
| 300 | $self->overlap_function->($feature1, $feature2); | ||
| 301 | } | ||
| 302 | |||
| 303 | } else { | ||
| 304 | printf(" overlap_check F2: %s [%s] :: nope\n", $feature2->chrom_location, $feature2->primary_name) if($self->{'debug'}>1); | ||
| 305 | } | ||
| 306 | } | ||
| 307 | |||
| 308 | #OK now the buffer should be setup so that the first feature2 is < feature1 | ||
| 309 | #and the last feature2 > feature1 | ||
| 310 | |||
| 311 | if(!$does_overlap and $self->between_function and (scalar(@$f2_buffer) < 3)) { | ||
| 312 | #if less than 3 feature2 in buffer then there is good chance of a between | ||
| 313 | my $f2a = $f2_buffer->[0]; | ||
| 314 | my $f2b = $f2_buffer->[1]; | ||
| 315 | if($self->overlap_check($feature1, $f2a) == -1) { | ||
| 316 | # f1 < f2a so between end and f2a | ||
| 317 | printf(" BEFORE [%s] %s\n", $f2a->primary_name, $f2a->chrom_location) if($self->{'debug'}>1); | ||
| 318 | $self->between_function->($feature1, undef, $f2a); | ||
| 319 | } | ||
| 320 | elsif(($self->overlap_check($feature1, $f2a) == 1) and !defined($f2b)) { | ||
| 321 | # >f2a and no f2b then and the end | ||
| 322 | printf(" AFTER [%s] %s\n", $f2a->primary_name, $f2a->chrom_location) if($self->{'debug'}>1); | ||
| 323 | $self->between_function->($feature1, $f2a, undef); | ||
| 324 | } | ||
| 325 | elsif(($self->overlap_check($feature1, $f2a) == 1) and ($self->overlap_check($feature1, $f2b) == -1)) { | ||
| 326 | # after f2a but before f2b, a real between | ||
| 327 | printf(" BETWEEN [%s] %s <=> [%s] %s\n", | ||
| 328 | $f2_buffer->[0]->primary_name, | ||
| 329 | $f2_buffer->[0]->chrom_location, | ||
| 330 | $f2_buffer->[1]->primary_name, | ||
| 331 | $f2_buffer->[1]->chrom_location) if($self->{'debug'}>1); | ||
| 332 | $self->between_function->($feature1, $f2_buffer->[0], $f2_buffer->[1]); | ||
| 333 | } | ||
| 334 | } | ||
| 335 | |||
| 336 | |||
| 337 | } | ||
| 338 | |||
| 339 | |||
| 340 | |||
| 341 | sub overlap_check { | ||
| 342 | my $self = shift; | ||
| 343 | my $feature1 = shift; | ||
| 344 | my $feature2 = shift; | ||
| 345 | |||
| 346 | return -999 unless($feature1 and $feature2); #stop | ||
| 347 | |||
| 348 | # -1 means feature2 stream has not caught up to feature1 yet | ||
| 349 | # 1 means feature2 stream has gone past feature1 | ||
| 350 | # 0 means overlapping | ||
| 351 | |||
| 352 | my $mod_start = $feature2->chrom_start; | ||
| 353 | my $mod_end = $feature2->chrom_end; | ||
| 354 | |||
| 355 | if($feature2->strand eq '+') { | ||
| 356 | $mod_start -= $self->upstream_distance; | ||
| 357 | $mod_end += $self->downstream_distance; | ||
| 358 | } else { | ||
| 359 | $mod_start -= $self->downstream_distance; | ||
| 360 | $mod_end += $self->upstream_distance; | ||
| 361 | } | ||
| 362 | |||
| 363 | if($feature1->chrom_start > $mod_end) { return 1; } #f1 starts AFTER f2 ends (f1>f2) | ||
| 364 | if($feature1->chrom_end < $mod_start) { return -1; } #f1 ends before f2 starts (f1<g2) | ||
| 365 | |||
| 366 | if(($mod_start <= $feature1->chrom_end) and | ||
| 367 | ($mod_end >= $feature1->chrom_start)) { | ||
| 368 | return 0; | ||
| 369 | } | ||
| 370 | return -999; #hmm something is off | ||
| 371 | } | ||
| 372 | |||
| 373 | |||
| 374 | 1; | ||
| 375 |