| 1 | =head1 NAME - EEDB::Feature | ||
| 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_EEDB_feature_global_should_cache = 0; | ||
| 46 | my $__riken_EEDB_feature_global_id_cache = {}; | ||
| 47 | |||
| 48 | $VERSION = 0.953; | ||
| 49 | |||
| 50 | package EEDB::Feature; | ||
| 51 | |||
| 52 | use strict; | ||
| 53 | use Time::HiRes qw(time gettimeofday tv_interval); | ||
| 54 | use EEDB::Chrom; | ||
| 55 | use EEDB::ChromChunk; | ||
| 56 | use EEDB::FeatureSource; | ||
| 57 | use EEDB::MetadataSet; | ||
| 58 | use EEDB::Edge; | ||
| 59 | use EEDB::EdgeSet; | ||
| 60 | use EEDB::Expression; | ||
| 61 | |||
| 62 | use MQdb::MappedQuery; | ||
| 63 | our @ISA = qw(MQdb::MappedQuery); | ||
| 64 | |||
| 65 | |||
| 66 | ################################################# | ||
| 67 | # Class methods | ||
| 68 | ################################################# | ||
| 69 | |||
| 70 | sub class { return "Feature"; } | ||
| 71 | |||
| 72 | sub set_cache_behaviour { | ||
| 73 | my $class = shift; | ||
| 74 | my $mode = shift; | ||
| 75 | |||
| 76 | $__riken_EEDB_feature_global_should_cache = $mode; | ||
| 77 | |||
| 78 | if(defined($mode) and ($mode eq '0')) { | ||
| 79 | #if turning off caching, then flush the caches | ||
| 80 | foreach my $feature (values(%{$__riken_EEDB_feature_global_id_cache})) { | ||
| 81 | $feature->clear_edges; | ||
| 82 | } | ||
| 83 | $__riken_EEDB_feature_global_id_cache = {}; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | sub get_cache_size { | ||
| 88 | return scalar(keys(%$__riken_EEDB_feature_global_id_cache)); | ||
| 89 | } | ||
| 90 | |||
| 91 | |||
| 92 | ################################################# | ||
| 93 | # Instance methods | ||
| 94 | ################################################# | ||
| 95 | |||
| 96 | sub init { | ||
| 97 | my $self = shift; | ||
| 98 | $self->SUPER::init; | ||
| 99 | |||
| 100 | $self->{'chrom_name'} = undef; | ||
| 101 | $self->{'assembly'} = undef; | ||
| 102 | $self->{'chrom_length'} = undef; | ||
| 103 | #$self->{'metadataset'} = new EEDB::MetadataSet; | ||
| 104 | $self->{'strand'} = ''; | ||
| 105 | $self->{'primary_name'} = ''; | ||
| 106 | |||
| 107 | return $self; | ||
| 108 | } | ||
| 109 | |||
| 110 | |||
| 111 | ########################## | ||
| 112 | # | ||
| 113 | # getter/setter methods of data which is stored in database | ||
| 114 | # | ||
| 115 | ########################## | ||
| 116 | |||
| 117 | sub feature_source { | ||
| 118 | my ($self, $source) = @_; | ||
| 119 | if($source) { | ||
| 120 | unless(defined($source) && $source->isa('EEDB::FeatureSource')) { | ||
| 121 | die('feature_source param must be a EEDB::FeatureSource'); | ||
| 122 | } | ||
| 123 | $self->{'feature_source'} = $source; | ||
| 124 | } | ||
| 125 | |||
| 126 | #lazy load from database if possible | ||
| 127 | if(!defined($self->{'feature_source'}) and | ||
| 128 | defined($self->database) and | ||
| 129 | defined($self->{'_feature_source_id'})) | ||
| 130 | { | ||
| 131 | my $source = EEDB::FeatureSource->fetch_by_id($self->database, $self->{'_feature_source_id'}); | ||
| 132 | if(defined($source)) { | ||
| 133 | $self->{'feature_source'} = $source; | ||
| 134 | } | ||
| 135 | } | ||
| 136 | return $self->{'feature_source'}; | ||
| 137 | } | ||
| 138 | |||
| 139 | |||
| 140 | sub chrom { | ||
| 141 | my $self = shift; | ||
| 142 | |||
| 143 | if(@_) { | ||
| 144 | my $chrom = shift; | ||
| 145 | if(defined($chrom) && !$chrom->isa('EEDB::Chrom')) { | ||
| 146 | die('chrom param must be a EEDB::Chrom'); | ||
| 147 | } | ||
| 148 | $self->{'chrom'} = $chrom; | ||
| 149 | $self->{'_chrom_id'} = undef; | ||
| 150 | if($chrom) { $self->{'_chrom_id'} = $chrom->id; } | ||
| 151 | } | ||
| 152 | |||
| 153 | #lazy load from database if possible | ||
| 154 | if(!defined($self->{'chrom'}) and | ||
| 155 | defined($self->database) and | ||
| 156 | defined($self->{'_chrom_id'})) | ||
| 157 | { | ||
| 158 | my $chrom = EEDB::Chrom->fetch_by_id($self->database, $self->{'_chrom_id'}); | ||
| 159 | if(defined($chrom)) { | ||
| 160 | $self->{'chrom'} = $chrom; | ||
| 161 | } | ||
| 162 | } | ||
| 163 | return $self->{'chrom'}; | ||
| 164 | } | ||
| 165 | |||
| 166 | ##### | ||
| 167 | |||
| 168 | sub chrom_name { | ||
| 169 | my $self = shift; | ||
| 170 | return $self->{'chrom_name'} = shift if(@_); | ||
| 171 | if(defined($self->{'chrom_name'})) { | ||
| 172 | return $self->{'chrom_name'}; | ||
| 173 | } elsif($self->chrom) { | ||
| 174 | return $self->chrom->chrom_name; | ||
| 175 | } else { | ||
| 176 | return ''; | ||
| 177 | } | ||
| 178 | } | ||
| 179 | |||
| 180 | sub chrom_id { | ||
| 181 | my $self = shift; | ||
| 182 | return $self->{'_chrom_id'} = shift if(@_); | ||
| 183 | if(defined($self->{'_chrom_id'})) { | ||
| 184 | return $self->{'_chrom_id'}; | ||
| 185 | } elsif($self->chrom) { | ||
| 186 | return $self->chrom->id; | ||
| 187 | } else { | ||
| 188 | return ''; | ||
| 189 | } | ||
| 190 | } | ||
| 191 | |||
| 192 | sub chrom_start { | ||
| 193 | my $self = shift; | ||
| 194 | return $self->{'chrom_start'} = shift if(@_); | ||
| 195 | $self->{'chrom_start'}=-1 unless(defined($self->{'chrom_start'})); | ||
| 196 | return $self->{'chrom_start'}; | ||
| 197 | } | ||
| 198 | |||
| 199 | sub chrom_end { | ||
| 200 | my $self = shift; | ||
| 201 | return $self->{'chrom_end'} = shift if(@_); | ||
| 202 | $self->{'chrom_end'}=-1 unless(defined($self->{'chrom_end'})); | ||
| 203 | return $self->{'chrom_end'}; | ||
| 204 | } | ||
| 205 | |||
| 206 | sub strand { | ||
| 207 | #either '-' or '+' | ||
| 208 | my $self = shift; | ||
| 209 | my $strand = shift; | ||
| 210 | if(defined($strand)) { | ||
| 211 | if(($strand eq '-') or ($strand eq '+')) { | ||
| 212 | $self->{'strand'} = $strand; | ||
| 213 | } elsif($strand eq '1') { | ||
| 214 | $self->{'strand'} = '+'; | ||
| 215 | } elsif($strand eq '-1') { | ||
| 216 | $self->{'strand'} = '-'; | ||
| 217 | } | ||
| 218 | } | ||
| 219 | return $self->{'strand'}; | ||
| 220 | } | ||
| 221 | |||
| 222 | sub chrom_location { | ||
| 223 | my $self = shift; | ||
| 224 | my $str = sprintf("%s:%d..%d%s", | ||
| 225 | $self->chrom_name, | ||
| 226 | $self->chrom_start, | ||
| 227 | $self->chrom_end, | ||
| 228 | $self->strand); | ||
| 229 | return $str; | ||
| 230 | } | ||
| 231 | |||
| 232 | sub primary_name { | ||
| 233 | my $self = shift; | ||
| 234 | return $self->{'primary_name'} = shift if(@_); | ||
| 235 | $self->{'primary_name'}='' unless(defined($self->{'primary_name'})); | ||
| 236 | return $self->{'primary_name'}; | ||
| 237 | } | ||
| 238 | |||
| 239 | sub significance { | ||
| 240 | my $self = shift; | ||
| 241 | return $self->{'significance'} = shift if(@_); | ||
| 242 | $self->{'significance'}=0 unless(defined($self->{'significance'})); | ||
| 243 | return $self->{'significance'}; | ||
| 244 | } | ||
| 245 | |||
| 246 | # | ||
| 247 | ################################################# | ||
| 248 | # | ||
| 249 | |||
| 250 | sub metadataset { | ||
| 251 | my $self = shift; | ||
| 252 | $self->_load_metadata; #return if already loaded | ||
| 253 | return $self->{'metadataset'}; | ||
| 254 | } | ||
| 255 | |||
| 256 | sub add_symbol { | ||
| 257 | my $self = shift; | ||
| 258 | my $tag = shift; | ||
| 259 | my $value = shift; | ||
| 260 | $self->metadataset->add_tag_symbol($tag, $value); | ||
| 261 | } | ||
| 262 | |||
| 263 | sub add_data { | ||
| 264 | my $self = shift; | ||
| 265 | my $tag = shift; | ||
| 266 | my $value = shift; | ||
| 267 | $self->metadataset->add_tag_data($tag, $value); | ||
| 268 | } | ||
| 269 | |||
| 270 | sub find_symbol { | ||
| 271 | my $self = shift; | ||
| 272 | my $tag = shift; | ||
| 273 | my $value = shift; | ||
| 274 | return $self->metadataset->find_metadata($tag, $value); | ||
| 275 | } | ||
| 276 | |||
| 277 | |||
| 278 | # | ||
| 279 | ################################################# | ||
| 280 | # | ||
| 281 | |||
| 282 | sub max_expression { | ||
| 283 | my $self = shift; | ||
| 284 | |||
| 285 | if(!defined($self->{'max_express'}) and ($self->database)) { | ||
| 286 | $self->_get_max_expression(); | ||
| 287 | } | ||
| 288 | return $self->{'max_express'}; | ||
| 289 | } | ||
| 290 | |||
| 291 | sub sum_expression { | ||
| 292 | my $self = shift; | ||
| 293 | |||
| 294 | if(!defined($self->{'sum_express'}) and ($self->database)) { | ||
| 295 | $self->_get_sum_expression(); | ||
| 296 | } | ||
| 297 | return $self->{'sum_express'}; | ||
| 298 | } | ||
| 299 | |||
| 300 | sub expression_cache { | ||
| 301 | my $self = shift; | ||
| 302 | if(!defined($self->{'expression_cache'})) { | ||
| 303 | $self->{'expression_cache'}={}; | ||
| 304 | if($self->database) { | ||
| 305 | my $express_array = EEDB::Expression->fetch_all_by_feature($self); | ||
| 306 | foreach my $express (@$express_array) { | ||
| 307 | $self->{'expression_cache'}->{$express->type . $express->experiment->id} = $express; | ||
| 308 | } | ||
| 309 | } | ||
| 310 | } | ||
| 311 | return $self->{'expression_cache'}; | ||
| 312 | } | ||
| 313 | |||
| 314 | sub empty_expression_cache { | ||
| 315 | my $self = shift; | ||
| 316 | $self->{'expression_cache'} = undef; | ||
| 317 | return 1; | ||
| 318 | } | ||
| 319 | |||
| 320 | #used for bulk storage system | ||
| 321 | sub add_expression { | ||
| 322 | my $self = shift; | ||
| 323 | my $express = shift; | ||
| 324 | |||
| 325 | unless(defined($express) && $express->isa('EEDB::Expression')) { | ||
| 326 | die('add_expression() param must be a EEDB::Expression'); | ||
| 327 | } | ||
| 328 | $self->{'expression_cache'}->{$express->type . $express->experiment->id} = $express; | ||
| 329 | } | ||
| 330 | |||
| 331 | sub get_expression { | ||
| 332 | my $self = shift; | ||
| 333 | my $experiment = shift; #EEDB::Experiment object | ||
| 334 | my $type = shift; | ||
| 335 | |||
| 336 | unless(defined($experiment) && $experiment->isa('EEDB::Experiment')) { | ||
| 337 | die('get_expression() param 1 must be a EEDB::Experiment'); | ||
| 338 | } | ||
| 339 | if(!defined($type)) { $type = ''; } | ||
| 340 | return $self->expression_cache->{$type . $experiment->id}; | ||
| 341 | } | ||
| 342 | |||
| 343 | |||
| 344 | sub find_expression { | ||
| 345 | my $self = shift; | ||
| 346 | my %options = @_; #like datatypes=>["tpm","raw"], experiments=>[$exp1, $exp2,$exp3] | ||
| 347 | |||
| 348 | my $type_hash; | ||
| 349 | my $exp_hash; | ||
| 350 | |||
| 351 | if($options{'datatypes'}) { | ||
| 352 | my @types = @{$options{'datatypes'}}; | ||
| 353 | foreach my $type (@types) { $type_hash->{$type} = 1; } | ||
| 354 | } | ||
| 355 | if($options{'experiments'}) { | ||
| 356 | my @experiments = @{$options{'experiments'}}; | ||
| 357 | foreach my $exp (@experiments) { $exp_hash->{$exp->id} = 1; } | ||
| 358 | } | ||
| 359 | |||
| 360 | my @express_array = values %{$self->expression_cache}; | ||
| 361 | my @filter_express; | ||
| 362 | foreach my $express (@express_array) { | ||
| 363 | if($type_hash and $type_hash->{$express->type}) { push @filter_express, $express; } | ||
| 364 | if($exp_hash and $exp_hash->{$express->experiment->id}) { push @filter_express, $express; } | ||
| 365 | } | ||
| 366 | return \@filter_express; | ||
| 367 | } | ||
| 368 | |||
| 369 | |||
| 370 | sub get_expression_array { | ||
| 371 | my $self = shift; | ||
| 372 | if(!defined($self->{'expression_cache'})) { return []; } | ||
| 373 | my @exps = values %{$self->{'expression_cache'}}; | ||
| 374 | return \@exps; | ||
| 375 | } | ||
| 376 | |||
| 377 | sub add_expression_data { | ||
| 378 | my $self = shift; | ||
| 379 | my $experiment = shift; #EEDB::Experiment object | ||
| 380 | my $type = shift; | ||
| 381 | my $value = shift; | ||
| 382 | |||
| 383 | my $expression = $self->get_expression($experiment, $type); | ||
| 384 | unless($expression) { | ||
| 385 | $expression = new EEDB::Expression; | ||
| 386 | $expression->feature($self); | ||
| 387 | $expression->experiment($experiment); | ||
| 388 | $expression->sig_error(0.0); #default | ||
| 389 | } | ||
| 390 | $expression->type($type); | ||
| 391 | $expression->value($value); | ||
| 392 | $self->add_expression($expression); | ||
| 393 | return $expression; | ||
| 394 | } | ||
| 395 | |||
| 396 | # | ||
| 397 | ################################################# | ||
| 398 | # | ||
| 399 | |||
| 400 | sub simple_display_desc { | ||
| 401 | my $self = shift; | ||
| 402 | |||
| 403 | my $str = sprintf("Feature(%s)", $self->id); | ||
| 404 | if($self->feature_source) { | ||
| 405 | $str .= sprintf(" %s", $self->feature_source->name); | ||
| 406 | } | ||
| 407 | if($self->chrom) { | ||
| 408 | $str .= sprintf(" %s %s:%d..%d%s", | ||
| 409 | $self->chrom->assembly->ucsc_name, | ||
| 410 | $self->chrom_name, | ||
| 411 | $self->chrom_start, | ||
| 412 | $self->chrom_end, | ||
| 413 | $self->strand); | ||
| 414 | } | ||
| 415 | $str .= sprintf(" : %s", $self->primary_name); | ||
| 416 | if($self->significance > 0.0) { $str .= sprintf(" sig:%1.2f", $self->significance); } | ||
| 417 | return $str; | ||
| 418 | } | ||
| 419 | |||
| 420 | sub display_desc { | ||
| 421 | my $self = shift; | ||
| 422 | |||
| 423 | my $str = $self->simple_display_desc; | ||
| 424 | |||
| 425 | my $mdata_list = $self->metadataset->metadata_list; | ||
| 426 | my $first=1; | ||
| 427 | if(defined($mdata_list) and (scalar(@$mdata_list))) { | ||
| 428 | $str .= ' ('; | ||
| 429 | foreach my $mdata (@$mdata_list) { | ||
| 430 | if($first) { $first=0; } | ||
| 431 | else { $str .= ','; } | ||
| 432 | $str .= sprintf("(%s,\"%s\")", $mdata->type, $mdata->data); | ||
| 433 | } | ||
| 434 | $str .= ')'; | ||
| 435 | } | ||
| 436 | return $str; | ||
| 437 | } | ||
| 438 | |||
| 439 | sub display_contents { | ||
| 440 | my $self = shift; | ||
| 441 | my $fsrc = ''; | ||
| 442 | my $str = sprintf("Feature(%s)", $self->id); | ||
| 443 | if($self->feature_source) { | ||
| 444 | $str .= sprintf(" %s", $fsrc = $self->feature_source->name); | ||
| 445 | } | ||
| 446 | if($self->chrom) { | ||
| 447 | $str .= sprintf(" %s %s:%d..%d%s", | ||
| 448 | $self->chrom->assembly->ucsc_name, | ||
| 449 | $self->chrom_name, | ||
| 450 | $self->chrom_start, | ||
| 451 | $self->chrom_end, | ||
| 452 | $self->strand); | ||
| 453 | } | ||
| 454 | $str .= sprintf(" : %s", $self->primary_name); | ||
| 455 | |||
| 456 | if($self->significance > 0.0) { $str .= sprintf(" sig:%1.2f", $self->significance); } | ||
| 457 | $str .= "\n". $self->metadataset->display_contents; | ||
| 458 | |||
| 459 | my $expr_array = $self->get_expression_array(); | ||
| 460 | foreach my $express (@$expr_array) { | ||
| 461 | $str .= " ". $express->display_desc ."\n"; | ||
| 462 | } | ||
| 463 | |||
| 464 | return $str; | ||
| 465 | } | ||
| 466 | |||
| 467 | sub xml { | ||
| 468 | my $self = shift; | ||
| 469 | |||
| 470 | my $str = $self->xml_start; | ||
| 471 | |||
| 472 | $str .= "\n"; | ||
| 473 | $str .= " " . $self->feature_source->xml if($self->feature_source); | ||
| 474 | $str .= " " . $self->chrom->assembly->xml if($self->chrom); | ||
| 475 | $str .= $self->metadataset->xml; | ||
| 476 | |||
| 477 | # display max_expression section if data is available | ||
| 478 | my $maxexpress = $self->max_expression(); | ||
| 479 | if(defined($maxexpress)) { | ||
| 480 | $str .= " <max_expression>\n"; | ||
| 481 | foreach my $express (@$maxexpress) { | ||
| 482 | $str .= sprintf(" <express platform=\'%s\' maxvalue=\'%1.2f\'/>", $express->[0], $express->[1]); | ||
| 483 | } | ||
| 484 | $str .= " </max_expression>\n"; | ||
| 485 | } | ||
| 486 | |||
| 487 | $str .= $self->xml_end; | ||
| 488 | return $str; | ||
| 489 | } | ||
| 490 | |||
| 491 | sub xml_start { | ||
| 492 | my $self = shift; | ||
| 493 | |||
| 494 | my $str = sprintf("<feature id=\"%s\" desc=\"%s\" ", $self->id, $self->primary_name); | ||
| 495 | if($self->database and $self->database->alias) { | ||
| 496 | $str .= sprintf("peer=\"%s\" ", $self->database->alias); | ||
| 497 | } | ||
| 498 | |||
| 499 | if($self->feature_source) { | ||
| 500 | $str .= sprintf("fsrc=\"%s\" category=\"%s\" ", | ||
| 501 | $self->feature_source->name, | ||
| 502 | $self->feature_source->category); | ||
| 503 | } | ||
| 504 | if(defined($self->chrom)) { | ||
| 505 | my $assembly = $self->chrom->assembly; | ||
| 506 | $str .= sprintf(" taxon_id=\"%s\" ncbi_asm=\"%s\" asm=\"%s\" chr=\"%s\" start=\"%d\" end=\"%d\" strand=\"%s\" ", | ||
| 507 | $assembly->taxon_id, | ||
| 508 | $assembly->ncbi_version, | ||
| 509 | $assembly->ucsc_name, | ||
| 510 | $self->chrom_name, | ||
| 511 | $self->chrom_start, | ||
| 512 | $self->chrom_end, | ||
| 513 | $self->strand); | ||
| 514 | } | ||
| 515 | $str .= ">"; | ||
| 516 | return $str; | ||
| 517 | } | ||
| 518 | |||
| 519 | sub xml_end { | ||
| 520 | my $self = shift; | ||
| 521 | return "</feature>\n"; | ||
| 522 | } | ||
| 523 | |||
| 524 | sub gff_description { | ||
| 525 | my $self = shift; | ||
| 526 | my $show_metadata = shift; | ||
| 527 | |||
| 528 | my $str = sprintf("%s\t%s\t%s\t%d\t%d\t.\t%s\t.\t", | ||
| 529 | $self->chrom_name, | ||
| 530 | $self->feature_source->name, | ||
| 531 | $self->feature_source->category, | ||
| 532 | $self->chrom_start, | ||
| 533 | $self->chrom_end, | ||
| 534 | $self->strand); | ||
| 535 | |||
| 536 | my $metadata = sprintf("ID=\"%d\";Name=\"%s\";asm=\"%s\"", $self->id, $self->primary_name, $self->chrom->assembly->ucsc_name); | ||
| 537 | if($show_metadata) { | ||
| 538 | $metadata .= ";" . $self->metadataset->gff_description; | ||
| 539 | unless($metadata) { $metadata = '.'; } | ||
| 540 | } | ||
| 541 | $str .= $metadata; | ||
| 542 | |||
| 543 | return $str; | ||
| 544 | } | ||
| 545 | |||
| 546 | sub bed_description { | ||
| 547 | my $self = shift; | ||
| 548 | |||
| 549 | my $str = sprintf("%s\t%d\t%d\t%s\t%1.3f\t%s", | ||
| 550 | $self->chrom_name, | ||
| 551 | $self->chrom_start, | ||
| 552 | $self->chrom_end, | ||
| 553 | $self->primary_name, | ||
| 554 | $self->significance, | ||
| 555 | $self->strand); | ||
| 556 | return $str; | ||
| 557 | } | ||
| 558 | |||
| 559 | sub dasgff_xml { | ||
| 560 | my $self = shift; | ||
| 561 | |||
| 562 | #<FEATURE id="id" label="label"> | ||
| 563 | #<TYPE id="id" category="category" reference="yes|no">type label</TYPE> | ||
| 564 | #<METHOD id="id"> method label </METHOD> | ||
| 565 | #<START> start </START> | ||
| 566 | #<END> end </END> | ||
| 567 | #<SCORE> [X.XX|-] </SCORE> | ||
| 568 | #<ORIENTATION> [0|-|+] </ORIENTATION> | ||
| 569 | #<PHASE> [0|1|2|-]</PHASE> | ||
| 570 | #<NOTE> note text </NOTE> | ||
| 571 | #<LINK href="url"> link text </LINK> | ||
| 572 | #<TARGET id="id" start="x" stop="y">target name</TARGET> | ||
| 573 | #<GROUP id="id" label="label" type="type"> | ||
| 574 | #<NOTE> note text </NOTE> | ||
| 575 | #<LINK href="url"> link text </LINK> | ||
| 576 | #<TARGET id="id" start="x" stop="y">target name</TARGET> | ||
| 577 | #</GROUP> | ||
| 578 | #</FEATURE> | ||
| 579 | |||
| 580 | my $str = sprintf("<FEATURE id=\"%d\" label=\"%s\">\n", | ||
| 581 | $self->id, | ||
| 582 | $self->primary_name); | ||
| 583 | $str .= sprintf("<TYPE id=\"%d\" category=\"%s\" reference=\"no\">%s</TYPE>\n", | ||
| 584 | $self->feature_source->id, | ||
| 585 | $self->feature_source->category, | ||
| 586 | $self->feature_source->name); | ||
| 587 | $str .= sprintf("<START>%d</START>\n", $self->chrom_start); | ||
| 588 | $str .= sprintf("<END>%d</END>\n", $self->chrom_end); | ||
| 589 | |||
| 590 | my $strand = $self->strand; | ||
| 591 | if(($strand eq '') or !defined($strand)) { $strand = '0'; } | ||
| 592 | $str .= sprintf("<ORIENTATION>%s</ORIENTATION>\n", $strand); | ||
| 593 | |||
| 594 | $str .= sprintf("<PHASE>-</PHASE>\n"); | ||
| 595 | $str .= sprintf("<SCORE>%1.5f</SCORE>\n", $self->significance); | ||
| 596 | |||
| 597 | $str .= "</FEATURE>\n"; | ||
| 598 | return $str; | ||
| 599 | } | ||
| 600 | |||
| 601 | |||
| 602 | |||
| 603 | # | ||
| 604 | ################################################# | ||
| 605 | # | ||
| 606 | |||
| 607 | sub check_overlap { | ||
| 608 | my $self = shift; | ||
| 609 | my $other = shift; | ||
| 610 | my $distance = shift; | ||
| 611 | |||
| 612 | unless($other) { return 0; } | ||
| 613 | if($other->chrom_id ne $self->chrom_id) { return 0; } | ||
| 614 | unless($distance) { $distance = 0; } | ||
| 615 | |||
| 616 | if(($self->chrom_start - $distance <= $other->chrom_end) and | ||
| 617 | ($self->chrom_end + $distance >= $other->chrom_start)) { | ||
| 618 | return 1; | ||
| 619 | } | ||
| 620 | return 0; | ||
| 621 | } | ||
| 622 | |||
| 623 | sub overlap_expand { | ||
| 624 | my $self = shift; | ||
| 625 | my $other = shift; | ||
| 626 | |||
| 627 | if($self->check_overlap($other)) { | ||
| 628 | if($other->chrom_start < $self->chrom_start) { | ||
| 629 | $self->chrom_start($other->chrom_start); | ||
| 630 | } | ||
| 631 | if($other->chrom_end > $self->chrom_end) { | ||
| 632 | $self->chrom_end($other->chrom_end); | ||
| 633 | } | ||
| 634 | return 1; | ||
| 635 | } | ||
| 636 | return 0; | ||
| 637 | } | ||
| 638 | |||
| 639 | ################################################# | ||
| 640 | # | ||
| 641 | # pure object-oriented edge management system | ||
| 642 | # created double references so perl's memory | ||
| 643 | # management system will not auto-release memory | ||
| 644 | # user must make explicit call clear_edges() | ||
| 645 | # before unref-ing the object | ||
| 646 | # | ||
| 647 | ################################################# | ||
| 648 | |||
| 649 | sub clear_edges { | ||
| 650 | my $self = shift; | ||
| 651 | $self->{'_left_links'} = undef; | ||
| 652 | $self->{'_right_links'} = undef; | ||
| 653 | } | ||
| 654 | |||
| 655 | sub left_edges { | ||
| 656 | my $self = shift; | ||
| 657 | |||
| 658 | if(!defined($self->{'_left_links'})) { | ||
| 659 | my $edges = EEDB::Edge->fetch_all_to_feature_id($self->database, $self->id); | ||
| 660 | $self->{'_left_links'} = EEDB::EdgeSet->new('larray' => $edges); | ||
| 661 | } | ||
| 662 | return $self->{'_left_links'}; | ||
| 663 | } | ||
| 664 | |||
| 665 | sub right_edges { | ||
| 666 | my $self = shift; | ||
| 667 | |||
| 668 | if(!defined($self->{'_right_links'})) { | ||
| 669 | my $edges = EEDB::Edge->fetch_all_from_feature_id($self->database, $self->id); | ||
| 670 | $self->{'_right_links'} = EEDB::EdgeSet->new('larray' => $edges); | ||
| 671 | } | ||
| 672 | return $self->{'_right_links'}; | ||
| 673 | } | ||
| 674 | |||
| 675 | sub all_edges { | ||
| 676 | my $self = shift; | ||
| 677 | |||
| 678 | my $lset = new EEDB::EdgeSet; | ||
| 679 | $lset->add_edges($self->left_edges->edges); | ||
| 680 | $lset->add_edges($self->right_edges->edges); | ||
| 681 | return $lset; | ||
| 682 | } | ||
| 683 | |||
| 684 | ################################################# | ||
| 685 | # | ||
| 686 | # routine to combine sequences from multiple chunks | ||
| 687 | # if the requested region is >5kb overlap on | ||
| 688 | # the ChromChunks | ||
| 689 | # | ||
| 690 | ################################################# | ||
| 691 | |||
| 692 | sub get_bioseq { | ||
| 693 | my $self = shift; | ||
| 694 | my $seqdb = shift; | ||
| 695 | my $downstream = shift; ## optional | ||
| 696 | my $upstream = shift; ## optional | ||
| 697 | |||
| 698 | ## no bioseq if no seqdb | ||
| 699 | die "EEDB::Feature::get_bioseq : not a valid seqdb\n" unless(defined($seqdb) && $seqdb->isa('MQdb::Database')); | ||
| 700 | |||
| 701 | ## no bioseq if no coordinates | ||
| 702 | die "EEDB::Feature::get_bioseq : no bioseq retrievable without coordinates\n" unless ((defined $self->chrom) and ($self->chrom_start >0) and ($self->chrom_end >0)); | ||
| 703 | |||
| 704 | ## get the end points of the seq to be retrieved | ||
| 705 | ## ... check that downstream and upstream args do not violate chrom boundaries (rmk: as dedfined in the feature db, not the seqdb) | ||
| 706 | my $asm_name = $self->chrom->assembly->ucsc_name; | ||
| 707 | my $chrom_name = $self->chrom->chrom_name; | ||
| 708 | my $chrom_start = ($downstream) ? $self->chrom_start - $downstream : $self->chrom_start; | ||
| 709 | my $chrom_end = ($upstream) ? $self->chrom_end + $upstream : $self->chrom_end; | ||
| 710 | my $strand = $self->strand; | ||
| 711 | die "EEDB::Feature::get_bioseq : downstream arg violates chrom boundaries, max downstream possible is ", $self->chrom_start -1,"\n" if ($chrom_start < 1); ## check with Jess for 1-base / 0-base coord | ||
| 712 | die "EEDB::Feature::get_bioseq : upstream arg violates chrom boundaries, max upstream possible is ", $self->chrom->chrom_length - $self->chrom_end, "\n" if ($chrom_end > $self->chrom->chrom_length); | ||
| 713 | |||
| 714 | |||
| 715 | ## compose a name based on feature information | ||
| 716 | my $bioseq_name = sprintf("feature_%d_%s_%s:%d..%d%s", ($self->id)?$self->id:'0', ($self->primary_name)?$self->primary_name:'', $chrom_name, $chrom_start, $chrom_end, $strand); | ||
| 717 | my $bioseq = Bio::Seq->new(-id => $bioseq_name); | ||
| 718 | |||
| 719 | ## get the chunks (in seqdb) | ||
| 720 | my @chunks = @{EEDB::ChromChunk->fetch_all_named_region($seqdb, $asm_name, $chrom_name, $chrom_start, $chrom_end)}; | ||
| 721 | |||
| 722 | ## die if no chunk | ||
| 723 | ## this should only happened if something is utterly wrong with the db | ||
| 724 | if (scalar @chunks == 0){ | ||
| 725 | die "EEDB::Feature::get_bioseq : no chunks for \n\t", join("\n\t", $seqdb->url, $asm_name, $chrom_name, $chrom_start, $chrom_end),"\n"; | ||
| 726 | } | ||
| 727 | |||
| 728 | ## get the seq from EEDB::ChromChunk::get_subsequence and assign it to the bioseq to be returned | ||
| 729 | ## EEDB::ChromChunk::get_subsequence uses BioSeq to effectively return the revcomp when minus strand | ||
| 730 | elsif (scalar @chunks == 1){ | ||
| 731 | $bioseq->seq($chunks[0]->get_subsequence($chrom_start, $chrom_end, $strand)->seq); | ||
| 732 | } | ||
| 733 | |||
| 734 | ## more than one chunk requires to concat subsequences of all the chunks | ||
| 735 | ## concat the plus strand subsequnces from `feature_start`/`chunk_start` to `next_chunk_start -1`/`feature_end` | ||
| 736 | ## use bioseq->revcomp if feature_strand is minus | ||
| 737 | else { | ||
| 738 | my $seq; | ||
| 739 | @chunks = sort {$a->chrom_start <=> $b->chrom_start} @chunks; | ||
| 740 | for (my $i = 0; $i < scalar @chunks; $i++){ | ||
| 741 | my $start = ($chrom_start >= $chunks[$i]->chrom_start) ? $chrom_start : $chunks[$i]->chrom_start; | ||
| 742 | my $end = ($chunks[$i+1]) ? ($chrom_end > $chunks[$i+1]->chrom_start) ? $chunks[$i+1]->chrom_start -1 : $chrom_end : $chrom_end; | ||
| 743 | $seq .= $chunks[$i]->get_subsequence($start, $end)->seq if ($end > $start); | ||
| 744 | } | ||
| 745 | $bioseq->seq($seq); | ||
| 746 | $bioseq = $bioseq->revcom if ($strand eq '-'); | ||
| 747 | } | ||
| 748 | return $bioseq; | ||
| 749 | } | ||
| 750 | |||
| 751 | ################################################# | ||
| 752 | # | ||
| 753 | # MappedQuery override methods - storage | ||
| 754 | # | ||
| 755 | ################################################# | ||
| 756 | |||
| 757 | sub store { | ||
| 758 | my $self = shift; | ||
| 759 | my $db = shift; | ||
| 760 | |||
| 761 | if($db) { $self->database($db); } | ||
| 762 | my $dbh = $self->database->get_connection; | ||
| 763 | |||
| 764 | my $chrom_id = $self->chrom_id; | ||
| 765 | if($chrom_id eq '') { $chrom_id = undef; } | ||
| 766 | |||
| 767 | my $sql = "INSERT INTO feature ( | ||
| 768 | feature_source_id, | ||
| 769 | chrom_id, | ||
| 770 | chrom_start, | ||
| 771 | chrom_end, | ||
| 772 | strand, | ||
| 773 | significance, | ||
| 774 | primary_name) | ||
| 775 | VALUES(?,?,?,?,?,?,?)"; | ||
| 776 | my $sth = $dbh->prepare($sql); | ||
| 777 | $sth->execute($self->feature_source->id, | ||
| 778 | $chrom_id, | ||
| 779 | $self->chrom_start, | ||
| 780 | $self->chrom_end, | ||
| 781 | $self->strand, | ||
| 782 | $self->significance, | ||
| 783 | $self->primary_name); | ||
| 784 | |||
| 785 | my $dbID = $dbh->last_insert_id(undef, undef, qw(feature feature_id)); | ||
| 786 | $sth->finish; | ||
| 787 | return undef unless($dbID); | ||
| 788 | $self->primary_id($dbID); | ||
| 789 | |||
| 790 | #now do the symbols and metadata | ||
| 791 | $self->store_metadata; | ||
| 792 | |||
| 793 | #link to chunk system | ||
| 794 | $self->link_2_chunk(); | ||
| 795 | |||
| 796 | return $self; | ||
| 797 | } | ||
| 798 | |||
| 799 | sub store_metadata { | ||
| 800 | my $self = shift; | ||
| 801 | die("error no database to store metadata\n") unless($self->database); | ||
| 802 | return unless($self->{'metadataset'}); #if not created then don't lazy load | ||
| 803 | |||
| 804 | my $mdata_list = $self->metadataset->metadata_list; | ||
| 805 | foreach my $mdata (@$mdata_list) { | ||
| 806 | if(!defined($mdata->primary_id)) { | ||
| 807 | $mdata->store($self->database); | ||
| 808 | $mdata->store_link_to_feature($self); | ||
| 809 | } | ||
| 810 | } | ||
| 811 | } | ||
| 812 | |||
| 813 | sub create_link_to_feature { | ||
| 814 | my $self = shift; | ||
| 815 | my $feature = shift; | ||
| 816 | my $link_type = shift; | ||
| 817 | my $dir = shift; | ||
| 818 | my $weight = shift; | ||
| 819 | |||
| 820 | die("error no database to store symbol\n") unless($self->database); | ||
| 821 | |||
| 822 | #first do insert ignore to make sure symbol is in the database | ||
| 823 | my $dbc = $self->database->get_connection; | ||
| 824 | my $sql = "INSERT ignore INTO edge ". | ||
| 825 | "(feature1_id, feature2_id, direction, sub_type, weight, edge_source_id) VALUES(?,?,?,?,?,0)"; | ||
| 826 | my $sth = $dbc->prepare($sql); | ||
| 827 | $sth->execute($self->id, $feature->id, $dir, $link_type, $weight); | ||
| 828 | $sth->finish; | ||
| 829 | } | ||
| 830 | |||
| 831 | sub update_location { | ||
| 832 | my $self = shift; | ||
| 833 | |||
| 834 | die("error no database set\n") unless($self->database); | ||
| 835 | dies("Feature with undef id") unless(defined($self->primary_id)); | ||
| 836 | |||
| 837 | my $chrom_id = undef; | ||
| 838 | if($self->chrom) { $chrom_id = $self->chrom_id; } | ||
| 839 | |||
| 840 | my $sql = "UPDATE feature SET primary_name=?, chrom_id=?, chrom_start=?, chrom_end=?, strand=? WHERE feature_id=?"; | ||
| 841 | $self->database->execute_sql($sql, | ||
| 842 | $self->primary_name, | ||
| 843 | $chrom_id, | ||
| 844 | $self->chrom_start, | ||
| 845 | $self->chrom_end, | ||
| 846 | $self->strand, | ||
| 847 | $self->id); | ||
| 848 | $self->link_2_chunk(); | ||
| 849 | } | ||
| 850 | |||
| 851 | |||
| 852 | ################################################# | ||
| 853 | # | ||
| 854 | # MappedQuery override methods - fetching | ||
| 855 | # | ||
| 856 | ################################################# | ||
| 857 | |||
| 858 | |||
| 859 | sub mapRow { | ||
| 860 | my $self = shift; | ||
| 861 | my $rowHash = shift; | ||
| 862 | my $dbh = shift; | ||
| 863 | |||
| 864 | my $dbID = $rowHash->{'feature_id'}; | ||
| 865 | if(($__riken_EEDB_feature_global_should_cache != 0) and ($self->database())) { | ||
| 866 | my $cached_self = $__riken_EEDB_feature_global_id_cache->{$self->database() . $dbID}; | ||
| 867 | if(defined($cached_self)) { | ||
| 868 | #printf("link already loaded in cache, reuse\n"); | ||
| 869 | #$cached_self->display_info; | ||
| 870 | #printf(" db_id :: %s\n", $cached_self->db_id); | ||
| 871 | return $cached_self; | ||
| 872 | } | ||
| 873 | } | ||
| 874 | |||
| 875 | $self->{'_primary_db_id'} = $rowHash->{'feature_id'}; | ||
| 876 | $self->{'chrom_name'} = $rowHash->{'chrom_name'}; | ||
| 877 | $self->{'chrom_start'} = $rowHash->{'chrom_start'}; | ||
| 878 | $self->{'chrom_end'} = $rowHash->{'chrom_end'}; | ||
| 879 | $self->{'strand'} = $rowHash->{'strand'}; | ||
| 880 | $self->{'primary_name'} = $rowHash->{'primary_name'}; | ||
| 881 | $self->{'significance'} = $rowHash->{'significance'}; | ||
| 882 | |||
| 883 | $self->{'_chrom_id'} = $rowHash->{'chrom_id'}; | ||
| 884 | $self->{'_feature_source_id'} = $rowHash->{'feature_source_id'}; | ||
| 885 | |||
| 886 | if($rowHash->{'symbol_count'}) { | ||
| 887 | if($self->{'significance'}) { | ||
| 888 | $self->{'significance'} = $self->{'significance'} * $rowHash->{'symbol_count'}; | ||
| 889 | } else { | ||
| 890 | $self->{'significance'} = $rowHash->{'symbol_count'}; | ||
| 891 | } | ||
| 892 | } | ||
| 893 | |||
| 894 | if(($__riken_EEDB_feature_global_should_cache != 0) and ($self->database())) { | ||
| 895 | $__riken_EEDB_feature_global_id_cache->{$self->database() . $self->id} = $self; | ||
| 896 | } | ||
| 897 | |||
| 898 | return $self; | ||
| 899 | } | ||
| 900 | |||
| 901 | ############################################################################################### | ||
| 902 | # | ||
| 903 | # object fetch API section | ||
| 904 | # | ||
| 905 | ############################################################################################### | ||
| 906 | |||
| 907 | sub fetch_by_id { | ||
| 908 | my $class = shift; | ||
| 909 | my $db = shift; | ||
| 910 | my $id = shift; | ||
| 911 | |||
| 912 | if($__riken_EEDB_feature_global_should_cache != 0) { | ||
| 913 | my $feature = $__riken_EEDB_feature_global_id_cache->{$db . $id}; | ||
| 914 | if(defined($feature)) { return $feature; } | ||
| 915 | } | ||
| 916 | my $sql = "SELECT f.* FROM feature f WHERE feature_id=?"; | ||
| 917 | return $class->fetch_single($db, $sql, $id); | ||
| 918 | } | ||
| 919 | |||
| 920 | |||
| 921 | sub fetch_all_by_id_list { | ||
| 922 | my $class = shift; | ||
| 923 | my $db = shift; | ||
| 924 | my $id_list = shift; | ||
| 925 | |||
| 926 | my $sql = sprintf("SELECT * FROM feature WHERE feature_id in(%s)", $id_list); | ||
| 927 | return $class->fetch_multiple($db, $sql); | ||
| 928 | } | ||
| 929 | |||
| 930 | |||
| 931 | sub fetch_all_neighbors { | ||
| 932 | my $class = shift; | ||
| 933 | my $db = shift; | ||
| 934 | my $id = shift; | ||
| 935 | |||
| 936 | my $sql = "SELECT f.* FROM feature f JOIN ". | ||
| 937 | " (SELECT feature2_id feature_id FROM edge WHERE feature1_id =? | ||
| 938 | UNION SELECT feature1_id feature_id FROM edge where feature2_id =?)t using(feature_id)"; | ||
| 939 | return $class->fetch_multiple($db, $sql, $id); | ||
| 940 | } | ||
| 941 | |||
| 942 | =head2 fetch_all_by_chrom | ||
| 943 | |||
| 944 | Description : uses an EEDB::Chrom object to stream all features in sorted order by chrom start,end | ||
| 945 | Parameter[1] : EEDB::Chrom object with database connection | ||
| 946 | Parameter[2...] : optional EEDB::FeatureSource(s) to limit the feature stream | ||
| 947 | Returntype : array reference of EEDB::Feature objects | ||
| 948 | Exceptions : die if anything other than EEDB::FeatureSource(s) are passed in the optional parameter list | ||
| 949 | |||
| 950 | =cut | ||
| 951 | |||
| 952 | sub fetch_all_by_chrom { | ||
| 953 | my $class = shift; | ||
| 954 | my $chrom = shift; #Chrom object | ||
| 955 | my @sources = @_; #(optional) FeatureSource object(s) | ||
| 956 | |||
| 957 | if(!defined($chrom) or !defined($chrom->database)) { return []; } | ||
| 958 | if(!($chrom->isa('EEDB::Chrom'))) { | ||
| 959 | die('fetch_all_by_chrom: param1 must be a EEDB::Chrom'); | ||
| 960 | } | ||
| 961 | |||
| 962 | my @fsrc_ids; | ||
| 963 | foreach my $fsrc (@sources) { | ||
| 964 | next unless(defined($fsrc)); | ||
| 965 | if(!($fsrc->isa('EEDB::FeatureSource'))) { | ||
| 966 | die('[source] must be a EEDB::FeatureSource'); | ||
| 967 | } | ||
| 968 | push @fsrc_ids, $fsrc->id; | ||
| 969 | } | ||
| 970 | |||
| 971 | my $sql = sprintf("SELECT * FROM feature WHERE chrom_id=%d ", $chrom->id); | ||
| 972 | $sql .= "AND feature_source_id in(" . join(',', @fsrc_ids) . ") " if(@fsrc_ids); | ||
| 973 | $sql .= "ORDER BY chrom_start, chrom_end"; | ||
| 974 | return $class->fetch_multiple($chrom->database, $sql); | ||
| 975 | } | ||
| 976 | |||
| 977 | |||
| 978 | sub fetch_all_by_chrom_chunk_id { | ||
| 979 | my $class = shift; | ||
| 980 | my $db = shift; | ||
| 981 | my $id = shift; | ||
| 982 | |||
| 983 | my $sql = "SELECT * FROM feature ". | ||
| 984 | "JOIN feature_source using(feature_source_id) ". | ||
| 985 | "JOIN chrom using(chrom_id) ". | ||
| 986 | "JOIN feature_2_chunk using(feature_id) ". | ||
| 987 | "WHERE chrom_chunk_id = ? ORDER BY chrom_start"; | ||
| 988 | return $class->fetch_multiple($db, $sql, $id); | ||
| 989 | } | ||
| 990 | |||
| 991 | |||
| 992 | sub fetch_all_within_region { | ||
| 993 | #does a search for features completely contained within the specified region | ||
| 994 | my $class = shift; | ||
| 995 | my $db = shift; | ||
| 996 | my $chrom_chunk_id = shift; | ||
| 997 | my $chrom_start = shift; | ||
| 998 | my $chrom_end = shift; | ||
| 999 | |||
| 1000 | my $sql = "SELECT * FROM feature JOIN feature_source using(feature_source_id) JOIN chrom using(chrom_id) JOIN feature_2_chunk using(feature_id) ". | ||
| 1001 | "WHERE chrom_chunk_id = ? AND chrom_start >= ? AND chrom_end <= ? ". | ||
| 1002 | "ORDER BY chrom_start"; | ||
| 1003 | return $class->fetch_multiple($db, $sql, $chrom_chunk_id, $chrom_start, $chrom_end); | ||
| 1004 | } | ||
| 1005 | |||
| 1006 | |||
| 1007 | sub fetch_all_overlapping_region { | ||
| 1008 | #does a search for features which overlap with the specified region | ||
| 1009 | my $class = shift; | ||
| 1010 | my $db = shift; | ||
| 1011 | my $chrom_chunk_id = shift; | ||
| 1012 | my $chrom_start = shift; | ||
| 1013 | my $chrom_end = shift; | ||
| 1014 | |||
| 1015 | my $sql = "SELECT * FROM feature JOIN feature_source using(feature_source_id) JOIN chrom using(chrom_id) JOIN feature_2_chunk using(feature_id) ". | ||
| 1016 | "WHERE chrom_chunk_id = ? AND chrom_start <= ? AND chrom_end >= ? ". | ||
| 1017 | "ORDER BY chrom_start"; | ||
| 1018 | return $class->fetch_multiple($db, $sql, $chrom_chunk_id, $chrom_end, $chrom_start); | ||
| 1019 | } | ||
| 1020 | |||
| 1021 | |||
| 1022 | sub fetch_all_named_region { | ||
| 1023 | my $class = shift; | ||
| 1024 | my $db = shift; | ||
| 1025 | my $assembly_name = shift; | ||
| 1026 | my $chrom_name = shift; | ||
| 1027 | my $chrom_start = shift; | ||
| 1028 | my $chrom_end = shift; | ||
| 1029 | my @sources = @_; #(optional) FeatureSource(s) objects | ||
| 1030 | |||
| 1031 | #printf("fetch_all_named_region %s : %d .. %d\n", $chrom_name, $chrom_start, $chrom_end); | ||
| 1032 | my $chunks = EEDB::ChromChunk->fetch_all_named_region($db, $assembly_name, $chrom_name, $chrom_start, $chrom_end); | ||
| 1033 | return [] unless(defined($chunks) and scalar(@$chunks)>0); | ||
| 1034 | |||
| 1035 | my @chunk_ids; | ||
| 1036 | foreach my $chunk (@$chunks) { push @chunk_ids, $chunk->id; } | ||
| 1037 | |||
| 1038 | my @fsrc_ids; | ||
| 1039 | foreach my $fsrc (@sources) { | ||
| 1040 | next unless(defined($fsrc)); | ||
| 1041 | if(!($fsrc->isa('EEDB::FeatureSource'))) { | ||
| 1042 | die('[source] must be a EEDB::FeatureSource'); | ||
| 1043 | } | ||
| 1044 | push @fsrc_ids, $fsrc->id; | ||
| 1045 | } | ||
| 1046 | |||
| 1047 | my $sql = "SELECT f.* FROM feature f JOIN ". | ||
| 1048 | "(select distinct feature_id FROM feature_2_chunk ". | ||
| 1049 | " WHERE chrom_chunk_id in (". join(",", @chunk_ids). ")". | ||
| 1050 | ")fc using(feature_id)". | ||
| 1051 | "WHERE chrom_start <= " .$chrom_end. " AND chrom_end >= " .$chrom_start. " "; | ||
| 1052 | $sql .= "AND feature_source_id in(" . join(',', @fsrc_ids) . ") " if(@fsrc_ids); | ||
| 1053 | $sql .= "ORDER BY chrom_start, chrom_end, f.feature_id"; | ||
| 1054 | |||
| 1055 | #print($sql, "\n"); | ||
| 1056 | return $class->fetch_multiple($db, $sql); | ||
| 1057 | } | ||
| 1058 | |||
| 1059 | |||
| 1060 | sub fetch_all_chrom_region { | ||
| 1061 | my $class = shift; | ||
| 1062 | my $db = shift; | ||
| 1063 | my $chrom = shift; #Chrom object | ||
| 1064 | my $chrom_start = shift; | ||
| 1065 | my $chrom_end = shift; | ||
| 1066 | |||
| 1067 | my $sql = "SELECT f.* FROM feature f ". | ||
| 1068 | "JOIN feature_source using(feature_source_id) ". | ||
| 1069 | "WHERE chrom_id =? ". | ||
| 1070 | "AND chrom_start <= ? AND chrom_end >= ? ". | ||
| 1071 | "ORDER BY chrom_start"; | ||
| 1072 | return $class->fetch_multiple($db, $sql, $chrom->id, $chrom_end, $chrom_start); | ||
| 1073 | } | ||
| 1074 | |||
| 1075 | |||
| 1076 | sub fetch_all_by_source { | ||
| 1077 | my $class = shift; | ||
| 1078 | my $db = shift; | ||
| 1079 | my $source = shift; #FeatureSource object | ||
| 1080 | |||
| 1081 | my $sql = "SELECT * FROM feature WHERE feature_source_id=? "; | ||
| 1082 | return $class->fetch_multiple($db, $sql, $source->id); | ||
| 1083 | } | ||
| 1084 | |||
| 1085 | |||
| 1086 | sub fetch_all_by_primary_name { | ||
| 1087 | my $class = shift; | ||
| 1088 | my $db = shift; | ||
| 1089 | my $primary_name = shift; | ||
| 1090 | my $source = shift; #FeatureSource object / optional | ||
| 1091 | |||
| 1092 | my $sql = "SELECT * FROM feature WHERE primary_name=? "; | ||
| 1093 | if(defined($source)) { | ||
| 1094 | $sql .= sprintf("AND feature_source_id=%d", $source->id); | ||
| 1095 | } | ||
| 1096 | #"WHERE feature_source_id=? and primary_name like '\%". $primary_name ."\%' ORDER BY chrom_start"; | ||
| 1097 | #print($sql, "\n"); | ||
| 1098 | return $class->fetch_multiple($db, $sql, $primary_name); | ||
| 1099 | } | ||
| 1100 | |||
| 1101 | sub fetch_all_by_source_symbol { | ||
| 1102 | my $class = shift; | ||
| 1103 | my $db = shift; | ||
| 1104 | my $source = shift; #FeatureSource object | ||
| 1105 | my $sym_value = shift; | ||
| 1106 | my $sym_type = shift; #optional | ||
| 1107 | my $response_limit = shift; #optional | ||
| 1108 | |||
| 1109 | return [] unless(defined($sym_value)); | ||
| 1110 | |||
| 1111 | $sym_value =~ s/\"/\\\"/g; | ||
| 1112 | $sym_value =~ s/\'/\\\'/g; #' | ||
| 1113 | |||
| 1114 | my $sql = sprintf("SELECT * from ". | ||
| 1115 | "(SELECT f.* FROM feature f ". | ||
| 1116 | "JOIN feature_2_symbol using(feature_id) ". | ||
| 1117 | "JOIN symbol using(symbol_id) ". | ||
| 1118 | "WHERE sym_value = \"%s\"", | ||
| 1119 | $sym_value); | ||
| 1120 | if(defined($sym_type)) { | ||
| 1121 | $sql .= sprintf(" AND sym_type='%s'", $sym_type); | ||
| 1122 | } | ||
| 1123 | $sql .= " )t "; | ||
| 1124 | if(defined($source)) { | ||
| 1125 | $sql .= sprintf(" WHERE feature_source_id=%d", $source->id); | ||
| 1126 | } | ||
| 1127 | $sql .= sprintf(" GROUP BY feature_id"); | ||
| 1128 | if($response_limit) { | ||
| 1129 | $sql .= sprintf(" LIMIT %d", $response_limit); | ||
| 1130 | } | ||
| 1131 | |||
| 1132 | #print($sql, "\n", ); | ||
| 1133 | return $class->fetch_multiple($db, $sql); | ||
| 1134 | } | ||
| 1135 | |||
| 1136 | |||
| 1137 | sub fetch_all_symbol_search { | ||
| 1138 | my $class = shift; | ||
| 1139 | my $db = shift; | ||
| 1140 | my $source = shift; #FeatureSource object | ||
| 1141 | my $sym_value = shift; | ||
| 1142 | my $sym_type = shift; #optional | ||
| 1143 | my $response_limit = shift; #optional | ||
| 1144 | |||
| 1145 | if(defined($source) && !($source->isa('EEDB::FeatureSource'))) { | ||
| 1146 | die('second parameter [source] must be a EEDB::FeatureSource'); | ||
| 1147 | } | ||
| 1148 | |||
| 1149 | $sym_value =~ s/\"/\\\"/g; | ||
| 1150 | $sym_value =~ s/\'/\\\'/g; #' | ||
| 1151 | |||
| 1152 | my $sql = sprintf("SELECT * from ". | ||
| 1153 | "(SELECT f.* FROM feature f ". | ||
| 1154 | "JOIN feature_2_symbol using(feature_id) ". | ||
| 1155 | "JOIN symbol using(symbol_id) ". | ||
| 1156 | "WHERE sym_value like \"%s%%\"", | ||
| 1157 | $sym_value); | ||
| 1158 | if(defined($sym_type)) { | ||
| 1159 | $sql .= sprintf(" AND sym_type='%s'", $sym_type); | ||
| 1160 | } | ||
| 1161 | $sql .= " )t "; | ||
| 1162 | if(defined($source)) { | ||
| 1163 | $sql .= sprintf(" WHERE feature_source_id=%d", $source->id); | ||
| 1164 | } | ||
| 1165 | $sql .= sprintf(" GROUP BY feature_id"); | ||
| 1166 | |||
| 1167 | #print($sql, "\n", ); | ||
| 1168 | return $class->fetch_multiple($db, $sql); | ||
| 1169 | } | ||
| 1170 | |||
| 1171 | |||
| 1172 | sub fetch_all_keyword_search { | ||
| 1173 | my $class = shift; | ||
| 1174 | my $db = shift; | ||
| 1175 | my $keywords = shift; | ||
| 1176 | my $response_limit = shift; #optional | ||
| 1177 | my @sources = @_; #(optional) FeatureSource(s) objects | ||
| 1178 | |||
| 1179 | my @fsrc_ids; | ||
| 1180 | foreach my $fsrc (@sources) { | ||
| 1181 | next unless(defined($fsrc)); | ||
| 1182 | if(!($fsrc->isa('EEDB::FeatureSource'))) { | ||
| 1183 | die('[source] must be a EEDB::FeatureSource'); | ||
| 1184 | } | ||
| 1185 | push @fsrc_ids, $fsrc->id; | ||
| 1186 | } | ||
| 1187 | |||
| 1188 | my @where_clauses; | ||
| 1189 | my @keyword_array = split (/\s+/, $keywords); | ||
| 1190 | foreach my $tok (@keyword_array) { | ||
| 1191 | $tok =~ s/^\s+//g; | ||
| 1192 | $tok =~ s/\s+$//g; | ||
| 1193 | if(length($tok) < 3) { next; } | ||
| 1194 | |||
| 1195 | $tok =~ s/_/\\_/g; | ||
| 1196 | $tok =~ s/\"/\\\"/g; | ||
| 1197 | $tok =~ s/\'/\\\'/g; #' | ||
| 1198 | push @where_clauses, sprintf("sym_value like \"%s%%\"", $tok); | ||
| 1199 | } | ||
| 1200 | if(scalar(@where_clauses) == 0) { return []; } | ||
| 1201 | |||
| 1202 | my $sql = "SELECT f.*, count(*) symbol_count FROM ". | ||
| 1203 | "feature_source join feature f using(feature_source_id) ". | ||
| 1204 | "JOIN feature_2_symbol using(feature_id) ". | ||
| 1205 | "JOIN symbol using(symbol_id) WHERE is_active='y' and ("; | ||
| 1206 | $sql .= join(" or ", @where_clauses) . ") "; | ||
| 1207 | $sql .= "AND feature_source_id in(" . join(',', @fsrc_ids) . ") " if(@fsrc_ids); | ||
| 1208 | $sql .= " GROUP BY f.feature_id"; | ||
| 1209 | $sql .= " ORDER BY symbol_count desc"; | ||
| 1210 | if($response_limit) { $sql .= sprintf(" LIMIT %d", $response_limit); } | ||
| 1211 | |||
| 1212 | print($sql, "\n", ); | ||
| 1213 | return $class->fetch_multiple($db, $sql); | ||
| 1214 | } | ||
| 1215 | |||
| 1216 | |||
| 1217 | sub get_count_symbol_search { | ||
| 1218 | my $class = shift; | ||
| 1219 | my $db = shift; | ||
| 1220 | my $sym_value = shift; | ||
| 1221 | my $sym_type = shift; #optional | ||
| 1222 | |||
| 1223 | $sym_value =~ s/\"/\\\"/g; | ||
| 1224 | $sym_value =~ s/\'/\\\'/g; #' | ||
| 1225 | |||
| 1226 | my $sql = sprintf("select count(distinct feature_id) from symbol join feature_2_symbol using(symbol_id) WHERE sym_value like '%s'", $sym_value); | ||
| 1227 | if(defined($sym_type)) { | ||
| 1228 | $sql .= sprintf(" AND sym_type='%s'", $sym_type); | ||
| 1229 | } | ||
| 1230 | return $class->fetch_col_value($db, $sql); | ||
| 1231 | } | ||
| 1232 | |||
| 1233 | sub fetch_all_with_data { | ||
| 1234 | my $class = shift; | ||
| 1235 | my $db = shift; | ||
| 1236 | my $data = shift; #metadata.data | ||
| 1237 | my $data_type = shift; #optional | ||
| 1238 | my $source = shift; #optional FeatureSource object | ||
| 1239 | |||
| 1240 | my $sql = "SELECT f.* from metadata m JOIN feature_2_metadata using(metadata_id) ". | ||
| 1241 | "JOIN feature f using(feature_id) ". | ||
| 1242 | "WHERE m.data=?"; | ||
| 1243 | if(defined($data_type)) { | ||
| 1244 | $sql .= sprintf(" AND m.data_type='%s'", $data_type); | ||
| 1245 | } | ||
| 1246 | if(defined($source)) { | ||
| 1247 | $sql .= sprintf("AND feature_source_id=%d", $source->id); | ||
| 1248 | } | ||
| 1249 | #print($sql, "\n", ); | ||
| 1250 | return $class->fetch_multiple($db, $sql, $data); | ||
| 1251 | } | ||
| 1252 | |||
| 1253 | |||
| 1254 | sub fetch_all_with_metadata { | ||
| 1255 | my $class = shift; | ||
| 1256 | my $source = shift; #FeatureSource object uses database of source for fetching | ||
| 1257 | my @mdata_array = @_; #Metadata objects | ||
| 1258 | |||
| 1259 | if(defined($source) && !($source->isa('EEDB::FeatureSource'))) { | ||
| 1260 | die('second parameter [source] must be a EEDB::FeatureSource'); | ||
| 1261 | } | ||
| 1262 | |||
| 1263 | if(!defined($source->database) or !defined($source->primary_id)) { return []; } | ||
| 1264 | |||
| 1265 | my @mdata_ids; | ||
| 1266 | foreach my $mdata (@mdata_array) { | ||
| 1267 | unless(defined($mdata) && $mdata->isa('EEDB::Metadata')) { | ||
| 1268 | die("$mdata is not a EEDB::Metadata"); | ||
| 1269 | } | ||
| 1270 | if(defined($mdata->primary_id)) {push @mdata_ids, $mdata->id; } | ||
| 1271 | } | ||
| 1272 | if(scalar(@mdata_ids) == 0) { return []; } #if mdata objects not stored then return [] | ||
| 1273 | |||
| 1274 | my $sql = sprintf("SELECT f.* FROM feature f JOIN feature_2_metadata using(feature_id) WHERE metadata_id in(%s) ", | ||
| 1275 | join(',', @mdata_ids)); | ||
| 1276 | $sql .= sprintf(" AND feature_source_id=%d", $source->id); | ||
| 1277 | |||
| 1278 | #print($sql, "\n", ); | ||
| 1279 | return $class->fetch_multiple($source->database, $sql); | ||
| 1280 | } | ||
| 1281 | |||
| 1282 | sub fetch_all_with_symbol { | ||
| 1283 | my $class = shift; | ||
| 1284 | my $db = shift; | ||
| 1285 | my $source = shift; #FeatureSource object | ||
| 1286 | my $symbol = shift; #Symbol object | ||
| 1287 | my $response_limit = shift; #optional | ||
| 1288 | |||
| 1289 | unless(defined($source) && $source->isa('EEDB::FeatureSource')) { | ||
| 1290 | die('second parameter [source] must be a EEDB::FeatureSource'); | ||
| 1291 | } | ||
| 1292 | unless(defined($symbol) && $symbol->isa('EEDB::Symbol')) { | ||
| 1293 | die('third parameter [symbol] must be a EEDB::Symbol'); | ||
| 1294 | } | ||
| 1295 | |||
| 1296 | my $sql = sprintf("SELECT f.* FROM feature f ". | ||
| 1297 | "JOIN feature_2_symbol using(feature_id) ". | ||
| 1298 | "WHERE symbol_id = %d ", | ||
| 1299 | $symbol->id); | ||
| 1300 | if(defined($source)) { | ||
| 1301 | $sql .= sprintf("AND feature_source_id=%d", $source->id); | ||
| 1302 | } | ||
| 1303 | $sql .= sprintf(" GROUP BY feature_id"); | ||
| 1304 | if($response_limit) { | ||
| 1305 | $sql .= sprintf(" LIMIT %d", $response_limit); | ||
| 1306 | } | ||
| 1307 | |||
| 1308 | #print($sql, "\n", ); | ||
| 1309 | return $class->fetch_multiple($db, $sql); | ||
| 1310 | } | ||
| 1311 | |||
| 1312 | |||
| 1313 | #does a multi-symbol search, all symbols must be linked to the feature | ||
| 1314 | sub fetch_all_with_symbols { | ||
| 1315 | my $class = shift; | ||
| 1316 | my $db = shift; | ||
| 1317 | my $source = shift; #FeatureSource object | ||
| 1318 | my @symbols = @_; #Symbol objects | ||
| 1319 | |||
| 1320 | if(defined($source) && !($source->isa('EEDB::FeatureSource'))) { | ||
| 1321 | die('second parameter [source] must be a EEDB::FeatureSource'); | ||
| 1322 | } | ||
| 1323 | |||
| 1324 | my @symbol_ids; | ||
| 1325 | foreach my $symbol (@symbols) { | ||
| 1326 | unless(defined($symbol) && $symbol->isa('EEDB::Symbol')) { | ||
| 1327 | die("$symbol is not a EEDB::Symbol"); | ||
| 1328 | } | ||
| 1329 | push @symbol_ids, $symbol->id; | ||
| 1330 | } | ||
| 1331 | |||
| 1332 | my $sql = sprintf("SELECT f.* FROM feature f, ". | ||
| 1333 | "(select feature_id, count(*) cnt from feature_2_symbol where symbol_id in(%s) ". | ||
| 1334 | " group by feature_id)t where cnt=%d and f.feature_id = t.feature_id ", | ||
| 1335 | join(',', @symbol_ids), | ||
| 1336 | scalar(@symbol_ids) | ||
| 1337 | ); | ||
| 1338 | if(defined($source)) { | ||
| 1339 | $sql .= sprintf(" AND feature_source_id=%d", $source->id); | ||
| 1340 | } | ||
| 1341 | |||
| 1342 | #print($sql, "\n", ); | ||
| 1343 | return $class->fetch_multiple($db, $sql); | ||
| 1344 | } | ||
| 1345 | |||
| 1346 | ############################################################################################### | ||
| 1347 | # | ||
| 1348 | # streaming API section | ||
| 1349 | # | ||
| 1350 | ############################################################################################### | ||
| 1351 | |||
| 1352 | |||
| 1353 | =head2 stream_all | ||
| 1354 | |||
| 1355 | Description: stream all features out of database with a given set of source filters | ||
| 1356 | Arg (1) : $database (MQdb::Database) | ||
| 1357 | Arg (2...) : hash named filter parameters. | ||
| 1358 | sources=>[$fsrc1, $fsrc2,$fsrc3], instances of EEDB::FeatureSource | ||
| 1359 | Returntype : a DBStream instance | ||
| 1360 | Exceptions : none | ||
| 1361 | |||
| 1362 | =cut | ||
| 1363 | |||
| 1364 | sub stream_all { | ||
| 1365 | my $class = shift; | ||
| 1366 | my $db = shift; #database | ||
| 1367 | my %options = @_; #like sources=>[$fsrc1, $fsrc2,$fsrc3] | ||
| 1368 | |||
| 1369 | return [] unless($db); | ||
| 1370 | |||
| 1371 | my @sources = @{$options{'sources'}} if($options{'sources'}); | ||
| 1372 | |||
| 1373 | my @fsrc_ids; | ||
| 1374 | foreach my $source (@sources) { | ||
| 1375 | next unless($source); | ||
| 1376 | if($source->class eq 'FeatureSource') { push @fsrc_ids, $source->id; } | ||
| 1377 | } | ||
| 1378 | |||
| 1379 | my $sql = "SELECT * FROM feature f WHERE 1=1 "; | ||
| 1380 | if(@fsrc_ids) { $sql .= sprintf(" AND feature_source_id in(%s) ", join(',', @fsrc_ids)); } | ||
| 1381 | $sql .= " ORDER BY chrom_id, chrom_start, chrom_end, feature_id"; | ||
| 1382 | #print($sql, "\n"); | ||
| 1383 | return $class->stream_multiple($db, $sql); | ||
| 1384 | } | ||
| 1385 | |||
| 1386 | |||
| 1387 | =head2 stream_by_chrom | ||
| 1388 | |||
| 1389 | Description : uses an EEDB::Chrom object to stream all features in sorted order by chrom start,end | ||
| 1390 | Useful for feeding features into EEDB::Tools::OverlapCompare2 | ||
| 1391 | Arg [1] : EEDB::Chrom object with database connection | ||
| 1392 | Arg (2...) : hash named filter parameters. | ||
| 1393 | sources=>[$fsrc1, $fsrc2,$fsrc3], instances of EEDB::FeatureSource | ||
| 1394 | Returntype : MQdb::DBStream object | ||
| 1395 | Exceptions : die if anything other than EEDB::FeatureSource(s) are passed in the optional parameter list | ||
| 1396 | Example : | ||
| 1397 | my $chroms = EEDB::Chrom->fetch_all_by_assembly($assembly); | ||
| 1398 | foreach my $chrom (@$chroms) { | ||
| 1399 | my $stream = EEDB::Feature->stream_by_chrom($chrom, sources=>[$fsrc1, $fsrc2]); | ||
| 1400 | my $feature1 = $stream1->next_in_stream; | ||
| 1401 | while($feature1) { | ||
| 1402 | #do something | ||
| 1403 | $feature1 = $stream1->next_in_stream; | ||
| 1404 | } | ||
| 1405 | } | ||
| 1406 | |||
| 1407 | =cut | ||
| 1408 | |||
| 1409 | sub stream_by_chrom { | ||
| 1410 | my $class = shift; | ||
| 1411 | my $chrom = shift; #Chrom object with database | ||
| 1412 | my %options = @_; #like sources=>[$fsrc1, $fsrc2,$fsrc3] | ||
| 1413 | |||
| 1414 | return undef unless($chrom and $chrom->database); | ||
| 1415 | my $db = $chrom->database; | ||
| 1416 | |||
| 1417 | my @sources = @{$options{'sources'}} if($options{'sources'}); | ||
| 1418 | |||
| 1419 | my @fsrc_ids; | ||
| 1420 | foreach my $source (@sources) { | ||
| 1421 | next unless($source); | ||
| 1422 | if($source->class eq 'FeatureSource') { push @fsrc_ids, $source->id; } | ||
| 1423 | } | ||
| 1424 | |||
| 1425 | my $sql = sprintf("SELECT * FROM feature f WHERE chrom_id=%d", $chrom->id); | ||
| 1426 | if(@fsrc_ids) { $sql .= sprintf(" AND feature_source_id in(%s) ", join(',', @fsrc_ids)); } | ||
| 1427 | $sql .= " ORDER BY chrom_start, chrom_end, feature_id"; | ||
| 1428 | #print($sql, "\n"); | ||
| 1429 | return $class->stream_multiple($db, $sql); | ||
| 1430 | } | ||
| 1431 | |||
| 1432 | |||
| 1433 | =head2 stream_by_named_region | ||
| 1434 | |||
| 1435 | Description: stream all features from a specific region on a genome | ||
| 1436 | with a given set of source filters | ||
| 1437 | Arg (1) : $database (MQdb::Database) | ||
| 1438 | Arg (2) : $assembly_name (string) | ||
| 1439 | Arg (3) : $chrom_name (string) | ||
| 1440 | Arg (4) : $chrom_start (integer) | ||
| 1441 | Arg (5) : $chrom_end (integer) | ||
| 1442 | Arg (6...) : hash named filter parameters. | ||
| 1443 | sources=>[$fsrc1, $fsrc2,$fsrc3], instances of EEDB::FeatureSource | ||
| 1444 | Returntype : a DBStream instance | ||
| 1445 | Exceptions : none | ||
| 1446 | |||
| 1447 | =cut | ||
| 1448 | |||
| 1449 | sub stream_by_named_region { | ||
| 1450 | #returns an array of Expression objects, but the Feature object has been prebuilt, | ||
| 1451 | #so a lazy-load does not need to occur | ||
| 1452 | my $class = shift; | ||
| 1453 | my $db = shift; | ||
| 1454 | my $assembly_name = shift; | ||
| 1455 | my $chrom_name = shift; | ||
| 1456 | my $chrom_start = shift; | ||
| 1457 | my $chrom_end = shift; | ||
| 1458 | my %options = @_; #like sources=>[$fsrc1, $fsrc2,$fsrc3] | ||
| 1459 | |||
| 1460 | my @sources = @{$options{'sources'}} if($options{'sources'}); | ||
| 1461 | |||
| 1462 | my @fsrc_ids; | ||
| 1463 | foreach my $source (@sources) { | ||
| 1464 | next unless($source); | ||
| 1465 | if($source->class eq 'FeatureSource') { push @fsrc_ids, $source->id; } | ||
| 1466 | } | ||
| 1467 | |||
| 1468 | #printf("fetch_all_named_region %s : %d .. %d\n", $chrom_name, $chrom_start, $chrom_end); | ||
| 1469 | my $chrom = EEDB::Chrom->fetch_by_name($db, $assembly_name, $chrom_name); | ||
| 1470 | if(defined($chrom_start) and defined($chrom_end)) { | ||
| 1471 | my $chunks = EEDB::ChromChunk->fetch_all_named_region($db, $assembly_name, $chrom_name, $chrom_start, $chrom_end); | ||
| 1472 | return [] unless(defined($chunks) and scalar(@$chunks)>0); | ||
| 1473 | my @chunk_ids; | ||
| 1474 | foreach my $chunk (@$chunks) { push @chunk_ids, $chunk->id; } | ||
| 1475 | |||
| 1476 | my $sql=""; | ||
| 1477 | if(scalar(@$chunks) < 10) { | ||
| 1478 | $sql = "SELECT * from "; | ||
| 1479 | $sql .= "(SELECT f.* FROM feature f JOIN ". | ||
| 1480 | "(select distinct feature_id FROM feature_2_chunk ". | ||
| 1481 | " WHERE chrom_chunk_id in (". join(",", @chunk_ids). ")". | ||
| 1482 | ")fc using(feature_id)". | ||
| 1483 | "WHERE chrom_start <= ". $chrom_end ." AND chrom_end >= ". $chrom_start . " "; | ||
| 1484 | $sql .= "AND feature_source_id in(" . join(',', @fsrc_ids) . ") " if(@fsrc_ids); | ||
| 1485 | $sql .= ")f "; | ||
| 1486 | $sql .= "WHERE 1=1"; | ||
| 1487 | if(@fsrc_ids) { $sql .= sprintf(" AND feature_source_id in(%s) ", join(',', @fsrc_ids)); } | ||
| 1488 | $sql .= " ORDER BY chrom_start, chrom_end, f.feature_id"; | ||
| 1489 | return $class->stream_multiple($db, $sql); | ||
| 1490 | } | ||
| 1491 | } | ||
| 1492 | |||
| 1493 | my $sql = "SELECT * from feature f WHERE chrom_id =" . $chrom->id; | ||
| 1494 | if(defined($chrom_start) and defined($chrom_end)) { | ||
| 1495 | $sql .= " AND chrom_start <= ". $chrom_end ." AND chrom_end >= ". $chrom_start; | ||
| 1496 | } | ||
| 1497 | if(@fsrc_ids) { $sql .= " AND feature_source_id in(" . join(',', @fsrc_ids) . ") "; } | ||
| 1498 | $sql .= " ORDER BY chrom_start, chrom_end, f.feature_id"; | ||
| 1499 | |||
| 1500 | #print($sql, "\n"); | ||
| 1501 | return $class->stream_multiple($db, $sql); | ||
| 1502 | } | ||
| 1503 | |||
| 1504 | =head2 stream_all_by_source | ||
| 1505 | |||
| 1506 | Description: stream all features from a single FeatureSource | ||
| 1507 | Arg (1) : $sourcedatabase (EEDB::FeatureSource with database) | ||
| 1508 | Returntype : a DBStream instance | ||
| 1509 | Exceptions : none | ||
| 1510 | Comments : this is an older API method and despite being simple and fast | ||
| 1511 | the stream_all() method now replaces this call. It may be | ||
| 1512 | deprecated in the near future. | ||
| 1513 | |||
| 1514 | =cut | ||
| 1515 | |||
| 1516 | sub stream_all_by_source { | ||
| 1517 | my $class = shift; | ||
| 1518 | my $source = shift; #FeatureSource object with database connection | ||
| 1519 | |||
| 1520 | return undef unless($source and $source->database); | ||
| 1521 | |||
| 1522 | my $sql = "SELECT * FROM feature WHERE feature_source_id=? "; | ||
| 1523 | return $class->stream_multiple($source->database, $sql, $source->id); | ||
| 1524 | } | ||
| 1525 | |||
| 1526 | |||
| 1527 | ############################################################################################### | ||
| 1528 | # | ||
| 1529 | # internal methods | ||
| 1530 | # | ||
| 1531 | ############################################################################################### | ||
| 1532 | |||
| 1533 | sub _load_metadata { | ||
| 1534 | my $self = shift; | ||
| 1535 | |||
| 1536 | return if(defined($self->{'metadataset'})); | ||
| 1537 | $self->{'metadataset'} = new EEDB::MetadataSet; | ||
| 1538 | |||
| 1539 | return unless($self->database); #not connected yet so can't load | ||
| 1540 | my $symbols = EEDB::Symbol->fetch_all_by_feature_id($self->database, $self->id); | ||
| 1541 | $self->{'metadataset'}->add_metadata(@$symbols); | ||
| 1542 | |||
| 1543 | my $mdata = EEDB::Metadata->fetch_all_by_feature($self); | ||
| 1544 | $self->{'metadataset'}->add_metadata(@$mdata); | ||
| 1545 | } | ||
| 1546 | |||
| 1547 | |||
| 1548 | sub link_2_chunk { | ||
| 1549 | my $self = shift; | ||
| 1550 | return undef if(($self->chrom_start == -1) or ($self->chrom_end == -1)); | ||
| 1551 | my $sql ="INSERT ignore into feature_2_chunk ". | ||
| 1552 | "SELECT feature_id, chrom_chunk_id from chrom_chunk c, feature f ". | ||
| 1553 | "WHERE f.chrom_id=c.chrom_id and c.chrom_start<=f.chrom_end and c.chrom_end>=f.chrom_start and f.feature_id=?"; | ||
| 1554 | $self->database->execute_sql($sql, $self->id); | ||
| 1555 | } | ||
| 1556 | |||
| 1557 | sub _get_max_expression { | ||
| 1558 | my $self = shift; | ||
| 1559 | |||
| 1560 | my $sql ="SELECT platform, max(express) FROM (". | ||
| 1561 | "SELECT platform, max(value) express ". | ||
| 1562 | "FROM experiment ". | ||
| 1563 | "JOIN expression using (experiment_id) ". | ||
| 1564 | "JOIN expression_datatype using(datatype_id) ". | ||
| 1565 | "WHERE feature_id = ? and sig_error >=0.99 and datatype='norm' group by platform ". | ||
| 1566 | "UNION ". | ||
| 1567 | "SELECT platform, max(value) express ". | ||
| 1568 | "FROM edge ". | ||
| 1569 | "JOIN edge_source fls using(edge_source_id) ". | ||
| 1570 | "JOIN expression on(feature_id=feature1_id) ". | ||
| 1571 | "JOIN experiment e using (experiment_id) ". | ||
| 1572 | "JOIN expression_datatype on(expression.datatype_id = expression_datatype.datatype_id) ". | ||
| 1573 | "WHERE feature2_id = ? and sig_error >=0.99 and datatype='norm' and e.is_active='y' and fls.is_active='y' group by platform ". | ||
| 1574 | ")t group by platform"; | ||
| 1575 | |||
| 1576 | my $dbc = $self->database->get_connection; | ||
| 1577 | my $sth = $dbc->prepare($sql); | ||
| 1578 | $sth->execute($self->primary_id, $self->primary_id); | ||
| 1579 | while(my ($platform, $max_express) = $sth->fetchrow_array) { | ||
| 1580 | if(!defined($self->{'max_express'})) { $self->{'max_express'} = []; } | ||
| 1581 | push @{$self->{'max_express'}}, [$platform, $max_express]; | ||
| 1582 | } | ||
| 1583 | } | ||
| 1584 | |||
| 1585 | sub _get_sum_expression { | ||
| 1586 | my $self = shift; | ||
| 1587 | |||
| 1588 | my $sql ="SELECT platform, max(express) FROM (". | ||
| 1589 | "SELECT platform, sum(value) express ". | ||
| 1590 | "FROM edge ". | ||
| 1591 | "JOIN edge_source fls using(edge_source_id) ". | ||
| 1592 | "JOIN expression on(feature_id=feature1_id) ". | ||
| 1593 | "JOIN experiment e using (experiment_id) ". | ||
| 1594 | "WHERE feature2_id = ? and sig_error >=0.99 and e.is_active='y' and fls.is_active='y' group by platform ". | ||
| 1595 | "UNION ". | ||
| 1596 | "SELECT platform, sum(value) express ". | ||
| 1597 | "FROM expression ". | ||
| 1598 | "JOIN experiment using (experiment_id) ". | ||
| 1599 | "WHERE feature_id = ? and sig_error >=0.99 group by platform ". | ||
| 1600 | ")t group by platform"; | ||
| 1601 | |||
| 1602 | my $dbc = $self->database->get_connection; | ||
| 1603 | my $sth = $dbc->prepare($sql); | ||
| 1604 | $sth->execute($self->primary_id, $self->primary_id); | ||
| 1605 | while(my ($platform, $sum_express) = $sth->fetchrow_array) { | ||
| 1606 | if(!defined($self->{'sum_express'})) { $self->{'sum_express'} = []; } | ||
| 1607 | push @{$self->{'sum_express'}}, [$platform, $sum_express]; | ||
| 1608 | } | ||
| 1609 | } | ||
| 1610 | |||
| 1611 | 1; | ||
| 1612 |