| 1 | =head1 LICENSE | ||
| 2 | |||
| 3 | See the NOTICE file distributed with this work for additional information | ||
| 4 | regarding copyright ownership. | ||
| 5 | |||
| 6 | Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 7 | you may not use this file except in compliance with the License. | ||
| 8 | You may obtain a copy of the License at | ||
| 9 | |||
| 10 | http://www.apache.org/licenses/LICENSE-2.0 | ||
| 11 | |||
| 12 | Unless required by applicable law or agreed to in writing, software | ||
| 13 | distributed under the License is distributed on an "AS IS" BASIS, | ||
| 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 15 | See the License for the specific language governing permissions and | ||
| 16 | limitations under the License. | ||
| 17 | |||
| 18 | =cut | ||
| 19 | |||
| 20 | |||
| 21 | =head1 CONTACT | ||
| 22 | |||
| 23 | Please email comments or questions to the public Ensembl | ||
| 24 | developers list at <http://lists.ensembl.org/mailman/listinfo/dev>. | ||
| 25 | |||
| 26 | Questions may also be sent to the Ensembl help desk at | ||
| 27 | <http://www.ensembl.org/Help/Contact>. | ||
| 28 | |||
| 29 | =cut | ||
| 30 | |||
| 31 | =head1 NAME | ||
| 32 | |||
| 33 | Bio::EnsEMBL::DBSQL::AnalysisAdaptor | ||
| 34 | |||
| 35 | =head1 SYNOPSIS | ||
| 36 | |||
| 37 | use Bio::EnsEMBL::Registry; | ||
| 38 | |||
| 39 | Bio::EnsEMBL::Registry->load_registry_from_db( | ||
| 40 | -host => 'ensembldb.ensembl.org', | ||
| 41 | -user => 'anonymous' | ||
| 42 | ); | ||
| 43 | |||
| 44 | $analysis_adaptor = | ||
| 45 | Bio::EnsEMBL::Registry->get_adaptor( "human", "core", "analysis" ); | ||
| 46 | |||
| 47 | my $analysis = $analysis_adaptor->fetch_by_logic_name('genscan'); | ||
| 48 | |||
| 49 | =head1 DESCRIPTION | ||
| 50 | |||
| 51 | Module to encapsulate all db access for persistent class Analysis. | ||
| 52 | There should be just one per application and database connection. | ||
| 53 | |||
| 54 | =head1 METHODS | ||
| 55 | |||
| 56 | =cut | ||
| 57 | |||
| 58 | |||
| 59 | package Bio::EnsEMBL::DBSQL::AnalysisAdaptor; | ||
| 60 | $Bio::EnsEMBL::DBSQL::AnalysisAdaptor::VERSION = '114.0.0_50'; # TRIAL | ||
| 61 | $Bio::EnsEMBL::DBSQL::AnalysisAdaptor::VERSION = '114.0.050'; | ||
| 62 | use Bio::EnsEMBL::Analysis; | ||
| 63 | use Bio::EnsEMBL::DBSQL::BaseAdaptor; | ||
| 64 | use Bio::EnsEMBL::Utils::Exception; | ||
| 65 | |||
| 66 | use vars qw(@ISA); | ||
| 67 | use strict; | ||
| 68 | |||
| 69 | @ISA = qw( Bio::EnsEMBL::DBSQL::BaseAdaptor); | ||
| 70 | |||
| 71 | |||
| 72 | =head2 new | ||
| 73 | |||
| 74 | Args : Bio::EnsEMBL::DBSQL::DBAdaptor | ||
| 75 | Example : my $aa = new Bio::EnsEMBL::DBSQL::AnalysisAdaptor(); | ||
| 76 | Description: Creates a new Bio::EnsEMBL::DBSQL::AnalysisAdaptor object and | ||
| 77 | internally loads and caches all the Analysis objects from the | ||
| 78 | database. | ||
| 79 | Returntype : Bio::EnsEMBL::DBSQL::AnalysisAdaptor | ||
| 80 | Exceptions : none | ||
| 81 | Caller : Bio::EnsEMBL::DBSQL::DBAdaptor | ||
| 82 | Status : Stable | ||
| 83 | |||
| 84 | =cut | ||
| 85 | |||
| 86 | sub new { | ||
| 87 | my ($class, $db) = @_; | ||
| 88 | |||
| 89 | my $self = $class->SUPER::new($db); | ||
| 90 | |||
| 91 | #load and cache all of the Analysis objects | ||
| 92 | $self->fetch_all; | ||
| 93 | |||
| 94 | return $self; | ||
| 95 | } | ||
| 96 | |||
| 97 | |||
| 98 | =head2 fetch_all | ||
| 99 | |||
| 100 | Args : none | ||
| 101 | Example : my @analysis = @{$analysis_adaptor->fetch_all()}; | ||
| 102 | Description: fetches all of the Analysis objects from the database and caches | ||
| 103 | them internally. | ||
| 104 | Returntype : listref of Bio::EnsEMBL::Analysis retrieved from the database | ||
| 105 | Exceptions : none | ||
| 106 | Caller : AnalysisAdaptor::new | ||
| 107 | Status : Stable | ||
| 108 | |||
| 109 | =cut | ||
| 110 | |||
| 111 | sub fetch_all { | ||
| 112 | my $self = shift; | ||
| 113 | my ( $analysis, $dbID ); | ||
| 114 | my $rowHashRef; | ||
| 115 | |||
| 116 | $self->{_cache} = {}; | ||
| 117 | $self->{_logic_name_cache} = {}; | ||
| 118 | |||
| 119 | my $sth = $self->prepare( q { | ||
| 120 | SELECT analysis.analysis_id, logic_name, | ||
| 121 | program, program_version, program_file, | ||
| 122 | db, db_version, db_file, | ||
| 123 | module, module_version, | ||
| 124 | gff_source, gff_feature, | ||
| 125 | created, parameters, description, display_label, displayable, web_data | ||
| 126 | FROM analysis | ||
| 127 | LEFT JOIN analysis_description | ||
| 128 | ON analysis.analysis_id = analysis_description.analysis_id } ); | ||
| 129 | $sth->execute; | ||
| 130 | |||
| 131 | while( $rowHashRef = $sth->fetchrow_hashref ) { | ||
| 132 | my $analysis = $self->_objFromHashref( $rowHashRef ); | ||
| 133 | |||
| 134 | $self->{_cache}->{$analysis->dbID} = $analysis; | ||
| 135 | $self->{_logic_name_cache}->{lc($analysis->logic_name())} = $analysis; | ||
| 136 | } | ||
| 137 | |||
| 138 | my @ana = values %{$self->{_cache}}; | ||
| 139 | |||
| 140 | return \@ana; | ||
| 141 | } | ||
| 142 | |||
| 143 | |||
| 144 | =head2 fetch_all_by_feature_class | ||
| 145 | |||
| 146 | Arg [1] : string $feature_cless - The name of the feature class | ||
| 147 | Example : my @analyses = @{$analysis_adaptor->fetch_all_by_feature_class('Gene'); | ||
| 148 | Description: Returns all analyses that correspond to a given | ||
| 149 | feature class; see feature_classes method for a list. | ||
| 150 | Returntype : Listref of Bio::EnsEMBL::Analysis | ||
| 151 | Exceptions : none | ||
| 152 | Caller : general | ||
| 153 | Status : Stable | ||
| 154 | |||
| 155 | =cut | ||
| 156 | |||
| 157 | sub fetch_all_by_feature_class { | ||
| 158 | my $self = shift; | ||
| 159 | my $feat_class = shift || throw( "Need a feature type, e.g. SimpleFeature" ); | ||
| 160 | |||
| 161 | # Need a special case for density | ||
| 162 | # DensityFeature is the feature, but the analysis is linked to the DensityType | ||
| 163 | if ($feat_class =~ /Density/) { | ||
| 164 | $feat_class = 'DensityType'; | ||
| 165 | } | ||
| 166 | |||
| 167 | my $adaptor = $self->db->get_adaptor($feat_class); | ||
| 168 | if (!$adaptor) { | ||
| 169 | throw("$feat_class is not a know feature. No adaptor found"); | ||
| 170 | } | ||
| 171 | |||
| 172 | # Check that feature has an analysis | ||
| 173 | my $has_analysis = 0; | ||
| 174 | my @columns = $adaptor->_columns; | ||
| 175 | foreach my $column (@columns) { | ||
| 176 | if ($column =~ /analysis/) { | ||
| 177 | $has_analysis = 1; | ||
| 178 | last; | ||
| 179 | } | ||
| 180 | } | ||
| 181 | if ($has_analysis == 0) { | ||
| 182 | throw("$feat_class does not have an analysis column"); | ||
| 183 | } | ||
| 184 | |||
| 185 | # Retrieve the name of the table | ||
| 186 | my @tables = $adaptor->_tables(); | ||
| 187 | my $table = $tables[0]->[0]; | ||
| 188 | |||
| 189 | my $sql_t = qq| | ||
| 190 | SELECT DISTINCT analysis_id FROM %s |; | ||
| 191 | |||
| 192 | my $sql = sprintf( $sql_t, $table ); | ||
| 193 | my $sth = $self->prepare( $sql ); | ||
| 194 | my $rv = $sth->execute(); | ||
| 195 | my $res = $sth->fetchall_arrayref; | ||
| 196 | my @analyses; | ||
| 197 | foreach my $r( @{$res} ){ | ||
| 198 | my $analysis = $self->fetch_by_dbID($r->[0]) | ||
| 199 | || throw( "analysis_id $r->[0] from $table table " | ||
| 200 | . "is not in the analysis table!" ); | ||
| 201 | push @analyses, $analysis; | ||
| 202 | } | ||
| 203 | return [@analyses]; | ||
| 204 | } | ||
| 205 | |||
| 206 | =head2 fetch_by_dbID | ||
| 207 | |||
| 208 | Arg [1] : int $internal_analysis_id - the database id of the analysis | ||
| 209 | record to retrieve | ||
| 210 | Example : my $analysis = $analysis_adaptor->fetch_by_dbID(1); | ||
| 211 | Description: Retrieves an Analysis object from the database via its internal | ||
| 212 | id. | ||
| 213 | Returntype : Bio::EnsEMBL::Analysis | ||
| 214 | Exceptions : none | ||
| 215 | Caller : general | ||
| 216 | Status : Stable | ||
| 217 | |||
| 218 | =cut | ||
| 219 | |||
| 220 | sub fetch_by_dbID { | ||
| 221 | my $self = shift; | ||
| 222 | my $id = shift; | ||
| 223 | |||
| 224 | if( defined $self->{_cache}->{$id} ) { | ||
| 225 | return $self->{_cache}->{$id}; | ||
| 226 | } | ||
| 227 | |||
| 228 | my $query = q{ | ||
| 229 | SELECT analysis.analysis_id, logic_name, | ||
| 230 | program, program_version, program_file, | ||
| 231 | db, db_version, db_file, | ||
| 232 | module, module_version, | ||
| 233 | gff_source, gff_feature, | ||
| 234 | created, parameters, description, display_label, displayable, web_data | ||
| 235 | FROM analysis | ||
| 236 | LEFT JOIN analysis_description | ||
| 237 | ON analysis.analysis_id = analysis_description.analysis_id | ||
| 238 | WHERE analysis.analysis_id = ? }; | ||
| 239 | |||
| 240 | my $sth = $self->prepare($query); | ||
| 241 | $sth->bind_param(1,$id,SQL_INTEGER); | ||
| 242 | $sth->execute(); | ||
| 243 | my $rowHashRef = $sth->fetchrow_hashref; | ||
| 244 | if( ! defined $rowHashRef ) { | ||
| 245 | return; | ||
| 246 | } | ||
| 247 | |||
| 248 | my $anal = $self->_objFromHashref( $rowHashRef ); | ||
| 249 | $self->{_cache}->{$anal->dbID} = $anal; | ||
| 250 | $self->{_logic_name_cache}->{lc($anal->logic_name())} = $anal; | ||
| 251 | return $anal; | ||
| 252 | } | ||
| 253 | |||
| 254 | |||
| 255 | =head2 fetch_by_logic_name | ||
| 256 | |||
| 257 | Arg [1] : string $logic_name the logic name of the analysis to retrieve | ||
| 258 | Example : my $analysis = $a_adaptor->fetch_by_logic_name('Eponine'); | ||
| 259 | Description: Retrieves an analysis object from the database using its unique | ||
| 260 | logic name. | ||
| 261 | Returntype : Bio::EnsEMBL::Analysis | ||
| 262 | Exceptions : none | ||
| 263 | Caller : general | ||
| 264 | Status : Stable | ||
| 265 | |||
| 266 | =cut | ||
| 267 | |||
| 268 | sub fetch_by_logic_name { | ||
| 269 | my ( $self, $logic_name ) = @_; | ||
| 270 | |||
| 271 | my $analysis; | ||
| 272 | my $rowHash; | ||
| 273 | |||
| 274 | # Check the cache for the logic name | ||
| 275 | if ( defined( $self->{_logic_name_cache}{ lc($logic_name) } ) ) { | ||
| 276 | return $self->{_logic_name_cache}{ lc($logic_name) }; | ||
| 277 | } | ||
| 278 | |||
| 279 | my $sth = $self->prepare( | ||
| 280 | qq( | ||
| 281 | SELECT analysis.analysis_id, | ||
| 282 | logic_name, | ||
| 283 | program, | ||
| 284 | program_version, | ||
| 285 | program_file, | ||
| 286 | db, | ||
| 287 | db_version, | ||
| 288 | db_file, | ||
| 289 | module, | ||
| 290 | module_version, | ||
| 291 | gff_source, | ||
| 292 | gff_feature, | ||
| 293 | created, | ||
| 294 | parameters, | ||
| 295 | description, | ||
| 296 | display_label, | ||
| 297 | displayable, | ||
| 298 | web_data | ||
| 299 | FROM analysis | ||
| 300 | LEFT JOIN analysis_description | ||
| 301 | ON ( analysis.analysis_id = analysis_description.analysis_id ) | ||
| 302 | WHERE LOWER(logic_name) = ?) | ||
| 303 | ); | ||
| 304 | |||
| 305 | $sth->bind_param( 1, lc($logic_name), SQL_VARCHAR ); | ||
| 306 | $sth->execute(); | ||
| 307 | my $rowHashRef = $sth->fetchrow_hashref(); | ||
| 308 | |||
| 309 | if ( !defined($rowHashRef) ) { return } | ||
| 310 | |||
| 311 | $analysis = $self->_objFromHashref($rowHashRef); | ||
| 312 | |||
| 313 | # place the analysis in the caches, cross referenced by dbID and | ||
| 314 | # logic_name | ||
| 315 | $self->{_cache}->{ $analysis->dbID() } = $analysis; | ||
| 316 | $self->{_logic_name_cache}->{ lc($logic_name) } = $analysis; | ||
| 317 | |||
| 318 | return $analysis; | ||
| 319 | } ## end sub fetch_by_logic_name | ||
| 320 | |||
| 321 | |||
| 322 | =head2 store | ||
| 323 | |||
| 324 | Arg [1] : Bio:EnsEMBL::Analysis $analysis | ||
| 325 | Example : $analysis_adaptor->store($analysis); | ||
| 326 | Description: Stores $analysis in db. If the analysis is already stored in | ||
| 327 | the database its dbID and adaptor are updated, but the analysis | ||
| 328 | is not stored a second time. | ||
| 329 | Sets created date if not already set. Sets dbID and adaptor | ||
| 330 | inside $analysis. Returns dbID. | ||
| 331 | Returntype : int - dbID of stored analysis | ||
| 332 | Exceptions : throw on incorrect argument | ||
| 333 | throw if analysis argument does not have a logic name | ||
| 334 | Caller : general | ||
| 335 | Status : Stable | ||
| 336 | |||
| 337 | =cut | ||
| 338 | |||
| 339 | sub store { | ||
| 340 | my $self = shift; | ||
| 341 | my $analysis = shift; | ||
| 342 | |||
| 343 | if(!ref($analysis) || !$analysis->isa('Bio::EnsEMBL::Analysis')) { | ||
| 344 | throw("Bio::EnsEMBL::Analysis argument expected."); | ||
| 345 | } | ||
| 346 | |||
| 347 | if($analysis->is_stored($self->db())) { | ||
| 348 | return $analysis->dbID(); | ||
| 349 | } | ||
| 350 | |||
| 351 | if(!$analysis->logic_name()) { | ||
| 352 | throw("Analysis cannot be stored without a valid logic_name"); | ||
| 353 | } | ||
| 354 | |||
| 355 | my $insert_ignore = $self->insert_ignore_clause(); | ||
| 356 | |||
| 357 | my $rows_inserted = 0; | ||
| 358 | my $sth; | ||
| 359 | |||
| 360 | if ( $analysis->created() ) { | ||
| 361 | |||
| 362 | # We use insert IGNORE so that this method can be used in a | ||
| 363 | # multi-process environment. If another process has already written | ||
| 364 | # this record then there will not be a problem. | ||
| 365 | |||
| 366 | $sth = $self->prepare( | ||
| 367 | qq{ | ||
| 368 | ${insert_ignore} INTO analysis | ||
| 369 | (created, logic_name, db, db_version, db_file, program, program_version, program_file, parameters, module, module_version, gff_source, gff_feature) | ||
| 370 | VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) | ||
| 371 | } | ||
| 372 | ); | ||
| 373 | $sth->bind_param( 1, $analysis->created(), SQL_DATETIME ); | ||
| 374 | $sth->bind_param( 2, lc( $analysis->logic_name() ), SQL_VARCHAR ); | ||
| 375 | $sth->bind_param( 3, $analysis->db(), SQL_VARCHAR ); | ||
| 376 | $sth->bind_param( 4, $analysis->db_version(), SQL_VARCHAR ); | ||
| 377 | $sth->bind_param( 5, $analysis->db_file(), SQL_VARCHAR ); | ||
| 378 | $sth->bind_param( 6, $analysis->program(), SQL_VARCHAR ); | ||
| 379 | $sth->bind_param( 7, $analysis->program_version(), SQL_VARCHAR ); | ||
| 380 | $sth->bind_param( 8, $analysis->program_file(), SQL_VARCHAR ); | ||
| 381 | $sth->bind_param( 9, $analysis->parameters(), SQL_VARCHAR ); | ||
| 382 | $sth->bind_param( 10, $analysis->module(), SQL_VARCHAR ); | ||
| 383 | $sth->bind_param( 11, $analysis->module_version(), SQL_VARCHAR ); | ||
| 384 | $sth->bind_param( 12, $analysis->gff_source(), SQL_VARCHAR ); | ||
| 385 | $sth->bind_param( 13, $analysis->gff_feature(), SQL_VARCHAR ); | ||
| 386 | |||
| 387 | $rows_inserted = $sth->execute(); | ||
| 388 | |||
| 389 | } else { | ||
| 390 | $sth = $self->prepare( | ||
| 391 | qq{ | ||
| 392 | ${insert_ignore} INTO analysis | ||
| 393 | (created, logic_name, db, db_version, db_file, program, program_version, program_file, parameters, module, module_version, gff_source, gff_feature) | ||
| 394 | VALUES (CURRENT_TIMESTAMP, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) | ||
| 395 | } | ||
| 396 | ); | ||
| 397 | |||
| 398 | $sth->bind_param( 1, $analysis->logic_name, SQL_VARCHAR ); | ||
| 399 | $sth->bind_param( 2, $analysis->db, SQL_VARCHAR ); | ||
| 400 | $sth->bind_param( 3, $analysis->db_version, SQL_VARCHAR ); | ||
| 401 | $sth->bind_param( 4, $analysis->db_file, SQL_VARCHAR ); | ||
| 402 | $sth->bind_param( 5, $analysis->program, SQL_VARCHAR ); | ||
| 403 | $sth->bind_param( 6, $analysis->program_version, SQL_VARCHAR ); | ||
| 404 | $sth->bind_param( 7, $analysis->program_file, SQL_VARCHAR ); | ||
| 405 | $sth->bind_param( 8, $analysis->parameters, SQL_VARCHAR ); | ||
| 406 | $sth->bind_param( 9, $analysis->module, SQL_VARCHAR ); | ||
| 407 | $sth->bind_param( 10, $analysis->module_version, SQL_VARCHAR ); | ||
| 408 | $sth->bind_param( 11, $analysis->gff_source, SQL_VARCHAR ); | ||
| 409 | $sth->bind_param( 12, $analysis->gff_feature, SQL_VARCHAR ); | ||
| 410 | |||
| 411 | $rows_inserted = $sth->execute(); | ||
| 412 | |||
| 413 | } ## end else [ if ( $analysis->created...)] | ||
| 414 | |||
| 415 | my $dbID; | ||
| 416 | # If we need to fetch the timestamp, or the insert failed due to | ||
| 417 | # existance of an existing entry, we need to retrieve the entry from | ||
| 418 | # the database. Note: $sth->execute() may return 0E0 on error which | ||
| 419 | # is zero, but true which is why the $rows_inserted clause was added. | ||
| 420 | if ( !$analysis->created() || !$rows_inserted || $rows_inserted == 0 ) | ||
| 421 | { | ||
| 422 | my $new_analysis = | ||
| 423 | $self->fetch_by_logic_name( $analysis->logic_name ); | ||
| 424 | |||
| 425 | if ( !$new_analysis ) { | ||
| 426 | throw("Could not retrieve just stored analysis from database.\n" | ||
| 427 | . "Possibly incorrect db permissions or missing analysis table\n" | ||
| 428 | ); | ||
| 429 | } | ||
| 430 | |||
| 431 | $dbID = $new_analysis->dbID(); | ||
| 432 | $analysis->created( $new_analysis->created() ); | ||
| 433 | } | ||
| 434 | |||
| 435 | $dbID ||= $self->last_insert_id('analysis_id', undef, 'analysis'); | ||
| 436 | $sth->finish(); | ||
| 437 | |||
| 438 | # store description and display_label | ||
| 439 | if( defined( $analysis->description() ) || defined( $analysis->display_label() )|| defined( $analysis->web_data() )) { | ||
| 440 | $self->_store_description($analysis, $dbID); | ||
| 441 | } | ||
| 442 | |||
| 443 | |||
| 444 | $self->{_cache}->{$dbID} = $analysis; | ||
| 445 | $self->{_logic_name_cache}{lc($analysis->logic_name)} = $analysis; | ||
| 446 | |||
| 447 | $analysis->adaptor( $self ); | ||
| 448 | $analysis->dbID( $dbID ); | ||
| 449 | |||
| 450 | return $dbID; | ||
| 451 | } | ||
| 452 | |||
| 453 | |||
| 454 | sub _store_description { | ||
| 455 | my ($self, $analysis, $dbID) = @_; | ||
| 456 | |||
| 457 | my $insert_ignore = $self->insert_ignore_clause(); | ||
| 458 | my $sth = $self->prepare( | ||
| 459 | "${insert_ignore} INTO analysis_description (analysis_id, display_label, description, displayable, web_data) " . | ||
| 460 | "VALUES (?,?,?,?,?)" | ||
| 461 | ); | ||
| 462 | |||
| 463 | my $display_label = $analysis->display_label(); | ||
| 464 | $display_label = '' unless defined $display_label; # SQLite doesn't ignore NOT NULL errors | ||
| 465 | |||
| 466 | $sth->bind_param(1,$dbID,SQL_INTEGER); | ||
| 467 | $sth->bind_param(2,$display_label,SQL_VARCHAR); | ||
| 468 | $sth->bind_param(3,$analysis->description,SQL_LONGVARCHAR); | ||
| 469 | $sth->bind_param(4,$analysis->displayable,SQL_TINYINT); | ||
| 470 | $sth->bind_param(5,$analysis->web_data,SQL_LONGVARCHAR); | ||
| 471 | $sth->execute(); | ||
| 472 | |||
| 473 | $sth->finish(); | ||
| 474 | return; | ||
| 475 | } | ||
| 476 | |||
| 477 | |||
| 478 | =head2 update | ||
| 479 | |||
| 480 | Arg [1] : Bio::EnsEMBL::Analysis $anal | ||
| 481 | Example : $adaptor->update($anal) | ||
| 482 | Description: Updates this analysis in the database | ||
| 483 | Returntype : int 1 if update is performed, undef if it is not | ||
| 484 | Exceptions : throw if arg is not an analysis object | ||
| 485 | Caller : ? | ||
| 486 | Status : Stable | ||
| 487 | |||
| 488 | =cut | ||
| 489 | |||
| 490 | sub update { | ||
| 491 | my $self = shift; | ||
| 492 | my $a = shift; | ||
| 493 | |||
| 494 | if (!ref($a) || !$a->isa('Bio::EnsEMBL::Analysis')) { | ||
| 495 | throw("Expected Bio::EnsEMBL::Analysis argument."); | ||
| 496 | } | ||
| 497 | |||
| 498 | if(!$a->is_stored($self->db())) { | ||
| 499 | return; | ||
| 500 | } | ||
| 501 | |||
| 502 | my $sth = $self->prepare | ||
| 503 | ("UPDATE analysis " . | ||
| 504 | "SET created = ?, logic_name = ?, db = ?, db_version = ?, db_file = ?, ". | ||
| 505 | " program = ?, program_version = ?, program_file = ?, ". | ||
| 506 | " parameters = ?, module = ?, module_version = ?, ". | ||
| 507 | " gff_source = ?, gff_feature = ? " . | ||
| 508 | "WHERE analysis_id = ?"); | ||
| 509 | |||
| 510 | |||
| 511 | |||
| 512 | $sth->bind_param(1,$a->created,SQL_DATETIME); | ||
| 513 | $sth->bind_param(2,$a->logic_name,SQL_VARCHAR); | ||
| 514 | $sth->bind_param(3,$a->db,SQL_VARCHAR); | ||
| 515 | $sth->bind_param(4,$a->db_version,SQL_VARCHAR); | ||
| 516 | $sth->bind_param(5,$a->db_file,SQL_VARCHAR); | ||
| 517 | $sth->bind_param(6,$a->program,SQL_VARCHAR); | ||
| 518 | $sth->bind_param(7,$a->program_version,SQL_VARCHAR); | ||
| 519 | $sth->bind_param(8,$a->program_file,SQL_VARCHAR); | ||
| 520 | $sth->bind_param(9,$a->parameters,SQL_VARCHAR); | ||
| 521 | $sth->bind_param(10,$a->module,SQL_VARCHAR); | ||
| 522 | $sth->bind_param(11,$a->module_version,SQL_VARCHAR); | ||
| 523 | $sth->bind_param(12,$a->gff_source,SQL_VARCHAR); | ||
| 524 | $sth->bind_param(13,$a->gff_feature,SQL_VARCHAR); | ||
| 525 | $sth->bind_param(14,$a->dbID,SQL_INTEGER); | ||
| 526 | |||
| 527 | $sth->execute(); | ||
| 528 | |||
| 529 | $sth->finish(); | ||
| 530 | |||
| 531 | # also update description & display label - may need to create these if | ||
| 532 | # not already there | ||
| 533 | $sth = $self->prepare("SELECT description FROM analysis_description WHERE analysis_id= ?"); | ||
| 534 | $sth->execute($a->dbID); | ||
| 535 | if ($sth->fetchrow_hashref) { # update if exists | ||
| 536 | $sth = $self->prepare | ||
| 537 | ("UPDATE analysis_description SET description = ?, display_label = ?, displayable = ?, web_data = ? WHERE analysis_id = ?"); | ||
| 538 | $sth->bind_param(1,$a->description,SQL_LONGVARCHAR); | ||
| 539 | $sth->bind_param(2,$a->display_label(),SQL_VARCHAR); | ||
| 540 | $sth->bind_param(3,$a->displayable,SQL_TINYINT); | ||
| 541 | $sth->bind_param(4,$a->web_data,SQL_LONGVARCHAR); | ||
| 542 | $sth->bind_param(5,$a->dbID,SQL_INTEGER); | ||
| 543 | $sth->execute(); | ||
| 544 | |||
| 545 | } else { # create new entry | ||
| 546 | |||
| 547 | if( $a->description() || $a->display_label() || $a->web_data) { | ||
| 548 | $self->_store_description($a, $a->dbID); | ||
| 549 | } | ||
| 550 | |||
| 551 | } | ||
| 552 | |||
| 553 | |||
| 554 | $sth->finish(); | ||
| 555 | |||
| 556 | # the logic_name cache needs to be re-updated now, since we may have just | ||
| 557 | # changed the logic_name | ||
| 558 | $self->fetch_all(); | ||
| 559 | |||
| 560 | return 1; | ||
| 561 | } | ||
| 562 | |||
| 563 | |||
| 564 | |||
| 565 | =head2 remove | ||
| 566 | |||
| 567 | Arg [1] : Bio::EnsEMBL::Analysis $anal | ||
| 568 | Example : $adaptor->remove($anal) | ||
| 569 | Description: Removes this analysis from the database. This is not really | ||
| 570 | safe to execute in a multi process environment, so programs | ||
| 571 | should not remove analysis while out on the farm. | ||
| 572 | Returntype : none | ||
| 573 | Exceptions : thrown if $anal arg is not an analysis object | ||
| 574 | Caller : ? | ||
| 575 | Status : Stable | ||
| 576 | |||
| 577 | =cut | ||
| 578 | |||
| 579 | sub remove { | ||
| 580 | my ($self, $analysis) = @_; | ||
| 581 | |||
| 582 | if (!defined $analysis || !ref $analysis) { | ||
| 583 | throw("called remove on AnalysisAdaptor with a [$analysis]"); | ||
| 584 | } | ||
| 585 | |||
| 586 | if(!$analysis->is_stored($self->db())) { | ||
| 587 | return; | ||
| 588 | } | ||
| 589 | |||
| 590 | my $sth = $self->prepare("DELETE FROM analysis WHERE analysis_id = ?"); | ||
| 591 | $sth->bind_param(1,$analysis->dbID,SQL_INTEGER); | ||
| 592 | $sth->execute(); | ||
| 593 | |||
| 594 | $sth = $self->prepare("DELETE FROM analysis_description WHERE analysis_id = ?"); | ||
| 595 | $sth->execute($analysis->dbID()); | ||
| 596 | |||
| 597 | # remove this analysis from the cache | ||
| 598 | delete $self->{'_cache'}->{$analysis->dbID()}; | ||
| 599 | delete $self->{'_logic_name_cache'}->{lc($analysis->logic_name)}; | ||
| 600 | |||
| 601 | |||
| 602 | # unset the adaptor and dbID | ||
| 603 | $analysis->dbID(undef); | ||
| 604 | $analysis->adaptor(undef); | ||
| 605 | |||
| 606 | return; | ||
| 607 | } | ||
| 608 | |||
| 609 | |||
| 610 | |||
| 611 | =head2 exists | ||
| 612 | |||
| 613 | Arg [1] : Bio::EnsEMBL::Analysis $anal | ||
| 614 | Example : if($adaptor->exists($anal)) #do something | ||
| 615 | Description: Tests whether this Analysis already exists in the database | ||
| 616 | by checking first if the adaptor and dbID are set and | ||
| 617 | secondly by whether it is in this adaptors internal cache. | ||
| 618 | Note that this will not actually check the database and will | ||
| 619 | not find and analysis which were recently added by other | ||
| 620 | processes. You are better off simply trying to store an | ||
| 621 | analysis which will reliably ensure that it is not stored twice | ||
| 622 | in the database. | ||
| 623 | Returntype : int dbID if analysis is found, otherwise returns undef | ||
| 624 | Exceptions : thrown if $anal arg is not an analysis object | ||
| 625 | Caller : store | ||
| 626 | Status : Stable | ||
| 627 | |||
| 628 | =cut | ||
| 629 | |||
| 630 | sub exists { | ||
| 631 | my ($self,$anal) = @_; | ||
| 632 | |||
| 633 | if(!ref($anal) || !$anal->isa("Bio::EnsEMBL::Analysis")) { | ||
| 634 | throw("Object is not a Bio::EnsEMBL::Analysis"); | ||
| 635 | } | ||
| 636 | |||
| 637 | #if this analysis is stored in this db already return its dbID | ||
| 638 | if($anal->is_stored($self->db())) { | ||
| 639 | return $anal->dbID(); | ||
| 640 | } | ||
| 641 | |||
| 642 | #this analysis object is not stored but one exactly like it may have been | ||
| 643 | foreach my $cacheId (keys %{$self->{_cache}}) { | ||
| 644 | if ($self->{_cache}->{$cacheId}->compare($anal) >= 0) { | ||
| 645 | # $anal->dbID( $cacheId ); | ||
| 646 | # $anal->adaptor( $self ); | ||
| 647 | return $cacheId; | ||
| 648 | } | ||
| 649 | } | ||
| 650 | |||
| 651 | #no analysis like this one exists in the database | ||
| 652 | return; | ||
| 653 | } | ||
| 654 | |||
| 655 | |||
| 656 | =head2 _objFromHashref | ||
| 657 | |||
| 658 | Arg [1] : hashref $rowHash | ||
| 659 | Description: Private helper function generates an Analysis object from a | ||
| 660 | mysql row hash reference. | ||
| 661 | Returntype : Bio::EnsEMBL::Analysis | ||
| 662 | Exceptions : none | ||
| 663 | Caller : Bio::EnsEMBL::DBSQL::AnalsisAdaptor::fetch_* methods | ||
| 664 | Status : Stable | ||
| 665 | |||
| 666 | =cut | ||
| 667 | |||
| 668 | sub _objFromHashref { | ||
| 669 | my $self = shift; | ||
| 670 | my $h = shift; | ||
| 671 | |||
| 672 | return Bio::EnsEMBL::Analysis->new_fast({ | ||
| 673 | dbID => $h->{analysis_id}, | ||
| 674 | adaptor => $self, | ||
| 675 | _db => $h->{db}, | ||
| 676 | _db_file => $h->{db_file}, | ||
| 677 | _db_version => $h->{db_version}, | ||
| 678 | _program => $h->{program}, | ||
| 679 | _program_version => $h->{program_version}, | ||
| 680 | _program_file => $h->{program_file}, | ||
| 681 | _gff_source => $h->{gff_source}, | ||
| 682 | _gff_feature => $h->{gff_feature}, | ||
| 683 | _module => $h->{module}, | ||
| 684 | _module_version => $h->{module_version}, | ||
| 685 | _parameters => $h->{parameters}, | ||
| 686 | _created => $h->{created}, | ||
| 687 | _logic_name => $h->{logic_name}, | ||
| 688 | _description => $h->{description}, | ||
| 689 | _display_label => $h->{display_label}, | ||
| 690 | _displayable => $h->{displayable}, | ||
| 691 | _web_data => $h->{web_data}, | ||
| 692 | }); | ||
| 693 | } | ||
| 694 | |||
| 695 | |||
| 696 | |||
| 697 | 1; |