<commit_before>package EnsEMBL::REST::Model::Lookup;

use Moose;
use namespace::autoclean;
use Try::Tiny;

extends 'Catalyst::Model';
with 'Catalyst::Component::InstancePerContext';

# Config
has 'lookup_model' => ( is => 'ro', isa => 'Str', required => 1, default => 'DatabaseIDLookup' );

# Per instance variables
has 'context' => (is => 'ro');

sub build_per_context_instance {
  my ($self, $c, @args) = @_;
  return $self->new({ context => $c, %$self, @args });
}

sub find_genetree_by_stable_id {
  my ($self, $id) = @_;
  my $gt;
  my $c = $self->context();
  my $compara_name = $c->request->parameters->{compara};
  my $reg = $c->model('Registry');
  
  #Force search to use compara as the DB type
  $c->request->parameters->{db_type} = 'compara' if ! $c->request->parameters->{db_type};
  
  #Try to do a lookup if the ID DB is there
  my $lookup = $reg->get_DBAdaptor('multi', 'stable_ids', 1);
  if($lookup) {
    my ($species, $object_type, $db_type) = $self->find_object_location($c, $id);
    if($species) {
      my $gta = $reg->get_adaptor($species, $db_type, $object_type);
      $c->go('ReturnError', 'custom', ["No adaptor found for ID $id, species $species, object $object_type and db $db_type"]) if ! $gta;
      $gt = $gta->fetch_by_stable_id($id);
    }
  }
  
  #If we haven't got one then do a linear search
  if(! $gt) {
    my $comparas = $c->model('Registry')->get_all_DBAdaptors('compara', $compara_name);
    foreach my $c (@{$comparas}) {
      my $gta = $c->get_GeneTreeAdaptor();
      $gt = $gta->fetch_by_stable_id($id);
      last if $gt;
    }
  }
  return $gt if $gt;
  $c->go('ReturnError', 'custom', ["No GeneTree found for ID $id"]);
}

sub find_genetree_by_member_id {
  my ($self,$id) = @_;
  my $c = $self->context();
  my $compara_name = $c->request->parameters->{compara}; 
  my $reg = $c->model('Registry');
 
  my ($species, $type, $db) = $c->model('Lookup')->find_object_location($c, $id);
  $c->go('ReturnError', 'custom', ["Unable to find given object: $id"]) unless $species;
  
  my $dba = $reg->get_best_compara_DBAdaptor($c,$species,$compara_name);
  my $ma = $dba->get_GeneMemberAdaptor;
  my $member = $ma->fetch_by_source_stable_id('ENSEMBLGENE',$id);
  $c->go('ReturnError', 'custom', ["Could not fetch GeneTree Member"]) unless $member;
  
  my $gta = $dba->get_GeneTreeAdaptor;
  my $gt = $gta->fetch_default_for_Member($member);
  return $gt;
}

# uses the request for more optional arguments
sub find_object_by_stable_id {
  my ($self, $id) = @_;
  my $c = $self->context();
  my $reg = $c->model('Registry');
  my ($species, $type, $db) = $self->find_object_location($id);
  return $self->find_object($id, $species, $type, $db);
}<commit_msg>overhanging context passing stuff causing issues. should be gone<commit_after>package EnsEMBL::REST::Model::Lookup;

use Moose;
use namespace::autoclean;
use Try::Tiny;

extends 'Catalyst::Model';
with 'Catalyst::Component::InstancePerContext';

# Config
has 'lookup_model' => ( is => 'ro', isa => 'Str', required => 1, default => 'DatabaseIDLookup' );

# Per instance variables
has 'context' => (is => 'ro');

sub build_per_context_instance {
  my ($self, $c, @args) = @_;
  return $self->new({ context => $c, %$self, @args });
}

sub find_genetree_by_stable_id {
  my ($self, $id) = @_;
  my $gt;
  my $c = $self->context();
  my $compara_name = $c->request->parameters->{compara};
  my $reg = $c->model('Registry');
  
  #Force search to use compara as the DB type
  $c->request->parameters->{db_type} = 'compara' if ! $c->request->parameters->{db_type};
  
  #Try to do a lookup if the ID DB is there
  my $lookup = $reg->get_DBAdaptor('multi', 'stable_ids', 1);
  if($lookup) {
    my ($species, $object_type, $db_type) = $self->find_object_location($id);
    if($species) {
      my $gta = $reg->get_adaptor($species, $db_type, $object_type);
      $c->go('ReturnError', 'custom', ["No adaptor found for ID $id, species $species, object $object_type and db $db_type"]) if ! $gta;
      $gt = $gta->fetch_by_stable_id($id);
    }
  }
  
  #If we haven't got one then do a linear search
  if(! $gt) {
    my $comparas = $c->model('Registry')->get_all_DBAdaptors('compara', $compara_name);
    foreach my $c (@{$comparas}) {
      my $gta = $c->get_GeneTreeAdaptor();
      $gt = $gta->fetch_by_stable_id($id);
      last if $gt;
    }
  }
  return $gt if $gt;
  $c->go('ReturnError', 'custom', ["No GeneTree found for ID $id"]);
}

sub find_genetree_by_member_id {
  my ($self,$id) = @_;
  my $c = $self->context();
  my $compara_name = $c->request->parameters->{compara}; 
  my $reg = $c->model('Registry');
 
  my ($species, $type, $db) = $c->model('Lookup')->find_object_location($id);
  $c->go('ReturnError', 'custom', ["Unable to find given object: $id"]) unless $species;
  
  my $dba = $reg->get_best_compara_DBAdaptor($species,$compara_name);
  my $ma = $dba->get_GeneMemberAdaptor;
  my $member = $ma->fetch_by_source_stable_id('ENSEMBLGENE',$id);
  $c->go('ReturnError', 'custom', ["Could not fetch GeneTree Member"]) unless $member;
  
  my $gta = $dba->get_GeneTreeAdaptor;
  my $gt = $gta->fetch_default_for_Member($member);
  return $gt;
}

# uses the request for more optional arguments
sub find_object_by_stable_id {
  my ($self, $id) = @_;
  my $c = $self->context();
  my $reg = $c->model('Registry');
  my ($species, $type, $db) = $self->find_object_location($id);
  return $self->find_object($id, $species, $type, $db);
}
{'fraction_non_alphanumeric': 0.19211009174311927, 'fraction_numerical': 0.0007339449541284404, 'mean_word_length': 4.386363636363637, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 99, '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': '7566929', 'n_tokens_mistral': 2074, 'n_tokens_neox': 2036, 'n_words': 490}