From af947b107a6edb289a4d1b3d19fead92f47a4f6d Mon Sep 17 00:00:00 2001
From: Alessandro Vullo <avullo@ebi.ac.uk>
Date: Thu, 5 Dec 2013 16:41:48 +0000
Subject: [PATCH] [ENSCORESW-777]. Added GeneAdaptor support for fetching genes
 by source(s).

---
 modules/Bio/EnsEMBL/DBSQL/GeneAdaptor.pm | 49 ++++++++++++++++++++++++
 1 file changed, 49 insertions(+)

diff --git a/modules/Bio/EnsEMBL/DBSQL/GeneAdaptor.pm b/modules/Bio/EnsEMBL/DBSQL/GeneAdaptor.pm
index 50bd2444be..db37ea2bb3 100644
--- a/modules/Bio/EnsEMBL/DBSQL/GeneAdaptor.pm
+++ b/modules/Bio/EnsEMBL/DBSQL/GeneAdaptor.pm
@@ -251,6 +251,55 @@ sub fetch_by_stable_id {
   return $gene;
 }
 
+=head2 fetch_all_by_source
+
+  Arg [1]    : String $source
+               listref of $sources
+               The source of the gene to retrieve. You can have as an argument a reference
+               to a list of sources
+  Example    : $genes = $gene_adaptor->fetch_all_by_source('havana'); 
+               $genes = $gene_adaptor->fetch_all_by_source(['ensembl', 'vega']);
+  Description: Retrieves an array reference of gene objects from the database via its source or sources.
+               The gene will be retrieved in its native coordinate system (i.e.
+               in the coordinate system it is stored in the database). It may
+               be converted to a different coordinate system through a call to
+               transform() or transfer(). If the gene or exon is not found
+               undef is returned instead.
+  Returntype  : listref of Bio::EnsEMBL::Gene
+  Exceptions : if we cant get the gene in given coord system
+  Caller     : general
+  Status     : Stable
+
+=cut
+
+sub fetch_all_by_source {
+  my ($self, $source) = @_;
+  my @genes = @{$self->generic_fetch($self->source_constraint($source))};
+  return \@genes;
+}
+
+=head2 source_constraint 
+
+  Arg [1]    : String $source
+               listref of $sources
+               The source of the gene to retrieve. You can have as an argument a reference
+               to a list of sources
+  Description: Used internally to generate a SQL constraint to restrict a transcript query by source
+  Returntype  : String
+  Exceptions : If source is not supplied
+  Caller     : general
+  Status     : Stable
+
+=cut
+
+sub source_constraint {
+  my ($self, $sources, $inline_variables) = @_;
+  my $constraint = "g.is_current = 1";
+  my $in_statement = $self->generate_in_constraint($sources, 'g.source', SQL_VARCHAR, $inline_variables);
+  $constraint .= " and $in_statement";
+  return $constraint;
+}
+
 =head2 fetch_all_by_biotype 
 
   Arg [1]    : String $biotype 
-- 
GitLab