Archive Ensembl HomeArchive Ensembl Home
MakeSpeciesTree.pm
Go to the documentation of this file.
00001 
00002 =pod 
00003 
00004 =head1 NAME
00005 
00006     Bio::EnsEMBL::Compara::RunnableDB::MakeSpeciesTree
00007 
00008 =head1 SYNOPSIS
00009 
00010             # a configuration example:
00011         {   -logic_name    => 'make_species_tree',
00012             -module        => 'Bio::EnsEMBL::Compara::RunnableDB::MakeSpeciesTree',
00013             -parameters    => { },
00014             -input_ids     => [
00015                 { 'species_tree_input_file' => $self->o('species_tree_input_file') },   # if this parameter is set, the tree will be taken from the file, otherwise it will be generated
00016             ],
00017             -flow_into  => {
00018                 3 => { 'mysql:////meta' => { 'meta_key' => 'test_species_tree', 'meta_value' => '#species_tree_string#' } },    # store the tree in 'meta' table (as an example)
00019             },
00020         },
00021 
00022 =head1 DESCRIPTION
00023 
00024     This module is supposed to be a cleaner way of creating species trees in Newick string format needed by various pipelines.
00025 
00026 =cut
00027 
00028 
00029 package Bio::EnsEMBL::Compara::RunnableDB::MakeSpeciesTree;
00030 
00031 use strict;
00032 use Bio::EnsEMBL::Compara::Graph::NewickParser;
00033 
00034 use base ('Bio::EnsEMBL::Compara::RunnableDB::BaseRunnable');
00035 
00036 
00037 sub param_defaults {
00038     return {
00039             'newick_format'         => 'njtree',    # the desired output format
00040     };
00041 }
00042 
00043 
00044 sub fetch_input {
00045     my $self = shift @_;
00046 
00047     return if($self->param('species_tree_string'));     # skip the functionality if the tree has been provided
00048 
00049     my $species_tree_string;
00050 
00051     if(my $species_tree_input_file = $self->param('species_tree_input_file')) {     # load the tree given from a file
00052         die "The file '$species_tree_input_file' cannot be open for reading" unless(-r $species_tree_input_file);
00053 
00054         $species_tree_string = `cat $species_tree_input_file`;
00055         chomp $species_tree_string;
00056 
00057     } else {    # generate the tree from the database+params
00058 
00059         my @tree_creation_args = ();
00060 
00061         foreach my $config_param
00062                 (qw(no_previous species_set_id extrataxon_sequenced extrataxon_incomplete multifurcation_deletes_node multifurcation_deletes_all_subnodes)) {
00063 
00064             if(defined(my $config_value = $self->param($config_param))) {
00065                 push @tree_creation_args, ("-$config_param", $config_value);
00066             }
00067         }
00068 
00069         my $species_tree;
00070         if(my $blength_tree_file = $self->param('blength_tree_file')) {     # defines the mode
00071 
00072             my $blength_tree = Bio::EnsEMBL::Compara::Graph::NewickParser::parse_newick_into_tree( `cat $blength_tree_file` );
00073             $species_tree  = $self->compara_dba()->get_SpeciesTreeAdaptor()->prune_tree( $blength_tree );
00074 
00075         } else {
00076 
00077             $species_tree = $self->compara_dba()->get_SpeciesTreeAdaptor()->create_species_tree( @tree_creation_args );
00078         }
00079         
00080         my $newick_format   = $self->param('newick_format');
00081         $species_tree_string = $species_tree->newick_format( $newick_format );
00082     }
00083 
00084     $self->param('species_tree_string', $species_tree_string);
00085 }
00086 
00087 
00088 sub write_output {
00089     my $self = shift @_;
00090 
00091     my $species_tree_string = $self->param('species_tree_string');
00092     my $output_branch = $self->param('blength_tree_file') ? 4 : 3;
00093 
00094     $self->dataflow_output_id( { 'species_tree_string'   => $species_tree_string }, $output_branch);
00095 }
00096 
00097 1;
00098