| 1 | =head1 NAME - EEDB::Expression | ||
| 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_expression_global_datatype_cache = {}; | ||
| 46 | |||
| 47 | $VERSION = 0.953; | ||
| 48 | |||
| 49 | package EEDB::Expression; | ||
| 50 | |||
| 51 | use strict; | ||
| 52 | use Time::HiRes qw(time gettimeofday tv_interval); | ||
| 53 | |||
| 54 | use EEDB::Feature; | ||
| 55 | use EEDB::Experiment; | ||
| 56 | |||
| 57 | use MQdb::MappedQuery; | ||
| 58 | our @ISA = qw(MQdb::MappedQuery); | ||
| 59 | |||
| 60 | ################################################# | ||
| 61 | # Class methods | ||
| 62 | ################################################# | ||
| 63 | |||
| 64 | sub class { return "Expression"; } | ||
| 65 | |||
| 66 | ################################################# | ||
| 67 | # Instance methods | ||
| 68 | ################################################# | ||
| 69 | |||
| 70 | sub init { | ||
| 71 | my $self = shift; | ||
| 72 | $self->SUPER::init; | ||
| 73 | |||
| 74 | $self->{'type'}=undef; | ||
| 75 | $self->{'value'}=0.0; | ||
| 76 | $self->{'sig_error'}=1.0; | ||
| 77 | |||
| 78 | return $self; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub database { | ||
| 82 | my $self = shift; | ||
| 83 | if(defined($self->{'feature'}) and @_) { $self->{'feature'}->database(@_); } | ||
| 84 | return $self->SUPER::database(@_); | ||
| 85 | } | ||
| 86 | |||
| 87 | ########################## | ||
| 88 | # | ||
| 89 | # getter/setter methods of data which is stored in database | ||
| 90 | # | ||
| 91 | ########################## | ||
| 92 | |||
| 93 | sub feature { | ||
| 94 | my $self = shift; | ||
| 95 | if(@_) { | ||
| 96 | my $feature = shift; | ||
| 97 | if(defined($feature) && !($feature->isa('EEDB::Feature'))) { | ||
| 98 | die('ERROR : feature param must be a EEDB::Feature'); | ||
| 99 | } | ||
| 100 | $self->{'feature'} = $feature; | ||
| 101 | return $feature; | ||
| 102 | } | ||
| 103 | |||
| 104 | #lazy load from database if possible | ||
| 105 | if(!defined($self->{'feature'}) and | ||
| 106 | defined($self->database) and | ||
| 107 | defined($self->{'_feature_id'})) | ||
| 108 | { | ||
| 109 | #printf("LAZY LOAD feature_id=%d\n", $self->{'_feature_id'}); | ||
| 110 | my $feature = EEDB::Feature->fetch_by_id($self->database, $self->{'_feature_id'}); | ||
| 111 | if(defined($feature)) { $self->{'feature'} = $feature; } | ||
| 112 | } | ||
| 113 | |||
| 114 | return $self->{'feature'}; | ||
| 115 | } | ||
| 116 | |||
| 117 | |||
| 118 | sub experiment { | ||
| 119 | my $self = shift; | ||
| 120 | if(@_) { | ||
| 121 | my $experiment = shift; | ||
| 122 | unless(defined($experiment) && $experiment->isa('EEDB::Experiment')) { | ||
| 123 | die('experiment param must be a EEDB::Experiment'); | ||
| 124 | } | ||
| 125 | $self->{'experiment'} = $experiment; | ||
| 126 | } | ||
| 127 | |||
| 128 | #lazy load from database if possible | ||
| 129 | if(!defined($self->{'experiment'}) and | ||
| 130 | defined($self->database) and | ||
| 131 | defined($self->{'_experiment_id'})) | ||
| 132 | { | ||
| 133 | #printf("LAZY LOAD experiment_id=%d\n", $self->{'_experiment_id'}); | ||
| 134 | my $experiment = EEDB::Experiment->fetch_by_id($self->database, $self->{'_experiment_id'}); | ||
| 135 | if(defined($experiment)) { $self->{'experiment'} = $experiment; } | ||
| 136 | } | ||
| 137 | |||
| 138 | return $self->{'experiment'}; | ||
| 139 | } | ||
| 140 | |||
| 141 | sub type { | ||
| 142 | my $self = shift; | ||
| 143 | return $self->{'type'} = shift if(@_); | ||
| 144 | if(!defined($self->{'type'}) and defined($self->{'_datatype_id'})) { | ||
| 145 | $self->{'type'} = EEDB::Expression->_get_expression_datatype($self->database, $self->{'_datatype_id'}); | ||
| 146 | } | ||
| 147 | if(defined($self->{'type'})) { return $self->{'type'}; } | ||
| 148 | else { return ''; } | ||
| 149 | } | ||
| 150 | |||
| 151 | sub value { | ||
| 152 | my $self = shift; | ||
| 153 | return $self->{'value'} = shift if(@_); | ||
| 154 | $self->{'value'}=0.0 unless(defined($self->{'value'})); | ||
| 155 | return $self->{'value'}; | ||
| 156 | } | ||
| 157 | |||
| 158 | sub sig_error { | ||
| 159 | my $self = shift; | ||
| 160 | return $self->{'sig_error'} = shift if(@_); | ||
| 161 | $self->{'sig_error'}=1.0 unless(defined($self->{'sig_error'})); | ||
| 162 | return $self->{'sig_error'}; | ||
| 163 | } | ||
| 164 | |||
| 165 | |||
| 166 | ################## | ||
| 167 | # redirect methods to work with Expression objects as if they are Feature objects | ||
| 168 | # since each expression object only has one feature this works | ||
| 169 | # but only implement read access | ||
| 170 | # | ||
| 171 | |||
| 172 | sub chrom_location { | ||
| 173 | my $self = shift; | ||
| 174 | return $self->feature->chrom_location; | ||
| 175 | } | ||
| 176 | |||
| 177 | sub chrom_name { | ||
| 178 | my $self = shift; | ||
| 179 | return $self->feature->chrom_name; | ||
| 180 | } | ||
| 181 | |||
| 182 | sub chrom_id { | ||
| 183 | my $self = shift; | ||
| 184 | return $self->feature->chrom_id; | ||
| 185 | } | ||
| 186 | |||
| 187 | sub chrom { | ||
| 188 | my $self = shift; | ||
| 189 | return $self->feature->chrom; | ||
| 190 | } | ||
| 191 | |||
| 192 | sub chrom_start { | ||
| 193 | my $self = shift; | ||
| 194 | return $self->feature->chrom_start(@_); | ||
| 195 | } | ||
| 196 | |||
| 197 | sub chrom_end { | ||
| 198 | my $self = shift; | ||
| 199 | return $self->feature->chrom_end(@_); | ||
| 200 | } | ||
| 201 | |||
| 202 | sub strand { | ||
| 203 | my $self = shift; | ||
| 204 | return $self->feature->strand; | ||
| 205 | } | ||
| 206 | |||
| 207 | sub primary_name { | ||
| 208 | my $self = shift; | ||
| 209 | return $self->feature->primary_name; | ||
| 210 | } | ||
| 211 | |||
| 212 | ################## | ||
| 213 | |||
| 214 | sub display_desc { | ||
| 215 | my $self = shift; | ||
| 216 | return sprintf("Expression(db %s ) %s <=> %s : [%s | %1.7f value | %1.5f sigerr]", | ||
| 217 | $self->id, | ||
| 218 | $self->feature->primary_name, | ||
| 219 | $self->experiment->display_name, | ||
| 220 | $self->type, | ||
| 221 | $self->value, | ||
| 222 | $self->sig_error | ||
| 223 | ); | ||
| 224 | } | ||
| 225 | |||
| 226 | sub xml { | ||
| 227 | my $self = shift; | ||
| 228 | return $self->simple_xml; | ||
| 229 | } | ||
| 230 | |||
| 231 | sub simple_xml { | ||
| 232 | my $self = shift; | ||
| 233 | |||
| 234 | |||
| 235 | my $str = sprintf("<expression type=\"%s\" value=\"%f\" sig_error=\"%f\" feature_id=\"%s\" ", | ||
| 236 | $self->type, | ||
| 237 | $self->value, | ||
| 238 | $self->sig_error, | ||
| 239 | $self->feature->id); | ||
| 240 | if($self->experiment) { | ||
| 241 | $str .= sprintf("platform=\"%s\" ", $self->experiment->platform) if($self->experiment->platform); | ||
| 242 | $str .= sprintf("experiment_id=\"%s\" ", $self->experiment->id) if($self->experiment->id); | ||
| 243 | $str .= sprintf("exp_name=\"%s\" ", $self->experiment->display_name) if($self->experiment->display_name); | ||
| 244 | $str .= sprintf("series_name=\"%s\" ", $self->experiment->series_name) if($self->experiment->series_name); | ||
| 245 | $str .= sprintf("series_point=\"%s\" ", $self->experiment->series_point) if(defined($self->experiment->series_point)); | ||
| 246 | } | ||
| 247 | $str .= "/>\n"; | ||
| 248 | return $str; | ||
| 249 | } | ||
| 250 | |||
| 251 | |||
| 252 | ################################################# | ||
| 253 | # | ||
| 254 | # DBObject override methods | ||
| 255 | # | ||
| 256 | ################################################# | ||
| 257 | |||
| 258 | sub store { | ||
| 259 | my $self = shift; | ||
| 260 | my $db = shift; | ||
| 261 | |||
| 262 | if($db) { $self->database($db); } | ||
| 263 | my $dbh = $self->database->get_connection; | ||
| 264 | my $sql = "INSERT ignore INTO expression ". | ||
| 265 | "(experiment_id, feature_id, type, value, sig_error) ". | ||
| 266 | "VALUES(?,?,?,?,?)"; | ||
| 267 | my $sth = $dbh->prepare($sql); | ||
| 268 | $sth->execute($self->experiment->id, | ||
| 269 | $self->feature->id, | ||
| 270 | $self->type, | ||
| 271 | $self->value, | ||
| 272 | $self->sig_error | ||
| 273 | ); | ||
| 274 | my $dbID = $dbh->last_insert_id(undef, undef, qw(expression expression_id)); | ||
| 275 | $sth->finish; | ||
| 276 | if(!$dbID) { | ||
| 277 | $dbID = $self->fetch_col_value( | ||
| 278 | $db, | ||
| 279 | "select expression_id from expression where experiment_id=? and feature_id=?", | ||
| 280 | $self->experiment->id, | ||
| 281 | $self->feature->id); | ||
| 282 | |||
| 283 | } | ||
| 284 | $self->primary_id($dbID); | ||
| 285 | |||
| 286 | return $self; | ||
| 287 | } | ||
| 288 | |||
| 289 | |||
| 290 | ##### DBObject instance override methods ##### | ||
| 291 | |||
| 292 | sub mapRow { | ||
| 293 | my $self = shift; | ||
| 294 | my $rowHash = shift; | ||
| 295 | my $dbh = shift; | ||
| 296 | |||
| 297 | $self->{'_primary_db_id'} = $rowHash->{'expression_id'}; | ||
| 298 | $self->{'type'} = $rowHash->{'datatype'} if(defined($rowHash->{'datatype'})); | ||
| 299 | $self->{'value'} = $rowHash->{'value'}; | ||
| 300 | $self->{'sig_error'} = $rowHash->{'sig_error'}; | ||
| 301 | |||
| 302 | $self->{'_feature_id'} = $rowHash->{'feature_id'}; | ||
| 303 | $self->{'_experiment_id'} = $rowHash->{'experiment_id'}; | ||
| 304 | $self->{'_datatype_id'} = $rowHash->{'datatype_id'}; | ||
| 305 | |||
| 306 | if($rowHash->{'feature_source_id'}) { | ||
| 307 | #create feature now | ||
| 308 | my $feature = EEDB::Feature->new(); | ||
| 309 | $feature->mapRow($rowHash); | ||
| 310 | $feature->database($self->database) if($self->database); | ||
| 311 | $self->{'feature'} = $feature; | ||
| 312 | } | ||
| 313 | |||
| 314 | return $self; | ||
| 315 | } | ||
| 316 | |||
| 317 | |||
| 318 | ##### public class methods for fetching by utilizing DBObject framework methods ##### | ||
| 319 | |||
| 320 | sub fetch_by_id { | ||
| 321 | my $class = shift; | ||
| 322 | my $db = shift; | ||
| 323 | my $id = shift; | ||
| 324 | |||
| 325 | my $sql = "SELECT * FROM expression WHERE expression_id=?"; | ||
| 326 | return $class->fetch_single($db, $sql, $id); | ||
| 327 | } | ||
| 328 | |||
| 329 | |||
| 330 | sub fetch_by_feature_experiment_type { | ||
| 331 | #must specify all since this will return a unique Expression object | ||
| 332 | my $class = shift; | ||
| 333 | my $feature = shift; #Feature object | ||
| 334 | my $experiment = shift; #Experiment object | ||
| 335 | my $type = shift; | ||
| 336 | |||
| 337 | unless(defined($feature) && $feature->isa('EEDB::Feature')) { | ||
| 338 | die('ERROR : fetch_by_feature_experiment_type param[1] must be a EEDB::Feature'); | ||
| 339 | } | ||
| 340 | unless(defined($experiment) && $experiment->isa('EEDB::Experiment')) { | ||
| 341 | die('ERROR : fetch_by_feature_experiment_type param[2] must be a EEDB::Experiment'); | ||
| 342 | } | ||
| 343 | unless(defined($type)) { | ||
| 344 | die('ERROR : fetch_by_feature_experiment_type param[3] must be a defined scalar'); | ||
| 345 | } | ||
| 346 | |||
| 347 | my $datatype_id = $class->_fetch_expression_datatype_id($feature->database, $type); | ||
| 348 | if(!$datatype_id) { return undef; } | ||
| 349 | |||
| 350 | my $sql = "SELECT * FROM expression e ". | ||
| 351 | "WHERE experiment_id=? and feature_id=? and e.datatype_id=?"; | ||
| 352 | return $class->fetch_single($feature->database, $sql, $experiment->id, $feature->id, $datatype_id); | ||
| 353 | } | ||
| 354 | |||
| 355 | |||
| 356 | sub fetch_all_by_feature_experiment { | ||
| 357 | my $class = shift; | ||
| 358 | my $feature = shift; #Feature object | ||
| 359 | my $experiment = shift; #Experiment object | ||
| 360 | |||
| 361 | unless(defined($feature) && $feature->isa('EEDB::Feature')) { | ||
| 362 | die('ERROR : fetch_by_feature_experiment_type param[1] must be a EEDB::Feature'); | ||
| 363 | } | ||
| 364 | unless(defined($experiment) && $experiment->isa('EEDB::Experiment')) { | ||
| 365 | die('ERROR : fetch_by_feature_experiment_type param[2] must be a EEDB::Experiment'); | ||
| 366 | } | ||
| 367 | my $sql = "SELECT * FROM expression ". | ||
| 368 | "WHERE experiment_id=? and feature_id=?"; | ||
| 369 | return $class->fetch_multiple($feature->database, $sql, $experiment->id, $feature->id); | ||
| 370 | } | ||
| 371 | |||
| 372 | sub fetch_all_by_feature { | ||
| 373 | my $class = shift; | ||
| 374 | my $feature = shift; #Feature object | ||
| 375 | my $type = shift; #optional type | ||
| 376 | |||
| 377 | my $sql = "SELECT * FROM expression e ". | ||
| 378 | "WHERE feature_id=?"; | ||
| 379 | if(defined($type)) { | ||
| 380 | my $datatype_id = $class->_fetch_expression_datatype_id($feature->database, $type); | ||
| 381 | if(!$datatype_id) { return []; } | ||
| 382 | $sql .=" AND datatype_id=" . $datatype_id; | ||
| 383 | } | ||
| 384 | |||
| 385 | return $class->fetch_multiple($feature->database, $sql, $feature->id); | ||
| 386 | } | ||
| 387 | |||
| 388 | sub fetch_all_by_experiment { | ||
| 389 | my $class = shift; | ||
| 390 | my $experiment = shift; #Experiment object | ||
| 391 | |||
| 392 | my $sql = "SELECT * FROM expression ". | ||
| 393 | "WHERE experiment_id=? "; | ||
| 394 | return $class->fetch_multiple($experiment->database, $sql, $experiment->id); | ||
| 395 | } | ||
| 396 | |||
| 397 | sub fetch_all_feature_expression_by_named_region { | ||
| 398 | my $class = shift; | ||
| 399 | my $stream = EEDB::Expression->stream_by_named_region(@_); | ||
| 400 | return $stream->as_array; | ||
| 401 | } | ||
| 402 | |||
| 403 | ############################################################################################### | ||
| 404 | # | ||
| 405 | # streaming API section | ||
| 406 | # | ||
| 407 | ############################################################################################### | ||
| 408 | |||
| 409 | |||
| 410 | =head2 stream_all | ||
| 411 | |||
| 412 | Description: stream all expression (with feature) out of database | ||
| 413 | with a given set of source, experiment and datatype filters | ||
| 414 | Arg (1) : $database (MQdb::Database) | ||
| 415 | Arg (2...) : hash named filter parameters. | ||
| 416 | datatypes=>["tpm","raw"], | ||
| 417 | sources=>[$fsrc1, $fsrc2,$fsrc3], instances of EEDB::FeatureSource | ||
| 418 | experiments=>[$exp1, $exp2] instances of EEDB::Experiment | ||
| 419 | Returntype : a DBStream instance | ||
| 420 | Exceptions : none | ||
| 421 | |||
| 422 | =cut | ||
| 423 | |||
| 424 | sub stream_all { | ||
| 425 | my $class = shift; | ||
| 426 | my $db = shift; #database | ||
| 427 | my %options = @_; #like datatypes=>["tpm","raw"], sources=>[$esrc1, $esrc2,$esrc3], experiments=>[exp1] | ||
| 428 | |||
| 429 | return [] unless($db); | ||
| 430 | |||
| 431 | my @types = @{$options{'datatypes'}} if($options{'datatypes'}); | ||
| 432 | my @sources = @{$options{'sources'}} if($options{'sources'}); | ||
| 433 | my @experiments = @{$options{'experiments'}} if($options{'experiments'}); | ||
| 434 | |||
| 435 | my @fsrc_ids; | ||
| 436 | my @exp_ids; | ||
| 437 | my @type_ids; | ||
| 438 | foreach my $source (@sources) { | ||
| 439 | next unless($source); | ||
| 440 | if($source->class eq 'FeatureSource') { push @fsrc_ids, $source->id; } | ||
| 441 | } | ||
| 442 | foreach my $exp (@experiments) { | ||
| 443 | next unless($exp); | ||
| 444 | if($exp->class eq 'Experiment') { push @exp_ids, $exp->id; } | ||
| 445 | } | ||
| 446 | foreach my $type (@types) { | ||
| 447 | my $datatype_id = $class->_fetch_expression_datatype_id($db, $type); | ||
| 448 | if($datatype_id) { push @type_ids, $datatype_id; } | ||
| 449 | } | ||
| 450 | |||
| 451 | my $sql = "SELECT * FROM feature f JOIN expression fe using(feature_id) WHERE 1=1 "; | ||
| 452 | if(@fsrc_ids) { $sql .= sprintf(" AND feature_source_id in(%s) ", join(',', @fsrc_ids)); } | ||
| 453 | if(@exp_ids) { $sql .= sprintf(" AND experiment_id in (%s) ", join(',', @exp_ids)); } | ||
| 454 | if(@type_ids) { $sql .= sprintf(" AND datatype_id in (%s) ", join(',', @type_ids)); } | ||
| 455 | $sql .= " ORDER BY chrom_id, chrom_start, chrom_end, fe.feature_id, experiment_id, datatype_id"; | ||
| 456 | #print($sql, "\n"); | ||
| 457 | return $class->stream_multiple($db, $sql); | ||
| 458 | } | ||
| 459 | |||
| 460 | =head2 stream_by_chrom | ||
| 461 | |||
| 462 | Description: stream all expression (with feature) on a specific EEDB::Chrom | ||
| 463 | with a given set of source, experiment and datatype filters | ||
| 464 | Arg (1) : $chrom (EEDB::Chrom object with database) | ||
| 465 | Arg (2...) : hash named filter parameters. | ||
| 466 | datatypes=>["tpm","raw"], | ||
| 467 | sources=>[$fsrc1, $fsrc2,$fsrc3], instances of EEDB::FeatureSource | ||
| 468 | experiments=>[$exp1, $exp2] instances of EEDB::Experiment | ||
| 469 | Returntype : a DBStream instance | ||
| 470 | Exceptions : none | ||
| 471 | |||
| 472 | =cut | ||
| 473 | |||
| 474 | sub stream_by_chrom { | ||
| 475 | my $class = shift; | ||
| 476 | my $chrom = shift; #Chrom object with database | ||
| 477 | my %options = @_; #like datatypes=>["tpm","raw"], sources=>[$esrc1, $esrc2,$esrc3], experiments=>[exp1] | ||
| 478 | |||
| 479 | return undef unless($chrom and $chrom->database); | ||
| 480 | my $db = $chrom->database; | ||
| 481 | |||
| 482 | my @types = @{$options{'datatypes'}} if($options{'datatypes'}); | ||
| 483 | my @sources = @{$options{'sources'}} if($options{'sources'}); | ||
| 484 | my @experiments = @{$options{'experiments'}} if($options{'experiments'}); | ||
| 485 | |||
| 486 | my @fsrc_ids; | ||
| 487 | my @exp_ids; | ||
| 488 | my @type_ids; | ||
| 489 | foreach my $source (@sources) { | ||
| 490 | next unless($source); | ||
| 491 | if($source->class eq 'FeatureSource') { push @fsrc_ids, $source->id; } | ||
| 492 | } | ||
| 493 | foreach my $exp (@experiments) { | ||
| 494 | next unless($exp); | ||
| 495 | if($exp->class eq 'Experiment') { push @exp_ids, $exp->id; } | ||
| 496 | } | ||
| 497 | foreach my $type (@types) { | ||
| 498 | my $datatype_id = $class->_fetch_expression_datatype_id($db, $type); | ||
| 499 | if($datatype_id) { push @type_ids, $datatype_id; } | ||
| 500 | } | ||
| 501 | |||
| 502 | my $sql = sprintf("SELECT * FROM feature f ". | ||
| 503 | "JOIN expression fe using(feature_id) ". | ||
| 504 | "WHERE chrom_id=%d", $chrom->id); | ||
| 505 | if(@fsrc_ids) { $sql .= sprintf(" AND feature_source_id in(%s) ", join(',', @fsrc_ids)); } | ||
| 506 | if(@exp_ids) { $sql .= sprintf(" AND experiment_id in (%s) ", join(',', @exp_ids)); } | ||
| 507 | if(@type_ids) { $sql .= sprintf(" AND datatype_id in (%s) ", join(',', @type_ids)); } | ||
| 508 | $sql .= " ORDER BY chrom_start, chrom_end, fe.feature_id, experiment_id, datatype_id"; | ||
| 509 | #print($sql, "\n"); | ||
| 510 | return $class->stream_multiple($db, $sql); | ||
| 511 | } | ||
| 512 | |||
| 513 | =head2 stream_by_named_region | ||
| 514 | |||
| 515 | Description: stream all expression (with feature) from a specific region on a genome | ||
| 516 | with a given set of source, experiment and datatype filters | ||
| 517 | Arg (1) : $database (MQdb::Database) | ||
| 518 | Arg (2) : $assembly_name (string) | ||
| 519 | Arg (3) : $chrom_name (string) | ||
| 520 | Arg (4) : $chrom_start (integer) | ||
| 521 | Arg (5) : $chrom_end (integer) | ||
| 522 | Arg (6...) : hash named filter parameters. | ||
| 523 | datatypes=>["tpm","raw"], | ||
| 524 | sources=>[$fsrc1, $fsrc2,$fsrc3], instances of EEDB::FeatureSource | ||
| 525 | experiments=>[$exp1, $exp2] instances of EEDB::Experiment | ||
| 526 | Returntype : a DBStream instance | ||
| 527 | Exceptions : none | ||
| 528 | |||
| 529 | =cut | ||
| 530 | |||
| 531 | sub stream_by_named_region { | ||
| 532 | #returns an array of Expression objects, but the Feature object has been prebuilt, | ||
| 533 | #so a lazy-load does not need to occur | ||
| 534 | my $class = shift; | ||
| 535 | my $db = shift; | ||
| 536 | my $assembly_name = shift; | ||
| 537 | my $chrom_name = shift; | ||
| 538 | my $chrom_start = shift; | ||
| 539 | my $chrom_end = shift; | ||
| 540 | my %options = @_; #like datatypes=>["tpm","raw"], sources=>[$esrc1, $esrc2,$esrc3], experiments=>[exp1] | ||
| 541 | |||
| 542 | my @types = @{$options{'datatypes'}} if($options{'datatypes'}); | ||
| 543 | my @sources = @{$options{'sources'}} if($options{'sources'}); | ||
| 544 | my @experiments = @{$options{'experiments'}} if($options{'experiments'}); | ||
| 545 | |||
| 546 | my @fsrc_ids; | ||
| 547 | my @exp_ids; | ||
| 548 | my @type_ids; | ||
| 549 | foreach my $source (@sources) { | ||
| 550 | next unless($source); | ||
| 551 | if($source->class eq 'FeatureSource') { push @fsrc_ids, $source->id; } | ||
| 552 | } | ||
| 553 | foreach my $exp (@experiments) { | ||
| 554 | next unless($exp); | ||
| 555 | if($exp->class eq 'Experiment') { push @exp_ids, $exp->id; } | ||
| 556 | } | ||
| 557 | foreach my $type (@types) { | ||
| 558 | my $datatype_id = $class->_fetch_expression_datatype_id($db, $type); | ||
| 559 | if($datatype_id) { push @type_ids, $datatype_id; } | ||
| 560 | } | ||
| 561 | |||
| 562 | #printf("fetch_all_named_region %s : %d .. %d\n", $chrom_name, $chrom_start, $chrom_end); | ||
| 563 | my $chrom = EEDB::Chrom->fetch_by_name($db, $assembly_name, $chrom_name); | ||
| 564 | my $chunks = EEDB::ChromChunk->fetch_all_named_region($db, $assembly_name, $chrom_name, $chrom_start, $chrom_end); | ||
| 565 | return [] unless(defined($chunks) and scalar(@$chunks)>0); | ||
| 566 | my @chunk_ids; | ||
| 567 | foreach my $chunk (@$chunks) { push @chunk_ids, $chunk->id; } | ||
| 568 | |||
| 569 | my $sql=""; | ||
| 570 | if(scalar(@$chunks) < 10) { | ||
| 571 | $sql = "SELECT * from "; | ||
| 572 | $sql .= "(SELECT f.* FROM feature f JOIN ". | ||
| 573 | "(select distinct feature_id FROM feature_2_chunk ". | ||
| 574 | " WHERE chrom_chunk_id in (". join(",", @chunk_ids). ")". | ||
| 575 | ")fc using(feature_id)". | ||
| 576 | "WHERE chrom_start <= ". $chrom_end ." AND chrom_end >= ". $chrom_start . " "; | ||
| 577 | $sql .= "AND feature_source_id in(" . join(',', @fsrc_ids) . ") " if(@fsrc_ids); | ||
| 578 | $sql .= ")f "; | ||
| 579 | $sql .= "JOIN expression using(feature_id) "; | ||
| 580 | $sql .= "WHERE 1=1"; | ||
| 581 | if(@fsrc_ids) { $sql .= sprintf(" AND feature_source_id in(%s) ", join(',', @fsrc_ids)); } | ||
| 582 | if(@exp_ids) { $sql .= sprintf(" AND experiment_id in (%s) ", join(',', @exp_ids)); } | ||
| 583 | if(@type_ids) { $sql .= sprintf(" AND datatype_id in (%s) ", join(',', @type_ids)); } | ||
| 584 | } else { | ||
| 585 | $sql = "SELECT * from feature f JOIN expression using(feature_id) "; | ||
| 586 | $sql .= "WHERE chrom_id =" . $chrom->id; | ||
| 587 | $sql .= " AND chrom_start <= ". $chrom_end ." AND chrom_end >= ". $chrom_start; | ||
| 588 | if(@type_ids) { $sql .= sprintf(" AND datatype_id in (%s) ", join(',', @type_ids)); } | ||
| 589 | if(@fsrc_ids) { $sql .= " AND feature_source_id in(" . join(',', @fsrc_ids) . ") "; } | ||
| 590 | if(@exp_ids) { $sql .= sprintf(" AND experiment_id in (%s) ", join(',', @exp_ids)); } | ||
| 591 | } | ||
| 592 | |||
| 593 | $sql .= " ORDER BY chrom_start, chrom_end, f.feature_id, experiment_id, datatype_id"; | ||
| 594 | |||
| 595 | #print($sql, "\n"); | ||
| 596 | return $class->stream_multiple($db, $sql); | ||
| 597 | } | ||
| 598 | |||
| 599 | #################### methods to work with the expression_datatype dictionary ############### | ||
| 600 | |||
| 601 | sub _fetch_expression_datatype_id { | ||
| 602 | my $class = shift; | ||
| 603 | my $db = shift; | ||
| 604 | my $datatype = shift; | ||
| 605 | |||
| 606 | if(!defined($__riken_EEDB_expression_global_datatype_cache->{$db . $datatype})) { | ||
| 607 | my $type_id = $db->fetch_col_value("SELECT datatype_id FROM expression_datatype WHERE datatype=?", $datatype); | ||
| 608 | if($type_id) { $__riken_EEDB_expression_global_datatype_cache->{$db . $datatype} = $type_id; } | ||
| 609 | } | ||
| 610 | return $__riken_EEDB_expression_global_datatype_cache->{$db . $datatype}; | ||
| 611 | } | ||
| 612 | |||
| 613 | sub _storeget_expression_datatype_id { | ||
| 614 | my $class = shift; | ||
| 615 | my $db = shift; | ||
| 616 | my $datatype = shift; | ||
| 617 | |||
| 618 | if(!defined($__riken_EEDB_expression_global_datatype_cache->{$db . $datatype})) { | ||
| 619 | |||
| 620 | $db->execute_sql("INSERT IGNORE INTO expression_datatype set datatype=?", $datatype); | ||
| 621 | #printf("datatype_id cache miss, store it [%s]\n", $datatype); | ||
| 622 | |||
| 623 | my $type_id = $db->fetch_col_value("SELECT datatype_id FROM expression_datatype WHERE datatype=?", $datatype); | ||
| 624 | if($type_id) { $__riken_EEDB_expression_global_datatype_cache->{$db . $datatype} = $type_id; } | ||
| 625 | |||
| 626 | } | ||
| 627 | return $__riken_EEDB_expression_global_datatype_cache->{$db . $datatype}; | ||
| 628 | } | ||
| 629 | |||
| 630 | sub get_all_expression_datatypes { | ||
| 631 | my $class = shift; | ||
| 632 | my $db = shift; | ||
| 633 | |||
| 634 | my $dbc = $db->get_connection; | ||
| 635 | my $sth = $dbc->prepare("SELECT datatype_id, datatype FROM expression_datatype"); | ||
| 636 | $sth->execute(); | ||
| 637 | my @types; | ||
| 638 | while(my ($type_id, $datatype) = $sth->fetchrow_array) { | ||
| 639 | $__riken_EEDB_expression_global_datatype_cache->{$db . $datatype} = $type_id; | ||
| 640 | $__riken_EEDB_expression_global_datatype_cache->{$db .'__'. $type_id} = $datatype; | ||
| 641 | push @types, $datatype; | ||
| 642 | } | ||
| 643 | return \@types; | ||
| 644 | } | ||
| 645 | |||
| 646 | sub _get_expression_datatype { | ||
| 647 | my $class = shift; | ||
| 648 | my $db = shift; | ||
| 649 | my $type_id = shift; | ||
| 650 | |||
| 651 | my $datatype = $__riken_EEDB_expression_global_datatype_cache->{$db .'__'. $type_id}; | ||
| 652 | if(!$datatype) { | ||
| 653 | EEDB::Expression->get_all_expression_datatypes($db); | ||
| 654 | } | ||
| 655 | return $__riken_EEDB_expression_global_datatype_cache->{$db .'__'. $type_id}; | ||
| 656 | } | ||
| 657 | |||
| 658 | 1; | ||
| 659 |