Archive Ensembl HomeArchive Ensembl Home
SyntenyRegionAdaptor.pm
Go to the documentation of this file.
00001 #
00002 # Ensembl module for Bio::EnsEMBL::Compara::SyntenyRegionAdaptor
00003 #
00004 # Cared for by Abel Ureta-Vidal <abel@ebi.ac.uk>
00005 #
00006 # Copyright GRL and EBI
00007 #
00008 # You may distribute this module under the same terms as perl itself
00009 
00010 # POD documentation - main docs before the code
00011 
00012 =head1 NAME
00013 
00014 Bio::EnsEMBL::Compara::DBSQL::SyntenyRegionAdaptor - Object to access data in the synteny_region
00015 and dnafrag_region tables
00016 
00017 =head1 SYNOPSIS
00018 
00019 =head2 Get the adaptor from the Registry
00020 
00021   use Bio::EnsEMBL::Registry;
00022 
00023   my $reg = "Bio::EnsEMBL::Registry";
00024   $reg->load_registry_from_db(
00025       -host => "ensembldb.ensembl.org",
00026       -user => "anonymous");
00027 
00028   my $synteny_region_adaptor = $reg->get_adaptor(
00029       "Multi", "compara", "SyntenyRegion");
00030 
00031 =head2 Store method
00032 
00033   $synteny_region_adaptor->store($synteny_region);
00034 
00035 =head2 Fetching methods
00036 
00037   my $synteny_region = $synteny_region_adaptor->fetch_by_dbID(1);
00038   my $synteny_regions = $synteny_region_adaptor->
00039       fetch_all_by_MethodLinkSpeciesSet_DnaFrag(
00040           $mlss, $dnafrag, $start, $end);
00041 
00042 
00043 =head2 Example script
00044 
00045   use strict;
00046   use Bio::EnsEMBL::Registry;
00047 
00048   my $reg = "Bio::EnsEMBL::Registry";
00049 
00050   $reg->load_registry_from_db(
00051       -host=>"ensembldb.ensembl.org", -user=>"anonymous", -software_version => 38);
00052 
00053   my $method_link_species_set_adaptor = $reg->get_adaptor(
00054       "Multi", "compara", "MethodLinkSpeciesSet");
00055   my $human_mouse_synteny_method_link_species_set =
00056       $method_link_species_set_adaptor->
00057           fetch_by_method_link_type_registry_aliases(
00058               "SYNTENY", ["human", "mouse"]);
00059 
00060   my $genome_db_adaptor = $reg->get_adaptor(
00061       "Multi", "compara", "GenomeDB");
00062   my $genome_db = $genome_db_adaptor->
00063       fetch_by_name_assembly("Homo sapiens", undef);
00064 
00065   my $dnafrag_adaptor = $reg->get_adaptor(
00066       "Multi", "compara", "DnaFrag");
00067   my $dnafrag = $dnafrag_adaptor->
00068       fetch_by_GenomeDB_and_name($genome_db, "3");
00069 
00070   my $synteny_region_adaptor = $reg->get_adaptor(
00071       "Multi", "compara", "SyntenyRegion");
00072   my $synteny_regions = $synteny_region_adaptor->
00073       fetch_all_by_MethodLinkSpeciesSet_DnaFrag(
00074           $human_mouse_synteny_method_link_species_set,
00075           $dnafrag, 100000, 200000);
00076 
00077   foreach my $this_synteny_region (@$synteny_regions) {
00078     my $these_dnafrag_regions = $this_synteny_region->children();
00079     foreach my $this_dnafrag_region (@$these_dnafrag_regions) {
00080       print $this_dnafrag_region->dnafrag->genome_db->name, ": ", $this_dnafrag_region->slice->name, "\n";
00081     }
00082     print "\n";
00083   }
00084 
00085 =head1 DESCRIPTION
00086 
00087 This object is intended for accessing data in the synteny_region and dnafrag_region tables.
00088 
00089 =head1 INHERITANCE
00090 
00091 This class inherits all the methods and attributes from Bio::EnsEMBL::DBSQL::BaseAdaptor
00092 
00093 =head1 SEE ALSO
00094 
00095  - Bio::EnsEMBL::Compara::SyntenyRegion
00096  - Bio::EnsEMBL::Compara::DnaFragRegion
00097  - Bio::EnsEMBL::Compara::MethodLinkSpeciesSet
00098  - Bio::EnsEMBL::Compara::GenomeDB
00099 
00100 =head1 CONTACT
00101 
00102 Ensembl - dev@ensembl.org
00103 
00104 =head1 APPENDIX
00105 
00106 The rest of the documentation details each of the object methods. Internal methods are usually preceded with a _
00107 
00108 =cut
00109 
00110 package Bio::EnsEMBL::Compara::DBSQL::SyntenyRegionAdaptor;
00111 
00112 use strict;
00113 use warnings;
00114 use Data::Dumper;
00115 use Bio::EnsEMBL::Utils::Exception;
00116 use Bio::EnsEMBL::Compara::SyntenyRegion;
00117 
00118 use Bio::EnsEMBL::DBSQL::BaseAdaptor;
00119 our @ISA = qw(Bio::EnsEMBL::DBSQL::BaseAdaptor);
00120 
00121 
00122 =head2 fetch_by_dbID
00123 
00124   Arg  1     : int $synteny_region_id
00125   Example    : my $this_synteny_region = $synteny_region_adaptor->
00126                   fetch_by_dbID($synteny_region_id)
00127   Description: Fetches the corresponding Bio::EnsEMBL::Compara::SyntenyRegion
00128                object.
00129   Returntype : Bio::EnsEMBL::Compara::SyntenyRegion object
00130   Exception  : Thrown if the argument is not defined
00131   Caller     :
00132 
00133 =cut
00134 
00135 sub fetch_by_dbID{
00136    my ($self,$dbID) = @_;
00137 
00138    if( !defined $dbID ) {
00139      throw("fetch_by_dbID with no dbID!");
00140    }
00141 
00142    my $sth = $self->prepare("select synteny_region_id, method_link_species_set_id from synteny_region where synteny_region_id = $dbID");
00143    $sth->execute;
00144    my ($synteny_region_id, $method_link_species_set_id) = $sth->fetchrow_array();
00145 
00146    my $sr = new Bio::EnsEMBL::Compara::SyntenyRegion;
00147    $sr->dbID($synteny_region_id);
00148    $sr->method_link_species_set_id($method_link_species_set_id);
00149 
00150    my $dfra = $self->db->get_DnaFragRegionAdaptor;
00151    my $dfrs = $dfra->fetch_by_synteny_region_id($dbID);
00152    $sr->regions($dfrs);
00153    # while (my $dfr = shift @{$dfrs}) {
00154    #   $sr->add_child($dfr);
00155    # }
00156    return $sr;
00157 }
00158 
00159 =head2 store
00160 
00161   Arg  1     : Bio::EnsEMBL::Compara::SyntenyRegion object
00162   Example    : $synteny_region_adaptor->store($synteny_region)
00163   Description: Stores a Bio::EnsEMBL::Compara::SyntenyRegion object into
00164                the database as well as the underlying
00165                Bio::EnsEMBL::Compara::DnaFragRegion objects
00166   Returntype : int (the synteny_region_id)
00167   Exception  : Thrown if the argument is not a
00168                Bio::EnsEMBL::Compara::SyntenyRegion object
00169   Caller     :
00170 
00171 =cut
00172 
00173 sub store{
00174    my ($self,$sr) = @_;
00175 
00176    if( !ref $sr || !$sr->isa("Bio::EnsEMBL::Compara::SyntenyRegion") ) {
00177        throw("$sr is not a SyntenyRegion object");
00178    }
00179 
00180    my $sth = $self->prepare("insert into synteny_region (method_link_species_set_id) VALUES (?)");
00181 
00182    $sth->execute($sr->method_link_species_set_id);
00183    my $synteny_region_id = $sth->{'mysql_insertid'};
00184 
00185    $sr->dbID($synteny_region_id);
00186    $sr->adaptor($self);
00187 
00188    my $dfra = $self->db->get_DnaFragRegionAdaptor;
00189    foreach my $dfr (@{$sr->regions}) {
00190      $dfr->synteny_region_id($synteny_region_id);
00191      $dfra->store($dfr);
00192    }
00193    return $sr->dbID;
00194 }
00195 
00196 
00197 =head2 fetch_all_by_MethodLinkSpeciesSet_Slice
00198 
00199   Arg  1     : Bio::EnsEMBL::Compara::MethodLinkSpeciesSet $method_link_species_set
00200   Arg  2     : Bio::EnsEMBL::Slice $original_slice
00201   Example    : my $synteny_regions =
00202                   $synteny_region_adaptor->fetch_all_by_MethodLinkSpeciesSet_Slice(
00203                       $method_link_species_set, $original_slice);
00204   Description: Retrieve the corresponding
00205                Bio::EnsEMBL::Compara::SyntenyRegion objects.
00206   Returntype : ref. to an array of Bio::EnsEMBL::Compara::SyntenyRegion objects.
00207   Exceptions : Returns ref. to an empty array if no matching
00208                Bio::EnsEMBL::Compara::SyntenyRegion object can be retrieved
00209   Caller     : $object->method_name
00210   Status     : Medium risk
00211 
00212 =cut
00213 
00214 sub fetch_all_by_MethodLinkSpeciesSet_Slice {
00215   my ($self, $method_link_species_set, $reference_slice) = @_;
00216   my $all_synteny_regions = []; # Returned value
00217 
00218   ## method_link_species_set will be checked in the fetch_all_by_MethodLinkSpeciesSet_DnaFrag method
00219 
00220   ## Check original_slice
00221   unless(UNIVERSAL::isa($reference_slice, 'Bio::EnsEMBL::Slice')) {
00222     throw("[$reference_slice] should be a Bio::EnsEMBL::Slice object\n");
00223   }
00224 
00225   my $dnafrag_adaptor = $self->db->get_DnaFragAdaptor;
00226 
00227   my $projection_segments = $reference_slice->project('toplevel');
00228   return [] if(!@$projection_segments);
00229 
00230   foreach my $this_projection_segment (@$projection_segments) {
00231     my $this_slice = $this_projection_segment->to_Slice;
00232     my $this_dnafrag = $dnafrag_adaptor->fetch_by_Slice($this_slice);
00233     next if (!$this_dnafrag);
00234     my $these_synteny_regions = $self->fetch_all_by_MethodLinkSpeciesSet_DnaFrag(
00235             $method_link_species_set,
00236             $this_dnafrag,
00237             $this_slice->start,
00238             $this_slice->end,
00239         );
00240 
00241     push (@$all_synteny_regions, @$these_synteny_regions);
00242   }
00243 
00244   return $all_synteny_regions;
00245 }
00246 
00247 
00248 =head2 fetch_all_by_MethodLinkSpeciesSet_DnaFrag
00249 
00250   Arg 1      : Bio::EnsEMBL::Compara::MethodLinkSpeciesSet $mlss
00251   Arg 2      : Bio::EnsEMBL::Compara::DnaFrag $dnafrag
00252   Arg 3 (opt): int $start
00253   Arg 4 (opt): int $end
00254   Example    : my $these_synteny_regions = $synteny_region_adaptor->
00255                   fetch_all_by_MethodLinkSpeciesSet_DnaFrag(
00256                   $mlss, $dnafrag, 100000, 200000);
00257   Description: Fetches the Bio::EnsEMBL::Compara::SyntenyRegion
00258                objects in this region for the set of species
00259                defined by the $mlss.
00260   Returntype : listref of Bio::EnsEMBL::Compara::SyntenyRegion objects
00261   Exception  : Thrown if the argument is not defined
00262   Caller     :
00263   Status     : Stable
00264 
00265 =cut
00266 
00267 sub fetch_all_by_MethodLinkSpeciesSet_DnaFrag {
00268   my ($self, $mlss, $dnafrag, $start, $end) = @_;
00269 
00270   if (!UNIVERSAL::isa($mlss, "Bio::EnsEMBL::Compara::MethodLinkSpeciesSet")) {
00271     throw("[$mlss] is not a Bio::EnsEMBL::Compara::MethodLinkSpeciesSet object");
00272   }
00273   if (!UNIVERSAL::isa($dnafrag, "Bio::EnsEMBL::Compara::DnaFrag")) {
00274     throw("[$dnafrag] is not a Bio::EnsEMBL::Compara::DnaFrag object");
00275   }
00276 
00277   my $sql = "select sr.synteny_region_id from synteny_region sr, dnafrag_region dfr where sr.method_link_species_set_id = ? and sr.synteny_region_id=dfr.synteny_region_id and dfr.dnafrag_id = ?";
00278   
00279   if (defined $start) {
00280     $sql .= " and dfr.dnafrag_end >= $start";
00281   }
00282   if (defined $end) {
00283     $sql .= " and dfr.dnafrag_start <= $end";
00284   }
00285 
00286   my $sth = $self->prepare($sql);
00287   $sth->execute($mlss->dbID, $dnafrag->dbID);
00288 
00289   my $synteny_region_id;
00290   $sth->bind_columns(\$synteny_region_id);
00291   my @srs;
00292   while ($sth->fetch) {
00293     my $sr = Bio::EnsEMBL::Compara::SyntenyRegion->new();
00294     $sr->dbID($synteny_region_id);
00295     $sr->method_link_species_set_id($mlss->dbID);
00296 
00297     my $dfra = $self->db->get_DnaFragRegionAdaptor;
00298     my $dfrs = $dfra->fetch_by_synteny_region_id($synteny_region_id);
00299     $sr->regions($dfrs);
00300     # while (my $dfr = shift @{$dfrs}) {
00301     #   $sr->add_child($dfr);
00302     # }
00303 
00304     push @srs, $sr;
00305   }
00306 
00307   return \@srs;
00308 }
00309 
00310 
00311 =head2 fetch_all_by_MethodLinkSpeciesSet
00312 
00313   Arg 1      : Bio::EnsEMBL::Compara::MethodLinkSpeciesSet $mlss
00314   Example    : my $these_synteny_regions = $synteny_region_adaptor->
00315                   fetch_all_by_MethodLinkSpeciesSet($mlss);
00316   Description: Fetches the Bio::EnsEMBL::Compara::SyntenyRegion
00317                objects for the set of species defined by the $mlss.
00318   Returntype : listref of Bio::EnsEMBL::Compara::SyntenyRegion objects
00319   Exception  : Thrown if the argument is not defined
00320   Caller     :
00321   Status     : Stable
00322 
00323 =cut
00324 
00325 sub fetch_all_by_MethodLinkSpeciesSet {
00326   my ($self, $mlss) = @_;
00327 
00328   if (!UNIVERSAL::isa($mlss, "Bio::EnsEMBL::Compara::MethodLinkSpeciesSet")) {
00329     throw("[$mlss] is not a Bio::EnsEMBL::Compara::MethodLinkSpeciesSet object");
00330   }
00331 
00332   my $sql = "select sr.synteny_region_id from synteny_region sr where sr.method_link_species_set_id = ?";
00333 
00334   my $sth = $self->prepare($sql);
00335   $sth->execute($mlss->dbID);
00336 
00337   my $synteny_region_id;
00338   $sth->bind_columns(\$synteny_region_id);
00339   my @srs;
00340   while ($sth->fetch) {
00341     my $sr = new Bio::EnsEMBL::Compara::SyntenyRegion;
00342     $sr->dbID($synteny_region_id);
00343     $sr->method_link_species_set_id($mlss->dbID);
00344 
00345     my $dfra = $self->db->get_DnaFragRegionAdaptor;
00346     my $dfrs = $dfra->fetch_by_synteny_region_id($synteny_region_id);
00347     $sr->regions($dfrs);
00348     # while (my $dfr = shift @{$dfrs}) {
00349     #   $sr->add_child($dfr);
00350     # }
00351 
00352     push @srs, $sr;
00353   }
00354 
00355   return \@srs;
00356 }
00357 
00358 1;
00359 
00360 
00361 
00362 
00363 
00364 
00365