BaseInfo.pm
Go to the documentation of this file.
00001 
00002 =head1 LICENSE
00003 
00004 Copyright [1999-2016] EMBL-European Bioinformatics Institute
00005 
00006 Licensed under the Apache License, Version 2.0 (the "License");
00007 you may not use this file except in compliance with the License.
00008 You may obtain a copy of the License at
00009 
00010      http://www.apache.org/licenses/LICENSE-2.0
00011 
00012 Unless required by applicable law or agreed to in writing, software
00013 distributed under the License is distributed on an "AS IS" BASIS,
00014 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 See the License for the specific language governing permissions and
00016 limitations under the License.
00017 
00018 =head1 CONTACT
00019 
00020   Please email comments or questions to the public Ensembl
00021   developers list at <dev@ensembl.org>.
00022 
00023   Questions may also be sent to the Ensembl help desk at
00024   <helpdesk@ensembl.org>.
00025 
00026 =head1 NAME
00027 
00028 Bio::EnsEMBL::MetaData::BaseInfo
00029 
00030 =head DESCRIPTION
00031 
00032 Base class for other data objects in Bio::EnsEMBL::MetaData API.
00033 
00034 =head1 AUTHOR
00035 
00036 Dan Staines
00037 
00038 =head1 SEE ALSO
00039 
00040 Bio::EnsEMBL::MetaData::GenomeInfo
00041 Bio::EnsEMBL::MetaData::GenomeComparaInfo
00042 Bio::EnsEMBL::MetaData::GenomeAssemblyInfo
00043 Bio::EnsEMBL::MetaData::GenomeOrganismInfo
00044 Bio::EnsEMBL::MetaData::DataReleaseInfo
00045 Bio::EnsEMBL::MetaData::DatabaseInfo
00046 Bio::EnsEMBL::MetaData::EventInfo
00047 Bio::EnsEMBL::MetaData::DBSQL::BaseInfoAdaptor
00048 
00049 =cut
00050 
00051 use strict;
00052 use warnings;
00053 
00054 package Bio::EnsEMBL::MetaData::BaseInfo;
00055 use Bio::EnsEMBL::Utils::Argument qw(rearrange);
00056 
00057 =head1 CONSTRUCTOR
00058 =head2 new 
00059   Arg        : -DBID - ID for Storable object
00060   Arg:       : -ADAPTOR - corresponding DBAdaptor
00061   Description: Creates a new info object
00062   Returntype : Bio::EnsEMBL::MetaData::GenomeInfo
00063   Exceptions : none
00064   Caller     : internal
00065   Status     : Stable
00066 =cut
00067 
00068 sub new {
00069   my ( $proto, @args ) = @_;
00070   my $class = ref($proto) || $proto;
00071   my $self = bless( {}, $class );
00072   ( $self->{dbID}, $self->{adaptor} ) =
00073     rearrange( [ 'DBID', 'ADAPTOR' ], @args );
00074   return $self;
00075 }
00076 
00077 =head1 METHODS
00078 
00079 =head2 to_hash
00080   Description: Render object as plain hash suitable for export as JSON/XML
00081   Argument   : (optional) if set to 1, force expansion of children
00082   Returntype : Hashref
00083   Exceptions : none
00084   Caller     : general
00085   Status     : Stable
00086 =cut
00087 
00088 sub to_hash {
00089   my ( $in, $keen ) = @_;
00090   my $out;
00091   my $type = ref $in;
00092   if ( defined $keen &&
00093        $keen == 1 &&
00094        $type =~ m/Bio::EnsEMBL::MetaData::[A-z]+Info/ )
00095   {
00096     $in->_preload();
00097   }
00098   if ( $type eq 'ARRAY' ) {
00099     $out = [];
00100     for my $item ( @{$in} ) {
00101       push @{$out}, to_hash( $item, $keen );
00102     }
00103   }
00104   elsif ( $type eq 'HASH' || $type eq 'Bio::EnsEMBL::MetaData::GenomeInfo' ) {
00105     $out = {};
00106     while ( my ( $key, $val ) = each %$in ) {
00107       if ( $key ne 'dbID' && $key ne 'adaptor' && $key ne 'logger' ) {
00108 
00109 # deal with keys starting with numbers, which are not valid element names in XML
00110         if ( $key =~ m/^[0-9].*/ ) {
00111           $key = '_' . $key;
00112         }
00113         $out->{$key} = to_hash( $val, $keen );
00114       }
00115 
00116     }
00117   }
00118   elsif ( $type =~ m/Bio::EnsEMBL::MetaData::[A-z]+Info/ ) {
00119     $out = $in->to_hash($keen);
00120   }
00121   else {
00122     $out = $in;
00123   }
00124   if ( defined $keen &&
00125        $keen == 1 &&
00126        $type =~ m/Bio::EnsEMBL::MetaData::[A-z]+Info/ )
00127   {
00128     $in->_preload();
00129   }
00130   return $out;
00131 } ## end sub to_hash
00132 
00133 =head1 INTERNAL METHODS
00134 =head2 count_hash_values
00135   Description: Sums values found in hash
00136   Arg        : hashref
00137   Returntype : integer
00138   Exceptions : none
00139   Caller     : internal
00140   Status     : Stable
00141 =cut
00142 
00143 sub count_hash_values {
00144   my ( $self, $hash ) = @_;
00145   my $tot = 0;
00146   if ( defined $hash ) {
00147     for my $v ( values %{$hash} ) {
00148       $tot += $v;
00149     }
00150   }
00151   return $tot;
00152 }
00153 
00154 =head2 count_hash_lengths
00155   Description: Sums sizes of arrays found in hash as values
00156   Arg        : hashref
00157   Returntype : integer
00158   Exceptions : none
00159   Caller     : internal
00160   Status     : Stable
00161 =cut
00162 
00163 sub count_hash_lengths {
00164   my ( $self, $hash ) = @_;
00165   my $tot = 0;
00166   if ( defined $hash ) {
00167     for my $v ( values %{$hash} ) {
00168       $tot += scalar(@$v);
00169     }
00170   }
00171   return $tot;
00172 }
00173 
00174 =head2 dbID
00175   Arg        : (optional) dbID to set set
00176   Description: Gets/sets internal genome_id used as database primary key
00177   Returntype : dbID string
00178   Exceptions : none
00179   Caller     : Bio::EnsEMBL::MetaData::DBSQL::GenomeInfoAdaptor
00180   Status     : Stable
00181 =cut
00182 
00183 sub dbID {
00184   my ( $self, $id ) = @_;
00185   $self->{dbID} = $id if ( defined $id );
00186   return $self->{dbID};
00187 }
00188 
00189 =head2 adaptor
00190   Arg        : (optional) adaptor to set set
00191   Description: Gets/sets GenomeInfoAdaptor
00192   Returntype : Bio::EnsEMBL::MetaData::DBSQL::GenomeInfoAdaptor
00193   Exceptions : none
00194   Caller     : Internal
00195   Status     : Stable
00196 =cut
00197 
00198 sub adaptor {
00199   my ( $self, $adaptor ) = @_;
00200   $self->{adaptor} = $adaptor if ( defined $adaptor );
00201   return $self->{adaptor};
00202 }
00203 
00204 =head2 _preload
00205   Description: Ensure all children are loaded (used for hash transformation)
00206   Returntype : none
00207   Exceptions : none
00208   Caller     : Internal
00209   Status     : Stable
00210 =cut
00211 
00212 sub _preload {
00213   my ($self) = @_;
00214   return;
00215 }
00216 
00217 =head2 _preload
00218   Description: Remove all children (used after hash transformation to ensure object is minimised)
00219   Returntype : none
00220   Exceptions : none
00221   Caller     : dump_metadata.pl
00222   Status     : Stable
00223 =cut
00224 
00225 sub _unload {
00226   my ($self) = @_;
00227   return;
00228 }
00229 
00230 =head2 _load_child
00231   Description: Lazy load method for loading children from database if not initialised
00232   Arg        : Key to find child data in object hash
00233   Arg        : Method for loading child data from adaptor()
00234   Returntype : none
00235   Exceptions : none
00236   Caller     : Internal
00237   Status     : Stable
00238 =cut
00239 sub _load_child {
00240   my ( $self, $key, $method ) = @_;
00241   if ( !defined $self->{$key} &&
00242        defined $self->dbID() &&
00243        defined $self->adaptor() )
00244   {
00245     $self->adaptor()->$method($self);
00246   }
00247   return;
00248 }
00249 
00250 1;