<commit_before>=head1 LICENSE

Copyright [1999-2015] 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.

=cut


=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>.

=cut

# Ensembl module for Bio::EnsEMBL::Variation::VCFVariationFeature
#
#


=head1 NAME

Bio::EnsEMBL::Variation::BaseVariationFeature - Abstract base class for variation features

=head1 SYNOPSIS

None

=head1 DESCRIPTION

Abstract base class representing variation features. Should not be instantiated
directly.

=head1 METHODS

=cut

use strict;
use warnings;

package Bio::EnsEMBL::Variation::VCFVariation;

use Bio::EnsEMBL::Utils::Scalar qw(assert_ref);
use Bio::EnsEMBL::Utils::Exception qw(warning throw);
use Scalar::Util qw(weaken);

use Bio::EnsEMBL::Variation::Variation;
use Bio::EnsEMBL::Variation::Source;

our @ISA = ('Bio::EnsEMBL::Variation::Variation');

sub new_from_VCFVariationFeature {
  my $caller = shift;
  my $class = ref($caller) || $caller;
  
  # get and check VF
  my $vf = shift;
  assert_ref($vf, 'Bio::EnsEMBL::Variation::VCFVariationFeature');
  
  my $v = Bio::EnsEMBL::Variation::Variation->new_fast({
    name => $vf->variation_name,
    source => Bio::EnsEMBL::Variation::Source->new_fast({
      name => $vf->vcf_record->{metadata}->{source} || $vf->collection->id,
      description => $vf->vcf_record->{metadata}->{source} || $vf->collection->id,
    }),
  });
  
  bless $v, $class;
  
  $v->{variation_feature} = $vf;
  weaken($v->{variation_feature});
  
  return $v;
}

sub variation_feature {
  my $self = shift;
  $self->{variation_feature} = shift if @_;
  return $self->{variation_feature};
}

sub collection {
  my $self = shift;
  return $self->variation_feature->collection(@_);
}

sub vcf_record {
  my $self = shift;
  return $self->variation_feature->vcf_record(@_);
}

sub get_all_Alleles {
  my $self = shift;
  
  if(!exists($self->{alleles})) {
  
    my @alleles;
    
    # get genotypes as a hash keyed on individual dbID
    my %gt_hash = map {$_->individual->dbID() => $_} @{$self->get_all_IndividualGenotypes()};
    
    ## fetch from genotypes
    if(scalar keys %gt_hash) {
      
      # get pop/ind hash
      # $hash->{$pop_dbID}->{$ind_dbID} => 1
      my $pop_hash = $self->collection->_get_Population_Individual_hash();
    
      my %pops_by_dbID = map {$_->dbID() => $_} @{$self->collection->get_all_Populations};
    
      foreach my $pop_id(keys %$pop_hash) {
      
        # don't try and add data for a population we don't have
        next unless $pops_by_dbID{$pop_id};
      
        # get counts
        my %counts;
        $counts{$_}++ for
          map {@{$_->genotype}}
          map {$gt_hash{$_}}
          grep {$gt_hash{$_}}
          keys %{$pop_hash->{$pop_id}};
      
        # get total
        my $total = 0;
        $total += $_ for values %counts;
      
        # don't want to divide by zero
        if($total) {
          push @alleles, Bio::EnsEMBL::Variation::Allele->new_fast({
    				allele     => $_,
    				count      => $counts{$_},
    				frequency  => $counts{$_} / $total,
    				population => $pops_by_dbID{$pop_id},
    				variation  => $self,
          }) for keys %counts;
        }
      }
    }
    
    ## fetch from info field
    else {
      my $vcf_record = $self->vcf_record();
      my $info = $vcf_record->get_info();
      my @alts = @{$vcf_record->get_alternatives};
      my $ref = $vcf_record->get_reference;
    
      # check we have required fields before attempting
      if(
        $info && ref($info) eq 'HASH' &&
        $info->{AN} && $ref && scalar @alts
      ) {
        
        # find INFO keys that look like a frequency, e.g AMR_AF        
        foreach my $af_key(grep {/\_AF$/} keys %$info) {
          
          my $total = 0;
          my $i = 0;
          
          my $pop_name = $af_key;
          $pop_name =~ s/\_AF$//;
          
          # add an allele object for each frequency found
          foreach my $af(split(',', $info->{$af_key})) {
            throw("ERROR: ALT allele count mismatch") unless $alts[$i];
            
            push @alleles, Bio::EnsEMBL::Variation::Allele->new_fast({
      				allele     => $alts[$i],
      				count      => sprintf("%.0f", $af * $info->{AN}),
      				frequency  => $af,
              population => $self->collection->_create_Population($pop_name),
      				variation  => $self,
            });
            
            $total += $af;
            $i++;
          }
          
          # add reference allele
          push @alleles, Bio::EnsEMBL::Variation::Allele->new_fast({
    				allele     => $ref,
    				count      => sprintf("%.0f", (1 - $total) * $info->{AN}),
    				frequency  => (1 - $total),
            population => $self->collection->_create_Population($pop_name),
    				variation  => $self,
          });
        }
      }
    }
    
    # weaken ref back to self (variation)
    weaken $_->{variation} for @alleles;
    
    $self->{alleles} = \@alleles;
  }
  
  return $self->{alleles};<commit_msg>add counts only if we have AN<commit_after>=head1 LICENSE

Copyright [1999-2015] 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.

=cut


=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>.

=cut

# Ensembl module for Bio::EnsEMBL::Variation::VCFVariationFeature
#
#


=head1 NAME

Bio::EnsEMBL::Variation::BaseVariationFeature - Abstract base class for variation features

=head1 SYNOPSIS

None

=head1 DESCRIPTION

Abstract base class representing variation features. Should not be instantiated
directly.

=head1 METHODS

=cut

use strict;
use warnings;

package Bio::EnsEMBL::Variation::VCFVariation;

use Bio::EnsEMBL::Utils::Scalar qw(assert_ref);
use Bio::EnsEMBL::Utils::Exception qw(warning throw);
use Scalar::Util qw(weaken);

use Bio::EnsEMBL::Variation::Variation;
use Bio::EnsEMBL::Variation::Source;

our @ISA = ('Bio::EnsEMBL::Variation::Variation');

sub new_from_VCFVariationFeature {
  my $caller = shift;
  my $class = ref($caller) || $caller;
  
  # get and check VF
  my $vf = shift;
  assert_ref($vf, 'Bio::EnsEMBL::Variation::VCFVariationFeature');
  
  my $v = Bio::EnsEMBL::Variation::Variation->new_fast({
    name => $vf->variation_name,
    source => Bio::EnsEMBL::Variation::Source->new_fast({
      name => $vf->vcf_record->{metadata}->{source} || $vf->collection->id,
      description => $vf->vcf_record->{metadata}->{source} || $vf->collection->id,
    }),
  });
  
  bless $v, $class;
  
  $v->{variation_feature} = $vf;
  weaken($v->{variation_feature});
  
  return $v;
}

sub variation_feature {
  my $self = shift;
  $self->{variation_feature} = shift if @_;
  return $self->{variation_feature};
}

sub collection {
  my $self = shift;
  return $self->variation_feature->collection(@_);
}

sub vcf_record {
  my $self = shift;
  return $self->variation_feature->vcf_record(@_);
}

sub get_all_Alleles {
  my $self = shift;
  
  if(!exists($self->{alleles})) {
  
    my @alleles;
    
    # get genotypes as a hash keyed on individual dbID
    my %gt_hash = map {$_->individual->dbID() => $_} @{$self->get_all_IndividualGenotypes()};
    
    ## fetch from genotypes
    if(scalar keys %gt_hash) {
      
      # get pop/ind hash
      # $hash->{$pop_dbID}->{$ind_dbID} => 1
      my $pop_hash = $self->collection->_get_Population_Individual_hash();
    
      my %pops_by_dbID = map {$_->dbID() => $_} @{$self->collection->get_all_Populations};
    
      foreach my $pop_id(keys %$pop_hash) {
      
        # don't try and add data for a population we don't have
        next unless $pops_by_dbID{$pop_id};
      
        # get counts
        my %counts;
        $counts{$_}++ for
          map {@{$_->genotype}}
          map {$gt_hash{$_}}
          grep {$gt_hash{$_}}
          keys %{$pop_hash->{$pop_id}};
      
        # get total
        my $total = 0;
        $total += $_ for values %counts;
      
        # don't want to divide by zero
        if($total) {
          push @alleles, Bio::EnsEMBL::Variation::Allele->new_fast({
    				allele     => $_,
    				count      => $counts{$_},
    				frequency  => $counts{$_} / $total,
    				population => $pops_by_dbID{$pop_id},
    				variation  => $self,
          }) for keys %counts;
        }
      }
    }
    
    ## fetch from info field
    else {
      my $vcf_record = $self->vcf_record();
      my $info = $vcf_record->get_info();
      my @alts = @{$vcf_record->get_alternatives};
      my $ref = $vcf_record->get_reference;
    
      # check we have required fields before attempting
      if(
        $info && ref($info) eq 'HASH' &&
        $ref && scalar @alts
      ) {
        
        # find INFO keys that look like a frequency, e.g AMR_AF        
        foreach my $af_key(grep {/\_AF$/} keys %$info) {
          
          my $total = 0;
          my $i = 0;
          
          my $pop_name = $af_key;
          $pop_name =~ s/\_AF$//;
          
          # add an allele object for each frequency found
          foreach my $af(split(',', $info->{$af_key})) {
            throw("ERROR: ALT allele count mismatch") unless $alts[$i];
            
            push @alleles, Bio::EnsEMBL::Variation::Allele->new_fast({
      				allele     => $alts[$i],
      				count      => $info->{AN} ? sprintf("%.0f", $af * $info->{AN}) : undef,
      				frequency  => $af,
              population => $self->collection->_create_Population($pop_name),
      				variation  => $self,
            });
            
            $total += $af;
            $i++;
          }
          
          # add reference allele
          push @alleles, Bio::EnsEMBL::Variation::Allele->new_fast({
    				allele     => $ref,
    				count      => $info->{AN} ? sprintf("%.0f", (1 - $total) * $info->{AN}) : undef,
    				frequency  => (1 - $total),
            population => $self->collection->_create_Population($pop_name),
    				variation  => $self,
          });
        }
      }
    }
    
    # weaken ref back to self (variation)
    weaken $_->{variation} for @alleles;
    
    $self->{alleles} = \@alleles;
  }
  
  return $self->{alleles};
{'fraction_non_alphanumeric': 0.140410070075266, 'fraction_numerical': 0.004498659053551345, 'mean_word_length': 2.5286935286935286, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 156, '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': '2881072', 'n_tokens_mistral': 3990, 'n_tokens_neox': 3799, 'n_words': 956}