Archive Ensembl HomeArchive Ensembl Home
DnaFragChunkSet.pm
Go to the documentation of this file.
00001 #
00002 # You may distribute this module under the same terms as perl itself
00003 #
00004 # POD documentation - main docs before the code
00005 
00006 =pod
00007 
00008 =head1 NAME
00009 
00010 Bio::EnsEMBL::Compara::Production::DnaFragChunkSet
00011 
00012 =head1 SYNOPSIS
00013 
00014 =head1 DESCRIPTION
00015 
00016 An object to hold a set or group of DnaFragChunk objects.  Used in production to reduce
00017 overhead of feeding sequences into alignment programs like blastz and exonerate.
00018 
00019 =head1 CONTACT
00020 
00021 Jessica Severin <jessica@ebi.ac.uk>
00022 
00023 =head1 APPENDIX
00024 
00025 The rest of the documentation details each of the object methods. Internal methods are usually preceded with a _
00026 
00027 =cut
00028 
00029 package Bio::EnsEMBL::Compara::Production::DnaFragChunkSet;
00030 
00031 use strict;
00032 use Bio::EnsEMBL::Compara::Production::DnaFragChunk;
00033 use Bio::EnsEMBL::Utils::Exception;
00034 use Bio::EnsEMBL::Utils::Argument;
00035 use Time::HiRes qw(time gettimeofday tv_interval);
00036 
00037 sub new {
00038   my ($class, @args) = @_;
00039 
00040   my $self = {};
00041   bless $self,$class;
00042 
00043   $self->{'_dnafrag_chunk_id_list'} = [];
00044   $self->{'_cached_chunk_list'} = undef;
00045   $self->{'_total_basepairs'} = 0;
00046 
00047   if (scalar @args) {
00048     #do this explicitly.
00049     my ($dbid, $description, $adaptor) = rearrange([qw(DBID NAME ADAPTOR)], @args);
00050 
00051     $self->dbID($dbid)               if($dbid);
00052     $self->description($description) if($description);
00053     $self->adaptor($adaptor)         if($adaptor);
00054   }
00055 
00056   return $self;
00057 }
00058 
00059 =head2 adaptor
00060 
00061  Title   : adaptor
00062  Usage   :
00063  Function: getter/setter of the adaptor for this object
00064  Example :
00065  Returns :
00066  Args    :
00067 
00068 =cut
00069 
00070 sub adaptor {
00071   my $self = shift;
00072   $self->{'_adaptor'} = shift if(@_);
00073   return $self->{'_adaptor'};
00074 }
00075 
00076 
00077 =head2 dbID
00078 
00079   Arg [1]    : int $dbID (optional)
00080   Example    :
00081   Description:
00082   Returntype :
00083   Exceptions :
00084   Caller     :
00085 
00086 =cut
00087 
00088 sub dbID {
00089   my $self = shift;
00090   $self->{'_dbID'} = shift if(@_);
00091   return $self->{'_dbID'};
00092 }
00093 
00094 =head2 description
00095 
00096   Arg [1]    : string $description (optional)
00097   Example    :
00098   Description:
00099   Returntype : string
00100   Exceptions :
00101   Caller     :
00102 
00103 =cut
00104 
00105 sub description {
00106   my $self = shift;
00107   $self->{'_description'} = shift if(@_);
00108   return $self->{'_description'};
00109 }
00110 
00111 =head2 dump_loc
00112 
00113   Arg [1]    : string $dump_loc (optional)
00114   Example    :
00115   Description:
00116   Returntype : string
00117   Exceptions :
00118   Caller     :
00119 
00120 =cut
00121 
00122 sub dump_loc {
00123   my $self = shift;
00124   $self->{'_dump_loc'} = shift if(@_);
00125   return $self->{'_dump_loc'};
00126 }
00127 
00128 =head2 add_dnafrag_chunk_id
00129 
00130   Arg [1]    : $chunk_id
00131   Example    :
00132   Description:
00133   Returntype :
00134   Exceptions :
00135   Caller     :
00136 
00137 =cut
00138 
00139 sub add_dnafrag_chunk_id {
00140   my $self = shift;
00141   my $count=0;
00142 
00143   if(@_) {
00144     my $chunk_id = shift;
00145     $count = push @{$self->{'_dnafrag_chunk_id_list'}}, $chunk_id;
00146     #print("added $count element to list\n");
00147     $self->{'_total_basepairs'}=0; #reset so will be recalculated
00148     if(defined($self->adaptor)) {
00149       $self->adaptor->store_link($self->dbID, $chunk_id);
00150     }
00151   }
00152   return $count
00153 }
00154 
00155 sub add_DnaFragChunk {
00156   my ($self, $chunk) = @_;
00157 
00158   unless(defined($chunk) and
00159          $chunk->isa('Bio::EnsEMBL::Compara::Production::DnaFragChunk'))
00160   {
00161     $self->throw(
00162       "chunk arg must be a [Bio::EnsEMBL::Compara::Production::DnaFragChunk] ".
00163       "not a [$chunk]");
00164   }
00165 
00166   my $count = $self->add_dnafrag_chunk_id($chunk->dbID);
00167 
00168   $self->{'_cached_chunk_list'} = []
00169     unless(defined($self->{'_cached_chunk_list'}));
00170   
00171   push @{$self->{'_cached_chunk_list'}}, $chunk;
00172 
00173   return $count;
00174 }
00175 
00176 =head2 get_all_DnaFragChunks
00177 
00178   Example    : @chunks = @{$chunkSet->get_all_DnaFragChunks};
00179   Description: returns array reference to all the DnaFragChunk objects in this set
00180                will lazy load the set if it was not previously loaded
00181   Returntype : reference to array of Bio::EnsEMBL::Compara::Production::DnaFragChunk objects
00182   Exceptions :
00183   Caller     :
00184 
00185 =cut
00186 
00187 sub get_all_DnaFragChunks {
00188   my $self = shift;
00189   if(!defined($self->{'_cached_chunk_list'}) and
00190      $self->count > 0 and defined($self->adaptor))
00191   {
00192     #lazy load all the DnaFragChunk objects
00193     $self->{'_cached_chunk_list'} =
00194       $self->adaptor->_fetch_all_DnaFragChunk_by_ids($self->dnafrag_chunk_ids);
00195     $self->{'_total_basepairs'}=0; #reset so it's recalculated
00196   }
00197   return $self->{'_cached_chunk_list'};
00198 }
00199 
00200 =head2 dnafrag_chunk_ids
00201 
00202   Example    : @chunk_ids = @{$chunkSet->dnafrag_chunk_ids};
00203   Description:
00204   Returntype : reference to array of dnafrag_chunk_id
00205   Exceptions :
00206   Caller     :
00207 
00208 =cut
00209 
00210 sub dnafrag_chunk_ids {
00211   my $self = shift;
00212   return $self->{'_dnafrag_chunk_id_list'};
00213 }
00214 
00215 
00216 =head2 count
00217 
00218   Example    : $count = $chunkSet->count;
00219   Description: returns count of DnaFragChunks in this set
00220   Returntype : int
00221   Exceptions :
00222   Caller     :
00223 
00224 =cut
00225 
00226 sub count {
00227   my $self = shift;
00228   return scalar(@{$self->dnafrag_chunk_ids()});
00229 }
00230 
00231 
00232 =head2 total_basepairs
00233 
00234   Example    : $size = $chunkSet->total_basepairs;
00235   Description: returns summed length of all DnaFragChunks in this set
00236   Returntype : int
00237   Exceptions :
00238   Caller     :
00239 
00240 =cut
00241 
00242 sub total_basepairs {
00243   my $self = shift;
00244   unless($self->{'_total_basepairs'}) {
00245     $self->{'_total_basepairs'} =0;
00246     if($self->get_all_DnaFragChunks) {
00247       foreach my $chunk (@{$self->get_all_DnaFragChunks}) {
00248         $self->{'_total_basepairs'} += $chunk->length;
00249       }
00250     }
00251   }
00252   return $self->{'_total_basepairs'};
00253 }
00254 
00255 1;