[ensembl-dev] Registry->get_adaptor and magic strings

Matthew Astley mca at sanger.ac.uk
Fri Nov 26 11:33:16 GMT 2010


On Fri, Nov 26, 2010 at 10:39:48AM +0000, Daniel Sobral wrote:

> Some people during a recent API course commented that
> Registry->get_adaptor() should return a warning or throw an error
> when invalid parameters are passed, instead of just returning
> undef. I also tend to agree with them.

Thank you for remembering to bring this up.  This seems to bite me
each time I come back to Ensembl after not using it for a while.

  my $slice_adaptor = Bio::EnsEMBL::Registry->get_adaptor(qw( Human Core SliceAdaptor ));
  my $slice = $slice_adaptor->fetch_by_region('chromosome', 20, 59_990, 160_000);   # boom!

Michael is right, that I should append ||die to each of these, but
I've never noticed $! or $@ being set after these calls so I have to
construct the whole message myself.

All the example code I've seen cheerfully encourages the view that
each call will return something useful so you don't need to test for
undef.


Another thing that bit me was s/Adaptor/Adapter/ , which can produce
error messages which I find unhelpful:

  my $dba = Bio::EnsEMBL::Registry->get_DBAdaptor(qw( Human core ));
  # If I got the $dba from some other configuration subroutine, there
  # are no spelling clues nearby

  my $ga = $dba->get_GeneAdapter; # boom?!

the error says

  -------------------- WARNING ----------------------
  MSG: Could not find GeneAdapter adaptor in the registry for homo_sapiens core

  FILE: EnsEMBL/DBSQL/DBAdaptor.pm LINE: 900
  CALLED BY: ./ex2.pl  LINE: 13
  Ensembl API version = 60
  ---------------------------------------------------

  -------------------- EXCEPTION --------------------
  MSG: Could not get adaptor GeneAdapter for homo_sapiens core

  STACK Bio::EnsEMBL::DBSQL::DBAdaptor::AUTOLOAD /nfs/.../ensembl/modules/Bio/EnsEMBL/DBSQL/DBAdaptor.pm:905
  STACK toplevel ./ex2.pl:13
  Ensembl API version = 60
  ---------------------------------------------------

which I tend to read as some kind of database problem.

I guess the clue is the AUTOLOAD failure, and I think that is where it
would be more helpful[1] to produce a message like this

  -------------------- EXCEPTION --------------------
  MSG: Bio::EnsEMBL::DBSQL::DBAdaptor::get_GeneAdapter: method is spelled 'get_GeneAdaptor'

  STACK Bio::EnsEMBL::DBSQL::DBAdaptor::AUTOLOAD /nfs/.../ensembl/modules/Bio/EnsEMBL/DBSQL/DBAdaptor.pm:884
  STACK toplevel ./ex2.pl:13
  Ensembl API version = 60
  ---------------------------------------------------


While I'm looking at that AUTOLOAD, is there a reason why
  my $dba = Bio::EnsEMBL::Registry->get_DBAdaptor(qw( Human core ));
  my $g = $dba->get_Gene("foo");
  print "Got a gene? $g\n";

produces this
  Got a gene? Bio::EnsEMBL::DBSQL::GeneAdaptor=HASH(0x8c0c4c0)

?  I would be expecting a Gene, but receiving a GeneAdaptor.


I believe it's worth polishing these methods because they make a first
impression on newcomers...

-- 
Matthew

[1]
Index: DBAdaptor.pm
===================================================================
RCS file: /cvsroot/ensembl/ensembl/modules/Bio/EnsEMBL/DBSQL/DBAdaptor.pm,v
retrieving revision 1.212
diff -u -r1.212 DBAdaptor.pm
--- DBAdaptor.pm	26 Aug 2010 13:59:36 -0000	1.212
+++ DBAdaptor.pm	26 Nov 2010 11:24:33 -0000
@@ -883,6 +883,8 @@
   my $type;
   if ( $AUTOLOAD =~ /^.*::get_(\w+)Adaptor$/ ) {
     $type = $1;
+  } elsif ( $AUTOLOAD =~ /^.*::get_(\w+)Adapter$/ ) {
+      throw( sprintf( "%s: method is spelled 'get_${1}Adaptor'\n", $AUTOLOAD ) );
   } elsif ( $AUTOLOAD =~ /^.*::get_(\w+)$/ ) {
     $type = $1;
   } else {




More information about the Dev mailing list