Archive Ensembl HomeArchive Ensembl Home
GenomicAlignAdaptor.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 =head1 NAME
00020 
00021 Bio::EnsEMBL::DBSQL::GenomicAlignAdaptor - Object adaptor to access data in the genomic_align table
00022 
00023 =head1 SYNOPSIS
00024 
00025   Please, consider using the Bio::EnsEMBL::Compara::DBSQL::GenomicAlignBlockAdaptor instead.
00026 
00027 =head2 Get the adaptor from the Registry
00028 
00029   use Bio::EnsEMBL::Registry;
00030 
00031   my $reg = "Bio::EnsEMBL::Registry";
00032   $reg->load_registry_from_db(
00033       -host => "ensembldb.ensembl.org",
00034       -user => "anonymous");
00035 
00036   my $genomic_align_adaptor = $reg->get_adaptor(
00037       "Multi", "compara", "GenomicAlign");
00038 
00039 =head2 Store method
00040 
00041   $genomic_align_adaptor->store($synteny_region);
00042 
00043 =head2 Fetching methods
00044 
00045   my $genomic_align = $genomic_align_adaptor->fetch_by_dbID(1);
00046 
00047   my $genomic_aligns = $genomic_align_adaptor->
00048       fetch_by_GenomicAlignBlock($genomic_align_block);
00049 
00050   my $genomic_aligns = $genomic_align_adaptor->
00051       fetch_by_genomic_align_block_id(1001);
00052 
00053 =head2 Other methods
00054 
00055   $genomic_align_adaptor->delete_by_genomic_align_block_id(1001);
00056 
00057   $genomic_align = $genomic_align_adaptor->
00058       retrieve_all_direct_attributes($genomic_align);
00059 
00060   $genomic_align_adaptor->use_autoincrement(0);
00061 
00062 =head1 DESCRIPTION
00063 
00064 This module is intended to access data in the genomic_align table. In most cases, you want to use
00065 the Bio::EnsEMBL::Compara::DBSQL::GenomicAlignBlockAdaptor instead.
00066 
00067 Each alignment is represented by Bio::EnsEMBL::Compara::GenomicAlignBlock. Each GenomicAlignBlock
00068 contains several Bio::EnsEMBL::Compara::GenomicAlign, one per sequence included in the alignment.
00069 The GenomicAlign contains information about the coordinates of the sequence and the sequence of
00070 gaps, information needed to rebuild the aligned sequence. By combining all the aligned sequences
00071 of the GenomicAlignBlock, it is possible to get the orignal alignment back.
00072 
00073 =head1 INHERITANCE
00074 
00075 This class inherits all the methods and attributes from Bio::EnsEMBL::DBSQL::BaseAdaptor
00076 
00077 =head1 SEE ALSO
00078 
00079  - Bio::EnsEMBL::Registry
00080  - Bio::EnsEMBL::DBSQL::BaseAdaptor
00081  - Bio::EnsEMBL::Compara::DBSQL::GenomicAlignBlockAdaptor
00082  - Bio::EnsEMBL::Compara::GenomicAlignBlock
00083  - Bio::EnsEMBL::Compara::GenomicAlign
00084  - Bio::EnsEMBL::Compara::DnaFrag;
00085 
00086 =head1 APPENDIX
00087 
00088 The rest of the documentation details each of the object methods. Internal methods are usually preceded with a _
00089 
00090 =cut
00091 
00092 
00093 # Let the code begin...
00094 
00095 
00096 package Bio::EnsEMBL::Compara::DBSQL::GenomicAlignAdaptor;
00097 
00098 use vars qw(@ISA);
00099 use strict;
00100 
00101 use Bio::EnsEMBL::DBSQL::BaseAdaptor;
00102 use Bio::EnsEMBL::Compara::GenomicAlign;
00103 use Bio::EnsEMBL::Compara::DnaFrag;
00104 use Bio::EnsEMBL::Utils::Exception;
00105 
00106 @ISA = qw(Bio::EnsEMBL::DBSQL::BaseAdaptor);
00107 
00108 =head2 new
00109 
00110   Arg [1]    : list of args to super class constructor
00111   Example    : $ga_a = new Bio::EnsEMBL::Compara::GenomicAlignAdaptor($dbobj);
00112   Description: Creates a new GenomicAlignAdaptor. This
00113                class should be instantiated through the get method on the 
00114                DBAdaptor rather than calling this method directly.
00115   Returntype : none
00116   Exceptions : none
00117   Caller     : Bio::EnsEMBL::DBSQL::DBConnection
00118   Status     : Stable
00119 
00120 =cut
00121 
00122 sub new {
00123   my $class = shift;
00124 
00125   my $self = $class->SUPER::new(@_);
00126 
00127   $self->{_use_autoincrement} = 1;
00128 
00129   return $self;
00130 }
00131 
00132 
00133 =head2 store
00134 
00135   Arg  1     : listref  Bio::EnsEMBL::Compara::GenomicAlign $ga 
00136                The things you want to store
00137   Example    : none
00138   Description: It stores the given GA in the database. Attached
00139                objects are not stored. Make sure you store them first.
00140   Returntype : none
00141   Exceptions : throw if the linked Bio::EnsEMBL::Compara::DnaFrag object or
00142                the linked Bio::EnsEMBL::Compara::GenomicAlignBlock or
00143                the linked Bio::EnsEMBL::Compara::MethodLinkSpeciesSet
00144                are not in the database
00145   Caller     : $object->methodname
00146   Status     : Stable
00147 
00148 =cut
00149 
00150 sub store {
00151   my ( $self, $genomic_aligns ) = @_;
00152 
00153   my $genomic_align_sql = qq{INSERT INTO genomic_align (
00154           genomic_align_id,
00155           genomic_align_block_id,
00156           method_link_species_set_id,
00157           dnafrag_id,
00158           dnafrag_start,
00159           dnafrag_end,
00160           dnafrag_strand,
00161           cigar_line,
00162           visible,
00163           node_id
00164       ) VALUES (?,?,?, ?,?,?, ?,?,?,?)};
00165   
00166   my $genomic_align_sth = $self->prepare($genomic_align_sql);
00167   
00168   for my $ga ( @$genomic_aligns ) {
00169     if(!defined($ga->dnafrag) or !defined($ga->dnafrag->dbID)) {
00170       throw( "dna_fragment in GenomicAlign is not in DB" );
00171     }
00172     if(!defined($ga->genomic_align_block) or !defined($ga->genomic_align_block->dbID)) {
00173       throw( "genomic_align_block in GenomicAlign is not in DB" );
00174     }
00175     if(!defined($ga->method_link_species_set) or !defined($ga->method_link_species_set->dbID)) {
00176       throw( "method_link_species_set in GenomicAlign is not in DB" );
00177     }
00178 
00179     my $lock_tables = 0;
00180     if (!$ga->dbID and !$self->use_autoincrement()) {
00181       ## Lock tables
00182       $lock_tables = 1;
00183       $self->dbc->do(qq{ LOCK TABLES genomic_align WRITE });
00184 
00185       my $sql =
00186               "SELECT MAX(genomic_align_id) FROM genomic_align WHERE".
00187               " genomic_align_block_id > ".$ga->method_link_species_set->dbID.
00188               "0000000000 AND genomic_align_block_id < ".
00189               ($ga->method_link_species_set->dbID + 1)."0000000000";
00190       my $sth = $self->prepare($sql);
00191       $sth->execute();
00192       my $genomic_align_id = ($sth->fetchrow_array() or
00193           ($ga->method_link_species_set->dbID * 10000000000));
00194       $ga->dbID($genomic_align_id + 1);
00195     }
00196 
00197     $genomic_align_sth->execute(
00198             ($ga->dbID or undef),
00199             $ga->genomic_align_block->dbID,
00200             $ga->method_link_species_set->dbID,
00201             $ga->dnafrag->dbID,
00202             $ga->dnafrag_start,
00203             $ga->dnafrag_end,
00204             $ga->dnafrag_strand,
00205             ($ga->cigar_line or "NULL"),    # FIXME: please check that this "NULL" string in a mediumtext field is what you really want
00206             $ga->visible,
00207         ($ga->node_id or undef)
00208         );
00209 
00210     if ($lock_tables) {
00211       ## Unlock tables
00212       $self->dbc->do("UNLOCK TABLES");
00213     }
00214     if (!$ga->dbID) {
00215       $ga->dbID($genomic_align_sth->{'mysql_insertid'});
00216     }
00217 
00218     info("Stored Bio::EnsEMBL::Compara::GenomicAlign ".
00219           ($ga->dbID or "NULL").
00220           ", gab=".$ga->genomic_align_block->dbID.
00221           ", mlss=".$ga->method_link_species_set->dbID.
00222           ", dnaf=".$ga->dnafrag->dbID.
00223           " [".$ga->dnafrag_start.
00224           "-".$ga->dnafrag_end."]".
00225           " (".$ga->dnafrag_strand.")".
00226           ", cgr=".($ga->cigar_line or "NULL").
00227           ", vis=".$ga->visible);
00228 
00229   }
00230 }
00231 
00232 
00233 =head2 delete_by_genomic_align_block_id
00234 
00235   Arg  1     : integer $genomic_align_block_id
00236   Example    : $gen_ali_blk_adaptor->delete_by_genomic_align_block_id(352158763);
00237   Description: It removes the matching GenomicAlign objects from the database
00238   Returntype : none
00239   Exceptions : none
00240   Caller     : general
00241   Status     : Stable
00242 
00243 =cut
00244 
00245 sub delete_by_genomic_align_block_id {
00246   my ($self, $genomic_align_block_id) = @_;
00247 
00248   my $genomic_align_sql =
00249         qq{DELETE FROM genomic_align WHERE genomic_align_block_id = ?};
00250   
00251   ## Deletes genomic_block entries
00252   my $sth = $self->prepare($genomic_align_sql);
00253   $sth->execute($genomic_align_block_id);
00254 }
00255 
00256 
00257 =head2 fetch_by_dbID
00258 
00259   Arg  1     : integer $dbID
00260   Example    : my $genomic_align = $genomic_align_adaptor->fetch_by_dbID(23134);
00261   Description: Retrieve the corresponding
00262                Bio::EnsEMBL::Compara::GenomicAlign object
00263   Returntype : Bio::EnsEMBL::Compara::GenomicAlign object
00264   Exceptions : Returns undef if no matching entry is found in the database.
00265   Caller     : object::methodname
00266   Status     : Stable
00267 
00268 =cut
00269 
00270 sub fetch_by_dbID {
00271   my ($self, $dbID) = @_;
00272   my $genomic_align;
00273 
00274   my $sql = qq{
00275           SELECT
00276               genomic_align_id,
00277               genomic_align_block_id,
00278               method_link_species_set_id,
00279               dnafrag_id,
00280               dnafrag_start,
00281               dnafrag_end,
00282               dnafrag_strand,
00283               cigar_line,
00284               visible,
00285               node_id
00286           FROM
00287               genomic_align
00288           WHERE
00289               genomic_align_id = ?
00290       };
00291 
00292   my $sth = $self->prepare($sql);
00293   $sth->execute($dbID);
00294   my @values = $sth->fetchrow_array();
00295   $genomic_align = new Bio::EnsEMBL::Compara::GenomicAlign(
00296           -dbID => $values[0],
00297           -adaptor => $self,
00298           -genomic_align_block_id => $values[1],
00299           -method_link_species_set_id => $values[2],
00300           -dnafrag_id => $values[3],
00301           -dnafrag_start => $values[4],
00302           -dnafrag_end => $values[5],
00303           -dnafrag_strand => $values[6],
00304           -cigar_line => $values[7],
00305           -visible => $values[8],
00306       -node_id => $values[9],
00307       );
00308 
00309   return $genomic_align;
00310 }
00311 
00312 
00313 =head2 fetch_all_by_GenomicAlignBlock
00314 
00315   Arg  1     : Bio::EnsEMBL::Compara::GenomicAlignBlock object with a valid dbID
00316   Example    : my $genomic_aligns =
00317                     $genomic_align_adaptor->fetch_all_by_GenomicAlignBlock($genomic_align_block);
00318   Description: Retrieve the corresponding
00319                Bio::EnsEMBL::Compara::GenomicAlign objects
00320   Returntype : ref. to an array of Bio::EnsEMBL::Compara::GenomicAlign objects
00321   Exceptions : Returns a ref. to an empty array if there are no matching entries
00322   Exceptions : Thrown if $genomic_align_block is not a
00323                Bio::EnsEMBL::Compara::GenomicAlignBlock object
00324   Caller     : object::methodname
00325   Status     : Stable
00326 
00327 =cut
00328 
00329 sub fetch_all_by_GenomicAlignBlock {
00330   my ($self, $genomic_align_block) = @_;
00331   my $genomic_aligns = [];
00332 
00333   throw("$genomic_align_block is not a Bio::EnsEMBL::Compara::GenomicAlignBlock object")
00334       if (!$genomic_align_block->isa("Bio::EnsEMBL::Compara::GenomicAlignBlock"));
00335   my $genomic_align_block_id = $genomic_align_block->dbID;
00336 
00337   return $self->fetch_all_by_genomic_align_block_id($genomic_align_block_id);
00338 }
00339 
00340 
00341 =head2 fetch_all_by_genomic_align_block_id
00342 
00343   Arg  1     : integer $genomic_align_block_id
00344   Example    : my $genomic_aligns =
00345                     $genomic_align_adaptor->fetch_all_by_genomic_align_block_id(23134);
00346   Description: Retrieve the corresponding
00347                Bio::EnsEMBL::Compara::GenomicAlign objects
00348   Returntype : ref. to an array of Bio::EnsEMBL::Compara::GenomicAlign objects
00349   Exceptions : Returns a ref. to an empty array if there are no matching entries
00350   Exceptions : Thrown if $genomic_align_block is neither a number 
00351   Caller     : object::methodname
00352   Status     : Stable
00353 
00354 =cut
00355 
00356 sub fetch_all_by_genomic_align_block_id {
00357   my ($self, $incoming_genomic_align_block_id) = @_;
00358   my $genomic_aligns = [];
00359 
00360   my $sql = qq{
00361           SELECT
00362               genomic_align_id,
00363               genomic_align_block_id,
00364               method_link_species_set_id,
00365               dnafrag_id,
00366               dnafrag_start,
00367               dnafrag_end,
00368               dnafrag_strand,
00369               cigar_line,
00370               visible,
00371               node_id
00372           FROM
00373               genomic_align
00374           WHERE
00375               genomic_align_block_id = ?
00376       };
00377 
00378   my $sth = $self->prepare($sql);
00379   $sth->execute($incoming_genomic_align_block_id);
00380   my ($genomic_align_id, $genomic_align_block_id, $method_link_species_set_id,
00381       $dnafrag_id, $dnafrag_start, $dnafrag_end, $dnafrag_strand, $cigar_line,
00382       $visible, $node_id);
00383   $sth->bind_columns(\$genomic_align_id, \$genomic_align_block_id,
00384       \$method_link_species_set_id, \$dnafrag_id, \$dnafrag_start, \$dnafrag_end,
00385       \$dnafrag_strand, \$cigar_line, \$visible, \$node_id);
00386   my $genomic_align_groups = {};
00387   while ($sth->fetchrow_arrayref()) {
00388     my $this_genomic_align = new Bio::EnsEMBL::Compara::GenomicAlign(
00389             -dbID => $genomic_align_id,
00390             -adaptor => $self,
00391             -genomic_align_block_id => $genomic_align_block_id,
00392             -method_link_species_set_id => $method_link_species_set_id,
00393             -dnafrag_id => $dnafrag_id,
00394             -dnafrag_start => $dnafrag_start,
00395             -dnafrag_end => $dnafrag_end,
00396             -dnafrag_strand => $dnafrag_strand,
00397             -cigar_line => $cigar_line,
00398             -visible => $visible,
00399         -node_id => $node_id
00400         );
00401     push(@$genomic_aligns, $this_genomic_align);
00402   }
00403 
00404   return $genomic_aligns;
00405 }
00406 
00407 =head2 fetch_all_by_node_id
00408 
00409   Arg  1     : integer $node_id
00410   Example    : my $genomic_aligns =
00411                     $genomic_align_adaptor->fetch_all_by_node_id(5530002705680);
00412   Description: Retrieve the corresponding
00413                Bio::EnsEMBL::Compara::GenomicAlign objects
00414   Returntype : ref. to an array of Bio::EnsEMBL::Compara::GenomicAlign objects
00415   Exceptions : Returns a ref. to an empty array if there are no matching entries
00416   Exceptions : Thrown if $node_id is not a number 
00417   Caller     : object::methodname
00418   Status     : At risk
00419 
00420 =cut
00421 
00422 sub fetch_all_by_node_id {
00423   my ($self, $incoming_node_id) = @_;
00424   my $genomic_aligns = [];
00425 
00426   my $sql = qq{
00427           SELECT
00428               genomic_align_id,
00429               genomic_align_block_id,
00430               method_link_species_set_id,
00431               dnafrag_id,
00432               dnafrag_start,
00433               dnafrag_end,
00434               dnafrag_strand,
00435               cigar_line,
00436               level_id,
00437               node_id
00438           FROM
00439               genomic_align
00440           WHERE
00441               node_id = ?
00442       };
00443 
00444   my $sth = $self->prepare($sql);
00445   $sth->execute($incoming_node_id);
00446   my ($genomic_align_id, $genomic_align_block_id, $method_link_species_set_id,
00447       $dnafrag_id, $dnafrag_start, $dnafrag_end, $dnafrag_strand, $cigar_line,
00448       $level_id, $node_id);
00449   $sth->bind_columns(\$genomic_align_id, \$genomic_align_block_id,
00450       \$method_link_species_set_id, \$dnafrag_id, \$dnafrag_start, \$dnafrag_end,
00451       \$dnafrag_strand, \$cigar_line, \$level_id, \$node_id);
00452   while ($sth->fetchrow_arrayref()) {
00453     my $this_genomic_align = new Bio::EnsEMBL::Compara::GenomicAlign(
00454             -dbID => $genomic_align_id,
00455             -adaptor => $self,
00456             -genomic_align_block_id => $genomic_align_block_id,
00457             -method_link_species_set_id => $method_link_species_set_id,
00458             -dnafrag_id => $dnafrag_id,
00459             -dnafrag_start => $dnafrag_start,
00460             -dnafrag_end => $dnafrag_end,
00461             -dnafrag_strand => $dnafrag_strand,
00462             -cigar_line => $cigar_line,
00463             -level_id => $level_id,
00464         -node_id => $node_id
00465         );
00466     push(@$genomic_aligns, $this_genomic_align);
00467   }
00468 
00469   return $genomic_aligns;
00470 }
00471 
00472 =head2 retrieve_all_direct_attributes
00473 
00474   Arg  1     : Bio::EnsEMBL::Compara::GenomicAlign $genomic_align
00475   Example    : $genomic_align_adaptor->retrieve_all_direct_attributes($genomic_align)
00476   Description: Retrieve the all the direct attibutes corresponding to the dbID of the
00477                Bio::EnsEMBL::Compara::GenomicAlign object. It is used after lazy fetching
00478                of the object for populating it when required.
00479   Returntype : Bio::EnsEMBL::Compara::GenomicAlign object
00480   Exceptions : 
00481   Caller     : none
00482   Status     : Stable
00483 
00484 =cut
00485 
00486 sub retrieve_all_direct_attributes {
00487   my ($self, $genomic_align) = @_;
00488 
00489   my $sql = qq{
00490                 SELECT
00491                     genomic_align_block_id,
00492                     method_link_species_set_id,
00493                     dnafrag_id,
00494                     dnafrag_start,
00495                     dnafrag_end,
00496                     dnafrag_strand,
00497                     cigar_line,
00498                     visible,
00499                     node_id
00500                 FROM
00501                     genomic_align
00502                 WHERE
00503                     genomic_align_id = ?
00504         };
00505 
00506   my $sth = $self->prepare($sql);
00507   $sth->execute($genomic_align->dbID);
00508   my ($genomic_align_block_id, $method_link_species_set_id, $dnafrag_id, $dnafrag_start, $dnafrag_end,
00509           $dnfrag_strand, $cigar_line, $visible, $node_id) = $sth->fetchrow_array();
00510   
00511   ## Populate the object
00512   $genomic_align->adaptor($self);
00513   $genomic_align->genomic_align_block_id($genomic_align_block_id) if (defined($genomic_align_block_id));
00514   $genomic_align->method_link_species_set_id($method_link_species_set_id) if (defined($method_link_species_set_id));
00515   $genomic_align->dnafrag_id($dnafrag_id) if (defined($dnafrag_id));
00516   $genomic_align->dnafrag_start($dnafrag_start) if (defined($dnafrag_start));
00517   $genomic_align->dnafrag_end($dnafrag_end) if (defined($dnafrag_end));
00518   $genomic_align->dnafrag_strand($dnfrag_strand) if (defined($dnfrag_strand));
00519   $genomic_align->cigar_line($cigar_line) if (defined($cigar_line));
00520   $genomic_align->visible($visible) if (defined($visible));
00521   $genomic_align->node_id($node_id) if (defined($node_id));
00522 
00523   return $genomic_align;
00524 }
00525 
00526 
00527 =head2 use_autoincrement
00528 
00529   [Arg  1]   : (optional)int value
00530   Example    : $genomic_align_adaptor->use_autoincrement(0);
00531   Description: Getter/setter for the _use_autoincrement flag. This flag
00532                is used when storing new objects with no dbID in the
00533                database. If the flag is ON (default), the adaptor will
00534                let the DB set the dbID using the AUTO_INCREMENT ability.
00535                If you unset the flag, then the adaptor will look for the
00536                first available dbID after 10^10 times the
00537                method_link_species_set_id.
00538   Returntype : integer
00539   Exceptions : 
00540   Caller     : none
00541   Status     : Stable
00542 
00543 =cut
00544 
00545 sub use_autoincrement {
00546   my ($self, $value) = @_;
00547 
00548   if (defined $value) {
00549     $self->{_use_autoincrement} = $value;
00550   }
00551 
00552   return $self->{_use_autoincrement};
00553 }
00554 
00555 1;