<commit_before># $Id$

package EnsEMBL::Web::Component::Transcript::TranscriptSNPTable;

use strict;

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

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

sub content {
  my $self        = shift;
  my $hub         = $self->hub;
  my $strain_name = $hub->species_defs->translate('strain');
  my ($html, @samples, %tables);
  
  foreach my $param ($hub->param) {
    if ($param =~ /opt_pop/ && $hub->param($param) eq 'on') {
      $param =~ s/opt_pop_//;
      push @samples, $param;
    }
  }
  
  my $snp_data = $self->get_page_data(\@samples);
  my $columns  = [
    { key => 'ID',          sort => 'html'                                               },
    { key => 'consequence', sort => 'string',   title => 'Type'                          },
    { key => 'chr' ,        sort => 'position', title => 'Chr: bp'                       },
    { key => 'ref_alleles', sort => 'string',   title => 'Ref. allele'                   },
    { key => 'Alleles',     sort => 'string',   title => ucfirst "$strain_name genotype" },
    { key => 'Ambiguity',   sort => 'string',   title => 'Ambiguity'                     },
    { key => 'HGVS',        sort => 'string',   title => 'HGVS name(s)'                  },
    { key => 'Codon',       sort => 'html',     title => 'Transcript codon'              },
    { key => 'cdscoord',    sort => 'numeric',  title => 'CDS coord.'                    },
    { key => 'aachange',    sort => 'string',   title => 'AA change'                     },
    { key => 'aacoord',     sort => 'numeric',  title => 'AA coord.'                     },
    { key => 'Class',       sort => 'string'                                             },
    { key => 'Source',      sort => 'string'                                             },
    { key => 'Status',      sort => 'string',   title => 'Validation'                    }
  ];
 
 
  foreach my $sample (@samples) {
    my @rows = map @{$snp_data->{$_}{$sample} || []}, sort keys %$snp_data;
    
    if (scalar @rows) {      
      $tables{$sample} = $self->new_table($columns, \@rows, { data_table => 1, sorting => [ 'chr asc' ] })->render;
    } else {
      $tables{$sample} = '
        <p>
          There are no variations in this region in this strain, or the variations have been filtered out by the options set in the page configuration. 
          To change the filtering options select the "Configure this page" link from the menu on the left hand side of this page.
        </p><br />
      ';
    }
  }
  
  $html .= "<h2>Variations in $_:</h2>$tables{$_}" for keys %tables;
  $html .= $self->_info('Configuring the display', sprintf('
    <p>These %ss are displayed by default: <b>%s.</b><br />
    Select the "Configure this page" link in the left hand menu to customise which %ss and types of variation are displayed in the tables above.</p>',
    $strain_name, join(', ', $self->object->get_samples('default')), $strain_name
  ));  
  
  return $html;
}

sub get_page_data {
  my ($self, $samples) = @_;
  my $hub        = $self->hub;
  my $object     = $self->object;
  my $transcript = $object->Obj;
  my %snp_data;
  
  my $base_url = $hub->url({
    'type' => 'Variation',
    'action' => 'Summary',
    'v' => undef ,
    'vf' => undef,
    'source' => undef
  });

  foreach my $sample (@$samples) {
    my $munged_transcript = $object->get_munged_slice('tsv_transcript',  $hub->param('context') eq 'FULL' ? 1000 : $hub->param('context'), 1) || warn "Couldn't get munged transcript";
    my $sample_slice      = $munged_transcript->[1]->get_by_strain($sample);

    my ($allele_info, $consequences) = $object->getAllelesConsequencesOnSlice($sample, 'tsv_transcript', $sample_slice);
    
    next unless @$consequences && @$allele_info;
    
    my ($coverage_level, $raw_coverage_obj) = $object->read_coverage($sample, $sample_slice);
    my @coverage_obj = @$raw_coverage_obj ? sort { $a->start <=> $b->start } @$raw_coverage_obj : ();
    my $index        = 0;
    
    foreach my $allele_ref (@$allele_info) {
      my $allele      = $allele_ref->[2];
      my $conseq_type = $consequences->[$index];
      
      $index++;
      
      next unless $conseq_type && $allele;

      # Check consequence obj and allele feature obj have same alleles
      my $tmp        = join '', @{$conseq_type->alleles || []};
      $tmp           =~ tr/ACGT/TGCA/ if $transcript->strand ne $allele->strand;
      my $type       = join ', ', @{$conseq_type->type || []};
      $type         .= ' (Same As Ref. Assembly)' if $type eq 'SARA';
      my $offset     = $sample_slice->strand > 0 ? $sample_slice->start - 1 :  $sample_slice->end + 1;
      my $chr_start  = $allele->start + $offset;
      my $chr_end    = $allele->end   + $offset;
      my $class      = $object->var_class($allele);
      my $codon      = $conseq_type->codon;
      my $chr        = $sample_slice->seq_region_name;
      my $aa_alleles = $conseq_type->aa_alleles || [];
      my $aa_coord   = $conseq_type->aa_start;<commit_msg>remove unnecessary argument to get_munged_slice<commit_after># $Id$

package EnsEMBL::Web::Component::Transcript::TranscriptSNPTable;

use strict;

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

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

sub content {
  my $self        = shift;
  my $hub         = $self->hub;
  my $strain_name = $hub->species_defs->translate('strain');
  my ($html, @samples, %tables);
  
  foreach my $param ($hub->param) {
    if ($param =~ /opt_pop/ && $hub->param($param) eq 'on') {
      $param =~ s/opt_pop_//;
      push @samples, $param;
    }
  }
  
  my $snp_data = $self->get_page_data(\@samples);
  my $columns  = [
    { key => 'ID',          sort => 'html'                                               },
    { key => 'consequence', sort => 'string',   title => 'Type'                          },
    { key => 'chr' ,        sort => 'position', title => 'Chr: bp'                       },
    { key => 'ref_alleles', sort => 'string',   title => 'Ref. allele'                   },
    { key => 'Alleles',     sort => 'string',   title => ucfirst "$strain_name genotype" },
    { key => 'Ambiguity',   sort => 'string',   title => 'Ambiguity'                     },
    { key => 'HGVS',        sort => 'string',   title => 'HGVS name(s)'                  },
    { key => 'Codon',       sort => 'html',     title => 'Transcript codon'              },
    { key => 'cdscoord',    sort => 'numeric',  title => 'CDS coord.'                    },
    { key => 'aachange',    sort => 'string',   title => 'AA change'                     },
    { key => 'aacoord',     sort => 'numeric',  title => 'AA coord.'                     },
    { key => 'Class',       sort => 'string'                                             },
    { key => 'Source',      sort => 'string'                                             },
    { key => 'Status',      sort => 'string',   title => 'Validation'                    }
  ];
 
 
  foreach my $sample (@samples) {
    my @rows = map @{$snp_data->{$_}{$sample} || []}, sort keys %$snp_data;
    
    if (scalar @rows) {      
      $tables{$sample} = $self->new_table($columns, \@rows, { data_table => 1, sorting => [ 'chr asc' ] })->render;
    } else {
      $tables{$sample} = '
        <p>
          There are no variations in this region in this strain, or the variations have been filtered out by the options set in the page configuration. 
          To change the filtering options select the "Configure this page" link from the menu on the left hand side of this page.
        </p><br />
      ';
    }
  }
  
  $html .= "<h2>Variations in $_:</h2>$tables{$_}" for keys %tables;
  $html .= $self->_info('Configuring the display', sprintf('
    <p>These %ss are displayed by default: <b>%s.</b><br />
    Select the "Configure this page" link in the left hand menu to customise which %ss and types of variation are displayed in the tables above.</p>',
    $strain_name, join(', ', $self->object->get_samples('default')), $strain_name
  ));  
  
  return $html;
}

sub get_page_data {
  my ($self, $samples) = @_;
  my $hub        = $self->hub;
  my $object     = $self->object;
  my $transcript = $object->Obj;
  my %snp_data;
  
  my $base_url = $hub->url({
    'type' => 'Variation',
    'action' => 'Summary',
    'v' => undef ,
    'vf' => undef,
    'source' => undef
  });

  foreach my $sample (@$samples) {
    my $munged_transcript = $object->get_munged_slice($hub->param('context') eq 'FULL' ? 1000 : $hub->param('context'), 1) || warn "Couldn't get munged transcript";
    my $sample_slice      = $munged_transcript->[1]->get_by_strain($sample);

    my ($allele_info, $consequences) = $object->getAllelesConsequencesOnSlice($sample, 'tsv_transcript', $sample_slice);
    
    next unless @$consequences && @$allele_info;
    
    my ($coverage_level, $raw_coverage_obj) = $object->read_coverage($sample, $sample_slice);
    my @coverage_obj = @$raw_coverage_obj ? sort { $a->start <=> $b->start } @$raw_coverage_obj : ();
    my $index        = 0;
    
    foreach my $allele_ref (@$allele_info) {
      my $allele      = $allele_ref->[2];
      my $conseq_type = $consequences->[$index];
      
      $index++;
      
      next unless $conseq_type && $allele;

      # Check consequence obj and allele feature obj have same alleles
      my $tmp        = join '', @{$conseq_type->alleles || []};
      $tmp           =~ tr/ACGT/TGCA/ if $transcript->strand ne $allele->strand;
      my $type       = join ', ', @{$conseq_type->type || []};
      $type         .= ' (Same As Ref. Assembly)' if $type eq 'SARA';
      my $offset     = $sample_slice->strand > 0 ? $sample_slice->start - 1 :  $sample_slice->end + 1;
      my $chr_start  = $allele->start + $offset;
      my $chr_end    = $allele->end   + $offset;
      my $class      = $object->var_class($allele);
      my $codon      = $conseq_type->codon;
      my $chr        = $sample_slice->seq_region_name;
      my $aa_alleles = $conseq_type->aa_alleles || [];
      my $aa_coord   = $conseq_type->aa_start;
{'fraction_non_alphanumeric': 0.16980017718279358, 'fraction_numerical': 0.003149916330347475, 'mean_word_length': 2.150387596899225, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 205, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '136987', 'n_tokens_mistral': 3377, 'n_tokens_neox': 3231, 'n_words': 834}