<commit_before>=head1 LICENSE

Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
Copyright [2016-2020] 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.

=head1 CONTACT

Please email comments or questions to the public Ensembl
developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.

Questions may also be sent to the Ensembl help desk at
<http://www.ensembl.org/Help/Contact>.

=head1 NAME

Bio::EnsEMBL::Compara::DBSQL::ConservationScoreArrayAdaptor

=head1 SYNOPSIS

  Connecting to the database using the Registry

     use Bio::EnsEMBL::Registry;

     my $reg = "Bio::EnsEMBL::Registry";

      $reg->load_registry_from_db(-host=>"ensembldb.ensembl.org", -user=>"anonymous");

      my $conservation_score_adaptor = $reg->get_adaptor(
         "Multi", "compara", "ConservationScore");

  Store data in the database

     $conservation_score_adaptor->store($conservation_score);

  To retrieve score data from the database using the default display_size
     $conservation_scores = $conservation_score_adaptor->fetch_all_by_MethodLinkSpeciesSet_Slice($method_link_species_set, $slice);

  To retrieve one score per base in the slice
     $conservation_scores = $conservation_score_adaptor->fetch_all_by_MethodLinkSpeciesSet_Slice($method_link_species_set, $slice, $slice->end-$slice->start+1);
  Print the scores
   foreach my $score (@$conservation_scores) {
      printf("position %d observed %.4f expected %.4f difference %.4f\n",  $score->position, $score->observed_score, $score->expected_score, $score->diff_score);
   }

  A simple example script for extracting scores from a slice can be found in ensembl-compara/scripts/examples/getConservationScores.pl

=head1 DESCRIPTION

Like ConservationScoreAdaptor, this adaptor can retrieve conservation scores from the database for a given region. The output format is however more direct. It is the array of the difference scores (expected minus observed).

The same data constraints are assumed: scores have to be stored in the database as LITTLE ENDIAN

=head1 APPENDIX

The rest of the documentation details each of the object methods. Internal methods are usually preceded with a _

=cut


package Bio::EnsEMBL::Compara::DBSQL::ConservationScoreArrayAdaptor;

use strict;
use warnings;

use List::Util qw(min max);
use POSIX qw(floor);

use Set::IntervalTree;

use Bio::EnsEMBL::Compara::Locus;
use Bio::EnsEMBL::Utils::Exception qw(throw warning info);
use Bio::EnsEMBL::Compara::Utils::Projection;

#store as 4 byte float. If change here, must also change in 
#ConservationScore.pm
my $_pack_size = 4;
my $_pack_type = 'f<';  # available since perl 5.10

use base qw(Bio::EnsEMBL::DBSQL::BaseAdaptor);


=head2 fetch_all_by_MethodLinkSpeciesSet_Locus

  Arg[1]      : Bio::EnsEMBL::Compara::MethodLinkSpeciesSet $mlss
  Arg[2]      : Bio::EnsEMBL::Compara::Locus $locus
  Example     : $csa_adaptor->fetch_all_by_MethodLinkSpeciesSet_Locus($mlss, $locus);
  Description : Returns the (difference) conservation scores for the required region as
                an array of values (undef means no data). The strand of the locus is ignored
                and scores are always ordered in the array from the start coordinate to
                the end coordinate.
  Returntype  : Array-ref of floats
  Exceptions  : none
  Caller      : general

=cut

sub fetch_all_by_MethodLinkSpeciesSet_Locus {
    my ($self, $mlss, $locus) = @_;

    # Arrays to store the scores
    my @diff_scores;
    $#diff_scores = $locus->length;

    my $msa_mlss = $mlss->get_linked_mlss_by_tag('msa_mlss_id');

    $self->_fetch_and_add_scores_for_MethodLinkSpeciesSet_Locus(\@diff_scores, $msa_mlss, $locus, 0);

    return \@diff_scores;
}


=head2 fetch_all_by_MethodLinkSpeciesSet_Slice

  Arg[1]      : Bio::EnsEMBL::Compara::MethodLinkSpeciesSet $mlss
  Arg[2]      : Bio::EnsEMBL::Slice $slice
  Example     : $csa_adaptor->fetch_all_by_MethodLinkSpeciesSet_Slice($mlss, $slice);
  Description : Returns the (difference) conservation scores for the required region as
                an array of values (undef means no data). The strand of the slice is ignored
                and scores are always ordered in the array from the start coordinate to
                the end coordinate.
  Returntype  : Array-ref of floats
  Exceptions  : none
  Caller      : general

=cut

sub fetch_all_by_MethodLinkSpeciesSet_Slice {
    my ($self, $mlss, $orig_slice) = @_;

    my @diff_scores;
    $#diff_scores = $orig_slice->length;
    my $msa_mlss             = $mlss->get_linked_mlss_by_tag('msa_mlss_id');

    my $projection_segments = Bio::EnsEMBL::Compara::Utils::Projection::project_Slice_to_reference_toplevel($orig_slice);

    foreach my $this_projection_segment (@$projection_segments) {

        my $slice = $this_projection_segment->to_Slice;
       
        #print $slice->seq_region_name . " " . $slice->start . " " . $slice->end . " offset=$offset\n";
        #warn "SEGMENT ".$slice->name()." ".$this_projection_segment->from_start."/".$this_projection_segment->from_end."\n";

        my $dnafrag = $self->db->get_DnaFragAdaptor->fetch_by_Slice($slice);
        my $locus = Bio::EnsEMBL::Compara::Locus->new(
                -DNAFRAG        => $dnafrag,
                -DNAFRAG_START  => $slice->seq_region_start,
                -DNAFRAG_END    => $slice->seq_region_end,
            );
        $self->_fetch_and_add_scores_for_MethodLinkSpeciesSet_Locus(\@diff_scores, $msa_mlss, $locus, $this_projection_segment->from_start() - 1);
    }

    return \@diff_scores;
}<commit_msg>bugfix: $#diff_scores is the maximum index, not the length<commit_after>=head1 LICENSE

Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
Copyright [2016-2020] 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.

=head1 CONTACT

Please email comments or questions to the public Ensembl
developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.

Questions may also be sent to the Ensembl help desk at
<http://www.ensembl.org/Help/Contact>.

=head1 NAME

Bio::EnsEMBL::Compara::DBSQL::ConservationScoreArrayAdaptor

=head1 SYNOPSIS

  Connecting to the database using the Registry

     use Bio::EnsEMBL::Registry;

     my $reg = "Bio::EnsEMBL::Registry";

      $reg->load_registry_from_db(-host=>"ensembldb.ensembl.org", -user=>"anonymous");

      my $conservation_score_adaptor = $reg->get_adaptor(
         "Multi", "compara", "ConservationScore");

  Store data in the database

     $conservation_score_adaptor->store($conservation_score);

  To retrieve score data from the database using the default display_size
     $conservation_scores = $conservation_score_adaptor->fetch_all_by_MethodLinkSpeciesSet_Slice($method_link_species_set, $slice);

  To retrieve one score per base in the slice
     $conservation_scores = $conservation_score_adaptor->fetch_all_by_MethodLinkSpeciesSet_Slice($method_link_species_set, $slice, $slice->end-$slice->start+1);
  Print the scores
   foreach my $score (@$conservation_scores) {
      printf("position %d observed %.4f expected %.4f difference %.4f\n",  $score->position, $score->observed_score, $score->expected_score, $score->diff_score);
   }

  A simple example script for extracting scores from a slice can be found in ensembl-compara/scripts/examples/getConservationScores.pl

=head1 DESCRIPTION

Like ConservationScoreAdaptor, this adaptor can retrieve conservation scores from the database for a given region. The output format is however more direct. It is the array of the difference scores (expected minus observed).

The same data constraints are assumed: scores have to be stored in the database as LITTLE ENDIAN

=head1 APPENDIX

The rest of the documentation details each of the object methods. Internal methods are usually preceded with a _

=cut


package Bio::EnsEMBL::Compara::DBSQL::ConservationScoreArrayAdaptor;

use strict;
use warnings;

use List::Util qw(min max);
use POSIX qw(floor);

use Set::IntervalTree;

use Bio::EnsEMBL::Compara::Locus;
use Bio::EnsEMBL::Utils::Exception qw(throw warning info);
use Bio::EnsEMBL::Compara::Utils::Projection;

#store as 4 byte float. If change here, must also change in 
#ConservationScore.pm
my $_pack_size = 4;
my $_pack_type = 'f<';  # available since perl 5.10

use base qw(Bio::EnsEMBL::DBSQL::BaseAdaptor);


=head2 fetch_all_by_MethodLinkSpeciesSet_Locus

  Arg[1]      : Bio::EnsEMBL::Compara::MethodLinkSpeciesSet $mlss
  Arg[2]      : Bio::EnsEMBL::Compara::Locus $locus
  Example     : $csa_adaptor->fetch_all_by_MethodLinkSpeciesSet_Locus($mlss, $locus);
  Description : Returns the (difference) conservation scores for the required region as
                an array of values (undef means no data). The strand of the locus is ignored
                and scores are always ordered in the array from the start coordinate to
                the end coordinate.
  Returntype  : Array-ref of floats
  Exceptions  : none
  Caller      : general

=cut

sub fetch_all_by_MethodLinkSpeciesSet_Locus {
    my ($self, $mlss, $locus) = @_;

    # Arrays to store the scores
    my @diff_scores;
    $#diff_scores = $locus->length;

    my $msa_mlss = $mlss->get_linked_mlss_by_tag('msa_mlss_id');

    $self->_fetch_and_add_scores_for_MethodLinkSpeciesSet_Locus(\@diff_scores, $msa_mlss, $locus, 0);

    return \@diff_scores;
}


=head2 fetch_all_by_MethodLinkSpeciesSet_Slice

  Arg[1]      : Bio::EnsEMBL::Compara::MethodLinkSpeciesSet $mlss
  Arg[2]      : Bio::EnsEMBL::Slice $slice
  Example     : $csa_adaptor->fetch_all_by_MethodLinkSpeciesSet_Slice($mlss, $slice);
  Description : Returns the (difference) conservation scores for the required region as
                an array of values (undef means no data). The strand of the slice is ignored
                and scores are always ordered in the array from the start coordinate to
                the end coordinate.
  Returntype  : Array-ref of floats
  Exceptions  : none
  Caller      : general

=cut

sub fetch_all_by_MethodLinkSpeciesSet_Slice {
    my ($self, $mlss, $orig_slice) = @_;

    my @diff_scores;
    $#diff_scores = $orig_slice->length - 1;
    my $msa_mlss             = $mlss->get_linked_mlss_by_tag('msa_mlss_id');

    my $projection_segments = Bio::EnsEMBL::Compara::Utils::Projection::project_Slice_to_reference_toplevel($orig_slice);

    foreach my $this_projection_segment (@$projection_segments) {

        my $slice = $this_projection_segment->to_Slice;
       
        #print $slice->seq_region_name . " " . $slice->start . " " . $slice->end . " offset=$offset\n";
        #warn "SEGMENT ".$slice->name()." ".$this_projection_segment->from_start."/".$this_projection_segment->from_end."\n";

        my $dnafrag = $self->db->get_DnaFragAdaptor->fetch_by_Slice($slice);
        my $locus = Bio::EnsEMBL::Compara::Locus->new(
                -DNAFRAG        => $dnafrag,
                -DNAFRAG_START  => $slice->seq_region_start,
                -DNAFRAG_END    => $slice->seq_region_end,
            );
        $self->_fetch_and_add_scores_for_MethodLinkSpeciesSet_Locus(\@diff_scores, $msa_mlss, $locus, $this_projection_segment->from_start() - 1);
    }

    return \@diff_scores;
}
{'fraction_non_alphanumeric': 0.09678469678469678, 'fraction_numerical': 0.007081807081807082, 'mean_word_length': 4.076859504132232, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '6541219', 'n_tokens_mistral': 4084, 'n_tokens_neox': 3788, 'n_words': 1083}