| 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::UnmappedObjectAdaptor | ||
| 34 | |||
| 35 | =head1 SYNOPSIS | ||
| 36 | |||
| 37 | my $uoa = $database_adaptor->get_UnmappedObjectAdaptor(); | ||
| 38 | |||
| 39 | my $missed = @{ $uoa->fetch_all_by_type('xref') }; | ||
| 40 | |||
| 41 | =head1 DESCRIPTION | ||
| 42 | |||
| 43 | Unmapped ObjectAdaptor - An adaptor responsible for the creation, | ||
| 44 | editing, retrieval of Unmapped Objects. These being the Objects that | ||
| 45 | where not mapped in a specific process i.e. xref, cDNA, Markers. | ||
| 46 | |||
| 47 | =head1 METHODS | ||
| 48 | |||
| 49 | =cut | ||
| 50 | |||
| 51 | package Bio::EnsEMBL::DBSQL::UnmappedObjectAdaptor; | ||
| 52 | $Bio::EnsEMBL::DBSQL::UnmappedObjectAdaptor::VERSION = '114.0.0'; | ||
| 53 | use vars qw(@ISA); | ||
| 54 | use strict; | ||
| 55 | |||
| 56 | |||
| 57 | use POSIX; | ||
| 58 | use Bio::EnsEMBL::Utils::Cache; | ||
| 59 | use Bio::EnsEMBL::Utils::Exception qw(throw warning); | ||
| 60 | use Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor; | ||
| 61 | use Bio::EnsEMBL::UnmappedObject; | ||
| 62 | use Bio::EnsEMBL::Analysis; | ||
| 63 | @ISA = qw(Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor); | ||
| 64 | |||
| 65 | |||
| 66 | =head2 new | ||
| 67 | |||
| 68 | Arg [1] : list of args @args | ||
| 69 | Superclass constructor arguments | ||
| 70 | Example : none | ||
| 71 | Description: Constructor which just initializes internal cache structures | ||
| 72 | Returntype : Bio::EnsEMBL::DBSQL::UnmappedObjectAdaptor | ||
| 73 | Exceptions : none | ||
| 74 | Caller : implementing subclass constructors | ||
| 75 | Status : At Risk | ||
| 76 | |||
| 77 | =cut | ||
| 78 | |||
| 79 | our %desc_to_id; | ||
| 80 | sub new { | ||
| 81 | my $proto = shift; | ||
| 82 | |||
| 83 | my $class = ref($proto) || $proto; | ||
| 84 | my $self = $class->SUPER::new(@_); | ||
| 85 | |||
| 86 | my $sth = | ||
| 87 | $self->prepare(" | ||
| 88 | SELECT | ||
| 89 | unmapped_reason_id, | ||
| 90 | full_description | ||
| 91 | FROM | ||
| 92 | unmapped_reason | ||
| 93 | "); | ||
| 94 | |||
| 95 | $sth->execute(); | ||
| 96 | |||
| 97 | my ( $id, $desc ); | ||
| 98 | $sth->bind_columns( \( $id, $desc ) ); | ||
| 99 | |||
| 100 | while ( $sth->fetch() ) { | ||
| 101 | $desc_to_id{$desc} = $id; | ||
| 102 | } | ||
| 103 | |||
| 104 | $sth->finish(); | ||
| 105 | |||
| 106 | |||
| 107 | return $self; | ||
| 108 | } | ||
| 109 | |||
| 110 | |||
| 111 | # _tables | ||
| 112 | # Arg [1] : none | ||
| 113 | # Description: PROTECTED implementation of superclass abstract method | ||
| 114 | # returns the names, aliases of the tables to use for queries | ||
| 115 | # Returntype : list of listrefs of strings | ||
| 116 | # Exceptions : none | ||
| 117 | # Caller : internal | ||
| 118 | # Status : At Risk | ||
| 119 | sub _tables { | ||
| 120 | my $self = shift; | ||
| 121 | |||
| 122 | return (['unmapped_object', 'uo'], | ||
| 123 | ['unmapped_reason', 'ur']); | ||
| 124 | } | ||
| 125 | |||
| 126 | |||
| 127 | # _columns | ||
| 128 | # Arg [1] : none | ||
| 129 | # Example : none | ||
| 130 | # Description: PROTECTED implementation of superclass abstract method | ||
| 131 | # returns a list of columns to use for queries | ||
| 132 | # Returntype : list of strings | ||
| 133 | # Exceptions : none | ||
| 134 | # Caller : internal | ||
| 135 | # Status : At Risk | ||
| 136 | |||
| 137 | sub _columns { | ||
| 138 | my $self = shift; | ||
| 139 | |||
| 140 | return qw(uo.unmapped_object_id uo.type uo.analysis_id uo.external_db_id | ||
| 141 | uo.identifier uo.unmapped_reason_id uo.query_score uo.target_score | ||
| 142 | uo.ensembl_id uo.ensembl_object_type | ||
| 143 | ur.summary_description ur.full_description); | ||
| 144 | } | ||
| 145 | |||
| 146 | sub _left_join { | ||
| 147 | return ( [ | ||
| 148 | 'unmapped_object', "uo.unmapped_reason_id = ur.unmapped_reason_id" | ||
| 149 | ] ); | ||
| 150 | } | ||
| 151 | |||
| 152 | =head2 list_dbIDs | ||
| 153 | |||
| 154 | Arg [1] : none | ||
| 155 | Example : @unmapped_object_ids = @{$unmapped_object_adaptor->list_dbIDs()}; | ||
| 156 | Description: Gets an array of internal ids for all unmapped_objects in the current db | ||
| 157 | Returntype : list of ints | ||
| 158 | Exceptions : none | ||
| 159 | Caller : ? | ||
| 160 | Status : Stable | ||
| 161 | |||
| 162 | =cut | ||
| 163 | |||
| 164 | sub list_dbIDs { | ||
| 165 | my ($self) = @_; | ||
| 166 | |||
| 167 | return $self->_list_dbIDs("unmapped_object"); | ||
| 168 | } | ||
| 169 | |||
| 170 | =head2 list_unmapped_reasons | ||
| 171 | |||
| 172 | Arg [1] : none | ||
| 173 | Example : @unmapped_object_reason+ids = | ||
| 174 | @{$unmapped_object_adaptor->list_unmapped_reasons()}; | ||
| 175 | Description: Gets an array of internal ids for all unmapped_objects in the current db | ||
| 176 | Returntype : list of ints | ||
| 177 | Exceptions : none | ||
| 178 | Caller : ? | ||
| 179 | Status : Stable | ||
| 180 | |||
| 181 | =cut | ||
| 182 | |||
| 183 | sub list_unmapped_reasons { | ||
| 184 | my ($self) = @_; | ||
| 185 | |||
| 186 | return $self->_list_dbIDs("unmapped_reason"); | ||
| 187 | } | ||
| 188 | |||
| 189 | |||
| 190 | # _objs_from_sth | ||
| 191 | |||
| 192 | # Arg [1] : StatementHandle $sth | ||
| 193 | # Example : none | ||
| 194 | # Description: PROTECTED implementation of abstract superclass method. | ||
| 195 | # responsible for the creation of UnmappedObjects | ||
| 196 | # Returntype : listref of Bio::EnsEMBL::UnmappedObjects | ||
| 197 | # Exceptions : none | ||
| 198 | # Caller : internal | ||
| 199 | # Status : At Risk | ||
| 200 | |||
| 201 | sub _objs_from_sth { | ||
| 202 | my ($self, $sth) = @_; | ||
| 203 | |||
| 204 | my($unmapped_object_id, $type, $analysis_id, $external_db_id, $identifier, | ||
| 205 | $unmapped_reason_id, $query_score, $target_score, $ensembl_id, | ||
| 206 | $ensembl_object_type, $summary, $full_desc); | ||
| 207 | |||
| 208 | $sth->bind_columns(\$unmapped_object_id,\$type, \$analysis_id, | ||
| 209 | \$external_db_id, \$identifier, \$unmapped_reason_id, | ||
| 210 | \$query_score, \$target_score, \$ensembl_id, | ||
| 211 | \$ensembl_object_type, \$summary, \$full_desc); | ||
| 212 | |||
| 213 | my $analysis_adaptor = $self->db->get_AnalysisAdaptor(); | ||
| 214 | |||
| 215 | my @features; | ||
| 216 | while($sth->fetch()) { | ||
| 217 | my $analysis = $analysis_adaptor->fetch_by_dbID($analysis_id); | ||
| 218 | |||
| 219 | #print "$identifier\n"; | ||
| 220 | |||
| 221 | push( @features, | ||
| 222 | $self->_create_feature( | ||
| 223 | 'Bio::EnsEMBL::UnmappedObject', { | ||
| 224 | -unmapped_object_id => $unmapped_object_id, | ||
| 225 | -unmapped_reason_id => $unmapped_reason_id, | ||
| 226 | -type => $type, | ||
| 227 | -analysis => $analysis, | ||
| 228 | -external_db_id => $external_db_id, | ||
| 229 | -identifier => $identifier, | ||
| 230 | -query_score => $query_score, | ||
| 231 | -target_score => $target_score, | ||
| 232 | -ensembl_id => $ensembl_id, | ||
| 233 | -ensembl_object_type => $ensembl_object_type, | ||
| 234 | -summary => $summary, | ||
| 235 | -full_desc => $full_desc, | ||
| 236 | -adaptor => $self | ||
| 237 | } ) ); | ||
| 238 | |||
| 239 | } | ||
| 240 | return \@features; | ||
| 241 | } | ||
| 242 | |||
| 243 | |||
| 244 | |||
| 245 | =head2 store | ||
| 246 | |||
| 247 | Arg [1] : list of Bio::EnsEMBL::UnmappedObjects @uo | ||
| 248 | the unmapped objects to store in the database | ||
| 249 | Example : $ou_adaptor->store(@uo); | ||
| 250 | Description: Stores a list of unmapped objects in the database | ||
| 251 | Returntype : none | ||
| 252 | Exceptions : thrown if no Analysis, or no list of objects to store. | ||
| 253 | Caller : general | ||
| 254 | Status : Stable | ||
| 255 | |||
| 256 | =cut | ||
| 257 | |||
| 258 | sub store{ | ||
| 259 | my ($self,@uos) = @_; | ||
| 260 | |||
| 261 | if( scalar(@uos) == 0 ) { | ||
| 262 | throw("Must call store with list of UnmappedObjects"); | ||
| 263 | } | ||
| 264 | |||
| 265 | |||
| 266 | my $db = $self->db(); | ||
| 267 | my $analysis_adaptor = $db->get_AnalysisAdaptor(); | ||
| 268 | |||
| 269 | my $sth_reason = $self->prepare | ||
| 270 | ("INSERT INTO unmapped_reason (summary_description, full_description)". | ||
| 271 | " VALUES (?,?)"); | ||
| 272 | |||
| 273 | my $sth_unmapped_object = $self->prepare | ||
| 274 | ("INSERT INTO unmapped_object (type, analysis_id, external_db_id, | ||
| 275 | identifier, unmapped_reason_id, query_score, target_score, | ||
| 276 | ensembl_id, ensembl_object_type)". | ||
| 277 | " VALUES (?,?,?,?,?,?,?,?,?)"); | ||
| 278 | |||
| 279 | my $sth_fetch_reason = | ||
| 280 | $self->prepare( | ||
| 281 | "SELECT | ||
| 282 | unmapped_reason_id | ||
| 283 | FROM | ||
| 284 | unmapped_reason | ||
| 285 | WHERE | ||
| 286 | full_description = ? | ||
| 287 | " ); | ||
| 288 | |||
| 289 | FEATURE: foreach my $uo ( @uos ) { | ||
| 290 | |||
| 291 | if( !ref $uo || !$uo->isa("Bio::EnsEMBL::UnmappedObject") ) { | ||
| 292 | throw("UnmappedObject must be an Ensembl UnmappedObject, " . | ||
| 293 | "not a [".ref($uo)."]"); | ||
| 294 | } | ||
| 295 | if($uo->is_stored($db)){ | ||
| 296 | next; | ||
| 297 | } | ||
| 298 | |||
| 299 | my $analysis = $uo->analysis(); | ||
| 300 | throw("UnmappedObject must have an analysis object.".$uo->analysis."\n") if(!defined($analysis)); | ||
| 301 | |||
| 302 | my $analysis_id; | ||
| 303 | if($analysis->is_stored($db)) { | ||
| 304 | $analysis_id = $analysis->dbID(); | ||
| 305 | } | ||
| 306 | else { | ||
| 307 | $analysis_id = $db->get_AnalysisAdaptor->store($analysis); | ||
| 308 | } | ||
| 309 | |||
| 310 | |||
| 311 | |||
| 312 | # Check if unmapped reason is cached | ||
| 313 | # Check if it has been added since the cache was created | ||
| 314 | # Try to store it | ||
| 315 | |||
| 316 | if(!defined($desc_to_id{$uo->{'description'}})){ | ||
| 317 | $sth_reason->bind_param(1,$uo->{'summary'},SQL_VARCHAR); | ||
| 318 | $sth_reason->bind_param(2,$uo->{'description'},SQL_VARCHAR); | ||
| 319 | |||
| 320 | if(! eval{ $sth_reason->execute(); 1 }){ | ||
| 321 | # DBI Trace possible here? | ||
| 322 | warning($@); # | ||
| 323 | my $msg; | ||
| 324 | $msg .= "INSERT INTO unmapped_reason (summary_description, full_description) VALUES ("; | ||
| 325 | $msg .= $uo->{'summary'} .','. $uo->{'description'}. ')'; | ||
| 326 | # Temporary fix for naughty cross-dependency regulation code. | ||
| 327 | use Data::Dumper; | ||
| 328 | warning("Query: \n$msg"); | ||
| 329 | print STDERR "UnmappedObject: \n"; | ||
| 330 | print STDERR Dumper $uo; | ||
| 331 | |||
| 332 | $sth_fetch_reason->execute($uo->{'description'}); | ||
| 333 | |||
| 334 | my $unmapped_reasons = $sth_fetch_reason->fetchrow_arrayref(); | ||
| 335 | if(! defined($unmapped_reasons)){ | ||
| 336 | my $msg = $uo->{'description'}. " unable to store. Check MySQL schema, maybe PK not big enough?"; | ||
| 337 | throw($msg); | ||
| 338 | } | ||
| 339 | if(scalar @$unmapped_reasons != 1){ | ||
| 340 | throw("Multiple results for this description"); | ||
| 341 | } | ||
| 342 | $uo->{'unmapped_reason_id'} = $unmapped_reasons->[0]; | ||
| 343 | |||
| 344 | } | ||
| 345 | else{ | ||
| 346 | # print 'Last mohican: '.$self->last_insert_id ."\n"; | ||
| 347 | $uo->{'unmapped_reason_id'} = $self->last_insert_id; | ||
| 348 | } | ||
| 349 | } | ||
| 350 | else{ | ||
| 351 | $uo->{'unmapped_reason_id'} = $desc_to_id{$uo->{'description'}}; | ||
| 352 | } | ||
| 353 | $sth_unmapped_object->bind_param(1,$uo->{'type'},SQL_VARCHAR); | ||
| 354 | $sth_unmapped_object->bind_param(2,$uo->analysis->dbID,SQL_INTEGER); | ||
| 355 | $sth_unmapped_object->bind_param(3,$uo->{'external_db_id'},SQL_INTEGER); | ||
| 356 | $sth_unmapped_object->bind_param(4,$uo->{'identifier'},SQL_VARCHAR); | ||
| 357 | $sth_unmapped_object->bind_param(5,$uo->{'unmapped_reason_id'},SQL_VARCHAR); | ||
| 358 | $sth_unmapped_object->bind_param(6,$uo->{'query_score'},SQL_DOUBLE); | ||
| 359 | $sth_unmapped_object->bind_param(7,$uo->{'target_score'},SQL_DOUBLE); | ||
| 360 | $sth_unmapped_object->bind_param(8,$uo->{'ensembl_id'},SQL_INTEGER); | ||
| 361 | $sth_unmapped_object->bind_param(9,$uo->{'ensembl_object_type'},SQL_VARCHAR); | ||
| 362 | $sth_unmapped_object->execute(); | ||
| 363 | $uo->dbID($self->last_insert_id('unmapped_object_id', undef, 'unmapped_object')); | ||
| 364 | } | ||
| 365 | $sth_reason->finish(); | ||
| 366 | return; | ||
| 367 | } | ||
| 368 | |||
| 369 | |||
| 370 | =head2 fetch_all_by_type | ||
| 371 | |||
| 372 | Arg [1] : string type. The type of unmapped objects | ||
| 373 | Example : @unmapped_object = @{$uoa->fetch_all_by_type('xref')}; | ||
| 374 | Description : Retrieves all the unmapped object for a particular | ||
| 375 | type. e.g. 'xref','cDNA', 'marker' | ||
| 376 | Returntype : Array ref of Bio::EnsEMBL::UnmappedObject | ||
| 377 | Exceptions : none | ||
| 378 | Caller : general | ||
| 379 | Status : At Risk | ||
| 380 | |||
| 381 | =cut | ||
| 382 | |||
| 383 | sub fetch_all_by_type { | ||
| 384 | my ($self, $type) = @_; | ||
| 385 | |||
| 386 | unless($type) { | ||
| 387 | throw("type argument is required"); | ||
| 388 | } | ||
| 389 | $self->bind_param_generic_fetch($type,SQL_VARCHAR); | ||
| 390 | $self->generic_fetch("uo.type = ?"); | ||
| 391 | |||
| 392 | } | ||
| 393 | |||
| 394 | =head2 fetch_all_by_analysis | ||
| 395 | |||
| 396 | Arg [1] : Bio:EnsEMBL::Analysis object | ||
| 397 | Arg [2] : (optional) string database name | ||
| 398 | Example : @unmapped_object = @{$uoa->fetch_all_by_analysis($analysis)}; | ||
| 399 | Description : Retrieves all the unmapped object for a particular | ||
| 400 | analysis type with the the option of a particular | ||
| 401 | database type. | ||
| 402 | Returntype : array ref of Bio::EnsEMBL::UnmappedObject | ||
| 403 | Exceptions : thorws if first argument is not an anaylisi object | ||
| 404 | Caller : general | ||
| 405 | Status : At Risk | ||
| 406 | |||
| 407 | =cut | ||
| 408 | |||
| 409 | sub fetch_all_by_analysis { | ||
| 410 | my ($self, $analysis,$dbname) = @_; | ||
| 411 | |||
| 412 | unless($analysis) { | ||
| 413 | throw("analysis argument is required"); | ||
| 414 | } | ||
| 415 | $self->bind_param_generic_fetch($analysis->dbID,SQL_INTEGER); | ||
| 416 | my $constraint = "uo.analysis_id = ?"; | ||
| 417 | if(defined($dbname)){ | ||
| 418 | my $db_id =0; | ||
| 419 | my $sth = $self->prepare('select external_db_id from external_db where db_name like "'. | ||
| 420 | $dbname.'"'); | ||
| 421 | $sth->execute; | ||
| 422 | $sth->bind_columns(\$db_id); | ||
| 423 | $sth->fetch(); | ||
| 424 | if(!defined($db_id) or $db_id == 0){ | ||
| 425 | throw("$dbname could not be found in the external database table\n"); | ||
| 426 | } | ||
| 427 | $self->bind_param_generic_fetch($db_id,SQL_INTEGER); | ||
| 428 | $constraint .= " AND uo.external_db_id = ?"; | ||
| 429 | } | ||
| 430 | $self->generic_fetch($constraint); | ||
| 431 | |||
| 432 | } | ||
| 433 | |||
| 434 | =head2 fetch_by_identifier | ||
| 435 | |||
| 436 | Arg [1] : string type. The type of unmapped objects | ||
| 437 | Arg [2] : (optional) string database name | ||
| 438 | Example : @unmapped_object = @{$uoa->fetch_by_identifier('Q123345')}; | ||
| 439 | Description : Retrieves the unmapped object for a particular | ||
| 440 | identifier/accession | ||
| 441 | Returntype : array ref of Bio::EnsEMBL::UnmappedObject | ||
| 442 | Exceptions : none | ||
| 443 | Caller : general | ||
| 444 | Status : At Risk | ||
| 445 | |||
| 446 | =cut | ||
| 447 | |||
| 448 | sub fetch_by_identifier { | ||
| 449 | my ($self, $identifier, $dbname) = @_; | ||
| 450 | |||
| 451 | unless($identifier) { | ||
| 452 | throw("identifier argument is required"); | ||
| 453 | } | ||
| 454 | $self->bind_param_generic_fetch($identifier,SQL_VARCHAR); | ||
| 455 | my $constraint = 'uo.identifier like ?'; | ||
| 456 | |||
| 457 | if(defined($dbname)){ | ||
| 458 | my $db_id =0; | ||
| 459 | my $sth = $self->prepare('select external_db_id from external_db where db_name like "'. | ||
| 460 | $dbname.'"'); | ||
| 461 | $sth->execute; | ||
| 462 | $sth->bind_columns(\$db_id); | ||
| 463 | $sth->fetch(); | ||
| 464 | if(!defined($db_id) or $db_id == 0){ | ||
| 465 | throw("$dbname could not be found in the external database table\n"); | ||
| 466 | } | ||
| 467 | $self->bind_param_generic_fetch($db_id,SQL_INTEGER); | ||
| 468 | $constraint .= " AND uo.external_db_id = ?"; | ||
| 469 | } | ||
| 470 | return $self->generic_fetch($constraint); | ||
| 471 | } | ||
| 472 | |||
| 473 | =head2 fetch_all_by_object_type_id | ||
| 474 | |||
| 475 | Arg [1] : string - The object type of the ensembl object e.g. Gene | ||
| 476 | Arg [2] : int - The internal dbID of the ensembl object | ||
| 477 | Example : my @unmapped_objects = @{$uoa->fetch_all_by_object_type_id('Gene', 12341)}; | ||
| 478 | Description : Retrieves the unmapped objects for a particular ensembl object | ||
| 479 | This is a base method which should be called by wrapper methods | ||
| 480 | defining the correct object type e.g. $uoa->fetch_all_by_Gene($gene) | ||
| 481 | Returntype : array ref of Bio::EnsEMBL::UnmappedObject objects | ||
| 482 | Exceptions : Throws if arguments are not defined | ||
| 483 | Caller : general | ||
| 484 | Status : At Risk | ||
| 485 | |||
| 486 | =cut | ||
| 487 | |||
| 488 | sub fetch_all_by_object_type_id { | ||
| 489 | my ($self, $object_type, $dbid) = @_; | ||
| 490 | |||
| 491 | if(! ($object_type && $dbid)){ | ||
| 492 | throw("object_type and dbid arguments required"); | ||
| 493 | } | ||
| 494 | |||
| 495 | $self->bind_param_generic_fetch($object_type, SQL_VARCHAR); | ||
| 496 | $self->bind_param_generic_fetch($dbid, SQL_INTEGER); | ||
| 497 | |||
| 498 | my $constraint = 'uo.ensembl_object_type=? and uo.ensembl_id=?'; | ||
| 499 | |||
| 500 | return $self->generic_fetch($constraint); | ||
| 501 | } | ||
| 502 | |||
| 503 | |||
| 504 | |||
| 505 | 1; |