<commit_before>package EnsEMBL::Web::CoreObjects;

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

use Bio::EnsEMBL::Registry;

use EnsEMBL::Web::Fake;

use base qw(EnsEMBL::Web::Root);

sub new {
  my ($class, $input, $dbconnection, $flag) = @_;
  
  my $self = {
    input      => $input,
    dbc        => $dbconnection,
    parameters => {},
    objects    => {}
  };
  
  bless $self, $class;
  
  $self->_generate_objects;
  
  return $self;
}

sub location   :lvalue { $_[0]->{'objects'}{'location'};   }
sub gene       :lvalue { $_[0]->{'objects'}{'gene'};       }
sub transcript :lvalue { $_[0]->{'objects'}{'transcript'}; }
sub variation  :lvalue { $_[0]->{'objects'}{'variation'};  }
sub regulation :lvalue { $_[0]->{'objects'}{'regulation'}; }
sub lrg        :lvalue { $_[0]->{'objects'}{'lrg'};       }

sub param    { my $self = shift; return $self->{'input'}->param(@_); }
sub database { my $self = shift; return $self->{'dbc'}->get_DBAdaptor(@_); }

sub long_caption {
  my ($self, $type) = @_;
  
  return '-' unless $self->$type;
  
  my $dxr   = $self->$type->can('display_xref') ? $self->$type->display_xref : undef;
  my $label = $dxr ? ' (' . $dxr->display_id . ')' : '';
  
  return $self->$type->stable_id . $label;
}

sub transcript_short_caption {
  my $self = shift;  
  
  return '-' unless $self->transcript;
  return ucfirst($self->transcript->type) . ': ' . $self->transcript->stable_id if $self->transcript->isa('EnsEMBL::Web::Fake');
  
  my $dxr   = $self->transcript->can('display_xref') ? $self->transcript->display_xref : undef;
  my $label = $dxr ? $dxr->display_id : $self->transcript->stable_id;
  
  return length $label < 15 ? "Transcript: $label" : "Trans: $label";
}

sub gene_short_caption {
  my $self = shift;
  
  return '-' unless $self->gene;
  
  my $dxr   = $self->gene->can('display_xref') ? $self->gene->display_xref : undef;
  my $label = $dxr ? $dxr->display_id : $self->gene->stable_id;
  
  return "Gene: $label";
}

sub location_short_caption {
  my $self = shift;
  
  return '-' unless $self->location;
  if ($self->location->isa('EnsEMBL::Web::Fake')) {
    return 'Genome';
  } 
  my $label = $self->location->seq_region_name . ':' . $self->thousandify($self->location->start) . '-' . $self->thousandify($self->location->end);
  
  return "Location: $label";
}

sub variation_short_caption {
  my $self = shift;
  
  return '-' unless $self->variation;
  
  my $label = $self->variation->name;
  
  return (length $label > 30 ? 'Var: ' : 'Variation: ') . $label;
}

sub regulation_short_caption {
  my $self = shift;
  
  return '-' unless $self->regulation;
  return 'Regulation: ' . $self->regulation->stable_id;
}

sub lrg_short_caption {
  my $self = shift;
  return '-' unless $self->lrg;
  my $label = $self->lrg->stable_id;
  return $label ? "LRG: $label" : 'LRGs';
}

sub lrg_long_caption {
  my $self = shift;
  return '-' unless $self->lrg;
  return $self->lrg->stable_id;
}

sub _generate_objects {
  my $self = shift;

  return if $ENV{'ENSEMBL_SPECIES'} eq 'common';

  $self->_generate_regulation if $self->param('rf');
  $self->_generate_variation  if $self->param('v');
  $self->_generate_transcript;
  $self->_generate_gene;
  $self->_generate_lrg if $ENV{'ENSEMBL_TYPE'} eq 'LRG';
  $self->_generate_location;
  
  $self->param($_, $self->{'parameters'}->{$_}) for keys %{$self->{'parameters'}};
  
  $self->{'parameters'}{'h'} = $self->param('h') if $self->param('h');
}

sub _generate_location {
  my $self  = shift;
  my $slice = shift;
  
  if ($slice) {
    $slice = $slice->invert if $slice->strand < 0;
    
    if (!$slice->is_toplevel) {
      my $toplevel_projection = $slice->project('toplevel');
      
      if (my $seg = shift @$toplevel_projection) {
          $slice = $seg->to_Slice;
      }
    }

    $self->location = $slice;
  } elsif ($self->param('r')) {
    my ($r, $s, $e, $strand) = $self->param('r') =~ /^([^:]+):(-?\w+\.?\w*)-(-?\w+\.?\w*)(?::(-\d+))?/;
    $r ||= $self->param('r');
    
    if ($r) {      
      eval {
        $slice = $self->_get_slice($r, $s, $e);
      };
      
      if ($slice) {
        $self->location = $slice;
        $self->{'parameters'}{'r'} = $slice->seq_region_name;
        $self->{'parameters'}{'r'} .= ':' . $slice->start . '-' . $slice->end . ($strand ? ":$strand" : '') if $s || $e;
      }
    }
  }
  
  $self->{'parameters'}{'r'} ||= $self->location->seq_region_name . ':' . $self->location->start . '-' . $self->location->end if $self->location;
  
  if (!$self->location) {
    if ($self->param('m')) {
      $self->location = new EnsEMBL::Web::Fake({ 
        'view'    => 'Marker', 
        'type'    => 'Marker', 
        'markers' => $self->database($self->param('db') || 'core')->get_adaptor('Marker')->fetch_all_by_synonym($self->param('m'))
      });
    } else {
      $self->location = new EnsEMBL::Web::Fake({ view => 'Genome', type => 'Genome' });
    }
  }
}

sub _generate_gene {
  my $self = shift;
  
  if (!$self->transcript && $self->param('g')) {
    my $tdb         = $self->{'parameters'}{'db'} = $self->param('db') || 'core';
    my $tdb_adaptor = $self->database($tdb);
    
    if ($tdb_adaptor) {
      my $g = $tdb_adaptor->get_GeneAdaptor->fetch_by_stable_id($self->param('g'));
      
      if ($g) {
        $self->gene = $g;
        
        my $transcripts = $g->get_all_Transcripts;
        
        # set the transcript if there is only one;
        if (scalar @$transcripts == 1) {
          my $t = $transcripts->[0];
          $self->transcript = $t;
          $self->{'parameters'}{'t'} = $t->stable_id;
        }
        
        $self->_generate_location($self->gene->feature_Slice);
      } else {
        my $a = $tdb_adaptor->get_ArchiveStableIdAdaptor;
        $g = $a->fetch_by_stable_id($self->param('g')); 
        $self->gene = $g if $g;
      }
    }
  }
  
  if (!$self->gene && $self->param('family')) {
    my $compara_db = Bio::EnsEMBL::Registry->get_DBAdaptor('multi', 'compara');
    
    if ($compara_db) {
      my $fa = $compara_db->get_FamilyAdaptor;
      
      if ($fa) {
        my $f = $fa->fetch_by_stable_id($self->param('family'));
        $self->gene = $f if $f;
        $self->{'parameters'}{'family'} = $self->param('family') if $f;
      }
    }   
  }
  
  $self->{'parameters'}{'g'} = $self->gene->stable_id if $self->gene && !$self->gene->isa('EnsEMBL::Web::Fake');
}

sub _generate_transcript {
  my $self = shift;
  
  my $tdb = $self->{'parameters'}{'db'} = $self->param('db') || 'core';
  my $tdb_adaptor = $self->database($tdb);
  
  return unless $tdb_adaptor;
  
  if ($self->param('t')) {    
    my $t = $tdb_adaptor->get_TranscriptAdaptor->fetch_by_stable_id($self->param('t'));
    
    if ($tdb_adaptor) {
      my $t = $tdb_adaptor->get_TranscriptAdaptor->fetch_by_stable_id($self->param('t'));
      
      if ($t) {
        $self->transcript = $t;
        if ($t->stable_id =~ /LRG/) {
          $self->_get_lrg_location_from_transcript;
        } else {
          $self->_get_gene_location_from_transcript;
        }
      } else {
        my $a = $tdb_adaptor->get_ArchiveStableIdAdaptor;
        
        $t = $a->fetch_by_stable_id($self->param('t')); 
        $self->transcript = $t if $t;
      }
      $t = $a->fetch_by_stable_id($self->param('t')); 
      $self->transcript = $t if $t;
    }
    
    $self->{'parameters'}{'t'} = $t->stable_id if $t;
  }
  
  if ($self->param('pt')) {
    my $t = $tdb_adaptor->get_PredictionTranscriptAdaptor->fetch_by_stable_id($self->param('pt'));
    
    if ($t) {
      $self->transcript = $t;
      
      my $slice = $self->transcript->feature_Slice;
      $slice = $slice->invert if $slice->strand < 0;
      
      $self->location = $slice;<commit_msg>remove lines which shouldn't be there<commit_after>package EnsEMBL::Web::CoreObjects;

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

use Bio::EnsEMBL::Registry;

use EnsEMBL::Web::Fake;

use base qw(EnsEMBL::Web::Root);

sub new {
  my ($class, $input, $dbconnection, $flag) = @_;
  
  my $self = {
    input      => $input,
    dbc        => $dbconnection,
    parameters => {},
    objects    => {}
  };
  
  bless $self, $class;
  
  $self->_generate_objects;
  
  return $self;
}

sub location   :lvalue { $_[0]->{'objects'}{'location'};   }
sub gene       :lvalue { $_[0]->{'objects'}{'gene'};       }
sub transcript :lvalue { $_[0]->{'objects'}{'transcript'}; }
sub variation  :lvalue { $_[0]->{'objects'}{'variation'};  }
sub regulation :lvalue { $_[0]->{'objects'}{'regulation'}; }
sub lrg        :lvalue { $_[0]->{'objects'}{'lrg'};       }

sub param    { my $self = shift; return $self->{'input'}->param(@_); }
sub database { my $self = shift; return $self->{'dbc'}->get_DBAdaptor(@_); }

sub long_caption {
  my ($self, $type) = @_;
  
  return '-' unless $self->$type;
  
  my $dxr   = $self->$type->can('display_xref') ? $self->$type->display_xref : undef;
  my $label = $dxr ? ' (' . $dxr->display_id . ')' : '';
  
  return $self->$type->stable_id . $label;
}

sub transcript_short_caption {
  my $self = shift;  
  
  return '-' unless $self->transcript;
  return ucfirst($self->transcript->type) . ': ' . $self->transcript->stable_id if $self->transcript->isa('EnsEMBL::Web::Fake');
  
  my $dxr   = $self->transcript->can('display_xref') ? $self->transcript->display_xref : undef;
  my $label = $dxr ? $dxr->display_id : $self->transcript->stable_id;
  
  return length $label < 15 ? "Transcript: $label" : "Trans: $label";
}

sub gene_short_caption {
  my $self = shift;
  
  return '-' unless $self->gene;
  
  my $dxr   = $self->gene->can('display_xref') ? $self->gene->display_xref : undef;
  my $label = $dxr ? $dxr->display_id : $self->gene->stable_id;
  
  return "Gene: $label";
}

sub location_short_caption {
  my $self = shift;
  
  return '-' unless $self->location;
  if ($self->location->isa('EnsEMBL::Web::Fake')) {
    return 'Genome';
  } 
  my $label = $self->location->seq_region_name . ':' . $self->thousandify($self->location->start) . '-' . $self->thousandify($self->location->end);
  
  return "Location: $label";
}

sub variation_short_caption {
  my $self = shift;
  
  return '-' unless $self->variation;
  
  my $label = $self->variation->name;
  
  return (length $label > 30 ? 'Var: ' : 'Variation: ') . $label;
}

sub regulation_short_caption {
  my $self = shift;
  
  return '-' unless $self->regulation;
  return 'Regulation: ' . $self->regulation->stable_id;
}

sub lrg_short_caption {
  my $self = shift;
  return '-' unless $self->lrg;
  my $label = $self->lrg->stable_id;
  return $label ? "LRG: $label" : 'LRGs';
}

sub lrg_long_caption {
  my $self = shift;
  return '-' unless $self->lrg;
  return $self->lrg->stable_id;
}

sub _generate_objects {
  my $self = shift;

  return if $ENV{'ENSEMBL_SPECIES'} eq 'common';

  $self->_generate_regulation if $self->param('rf');
  $self->_generate_variation  if $self->param('v');
  $self->_generate_transcript;
  $self->_generate_gene;
  $self->_generate_lrg if $ENV{'ENSEMBL_TYPE'} eq 'LRG';
  $self->_generate_location;
  
  $self->param($_, $self->{'parameters'}->{$_}) for keys %{$self->{'parameters'}};
  
  $self->{'parameters'}{'h'} = $self->param('h') if $self->param('h');
}

sub _generate_location {
  my $self  = shift;
  my $slice = shift;
  
  if ($slice) {
    $slice = $slice->invert if $slice->strand < 0;
    
    if (!$slice->is_toplevel) {
      my $toplevel_projection = $slice->project('toplevel');
      
      if (my $seg = shift @$toplevel_projection) {
          $slice = $seg->to_Slice;
      }
    }

    $self->location = $slice;
  } elsif ($self->param('r')) {
    my ($r, $s, $e, $strand) = $self->param('r') =~ /^([^:]+):(-?\w+\.?\w*)-(-?\w+\.?\w*)(?::(-\d+))?/;
    $r ||= $self->param('r');
    
    if ($r) {      
      eval {
        $slice = $self->_get_slice($r, $s, $e);
      };
      
      if ($slice) {
        $self->location = $slice;
        $self->{'parameters'}{'r'} = $slice->seq_region_name;
        $self->{'parameters'}{'r'} .= ':' . $slice->start . '-' . $slice->end . ($strand ? ":$strand" : '') if $s || $e;
      }
    }
  }
  
  $self->{'parameters'}{'r'} ||= $self->location->seq_region_name . ':' . $self->location->start . '-' . $self->location->end if $self->location;
  
  if (!$self->location) {
    if ($self->param('m')) {
      $self->location = new EnsEMBL::Web::Fake({ 
        'view'    => 'Marker', 
        'type'    => 'Marker', 
        'markers' => $self->database($self->param('db') || 'core')->get_adaptor('Marker')->fetch_all_by_synonym($self->param('m'))
      });
    } else {
      $self->location = new EnsEMBL::Web::Fake({ view => 'Genome', type => 'Genome' });
    }
  }
}

sub _generate_gene {
  my $self = shift;
  
  if (!$self->transcript && $self->param('g')) {
    my $tdb         = $self->{'parameters'}{'db'} = $self->param('db') || 'core';
    my $tdb_adaptor = $self->database($tdb);
    
    if ($tdb_adaptor) {
      my $g = $tdb_adaptor->get_GeneAdaptor->fetch_by_stable_id($self->param('g'));
      
      if ($g) {
        $self->gene = $g;
        
        my $transcripts = $g->get_all_Transcripts;
        
        # set the transcript if there is only one;
        if (scalar @$transcripts == 1) {
          my $t = $transcripts->[0];
          $self->transcript = $t;
          $self->{'parameters'}{'t'} = $t->stable_id;
        }
        
        $self->_generate_location($self->gene->feature_Slice);
      } else {
        my $a = $tdb_adaptor->get_ArchiveStableIdAdaptor;
        $g = $a->fetch_by_stable_id($self->param('g')); 
        $self->gene = $g if $g;
      }
    }
  }
  
  if (!$self->gene && $self->param('family')) {
    my $compara_db = Bio::EnsEMBL::Registry->get_DBAdaptor('multi', 'compara');
    
    if ($compara_db) {
      my $fa = $compara_db->get_FamilyAdaptor;
      
      if ($fa) {
        my $f = $fa->fetch_by_stable_id($self->param('family'));
        $self->gene = $f if $f;
        $self->{'parameters'}{'family'} = $self->param('family') if $f;
      }
    }   
  }
  
  $self->{'parameters'}{'g'} = $self->gene->stable_id if $self->gene && !$self->gene->isa('EnsEMBL::Web::Fake');
}

sub _generate_transcript {
  my $self = shift;
  
  my $tdb = $self->{'parameters'}{'db'} = $self->param('db') || 'core';
  my $tdb_adaptor = $self->database($tdb);
  
  return unless $tdb_adaptor;
  
  if ($self->param('t')) {    
    my $t = $tdb_adaptor->get_TranscriptAdaptor->fetch_by_stable_id($self->param('t'));
    
    if ($tdb_adaptor) {
      my $t = $tdb_adaptor->get_TranscriptAdaptor->fetch_by_stable_id($self->param('t'));
      
      if ($t) {
        $self->transcript = $t;
        if ($t->stable_id =~ /LRG/) {
          $self->_get_lrg_location_from_transcript;
        } else {
          $self->_get_gene_location_from_transcript;
        }
      } else {
        my $a = $tdb_adaptor->get_ArchiveStableIdAdaptor;
        
        $t = $a->fetch_by_stable_id($self->param('t')); 
        $self->transcript = $t if $t;
      }
    }
    
    $self->{'parameters'}{'t'} = $t->stable_id if $t;
  }
  
  if ($self->param('pt')) {
    my $t = $tdb_adaptor->get_PredictionTranscriptAdaptor->fetch_by_stable_id($self->param('pt'));
    
    if ($t) {
      $self->transcript = $t;
      
      my $slice = $self->transcript->feature_Slice;
      $slice = $slice->invert if $slice->strand < 0;
      
      $self->location = $slice;
{'fraction_non_alphanumeric': 0.21445689487661118, 'fraction_numerical': 0.0018135889630157393, 'mean_word_length': 2.8407960199004973, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 382, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4919182', 'n_tokens_mistral': 5923, 'n_tokens_neox': 5718, 'n_words': 1012}