<commit_before>=head1 LICENSE

Copyright [1999-2013] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

=cut

package EnsEMBL::REST::Builder::TreeHash;

use Moose;
use namespace::autoclean;
use Bio::EnsEMBL::Utils::Scalar qw(check_ref);

has 'aligned'       => ( isa => 'Bool', is => 'rw', default => 0);
has 'cdna'          => ( isa => 'Bool', is => 'rw', default => 0);
has 'no_sequences'  => ( isa => 'Bool', is => 'rw', default => 0);
has 'source'        => ( isa => 'Str', is => 'rw', default => 'ensembl');
has 'type'          => ( isa => 'Str', is => 'rw', default => 'gene tree');

sub convert {
  my ($self, $tree) = @_;
  
  return $self->_head_node($tree);
}

sub _head_node {
  my ($self, $tree) = @_;
  my $hash = {
    type => $self->type(),
    rooted => 1,
  };

  if($tree->can('stable_id')) {
    $hash->{id} = $tree->stable_id();
  }

  $hash->{tree} = 
    $self->_recursive_conversion($tree->root());

  return $hash;
}

sub _recursive_conversion {
  my ($self, $tree) = @_;;
  my $new_hash = $self->_convert_node($tree);
  if($tree->get_child_count()) {
    my @converted_children;
    foreach my $child (@{$tree->sorted_children()}) {
      my $converted_child = $self->_recursive_conversion($child);
      push(@converted_children, $converted_child);
    }
    $new_hash->{children} = \@converted_children;
  }
  return $new_hash;
}

sub _convert_node {
  my ($self, $node) = @_;
  my $hash;

  my $type  = $node->get_tagvalue('node_type');
  my $boot  = $node->get_tagvalue('bootstrap');
  my $taxid = $node->get_tagvalue('taxon_id');
  my $tax   = $node->get_tagvalue('taxon_name');

  $hash->{branch_length} = $node->distance_to_parent() + 0;
  if($taxid) {
    $hash->{taxonomy} = { id => $taxid + 0, scientific_name => $tax };
  }
  if($boot) {
    $hash->{confidence} = { type => "boostrap", value => $boot + 0 };
  }
  if($type) { # && $type ~~ [qw/duplication dubious/]) {
    $hash->{events} = { type => $type };
  }
  
  if(check_ref($node, 'Bio::EnsEMBL::Compara::GeneTreeMember')) {
    my $gene = $node->gene_member();

    $hash->{id} = { source => "EnsEMBL", accession => $gene->stable_id() };

    my $genome_db = $node->genome_db();
    $taxid = $genome_db->taxon_id();
    $hash->{taxonomy} = 
      { id => $taxid + 0, scientific_name => $genome_db->taxon->scientific_name() }
	if $taxid;<commit_msg>Compara v74: the taxon_id and taxon_name tags are deprecated, we should use the species_tree_node() call instead<commit_after>=head1 LICENSE

Copyright [1999-2013] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

=cut

package EnsEMBL::REST::Builder::TreeHash;

use Moose;
use namespace::autoclean;
use Bio::EnsEMBL::Utils::Scalar qw(check_ref);

has 'aligned'       => ( isa => 'Bool', is => 'rw', default => 0);
has 'cdna'          => ( isa => 'Bool', is => 'rw', default => 0);
has 'no_sequences'  => ( isa => 'Bool', is => 'rw', default => 0);
has 'source'        => ( isa => 'Str', is => 'rw', default => 'ensembl');
has 'type'          => ( isa => 'Str', is => 'rw', default => 'gene tree');

sub convert {
  my ($self, $tree) = @_;
  
  return $self->_head_node($tree);
}

sub _head_node {
  my ($self, $tree) = @_;
  my $hash = {
    type => $self->type(),
    rooted => 1,
  };

  if($tree->can('stable_id')) {
    $hash->{id} = $tree->stable_id();
  }

  $hash->{tree} = 
    $self->_recursive_conversion($tree->root());

  return $hash;
}

sub _recursive_conversion {
  my ($self, $tree) = @_;;
  my $new_hash = $self->_convert_node($tree);
  if($tree->get_child_count()) {
    my @converted_children;
    foreach my $child (@{$tree->sorted_children()}) {
      my $converted_child = $self->_recursive_conversion($child);
      push(@converted_children, $converted_child);
    }
    $new_hash->{children} = \@converted_children;
  }
  return $new_hash;
}

sub _convert_node {
  my ($self, $node) = @_;
  my $hash;

  my $type  = $node->get_tagvalue('node_type');
  my $boot  = $node->get_tagvalue('bootstrap');
  my $tax   = $node->species_tree_node();

  $hash->{branch_length} = $node->distance_to_parent() + 0;
  if($tax) {
    $hash->{taxonomy} = { id => $tax->taxon_id + 0, scientific_name => $tax->node_name };
  }
  if($boot) {
    $hash->{confidence} = { type => "boostrap", value => $boot + 0 };
  }
  if($type) { # && $type ~~ [qw/duplication dubious/]) {
    $hash->{events} = { type => $type };
  }
  
  if(check_ref($node, 'Bio::EnsEMBL::Compara::GeneTreeMember')) {
    my $gene = $node->gene_member();

    $hash->{id} = { source => "EnsEMBL", accession => $gene->stable_id() };

    my $genome_db = $node->genome_db();
    my $taxid = $genome_db->taxon_id();
    $hash->{taxonomy} = 
      { id => $taxid + 0, scientific_name => $genome_db->taxon->scientific_name() }
	if $taxid;
{'fraction_non_alphanumeric': 0.1798327930387306, 'fraction_numerical': 0.007507251322299948, 'mean_word_length': 3.7350565428109856, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 126, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '653848', 'n_tokens_mistral': 2136, 'n_tokens_neox': 2013, 'n_words': 526}