Archive Ensembl HomeArchive Ensembl Home
GenomicAlignGroupAdaptor.pm
Go to the documentation of this file.
00001 #
00002 # Ensembl module for Bio::EnsEMBL::Compara::DBSQL::GenomicAlignGroupAdaptor
00003 #
00004 # Copyright Javier Herrero
00005 #
00006 # You may distribute this module under the same terms as perl itself
00007 # 
00008 # POD documentation - main docs before the code
00009 # 
00010 
00011 =head1 NAME
00012 
00013 Bio::EnsEMBL::Compara::DBSQL::GenomicAlignGroupAdaptor - Object to access data in genomic_align_group table
00014 
00015 =head1 SYNOPSIS
00016 
00017   use Bio::EnsEMBL::Compara::DBSQL::DBAdaptor; 
00018   my $db = new Bio::EnsEMBL::Compara::DBSQL::DBAdaptor (
00019       -host => $host,
00020       -user => $dbuser,
00021       -pass => $dbpass,
00022       -port => $port,
00023       -dbname => $dbname,
00024       -conf_file => $conf_file);
00025   
00026   my $genomic_align_group_adaptor = $db->get_GenomicAlignGroupAdaptor();
00027 
00028   $genomic_align_group_adaptor->store($genomic_align_group);
00029 
00030   $genomic_align_groups = $genomic_align_group_adaptor->fetch_all_by_GenomicAlign($genomic_align);
00031   $genomic_align_groups = $genomic_align_group_adaptor->fetch_all_by_genomic_align_id(11223);
00032 
00033   $genomic_align_group = $genomic_align_group_adaptor->fetch_by_GenomicAlign($genomic_align);
00034   $genomic_align_group = $genomic_align_group_adaptor->fetch_by_genomic_align_id(11223);
00035 
00036 =head1 DESCRIPTION
00037 
00038 This class is intended to access data in genomic_align_group table
00039 
00040 =head1 AUTHOR
00041 
00042 Javier Herrero (jherrero@ebi.ac.uk)
00043 
00044 This modules is part of the Ensembl project http://www.ensembl.org
00045 
00046 =head1 APPENDIX
00047 
00048 The rest of the documentation details each of the object methods. Internal methods are usually preceded with a _
00049 
00050 =cut
00051 
00052 
00053 # Let the code begin...
00054 
00055 
00056 package Bio::EnsEMBL::Compara::DBSQL::GenomicAlignGroupAdaptor;
00057 use vars qw(@ISA);
00058 use strict;
00059 
00060 use Bio::EnsEMBL::DBSQL::BaseAdaptor;
00061 use Bio::EnsEMBL::Utils::Exception;
00062 use Bio::EnsEMBL::Compara::GenomicAlignGroup;
00063 use Bio::EnsEMBL::Compara::GenomicAlign;
00064 use Bio::EnsEMBL::Compara::DnaFrag;
00065 
00066 @ISA = qw(Bio::EnsEMBL::DBSQL::BaseAdaptor);
00067 
00068 =head2 new
00069 
00070   Arg [1]    : list of args to super class constructor
00071   Example    : $gag_a = new Bio::EnsEMBL::Compara::GenomicAlignGroupAdaptor($dbobj);
00072   Description: Creates a new GenomicAlignGroupAdaptor. This
00073                class should be instantiated through the get method on the 
00074                DBAdaptor rather than calling this method directly.
00075   Returntype : none
00076   Exceptions : none
00077   Caller     : Bio::EnsEMBL::DBSQL::DBConnection
00078   Status     : Stable
00079 
00080 =cut
00081 
00082 sub new {
00083   my $class = shift;
00084 
00085   my $self = $class->SUPER::new(@_);
00086 
00087   $self->{_use_autoincrement} = 1;
00088 
00089   return $self;
00090 }
00091 
00092 
00093 =head2 store
00094 
00095   Arg  1     : Bio::EnsEMBL::Compara::GenomicAlignGroup
00096                The things you want to store
00097   Example    : $gen_ali_grp_adaptor->store($genomic_align_group);
00098   Description: It stores the given GenomicAlginGroup in the database
00099   Returntype : Bio::EnsEMBL::Compara::GenomicAlignGroup object
00100   Exceptions : not stored linked Bio::EnsEMBL::Compara::GenomicAlign objects throw.
00101   Caller     : general
00102   Status     : Stable
00103 
00104 =cut
00105 
00106 sub store {
00107   my ($self, $genomic_align_group) = @_;
00108 
00109   my $genomic_align_group_sql =
00110         qq{INSERT INTO genomic_align_group (
00111                 node_id,
00112                 genomic_align_id
00113         ) VALUES (?,?)};
00114   
00115   my @values;
00116   
00117   ## CHECKING
00118   my $all_genomic_aligns = $genomic_align_group->get_all_GenomicAligns;
00119   foreach my $genomic_align (@$all_genomic_aligns) {
00120     # check if every GenomicAlgin has a dbID
00121     if (!defined($genomic_align->dbID)) {
00122       throw("GenomicAlign [$genomic_align] in GenomicAlignGroup is not in DB");
00123     }
00124   }
00125   
00126   my $node_id = $genomic_align_group->dbID;
00127   if (!$node_id) {
00128     my $method_link_species_set_id;
00129     # Get common method_link_species_set_id
00130     foreach my $genomic_align (@$all_genomic_aligns) {
00131       if (!$genomic_align->method_link_species_set_id()) {
00132         ## undef value and exit loop if method_link_species_set_id does not match
00133         $method_link_species_set_id = undef;
00134         last;
00135       } elsif (!$method_link_species_set_id) {
00136         $method_link_species_set_id = $genomic_align->method_link_species_set_id();
00137       } elsif ($method_link_species_set_id != $genomic_align->method_link_species_set_id()) {
00138         ## undef value and exit loop if method_link_species_set_id does not match
00139         $method_link_species_set_id = undef;
00140         last;
00141       }
00142     }
00143     if ($method_link_species_set_id && !$self->use_autoincrement()) {
00144       ## Only if method_link_species_set_id is the same for all the GenomicAligns
00145       my $sql = 
00146               "SELECT MAX(node_id) FROM genomic_align_group WHERE".
00147               " node_id > ".$method_link_species_set_id.
00148               "0000000000 AND node_id < ".
00149               ($method_link_species_set_id + 1)."0000000000";
00150       my $sth = $self->prepare($sql);
00151       $sth->execute();
00152       $node_id = $sth->fetchrow_array();
00153       if (defined $node_id) {
00154         $node_id++;
00155       } else {
00156         $node_id = $method_link_species_set_id * 10000000000 + 1;
00157       }
00158     }
00159   }
00160 
00161   ## Stores data, all of them with the same id
00162   my $sth = $self->prepare($genomic_align_group_sql);
00163   for (my $i = 0; $i < @$all_genomic_aligns; $i++) {
00164     my $genomic_align  = $all_genomic_aligns->[$i];
00165     $sth->execute(
00166                   ($node_id or "NULL"),
00167                   $genomic_align->dbID
00168           );
00169     if (!$node_id) {$node_id = $sth->{'mysql_insertid'};}
00170 
00171     info("Stored Bio::EnsEMBL::Compara::GenomicAlignGroup ".
00172           "(".($i+1)."/".scalar(@$all_genomic_aligns).") ".
00173           ($node_id or "NULL").", ".$genomic_align->dbID, );
00174 
00175   }
00176   $genomic_align_group->dbID($node_id);
00177   
00178   return $genomic_align_group;
00179 }
00180 
00181 
00182 =head2 fetch_by_dbID
00183 
00184   Arg  1     : integer node_id
00185   Example    : my $genomic_align_group =
00186                   $genomic_align_group_adaptor->fetch_by_dbID(12413)
00187   Description: Returns a Bio::EnsEMBL::Compara::GenomicAlignGroup corresponding
00188                to the given node_id.
00189   Returntype : Bio::EnsEMBL::Compara::GenomicAlignGroup
00190   Exceptions : none
00191   Caller     : object::methodname
00192   Status     : Stable
00193 
00194 =cut
00195 
00196 sub fetch_by_dbID {
00197   my ($self, $node_id) = @_;
00198   my $genomic_align_group;
00199 
00200   my $genomic_align_adaptor = $self->db->get_GenomicAlignAdaptor;
00201   
00202   my $genomic_align_group_sql = qq{
00203               SELECT
00204                   node_id,
00205                   genomic_align_id
00206               FROM
00207                 genomic_align_group
00208               WHERE
00209                 node_id = ?
00210         };
00211   
00212   my @values;
00213 
00214   my $sth = $self->prepare($genomic_align_group_sql);
00215   $sth->execute($node_id);
00216 
00217   # Group results in order to be able to build Bio::EnsEMBL::Compara::GenomicAlignGroupAdaptor objects
00218   my $group;
00219   while (my $values = $sth->fetchrow_arrayref) {
00220     my ($node_id, $genomic_align_id) = @$values;
00221 
00222     $group->{'node_id'} = $node_id;
00223     my $this_genomic_align = new Bio::EnsEMBL::Compara::GenomicAlign(
00224             -dbID => $genomic_align_id,
00225             -adaptor => $genomic_align_adaptor
00226         );
00227 
00228     push(@{$group->{'genomic_align_array'}}, $this_genomic_align);
00229   }
00230 
00231   #return undef if no genomic_align_groups have been found
00232   if (!defined $group->{'node_id'}) {
00233       return $genomic_align_group;
00234   }
00235 
00236   $genomic_align_group = new Bio::EnsEMBL::Compara::GenomicAlignGroup(
00237           -dbID => $group->{'node_id'},
00238           -adaptor => $self,
00239           -genomic_align_array => $group->{'genomic_align_array'}
00240       );
00241   foreach my $this_genomic_align (@{$genomic_align_group->genomic_align_array}) {
00242     $this_genomic_align->genomic_align_group($genomic_align_group);
00243   }
00244 
00245   return $genomic_align_group;
00246 }
00247 
00248 =head2 fetch_by_GenomicAlign
00249 
00250   Arg  1     : Bio::EnsEMBL::Compara::GenomicAlign $genomic_align
00251   Example    : my $genomic_align_group =
00252                   $genomic_align_group_adaptor->fetch_by_GenomicAlign(
00253                           $genomic_align)
00254   Description: Returns a Bio::EnsEMBL::Compara::GenomicAlignGroup corresponding
00255                to the given Bio::EnsEMBL::Compara::GenomicAlign and group_type.
00256   Returntype : Bio::EnsEMBL::Compara::GenomicAlignGroup
00257   Exceptions : none
00258   Caller     : object::methodname
00259   Status     : Stable
00260 
00261 =cut
00262 
00263 sub fetch_by_GenomicAlign {
00264   my ($self, $genomic_align) = @_;
00265   my $genomic_align_group;
00266 
00267   my $genomic_align_adaptor = $self->db->get_GenomicAlignAdaptor;
00268   
00269   # Check Bio::EnsEMBL::Compara::GenomicAlign object
00270   unless($genomic_align && ref $genomic_align && 
00271         $genomic_align->isa('Bio::EnsEMBL::Compara::GenomicAlign')) {
00272     throw("[$genomic_align] must be a Bio::EnsEMBL::Compara::GenomicAlign object");
00273   }
00274 
00275   my $genomic_align_block_sql = qq{
00276               SELECT
00277                   b.node_id,
00278                   b.genomic_align_id
00279               FROM
00280                 genomic_align_group a, genomic_align_group b
00281               WHERE
00282                 a.node_id = b.node_id
00283                 AND a.genomic_align_id = ?
00284         };
00285   
00286   my @values;
00287   
00288   my $sth = $self->prepare($genomic_align_block_sql);
00289   $sth->execute($genomic_align->dbID);
00290 
00291   # Group results in order to be able to build Bio::EnsEMBL::Compara::GenomicAlignGroupAdaptor objects
00292   my $group;
00293   while (my $values = $sth->fetchrow_arrayref) {
00294     my ($node_id, $genomic_align_id) = @$values;
00295 
00296     $group->{'node_id'} = $node_id;
00297     my $this_genomic_align;
00298     if ($genomic_align_id == $genomic_align->dbID) {
00299       # Use Bio::EnsEMBL::Compara::GenomicAlign object given by argument if possible
00300       $this_genomic_align = $genomic_align;
00301     } else {
00302       # Create a new Bio::EnsEMBL::Compara::GenomicAlign object otherwise
00303       $this_genomic_align = new Bio::EnsEMBL::Compara::GenomicAlign(
00304               -dbID => $genomic_align_id,
00305               -adaptor => $genomic_align_adaptor
00306           );
00307     }
00308 
00309     push(@{$group->{'genomic_align_array'}}, $this_genomic_align);
00310   }
00311 
00312   $genomic_align_group = new Bio::EnsEMBL::Compara::GenomicAlignGroup(
00313           -dbID => $group->{'node_id'},
00314           -adaptor => $self,
00315           -genomic_align_array => $group->{'genomic_align_array'}
00316       );
00317   foreach my $this_genomic_align (@{$genomic_align_group->genomic_align_array}) {
00318     $this_genomic_align->genomic_align_group($genomic_align_group);
00319   }
00320 
00321   return $genomic_align_group;
00322 }
00323 
00324 
00325 =head2 fetch_by_genomic_align_id
00326 
00327   Arg  1     : integer $genomic_align_id
00328   Example    : my $genomic_align_group =
00329                   $genomic_align_group_adaptor->fetch_by_genomic_align_id(
00330                           12322)
00331   Description: Returns a Bio::EnsEMBL::Compara::GenomicAlignGroup corresponding
00332                to the given Bio::EnsEMBL::Compara::GenomicAlign defined by the
00333                $genomic_align_id
00334   Returntype : Bio::EnsEMBL::Compara::GenomicAlignGroup
00335   Exceptions : none
00336   Caller     : object::methodname
00337   Status     : Stable
00338 
00339 =cut
00340 
00341 sub fetch_by_genomic_align_id {
00342   my ($self, $genomic_align_id) = @_;
00343   my $genomic_align_group;
00344 
00345   my $genomic_align_adaptor = $self->db->get_GenomicAlignAdaptor;
00346   
00347   # Get Bio::EnsEMBL::Compara::GenomicAlign object
00348   my $genomic_align = $genomic_align_adaptor->fetch_by_dbID($genomic_align_id);
00349   unless($genomic_align && ref $genomic_align && 
00350         $genomic_align->isa('Bio::EnsEMBL::Compara::GenomicAlign')) {
00351     throw("[$genomic_align] must be a Bio::EnsEMBL::Compara::GenomicAlign object");
00352   }
00353 
00354   return $self->fetch_by_GenomicAlign($genomic_align);
00355 }
00356 
00357 
00358 
00359 =head2 use_autoincrement
00360 
00361   [Arg  1]   : (optional)int value
00362   Example    : $genomic_align_adaptor->use_autoincrement(0);
00363   Description: Getter/setter for the _use_autoincrement flag. This flag
00364                is used when storing new objects with no dbID in the
00365                database. If the flag is ON (default), the adaptor will
00366                let the DB set the dbID using the AUTO_INCREMENT ability.
00367                If you unset the flag, then the adaptor will look for the
00368                first available dbID after 10^10 times the
00369                method_link_species_set_id.
00370   Returntype : integer
00371   Exceptions : 
00372   Caller     : none
00373   Status     : Stable
00374 
00375 =cut
00376 
00377 sub use_autoincrement {
00378   my ($self, $value) = @_;
00379 
00380   if (defined $value) {
00381     $self->{_use_autoincrement} = $value;
00382   }
00383 
00384   return $self->{_use_autoincrement};
00385 }
00386 
00387 1;