<commit_before>#
# Ensembl module for Bio::EnsEMBL::DBSQL::Funcgen::ProbeFeatureAdaptor
#
# You may distribute this module under the same terms as Perl itself

=head1 NAME

Bio::EnsEMBL::DBSQL::Funcgen::ProbeFeatureAdaptor - A database adaptor for fetching and
storing ProbeFeature objects.

=head1 SYNOPSIS

my $ofa = $db->get_ProbeFeatureAdaptor();

my $features = $ofa->fetch_all_by_Probe($probe);
$features = $ofa->fetch_all_by_Slice_arrayname($slice, 'Array-1', 'Array-2');

=head1 DESCRIPTION

The ProbeFeatureAdaptor is a database adaptor for storing and retrieving
ProbeFeature objects.

=head1 AUTHOR

This module was created by <NAME>.

This module is part of the Ensembl project: http://www.ensembl.org/

=head1 CONTACT

Post comments or questions to the Ensembl development list: <EMAIL>

=head1 METHODS

=cut

use strict;
use warnings;

package Bio::EnsEMBL::Funcgen::DBSQL::ProbeFeatureAdaptor;

use DBI;
use Bio::EnsEMBL::Utils::Exception qw( throw warning );
use Bio::EnsEMBL::Funcgen::ProbeFeature;
use Bio::EnsEMBL::Funcgen::DBSQL::BaseFeatureAdaptor;
use Bio::EnsEMBL::Funcgen::DBSQL::BaseAdaptor;
use vars qw(@ISA);
use strict;
use warnings;

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

my @true_tables = (	[ 'probe_feature', 'pf' ], [ 'probe',   'p' ]);
#Include probe as we are always going to need this for querying
#And will most likely need probe.name and/or probe.probe_set_id
my @tables = @true_tables;

my $true_final_clause = ' ORDER BY pf.seq_region_id, pf.seq_region_start, pf.probe_feature_id';
#Do we really need this probe_feature_id in the order by?

my $final_clause = $true_final_clause;


=head2 fetch_all_by_Probe

  Arg [1]    : Bio::EnsEMBL::Funcgen::Probe
  Example    : my $features = $ofa->fetch_all_by_Probe($probe);
  Description: Fetchs all features that a given probe creates.
  Returntype : Listref of Bio::EnsEMBL::PasteFeature objects
  Exceptions : Throws if argument is not a stored Probe object
  Caller     : Probe->get_all_ProbeFeatures()
  Status     : At Risk

=cut

sub fetch_all_by_Probe {
  my $self  = shift;
  my $probe = shift;
  
  if ( !ref($probe) && !$probe->isa('Bio::EnsEMBL::Funcgen::Probe') ) {
    throw('fetch_all_by_Probe requires a Bio::EnsEMBL::Funcgen::Probe object');
  }

  if ( !defined $probe->dbID() ) {
    throw('fetch_all_by_Probe requires a stored Bio::EnsEMBL::Funcgen::Probe object');
  }
	
  return $self->generic_fetch( 'pf.probe_id = ' . $probe->dbID() );
}

=head2 fetch_all_by_probe_id

  Arg [1]    : int - Probe dbID
  Example    : my @features = @{$ofa->fetch_all_by_Probe_id($pid)};
  Description: Fetchs all features that a given probe creates.
  Returntype : Listref of Bio::EnsEMBL::PasteFeature objects
  Exceptions : Throws if argument not defined
  Caller     : Probe->get_all_ProbeFeatures()
  Status     : At Risk

=cut

sub fetch_all_by_probe_id {
  my $self  = shift;
  my $pid = shift;
  
  if ( ! defined $pid ) {
    throw('Need to specify a probe _id');
  }
	
  return $self->generic_fetch( 'pf.probe_id = ' . $pid );
}



=head2 fetch_all_by_probeset

  Arg [1]    : string - probeset
  Example    : my $features = $ofa->fetch_all_by_probeset('Set-1');
  Description: Fetchs all features that a given probeset creates.
  Returntype : Listref of Bio::EnsEMBL::ProbeFeature objects
  Exceptions : Throws if no probeset argument
  Caller     : General
  Status     : At Risk - need to add vendor/class to this?

=cut

sub fetch_all_by_probeset {
	my ($self, $probeset) = @_;
	
	if (! $probeset) {
	  throw('fetch_all_by_probeset requires a probeset name argument');
	}
	
	push @tables, (['probe_set', 'ps']);

	#Need to protect against SQL injection here due to text params

	my $constraint = ' ps.name=? AND ps.probe_set_id=p.probe_set_id';
	$final_clause = ' GROUP by pf.probe_feature_id '.$final_clause;	

	$self->bind_param_generic_fetch($probeset,  SQL_VARCHAR);
	
	my $features = $self->generic_fetch($constraint);
	@tables = @true_tables;
	$final_clause = $true_final_clause;

	return $features;
}


#Need to add:<commit_msg>added coord_system clause to fetch_all_by_probeset<commit_after>#
# Ensembl module for Bio::EnsEMBL::DBSQL::Funcgen::ProbeFeatureAdaptor
#
# You may distribute this module under the same terms as Perl itself

=head1 NAME

Bio::EnsEMBL::DBSQL::Funcgen::ProbeFeatureAdaptor - A database adaptor for fetching and
storing ProbeFeature objects.

=head1 SYNOPSIS

my $ofa = $db->get_ProbeFeatureAdaptor();

my $features = $ofa->fetch_all_by_Probe($probe);
$features = $ofa->fetch_all_by_Slice_arrayname($slice, 'Array-1', 'Array-2');

=head1 DESCRIPTION

The ProbeFeatureAdaptor is a database adaptor for storing and retrieving
ProbeFeature objects.

=head1 AUTHOR

This module was created by <NAME>.

This module is part of the Ensembl project: http://www.ensembl.org/

=head1 CONTACT

Post comments or questions to the Ensembl development list: <EMAIL>

=head1 METHODS

=cut

use strict;
use warnings;

package Bio::EnsEMBL::Funcgen::DBSQL::ProbeFeatureAdaptor;

use DBI;
use Bio::EnsEMBL::Utils::Exception qw( throw warning );
use Bio::EnsEMBL::Funcgen::ProbeFeature;
use Bio::EnsEMBL::Funcgen::DBSQL::BaseFeatureAdaptor;
use Bio::EnsEMBL::Funcgen::DBSQL::BaseAdaptor;
use vars qw(@ISA);
use strict;
use warnings;

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

my @true_tables = (	[ 'probe_feature', 'pf' ], [ 'probe',   'p' ]);
#Include probe as we are always going to need this for querying
#And will most likely need probe.name and/or probe.probe_set_id
my @tables = @true_tables;

my $true_final_clause = ' ORDER BY pf.seq_region_id, pf.seq_region_start, pf.probe_feature_id';
#Do we really need this probe_feature_id in the order by?

my $final_clause = $true_final_clause;


=head2 fetch_all_by_Probe

  Arg [1]    : Bio::EnsEMBL::Funcgen::Probe
  Example    : my $features = $ofa->fetch_all_by_Probe($probe);
  Description: Fetchs all features that a given probe creates.
  Returntype : Listref of Bio::EnsEMBL::PasteFeature objects
  Exceptions : Throws if argument is not a stored Probe object
  Caller     : Probe->get_all_ProbeFeatures()
  Status     : At Risk

=cut

sub fetch_all_by_Probe {
  my ($self, $probe, $cs) = @_;
  
  if ( !ref($probe) && !$probe->isa('Bio::EnsEMBL::Funcgen::Probe') ) {
    throw('fetch_all_by_Probe requires a Bio::EnsEMBL::Funcgen::Probe object');
  }

  if ( !defined $probe->dbID() ) {
    throw('fetch_all_by_Probe requires a stored Bio::EnsEMBL::Funcgen::Probe object');
  }
	
  return $self->generic_fetch( 'pf.probe_id = ' . $probe->dbID() );
}

=head2 fetch_all_by_probe_id

  Arg [1]    : int - Probe dbID
  Example    : my @features = @{$ofa->fetch_all_by_Probe_id($pid)};
  Description: Fetchs all features that a given probe creates.
  Returntype : Listref of Bio::EnsEMBL::PasteFeature objects
  Exceptions : Throws if argument not defined
  Caller     : Probe->get_all_ProbeFeatures()
  Status     : At Risk

=cut

sub fetch_all_by_probe_id {
  my ($self, $pid, $cs) = @_;
  
  if ( ! defined $pid ) {
    throw('Need to specify a probe _id');
  }
	
  return $self->generic_fetch( 'pf.probe_id = ' . $pid );
}



=head2 fetch_all_by_probeset

  Arg [1]    : string - probeset
  Example    : my $features = $ofa->fetch_all_by_probeset('Set-1');
  Description: Fetchs all features that a given probeset creates.
  Returntype : Listref of Bio::EnsEMBL::ProbeFeature objects
  Exceptions : Throws if no probeset argument
  Caller     : General
  Status     : At Risk - need to add vendor/class to this?

=cut

sub fetch_all_by_probeset {
	my ($self, $probeset, $coord_systems) = @_;

	#Need to add a clause to restrict to current default assembly
	#As this can return features from an old assembly if they 
	#have not been removed
	
	#Will this long IN be faster than a simple query extension to sr and cs?
	if (! $probeset) {
	  throw('fetch_all_by_probeset requires a probeset name argument');
	}

	my @cs_ids;
	
	if($coord_systems){
	  
	  if(ref($coord_systems) eq 'ARRAY' && 
		 (scalar(@$coord_systems) >0)){
		
		foreach my $cs(@$coord_systems){
		  $self->is_stored_and_valid('Bio::EnsEMBL::Funcgen::CoordSystem', $cs);
		  push @cs_ids, $cs->dbID;
		}
	  }
	  else{
		throw('CoordSystems parameter must be an arrayref of one or more Bio::EnsEMBL::Funcgen::CoordSystems');
	  }
	}

	else{

	  #Get current default cs's
	  foreach my $cs(@{$self->db->get_FGCoordSystemAdaptor->fetch_all($self->db->dnadb, 'DEFAULT')}){
		push @cs_ids, $cs->dbID;
	  }
	}
	  
	#This should never happen
	if(scalar(@cs_ids) == 0){
	  throw('Could not find any default CoordSystems');
	}
	

	
	push @tables, (['probe_set', 'ps'], ['seq_region', 'sr']);

	#Need to protect against SQL injection here due to text params
	my $cs_ids = join(', ', @cs_ids);
	my $constraint = " ps.name=? AND ps.probe_set_id=p.probe_set_id AND pf.seq_region_id=sr.seq_region_id and sr.coord_system_id IN ($cs_ids)";
	$final_clause = ' GROUP by pf.probe_feature_id '.$final_clause;	

	$self->bind_param_generic_fetch($probeset,  SQL_VARCHAR);
	
	my $features = $self->generic_fetch($constraint);
	@tables = @true_tables;
	$final_clause = $true_final_clause;

	return $features;
}


#Need to add:
{'fraction_non_alphanumeric': 0.1226801899007337, 'fraction_numerical': 0.0034527406128614588, 'mean_word_length': 4.6518292682926825, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1788152', 'n_tokens_mistral': 3498, 'n_tokens_neox': 3328, 'n_words': 875}