Archive Ensembl HomeArchive Ensembl Home
UnmappedObjectAdaptor.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::UnmappedObjectAdaptor
00024 
00025 =head1 SYNOPSIS
00026 
00027   my $uoa = $database_adaptor->get_UnmappedObjectAdaptor();
00028 
00029   my $missed = @{ $uoa->fetch_all_by_type('xref') };
00030 
00031 =head1 DESCRIPTION
00032 
00033 Unmapped ObjectAdaptor - An adaptor responsible for the creation,
00034 editing, retrieval of Unmapped Objects. These being the Objects that
00035 where not mapped in a specific process i.e. xref, cDNA, Markers.
00036 
00037 =head1 METHODS
00038 
00039 =cut
00040 
00041 package Bio::EnsEMBL::DBSQL::UnmappedObjectAdaptor;
00042 use vars qw(@ISA);
00043 use strict;
00044 
00045 
00046 use POSIX;
00047 use Bio::EnsEMBL::Utils::Cache;
00048 use Bio::EnsEMBL::Utils::Exception qw(throw warning);
00049 use Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor;
00050 use Bio::EnsEMBL::UnmappedObject;
00051 use Bio::EnsEMBL::Analysis;
00052 @ISA = qw(Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor);
00053 
00054 our %desc_to_id;
00055 
00056 =head2 new
00057 
00058   Arg [1]    : list of args @args
00059                Superclass constructor arguments
00060   Example    : none
00061   Description: Constructor which just initializes internal cache structures
00062   Returntype : Bio::EnsEMBL::DBSQL::UnmappedObjectAdaptor
00063   Exceptions : none
00064   Caller     : implementing subclass constructors
00065   Status     : At Risk
00066 
00067 =cut
00068 
00069 sub new {
00070   my $proto = shift;
00071 
00072   my $class = ref($proto) || $proto;
00073 
00074   my $self = $class->SUPER::new(@_);
00075 
00076   my $sth =
00077     $self->prepare(   "SELECT unmapped_reason_id, full_description "
00078                     . "FROM unmapped_reason" );
00079 
00080   $sth->execute();
00081 
00082   my ( $id, $desc );
00083   $sth->bind_columns( \( $id, $desc ) );
00084 
00085   while ( $sth->fetch() ) {
00086     $desc_to_id{$desc} = $id;
00087   }
00088 
00089   $sth->finish();
00090 
00091   return $self;
00092 }
00093 
00094 
00095 # _tables
00096 #  Arg [1]    : none
00097 #  Description: PROTECTED implementation of superclass abstract method
00098 #               returns the names, aliases of the tables to use for queries
00099 #  Returntype : list of listrefs of strings
00100 #  Exceptions : none
00101 #  Caller     : internal
00102 #  Status     : At Risk
00103 sub _tables {
00104   my $self = shift;
00105 
00106   return (['unmapped_object', 'uo'],
00107       ['unmapped_reason', 'ur']);
00108 }
00109 
00110 
00111 # _columns
00112 #  Arg [1]    : none
00113 #  Example    : none
00114 #  Description: PROTECTED implementation of superclass abstract method
00115 #               returns a list of columns to use for queries
00116 #  Returntype : list of strings
00117 #  Exceptions : none
00118 #  Caller     : internal
00119 #  Status     : At Risk
00120 
00121 sub _columns {
00122   my $self = shift;
00123 
00124   return qw(uo.unmapped_object_id uo.type uo.analysis_id uo.external_db_id
00125             uo.identifier uo.unmapped_reason_id uo.query_score uo.target_score 
00126         uo.ensembl_id uo.ensembl_object_type 
00127         ur.summary_description ur.full_description);
00128 }
00129 
00130 sub _left_join {
00131   return ( [
00132       'unmapped_object', "uo.unmapped_reason_id = ur.unmapped_reason_id"
00133     ] );
00134 }
00135 
00136 =head2 list_dbIDs
00137 
00138   Arg [1]    : none
00139   Example    : @unmapped_object_ids = @{$unmapped_object_adaptor->list_dbIDs()};
00140   Description: Gets an array of internal ids for all unmapped_objects in the current db
00141   Returntype : list of ints
00142   Exceptions : none
00143   Caller     : ?
00144   Status     : Stable
00145 
00146 =cut
00147 
00148 sub list_dbIDs {
00149    my ($self) = @_;
00150 
00151    return $self->_list_dbIDs("unmapped_object");
00152 }
00153 
00154 =head2 list_unmapped_reasons
00155 
00156   Arg [1]    : none
00157   Example    : @unmapped_object_reason+ids = 
00158                    @{$unmapped_object_adaptor->list_unmapped_reasons()};
00159   Description: Gets an array of internal ids for all unmapped_objects in the current db
00160   Returntype : list of ints
00161   Exceptions : none
00162   Caller     : ?
00163   Status     : Stable
00164 
00165 =cut
00166 
00167 sub list_unmapped_reasons {
00168    my ($self) = @_;
00169 
00170    return $self->_list_dbIDs("unmapped_reason");
00171 }
00172 
00173 
00174 # _objs_from_sth
00175 
00176 #  Arg [1]    : StatementHandle $sth
00177 #  Example    : none
00178 #  Description: PROTECTED implementation of abstract superclass method.
00179 #               responsible for the creation of UnmappedObjects
00180 #  Returntype : listref of Bio::EnsEMBL::UnmappedObjects
00181 #  Exceptions : none
00182 #  Caller     : internal
00183 #  Status     : At Risk
00184 
00185 sub _objs_from_sth {
00186   my ($self, $sth) = @_;
00187 
00188   my($unmapped_object_id, $type, $analysis_id, $external_db_id, $identifier,
00189      $unmapped_reason_id, $query_score, $target_score, $ensembl_id, 
00190      $ensembl_object_type, $summary, $full_desc);
00191 
00192   $sth->bind_columns(\$unmapped_object_id,\$type, \$analysis_id, 
00193              \$external_db_id, \$identifier, \$unmapped_reason_id, 
00194              \$query_score, \$target_score, \$ensembl_id, 
00195              \$ensembl_object_type, \$summary, \$full_desc);
00196 
00197   my $analysis_adaptor = $self->db->get_AnalysisAdaptor();
00198 
00199   my @features;
00200   while($sth->fetch()) {
00201     my $analysis = $analysis_adaptor->fetch_by_dbID($analysis_id);
00202     
00203     #print "$identifier\n";
00204 
00205     push( @features,
00206           $self->_create_feature(
00207                          'Bio::EnsEMBL::UnmappedObject', {
00208                            -unmapped_object_id  => $unmapped_object_id,
00209                            -unmapped_reason_id  => $unmapped_reason_id,
00210                            -type                => $type,
00211                            -analysis            => $analysis,
00212                            -external_db_id      => $external_db_id,
00213                            -identifier          => $identifier,
00214                            -query_score         => $query_score,
00215                            -target_score        => $target_score,
00216                            -ensembl_id          => $ensembl_id,
00217                            -ensembl_object_type => $ensembl_object_type,
00218                            -summary             => $summary,
00219                            -full_desc           => $full_desc,
00220                            -adaptor             => $self
00221                          } ) );
00222 
00223   }
00224   return \@features;
00225 }
00226 
00227 
00228 
00229 =head2 store
00230 
00231   Arg [1]    : list of Bio::EnsEMBL::UnmappedObjects @uo
00232                the unmapped objects to store in the database
00233   Example    : $ou_adaptor->store(@uo);
00234   Description: Stores a list of unmapped objects in the database
00235   Returntype : none
00236   Exceptions : thrown if no Analysis, or no list of objects to store. 
00237   Caller     : general
00238   Status     : Stable
00239 
00240 =cut
00241 
00242 sub store{
00243   my ($self,@uos) = @_;
00244 
00245   if( scalar(@uos) == 0 ) {
00246     throw("Must call store with list of UnmappedObjects");
00247   }
00248 
00249 
00250   my $db = $self->db();
00251   my $analysis_adaptor = $db->get_AnalysisAdaptor();
00252 
00253   my $sth_reason = $self->prepare
00254     ("INSERT INTO unmapped_reason (summary_description, full_description)".
00255      " VALUES (?,?)");
00256 
00257   my $sth_unmapped_object = $self->prepare
00258     ("INSERT INTO unmapped_object (type, analysis_id, external_db_id,
00259               identifier, unmapped_reason_id, query_score, target_score,
00260               ensembl_id, ensembl_object_type)".
00261         " VALUES (?,?,?,?,?,?,?,?,?)");
00262 
00263  FEATURE: foreach my $uo ( @uos ) {
00264 
00265     if( !ref $uo || !$uo->isa("Bio::EnsEMBL::UnmappedObject") ) {
00266       throw("UnmappedObject must be an Ensembl UnmappedObject, " .
00267             "not a [".ref($uo)."]");
00268     }
00269     if($uo->is_stored($db)){
00270       next;
00271     } 
00272     
00273     my $analysis = $uo->analysis();
00274     throw("UnmappedObject must have an analysis object.".$uo->analysis."\n") if(!defined($analysis));
00275 
00276     my $analysis_id;
00277     if($analysis->is_stored($db)) {
00278       $analysis_id = $analysis->dbID();
00279     } else {
00280       $analysis_id = $db->get_AnalysisAdaptor->store($analysis);
00281     }
00282 
00283     #First check to see unmapped reason is stored
00284     if(!defined($desc_to_id{$uo->{'description'}})){
00285       $sth_reason->bind_param(1,$uo->{'summary'},SQL_VARCHAR);
00286       $sth_reason->bind_param(2,$uo->{'description'},SQL_VARCHAR);
00287       $sth_reason->execute();
00288       $uo->{'unmapped_reason_id'} = $desc_to_id{$uo->{'description'}} 
00289     = $sth_reason->{'mysql_insertid'};
00290       
00291     }
00292     else{
00293       $uo->{'unmapped_reason_id'} = $desc_to_id{$uo->{'description'}} ;
00294     }
00295     $sth_unmapped_object->bind_param(1,$uo->{'type'},SQL_VARCHAR);
00296     $sth_unmapped_object->bind_param(2,$uo->analysis->dbID,SQL_INTEGER);
00297     $sth_unmapped_object->bind_param(3,$uo->{'external_db_id'},SQL_INTEGER);
00298     $sth_unmapped_object->bind_param(4,$uo->{'identifier'},SQL_VARCHAR);
00299     $sth_unmapped_object->bind_param(5,$uo->{'unmapped_reason_id'},SQL_VARCHAR);
00300     $sth_unmapped_object->bind_param(6,$uo->{'query_score'},SQL_DOUBLE);
00301     $sth_unmapped_object->bind_param(7,$uo->{'target_score'},SQL_DOUBLE);
00302     $sth_unmapped_object->bind_param(8,$uo->{'ensembl_id'},SQL_INTEGER);
00303     $sth_unmapped_object->bind_param(9,$uo->{'ensembl_object_type'},SQL_VARCHAR);
00304     $sth_unmapped_object->execute();
00305     $uo->dbID($sth_unmapped_object->{'mysql_insertid'});
00306   }
00307   $sth_reason->finish();      
00308   return;
00309 }
00310 
00311 
00312 =head2 fetch_all_by_type
00313 
00314   Arg [1]  : string type. The type of unmapped objects
00315   Example  : @unmapped_object = @{$uoa->fetch_all_by_type('xref')};
00316   Description : Retrieves all the unmapped object for a particular
00317                 type. e.g. 'xref','cDNA', 'marker'
00318   Returntype  : Array ref of Bio::EnsEMBL::UnmappedObject
00319   Exceptions  : none
00320   Caller      : general
00321   Status      : At Risk
00322 
00323 =cut
00324 
00325 sub fetch_all_by_type {
00326   my ($self, $type) = @_;
00327   
00328   unless($type) {
00329     throw("type argument is required");
00330   }
00331   $self->bind_param_generic_fetch($type,SQL_VARCHAR);
00332   $self->generic_fetch("uo.type = ?");
00333   
00334 }
00335 
00336 =head2 fetch_all_by_analysis
00337 
00338   Arg [1]  : Bio:EnsEMBL::Analysis object
00339   Arg [2]  : (optional) string database name
00340   Example  : @unmapped_object = @{$uoa->fetch_all_by_analysis($analysis)};
00341   Description : Retrieves all the unmapped object for a particular
00342                 analysis type with the the option of a particular
00343                 database type.
00344   Returntype  : array ref of Bio::EnsEMBL::UnmappedObject
00345   Exceptions  : thorws if first argument is not an anaylisi object
00346   Caller      : general
00347   Status      : At Risk
00348 
00349 =cut
00350 
00351 sub fetch_all_by_analysis {
00352   my ($self, $analysis,$dbname) = @_;
00353   
00354   unless($analysis) {
00355     throw("analysis argument is required");
00356   }
00357   $self->bind_param_generic_fetch($analysis->dbID,SQL_INTEGER);
00358   my $constraint = "uo.analysis_id = ?";
00359   if(defined($dbname)){
00360     my $db_id =0;
00361     my $sth = $self->prepare('select external_db_id from external_db where db_name like "'.
00362                   $dbname.'"');
00363     $sth->execute;
00364     $sth->bind_columns(\$db_id);
00365     $sth->fetch();
00366     if(!defined($db_id) or $db_id == 0){
00367       throw("$dbname could not be found in the external database table\n");
00368     }
00369     $self->bind_param_generic_fetch($db_id,SQL_INTEGER);
00370     $constraint .= " AND uo.external_db_id = ?";
00371   }
00372   $self->generic_fetch($constraint);
00373   
00374 }
00375 
00376 =head2 fetch_by_identifier
00377 
00378   Arg [1]  : string type. The type of unmapped objects
00379   Arg [2]  : (optional) string database name
00380   Example  : @unmapped_object = @{$uoa->fetch_by_identifier('Q123345')};
00381   Description : Retrieves the unmapped object for a particular
00382                 identifier/accession
00383   Returntype  : array ref of Bio::EnsEMBL::UnmappedObject
00384   Exceptions  : none
00385   Caller      : general
00386   Status      : At Risk
00387 
00388 =cut
00389 
00390 sub fetch_by_identifier {
00391   my ($self, $identifier, $dbname) = @_;
00392   
00393   unless($identifier) {
00394     throw("identifier argument is required");
00395   }
00396   $self->bind_param_generic_fetch($identifier,SQL_VARCHAR);
00397   my $constraint = 'uo.identifier like ?';
00398 
00399   if(defined($dbname)){
00400     my $db_id =0;
00401     my $sth = $self->prepare('select external_db_id from external_db where db_name like "'.
00402                   $dbname.'"');
00403     $sth->execute;
00404     $sth->bind_columns(\$db_id);
00405     $sth->fetch();
00406     if(!defined($db_id) or $db_id == 0){
00407       throw("$dbname could not be found in the external database table\n");
00408     }
00409     $self->bind_param_generic_fetch($db_id,SQL_INTEGER);
00410     $constraint .= " AND uo.external_db_id = ?";
00411   }
00412   return $self->generic_fetch($constraint);
00413 }
00414 
00415 =head2 fetch_all_by_object_type_id
00416 
00417   Arg [1]  : string - The object type of the ensembl object e.g. Gene
00418   Arg [2]  : int    - The internal dbID of the ensembl object
00419   Example  : my @unmapped_objects = @{$uoa->fetch_all_by_object_type_id('Gene', 12341)};
00420   Description : Retrieves the unmapped objects for a particular ensembl object
00421                 This is a base method which should be called by wrapper methods
00422                 defining the correct object type e.g. $uoa->fetch_all_by_Gene($gene)
00423   Returntype  : array ref of Bio::EnsEMBL::UnmappedObject objects
00424   Exceptions  : Throws if arguments are not defined
00425   Caller      : general
00426   Status      : At Risk
00427 
00428 =cut
00429 
00430 sub fetch_all_by_object_type_id {
00431   my ($self, $object_type, $dbid) = @_;
00432   
00433   if(! ($object_type && $dbid)){
00434     throw("object_type and dbid arguments required");
00435   }
00436   
00437   $self->bind_param_generic_fetch($object_type, SQL_VARCHAR);
00438   $self->bind_param_generic_fetch($dbid,        SQL_INTEGER);
00439 
00440   my $constraint = 'uo.ensembl_object_type=? and uo.ensembl_id=?';
00441 
00442   return $self->generic_fetch($constraint);
00443 }
00444 
00445 
00446 
00447 1;