<commit_before>=pod 

=head1 NAME

Bio::EnsEMBL::Compara::RunnableDB::GeneTrees::ReconcileTree

=cut

=head1 DESCRIPTION

This Runnable will run treebest on a tree to reconcile it with
the species tree. Information about lost taxa and speciation /
duplication events are then stored.
The tree topology is not changed.

This module is currently not used in any pipeline.

=cut

=head1 CONTACT

  Please email comments or questions to the public Ensembl
  developers list at <<EMAIL>>.

  Questions may also be sent to the Ensembl help desk at
  <<EMAIL>>.

=cut


package Bio::EnsEMBL::Compara::RunnableDB::GeneTrees::ReconcileTree;

use strict;

use Data::Dumper;

use Bio::EnsEMBL::Compara::Graph::NewickParser;

use base ('Bio::EnsEMBL::Compara::RunnableDB::BaseRunnable');


###########################
# eHive inherited methods #
###########################

sub param_defaults {
    return {
        # Which tags are we updating ?
        'store_lost_taxon_id'   => 1,
        'store_node_type'       => 1,
    }
}


sub fetch_input {
    my $self = shift @_;

    my $protein_tree_id = $self->param('protein_tree_id') or die "'protein_tree_id' is an obligatory parameter";
    my $protein_tree    = $self->compara_dba->get_GeneTreeAdaptor->fetch_by_dbID( $protein_tree_id )
                                        or die "Could not fetch protein_tree with protein_tree_id='$protein_tree_id'";
    $protein_tree->preload();
    $protein_tree->print_tree(10) if $self->debug;

    $self->param('protein_tree', $protein_tree->root);
}

sub run {
    my $self = shift;

    $self->run_treebest_sdi;
    $self->map_nodes('protein_tree', 'ini_nodes');
    $self->map_nodes('new_tree', 'new_nodes');
}

sub write_output {
    my $self = shift;

    $self->write_tags();
}

sub post_cleanup {
    my $self = shift;

    $self->param('protein_tree')->release_tree;
    $self->param('new_tree')->release_tree;
}


####################
# Internal methods #
####################

sub run_treebest_sdi {
    my $self = shift;

    my $tree = $self->param('protein_tree');
    my $tree_str = $tree->newick_format('member_id_taxon_id');
    my $species_tree_file = $self->get_species_tree_file();

    $self->compara_dba->dbc->disconnect_when_inactive(1);

    my $treebest_exe = $self->param('treebest_exe') or die "'treebest_exe' is an obligatory parameter";
    die "Cannot execute '$treebest_exe'" unless(-x $treebest_exe);

    my $basedir = $self->worker_temp_directory;
    my $treefile_in = $basedir.'tree.in';
    open(OUTGENETREE, ">$treefile_in");
    print OUTGENETREE $tree_str;
    close OUTGENETREE;
    my $treefile_out = $basedir.'tree.out';

    my $cmd = sprintf('%s sdi -s %s %s > %s', $treebest_exe, $species_tree_file, $treefile_in, $treefile_out);

    my $full_cmd = defined $basedir ? "cd $basedir; $cmd" : $cmd;
    print STDERR "Running:\n\t$full_cmd\n" if $self->debug;

    if(my $rc = system($full_cmd)) {<commit_msg>gene_tree_id is used instead of protein_tree_id<commit_after>=pod 

=head1 NAME

Bio::EnsEMBL::Compara::RunnableDB::GeneTrees::ReconcileTree

=cut

=head1 DESCRIPTION

This Runnable will run treebest on a tree to reconcile it with
the species tree. Information about lost taxa and speciation /
duplication events are then stored.
The tree topology is not changed.

This module is currently not used in any pipeline.

=cut

=head1 CONTACT

  Please email comments or questions to the public Ensembl
  developers list at <<EMAIL>>.

  Questions may also be sent to the Ensembl help desk at
  <<EMAIL>>.

=cut


package Bio::EnsEMBL::Compara::RunnableDB::GeneTrees::ReconcileTree;

use strict;

use Data::Dumper;

use Bio::EnsEMBL::Compara::Graph::NewickParser;

use base ('Bio::EnsEMBL::Compara::RunnableDB::BaseRunnable');


###########################
# eHive inherited methods #
###########################

sub param_defaults {
    return {
        # Which tags are we updating ?
        'store_lost_taxon_id'   => 1,
        'store_node_type'       => 1,
    }
}


sub fetch_input {
    my $self = shift @_;

    my $gene_tree_id = $self->param('gene_tree_id') or die "'gene_tree_id' is an obligatory parameter";
    my $gene_tree    = $self->compara_dba->get_GeneTreeAdaptor->fetch_by_dbID( $gene_tree_id )
                                        or die "Could not fetch gene_tree with gene_tree_id='$gene_tree_id'";
    $gene_tree->preload();
    $gene_tree->print_tree(10) if $self->debug;

    $self->param('gene_tree', $gene_tree->root);
}

sub run {
    my $self = shift;

    $self->run_treebest_sdi;
    $self->map_nodes('gene_tree', 'ini_nodes');
    $self->map_nodes('new_tree', 'new_nodes');
}

sub write_output {
    my $self = shift;

    $self->write_tags();
}

sub post_cleanup {
    my $self = shift;

    $self->param('gene_tree')->release_tree;
    $self->param('new_tree')->release_tree;
}


####################
# Internal methods #
####################

sub run_treebest_sdi {
    my $self = shift;

    my $tree = $self->param('gene_tree');
    my $tree_str = $tree->newick_format('member_id_taxon_id');
    my $species_tree_file = $self->get_species_tree_file();

    $self->compara_dba->dbc->disconnect_when_inactive(1);

    my $treebest_exe = $self->param('treebest_exe') or die "'treebest_exe' is an obligatory parameter";
    die "Cannot execute '$treebest_exe'" unless(-x $treebest_exe);

    my $basedir = $self->worker_temp_directory;
    my $treefile_in = $basedir.'tree.in';
    open(OUTGENETREE, ">$treefile_in");
    print OUTGENETREE $tree_str;
    close OUTGENETREE;
    my $treefile_out = $basedir.'tree.out';

    my $cmd = sprintf('%s sdi -s %s %s > %s', $treebest_exe, $species_tree_file, $treefile_in, $treefile_out);

    my $full_cmd = defined $basedir ? "cd $basedir; $cmd" : $cmd;
    print STDERR "Running:\n\t$full_cmd\n" if $self->debug;

    if(my $rc = system($full_cmd)) {
{'fraction_non_alphanumeric': 0.16027280477408354, 'fraction_numerical': 0.0027280477408354645, 'mean_word_length': 3.9796264855687604, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7320881', 'n_tokens_mistral': 2125, 'n_tokens_neox': 1999, 'n_words': 479}