<commit_before>
#
# BioPerl module for DB::Clone
#
# Cared for by <NAME> <<EMAIL>>
#
# Copyright <NAME>
#
# You may distribute this module under the same terms as perl itself

# POD documentation - main docs before the code

=head1 NAME

Bio::EnsEMBL::DB::Clone - Object representing one clone

=head1 SYNOPSIS

    # $db is Bio::EnsEMBL::DB::Obj 

    @contig = $db->get_Contigs();

    $clone = $db->get_Clone();

    @genes    = $clone->get_all_Genes();

=head1 DESCRIPTION

Represents information on one Clone

=head1 CONTACT

Describe contact details here

=head1 APPENDIX

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

=cut


# Let the code begin...


package Bio::EnsEMBL::DBSQL::Clone;
use vars qw(@ISA);
use strict;

# Object preamble - inheriets from Bio::Root::Object

use Bio::Root::Object;
use Bio::EnsEMBL::DBSQL::Contig;
use Bio::EnsEMBL::DB::CloneI;

@ISA = qw(Bio::Root::Object Bio::EnsEMBL::DB::CloneI);

# new() is inherited from Bio::Root::Object

# _initialize is where the heavy stuff will happen when new is called

sub _initialize {
  my($self,@args) = @_;

  my $make = $self->SUPER::_initialize;

  my ($dbobj,$id) = $self->_rearrange([qw(DBOBJ
					  ID
					  )],@args);

  $id || $self->throw("Cannot make contig db object without id");
  $dbobj || $self->throw("Cannot make contig db object without db object");
  $dbobj->isa('Bio::EnsEMBL::DBSQL::Obj') || $self->throw("Cannot make contig db object with a $dbobj object");

  $self->id($id);
  $self->_dbobj($dbobj);

# set stuff in self from @args
  return $make; # success - we hope!
}


=head2 get_all_Genes

 Title   : get_all_Genes
 Usage   :
 Function:
 Example :
 Returns : 
 Args    :


=cut

sub get_all_Genes{
   my ($self,@args) = @_;
   my @out;
   my $id = $self->id();
   my @genes;

   #
   # A quick trip to the database to pull out from the neighbourhood the genes
   # that might be on this clone.
   #

   my $sth = $self->_dbobj->prepare("select gene from geneclone_neighbourhood where clone = '$id'");

   $sth->execute();

   while( (my $hash = $sth->fetchrow_hashref()) ) {
       push(@genes,$hash->{'gene'});
   }
   
   #
   # A Gene/Clone Neighbourhood positive does not guarentee that a gene is
   # actually on this clone. The SQL statement needs to double check this
   #

   foreach my $geneid ( @genes ) {
       #
       # The aim here is to get all the information for constructing the genes one
       # juicy SQL statement, effectively removing multiple SQL statement gets from this
       # construction.
       #
       
       #
       # I know this SQL statement is silly.
       #
       
       $sth = $self->_dbobj->prepare("select p3.gene,p4.id,p3.id,p1.exon,p1.rank,p2.seq_start,p2.seq_end,p2.created,p2.modified,p2.strand,p2.phase,p5.seq_start,p5.start_exon,p5.seq_end,p5.end_exon,p5.id from contig as p4, transcript as p3, exon_transcript as p1, exon as p2,translation as p5 where p3.gene = '$geneid' and p4.clone = '$id' and p2.contig = p4.id and p1.exon = p2.id and p3.id = p1.transcript and p5.id = p3.translation order by p3.gene,p3.id,p1.rank");
   
       $sth->execute();

       my $current_gene_id       = '';
       my $current_transcript_id = '';

       my ($gene,$trans);

       while( (my $arr = $sth->fetchrow_arrayref()) ) {
	   my ($geneid,$contigid,$transcriptid,$exonid,$rank,$start,$end,$exoncreated,$exonmodified,$strand,$phase,$trans_start,$trans_exon_start,$trans_end,$trans_exon_end,$translationid) = @{$arr};

	   print STDERR "Got exon $exonid\n";

	   if( ! defined $phase ) {
	       $self->throw("Bad internal error! Have not got all the elements in gene array retrieval");
	   }

	   if( $geneid ne $current_gene_id ) {

	       if( $transcriptid eq $current_transcript_id ) {
		   $self->throw("Bad internal error. Switching genes without switching transcripts");
	       } 

	       $gene = Bio::EnsEMBL::Gene->new();
	       $gene->id($geneid);
	       
	       $sth = $self->_dbobj->prepare("select version from gene where id='".$gene->id."'");
	       $sth->execute();
	       my $rowhash = $sth->fetchrow_arrayref();
	       $gene->version($rowhash->{'version'});
	       
	       $gene->add_cloneid_neighbourhood($id);

	       $current_gene_id = $geneid;
	       push(@out,$gene);
	       print STDERR "Made new gene\n";
	   }

	   if( $transcriptid ne $current_transcript_id ) {
	       $trans = Bio::EnsEMBL::Transcript->new();
	       $trans->id($transcriptid);
	       $current_transcript_id = $transcriptid;

	       my $translation = Bio::EnsEMBL::Translation->new();
	       $translation->start        ($trans_start);
	       $translation->end          ($trans_end);
	       $translation->start_exon_id($trans_exon_start);<commit_msg>Fixed bug<commit_after>
#
# BioPerl module for DB::Clone
#
# Cared for by <NAME> <<EMAIL>>
#
# Copyright <NAME>
#
# You may distribute this module under the same terms as perl itself

# POD documentation - main docs before the code

=head1 NAME

Bio::EnsEMBL::DB::Clone - Object representing one clone

=head1 SYNOPSIS

    # $db is Bio::EnsEMBL::DB::Obj 

    @contig = $db->get_Contigs();

    $clone = $db->get_Clone();

    @genes    = $clone->get_all_Genes();

=head1 DESCRIPTION

Represents information on one Clone

=head1 CONTACT

Describe contact details here

=head1 APPENDIX

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

=cut


# Let the code begin...


package Bio::EnsEMBL::DBSQL::Clone;
use vars qw(@ISA);
use strict;

# Object preamble - inheriets from Bio::Root::Object

use Bio::Root::Object;
use Bio::EnsEMBL::DBSQL::Contig;
use Bio::EnsEMBL::DB::CloneI;

@ISA = qw(Bio::Root::Object Bio::EnsEMBL::DB::CloneI);

# new() is inherited from Bio::Root::Object

# _initialize is where the heavy stuff will happen when new is called

sub _initialize {
  my($self,@args) = @_;

  my $make = $self->SUPER::_initialize;

  my ($dbobj,$id) = $self->_rearrange([qw(DBOBJ
					  ID
					  )],@args);

  $id || $self->throw("Cannot make contig db object without id");
  $dbobj || $self->throw("Cannot make contig db object without db object");
  $dbobj->isa('Bio::EnsEMBL::DBSQL::Obj') || $self->throw("Cannot make contig db object with a $dbobj object");

  $self->id($id);
  $self->_dbobj($dbobj);

# set stuff in self from @args
  return $make; # success - we hope!
}


=head2 get_all_Genes

 Title   : get_all_Genes
 Usage   :
 Function:
 Example :
 Returns : 
 Args    :


=cut

sub get_all_Genes{
   my ($self,@args) = @_;
   my @out;
   my $id = $self->id();
   my @genes;

   #
   # A quick trip to the database to pull out from the neighbourhood the genes
   # that might be on this clone.
   #

   my $sth = $self->_dbobj->prepare("select gene from geneclone_neighbourhood where clone = '$id'");

   $sth->execute();

   while( (my $hash = $sth->fetchrow_hashref()) ) {
       push(@genes,$hash->{'gene'});
   }
   
   #
   # A Gene/Clone Neighbourhood positive does not guarentee that a gene is
   # actually on this clone. The SQL statement needs to double check this
   #

   foreach my $geneid ( @genes ) {
       #
       # The aim here is to get all the information for constructing the genes one
       # juicy SQL statement, effectively removing multiple SQL statement gets from this
       # construction.
       #
       
       #
       # I know this SQL statement is silly.
       #
       
       $sth = $self->_dbobj->prepare("select p3.gene,p4.id,p3.id,p1.exon,p1.rank,p2.seq_start,p2.seq_end,p2.created,p2.modified,p2.strand,p2.phase,p5.seq_start,p5.start_exon,p5.seq_end,p5.end_exon,p5.id from contig as p4, transcript as p3, exon_transcript as p1, exon as p2,translation as p5 where p3.gene = '$geneid' and p4.clone = '$id' and p2.contig = p4.id and p1.exon = p2.id and p3.id = p1.transcript and p5.id = p3.translation order by p3.gene,p3.id,p1.rank");
   
       $sth->execute();

       my $current_gene_id       = '';
       my $current_transcript_id = '';

       my ($gene,$trans);

       while( (my $arr = $sth->fetchrow_arrayref()) ) {
	   my ($geneid,$contigid,$transcriptid,$exonid,$rank,$start,$end,$exoncreated,$exonmodified,$strand,$phase,$trans_start,$trans_exon_start,$trans_end,$trans_exon_end,$translationid) = @{$arr};

	   print STDERR "Got exon $exonid\n";

	   if( ! defined $phase ) {
	       $self->throw("Bad internal error! Have not got all the elements in gene array retrieval");
	   }

	   if( $geneid ne $current_gene_id ) {

	       if( $transcriptid eq $current_transcript_id ) {
		   $self->throw("Bad internal error. Switching genes without switching transcripts");
	       } 

	       $gene = Bio::EnsEMBL::Gene->new();
	       $gene->id($geneid);
	       
	       $sth = $self->_dbobj->prepare("select version from gene where id='".$gene->id."'");
	       $sth->execute();
	       my $rowhash = $sth->fetchrow_hashref();
	       $gene->version($rowhash->{'version'});
	       
	       $gene->add_cloneid_neighbourhood($id);

	       $current_gene_id = $geneid;
	       push(@out,$gene);
	       print STDERR "Made new gene\n";
	   }

	   if( $transcriptid ne $current_transcript_id ) {
	       $trans = Bio::EnsEMBL::Transcript->new();
	       $trans->id($transcriptid);
	       $current_transcript_id = $transcriptid;

	       my $translation = Bio::EnsEMBL::Translation->new();
	       $translation->start        ($trans_start);
	       $translation->end          ($trans_end);
	       $translation->start_exon_id($trans_exon_start);
{'fraction_non_alphanumeric': 0.14119618728396355, 'fraction_numerical': 0.008379595684508223, 'mean_word_length': 3.180385288966725, 'pattern_counts': {'":': 0, '<': 11, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5812079', 'n_tokens_mistral': 3474, 'n_tokens_neox': 3199, 'n_words': 846}