<commit_before># EnsEMBL Gene reading writing adaptor for mySQL
#
# Copyright EMBL-EBI 2001
#
# Author: <NAME>
# 
# Date : 15.07.2002
#

=head1 NAME

Bio::EnsEMBL::DBSQL::GeneLiteAdaptor - 
MySQL Database queries to retrieve genes quickly from denormalized tables.

=head1 SYNOPSIS

=head1 CONTACT

  <NAME>: <EMAIL>
  <NAME>  : <EMAIL>

=head1 APPENDIX

=cut

use strict;

package Bio::EnsEMBL::Lite::SNPAdaptor;

use Bio::EnsEMBL::DBSQL::BaseAdaptor;
use Bio::EnsEMBL::DBEntry;
use Bio::EnsEMBL::SNP;

use vars '@ISA';


@ISA = qw(Bio::EnsEMBL::DBSQL::BaseAdaptor);


=head2 fetch_all_by_Slice

  Arg  1    : Bio::EnsEMBL::Slice $slice
              The slice we want SNPs on
  Function  : retrieve all the SNPs on this slice. 
              uses Lite databases transcript to get info
  Returntype: listreference of Bio::EnsEMBL::ExternalData::Variation
  Exceptions: none
  Caller    : Bio::EnsEMBL::Slice

=cut

sub fetch_all_by_Slice {
  my ($self, $slice ) = @_;

  my @snps;

  my $from_strand = $slice->strand();

  # wherever this slice is, it needs to be converted to
  # a toplevel slices since all snps in the lite database 
  # are stored on toplevel seqregions

  my @projection = @{$slice->project('toplevel')};

  my $link_col_idx = 12;
  my @link_dbs = ('dbSNP', 'WI', 'HGBASE', 'TSC-CSHL', 'ANO');

  my %link_cache = ();

  foreach my $segment (@projection) {
    my $from_start = $segment->from_start();
    my $from_end   = $segment->from_end();
    my $top_slice  = $segment->to_Slice();
    my $top_slice_start  = $top_slice->start();
    my $top_slice_end    = $top_slice->end();
    my $top_slice_strand = $top_slice->strand();

    my $sth = $self->prepare
      ("SELECT internal_id, chr_start, chr_end, chr_strand, type, " .
       "       range_type, validated, alleles, snpclass, mapweight, ".
       "       ambiguity, source, id_refsnp, id_wi, id_hgbase, id_tsc, " .
       "       id_ano " .
       "FROM   snp " .
       "WHERE  chr_name = ? " .
       "AND    chr_start >= ? " .
       "AND    chr_start <= ? " .
       "AND    chr_end >= ?");

    $sth->execute($top_slice->seq_region_name(),
                  $top_slice_start - 500,
                  $top_slice_end,
                  $top_slice_start);

    while(my $arrayref = $sth->fetchrow_arrayref()) {
      my @links = ();

      # loop over the last columns of the row to retrieve a single external
      # database link for each column

      for(my $i = 0; $i < scalar(@link_dbs); $i++) {
        my $link_id = $arrayref->[$link_col_idx + $i];

        next if(!$link_id);

        my $link_db = $link_dbs[$i];
        my $link = $link_cache{"$link_db:$link_id"};

        if(!$link) {
          $link = Bio::EnsEMBL::DBEntry->new_fast
            ({'dbname' => $link_db,
              'primary_id' => $link_id,
              'display_id' => $link_id});
          $link_cache{"$link_db:$link_id"} = $link;
        }

        push @links, $link;
      }

      #create a snp object through a fast (hacky) constructor
      my $status = $arrayref->[6];
      $status =~ s/-/ /;
      if($status && $status ne 'no info') {
        $status = "proven $status";
      } else {
        $status = 'suspected';
      }

      # coordinates must be adjusted so that they are first 
      # relative to the start of the top level slice (rather than absolute)
      # and then adjusted so they are relative to the start of the
      # original requested slice (w/ from_start)
      my($start,$end,$strand);
      if($top_slice_strand == 1) {
        $start = $arrayref->[1] - $top_slice_start + $from_start;
        $end   = $arrayref->[2] - $top_slice_start + $from_start;
        $strand = $arrayref->[3];
      } else {
        $start = $top_slice_end - $arrayref->[2] + $from_start;
        $end   = $top_slice_end - $arrayref->[1] + $from_start;
        $strand = $arrayref->[3] * -1;
      }

      push @snps, Bio::EnsEMBL::SNP->new_fast
        ({ 'dbID'       => $arrayref->[0],
           '_gsf_start'  => $start,
           '_gsf_end'    => $end,
           '_snp_strand' => $strand,
           '_gsf_score'  => 1,
           '_type'       => $arrayref->[4],
           '_range_type' => $arrayref->[5],
           '_validated'  => $arrayref->[6],
           'status'     => $status,
           'alleles'    => $arrayref->[7],
           '_ambiguity_code' => $arrayref->[10],
           '_snpclass'   => $arrayref->[8],
           '_mapweight'  => $arrayref->[9],
           '_source' => $arrayref->[11],
           '_source_tag' => $arrayref->[11],
           'link'        => \@links });

    }
  }
	
  return \@snps;
}

sub fetch_all_by_Slice_transcript_ids {
  my $self = shift;
  my $slice = shift;
  my $transcript_ids = shift;
  my $DB             = shift || 'core';
  my $snps = $self->fetch_all_by_Slice( $slice );
  my %SNPS = ();
  foreach my $transid ( @{$transcript_ids||[]} ) {
  warn "TRANSCRIPT: $transid";
    my $sth = $self->prepare(qq(select gs.snp_id, gs.type, gs.aminoacid_start,
                gs.aminoacid_offset, gs.wildtype_aminoacid,
                gs.aminoacids, s.internal_id, s.chr_start
           from gene_snp as gs, snp as s
          where gs.transcript_id = ? and gs.db = "$DB" and
                gs.snp_id = s.snp_id
    ));
    $sth->execute( $transid );
    while(my $a = $sth->fetchrow_arrayref()) {
      $SNPS{$a->[6]}{$transid} = [ @$a ];
    }
  }
  foreach my $snp ( @$snps ) {
    $snp->{'_transcripts'} = {};
    my $snptype = '99:';
    if( $SNPS{$snp->dbID} ) {
      foreach my $transid ( keys %{$SNPS{$snp->dbID}} ) {
        my $a = $SNPS{$snp->dbID}{$transid};
        $snp->{'_transcripts'}{$transid} = $a;
        warn $snp->dbID, $a->[1];
        $snptype = $a->[1] if $a->[1] lt $snptype;<commit_msg>Removed warning<commit_after># EnsEMBL Gene reading writing adaptor for mySQL
#
# Copyright EMBL-EBI 2001
#
# Author: <NAME>
# 
# Date : 15.07.2002
#

=head1 NAME

Bio::EnsEMBL::DBSQL::GeneLiteAdaptor - 
MySQL Database queries to retrieve genes quickly from denormalized tables.

=head1 SYNOPSIS

=head1 CONTACT

  <NAME>: <EMAIL>
  <NAME>  : <EMAIL>

=head1 APPENDIX

=cut

use strict;

package Bio::EnsEMBL::Lite::SNPAdaptor;

use Bio::EnsEMBL::DBSQL::BaseAdaptor;
use Bio::EnsEMBL::DBEntry;
use Bio::EnsEMBL::SNP;

use vars '@ISA';


@ISA = qw(Bio::EnsEMBL::DBSQL::BaseAdaptor);


=head2 fetch_all_by_Slice

  Arg  1    : Bio::EnsEMBL::Slice $slice
              The slice we want SNPs on
  Function  : retrieve all the SNPs on this slice. 
              uses Lite databases transcript to get info
  Returntype: listreference of Bio::EnsEMBL::ExternalData::Variation
  Exceptions: none
  Caller    : Bio::EnsEMBL::Slice

=cut

sub fetch_all_by_Slice {
  my ($self, $slice ) = @_;

  my @snps;

  my $from_strand = $slice->strand();

  # wherever this slice is, it needs to be converted to
  # a toplevel slices since all snps in the lite database 
  # are stored on toplevel seqregions

  my @projection = @{$slice->project('toplevel')};

  my $link_col_idx = 12;
  my @link_dbs = ('dbSNP', 'WI', 'HGBASE', 'TSC-CSHL', 'ANO');

  my %link_cache = ();

  foreach my $segment (@projection) {
    my $from_start = $segment->from_start();
    my $from_end   = $segment->from_end();
    my $top_slice  = $segment->to_Slice();
    my $top_slice_start  = $top_slice->start();
    my $top_slice_end    = $top_slice->end();
    my $top_slice_strand = $top_slice->strand();

    my $sth = $self->prepare
      ("SELECT internal_id, chr_start, chr_end, chr_strand, type, " .
       "       range_type, validated, alleles, snpclass, mapweight, ".
       "       ambiguity, source, id_refsnp, id_wi, id_hgbase, id_tsc, " .
       "       id_ano " .
       "FROM   snp " .
       "WHERE  chr_name = ? " .
       "AND    chr_start >= ? " .
       "AND    chr_start <= ? " .
       "AND    chr_end >= ?");

    $sth->execute($top_slice->seq_region_name(),
                  $top_slice_start - 500,
                  $top_slice_end,
                  $top_slice_start);

    while(my $arrayref = $sth->fetchrow_arrayref()) {
      my @links = ();

      # loop over the last columns of the row to retrieve a single external
      # database link for each column

      for(my $i = 0; $i < scalar(@link_dbs); $i++) {
        my $link_id = $arrayref->[$link_col_idx + $i];

        next if(!$link_id);

        my $link_db = $link_dbs[$i];
        my $link = $link_cache{"$link_db:$link_id"};

        if(!$link) {
          $link = Bio::EnsEMBL::DBEntry->new_fast
            ({'dbname' => $link_db,
              'primary_id' => $link_id,
              'display_id' => $link_id});
          $link_cache{"$link_db:$link_id"} = $link;
        }

        push @links, $link;
      }

      #create a snp object through a fast (hacky) constructor
      my $status = $arrayref->[6];
      $status =~ s/-/ /;
      if($status && $status ne 'no info') {
        $status = "proven $status";
      } else {
        $status = 'suspected';
      }

      # coordinates must be adjusted so that they are first 
      # relative to the start of the top level slice (rather than absolute)
      # and then adjusted so they are relative to the start of the
      # original requested slice (w/ from_start)
      my($start,$end,$strand);
      if($top_slice_strand == 1) {
        $start = $arrayref->[1] - $top_slice_start + $from_start;
        $end   = $arrayref->[2] - $top_slice_start + $from_start;
        $strand = $arrayref->[3];
      } else {
        $start = $top_slice_end - $arrayref->[2] + $from_start;
        $end   = $top_slice_end - $arrayref->[1] + $from_start;
        $strand = $arrayref->[3] * -1;
      }

      push @snps, Bio::EnsEMBL::SNP->new_fast
        ({ 'dbID'       => $arrayref->[0],
           '_gsf_start'  => $start,
           '_gsf_end'    => $end,
           '_snp_strand' => $strand,
           '_gsf_score'  => 1,
           '_type'       => $arrayref->[4],
           '_range_type' => $arrayref->[5],
           '_validated'  => $arrayref->[6],
           'status'     => $status,
           'alleles'    => $arrayref->[7],
           '_ambiguity_code' => $arrayref->[10],
           '_snpclass'   => $arrayref->[8],
           '_mapweight'  => $arrayref->[9],
           '_source' => $arrayref->[11],
           '_source_tag' => $arrayref->[11],
           'link'        => \@links });

    }
  }
	
  return \@snps;
}

sub fetch_all_by_Slice_transcript_ids {
  my $self = shift;
  my $slice = shift;
  my $transcript_ids = shift;
  my $DB             = shift || 'core';
  my $snps = $self->fetch_all_by_Slice( $slice );
  my %SNPS = ();
  foreach my $transid ( @{$transcript_ids||[]} ) {
 # warn "TRANSCRIPT: $transid";
    my $sth = $self->prepare(qq(select gs.snp_id, gs.type, gs.aminoacid_start,
                gs.aminoacid_offset, gs.wildtype_aminoacid,
                gs.aminoacids, s.internal_id, s.chr_start
           from gene_snp as gs, snp as s
          where gs.transcript_id = ? and gs.db = "$DB" and
                gs.snp_id = s.snp_id
    ));
    $sth->execute( $transid );
    while(my $a = $sth->fetchrow_arrayref()) {
      $SNPS{$a->[6]}{$transid} = [ @$a ];
    }
  }
  foreach my $snp ( @$snps ) {
    $snp->{'_transcripts'} = {};
    my $snptype = '99:';
    if( $SNPS{$snp->dbID} ) {
      foreach my $transid ( keys %{$SNPS{$snp->dbID}} ) {
        my $a = $SNPS{$snp->dbID}{$transid};
        $snp->{'_transcripts'}{$transid} = $a;
        #warn $snp->dbID, $a->[1];
        $snptype = $a->[1] if $a->[1] lt $snptype;
{'fraction_non_alphanumeric': 0.1600972391040111, 'fraction_numerical': 0.009202986629623199, 'mean_word_length': 2.313866513233602, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 147, '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': '4137459', 'n_tokens_mistral': 4392, 'n_tokens_neox': 4144, 'n_words': 936}