<commit_before>=head1 LICENSE

Copyright [1999-2015] 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::Web::Component::Transcript::Haplotypes;

use strict;
use warnings;
no warnings 'uninitialized';

use Time::HiRes qw(tv_interval gettimeofday);

use Bio::EnsEMBL::Variation::TranscriptHaplotypeContainer;

use base qw(EnsEMBL::Web::Component::Transcript);

sub _init {
  my $self = shift;
  $self->cacheable(1);
  $self->ajaxable(1);
}

sub content {
  my $self = shift;
  my $object = $self->object;
  
  my $html = '';
  
  # tell JS what panel type this is
  $html .= '<input type="hidden" class="panel_type" value="TranscriptHaplotypes" />';

  my $t0 = [gettimeofday];
  
  my $c = $self->get_haplotypes;
  
  print STDERR "HAPLOTYPES ".tv_interval($t0, [gettimeofday])."\n";
  
  my $table = $self->new_table(
    [], [], {
      data_table => 1,
      download_table => 1,
      sorting => [ 'freq desc' ],
      data_table_config => {iDisplayLength => 10}, 
    }
  );
  
  my $total_counts = $c->total_population_counts();
  
  my $pop_objs = $self->population_objects($total_counts);
  my $pop_struct = $self->population_structure;
  
  my %pop_descs = map {$_->name => $_->description} @$pop_objs;
  
  my @pop_cols =
    map {{
      key => $self->short_population_name($_),
      title => $self->short_population_name($_),
      sort => 'numeric',
      help => sprintf('<b>%s: </b>%s', $_, $pop_descs{$_})
    }}
    sort keys %$pop_struct;

  $table->add_columns(
    { key => 'protein', title => 'Protein haplotype', sort => 'none',    help => 'Haplotype names represent a comma-separated list of differences to the reference sequence'},
    { key => 'freq',    title => 'Frequency',         sort => 'numeric', help => 'Combined frequency across all samples'},
    { key => 'cds',     title => 'CDS haplotype(s)',  sort => 'none',    help => 'Haplotype names represent a comma-separated list of differences to the reference sequence'},
    # { key => 'count',       title => 'Count',             sort => 'numeric' },
    @pop_cols,
    { key => 'extra',   title => '',                  sort => 'none' }
  );
  
  my @rows;
  
  foreach my $ph(@{$c->get_all_ProteinHaplotypes}) {    
    $table->add_row($self->render_protein_haplotype_row($ph));
  }
  
  print STDERR "TOTAL ".tv_interval($t0, [gettimeofday])."\n";
  
  $html .= $table->render;

  return $html;
}

sub get_haplotypes {
  my $self = shift;
  
  my $tr = $self->object->Obj;
  
  my $variation_db = $tr->adaptor->db->get_db_adaptor('variation');
  
  # find VCF config
  my $c = $self->object->species_defs->multi_val('ENSEMBL_VCF_COLLECTIONS');

  if($c) {
   # set config file via ENV variable
   $ENV{ENSEMBL_VARIATION_VCF_CONFIG_FILE} = $c->{'CONFIG'};
   $variation_db->use_vcf($c->{'ENABLED'}) if $variation_db->can('use_vcf');
  }
  
  my $vca = $variation_db->get_VCFCollectionAdaptor();
  
  # my @gts = map {@{$_->get_all_IndividualGenotypeFeatures_by_Slice($tr->feature_Slice, undef, 1)}} @{$vca->fetch_all};
  
  my @gts;

  my $t0 = [gettimeofday];

  # we don't want variants in introns
  foreach my $exon(@{$tr->get_all_Exons}) {
    push @gts, map {@{$_->get_all_SampleGenotypeFeatures_by_Slice($exon->feature_Slice, undef, 1)}} @{$vca->fetch_all};
  }
  
  print STDERR "GENOTYPES ".tv_interval($t0, [gettimeofday])."\n";
  
  print STDERR "Fetched ".(scalar @gts)." GTs\n";
  
  return Bio::EnsEMBL::Variation::TranscriptHaplotypeContainer->new($tr, \@gts, $variation_db);
}

sub short_population_name {
  my $self = shift;
  my $name = shift;
  
  my $short = $name;
  $short =~ s/1000GENOMES:phase_3://i;
  
  return $short;
}

sub population_structure {<commit_msg>update to modified new method<commit_after>=head1 LICENSE

Copyright [1999-2015] 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::Web::Component::Transcript::Haplotypes;

use strict;
use warnings;
no warnings 'uninitialized';

use Time::HiRes qw(tv_interval gettimeofday);

use Bio::EnsEMBL::Variation::TranscriptHaplotypeContainer;

use base qw(EnsEMBL::Web::Component::Transcript);

sub _init {
  my $self = shift;
  $self->cacheable(1);
  $self->ajaxable(1);
}

sub content {
  my $self = shift;
  my $object = $self->object;
  
  my $html = '';
  
  # tell JS what panel type this is
  $html .= '<input type="hidden" class="panel_type" value="TranscriptHaplotypes" />';

  my $t0 = [gettimeofday];
  
  my $c = $self->get_haplotypes;
  
  print STDERR "HAPLOTYPES ".tv_interval($t0, [gettimeofday])."\n";
  
  my $table = $self->new_table(
    [], [], {
      data_table => 1,
      download_table => 1,
      sorting => [ 'freq desc' ],
      data_table_config => {iDisplayLength => 10}, 
    }
  );
  
  my $total_counts = $c->total_population_counts();
  
  my $pop_objs = $self->population_objects($total_counts);
  my $pop_struct = $self->population_structure;
  
  my %pop_descs = map {$_->name => $_->description} @$pop_objs;
  
  my @pop_cols =
    map {{
      key => $self->short_population_name($_),
      title => $self->short_population_name($_),
      sort => 'numeric',
      help => sprintf('<b>%s: </b>%s', $_, $pop_descs{$_})
    }}
    sort keys %$pop_struct;

  $table->add_columns(
    { key => 'protein', title => 'Protein haplotype', sort => 'none',    help => 'Haplotype names represent a comma-separated list of differences to the reference sequence'},
    { key => 'freq',    title => 'Frequency',         sort => 'numeric', help => 'Combined frequency across all samples'},
    { key => 'cds',     title => 'CDS haplotype(s)',  sort => 'none',    help => 'Haplotype names represent a comma-separated list of differences to the reference sequence'},
    # { key => 'count',       title => 'Count',             sort => 'numeric' },
    @pop_cols,
    { key => 'extra',   title => '',                  sort => 'none' }
  );
  
  my @rows;
  
  foreach my $ph(@{$c->get_all_ProteinHaplotypes}) {    
    $table->add_row($self->render_protein_haplotype_row($ph));
  }
  
  print STDERR "TOTAL ".tv_interval($t0, [gettimeofday])."\n";
  
  $html .= $table->render;

  return $html;
}

sub get_haplotypes {
  my $self = shift;
  
  my $tr = $self->object->Obj;
  
  my $variation_db = $tr->adaptor->db->get_db_adaptor('variation');
  
  # find VCF config
  my $c = $self->object->species_defs->multi_val('ENSEMBL_VCF_COLLECTIONS');

  if($c) {
   # set config file via ENV variable
   $ENV{ENSEMBL_VARIATION_VCF_CONFIG_FILE} = $c->{'CONFIG'};
   $variation_db->use_vcf($c->{'ENABLED'}) if $variation_db->can('use_vcf');
  }
  
  my $vca = $variation_db->get_VCFCollectionAdaptor();
  
  # my @gts = map {@{$_->get_all_IndividualGenotypeFeatures_by_Slice($tr->feature_Slice, undef, 1)}} @{$vca->fetch_all};
  
  my @gts;

  my $t0 = [gettimeofday];

  # we don't want variants in introns
  foreach my $exon(@{$tr->get_all_Exons}) {
    push @gts, map {@{$_->get_all_SampleGenotypeFeatures_by_Slice($exon->feature_Slice, undef, 1)}} @{$vca->fetch_all};
  }
  
  print STDERR "GENOTYPES ".tv_interval($t0, [gettimeofday])."\n";
  
  print STDERR "Fetched ".(scalar @gts)." GTs\n";
  
  return Bio::EnsEMBL::Variation::TranscriptHaplotypeContainer->new(
    -transcript => $tr,
    -genotypes  => \@gts,
    -samples    => [map {@{$_->get_all_Samples}} @{$vca->fetch_all}],
    -db         => $variation_db
  );
}

sub short_population_name {
  my $self = shift;
  my $name = shift;
  
  my $short = $name;
  $short =~ s/1000GENOMES:phase_3://i;
  
  return $short;
}

sub population_structure {
{'fraction_non_alphanumeric': 0.1549230591229897, 'fraction_numerical': 0.007173435149832234, 'mean_word_length': 3.652314316469322, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 147, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5398617', 'n_tokens_mistral': 3134, 'n_tokens_neox': 2995, 'n_words': 744}