=head1 LICENSE

  Copyright (c) 1999-2009 The European Bioinformatics Institute and
  Genome Research Limited.  All rights reserved.

  This software is distributed under a modified Apache license.
  For license details, please see

    http://www.ensembl.org/info/about/code_licence.html

=head1 CONTACT

  Please email comments or questions to the public Ensembl
  developers list at <ensembl-dev@ebi.ac.uk>.

  Questions may also be sent to the Ensembl help desk at
  <helpdesk@ensembl.org>.

=cut

=head1 NAME

Bio::EnsEMBL::DBSQL::AffyFeatureAdaptor - A database adaptor for fetching and
storing AffyFeature objects.

=head1 SYNOPSIS

  my $afa = $db->get_AffyFeatureAdaptor();

  my $features = $afa->fetch_all_by_AffyProbe($probe);
  $features =
    $afa->fetch_all_by_Slice_arrayname( $slice, 'Affy-1', 'Affy-2' );

=head1 DESCRIPTION

The AffyFeatureAdaptor is a database adaptor for storing and retrieving
AffyFeature objects.

=head1 METHODS

=cut

package Bio::EnsEMBL::DBSQL::AffyFeatureAdaptor;

use strict;
use warnings;

use Bio::EnsEMBL::AffyFeature;
use Bio::EnsEMBL::DBSQL::OligoFeatureAdaptor;

use vars qw(@ISA);
@ISA = qw(Bio::EnsEMBL::DBSQL::OligoFeatureAdaptor);


=head2 fetch_all_by_AffyProbe

  Arg [1]    : Bio::EnsEMBL::AffyProbe
  Example    : my $features = $afa->fetch_all_by_AffyProbe($probe);
  Description: Fetchs all features that a given probe creates.
  Returntype : Listref of Bio::EnsEMBL::AffyFeature objects
  Exceptions : None
  Caller     : AffyProbe->get_all_AffyFeatures()
  Status     : Medium Risk

=cut

sub fetch_all_by_AffyProbe {
	my $self  = shift;
	my $probe = shift;
	
	return $self->fetch_all_by_Probe($probe);
}

=head2 _default_where_clause

  Args       : None
  Example    : None
  Description: PROTECTED implementation of superclass abstract method.
               Returns an additional table joining constraint to use for
			   queries.
  Returntype : List of strings
  Exceptions : None
  Caller     : Internal
  Status     : Medium Risk

=cut
sub _default_where_clause {
	my $self = shift;
	
	return "
		    of.oligo_probe_id = op.oligo_probe_id
		AND op.oligo_array_id = oa.oligo_array_id
		AND oa.type='AFFY'
	";
}

=head2 _new_fast

  Args       : Hashref to be passed to AffyFeature->new_fast()
  Example    : None
  Description: Construct an AffyFeature object using quick and dirty new_fast.
  Returntype : Bio::EnsEMBL::AffyFeature
  Exceptions : None
  Caller     : _objs_from_sth
  Status     : Medium Risk

=cut

sub _new_fast {
	my $self = shift;
	
	my $hash_ref = shift;
	return Bio::EnsEMBL::AffyFeature->new_fast($hash_ref);
}

=head2 list_dbIDs

  Args       : None
  Example    : my @feature_ids = @{$afa->list_dbIDs()};
  Description: Gets an array of internal IDs for all AffyFeature objects
               in the current database.  NOTE: In a multi-species
               database, this method will return the dbIDs of all
               AffyFeature objects, not just the ones associated with
               the current species.
  Returntype : List of ints
  Exceptions : None
  Caller     : ?
  Status     : Medium Risk

=cut

sub list_dbIDs {
	my $self = shift;
	
	#return $self->_list_dbIDs('oligo_feature');
	# Can't use _list_dbIDs because only want OligoProbe objects on arrays of type AFFY
	
	my @out;

        # FIXME: This SQL will not work as expected on multi-species
        # databases.  It needs to be anchored in a coord_system entry
        # coord_system.species_id = $self->species_id(). /ak4@2008-07-15
	my $sql = "
		SELECT DISTINCT of.oligo_feature_id
		FROM   oligo_feature of, oligo_probe op, oligo_array oa
		WHERE  of.oligo_probe_id=op.oligo_probe_id
		AND    op.oligo_array_id=oa.oligo_array_id
		AND    oa.type='AFFY'
	";
	my $sth = $self->prepare($sql);
	$sth->execute;
	
	while (my ($id) = $sth->fetchrow() ) {
		push @out, $id;
	}
	
	$sth->finish;
	
	return \@out;
}

1;

