Archive Ensembl HomeArchive Ensembl Home
GenomicAlignGroup.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::Compara::GenomicAlignGroup - Defines groups of genomic aligned sequences
00022 
00023 =head1 SYNOPSIS
00024   
00025   use Bio::EnsEMBL::Compara::GenomicAlignGroup;
00026   
00027   my $genomic_align_group = new Bio::EnsEMBL::Compara::GenomicAlignGroup (
00028           -adaptor => $genomic_align_group_adaptor,
00029           -genomic_align_array => [$genomic_align1, $genomic_align2...]
00030       );
00031 
00032 SET VALUES
00033   $genomic_align_group->adaptor($gen_ali_group_adaptor);
00034   $genomic_align_group->dbID(12);
00035   $genomic_align_group->genomic_align_array([$genomic_align1, $genomic_align2]);
00036 
00037 GET VALUES
00038   my $genomic_align_group_adaptor = $genomic_align_group->adaptor();
00039   my $dbID = $genomic_align_group->dbID();
00040   my $genomic_aligns = $genomic_align_group->genomic_align_array();
00041 
00042 =head1 DESCRIPTION
00043 
00044 The GenomicAlignGroup object defines groups of alignments.
00045 
00046 =head1 OBJECT ATTRIBUTES
00047 
00048 =over
00049 
00050 =item dbID
00051 
00052 corresponds to genomic_align_group.node_id
00053 
00054 =item adaptor
00055 
00056 Bio::EnsEMBL::Compara::DBSQL::GenomicAlignGroupAdaptor object to access DB
00057 
00058 =item genomic_align_array
00059 
00060 listref of Bio::EnsEMBL::Compara::DBSQL::GenomicAlign objects corresponding to this
00061 Bio::EnsEMBL::Compara::DBSQL::GenomicAlignGroup object
00062 
00063 =back
00064 
00065 =head1 APPENDIX
00066 
00067 The rest of the documentation details each of the object methods. Internal methods are usually preceded with a _
00068 
00069 =cut
00070 
00071 
00072 # Let the code begin...
00073 
00074 
00075 package Bio::EnsEMBL::Compara::GenomicAlignGroup;
00076 use strict;
00077 
00078 # Object preamble
00079 use Bio::EnsEMBL::Utils::Argument qw(rearrange);
00080 use Bio::EnsEMBL::Utils::Exception qw(throw warning);
00081 use Scalar::Util qw(weaken);
00082 
00083 
00084 =head2 new (CONSTRUCTOR)
00085 
00086   Arg [-DBID] : (opt.) int $dbID (the database internal ID for this object)
00087   Arg [-ADAPTOR]
00088               : (opt.) Bio::EnsEMBL::Compara::DBSQL::GenomicAlignAdaptor $adaptor
00089                 (the adaptor for connecting to the database)
00090   Arg [-GENOMIC_ALIGN_ARRAY]
00091               : (opt.) array_ref $genomic_aligns (a reference to the array of
00092                 Bio::EnsEMBL::Compara::GenomicAlign objects corresponding to this
00093                 Bio::EnsEMBL::Compara::GenomicAlignGroup object)
00094   Example    : my $genomic_align_group =
00095                    new Bio::EnsEMBL::Compara::GenomicAlignGroup(
00096                        -adaptor => $genomic_align_group_adaptor,
00097                        -genomic_align_array => [$genomic_align1, $genomic_align2...]
00098                    );
00099   Description: Creates a new GenomicAligngroup object
00100   Returntype : Bio::EnsEMBL::Compara::DBSQL::GenomicAlignGroup
00101   Exceptions : none
00102   Caller     : general
00103   Status     : Stable
00104 
00105 =cut
00106 
00107 sub new {
00108   my($class, @args) = @_;
00109   
00110   my $self = {};
00111   bless $self,$class;
00112     
00113   my ($adaptor, $dbID, $genomic_align_array) = 
00114     rearrange([qw(
00115         ADAPTOR DBID GENOMIC_ALIGN_ARRAY)], @args);
00116 
00117   $self->adaptor($adaptor) if (defined ($adaptor));
00118   $self->dbID($dbID) if (defined ($dbID));
00119   $self->genomic_align_array($genomic_align_array) if (defined($genomic_align_array));
00120 
00121   return $self;
00122 }
00123 
00124 =head2 new_fast
00125 
00126   Arg [1]    : hash reference $hashref
00127   Example    : none
00128   Description: This is an ultra fast constructor which requires knowledge of
00129                the objects internals to be used.
00130   Returntype :
00131   Exceptions : none
00132   Caller     :
00133   Status     : Stable
00134 
00135 =cut
00136 
00137 sub new_fast {
00138   my $class = shift;
00139   my $hashref = shift;
00140 
00141   return bless $hashref, $class;
00142 }
00143 
00144 =head2 copy
00145 
00146   Arg         : none
00147   Example     : my $new_gag = $gag->copy()
00148   Description : Create a copy of this Bio::EnsEMBL::Compara::GenomicAlignGroup
00149                 object
00150   Returntype  : Bio::EnsEMBL::Compara::GenomicAlignGroup
00151   Exceptions  : none
00152   Caller      : general
00153   Status      : Stable
00154 
00155 =cut
00156 
00157 sub copy {
00158   my $self = shift;
00159   my $copy;
00160   $copy->{original_dbID} = $self->{dbID};
00161 
00162   #This is not a deep copy
00163   #$copy->{genomic_align_array} = $self->{genomic_align_array};
00164 
00165   my $new_genomic_align_array;
00166   foreach my $genomic_align (@{$self->{genomic_align_array}}) {
00167       my $new_ga = $genomic_align->copy;
00168       push @$new_genomic_align_array, $new_ga;
00169   }
00170   $copy->{genomic_align_array} = $new_genomic_align_array;
00171 
00172   return bless $copy, ref($self);
00173 }
00174 
00175 
00176 =head2 adaptor
00177 
00178   Arg [1]    : Bio::EnsEMBL::Compara::DBSQL::GenomicAlignGroupAdaptor $adaptor
00179   Example    : my $gen_ali_grp_adaptor = $genomic_align_block->adaptor();
00180   Example    : $genomic_align_block->adaptor($gen_ali_grp_adaptor);
00181   Description: Getter/Setter for the adaptor this object uses for database
00182                interaction.
00183   Returntype : Bio::EnsEMBL::Compara::DBSQL::GenomicAlignGroupAdaptor object
00184   Exceptions : thrown if $adaptor is not a
00185                Bio::EnsEMBL::Compara::DBSQL::GenomicAlignGroupAdaptor object
00186   Caller     : general
00187   Status     : Stable
00188 
00189 =cut
00190 
00191 sub adaptor {
00192   my ($self, $adaptor) = @_;
00193 
00194   if (defined($adaptor)) {
00195     throw("$adaptor is not a Bio::EnsEMBL::Compara::DBSQL::GenomicAlignGroupAdaptor object")
00196         unless ($adaptor->isa("Bio::EnsEMBL::Compara::DBSQL::GenomicAlignGroupAdaptor"));
00197     $self->{'adaptor'} = $adaptor;
00198   }
00199 
00200   return $self->{'adaptor'};
00201 }
00202 
00203 
00204 =head2 dbID
00205 
00206   Arg [1]    : integer $dbID
00207   Example    : my $dbID = $genomic_align_group->dbID();
00208   Example    : $genomic_align_group->dbID(12);
00209   Description: Getter/Setter for the attribute dbID
00210   Returntype : integer
00211   Exceptions : none
00212   Caller     : general
00213   Status     : Stable
00214 
00215 =cut
00216 
00217 sub dbID {
00218   my ($self, $dbID) = @_;
00219 
00220   if (defined($dbID)) {
00221     $self->{'dbID'} = $dbID;
00222   }
00223 
00224   return $self->{'dbID'};
00225 }
00226 
00227 =head2 genomic_align_array
00228 
00229   Arg [1]    : array reference containing Bio::EnsEMBL::Compara::GenomicAlign objects
00230   Example    : $genomic_aligns = $genomic_align_group->genomic_align_array();
00231                $genomic_align_group->genomic_align_array([$genomic_align1, $genomic_align2]);
00232   Description: get/set for attribute genomic_align_array
00233   Returntype : array reference containing Bio::EnsEMBL::Compara::GenomicAlign objects
00234   Exceptions : none
00235   Caller     : general
00236   Status     : Stable
00237 
00238 =cut
00239 
00240 sub genomic_align_array {
00241     my ($self, $genomic_align_array) = @_;
00242     my $genomic_align_adaptor;
00243 
00244     if (defined $genomic_align_array) {
00245     foreach my $genomic_align (@$genomic_align_array) {
00246         throw("$genomic_align is not a Bio::EnsEMBL::Compara::GenomicAlign object") unless ($genomic_align->isa("Bio::EnsEMBL::Compara::GenomicAlign"));
00247     }
00248     $self->{'genomic_align_array'} = $genomic_align_array;
00249     } elsif (!defined $self->{'genomic_align_array'}) {
00250     # Try to get genomic_align_array from other sources
00251     if (defined($self->{'adaptor'}) and defined($self->{'dbID'})) {
00252         my $genomic_align_group_adaptor = $self->adaptor->db->get_GenomicAlignGroupAdaptor;
00253         my $gag = $genomic_align_group_adaptor->fetch_by_dbID($self->{'dbID'});
00254         $self->{'genomic_align_array'} = $gag->{'genomic_align_array'};
00255     } else {
00256         warning("Fail to get data from other sources in Bio::EnsEMBL::Compara::GenomicAlignGroup->genomic_align_array".
00257             " You either have to specify more information (see perldoc for".
00258             " Bio::EnsEMBL::Compara::GenomicAlign) or to set it up directly");
00259     }
00260     }
00261 
00262     return $self->{'genomic_align_array'};
00263 }
00264 
00265 =head2 add_GenomicAlign
00266 
00267   Arg [1]    : Bio::EnsEMBL::Compara::GenomicAlign $genomic_align
00268   Example    : $genomic_align_block->add_GenomicAlign($genomic_align);
00269   Description: adds another Bio::EnsEMBL::Compara::GenomicAlign object to the set of
00270                Bio::EnsEMBL::Compara::GenomicAlign objects in the attribute
00271                genomic_align_array.
00272   Returntype : Bio::EnsEMBL::Compara::GenomicAlign object
00273   Exceptions : thrown if wrong argument
00274   Caller     : general
00275   Status     : Stable
00276 
00277 =cut
00278 
00279 sub add_GenomicAlign {
00280   my ($self, $genomic_align) = @_;
00281 
00282   throw("[$genomic_align] is not a Bio::EnsEMBL::Compara::GenomicAlign object")
00283       unless ($genomic_align and ref($genomic_align) and
00284           $genomic_align->isa("Bio::EnsEMBL::Compara::GenomicAlign"));
00285   push(@{$self->{'genomic_align_array'}}, $genomic_align);
00286 
00287   return $genomic_align;
00288 }
00289 
00290 
00291 =head2 get_all_GenomicAligns
00292 
00293   Arg [1]    : none
00294   Example    : $genomic_aligns = $genomic_align_block->get_all_GenomicAligns();
00295   Description: returns the set of Bio::EnsEMBL::Compara::GenomicAlign objects in
00296                the attribute genomic_align_array.
00297   Returntype : array reference containing Bio::EnsEMBL::Compara::GenomicAlign objects
00298   Exceptions : none
00299   Caller     : general
00300   Status     : Stable
00301 
00302 =cut
00303 
00304 sub get_all_GenomicAligns {
00305   my ($self) = @_;
00306 
00307   return ($self->{'genomic_align_array'} or []);
00308 }
00309 
00310 
00311 =head2 genome_db
00312 
00313   Arg [1]     : -none-
00314   Example     : $genome_db = $object->genome_db();
00315   Description : Get the genome_db object from the underlying GenomicAlign objects
00316   Returntype  : Bio::EnsEMBL::Compara::GenomeDB
00317   Exceptions  : none
00318   Caller      : general
00319   Status      : Stable
00320 
00321 =cut
00322 
00323 sub genome_db {
00324   my $self = shift;
00325 
00326   foreach my $genomic_align (@{$self->get_all_GenomicAligns}) {
00327     return $genomic_align->genome_db if ($genomic_align->genome_db);
00328   }
00329   return undef;
00330 }
00331 
00332 
00333 =head2 dnafrag
00334 
00335   Arg [1]     : -none-
00336   Example     : $dnafrag = $object->dnafrag();
00337   Description : Get the dnafrag object from the underlying GenomicAlign objects
00338   Returntype  : Bio::EnsEMBL::Compara::DnaFrag
00339   Exceptions  : none
00340   Caller      : general
00341   Status      : Stable
00342 
00343 =cut
00344 
00345 sub dnafrag {
00346   my $self = shift;
00347   my $dnafrag;
00348   foreach my $genomic_align (@{$self->get_all_GenomicAligns}) {
00349     if (!$dnafrag) {
00350       $dnafrag = $genomic_align->dnafrag;
00351     } elsif ($dnafrag != $genomic_align->dnafrag) {
00352       return bless({name => "Composite"}, "Bio::EnsEMBL::Compara::DnaFrag");
00353     }
00354   }
00355   return $dnafrag;
00356 }
00357 
00358 
00359 =head2 dnafrag_start
00360 
00361   Arg [1]     : -none-
00362   Example     : $dnafrag_start = $object->dnafrag_start();
00363   Description : Get the dnafrag_start value from the underlying GenomicAlign objects
00364   Returntype  : int
00365   Exceptions  : none
00366   Caller      : general
00367   Status      : Stable
00368 
00369 =cut
00370 
00371 sub dnafrag_start {
00372   my $self = shift;
00373   my $dnafrag;
00374   my $dnafrag_start;
00375   foreach my $genomic_align (@{$self->get_all_GenomicAligns}) {
00376     if (!$dnafrag) {
00377       $dnafrag = $genomic_align->dnafrag;
00378       $dnafrag_start = $genomic_align->dnafrag_start;
00379     } elsif ($dnafrag != $genomic_align->dnafrag) {
00380       return 1;
00381     } elsif ($genomic_align->dnafrag_start < $dnafrag_start) {
00382       $dnafrag_start = $genomic_align->dnafrag_start;
00383     }
00384   }
00385   return $dnafrag_start;
00386 }
00387 
00388 
00389 =head2 dnafrag_end
00390 
00391   Arg [1]     : -none-
00392   Example     : $dnafrag_end = $object->dnafrag_end();
00393   Description : Get the dnafrag_end value from the underlying GenomicAlign objects
00394   Returntype  : int
00395   Exceptions  : none
00396   Caller      : general
00397   Status      : Stable
00398 
00399 =cut
00400 
00401 sub dnafrag_end {
00402   my $self = shift;
00403   my $dnafrag;
00404   my $dnafrag_end;
00405   foreach my $genomic_align (@{$self->get_all_GenomicAligns}) {
00406     if (!$dnafrag) {
00407       $dnafrag = $genomic_align->dnafrag;
00408       $dnafrag_end = $genomic_align->dnafrag_end;
00409     } elsif ($dnafrag != $genomic_align->dnafrag) {
00410       return $genomic_align->length;
00411     } elsif ($genomic_align->dnafrag_end > $dnafrag_end) {
00412       $dnafrag_end = $genomic_align->dnafrag_end;
00413     }
00414   }
00415   return $dnafrag_end;
00416 }
00417 
00418 
00419 =head2 dnafrag_strand
00420 
00421   Arg [1]     : -none-
00422   Example     : $dnafrag_strand = $object->dnafrag_strand();
00423   Description : Get the dnafrag_strand value from the underlying GenomicAlign objects
00424   Returntype  : int
00425   Exceptions  : none
00426   Caller      : general
00427   Status      : Stable
00428 
00429 =cut
00430 
00431 sub dnafrag_strand {
00432   my $self = shift;
00433   my $dnafrag_strand;
00434   foreach my $genomic_align (@{$self->get_all_GenomicAligns}) {
00435     if (!$dnafrag_strand) {
00436       $dnafrag_strand = $genomic_align->dnafrag_strand;
00437     } elsif ($dnafrag_strand != $genomic_align->dnafrag_strand) {
00438       return 0;
00439     }
00440   }
00441   return $dnafrag_strand;
00442 }
00443 
00444 
00445 =head2 aligned_sequence
00446 
00447   Arg [1]     : -none-
00448   Example     : $aligned_sequence = $object->aligned_sequence();
00449   Description : Get the aligned sequence for this group. When the group
00450                 contains one single sequence, returns its aligned sequence.
00451                 For composite segments, returns the combined aligned seq.
00452   Returntype  : string
00453   Exceptions  : none
00454   Caller      : general
00455   Status      : At risk
00456 
00457 =cut
00458 
00459 sub aligned_sequence {
00460   my $self = shift;
00461 
00462   my $aligned_sequence;
00463   foreach my $this_genomic_align (@{$self->get_all_GenomicAligns}) {
00464     if (!$aligned_sequence) {
00465       $aligned_sequence = $this_genomic_align->aligned_sequence;
00466     } else {
00467       my $pos = 0;
00468       foreach my $substr (grep {$_} split(/(\.+)/, $this_genomic_align->aligned_sequence)) {
00469         if ($substr =~ /^\.+$/) {
00470           $pos += length($substr);
00471         } else {
00472           substr($aligned_sequence, $pos, length($substr), $substr);
00473         }
00474       }
00475     }
00476   }
00477 
00478   return $aligned_sequence;
00479 }
00480 
00481 
00482 
00483 1;