AnalysisAdaptor.pm 11.5 KB
Newer Older
Arne Stabenau's avatar
Arne Stabenau committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# Perl module for Bio::EnsEMBL::DBSQL::AnalysisAdaptor
#
# Creator: Arne Stabenau <stabenau@ebi.ac.uk>
# Date of creation: 25.01.2001
# Last modified : 25.01.2001 by Arne Stabenau
#
# Copyright EMBL-EBI 2000
#
# You may distribute this module under the same terms as perl itself

# POD documentation - main docs before the code

=head1 NAME

Bio::EnsEMBL::DBSQL::AnalysisAdaptor 

=head1 SYNOPSIS

19
  $analysisAdaptor = $db_adaptor->getAnalysisAdaptor;
Arne Stabenau's avatar
Arne Stabenau committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
  $analysisAdaptor = $analysisobj->getAnalysisAdaptor;


=head1 DESCRIPTION
  
  Module to encapsulate all db access for persistent class Analysis.
  There should be just one per application and database connection.
     

=head1 CONTACT

    Contact Arne Stabenau on implemetation/design detail: stabenau@ebi.ac.uk
    Contact Ewan Birney on EnsEMBL in general: birney@sanger.ac.uk

=head1 APPENDIX

The rest of the documentation details each of the object methods. Internal methods are usually preceded with a _

=cut


# Let the code begin...


package Bio::EnsEMBL::DBSQL::AnalysisAdaptor;

46
use Bio::EnsEMBL::Analysis;
47
use Bio::EnsEMBL::DBSQL::BaseAdaptor;
Arne Stabenau's avatar
Arne Stabenau committed
48 49 50 51

use vars qw(@ISA);
use strict;

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

Arne Stabenau's avatar
Arne Stabenau committed
54

55 56 57

=head2 new

58 59 60 61 62 63 64 65
  Args       : Bio::EnsEMBL::DBSQL::DBAdaptor
  Example    : my $aa = new Bio::EnsEMBL::DBSQL::AnalysisAdaptor();
  Description: Creates a new Bio::EnsEMBL::DBSQL::AnalysisAdaptor object and
               internally loads and caches all the Analysis objects from the 
               database.
  Returntype : Bio::EnsEMBL::DBSQL::AnalysisAdaptor
  Exceptions : none
  Caller     : Bio::EnsEMBL::DBSQL::DBAdaptor
66 67 68

=cut

Arne Stabenau's avatar
Arne Stabenau committed
69
sub new {
70
  my ($class, $db) = @_;
Arne Stabenau's avatar
Arne Stabenau committed
71

72 73 74
  my $self = $class->SUPER::new($db);
  
  #load and cache all of the Analysis objects
Arne Stabenau's avatar
Arne Stabenau committed
75
  $self->fetch_all;
76

Arne Stabenau's avatar
Arne Stabenau committed
77 78 79
  return $self;
}

80

Arne Stabenau's avatar
Arne Stabenau committed
81 82
=head2 fetch_all

83 84 85 86
  Args       : none
  Example    : my @analysis = $analysis_adaptor->fetch_all()
  Description: fetches all of the Analysis objects from the database and caches
               them internally.
Graham McVicker's avatar
Graham McVicker committed
87
  Returntype : listref of Bio::EnsEMBL::Analysis retrieved from the database
88 89
  Exceptions : none
  Caller     : AnalysisAdaptor::new
Arne Stabenau's avatar
Arne Stabenau committed
90 91 92 93 94 95 96 97 98

=cut

sub fetch_all {
  my $self = shift;
  my ( $analysis, $dbID );
  my $rowHashRef;

  $self->{_cache} = {};
99
  $self->{_logic_name_cache} = {};
Arne Stabenau's avatar
Arne Stabenau committed
100 101
  
  my $sth = $self->prepare( q {
Simon Potter's avatar
Simon Potter committed
102 103 104 105 106
    SELECT analysis_id, logic_name,
           program, program_version, program_file,
           db, db_version, db_file,
           module, module_version,
           gff_source, gff_feature,
Arne Stabenau's avatar
Arne Stabenau committed
107
           created, parameters
Simon Potter's avatar
Simon Potter committed
108
    FROM   analysis } );
Arne Stabenau's avatar
Arne Stabenau committed
109 110 111 112 113
  $sth->execute;

  while( $rowHashRef = $sth->fetchrow_hashref ) {
    my $analysis = $self->_objFromHashref( $rowHashRef  );
    $self->{_cache}->{$analysis->dbID} = $analysis;
114
    $self->{_logic_name_cache}->{$analysis->logic_name()} = $analysis;
Arne Stabenau's avatar
Arne Stabenau committed
115 116
  }

Graham McVicker's avatar
Graham McVicker committed
117
  return \{values %{$self->{_cache}}};
Arne Stabenau's avatar
Arne Stabenau committed
118 119
}

120

Arne Stabenau's avatar
Arne Stabenau committed
121 122
=head2 fetch_by_dbID

123 124 125 126 127 128 129 130
  Arg [1]    : int $internal_analysis_id - the database id of the analysis 
               record to retrieve
  Example    : my $analysis = $analysis_adaptor->fetch_by_dbID(1);
  Description: Retrieves an Analysis object from the database via its internal
               id.
  Returntype : Bio::EnsEMBL::Analysis
  Exceptions : none
  Caller     : general
131 132 133

=cut

Arne Stabenau's avatar
Arne Stabenau committed
134 135 136 137 138 139 140 141
sub fetch_by_dbID {
  my $self = shift;
  my $id = shift;

  if( defined $self->{_cache}->{$id} ) {
    return $self->{_cache}->{$id};
  }

Michele Clamp's avatar
Debug  
Michele Clamp committed
142 143

  my $query = q{
Simon Potter's avatar
Simon Potter committed
144 145 146 147 148
    SELECT analysis_id, logic_name,
           program, program_version, program_file,
           db, db_version, db_file,
           module, module_version,
           gff_source, gff_feature,
Arne Stabenau's avatar
Arne Stabenau committed
149
           created, parameters
Simon Potter's avatar
Simon Potter committed
150 151
    FROM   analysis
    WHERE  analysis_id = ? };
Michele Clamp's avatar
Debug  
Michele Clamp committed
152 153

  my $sth = $self->prepare($query);  
Arne Stabenau's avatar
Arne Stabenau committed
154 155 156 157 158 159 160 161
  $sth->execute( $id );
  my $rowHashRef = $sth->fetchrow_hashref;
  if( ! defined $rowHashRef ) {
    return undef;
  }

  my $anal = $self->_objFromHashref( $rowHashRef );
  $self->{_cache}->{$anal->dbID} = $anal;
162
  $self->{_logic_name_cache}->{$anal->logic_name()} = $anal;
Arne Stabenau's avatar
Arne Stabenau committed
163 164 165
  return $anal;
}

166

167
=head2 fetch_by_logic_name
168

169 170 171 172 173 174 175
  Arg [1]    : string $logic_name the logic name of the analysis to retrieve
  Example    : my $analysis = $a_adaptor->fetch_by_logic_name('Eponine');
  Description: Retrieves an analysis object from the database using its unique
               logic name.
  Returntype : Bio::EnsEMBL::Analysis
  Exceptions : none
  Caller     : general
176 177 178

=cut

Arne Stabenau's avatar
Arne Stabenau committed
179 180 181 182 183 184
sub fetch_by_logic_name {
  my $self = shift;
  my $logic_name = shift;
  my $analysis;
  my $rowHash;

185 186 187 188 189
  #check the cache for the logic name
  if(defined $self->{_logic_name_cache}{$logic_name}) {
    return $self->{_logic_name_cache}{$logic_name};
  }

190
  my $sth = $self->prepare( "
Simon Potter's avatar
Simon Potter committed
191 192 193 194 195
    SELECT analysis_id, logic_name,
           program, program_version, program_file,
           db, db_version, db_file,
           module, module_version,
           gff_source, gff_feature,
Arne Stabenau's avatar
Arne Stabenau committed
196
           created, parameters
Simon Potter's avatar
Simon Potter committed
197
    FROM   analysis
198
    WHERE  logic_name = ?" );
Arne Stabenau's avatar
Arne Stabenau committed
199
  
200
  $sth->execute($logic_name);
Arne Stabenau's avatar
Arne Stabenau committed
201
  my $rowHashRef;
202
  $rowHashRef = $sth->fetchrow_hashref; 
203 204 205 206 207

  unless(defined $rowHashRef) {
    return undef;
  }

208 209
  $analysis = $self->_objFromHashref( $rowHashRef );
  
210 211 212 213
  #place the analysis in the caches, cross referenced by dbID and logic_name
  $self->{_cache}{$analysis->dbID()} = $analysis;
  $self->{_logic_name_cache}{$logic_name} = $analysis;

214
  return $analysis;
Arne Stabenau's avatar
Arne Stabenau committed
215 216
}

217

218

219 220
=head2 store

221 222 223 224 225 226 227 228
  Arg [1]    : Bio:EnsEMBL::Analysis $analysis 
  Example    : $analysis_adaptor->store($analysis);
  Description: stores $analysis in db. Does not if already equiped with dbID.
               Sets created date if not already set. Sets dbID and adaptor
               inside $analysis. Returns dbID.
  Returntype : int dbID of stored analysis
  Exceptions : thrown if analysis argument does not have a logic name
  Caller     : ?
229 230 231

=cut

Arne Stabenau's avatar
Arne Stabenau committed
232 233 234 235
sub store {

  my $self = shift;
  my $analysis = shift;
236 237 238 239 240
  
  if( !defined $analysis || !ref $analysis) {
    $self->throw("called store on AnalysisAdaptor with a [$analysis]");
  }

241
  $analysis->dbID && return $analysis->dbID;
Arne Stabenau's avatar
Arne Stabenau committed
242
  my $dbID;
243 244 245 246 247 248
 
  if( !defined $analysis->logic_name ) {
    $self->throw("Must have a logic name on the analysis object");
  }

 
Arne Stabenau's avatar
Arne Stabenau committed
249 250
  if( defined $analysis->created ) {
    my $sth = $self->prepare( q{
Simon Potter's avatar
Simon Potter committed
251
      INSERT INTO analysis
Arne Stabenau's avatar
Arne Stabenau committed
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
      SET created = ?,
          logic_name = ?,
	  db = ?,
	  db_version = ?,
          db_file = ?,
          program = ?,
          program_version = ?,
          program_file = ?,
	  parameters = ?,
          module = ?,
          module_version = ?,
          gff_source = ?,
          gff_feature = ? } );
    $sth->execute
      ( $analysis->created,
	$analysis->logic_name,
	$analysis->db,
	$analysis->db_version,
	$analysis->db_file,
	$analysis->program,
	$analysis->program_version,
	$analysis->program_file,
	$analysis->parameters,
	$analysis->module,
	$analysis->module_version,
	$analysis->gff_source,
	$analysis->gff_feature
      );
280
    $dbID = $sth->{'mysql_insertid'};
Arne Stabenau's avatar
Arne Stabenau committed
281 282 283
  } else {
    my $sth = $self->prepare( q{

Simon Potter's avatar
Simon Potter committed
284
      INSERT INTO analysis
Arne Stabenau's avatar
Arne Stabenau committed
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
      SET created = now(),
          logic_name = ?,
	  db = ?,
	  db_version = ?,
          db_file = ?,
          program = ?,
          program_version = ?,
          program_file = ?,
	  parameters = ?,
          module = ?,
          module_version = ?,
          gff_source = ?,
          gff_feature = ? } );

    $sth->execute
      ( $analysis->logic_name,
	$analysis->db,
	$analysis->db_version,
	$analysis->db_file,
	$analysis->program,
	$analysis->program_version,
	$analysis->program_file,
	$analysis->parameters,
	$analysis->module,
	$analysis->module_version,
	$analysis->gff_source,
	$analysis->gff_feature
      );

314
    $dbID = $sth->{'mysql_insertid'};
Arne Stabenau's avatar
Arne Stabenau committed
315 316 317 318

    if( defined $dbID ) {
      $sth = $self->prepare( q{
	SELECT created 
Simon Potter's avatar
Simon Potter committed
319 320
	FROM   analysis
	WHERE  analysis_id = ? } );
Arne Stabenau's avatar
Arne Stabenau committed
321 322 323 324 325 326 327 328
      $sth->execute( $dbID );
      $analysis->created( ($sth->fetchrow_array)[0] );
    }
  }
  $self->{_cache}->{$dbID} = $analysis;

  if( $analysis->can( "adaptor" )) {
    $analysis->adaptor( $self );
329
    $analysis->dbID( $dbID );
Arne Stabenau's avatar
Arne Stabenau committed
330 331 332 333 334
  }
  
  return $dbID;
}

335 336


Arne Stabenau's avatar
Arne Stabenau committed
337 338
=head2 exists

339 340 341 342 343 344 345
  Arg [1]    : Bio::EnsEMBL::Analysis $anal
  Example    : if($adaptor->exists($anal)) #do something
  Description: Tests whether this Analysis already exists in the database.
               Returns a true value if it does.
  Returntype : boolean
  Exceptions : thrown if $anal arg is not an analysis object
  Caller     : ?
Arne Stabenau's avatar
Arne Stabenau committed
346 347 348 349

=cut

sub exists {
350
  my ($self,$anal) = @_;
Arne Stabenau's avatar
Arne Stabenau committed
351

352 353 354 355 356 357 358
  unless($anal->isa("Bio::EnsEMBL::Analysis")) {
    $self->throw("Object is not a Bio::EnsEMBL::Analysis");
  } 
  
  
  # objects with already have this adaptor are store here.
  if( $anal->can("adaptor") && defined $anal->adaptor &&
Arne Stabenau's avatar
Arne Stabenau committed
359
      $anal->adaptor == $self ) {
360 361
    if (my $id = $anal->dbID) {
      return $id;
Arne Stabenau's avatar
Arne Stabenau committed
362
    }
363 364
    else {
      $self->throw ("analysis does not have an analysisId");
Arne Stabenau's avatar
Arne Stabenau committed
365
    }
366 367 368 369 370 371 372 373 374 375
  }
  
  foreach my $cacheId (keys %{$self->{_cache}}) {
    if ($self->{_cache}->{$cacheId}->compare($anal) >= 0) {
      # $anal->dbID( $cacheId );
      # $anal->adaptor( $self );
      return $cacheId;
    }
  }
  return undef;
Arne Stabenau's avatar
Arne Stabenau committed
376 377 378
}


379
=head2 _objFromHashref
Arne Stabenau's avatar
Arne Stabenau committed
380

381 382 383 384 385 386
  Arg [1]    : hashref $rowHash
  Description: Private helper function generates an Analysis object from a 
               mysql row hash reference.
  Returntype : Bio::EnsEMBL::Analysis
  Exceptions : none
  Caller     : Bio::EnsEMBL::DBSQL::AnalsisAdaptor::fetch_* methods
Arne Stabenau's avatar
Arne Stabenau committed
387

388
=cut
Arne Stabenau's avatar
Arne Stabenau committed
389 390 391 392 393
  
sub _objFromHashref {
  my $self = shift;
  my $rowHash = shift;

Simon Potter's avatar
Simon Potter committed
394 395
  my $analysis = Bio::EnsEMBL::Analysis->new(
      -id              => $rowHash->{analysis_id},
396
      -adaptor         => $self,
Simon Potter's avatar
Simon Potter committed
397 398 399 400
      -db              => $rowHash->{db},
      -db_file         => $rowHash->{db_file},
      -db_version      => $rowHash->{db_version},
      -program         => $rowHash->{program},
Arne Stabenau's avatar
Arne Stabenau committed
401
      -program_version => $rowHash->{program_version},
Simon Potter's avatar
Simon Potter committed
402 403 404 405 406 407 408 409
      -program_file    => $rowHash->{program_file},
      -gff_source      => $rowHash->{gff_source},
      -gff_feature     => $rowHash->{gff_feature},
      -module          => $rowHash->{module},
      -module_version  => $rowHash->{module_version},
      -parameters      => $rowHash->{parameters},
      -created         => $rowHash->{created},
      -logic_name      => $rowHash->{logic_name}
Arne Stabenau's avatar
Arne Stabenau committed
410 411 412 413 414
    );
  
  return $analysis;
}

415 416 417 418 419 420 421 422 423 424 425 426 427 428

=head2 db

  Arg [1]    : (optional) Bio::EnsEMBL::DBSQL::DBAdaptor $db
               the database used by this adaptor.
  Example    : my $db = $analysis_adaptor->db()
  Description: Getter/Setter for the database this adaptor uses internally
               to fetch and store database objects.
  Returntype : Bio::EnsEMBL::DBSQL::DBAdaptor
  Exceptions : none
  Caller     : BaseAdaptor::new, general

=cut

Arne Stabenau's avatar
Arne Stabenau committed
429 430 431 432 433 434 435 436 437
sub db {
  my ( $self, $arg )  = @_;
  ( defined $arg ) &&
    ($self->{_db} = $arg);
  $self->{_db};;
}



438
=head2 fetch_by_newest_logic_name
Arne Stabenau's avatar
Arne Stabenau committed
439

440 441
  Description: DEPRECATED Logic names should now be unique and should not
               need to use this method.  Use fetch_by_logic_name instead.
Arne Stabenau's avatar
Arne Stabenau committed
442

443
=cut
444

445
sub fetch_by_newest_logic_name {
446
  my $self = shift;
447 448 449 450
  my $logic_name = shift;
  
  $self->warn("logic_names should now be unique should not " .
	      "need to use this method use fetch_by_logic_name\n");
451

452
  return $self->fetch_by_logic_name($logic_name);
453 454
}

Ensembl Pipeline's avatar
Ensembl Pipeline committed
455

456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
=head2 mysql2Unixtime

  Description: DEPRECATED do not use

=cut


sub mysql2Unixtime {
  my $sqltime = shift;
  
  my ($package, $file, $line) = caller();

  warn("AnalysisAdaptor::mysql2Unixtime is deprecated.\n " .
	      "package:$package: file:$file line:$line\n");

  require "Time::Local";
  
  my ($year,$month,$mday,$hour,$min,$sec ) = ( $sqltime =~ /(\d+)-(\d+)-(\d+)\s+(\d+):(\d+):(\d+)/ );
  my $time = timelocal( $sec, $min, $hour, $mday, $month, $year );
}

477
1;