Archive Ensembl HomeArchive Ensembl Home
AnalysisAdaptor.pm
Go to the documentation of this file.
00001 =head1 LICENSE
00002 
00003   Copyright (c) 1999-2012 The European Bioinformatics Institute and
00004   Genome Research Limited.  All rights reserved.
00005 
00006   This software is distributed under a modified Apache license.
00007   For license details, please see
00008 
00009     http://www.ensembl.org/info/about/code_licence.html
00010 
00011 =head1 CONTACT
00012 
00013   Please email comments or questions to the public Ensembl
00014   developers list at <dev@ensembl.org>.
00015 
00016   Questions may also be sent to the Ensembl help desk at
00017   <helpdesk@ensembl.org>.
00018 
00019 =cut
00020 
00021 =head1 NAME
00022 
00023 Bio::EnsEMBL::DBSQL::AnalysisAdaptor
00024 
00025 =head1 SYNOPSIS
00026 
00027   use Bio::EnsEMBL::Registry;
00028 
00029   Bio::EnsEMBL::Registry->load_registry_from_db(
00030     -host => 'ensembldb.ensembl.org',
00031     -user => 'anonymous'
00032   );
00033 
00034   $analysis_adaptor =
00035     Bio::EnsEMBL::Registry->get_adaptor( "human", "core", "analysis" );
00036 
00037   my $analysis = $analysis_adaptor->fetch_by_logic_name('genscan');
00038 
00039 =head1 DESCRIPTION
00040 
00041   Module to encapsulate all db access for persistent class Analysis.
00042   There should be just one per application and database connection.
00043 
00044 =head1 METHODS
00045 
00046 =cut
00047 
00048 
00049 package Bio::EnsEMBL::DBSQL::AnalysisAdaptor;
00050 
00051 use Bio::EnsEMBL::Analysis;
00052 use Bio::EnsEMBL::DBSQL::BaseAdaptor;
00053 use Bio::EnsEMBL::Utils::Exception;
00054 
00055 use vars qw(@ISA);
00056 use strict;
00057 
00058 @ISA = qw( Bio::EnsEMBL::DBSQL::BaseAdaptor);
00059 
00060 
00061 =head2 new
00062 
00063   Args       : Bio::EnsEMBL::DBSQL::DBAdaptor
00064   Example    : my $aa = new Bio::EnsEMBL::DBSQL::AnalysisAdaptor();
00065   Description: Creates a new Bio::EnsEMBL::DBSQL::AnalysisAdaptor object and
00066                internally loads and caches all the Analysis objects from the 
00067                database.
00068   Returntype : Bio::EnsEMBL::DBSQL::AnalysisAdaptor
00069   Exceptions : none
00070   Caller     : Bio::EnsEMBL::DBSQL::DBAdaptor
00071   Status     : Stable
00072 
00073 =cut
00074 
00075 sub new {
00076   my ($class, $db) = @_;
00077 
00078   my $self = $class->SUPER::new($db);
00079 
00080   #load and cache all of the Analysis objects
00081   $self->fetch_all;
00082 
00083   return $self;
00084 }
00085 
00086 
00087 =head2 fetch_all
00088 
00089   Args       : none
00090   Example    : my @analysis = @{$analysis_adaptor->fetch_all()};
00091   Description: fetches all of the Analysis objects from the database and caches
00092                them internally.
00093   Returntype : listref of Bio::EnsEMBL::Analysis retrieved from the database
00094   Exceptions : none
00095   Caller     : AnalysisAdaptor::new
00096   Status     : Stable
00097 
00098 =cut
00099 
00100 sub fetch_all {
00101   my $self = shift;
00102   my ( $analysis, $dbID );
00103   my $rowHashRef;
00104 
00105   $self->{_cache} = {};
00106   $self->{_logic_name_cache} = {};
00107 
00108   my $sth = $self->prepare( q {
00109     SELECT analysis.analysis_id, logic_name,
00110            program, program_version, program_file,
00111            db, db_version, db_file,
00112            module, module_version,
00113            gff_source, gff_feature,
00114            created, parameters, description, display_label, displayable, web_data
00115     FROM   analysis
00116     LEFT JOIN analysis_description
00117     ON analysis.analysis_id = analysis_description.analysis_id } );
00118   $sth->execute;
00119 
00120   while( $rowHashRef = $sth->fetchrow_hashref ) {
00121     my $analysis = $self->_objFromHashref( $rowHashRef  );
00122 
00123     $self->{_cache}->{$analysis->dbID}                    = $analysis;
00124     $self->{_logic_name_cache}->{lc($analysis->logic_name())} = $analysis;
00125   }
00126 
00127   my @ana = values %{$self->{_cache}};
00128 
00129   return \@ana;
00130 }
00131 
00132 
00133 =head2 fetch_all_by_feature_class
00134 
00135   Arg [1]    : string $feature_cless - The name of the feature class
00136   Example    : my @analyses = @{$analysis_adaptor->fetch_all_by_feature_class('Gene');
00137   Description: Returns all analyses that correspond to a given 
00138                feature class; see feature_classes method for a list.
00139   Returntype : Listref of Bio::EnsEMBL::Analysis
00140   Exceptions : none
00141   Caller     : general
00142   Status     : Stable
00143 
00144 =cut
00145 
00146 sub fetch_all_by_feature_class {
00147   my $self = shift;
00148   my $feat_class = shift || throw( "Need a feature type, e.g. SimpleFeature" );
00149  
00150   my @feature_classes = $self->feature_classes; # List of all feature classes
00151   my %feat_table_map;
00152   foreach my $class( @feature_classes ){
00153     # Map e.g. DnaAlignFeature to dna_align_feature
00154     my $table = join( "_", map lc, ( $class =~ /([A-Z][a-z]+)/g ) );
00155     $feat_table_map{$class} = $table;
00156   }
00157   $feat_table_map{DensityFeature}='density_type'; # analysis_id in diff table
00158   my $feat_table = $feat_table_map{$feat_class} || 
00159       ( warning( "No feature type corresponding to $feat_class" ) &&
00160         return [] );
00161 
00162   my $sql_t = qq|
00163 SELECT DISTINCT analysis_id FROM %s |;
00164   
00165   my $sql = sprintf( $sql_t, $feat_table );
00166   my $sth = $self->prepare( $sql );
00167   my $rv  = $sth->execute();
00168   my $res = $sth->fetchall_arrayref;
00169   my @analyses;
00170   foreach my $r( @{$res} ){
00171     my $analysis = $self->fetch_by_dbID($r->[0]) 
00172         || throw( "analysis_id $r->[0] from $feat_table table "
00173                   . "is not in the analysis table!" );
00174     push @analyses, $analysis;
00175   }
00176   return [@analyses];
00177 }
00178 
00179 
00180 =head2 feature_classes
00181 
00182   Arg [1]    : NONE
00183   Example    : my @fclasses = $analysis_adaptor->feature_classes;
00184   Description: Returns a list of the different classes of Ensembl feature 
00185                object that have an analysis
00186   Returntype : List of feature classes
00187   Exceptions : none
00188   Caller     : general
00189   Status     : Stable
00190 
00191 =cut
00192 
00193 sub feature_classes{
00194   # Can't think of a way to do this programatically, so hard-coded
00195   return qw(
00196             DensityFeature
00197             DnaAlignFeature
00198             Gene
00199             MarkerFeature
00200             PredictionTranscript
00201             ProteinAlignFeature
00202             ProteinFeature
00203             QtlFeature
00204             RepeatFeature
00205             SimpleFeature
00206             );
00207 }
00208 
00209 =head2 fetch_by_dbID
00210 
00211   Arg [1]    : int $internal_analysis_id - the database id of the analysis 
00212                record to retrieve
00213   Example    : my $analysis = $analysis_adaptor->fetch_by_dbID(1);
00214   Description: Retrieves an Analysis object from the database via its internal
00215                id.
00216   Returntype : Bio::EnsEMBL::Analysis
00217   Exceptions : none
00218   Caller     : general
00219   Status     : Stable
00220 
00221 =cut
00222 
00223 sub fetch_by_dbID {
00224   my $self = shift;
00225   my $id = shift;
00226 
00227   if( defined $self->{_cache}->{$id} ) {
00228     return $self->{_cache}->{$id};
00229   }
00230 
00231   my $query = q{
00232     SELECT analysis.analysis_id, logic_name,
00233            program, program_version, program_file,
00234            db, db_version, db_file,
00235            module, module_version,
00236            gff_source, gff_feature,
00237            created, parameters, description, display_label, displayable, web_data
00238     FROM   analysis
00239     LEFT JOIN analysis_description
00240     ON analysis.analysis_id = analysis_description.analysis_id
00241     WHERE  analysis.analysis_id = ? };
00242 
00243   my $sth = $self->prepare($query);
00244   $sth->bind_param(1,$id,SQL_INTEGER);
00245   $sth->execute();
00246   my $rowHashRef = $sth->fetchrow_hashref;
00247   if( ! defined $rowHashRef ) {
00248     return undef;
00249   }
00250 
00251   my $anal = $self->_objFromHashref( $rowHashRef );
00252   $self->{_cache}->{$anal->dbID} = $anal;
00253   $self->{_logic_name_cache}->{lc($anal->logic_name())} = $anal;
00254   return $anal;
00255 }
00256 
00257 
00258 =head2 fetch_by_logic_name
00259 
00260   Arg [1]    : string $logic_name the logic name of the analysis to retrieve
00261   Example    : my $analysis = $a_adaptor->fetch_by_logic_name('Eponine');
00262   Description: Retrieves an analysis object from the database using its unique
00263                logic name.
00264   Returntype : Bio::EnsEMBL::Analysis
00265   Exceptions : none
00266   Caller     : general
00267   Status     : Stable
00268 
00269 =cut
00270 
00271 sub fetch_by_logic_name {
00272   my ( $self, $logic_name ) = @_;
00273 
00274   my $analysis;
00275   my $rowHash;
00276 
00277   # Check the cache for the logic name
00278   if ( defined( $self->{_logic_name_cache}{ lc($logic_name) } ) ) {
00279     return $self->{_logic_name_cache}{ lc($logic_name) };
00280   }
00281 
00282   my $sth = $self->prepare(
00283     qq(
00284 SELECT  analysis.analysis_id,
00285         logic_name,
00286         program,
00287         program_version,
00288         program_file,
00289         db,
00290         db_version,
00291         db_file,
00292         module,
00293         module_version,
00294         gff_source,
00295         gff_feature,
00296         created,
00297         parameters,
00298         description,
00299         display_label,
00300         displayable,
00301         web_data
00302 FROM    analysis
00303   LEFT JOIN analysis_description
00304     ON  ( analysis.analysis_id = analysis_description.analysis_id )
00305 WHERE  LOWER(logic_name) = ?)
00306   );
00307 
00308   $sth->bind_param( 1, lc($logic_name), SQL_VARCHAR );
00309   $sth->execute();
00310   my $rowHashRef = $sth->fetchrow_hashref();
00311 
00312   if ( !defined($rowHashRef) ) { return undef }
00313 
00314   $analysis = $self->_objFromHashref($rowHashRef);
00315 
00316   # place the analysis in the caches, cross referenced by dbID and
00317   # logic_name
00318   $self->{_cache}->{ $analysis->dbID() } = $analysis;
00319   $self->{_logic_name_cache}->{ lc($logic_name) } = $analysis;
00320 
00321   return $analysis;
00322 } ## end sub fetch_by_logic_name
00323 
00324 
00325 =head2 store
00326 
00327   Arg [1]    : Bio:EnsEMBL::Analysis $analysis
00328   Example    : $analysis_adaptor->store($analysis);
00329   Description: Stores $analysis in db.  If the analysis is already stored in
00330                the database its dbID and adaptor are updated, but the analysis
00331                is not stored a second time.
00332                Sets created date if not already set. Sets dbID and adaptor
00333                inside $analysis. Returns dbID.
00334   Returntype : int - dbID of stored analysis
00335   Exceptions : throw on incorrect argument
00336                throw if analysis argument does not have a logic name
00337   Caller     : general
00338   Status     : Stable
00339 
00340 =cut
00341 
00342 sub store {
00343   my $self = shift;
00344   my $analysis = shift;
00345 
00346   if(!ref($analysis) || !$analysis->isa('Bio::EnsEMBL::Analysis')) {
00347     throw("Bio::EnsEMBL::Analysis argument expected.");
00348   }
00349 
00350   if($analysis->is_stored($self->db())) {
00351     return $analysis->dbID();
00352   }
00353 
00354   if(!$analysis->logic_name()) {
00355     throw("Analysis cannot be stored without a valid logic_name");
00356   }
00357     
00358   my $insertion_method = (lc($self->dbc->driver) eq 'sqlite') ? 'INSERT OR IGNORE' : 'INSERT IGNORE';
00359 
00360   my $rows_inserted = 0;
00361   my $sth;
00362 
00363   if ( $analysis->created() ) {
00364 
00365     # We use insert IGNORE so that this method can be used in a
00366     # multi-process environment.  If another process has already written
00367     # this record then there will not be a problem.
00368 
00369     $sth = $self->prepare(
00370       qq{
00371           $insertion_method INTO analysis
00372               (created, logic_name, db, db_version, db_file, program, program_version, program_file, parameters, module, module_version, gff_source, gff_feature)
00373           VALUES  (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
00374       }
00375     );
00376     $sth->bind_param( 1,  $analysis->created(),          SQL_DATETIME );
00377     $sth->bind_param( 2,  lc( $analysis->logic_name() ), SQL_VARCHAR );
00378     $sth->bind_param( 3,  $analysis->db(),               SQL_VARCHAR );
00379     $sth->bind_param( 4,  $analysis->db_version(),       SQL_VARCHAR );
00380     $sth->bind_param( 5,  $analysis->db_file(),          SQL_VARCHAR );
00381     $sth->bind_param( 6,  $analysis->program(),          SQL_VARCHAR );
00382     $sth->bind_param( 7,  $analysis->program_version(),  SQL_VARCHAR );
00383     $sth->bind_param( 8,  $analysis->program_file(),     SQL_VARCHAR );
00384     $sth->bind_param( 9,  $analysis->parameters(),       SQL_VARCHAR );
00385     $sth->bind_param( 10, $analysis->module(),           SQL_VARCHAR );
00386     $sth->bind_param( 11, $analysis->module_version(),   SQL_VARCHAR );
00387     $sth->bind_param( 12, $analysis->gff_source(),       SQL_VARCHAR );
00388     $sth->bind_param( 13, $analysis->gff_feature(),      SQL_VARCHAR );
00389 
00390     $rows_inserted = $sth->execute();
00391 
00392   } else {
00393     $sth = $self->prepare(
00394       qq{
00395           $insertion_method INTO analysis
00396               (created, logic_name, db, db_version, db_file, program, program_version, program_file, parameters, module, module_version, gff_source, gff_feature)
00397           VALUES  (CURRENT_TIMESTAMP, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
00398       }
00399     );
00400 
00401     $sth->bind_param( 1,  $analysis->logic_name,      SQL_VARCHAR );
00402     $sth->bind_param( 2,  $analysis->db,              SQL_VARCHAR );
00403     $sth->bind_param( 3,  $analysis->db_version,      SQL_VARCHAR );
00404     $sth->bind_param( 4,  $analysis->db_file,         SQL_VARCHAR );
00405     $sth->bind_param( 5,  $analysis->program,         SQL_VARCHAR );
00406     $sth->bind_param( 6,  $analysis->program_version, SQL_VARCHAR );
00407     $sth->bind_param( 7,  $analysis->program_file,    SQL_VARCHAR );
00408     $sth->bind_param( 8,  $analysis->parameters,      SQL_VARCHAR );
00409     $sth->bind_param( 9,  $analysis->module,          SQL_VARCHAR );
00410     $sth->bind_param( 10, $analysis->module_version,  SQL_VARCHAR );
00411     $sth->bind_param( 11, $analysis->gff_source,      SQL_VARCHAR );
00412     $sth->bind_param( 12, $analysis->gff_feature,     SQL_VARCHAR );
00413 
00414     $rows_inserted = $sth->execute();
00415 
00416   } ## end else [ if ( $analysis->created...)]
00417 
00418   my $dbID;
00419   # If we need to fetch the timestamp, or the insert failed due to
00420   # existance of an existing entry, we need to retrieve the entry from
00421   # the database.  Note: $sth->execute() may return 0E0 on error which
00422   # is zero, but true which is why the $rows_inserted clause was added.
00423   if ( !$analysis->created() || !$rows_inserted || $rows_inserted == 0 )
00424   {
00425     my $new_analysis =
00426       $self->fetch_by_logic_name( $analysis->logic_name );
00427 
00428     if ( !$new_analysis ) {
00429       throw("Could not retrieve just stored analysis from database.\n"
00430           . "Possibly incorrect db permissions or missing analysis table\n"
00431       );
00432     }
00433 
00434     $dbID = $new_analysis->dbID();
00435     $analysis->created( $new_analysis->created() );
00436   }
00437   
00438   $dbID ||= $sth->{'mysql_insertid'};
00439   $sth->finish();
00440 
00441   # store description and display_label
00442   if( defined( $analysis->description() ) || defined( $analysis->display_label() )|| defined( $analysis->web_data() )) {
00443       $sth = $self->prepare( "INSERT IGNORE INTO analysis_description (analysis_id, display_label, description, displayable, web_data) VALUES (?,?,?,?, ?)");
00444 
00445       $sth->bind_param(1,$dbID,SQL_INTEGER);
00446       $sth->bind_param(2,$analysis->display_label(),SQL_VARCHAR);
00447       $sth->bind_param(3,$analysis->description,SQL_LONGVARCHAR);
00448       $sth->bind_param(4,$analysis->displayable,SQL_TINYINT);
00449       #$sth->bind_param(5,$analysis->web_data(),SQL_LONGVARCHAR);
00450       my $web_data;
00451       $web_data = $self->dump_data($analysis->web_data()) if ($analysis->web_data());
00452       $sth->bind_param(5,$web_data,SQL_LONGVARCHAR);
00453       $sth->execute();
00454 
00455       $sth->finish();
00456   }
00457   
00458 
00459 
00460   $self->{_cache}->{$dbID} = $analysis;
00461   $self->{_logic_name_cache}{lc($analysis->logic_name)} = $analysis;
00462 
00463   $analysis->adaptor( $self );
00464   $analysis->dbID( $dbID );
00465 
00466   return $dbID;
00467 }
00468 
00469 
00470 
00471 =head2 update
00472 
00473   Arg [1]    : Bio::EnsEMBL::Analysis $anal
00474   Example    : $adaptor->update($anal)
00475   Description: Updates this analysis in the database
00476   Returntype : int 1 if update is performed, undef if it is not
00477   Exceptions : throw if arg is not an analysis object
00478   Caller     : ?
00479   Status     : Stable
00480 
00481 =cut
00482 
00483 sub update {
00484   my $self = shift;
00485   my $a    = shift;
00486 
00487   if (!ref($a) || !$a->isa('Bio::EnsEMBL::Analysis')) {
00488     throw("Expected Bio::EnsEMBL::Analysis argument.");
00489   }
00490 
00491   if(!$a->is_stored($self->db())) {
00492     return undef;
00493   }
00494 
00495   my $sth = $self->prepare
00496     ("UPDATE analysis " .
00497      "SET created = ?, logic_name = ?, db = ?, db_version = ?, db_file = ?, ".
00498      "    program = ?, program_version = ?, program_file = ?,  ".
00499      "    parameters = ?, module = ?, module_version = ?, ".
00500      "    gff_source = ?, gff_feature = ? " .
00501      "WHERE analysis_id = ?");
00502 
00503 
00504 
00505   $sth->bind_param(1,$a->created,SQL_DATETIME);
00506   $sth->bind_param(2,$a->logic_name,SQL_VARCHAR);
00507   $sth->bind_param(3,$a->db,SQL_VARCHAR);
00508   $sth->bind_param(4,$a->db_version,SQL_VARCHAR);
00509   $sth->bind_param(5,$a->db_file,SQL_VARCHAR);
00510   $sth->bind_param(6,$a->program,SQL_VARCHAR);
00511   $sth->bind_param(7,$a->program_version,SQL_VARCHAR);
00512   $sth->bind_param(8,$a->program_file,SQL_VARCHAR);
00513   $sth->bind_param(9,$a->parameters,SQL_VARCHAR);
00514   $sth->bind_param(10,$a->module,SQL_VARCHAR);
00515   $sth->bind_param(11,$a->module_version,SQL_VARCHAR);
00516   $sth->bind_param(12,$a->gff_source,SQL_VARCHAR);
00517   $sth->bind_param(13,$a->gff_feature,SQL_VARCHAR);
00518   $sth->bind_param(14,$a->dbID,SQL_INTEGER);
00519 
00520   $sth->execute();
00521 
00522   $sth->finish();
00523 
00524   # also update description & display label - may need to create these if
00525   # not already there
00526   $sth = $self->prepare("SELECT description FROM analysis_description WHERE analysis_id= ?");
00527   $sth->execute($a->dbID);
00528   my $web_data; #this is an anonymous reference to a hash, will have to be dumped into string before writing to db
00529   if ($sth->fetchrow_hashref) { # update if exists
00530       $web_data = $self->dump_data($a->web_data()) if ($a->web_data());
00531       $sth = $self->prepare
00532       ("UPDATE analysis_description SET description = ?, display_label = ?, displayable = ?, web_data = ? WHERE analysis_id = ?");
00533       $sth->bind_param(1,$a->description,SQL_LONGVARCHAR);     
00534       $sth->bind_param(2,$a->display_label(),SQL_VARCHAR);
00535       $sth->bind_param(3,$a->displayable,SQL_TINYINT);
00536       #      print "after $web_data\n";
00537       $sth->bind_param(4,$web_data,SQL_LONGVARCHAR);
00538       $sth->bind_param(5,$a->dbID,SQL_INTEGER);
00539       $sth->execute();
00540 
00541   } else { # create new entry
00542 
00543     if( $a->description() || $a->display_label() || $a->web_data) {
00544     $web_data = $self->dump_data($a->web_data()) if ($a->web_data());
00545       #my $web_data = $self->dump_data($a->web_data());
00546       $sth = $self->prepare( "INSERT IGNORE INTO analysis_description (analysis_id, display_label, description, displayable, web_data) VALUES (?,?,?,?,?)");
00547     $sth->bind_param(1,$a->dbID,SQL_INTEGER);   
00548     $sth->bind_param(2,$a->display_label(),SQL_VARCHAR);
00549     $sth->bind_param(3,$a->description,SQL_LONGVARCHAR);     
00550     $sth->bind_param(4,$a->displayable,SQL_TINYINT);
00551     #my $web_data = $self->dump_data($a->web_data());
00552     $sth->bind_param(5,$web_data,SQL_LONGVARCHAR);
00553     $sth->execute();
00554 
00555     }
00556 
00557   }
00558 
00559 
00560     $sth->finish();
00561 
00562   # the logic_name cache needs to be re-updated now, since we may have just
00563   # changed the logic_name
00564   $self->fetch_all();
00565 
00566   return 1;
00567 }
00568 
00569 
00570 
00571 =head2 remove
00572 
00573   Arg [1]    : Bio::EnsEMBL::Analysis $anal
00574   Example    : $adaptor->remove($anal)
00575   Description: Removes this analysis from the database.  This is not really
00576                safe to execute in a multi process environment, so programs
00577                should not remove analysis while out on the farm.
00578   Returntype : none
00579   Exceptions : thrown if $anal arg is not an analysis object
00580   Caller     : ?
00581   Status     : Stable
00582 
00583 =cut
00584 
00585 sub remove {
00586   my ($self, $analysis) = @_;
00587 
00588   if (!defined $analysis || !ref $analysis) {
00589     throw("called remove on AnalysisAdaptor with a [$analysis]");
00590   }
00591 
00592   if(!$analysis->is_stored($self->db())) {
00593     return undef;
00594   }
00595 
00596   my $sth = $self->prepare("DELETE FROM analysis WHERE analysis_id = ?");
00597   $sth->bind_param(1,$analysis->dbID,SQL_INTEGER);
00598   $sth->execute();
00599 
00600   $sth = $self->prepare("DELETE FROM analysis_description WHERE analysis_id = ?");
00601   $sth->execute($analysis->dbID());
00602 
00603   # remove this analysis from the cache
00604   delete $self->{'_cache'}->{$analysis->dbID()};
00605   delete $self->{'_logic_name_cache'}->{lc($analysis->logic_name)};
00606 
00607 
00608   # unset the adaptor and dbID
00609   $analysis->dbID(undef);
00610   $analysis->adaptor(undef);
00611 
00612   return;
00613 }
00614 
00615 
00616 
00617 =head2 exists
00618 
00619   Arg [1]    : Bio::EnsEMBL::Analysis $anal
00620   Example    : if($adaptor->exists($anal)) #do something
00621   Description: Tests whether this Analysis already exists in the database
00622                by checking first if the adaptor and dbID are set and
00623                secondly by whether it is in this adaptors internal cache.
00624                Note that this will not actually check the database and will
00625                not find and analysis which were recently added by other
00626                processes.  You are better off simply trying to store an
00627                analysis which will reliably ensure that it is not stored twice
00628                in the database.
00629   Returntype : int dbID if analysis is found, otherwise returns undef
00630   Exceptions : thrown if $anal arg is not an analysis object
00631   Caller     : store
00632   Status     : Stable
00633 
00634 =cut
00635 
00636 sub exists {
00637   my ($self,$anal) = @_;
00638 
00639   if(!ref($anal) || !$anal->isa("Bio::EnsEMBL::Analysis")) {
00640     throw("Object is not a Bio::EnsEMBL::Analysis");
00641   }
00642 
00643   #if this analysis is stored in this db already return its dbID
00644   if($anal->is_stored($self->db())) {
00645     return $anal->dbID();
00646   }
00647 
00648   #this analysis object is not stored but one exactly like it may have been
00649   foreach my $cacheId (keys %{$self->{_cache}}) {
00650     if ($self->{_cache}->{$cacheId}->compare($anal) >= 0) {
00651       # $anal->dbID( $cacheId );
00652       # $anal->adaptor( $self );
00653       return $cacheId;
00654     }
00655   }
00656 
00657   #no analysis like this one exists in the database
00658   return undef;
00659 }
00660 
00661 
00662 =head2 _objFromHashref
00663 
00664   Arg [1]    : hashref $rowHash
00665   Description: Private helper function generates an Analysis object from a 
00666                mysql row hash reference.
00667   Returntype : Bio::EnsEMBL::Analysis
00668   Exceptions : none
00669   Caller     : Bio::EnsEMBL::DBSQL::AnalsisAdaptor::fetch_* methods
00670   Status     : Stable
00671 
00672 =cut
00673 
00674 sub _objFromHashref {
00675   my $self = shift;
00676   my $h = shift;
00677 
00678   my $web_data = $h->{web_data} ? $self->get_dumped_data($h->{web_data}) : '';
00679 
00680   return Bio::EnsEMBL::Analysis->new_fast({
00681     dbID             => $h->{analysis_id},
00682     adaptor          => $self,
00683     _db               => $h->{db},
00684     _db_file          => $h->{db_file},
00685     _db_version       => $h->{db_version},
00686     _program          => $h->{program},
00687     _program_version  => $h->{program_version},
00688     _program_file     => $h->{program_file},
00689     _gff_source       => $h->{gff_source},
00690     _gff_feature      => $h->{gff_feature},
00691     _module           => $h->{module},
00692     _module_version   => $h->{module_version},
00693     _parameters       => $h->{parameters},
00694     _created          => $h->{created},
00695     _logic_name       => $h->{logic_name},
00696     _description      => $h->{description},
00697     _display_label    => $h->{display_label},
00698     _displayable      => $h->{displayable},
00699     _web_data         => $web_data,
00700   });
00701 }
00702 
00703 
00704 
00705 1;