<commit_before>=head1 LICENSE

Copyright [2014-2017] 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::Web::Component::Gene::ComparaOrthologs;

use Data::Dumper;

sub _species_sets {
## Group species into sets - separate method so it can be pluggable easily
  my ($self, $orthologue_list, $skipped) = @_;

  my $species_defs  = $self->hub->species_defs;

  my $set_order;
  my $is_pan = $self->hub->function eq 'pan_compara';
  if($is_pan){
    $set_order = [qw(all ensembl metazoa plants fungi protists bacteria archaea)];
  }


  my $categories = {};
  my $species_sets = {
    'ensembl'     => {'title' => 'Vertebrates', 'desc' => '', 'species' =>[]},
    'metazoa'     => {'title' => 'Metazoa', 'desc'=>'', 'species'=>[]},
    'plants'      => {'title' => 'Plants', 'desc' => '', 'species' => []},
    'fungi'       => {'title' => 'Fungi', 'desc' => '', 'species' => []},
    'protists'    => {'title' => 'Protists', 'desc' => '', 'species' => []},
    'bacteria'    => {'title' => 'Bacteria', 'desc' => '', 'species' => []},
    'archaea'     => {'title' => 'Archaea', 'desc' => '', 'species' => []},
    'all'       =>   {'title' => 'All', 'desc' => '', 'species' => []},
  };

  my $sets_by_species = {};

  my $spsites =  $species_defs->ENSEMBL_SPECIES_SITE();
  foreach my $species (keys %$orthologue_list) {
    next if $skipped->{$species};
    my $group = $spsites->{lc($species)};
    if($group eq 'bacteria'){
      if($self->is_archaea(lc $species)){
        $group='archaea';
      }
    }
    elsif (!$is_pan){ # not the pan compara page - generate groups
      $group = $species_defs->COMPARA_SPECIES_SET->{lc($species)} || $species_defs->get_config($species, 'SPECIES_GROUP') || 'all';
      if(!exists $species_sets->{$group}){
        $species_sets->{$group} = {'title'=>ucfirst $group,'species'=>[]};
        push(@$set_order,$group);
      }
    }

    push (@{$species_sets->{'all'}{'species'}}, $species);
    my $sets = [];

    my $orthologues = $orthologue_list->{$species} || {};
    foreach my $stable_id (keys %$orthologues) {
      my $orth_info = $orthologue_list->{$species}{$stable_id};
      my $orth_desc = $orth_info->{'homology_desc'};
      $species_sets->{'all'}{$orth_desc}++;
      $species_sets->{$group}{$orth_desc}++;
      $categories->{$orth_desc} = {key=>$orth_desc, title=>$orth_desc} unless exists $categories->{$orth_desc};
    }
    push(@{$species_sets->{$group}{'species'}},$species);
    push (@$sets, $group) if(exists $species_sets->{$group});
    $sets_by_species->{$species} = $sets;
  }
  
  $set_order = $species_defs->COMPARA_ORDER;
  
  return ($species_sets, $sets_by_species, $set_order, $categories);
}

sub content {
  my $self         = shift;
  my $hub          = $self->hub;
  my $object       = $self->object;
  my $species_defs = $hub->species_defs;
  my $cdb          = shift || $hub->param('cdb') || 'compara';
  my $availability = $object->availability;
  
  my @orthologues = (
    $object->get_homology_matches('ENSEMBL_ORTHOLOGUES', undef, undef, $cdb), 
    $object->get_homology_matches('ENSEMBL_PARALOGUES', 'possible_ortholog', undef, $cdb)
  );
  
  my %orthologue_list;
  my %skipped;
  
  foreach my $homology_type (@orthologues) {
    foreach (keys %$homology_type) {
      (my $species = $_) =~ tr/ /_/;
      foreach my $foo (keys %{$homology_type->{$_}}){
      }
      $orthologue_list{$species} = {%{$orthologue_list{$species}||{}}, %{$homology_type->{$_}}};
      $skipped{$species}        += keys %{$homology_type->{$_}} if $hub->param('species_' . lc $species) eq 'off';
    }
  }
  
  return '<p>No orthologues have been identified for this gene</p>' unless keys %orthologue_list;
  
  my %orthologue_map;
  my $alignview      = 0;
 
  my ($html, $columns, @rows);

  ##--------------------------- SUMMARY TABLE ----------------------------------------

  my ($species_sets, $sets_by_species, $set_order, $categories) = $self->_species_sets(\%orthologue_list, \%skipped, \%orthologue_map);

  if ($species_sets) {
    $html .= qq{
      <h3>Summary of orthologues of this gene</h3>
      <p class="space-below">Click on 'Show' to display the orthologues for one or more groups, or click on 'Configure this page' to choose a custom list of species</p>
    };
 
    $columns = [
      { key => 'set',       title => 'Species set',    align => 'left',     },
      { key => 'show',      title => 'Show details',   align => 'center',   },
    ];
    foreach my $orth_desc (sort keys %$categories){
      push( @$columns, $categories->{$orth_desc} );
    }
    my $width = sprintf("%d%", 100/@$columns);
    $_->{'width'}=$width for @$columns;

    foreach my $set (@$set_order) {
      my $set_info = $species_sets->{$set};
      
      my $row_data = {};
      my $title = $species_defs->COMPARA_DISPLAY_NAME->{lc($set_info->{'title'})} || $set_info->{'title'};
      $row_data->{'set'} = "<strong>$title</strong><br />$set_info->{'desc'}";
      if(@{$set_info->{'species'}} && $set_info->{'title'} ne 'All'){
        $row_data->{'show'} =  qq{<input type="checkbox" class="table_filter" title="Check to show these species in table below" name="orthologues" value="$set" />};
      }
      my $skip = 1;
      foreach my $orth_desc (%$categories){
        $row_data->{$orth_desc} = $set_info->{$orth_desc} || 0;
        $skip = 0 if $row_data->{$orth_desc} > 0;
      }
      next if $skip == 1;
      push @rows, $row_data;
    }
    
    $html .= $self->new_table($columns, \@rows)->render;
  }
<commit_msg>PARASITE-361 fixing show details checkbox<commit_after>=head1 LICENSE

Copyright [2014-2017] 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::Web::Component::Gene::ComparaOrthologs;

use Data::Dumper;

sub _species_sets {
## Group species into sets - separate method so it can be pluggable easily
  my ($self, $orthologue_list, $skipped) = @_;

  my $species_defs  = $self->hub->species_defs;

  my $set_order;
  my $is_pan = $self->hub->function eq 'pan_compara';
  if($is_pan){
    $set_order = [qw(all ensembl metazoa plants fungi protists bacteria archaea)];
  }


  my $categories = {};
  my $species_sets = {
    'ensembl'     => {'title' => 'Vertebrates', 'desc' => '', 'species' =>[]},
    'metazoa'     => {'title' => 'Metazoa', 'desc'=>'', 'species'=>[]},
    'plants'      => {'title' => 'Plants', 'desc' => '', 'species' => []},
    'fungi'       => {'title' => 'Fungi', 'desc' => '', 'species' => []},
    'protists'    => {'title' => 'Protists', 'desc' => '', 'species' => []},
    'bacteria'    => {'title' => 'Bacteria', 'desc' => '', 'species' => []},
    'archaea'     => {'title' => 'Archaea', 'desc' => '', 'species' => []},
    'all'       =>   {'title' => 'All', 'desc' => '', 'species' => []},
  };

  my $sets_by_species = {};

  my $spsites =  $species_defs->ENSEMBL_SPECIES_SITE();
  foreach my $species (keys %$orthologue_list) {
    next if $skipped->{$species};
    my $group = $spsites->{lc($species)};
    if($group eq 'bacteria'){
      if($self->is_archaea(lc $species)){
        $group='archaea';
      }
    }
    elsif (!$is_pan){ # not the pan compara page - generate groups
      $group = $species_defs->COMPARA_SPECIES_SET->{lc($species)} || $species_defs->get_config($species, 'SPECIES_GROUP') || 'all';
      if(!exists $species_sets->{$group}){
        $species_sets->{$group} = {'title'=>ucfirst $group,'species'=>[]};
        push(@$set_order,$group);
      }
    }

    push (@{$species_sets->{'all'}{'species'}}, $species);
    my $sets = [];

    my $orthologues = $orthologue_list->{$species} || {};
    foreach my $stable_id (keys %$orthologues) {
      my $orth_info = $orthologue_list->{$species}{$stable_id};
      my $orth_desc = $orth_info->{'homology_desc'};
      $species_sets->{'all'}{$orth_desc}++;
      $species_sets->{$group}{$orth_desc}++;
      $categories->{$orth_desc} = {key=>$orth_desc, title=>$orth_desc} unless exists $categories->{$orth_desc};
    }
    push(@{$species_sets->{$group}{'species'}},$species);
    push (@$sets, $group) if(exists $species_sets->{$group});
    $sets_by_species->{$species} = $sets;
  }
  
  $set_order = $species_defs->COMPARA_ORDER;
  
  return ($species_sets, $sets_by_species, $set_order, $categories);
}

sub content {
  my $self         = shift;
  my $hub          = $self->hub;
  my $object       = $self->object;
  my $species_defs = $hub->species_defs;
  my $cdb          = shift || $hub->param('cdb') || 'compara';
  my $availability = $object->availability;
  
  my @orthologues = (
    $object->get_homology_matches('ENSEMBL_ORTHOLOGUES', undef, undef, $cdb), 
    $object->get_homology_matches('ENSEMBL_PARALOGUES', 'possible_ortholog', undef, $cdb)
  );
  
  my %orthologue_list;
  my %skipped;
  
  foreach my $homology_type (@orthologues) {
    foreach (keys %$homology_type) {
      (my $species = $_) =~ tr/ /_/;
      foreach my $foo (keys %{$homology_type->{$_}}){
      }
      $orthologue_list{$species} = {%{$orthologue_list{$species}||{}}, %{$homology_type->{$_}}};
      $skipped{$species}        += keys %{$homology_type->{$_}} if $hub->param('species_' . lc $species) eq 'off';
    }
  }
  
  return '<p>No orthologues have been identified for this gene</p>' unless keys %orthologue_list;
  
  my %orthologue_map;
  my $alignview      = 0;
 
  my ($html, $columns, @rows);

  ##--------------------------- SUMMARY TABLE ----------------------------------------

  my ($species_sets, $sets_by_species, $set_order, $categories) = $self->_species_sets(\%orthologue_list, \%skipped, \%orthologue_map);

  if ($species_sets) {
    $html .= qq{
      <h3>Summary of orthologues of this gene</h3>
      <p class="space-below">Click on 'Show' to display the orthologues for one or more groups, or click on 'Configure this page' to choose a custom list of species</p>
    };
 
    $columns = [
      { key => 'set',       title => 'Species set',    align => 'left',     },
      { key => 'show',      title => 'Show details',   align => 'center',   },
    ];
    foreach my $orth_desc (sort keys %$categories){
      push( @$columns, $categories->{$orth_desc} );
    }
    my $width = sprintf("%d%", 100/@$columns);
    $_->{'width'}=$width for @$columns;

    foreach my $set (@$set_order) {
      my $set_info = $species_sets->{$set};
      my $species_selected = $set eq 'all' ? 'checked="checked"' : ''; # select all species by default
 
      my $row_data = {};
      my $title = $species_defs->COMPARA_DISPLAY_NAME->{lc($set_info->{'title'})} || $set_info->{'title'};
      $row_data->{'set'} = "<strong>$title</strong><br />$set_info->{'desc'}";
      if(@{$set_info->{'species'}}){
        $row_data->{'show'} =  qq{<input type="checkbox" class="table_filter" title="Check to show these species in table below" name="orthologues" value="$set" $species_selected />};
      }
      my $skip = 1;
      foreach my $orth_desc (%$categories){
        $row_data->{$orth_desc} = $set_info->{$orth_desc} || 0;
        $skip = 0 if $row_data->{$orth_desc} > 0;
      }
      next if $skip == 1;
      push @rows, $row_data;
    }
    
    $html .= $self->new_table($columns, \@rows)->render;
  }

{'fraction_non_alphanumeric': 0.2001640016400164, 'fraction_numerical': 0.004182041820418204, 'mean_word_length': 3.292854628651883, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 216, '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': '4507342', 'n_tokens_mistral': 4382, 'n_tokens_neox': 4117, 'n_words': 965}