<commit_before>#
# Ensembl module for Bio::EnsEMBL::Variation::DBSQL::TranscriptVariationAdaptor
#
# Copyright (c) 2004 Ensembl
#
# You may distribute this module under the same terms as perl itself
#
#

=head1 NAME

Bio::EnsEMBL::Variation::DBSQL::TranscriptVariationAdaptor

=head1 SYNOPSIS
  $reg = 'Bio::EnsEMBL::Registry';
  
  $reg->load_registry_from_db(-host => 'ensembldb.ensembl.org',-user => 'anonymous');
  
  $trva = $reg->get_adaptor("human","variation","transcriptvariation");
  $vfa = $reg->get_adaptor("human","variation","variationfeature");
  $tra = $reg->get_adaptor("human","core","transcript");

  # retrieve a TranscriptVariation by its internal identifier

  $trv = $tra->fetch_by_dbID(552);
  print $trv->transcript()->stable_id(), ' ',
        $trv->variation_feature()->variation_name(), ' ',
        $trv->consequence_type(), "\n";

  # retrieve all TranscriptVariations associated with a Transcript

  $tr = $tra->fetch_by_stable_id('ENST00000278995');
  foreach $trv (@{$trva->fetch_all_by_Transcripts([$tr])}) {
    print $trv->variation_feature->variation_name(), ' ', $trv->consequence_type(), "\n";
  }

  # retrieve all TranscriptVariations associated with a VariationFeature

  $vf = $vfa->fetch_by_dbID(99123);
  foreach $trv (@{$trva->fetch_all_by_VariationFeatures($vf)}) {
    print $trva->transcript->stable_id(), ' ', $trva->consequence_type(), "\n";
  }


=head1 DESCRIPTION

This adaptor provides database connectivity for TranscriptVariation objects.
TranscriptVariations which represent an association between a variation and
a Transcript may be retrieved from the Ensembl variation database via several
means using this module.

=head1 AUTHOR - <NAME>

=head1 CONTACT

Post questions to the Ensembl development list <EMAIL>

=head1 METHODS

=cut

use strict;
use warnings;

package Bio::EnsEMBL::Variation::DBSQL::TranscriptVariationAdaptor;

use Bio::EnsEMBL::DBSQL::BaseAdaptor;
use Bio::EnsEMBL::Utils::Exception qw(throw warning);
use Bio::EnsEMBL::Utils::Sequence qw(reverse_comp expand);
use Bio::EnsEMBL::Utils::TranscriptAlleles qw(type_variation);

use Bio::EnsEMBL::Variation::TranscriptVariation;

our @ISA = ('Bio::EnsEMBL::DBSQL::BaseAdaptor');

# size that we look either side of a variation for transcripts
our $UP_DOWN_SIZE = 5000;

=head2 fetch_all_by_Transcripts

    Arg[1]      : listref of Bio::EnsEMBL::Transcript
    Example     : $tr = $ta->fetch_by_stable_id('ENST00000278995');
                  @tr_vars = @{$tr_var_adaptor->fetch_all_by_Transcripts([$tr])};
    Description : Retrieves all TranscriptVariation objects associated with
                  provided Ensembl Transcript. Attaches them to the TranscriptVariation
    ReturnType  : ref to list of Bio::EnsEMBL::Variation::TranscriptVariations
    Exceptions  : throw on bad argument
    Caller      : general
    Status      : Stable

=cut

sub fetch_all_by_Transcripts{
    my $self = shift;
    my $transcript_ref = shift;

    if (ref($transcript_ref) ne 'ARRAY' or ! $transcript_ref->[0]->isa('Bio::EnsEMBL::Transcript')){
      throw('Array Bio::EnsEMBL::Transcript expected');
    }
    
    my %tr_by_id;

    foreach my $tr (@{$transcript_ref}) {
      if (!$tr->isa('Bio::EnsEMBL::Transcript')){
	throw('Bio::EnsEMBL::Transcript is expected');
      }
      $tr_by_id{$tr->stable_id()} = $tr;
    }

    my $instr = join (",", map{"'$_'"} keys( %tr_by_id));
    my $transcript_variations = $self->generic_fetch( "tv.transcript_stable_id in ( $instr )");
    for my $tv (@{$transcript_variations}){
	#add to the TranscriptVariation object all the Transcripts
	$tv->{'transcript'} = $tr_by_id{$tv->{'_transcript_stable_id'}};
	delete $tv->{'_transcript_stable_id'}; #remove the transcript_id from the transcript_variation object
    }
    return $transcript_variations;
}

=head2 fetch_all_by_VariationFeatures

  Arg [1]    : Bio::EnsEMBL::Variation::VariationFeature $vf
  Example    :
     $vf = $vf_adaptor->fetch_by_dbID(1234);
     @tr_vars = @{$tr_var_adaptor->fetch_all_by_VariationFeatures([$vf])});
  Description: Retrieves all TranscriptVariation objects associated with
               provided Ensembl variation features. Attaches them to the given variation
               features.
  Returntype : ref to list of Bio::EnsEMBL::Variation::TranscriptVariations
  Exceptions : throw on bad argument
  Caller     : general
  Status     : Stable

=cut

sub fetch_all_by_VariationFeatures {
  my $self = shift;
  my $vf_ref = shift;

  if(ref($vf_ref) ne 'ARRAY') {
    throw('Array Bio::EnsEMBL::Variation::VariationFeature expected');
  }

  my %vf_by_id;
  my $tvs;
  
  my @have_dbID;
  my @no_dbID;
  
  foreach my $vf(@$vf_ref) {
	if(defined $vf->dbID) {
		push @have_dbID, $vf;
	}
	
	else {
		push @no_dbID, $vf;
	}
  }
  
  # already existing VFs - get TVs from the database
  if(scalar @have_dbID) {

	%vf_by_id = map {$_->dbID(), $_ } @have_dbID;
	my $instr = join (",",keys( %vf_by_id));
	$tvs = $self->generic_fetch( "tv.variation_feature_id in ( $instr )" );
	for my $tv ( @$tvs ) {
	  #add to the variation feature object all the transcript variations
	  $vf_by_id{ $tv->{'_vf_id'} }->add_TranscriptVariation( $tv );
	  #delete $tv->{'_vf_id'}; #remove the variation_feature_id from the transcript_variation object    
	}
  }
  
  # if we have some de novo VFs, we need to calculate
  if(scalar @no_dbID) {
	
	my $species;
	
	if(defined $self->db) {
		$species = $self->db()->species;
	}
	else {
		$species = $self->{'species'};
	}
	
	unless(defined $species) {
		warn("No species defined in adaptor");
		return [];
	}
	
	# get functional genomics adaptors
	my $rf_adaptor = Bio::EnsEMBL::DBSQL::MergedAdaptor->new(-species => $species, -type => "RegulatoryFeature");
	my $ef_adaptor = Bio::EnsEMBL::DBSQL::MergedAdaptor->new(-species => $species, -type => "ExternalFeature");
	
	unless(defined $rf_adaptor && defined $ef_adaptor) {
		warn("Must have functional genomics database attached to consider regulatory features");
	}
	
	# get a gene and transcript adaptor
	my $gene_adaptor = Bio::EnsEMBL::DBSQL::MergedAdaptor->new(-species => $species, -type => "Gene");
	my $transcript_adaptor = Bio::EnsEMBL::DBSQL::MergedAdaptor->new(-species => $species, -type => "Transcript");
	
	unless(defined $gene_adaptor && defined $transcript_adaptor) {
		warn("Must have core database attached to consider regulatory features");
	}
	
	# iterate through the VFs
	foreach my $vf(@no_dbID) {
		
		# sanity check the alleles
		if($vf->allele_string =~ /INS|DEL|LARGE|CNV|PROBE|\(\)/) {
			warn("Can't calculate consequences for alleles ".($vf->allele_string));
			next;
		}
		
		my @this_vf_tvs;
			
		## REGULATORY FEATURES
		######################
		
		if(defined $rf_adaptor && defined $ef_adaptor && defined $gene_adaptor && defined $transcript_adaptor) {
			
			# get the feature slice
			my $slice = $vf->feature_Slice;
			
			# invert it if needed
			if($slice->strand < 0) {
				$slice = $slice->invert();<commit_msg>Fix to prevent warning in species with no reg build<commit_after>#
# Ensembl module for Bio::EnsEMBL::Variation::DBSQL::TranscriptVariationAdaptor
#
# Copyright (c) 2004 Ensembl
#
# You may distribute this module under the same terms as perl itself
#
#

=head1 NAME

Bio::EnsEMBL::Variation::DBSQL::TranscriptVariationAdaptor

=head1 SYNOPSIS
  $reg = 'Bio::EnsEMBL::Registry';
  
  $reg->load_registry_from_db(-host => 'ensembldb.ensembl.org',-user => 'anonymous');
  
  $trva = $reg->get_adaptor("human","variation","transcriptvariation");
  $vfa = $reg->get_adaptor("human","variation","variationfeature");
  $tra = $reg->get_adaptor("human","core","transcript");

  # retrieve a TranscriptVariation by its internal identifier

  $trv = $tra->fetch_by_dbID(552);
  print $trv->transcript()->stable_id(), ' ',
        $trv->variation_feature()->variation_name(), ' ',
        $trv->consequence_type(), "\n";

  # retrieve all TranscriptVariations associated with a Transcript

  $tr = $tra->fetch_by_stable_id('ENST00000278995');
  foreach $trv (@{$trva->fetch_all_by_Transcripts([$tr])}) {
    print $trv->variation_feature->variation_name(), ' ', $trv->consequence_type(), "\n";
  }

  # retrieve all TranscriptVariations associated with a VariationFeature

  $vf = $vfa->fetch_by_dbID(99123);
  foreach $trv (@{$trva->fetch_all_by_VariationFeatures($vf)}) {
    print $trva->transcript->stable_id(), ' ', $trva->consequence_type(), "\n";
  }


=head1 DESCRIPTION

This adaptor provides database connectivity for TranscriptVariation objects.
TranscriptVariations which represent an association between a variation and
a Transcript may be retrieved from the Ensembl variation database via several
means using this module.

=head1 AUTHOR - <NAME>

=head1 CONTACT

Post questions to the Ensembl development list <EMAIL>

=head1 METHODS

=cut

use strict;
use warnings;

package Bio::EnsEMBL::Variation::DBSQL::TranscriptVariationAdaptor;

use Bio::EnsEMBL::DBSQL::BaseAdaptor;
use Bio::EnsEMBL::Utils::Exception qw(throw warning);
use Bio::EnsEMBL::Utils::Sequence qw(reverse_comp expand);
use Bio::EnsEMBL::Utils::TranscriptAlleles qw(type_variation);

use Bio::EnsEMBL::Variation::TranscriptVariation;

our @ISA = ('Bio::EnsEMBL::DBSQL::BaseAdaptor');

# size that we look either side of a variation for transcripts
our $UP_DOWN_SIZE = 5000;

=head2 fetch_all_by_Transcripts

    Arg[1]      : listref of Bio::EnsEMBL::Transcript
    Example     : $tr = $ta->fetch_by_stable_id('ENST00000278995');
                  @tr_vars = @{$tr_var_adaptor->fetch_all_by_Transcripts([$tr])};
    Description : Retrieves all TranscriptVariation objects associated with
                  provided Ensembl Transcript. Attaches them to the TranscriptVariation
    ReturnType  : ref to list of Bio::EnsEMBL::Variation::TranscriptVariations
    Exceptions  : throw on bad argument
    Caller      : general
    Status      : Stable

=cut

sub fetch_all_by_Transcripts{
    my $self = shift;
    my $transcript_ref = shift;

    if (ref($transcript_ref) ne 'ARRAY' or ! $transcript_ref->[0]->isa('Bio::EnsEMBL::Transcript')){
      throw('Array Bio::EnsEMBL::Transcript expected');
    }
    
    my %tr_by_id;

    foreach my $tr (@{$transcript_ref}) {
      if (!$tr->isa('Bio::EnsEMBL::Transcript')){
	throw('Bio::EnsEMBL::Transcript is expected');
      }
      $tr_by_id{$tr->stable_id()} = $tr;
    }

    my $instr = join (",", map{"'$_'"} keys( %tr_by_id));
    my $transcript_variations = $self->generic_fetch( "tv.transcript_stable_id in ( $instr )");
    for my $tv (@{$transcript_variations}){
	#add to the TranscriptVariation object all the Transcripts
	$tv->{'transcript'} = $tr_by_id{$tv->{'_transcript_stable_id'}};
	delete $tv->{'_transcript_stable_id'}; #remove the transcript_id from the transcript_variation object
    }
    return $transcript_variations;
}

=head2 fetch_all_by_VariationFeatures

  Arg [1]    : Bio::EnsEMBL::Variation::VariationFeature $vf
  Example    :
     $vf = $vf_adaptor->fetch_by_dbID(1234);
     @tr_vars = @{$tr_var_adaptor->fetch_all_by_VariationFeatures([$vf])});
  Description: Retrieves all TranscriptVariation objects associated with
               provided Ensembl variation features. Attaches them to the given variation
               features.
  Returntype : ref to list of Bio::EnsEMBL::Variation::TranscriptVariations
  Exceptions : throw on bad argument
  Caller     : general
  Status     : Stable

=cut

sub fetch_all_by_VariationFeatures {
  my $self = shift;
  my $vf_ref = shift;

  if(ref($vf_ref) ne 'ARRAY') {
    throw('Array Bio::EnsEMBL::Variation::VariationFeature expected');
  }

  my %vf_by_id;
  my $tvs;
  
  my @have_dbID;
  my @no_dbID;
  
  foreach my $vf(@$vf_ref) {
	if(defined $vf->dbID) {
		push @have_dbID, $vf;
	}
	
	else {
		push @no_dbID, $vf;
	}
  }
  
  # already existing VFs - get TVs from the database
  if(scalar @have_dbID) {

	%vf_by_id = map {$_->dbID(), $_ } @have_dbID;
	my $instr = join (",",keys( %vf_by_id));
	$tvs = $self->generic_fetch( "tv.variation_feature_id in ( $instr )" );
	for my $tv ( @$tvs ) {
	  #add to the variation feature object all the transcript variations
	  $vf_by_id{ $tv->{'_vf_id'} }->add_TranscriptVariation( $tv );
	  #delete $tv->{'_vf_id'}; #remove the variation_feature_id from the transcript_variation object    
	}
  }
  
  # if we have some de novo VFs, we need to calculate
  if(scalar @no_dbID) {
	
	my $species;
	
	if(defined $self->db) {
		$species = $self->db()->species;
	}
	else {
		$species = $self->{'species'};
	}
	
	unless(defined $species) {
		warn("No species defined in adaptor");
		return [];
	}
	
	my ($rf_adaptor, $ef_adaptor);
	
	# get a feature set adaptor to check whether there is regulatory build
	my $fs_adaptor = Bio::EnsEMBL::DBSQL::MergedAdaptor->new(-species => $species, -type => "FeatureSet");
	if(defined $fs_adaptor && scalar @{$fs_adaptor->fetch_all_by_type('regulatory')}) {
		
		# get functional genomics adaptors
		$rf_adaptor = Bio::EnsEMBL::DBSQL::MergedAdaptor->new(-species => $species, -type => "RegulatoryFeature");
		$ef_adaptor = Bio::EnsEMBL::DBSQL::MergedAdaptor->new(-species => $species, -type => "ExternalFeature");
	}
	
	#unless(defined $rf_adaptor && defined $ef_adaptor) {
	#	warn("Must have functional genomics database attached to consider regulatory features");
	#}
	
	# get a gene and transcript adaptor
	my $gene_adaptor = Bio::EnsEMBL::DBSQL::MergedAdaptor->new(-species => $species, -type => "Gene");
	my $transcript_adaptor = Bio::EnsEMBL::DBSQL::MergedAdaptor->new(-species => $species, -type => "Transcript");
	
	unless(defined $gene_adaptor && defined $transcript_adaptor) {
		warn("Must have core database attached to consider regulatory features");
	}
	
	# iterate through the VFs
	foreach my $vf(@no_dbID) {
		
		# sanity check the alleles
		if($vf->allele_string =~ /INS|DEL|LARGE|CNV|PROBE|\(\)/) {
			warn("Can't calculate consequences for alleles ".($vf->allele_string));
			next;
		}
		
		my @this_vf_tvs;
			
		## REGULATORY FEATURES
		######################
		
		if(defined $rf_adaptor && defined $ef_adaptor && defined $gene_adaptor && defined $transcript_adaptor) {
			
			# get the feature slice
			my $slice = $vf->feature_Slice;
			
			# invert it if needed
			if($slice->strand < 0) {
				$slice = $slice->invert();
{'fraction_non_alphanumeric': 0.1425271263563178, 'fraction_numerical': 0.0075603780189009455, 'mean_word_length': 4.442285714285714, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 133, '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': '5936064', 'n_tokens_mistral': 5311, 'n_tokens_neox': 4826, 'n_words': 1129}