<commit_before># $Id$

package Bio::EnsEMBL::Collection;

use strict;
use warnings;

use Bio::EnsEMBL::Utils::Argument  ('rearrange');
use Bio::EnsEMBL::Utils::Exception ('throw');

use constant { FEATURE_DBID        => 0,
               FEATURE_SEQREGIONID => 1,
               FEATURE_START       => 2,
               FEATURE_END         => 3,
               FEATURE_STRAND      => 4 };

# ======================================================================
# The public interface added by Feature Collection

sub fetch_bins_by_Slice {
  my $this = shift;
  my ( $slice, $method, $nbins, $logic_name, $lightweight ) =
    rearrange( [ 'SLICE', 'METHOD',
                 'NBINS', 'LOGIC_NAME',
                 'LIGHTWEIGHT'
               ],
               @_ );

  # Temporarily set the colleciton to be lightweight.
  my $old_value = $this->_lightweight();
  if   ( defined($lightweight) ) { $this->_lightweight($lightweight) }
  else                           { $this->_lightweight(1) }

  my $bins =
    $this->_bin_features( -slice  => $slice,
                          -nbins  => $nbins,
                          -method => $method,
                          -features =>
                            $this->fetch_all_by_Slice_constraint(
                                              $slice, undef, $logic_name
                            ) );

  # Reset the lightweightness to whatever it was before.
  $this->_lightweight($old_value);

  return $bins;
}

# ======================================================================
# Specialization of methods in Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor

sub _remap {
  my ( $this, $features, $mapper, $slice ) = @_;

  if ( scalar( @{$features} ) > 0 ) {
    if ( $slice->get_seq_region_id() !=
         $features->[0][FEATURE_SEQREGIONID] )
    {
      throw(   '_remap() in Bio::EnsEMBL::Collection '
             . 'was unexpectedly expected to be intelligent' );
    }
  }

  return $features;
}

sub _create_feature {
  my ( $this, $feature_type, $args ) = @_;

  my ( $dbid, $start, $end, $strand, $slice ) =
    rearrange( [ 'DBID', 'START', 'END', 'STRAND', 'SLICE' ],
               %{$args} );
  my $feature =
    [ $dbid, $slice->get_seq_region_id(), $start, $end, $strand ];

  return $feature;
}

sub _create_feature_fast {
  my ( $this, $feature_type, $args ) = @_;

  if ( !defined( $this->{'recent_slice'} )
       || $this->{'recent_slice'} ne $args->{'slice'} )
  {
    $this->{'recent_slice'} = $args->{'slice'};
    $this->{'recent_slice_seq_region_id'} =
      $args->{'slice'}->get_seq_region_id();
  }

  my $feature = [ $args->{'dbID'},
                  $this->{'recent_slice_seq_region_id'},
                  $args->{'start'},
                  $args->{'end'},
                  $args->{'strand'} ];

  return $feature;
}

# ======================================================================
# Private methods

sub _lightweight {
  my ( $this, $value ) = @_;

  if ( defined($value) ) {
    $this->{'lightweight'} = ( $value != 0 );
  }

  return $this->{'lightweight'} || 0;
}

our %VALID_BINNING_METHODS = (
               'count'            => 0,
               'density'          => 0,    # Same as 'count'.
               'indices'          => 1,
               'index'            => 1,    # Same as 'indices'.
               'features'         => 2,
               'feature'          => 2,    # Same as 'features'.
               'fractional_count' => 3,
               'weight'           => 3,    # Same as 'fractional_count'.
               'coverage'         => 4 );

sub _bin_features {
  my $this = shift;
  my ( $slice, $nbins, $method_name, $features ) =<commit_msg>Diable the 'index' and 'feature' binning methods.<commit_after># $Id$

package Bio::EnsEMBL::Collection;

use strict;
use warnings;

use Bio::EnsEMBL::Utils::Argument  ('rearrange');
use Bio::EnsEMBL::Utils::Exception ('throw');

use constant { FEATURE_DBID        => 0,
               FEATURE_SEQREGIONID => 1,
               FEATURE_START       => 2,
               FEATURE_END         => 3,
               FEATURE_STRAND      => 4 };

# ======================================================================
# The public interface added by Feature Collection

sub fetch_bins_by_Slice {
  my $this = shift;
  my ( $slice, $method, $nbins, $logic_name, $lightweight ) =
    rearrange( [ 'SLICE', 'METHOD',
                 'NBINS', 'LOGIC_NAME',
                 'LIGHTWEIGHT'
               ],
               @_ );

  # Temporarily set the colleciton to be lightweight.
  my $old_value = $this->_lightweight();
  if   ( defined($lightweight) ) { $this->_lightweight($lightweight) }
  else                           { $this->_lightweight(1) }

  my $bins =
    $this->_bin_features( -slice  => $slice,
                          -nbins  => $nbins,
                          -method => $method,
                          -features =>
                            $this->fetch_all_by_Slice_constraint(
                                              $slice, undef, $logic_name
                            ) );

  # Reset the lightweightness to whatever it was before.
  $this->_lightweight($old_value);

  return $bins;
}

# ======================================================================
# Specialization of methods in Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor

sub _remap {
  my ( $this, $features, $mapper, $slice ) = @_;

  if ( scalar( @{$features} ) > 0 ) {
    if ( $slice->get_seq_region_id() !=
         $features->[0][FEATURE_SEQREGIONID] )
    {
      throw(   '_remap() in Bio::EnsEMBL::Collection '
             . 'was unexpectedly expected to be intelligent' );
    }
  }

  return $features;
}

sub _create_feature {
  my ( $this, $feature_type, $args ) = @_;

  my ( $dbid, $start, $end, $strand, $slice ) =
    rearrange( [ 'DBID', 'START', 'END', 'STRAND', 'SLICE' ],
               %{$args} );
  my $feature =
    [ $dbid, $slice->get_seq_region_id(), $start, $end, $strand ];

  return $feature;
}

sub _create_feature_fast {
  my ( $this, $feature_type, $args ) = @_;

  if ( !defined( $this->{'recent_slice'} )
       || $this->{'recent_slice'} ne $args->{'slice'} )
  {
    $this->{'recent_slice'} = $args->{'slice'};
    $this->{'recent_slice_seq_region_id'} =
      $args->{'slice'}->get_seq_region_id();
  }

  my $feature = [ $args->{'dbID'},
                  $this->{'recent_slice_seq_region_id'},
                  $args->{'start'},
                  $args->{'end'},
                  $args->{'strand'} ];

  return $feature;
}

# ======================================================================
# Private methods

sub _lightweight {
  my ( $this, $value ) = @_;

  if ( defined($value) ) {
    $this->{'lightweight'} = ( $value != 0 );
  }

  return $this->{'lightweight'} || 0;
}

our %VALID_BINNING_METHODS = (
               'count'            => 0,
               'density'          => 0,    # Same as 'count'.
             # 'indices'          => 1,
             # 'index'            => 1,    # Same as 'indices'.
             # 'features'         => 2,
             # 'feature'          => 2,    # Same as 'features'.
               'fractional_count' => 3,
               'weight'           => 3,    # Same as 'fractional_count'.
               'coverage'         => 4 );

sub _bin_features {
  my $this = shift;
  my ( $slice, $nbins, $method_name, $features ) =
{'fraction_non_alphanumeric': 0.20696665763079425, 'fraction_numerical': 0.005150447275684467, 'mean_word_length': 1.9826192400970089, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5195860', 'n_tokens_mistral': 2351, 'n_tokens_neox': 2208, 'n_words': 468}