<commit_before>=head1 NAME

=head1 SYNOPSIS

  
=head1 DESCRIPTION


=head1 CONTACT

<EMAIL>

=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::Pipeline::DBSQL::PmatchFeatureAdaptor;

use vars qw(@ISA);
use strict;

# Object preamble - inheriets from Bio::EnsEMBL::DBSQL::BaseAdaptor
use Bio::EnsEMBL::DBSQL::BaseAdaptor;


use Bio::EnsEMBL::Pipeline::Tools::Pmatch::PmatchFeature;

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


sub write_PmatchFeatures {
  my ($self,@features) = @_;

  my $sql = "LOCK TABLES protein WRITE, pmatch_feature WRITE";
  my $lock_st = $self->prepare($sql);
  $lock_st->execute;

  # the eval ensures that we always unlock tables, even on failure
  eval {
      my %protein;

      foreach my $f (@features) {
	  if (!defined($protein{$f->protein_id})) {
	      $protein{$f->protein_id} =  $self->write_protein($f->protein_id,$f->cdna_id);
	  }
	  if(!$f->analysis){
	      $self->throw("cant store ".$f." without an analysis\n");
	  }
	  my $protein_internal_id = $protein{$f->protein_id};

	  if (!defined($protein_internal_id)) {
	      $self->throw("No internal id found for " . $f->protein_id . "\n");
	  }
	  
	  my $query = "insert into pmatch_feature values(null," . 
	      $protein_internal_id . 
	      ",'" . $f->chr_name . 
	      "'," .  $f->start   . 
	      ","  . $f->end     . 
	      ","  . $f->coverage . 
	      ","  .$f->analysis->dbID.")";
	  
	  my $sth = $self->prepare($query);
	  #print STDERR "query = ".$query."\n";
	  my $res = $sth->execute;
	  
      }
  };
  my $err = $@;

  $sql = "UNLOCK TABLES";
  $lock_st = $self->db->prepare($sql);
  $lock_st->execute;

  $err and $self->throw("Had trouble putting Pmatch features into database: $err");
}

sub write_protein {
  my ($self,$protein_id,$cdna_id) = @_;
  if(!defined $protein_id){ $protein_id = ''; }
  if(!defined $cdna_id){ $cdna_id = ''; }
  #print STDERR "writing protein \n";
  my $tmpcdna = $self->get_cdna_id($protein_id);

  if (defined ($tmpcdna)) {
    if ($tmpcdna eq ''){
      my $query = "UPDATE protein SET cdna_id='$cdna_id' WHERE protein_id='$protein_id'";
      my $sth = $self->prepare($query);
      my $res = $sth->execute;
    }

    elsif ($tmpcdna ne $cdna_id) {
      $self->throw("ERROR: Protein $protein_id already exists with different cdna $tmpcdna : $cdna_id\n");
    }

    return $self->get_protein_internal_id($protein_id);

  } else {

    my $query = "insert into protein values(null,'$protein_id','$cdna_id')";
    my $sth = $self->prepare($query);
    my $res = $sth->execute;

    $sth = $self->prepare("select LAST_INSERT_ID()");
    $res = $sth->execute;

    my ($id) = $sth->fetchrow  or $self->throw("Failed to get last insert id");
    
    return $id;
  }
}

sub get_protein_internal_id {
  my ($self,$protein_id) = @_;

  my $query = "select protein_internal_id from protein where protein_id = '$protein_id'";
  my $sth = $self->prepare($query);
  my $res = $sth->execute;

  if ($sth->rows > 0) {
    my $row = $sth->fetchrow_hashref;
    my $id = $row->{'protein_internal_id'};
    return $id;
  }
}

sub delete_protein {
  my ($self,$protein_id) = @_;

  my $internal_id = $self->get_protein_internal_id($protein_id);

  if ($internal_id eq "") {
    $self->throw("Protein $protein_id does not exist in the database");
  }

  my $query = "delete from protein where protein_id = '$protein_id'";

  my $sth = $self->prepare($query);
  my $res = $sth->execute;

  $query  = "delete from pmatch_feature where protein_internal_id = $internal_id";

  $sth    = $self->prepare($query);
  $res    = $sth->execute;

}
    

sub get_PmatchFeatures_by_protein_id {
  my ($self,$prot_id) = @_;

  my $query = "select * from pmatch_feature,protein where protein.protein_internal_id = pmatch_feature.protein_internal_id and protein.protein_id = '$prot_id'";

  my $sth = $self->prepare($query);<commit_msg>Added logic_name arg to fetch_by_protein_id<commit_after>=head1 NAME

=head1 SYNOPSIS

  
=head1 DESCRIPTION


=head1 CONTACT

<EMAIL>

=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::Pipeline::DBSQL::PmatchFeatureAdaptor;

use vars qw(@ISA);
use strict;

# Object preamble - inheriets from Bio::EnsEMBL::DBSQL::BaseAdaptor
use Bio::EnsEMBL::DBSQL::BaseAdaptor;


use Bio::EnsEMBL::Pipeline::Tools::Pmatch::PmatchFeature;

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


sub write_PmatchFeatures {
  my ($self,@features) = @_;

  my $sql = "LOCK TABLES protein WRITE, pmatch_feature WRITE";
  my $lock_st = $self->prepare($sql);
  $lock_st->execute;

  # the eval ensures that we always unlock tables, even on failure
  eval {
      my %protein;

      foreach my $f (@features) {
	  if (!defined($protein{$f->protein_id})) {
	      $protein{$f->protein_id} =  $self->write_protein($f->protein_id,$f->cdna_id);
	  }
	  if(!$f->analysis){
	      $self->throw("cant store ".$f." without an analysis\n");
	  }
	  my $protein_internal_id = $protein{$f->protein_id};

	  if (!defined($protein_internal_id)) {
	      $self->throw("No internal id found for " . $f->protein_id . "\n");
	  }
	  
	  my $query = "insert into pmatch_feature values(null," . 
	      $protein_internal_id . 
	      ",'" . $f->chr_name . 
	      "'," .  $f->start   . 
	      ","  . $f->end     . 
	      ","  . $f->coverage . 
	      ","  .$f->analysis->dbID.")";
	  
	  my $sth = $self->prepare($query);
	  #print STDERR "query = ".$query."\n";
	  my $res = $sth->execute;
	  
      }
  };
  my $err = $@;

  $sql = "UNLOCK TABLES";
  $lock_st = $self->db->prepare($sql);
  $lock_st->execute;

  $err and $self->throw("Had trouble putting Pmatch features into database: $err");
}

sub write_protein {
  my ($self,$protein_id,$cdna_id) = @_;
  if(!defined $protein_id){ $protein_id = ''; }
  if(!defined $cdna_id){ $cdna_id = ''; }
  #print STDERR "writing protein \n";
  my $tmpcdna = $self->get_cdna_id($protein_id);

  if (defined ($tmpcdna)) {
    if ($tmpcdna eq ''){
      my $query = "UPDATE protein SET cdna_id='$cdna_id' WHERE protein_id='$protein_id'";
      my $sth = $self->prepare($query);
      my $res = $sth->execute;
    }

    elsif ($tmpcdna ne $cdna_id) {
      $self->throw("ERROR: Protein $protein_id already exists with different cdna $tmpcdna : $cdna_id\n");
    }

    return $self->get_protein_internal_id($protein_id);

  } else {

    my $query = "insert into protein values(null,'$protein_id','$cdna_id')";
    my $sth = $self->prepare($query);
    my $res = $sth->execute;

    $sth = $self->prepare("select LAST_INSERT_ID()");
    $res = $sth->execute;

    my ($id) = $sth->fetchrow  or $self->throw("Failed to get last insert id");
    
    return $id;
  }
}

sub get_protein_internal_id {
  my ($self,$protein_id) = @_;

  my $query = "select protein_internal_id from protein where protein_id = '$protein_id'";
  my $sth = $self->prepare($query);
  my $res = $sth->execute;

  if ($sth->rows > 0) {
    my $row = $sth->fetchrow_hashref;
    my $id = $row->{'protein_internal_id'};
    return $id;
  }
}

sub delete_protein {
  my ($self,$protein_id) = @_;

  my $internal_id = $self->get_protein_internal_id($protein_id);

  if ($internal_id eq "") {
    $self->throw("Protein $protein_id does not exist in the database");
  }

  my $query = "delete from protein where protein_id = '$protein_id'";

  my $sth = $self->prepare($query);
  my $res = $sth->execute;

  $query  = "delete from pmatch_feature where protein_internal_id = $internal_id";

  $sth    = $self->prepare($query);
  $res    = $sth->execute;

}
    

sub get_PmatchFeatures_by_protein_id {
  my ($self, $prot_id, $logic_name) = @_;

  my $query = "select * from pmatch_feature,protein where protein.protein_internal_id = pmatch_feature.protein_internal_id and protein.protein_id = '$prot_id'";

  if (defined $logic_name) {
    my $ana_id;
    eval {
      my $ana = $self->db->get_AnalysisAdaptor->fetch_by_logic_name($logic_name);
      $ana_id = $ana->dbID;
    };
    $@ and $self->throw("No analysis with logic_name '$logic_name' exists");
    
    $query .= " and analysis_id = $ana_id";
  }

  my $sth = $self->prepare($query);
{'fraction_non_alphanumeric': 0.167905972655313, 'fraction_numerical': 0.0014391940513312545, 'mean_word_length': 3.4522156967431927, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 106, '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': '371321', 'n_tokens_mistral': 3247, 'n_tokens_neox': 3011, 'n_words': 675}