Archive Ensembl HomeArchive Ensembl Home
ObjectFactory.pm
Go to the documentation of this file.
00001 
00002 =pod 
00003 
00004 =head1 NAME
00005 
00006 Bio::EnsEMBL::Compara::RunnableDB::ObjectFactory
00007 
00008 =head1 DESCRIPTION
00009 
00010 This is a Compara-specific generic job factory that:
00011     1) creates an object adaptor
00012     2) runs a fetch_all_by... method on that adaptor (which returns a list of objects)
00013     3) flows each object into a job using a given getter-to-column mapping
00014 
00015 It also serves as a good simple example of a Compara job factory:
00016     1) Inherits main factory functionality from Hive::RunnableDB::JobFactory
00017     2) Inherits Compara-specific functionality (like $self->compara_dba ) from Compara::RunnableDB::BaseRunnable
00018     3) Only defines fetch_input() method that takes specific parameters, uses API to fetch data and sets the $self->param('inputlist') in the end.
00019 
00020 =cut
00021 
00022 package Bio::EnsEMBL::Compara::RunnableDB::ObjectFactory;
00023 
00024 use strict;
00025 use Bio::EnsEMBL::Hive::Utils 'stringify';
00026 
00027     # Note: the order is important, this is a true example of Multiple Inheritance:
00028 use base ('Bio::EnsEMBL::Hive::RunnableDB::JobFactory', 'Bio::EnsEMBL::Compara::RunnableDB::BaseRunnable');
00029 
00030 
00031 sub fetch_input {
00032     my $self = shift @_;
00033 
00034     my $adaptor_name            = $self->param('adaptor_name')   or die "'adaptor_name' is an obligatory parameter";
00035     my $adaptor_method          = $self->param('adaptor_method') or die "'adaptor_method' is an obligatory parameter";
00036 
00037     my $method_param_list       = $self->param_substitute( $self->param('method_param_list') || [] );
00038 
00039     my $get_adaptor             = 'get_'.$adaptor_name;
00040     my $adaptor                 = $self->compara_dba->$get_adaptor or die "Could not create a '$adaptor_name' on the database";
00041     my $adaptor_method_result   = $adaptor->$adaptor_method( @$method_param_list ) or die "Could not call '$adaptor_method'(".stringify(@$method_param_list).") on '$adaptor_name'";
00042 
00043     my $object_method           = $self->param('object_method');
00044 
00045     my $object_method_result    = $object_method
00046                                     ? $adaptor_method_result->$object_method()
00047                                     : $adaptor_method_result;
00048 
00049     if(my $column_names2getters = $self->param_substitute( $self->param('column_names2getters') ) ) {
00050 
00051         my @getters             = values %$column_names2getters;
00052         my @inputlist           = ();
00053 
00054         foreach my $object (@$object_method_result) {
00055             push @inputlist, [ map { $object->$_ } @getters ];
00056         }
00057         $self->param('column_names', [ keys %$column_names2getters ] );
00058         $self->param('inputlist',    \@inputlist);
00059 
00060     } elsif( ref($object_method_result) eq 'ARRAY') {   # caller should set 'column_names' for better results
00061 
00062         $self->param('inputlist', $object_method_result);
00063 
00064     } else {
00065 
00066         die "Expected an arrayref instead of ".stringify($object_method_result);
00067     }
00068 }
00069 
00070 1;