AttributeAdaptor.pm 13.4 KB
Newer Older
1
=head1 LICENSE
2

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Copyright [1999-2013] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

=cut
18 19 20 21 22 23



=head1 CONTACT

  Please email comments or questions to the public Ensembl
Magali Ruffier's avatar
Magali Ruffier committed
24
  developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.
25 26

  Questions may also be sent to the Ensembl help desk at
Magali Ruffier's avatar
Magali Ruffier committed
27
  <http://www.ensembl.org/Help/Contact>.
28 29

=cut
30 31 32 33 34 35 36 37 38

=head1 NAME

Bio::EnsEMBL::DBSQL::AttributeAdaptor - Provides database interaction for
Bio::EnsEMBL::Attribute objects.


=head1 SYNOPSIS

39
  # $db is a Bio::EnsEMBL::DBSQL::DBAdaptor object:
40 41
  $attribute_adaptor = $db->get_AttributeAdaptor();

42
  $attributes = $attribute_adaptor->fetch_all_by_MiscFeature($feature);
43

44
  $attributes = $attribute_adaptor->fetch_all_by_Slice($slice);
45

46
  $attribute_adaptor->store_on_Slice( $slice, \@attributes );
47

48 49
  $attribute_adaptor->store_on_MiscFeature( $misc_feature,
    \@attributes )
50 51 52

=head1 DESCRIPTION

53
=head1 METHODS
54 55 56

=cut

57 58
package Bio::EnsEMBL::DBSQL::AttributeAdaptor;

59 60 61 62 63 64
use strict;
use warnings;

use Bio::EnsEMBL::DBSQL::BaseAdaptor;
use Bio::EnsEMBL::Attribute;

65
use Bio::EnsEMBL::Utils::Exception qw(throw warning);
66
use Bio::EnsEMBL::Utils::Scalar qw(assert_ref);
67

68 69 70 71 72 73 74 75 76 77 78
use vars qw(@ISA);

@ISA = qw(Bio::EnsEMBL::DBSQL::BaseAdaptor);

=head2 new

  Arg [...]  : Superclass args.  See Bio::EnsEMBL::DBSQL::BaseAdaptor
  Description: Instantiates a Bio::EnsEMBL::DBSQL::AttributeAdaptor
  Returntype : Bio::EnsEMBL::AttributeAdaptor
  Exceptions : none
  Caller     : DBAdaptor
79
  Status     : Stable
80 81 82 83 84 85 86 87 88 89 90 91 92 93

=cut

sub new {
  my $class = shift;

  my $self = $class->SUPER::new(@_);

  # cache creation could go here
  return $self;
}



94
## Storing methods
95

96
## Batch store
Patrick Meidl's avatar
Patrick Meidl committed
97

98 99
sub store_batch_on_Object {
  my ($self, $table, $attributes, $batch_size) = @_;
100 101 102 103 104
  # $attributes is a hashref where the key is the object ID
  # and the value is an array ref of Attribute objects

  # maintain a hash of attrib type IDs by code so we don't have to keep looking them up...
  my $attrib_type_ids = {};
105

106 107 108
  # create an arrayref of the values to store
  my $rows = [];
  $batch_size ||= scalar(values(%$attributes));
109

110
  while (my ($obj_id, $attribs) = each %{$attributes}) {
111 112 113 114 115 116 117 118 119 120 121 122 123 124
    for my $attrib (@{$attribs}) {

      my $attrib_type_id = $attrib_type_ids->{$attrib->code()};
      if (!defined $attrib_type_id) {
        $attrib_type_id = $self->_store_type($attrib);
        $attrib_type_ids->{$attrib->code()} = $attrib_type_id;
      }

      push @$rows, sprintf('(%d, %d, %s)', $obj_id, $attrib_type_id, $self->dbc()->db_handle()->quote($attrib->value()));

      if (scalar(@$rows) == $batch_size) {
        $rows = $self->_store_batch_rows($table, $rows);
      }
    }
125
  }
126 127 128

  $rows = $self->_store_batch_rows($table, $rows);

129
  return;
130
}
131 132

sub _store_batch_rows {
133
  my ($self, $table, $rows) = @_;
134
  if (scalar(@$rows) > 0) {
135
        $self->dbc()->sql_helper()->execute_update(-SQL => 'INSERT INTO ' . $table . '_attrib() VALUES' . join(',', @$rows));
136 137 138 139
  }
  return [];
}

140

141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
sub store_batch_on_MiscAttrib {
  my ($self, $attributes, $batch_size) = @_;

  $self->store_batch_on_Object('misc_feature', $attributes, $batch_size);

  return;
}

sub store_batch_on_Slice {
  my ($self, $attributes, $batch_size) = @_;

  $self->store_batch_on_Object('seq_region', $attributes, $batch_size);

  return;
}

sub store_batch_on_Gene {
  my ($self, $attributes, $batch_size) = @_;

  $self->store_batch_on_Object('gene', $attributes, $batch_size);

  return;
}

sub store_batch_on_Transcript {
  my ($self, $attributes, $batch_size) = @_;

  $self->store_batch_on_Object('transcript', $attributes, $batch_size);

  return;
}

sub store_batch_on_Translation {
  my ($self, $attributes, $batch_size) = @_;

  $self->store_batch_on_Object('translation', $attributes, $batch_size);

  return;
}


## Single store

sub store_on_Object {
  my ($self, $object_id, $attributes, $table, $type) = @_;

  my $db = $self->db();
  if (!defined $type) {
    $type = $table;
190 191
  }

192
  my $sth = $self->prepare( "INSERT IGNORE into " . $table. "_attrib ".
193 194 195 196 197 198 199 200 201 202 203 204 205 206
                            "SET " . $type . "_id = ?, attrib_type_id = ?, ".
                            "value = ? " );

  for my $attrib ( @$attributes ) {
    if(!ref($attrib) && $attrib->isa('Bio::EnsEMBL::Attribute')) {
      throw("Reference to list of Bio::EnsEMBL::Attribute objects " .
            "argument expected.");
    }

    my $atid = $self->_store_type( $attrib );
    $sth->bind_param(1,$object_id,SQL_INTEGER);
    $sth->bind_param(2,$atid,SQL_INTEGER);
    $sth->bind_param(3,$attrib->value,SQL_VARCHAR);
    $sth->execute();
207 208
  }

209
  return;
210
}
211

Patrick Meidl's avatar
Patrick Meidl committed
212

213 214 215 216
sub store_on_MiscFeature {
  my ($self, $object, $attributes) = @_;

  assert_ref( $object, 'Bio::EnsEMBL::MiscFeature');
Patrick Meidl's avatar
Patrick Meidl committed
217

218 219
  my $object_id = $object->dbID();
  $self->store_on_Object($object_id, $attributes, 'misc', 'misc_feature');
220

221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
  return;
}

sub store_on_Slice {
  my ($self, $object, $attributes) = @_;

  assert_ref( $object, 'Bio::EnsEMBL::Slice');

  my $object_id = $object->get_seq_region_id();
  $self->store_on_Object($object_id, $attributes, 'seq_region');

  my $undef_circular_cache = 0;
  for my $attrib ( @$attributes ) {
    if ((defined $attrib->code) and ($attrib->code eq 'circular_seq')) {
        $undef_circular_cache = 1;
    }
237
  }
238

239 240 241 242 243 244 245 246 247
  if ($undef_circular_cache) {
  #the slice is circular
    $object->{'circular'} = 1;
    my $slice_adaptor = $object->adaptor();
    #undefine slice adaptor->is_circular and the circular slice cache
    if (defined $slice_adaptor) {
      $slice_adaptor->{'is_circular'} = undef;
      $slice_adaptor->{'circular_sr_id_cache'} = {};
    }
248 249
  }

250 251 252 253 254 255
  return;
}

sub store_on_Gene {
  my ($self, $object, $attributes) = @_;

256 257 258 259 260 261 262
  my $object_id;
  if (!ref($object)){      
    $object_id = $object;
  }
  else {
    $object_id = $object->dbID;    
  }
263 264 265 266 267 268 269 270 271

  $self->store_on_Object($object_id, $attributes, 'gene');
 
  return;
}

sub store_on_Transcript {
  my ($self, $object, $attributes) = @_;

272 273 274 275 276 277 278
  my $object_id;
  if (!ref($object)){      
    $object_id = $object;
  }
  else {
    $object_id = $object->dbID;    
  }
279 280 281 282 283 284 285 286 287

  $self->store_on_Object($object_id, $attributes, 'transcript');

  return;
}

sub store_on_Translation {
  my ($self, $object, $attributes) = @_;

288 289 290 291 292 293 294
  my $object_id;
  if (!ref($object)){
    $object_id = $object;
  }
  else {
    $object_id = $object->dbID;
  }
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315

  $self->store_on_Object($object_id, $attributes, 'translation');

  return;
}


## Remove methods

sub remove_from_Object {
  my ($self, $object_id, $table, $code, $type) = @_;

  my $db = $self->db();
  
  if(!defined($object_id)) {
    throw("$table must have dbID.");
  } 
  if (!defined($type)) {
    $type = $table;
  }
  
316
  my $sth;
317 318 319 320 321 322 323 324 325 326 327 328
  if(defined($code)){
    $sth = $self->prepare("DELETE a FROM " . $table . "_attrib a, attrib_type at " .
                         "WHERE a.attrib_type_id = at.attrib_type_id AND ".
                         "a." . $type . "_id = ? AND ".
                         "at.code like ?");
    $sth->bind_param(1,$object_id,SQL_INTEGER);
    $sth->bind_param(2,$code,SQL_VARCHAR);
  }
  else{
    $sth = $self->prepare("DELETE FROM " . $table . "_attrib " .
                         "WHERE " . $type . "_id = ?");
    $sth->bind_param(1,$object_id,SQL_INTEGER);
329
  }
330
  $sth->execute();
331
  $sth->finish();
332 333 334 335 336 337 338 339 340 341 342 343 344
  
  return;

}


sub remove_from_MiscFeature {
  my ($self, $object, $code) = @_;

  assert_ref($object, 'Bio::EnsEMBL::MiscFeature');

  my $object_id = $object->dbID();
  $self->remove_from_Object($object_id, 'misc', $code, 'misc_feature');
345 346

  return;
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400

}

sub remove_from_Slice {
  my ($self, $object, $code) = @_;

  assert_ref($object, 'Bio::EnsEMBL::Slice');

  my $object_id = $object->get_seq_region_id();
  $self->remove_from_Object($object_id, 'seq_region', $code);

  return;

}

sub remove_from_Gene {
  my ($self, $object, $code) = @_;

  assert_ref($object, 'Bio::EnsEMBL::Gene');

  my $object_id = $object->dbID();
  $self->remove_from_Object($object_id, 'gene', $code);

  return;

}

sub remove_from_Transcript {
  my ($self, $object, $code) = @_;

  assert_ref($object, 'Bio::EnsEMBL::Transcript');

  my $object_id = $object->dbID();
  $self->remove_from_Object($object_id, 'transcript', $code);
  
  return;

}

sub remove_from_Translation {
  my ($self, $object, $code) = @_;

  assert_ref($object, 'Bio::EnsEMBL::Translation');

  my $object_id = $object->dbID();
  $self->remove_from_Object($object_id, 'translation', $code);
  
  return;

}



## Fetch methods
401

402 403 404 405
sub fetch_all {
  throw("Use of method fetch_all not supported for attributes");
}

406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
sub fetch_by_code {
  my ($self, $code) = @_;

  my $sql = "SELECT attrib_type_id, code, name, description " .
            "FROM attrib_type 
             WHERE code = '$code' ";
  my $sth = $self->prepare($sql);
  $sth->execute();

  my ($attrib_type_id, $name, $desc);
  $sth->bind_columns(\$attrib_type_id, \$code, \$name, \$desc);

  my @results = $sth->fetchrow_array;
  $sth->finish();

  return \@results;
}

424

Patrick Meidl's avatar
Patrick Meidl committed
425

426 427 428 429 430
sub fetch_all_by_Object {
  my ($self, $object_id, $table, $code, $type) = @_;

  if (!defined $type) {
    $type = $table;
431 432
  }

433 434 435
  my $sql = "SELECT at.code, at.name, at.description, a.value " .
            "FROM " . $table . "_attrib a, attrib_type at "  .
            "WHERE at.attrib_type_id = a.attrib_type_id ";
436

437
  if (defined($code)) {
438 439
        $sql .= 'AND at.code like "' . $code . '" ';
  } 
440
  if (defined($object_id)) {
441
        $sql .= "AND a." .$type . "_id = " . $object_id;
442
  }
443

444
  my $sth = $self->prepare($sql);
445 446 447
  $sth->execute();
  my $results = $self->_obj_from_sth($sth);
  $sth->finish();
448

449
  return $results;
450
}
451

452

453 454 455
sub fetch_all_by_MiscFeature {
  my ($self, $object, $code) = @_;

456 457
  my $object_id;

458 459
  if (defined($object)) {
    assert_ref($object, 'Bio::EnsEMBL::MiscFeature');
460
    $object_id = $object->dbID();
461 462 463 464 465 466 467 468 469 470 471
  }

  my $results = $self->fetch_all_by_Object($object_id, 'misc', $code, 'misc_feature');

  return $results;

}

sub fetch_all_by_Slice {
  my ($self, $object, $code) = @_;

472 473
  my $object_id;

474 475
  if (defined($object)) {
    assert_ref($object, 'Bio::EnsEMBL::Slice');
476
    $object_id = $object->get_seq_region_id();
477 478 479 480 481 482 483 484 485 486 487
  }

  my $results = $self->fetch_all_by_Object($object_id, 'seq_region', $code);

  return $results;

}

sub fetch_all_by_Gene {
  my ($self, $object, $code) = @_;

488 489
  my $object_id;

490 491
  if (defined($object)) {
    assert_ref($object, 'Bio::EnsEMBL::Gene');
492
    $object_id = $object->dbID();
493 494 495 496 497 498 499 500 501 502 503
  }

  my $results = $self->fetch_all_by_Object($object_id, 'gene', $code);

  return $results;

}

sub fetch_all_by_Transcript {
  my ($self, $object, $code) = @_;

504 505
  my $object_id;

506 507
  if (defined($object)) {
    assert_ref($object, 'Bio::EnsEMBL::Transcript');
508
    $object_id = $object->dbID();
509 510 511 512 513 514
  }

  my $results = $self->fetch_all_by_Object($object_id, 'transcript', $code);

  return $results;

515 516
}

517 518 519
sub fetch_all_by_Translation {
  my ($self, $object, $code) = @_;

520 521
  my $object_id;

522 523
  if (defined($object)) {
    assert_ref($object, 'Bio::EnsEMBL::Translation');
524
    $object_id = $object->dbID();
525 526 527 528 529 530 531 532 533 534
  }

  my $results = $self->fetch_all_by_Object($object_id, 'translation', $code);
  
  return $results;

}



535 536 537 538 539 540 541 542
#
# _id_check
#
# backwards compatibility check:
# check if $ensID is an object; if so, return $obj->dbID
#

sub _id_check {
543
  my $self  = shift;
544
  my $ensID = shift;
545

546
  if ($ensID =~ /^\d+$/) {
547 548 549 550 551 552
	return $ensID;

  } elsif (   ref($ensID) eq 'Bio::EnsEMBL::Gene'
		   or ref($ensID) eq 'Bio::EnsEMBL::Transcript'
		   or ref($ensID) eq 'Bio::EnsEMBL::Translation')
  {
553

554
	warning("You should pass a dbID rather than an ensembl object to store the attribute on");
555

556 557 558 559 560
	if ($ensID->dbID) {
	  return $ensID->dbID;
	} else {
	  throw("Ensembl object " . $ensID->display_id . " doesn't have a dbID, can't store attribute");
	}
561 562

  } else {
563
	throw("Invalid dbID");
564 565
  }

566
} ## end sub _id_check
567 568

# _store_type
569 570

sub _store_type {
571
  my $self   = shift;
572 573
  my $attrib = shift;

574
  my $sth1 = $self->prepare("INSERT IGNORE INTO attrib_type set code = ?, name = ?, " . "description = ?");
575

576 577 578
  $sth1->bind_param(1, $attrib->code,        SQL_VARCHAR);
  $sth1->bind_param(2, $attrib->name,        SQL_VARCHAR);
  $sth1->bind_param(3, $attrib->description, SQL_LONGVARCHAR);
579

580
  my $rows_inserted = $sth1->execute();
581

582
  my $atid = $sth1->{'mysql_insertid'};
583

584 585 586 587 588 589 590 591 592 593 594 595
  if ($rows_inserted == 0) {
	# the insert failed because the code is already stored
	my $sth2 = $self->prepare("SELECT attrib_type_id FROM attrib_type " . "WHERE code = ?");
	$sth2->bind_param(1, $attrib->code, SQL_VARCHAR);
	$sth2->execute();
	($atid) = $sth2->fetchrow_array();

	$sth2->finish();

	if (!$atid) {
	  throw("Could not store or fetch attrib_type code [" . $attrib->code . "]\n" . "Wrong database user/permissions?");
	}
596 597
  }

598 599
  $sth1->finish();

600
  return $atid;
601
} ## end sub _store_type
602 603 604

sub _obj_from_sth {
  my $self = shift;
605
  my $sth  = shift;
606

607 608
  my ($code, $name, $desc, $value);
  $sth->bind_columns(\$code, \$name, \$desc, \$value);
609

610
  my @results;
611 612 613 614 615 616
  while ($sth->fetch()) {
	push @results,
	  Bio::EnsEMBL::Attribute->new_fast({'code'        => $code,
										 'name'        => $name,
										 'description' => $desc,
										 'value'       => $value});
617
  }
618

619 620
  return \@results;
}
621 622

1;
623