<commit_before># Copyright [1999-2014] 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.

package EnsEMBL::REST::Model::LDFeatureContainer;

use Moose;
use namespace::autoclean;
use Try::Tiny;
use POSIX qw/floor/;
use Scalar::Util qw/weaken looks_like_number/;
use Catalyst::Exception qw(throw);
extends 'Catalyst::Model';

with 'Catalyst::Component::InstancePerContext';

has 'context' => (is => 'ro', weak_ref => 1);

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

sub fetch_LDFeatureContainer_variation_name {
  my ($self, $variation_name) = @_;
  Catalyst::Exception->throw("No variation given. Please specify a variation to retrieve from this service") if ! $variation_name;
  my $c = $self->context();
  my $species = $c->stash->{species};

  my $va = $c->model('Registry')->get_adaptor($species, 'Variation', 'Variation');
  my $ldfca = $c->model('Registry')->get_adaptor($species, 'Variation', 'LDFeatureContainer');

  my $window_size = $c->request->param('window_size') || 1000; # default is 1MB
  Catalyst::Exception->throw("window_size needs to be a value between 0 and 1000.") if (!looks_like_number($window_size));
  Catalyst::Exception->throw("window_size needs to be a value between 0 and 1000.") if ($window_size > 1000);
  Catalyst::Exception->throw("window_size needs to be a value between 0 and 1000.") if ($window_size < 0);
  $window_size = floor($window_size);
  my $max_snp_distance = ($window_size / 2) * 1000;
  $ldfca->max_snp_distance($max_snp_distance);

  my $ld_config = $c->config->{'Model::LDFeatureContainer'};
  if ($ld_config && $ld_config->{use_vcf}) {
    $ldfca->db->use_vcf($ld_config->{use_vcf});
    $Bio::EnsEMBL::Variation::DBSQL::VCFCollectionAdaptor::CONFIG_FILE = $ld_config->{vcf_config};
    $ENV{ENSEMBL_VARIATION_VCF_ROOT_DIR} = $ld_config->{dir} if (defined $ld_config->{dir});
  }
  my $variation = $va->fetch_by_name($variation_name);
  Catalyst::Exception->throw("Could not fetch variation object for id: $variation_name.") if ! $variation;
  my $vfs = $variation->get_all_VariationFeatures();
  Catalyst::Exception->throw("Variant maps more than once to the genome.") if (scalar @$vfs > 1);
  Catalyst::Exception->throw("Could not retrieve a variation feature.") if (scalar @$vfs == 0);
  my $vf = $vfs->[0];

  my $population_name = $c->request->param('population_name');
  if ($population_name) {
    my $pa = $c->model('Registry')->get_adaptor($species, 'Variation', 'Population');     
    my $population = $pa->fetch_by_name($population_name);
    if (!$population) {
      Catalyst::Exception->throw("Could not fetch population object for population name: $population_name");
    }
    my $ldfc;
    try {
      $ldfc = $ldfca->fetch_by_VariationFeature($vf, $population);
    } catch {
      Catalyst::Exception->throw("Something went wrong while fetching from LDFeatureContainerAdaptor");
    };
    return $self->to_array($ldfc)
  }
  my $ldfc = $ldfca->fetch_by_VariationFeature($vf);
  return $self->to_array($ldfc);
}

sub fetch_LDFeatureContainer_slice {
  my ($self, $slice) = @_;
  Catalyst::Exception->throw("No region given. Please specify a region to retrieve from this service.") if ! $slice;
  Catalyst::Exception->throw("Specified region is too large. Maximum allowed size for region is 1Mb.") if ($slice->length > 1_000_000);
  my $c = $self->context();
  my $species = $c->stash->{species};

  my $ldfca = $c->model('Registry')->get_adaptor($species, 'Variation', 'LDFeatureContainer');

  my $ld_config = $c->config->{'Model::LDFeatureContainer'};
  if ($ld_config && $ld_config->{use_vcf}) {
    $ldfca->db->use_vcf($ld_config->{use_vcf});
    $Bio::EnsEMBL::Variation::DBSQL::VCFCollectionAdaptor::CONFIG_FILE = $ld_config->{vcf_config};
    $ENV{ENSEMBL_VARIATION_VCF_ROOT_DIR} = $ld_config->{dir} if (defined $ld_config->{dir});
  }

  my $population_name = $c->request->param('population_name');
  if ($population_name) {
    my $pa = $c->model('Registry')->get_adaptor($species, 'Variation', 'Population');     
    my $population = $pa->fetch_by_name($population_name);
    if (!$population) {
      Catalyst::Exception->throw("Could not fetch population object for population name: $population_name");
    }
    my $ldfc;
    try {
      $ldfc = $ldfca->fetch_by_Slice($slice, $population);
    } catch {
      Catalyst::Exception->throw("Something went wrong while fetching from LDFeatureContainerAdaptor");
    };
    return $self->to_array($ldfc)
  }
  my $ldfc = $ldfca->fetch_by_Slice($slice);
  return $self->to_array($ldfc);
}

sub to_array {
  my ($self, $LDFC) = @_;
  my $c = $self->context();
  my $species = $c->stash->{species};
  my $pa = $c->model('Registry')->get_adaptor($species, 'Variation', 'Population');
  my $d_prime = $c->request->param('d_prime');
  my $r2 = $c->request->param('r2');
  my @LDFC_array = ();

  # we pass 1 to get_all_ld_values() so that it doesn't lazy load<commit_msg>Use methods from Variation::DBSQL::DBAdaptor to set vcf_config_file and vcf_root_dir<commit_after># Copyright [1999-2014] 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.

package EnsEMBL::REST::Model::LDFeatureContainer;

use Moose;
use namespace::autoclean;
use Try::Tiny;
use POSIX qw/floor/;
use Scalar::Util qw/weaken looks_like_number/;
use Catalyst::Exception qw(throw);
extends 'Catalyst::Model';

with 'Catalyst::Component::InstancePerContext';

has 'context' => (is => 'ro', weak_ref => 1);

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

sub fetch_LDFeatureContainer_variation_name {
  my ($self, $variation_name) = @_;
  Catalyst::Exception->throw("No variation given. Please specify a variation to retrieve from this service") if ! $variation_name;
  my $c = $self->context();
  my $species = $c->stash->{species};

  my $va = $c->model('Registry')->get_adaptor($species, 'Variation', 'Variation');
  my $ldfca = $c->model('Registry')->get_adaptor($species, 'Variation', 'LDFeatureContainer');

  my $window_size = $c->request->param('window_size') || 1000; # default is 1MB
  Catalyst::Exception->throw("window_size needs to be a value between 0 and 1000.") if (!looks_like_number($window_size));
  Catalyst::Exception->throw("window_size needs to be a value between 0 and 1000.") if ($window_size > 1000);
  Catalyst::Exception->throw("window_size needs to be a value between 0 and 1000.") if ($window_size < 0);
  $window_size = floor($window_size);
  my $max_snp_distance = ($window_size / 2) * 1000;
  $ldfca->max_snp_distance($max_snp_distance);

  my $ld_config = $c->config->{'Model::LDFeatureContainer'};
  if ($ld_config && $ld_config->{use_vcf}) {
    $ldfca->db->use_vcf($ld_config->{use_vcf});
    $ldfca->db->vcf_config_file($ld_config->{vcf_config});
    $ldfca->db->vcf_root_dir($ld_config->{dir}) if (defined $ld_config->{dir});
  }
  my $variation = $va->fetch_by_name($variation_name);
  Catalyst::Exception->throw("Could not fetch variation object for id: $variation_name.") if ! $variation;
  my $vfs = $variation->get_all_VariationFeatures();
  Catalyst::Exception->throw("Variant maps more than once to the genome.") if (scalar @$vfs > 1);
  Catalyst::Exception->throw("Could not retrieve a variation feature.") if (scalar @$vfs == 0);
  my $vf = $vfs->[0];

  my $population_name = $c->request->param('population_name');
  if ($population_name) {
    my $pa = $c->model('Registry')->get_adaptor($species, 'Variation', 'Population');     
    my $population = $pa->fetch_by_name($population_name);
    if (!$population) {
      Catalyst::Exception->throw("Could not fetch population object for population name: $population_name");
    }
    my $ldfc;
    try {
      $ldfc = $ldfca->fetch_by_VariationFeature($vf, $population);
    } catch {
      Catalyst::Exception->throw("Something went wrong while fetching from LDFeatureContainerAdaptor");
    };
    return $self->to_array($ldfc)
  }
  my $ldfc = $ldfca->fetch_by_VariationFeature($vf);
  return $self->to_array($ldfc);
}

sub fetch_LDFeatureContainer_slice {
  my ($self, $slice) = @_;
  Catalyst::Exception->throw("No region given. Please specify a region to retrieve from this service.") if ! $slice;
  Catalyst::Exception->throw("Specified region is too large. Maximum allowed size for region is 1Mb.") if ($slice->length > 1_000_000);
  my $c = $self->context();
  my $species = $c->stash->{species};

  my $ldfca = $c->model('Registry')->get_adaptor($species, 'Variation', 'LDFeatureContainer');

  my $ld_config = $c->config->{'Model::LDFeatureContainer'};
  if ($ld_config && $ld_config->{use_vcf}) {
    $ldfca->db->use_vcf($ld_config->{use_vcf});
    $ldfca->db->vcf_config_file($ld_config->{vcf_config});
    $ldfca->db->vcf_root_dir($ld_config->{dir}) if (defined $ld_config->{dir});
  }

  my $population_name = $c->request->param('population_name');
  if ($population_name) {
    my $pa = $c->model('Registry')->get_adaptor($species, 'Variation', 'Population');     
    my $population = $pa->fetch_by_name($population_name);
    if (!$population) {
      Catalyst::Exception->throw("Could not fetch population object for population name: $population_name");
    }
    my $ldfc;
    try {
      $ldfc = $ldfca->fetch_by_Slice($slice, $population);
    } catch {
      Catalyst::Exception->throw("Something went wrong while fetching from LDFeatureContainerAdaptor");
    };
    return $self->to_array($ldfc)
  }
  my $ldfc = $ldfca->fetch_by_Slice($slice);
  return $self->to_array($ldfc);
}

sub to_array {
  my ($self, $LDFC) = @_;
  my $c = $self->context();
  my $species = $c->stash->{species};
  my $pa = $c->model('Registry')->get_adaptor($species, 'Variation', 'Population');
  my $d_prime = $c->request->param('d_prime');
  my $r2 = $c->request->param('r2');
  my @LDFC_array = ();

  # we pass 1 to get_all_ld_values() so that it doesn't lazy load
{'fraction_non_alphanumeric': 0.16145598702585817, 'fraction_numerical': 0.010271195603207496, 'mean_word_length': 4.888594164456234, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 183, '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': '6603844', 'n_tokens_mistral': 3856, 'n_tokens_neox': 3645, 'n_words': 919}