From cc9bc6f9692b87519bf1862782a44d994d8b6114 Mon Sep 17 00:00:00 2001
From: Alessandro Vullo <avullo@ebi.ac.uk>
Date: Thu, 5 Dec 2013 16:09:37 +0000
Subject: [PATCH] fetch_all_by_biotype as in GeneAdaptor, which implies the
 inclusion of method biotype_contraint Added method count_all_by_biotype as in
 GeneAdaptor.

---
 .../Bio/EnsEMBL/DBSQL/TranscriptAdaptor.pm    | 129 +++++++-----------
 1 file changed, 52 insertions(+), 77 deletions(-)

diff --git a/modules/Bio/EnsEMBL/DBSQL/TranscriptAdaptor.pm b/modules/Bio/EnsEMBL/DBSQL/TranscriptAdaptor.pm
index 06818b65ad..362d37bc61 100644
--- a/modules/Bio/EnsEMBL/DBSQL/TranscriptAdaptor.pm
+++ b/modules/Bio/EnsEMBL/DBSQL/TranscriptAdaptor.pm
@@ -706,100 +706,75 @@ sub fetch_all_by_exon_stable_id {
   return \@trans;
 }
 
-=head2 fetch_all_by_source
-
-  Arg [1]    : String $source
-               listref of $sources
-               The source of the transcript to retrieve. You can also have a reference
-               to a list of sources in the event of needing several.
-  Example    : $transcript = $transcript_adaptor->fetch_all_by_source('ensembl'); 
-               $transcript = $transcript_adaptor->fetch_all_by_source(['ensembl','another_source']);
-  Description: Retrieves an array reference of transcript objects from the 
-               database via its source or sources.
-               The transcript 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 transcript is not found
+
+=head2 fetch_all_by_biotype 
+
+  Arg [1]    : String $biotype 
+               listref of $biotypes
+               The biotype of the transcript to retrieve. You can have as an argument a reference
+               to a list of biotypes
+  Example    : $gene = $transcript_adaptor->fetch_all_by_biotype('protein_coding'); 
+               $gene = $transcript_adaptor->fetch_all_by_biotypes(['protein_coding', 'sRNA', 'miRNA']);
+  Description: Retrieves an array reference of transcript objects from the database via its biotype or biotypes.
+               The transcript 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::Transcript
-  Exceptions : if we cant get the transcript in given coord system
+  Returntype  : listref of Bio::EnsEMBL::Transcript
+  Exceptions : if we cant get the gene in given coord system
   Caller     : general
   Status     : Stable
 
 =cut
 
-sub fetch_all_by_source {
-  my ($self, $source) = @_;
-
-  if (!defined $source){
-      throw("Source or listref of sources expected");
-  }
-  my $constraint;
-  if (ref($source) eq 'ARRAY'){
-      $constraint = "t.source IN (";
-      foreach my $s (@{$source}){
-        $constraint .= "?,";    
-        $self->bind_param_generic_fetch($s,SQL_VARCHAR);
-      }
-      chop($constraint); #remove last , from expression
-      $constraint .= ") and t.is_current = 1";
-      
-  }
-  else{
-      $constraint = "t.source = ? and t.is_current = 1";
-      $self->bind_param_generic_fetch($source,SQL_VARCHAR);
-  }
-  my @transcripts  = @{ $self->generic_fetch($constraint) };
-  return \@transcripts ;
+sub fetch_all_by_biotype {
+  my ($self, $biotype) = @_;
+  my @transcripts = @{$self->generic_fetch($self->biotype_constraint($biotype))};
+  return \@transcripts;
 }
 
-=head2 fetch_all_by_biotype 
+=head2 biotype_constraint 
 
-  Arg [1]    : String $biotype 
+  Arg [1]    : String $biotypes 
                listref of $biotypes
-               The biotype of the gene to retrieve. You can also have a reference
-               to a list of biotypes in the event of needing several.
-  Example    : $transcript = $transcript_adaptor->fetch_all_by_biotype('pseudogene'); 
-               $transcript = $transcript_adaptor->fetch_all_by_biotype(['protein_coding','ambiguous_orf']);
-  Description: Retrieves an array reference of transcript objects from the 
-               database via its biotype or biotypes.
-               The transcript 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 transcript is not found
-               undef is returned instead.
-  Returntype : listref of Bio::EnsEMBL::Transcript
-  Exceptions : if we cant get the transcript in given coord system
+               The biotype of the transcript to retrieve. You can have as an argument a reference
+               to a list of biotypes
+  Description: Used internally to generate a SQL constraint to restrict a transcript query by biotype
+  Returntype  : String
+  Exceptions : If biotype is not supplied
   Caller     : general
   Status     : Stable
 
 =cut
 
-sub fetch_all_by_biotype {
-  my ($self, $biotype) = @_;
-
-  if (!defined $biotype){
-      throw("Biotype or listref of biotypes expected");
-  }
-  my $constraint;
-  if (ref($biotype) eq 'ARRAY'){
-      $constraint = "t.biotype IN (";
-      foreach my $b (@{$biotype}){
-        $constraint .= "?,";    
-        $self->bind_param_generic_fetch($b,SQL_VARCHAR);
-      }
-      chop($constraint); #remove last , from expression
-      $constraint .= ") and t.is_current = 1";
-      
-  }
-  else{
-      $constraint = "t.biotype = ? and t.is_current = 1";
-      $self->bind_param_generic_fetch($biotype,SQL_VARCHAR);
-  }
-  my @transcripts  = @{ $self->generic_fetch($constraint) };
-  return \@transcripts ;
+sub biotype_constraint {
+  my ($self, $biotypes, $inline_variables) = @_;
+  my $constraint = "t.is_current = 1";
+  my $in_statement = $self->generate_in_constraint($biotypes, 't.biotype', SQL_VARCHAR, $inline_variables);
+  $constraint .= " and $in_statement";
+  return $constraint;
 }
 
+=head2 count_all_by_biotype 
+
+  Arg [1]     : String $biotype 
+                listref of $biotypes
+                The biotype of the transcript to retrieve. You can have as an argument a reference
+                to a list of biotypes
+  Example     : $cnt = $transcript_adaptor->count_all_by_biotype('protein_coding'); 
+                $cnt = $transcript_adaptor->count_all_by_biotypes(['protein_coding', 'sRNA', 'miRNA']);
+  Description : Retrieves count of transcript objects from the database via its biotype or biotypes.
+  Returntype  : integer
+  Caller      : general
+  Status      : Stable
+
+=cut
+
+sub count_all_by_biotype {
+  my ($self, $biotype) = @_;
+  return $self->generic_count($self->biotype_constraint($biotype));
+}
 
 =head2 store
 
-- 
GitLab