[BioRuby] Beautiful code for Bioinformatics

Pjotr Prins pjotr.public14 at thebird.nl
Thu Feb 9 10:03:54 UTC 2012


We would like to highlight a project which sets a shining example for
using Ruby for bioinformatics. We will vote on that on this list.

Hereby I invite everyone to have a look at the projects on
http://biogems.info/ and pick out a code snippet you really like.
Send it to the mailing list, and explain why you think you like it.

You can put forward one of your own projects :)

~~~

As an example, I am going to propose an implementation feature in the
biogem tool, which represents a bit of Ruby meta-programming. It is
called opening classes, and can be useful in overriding BioRuby
itself, something I'll show below.

Biogem does not implement everything from scratch. It uses a library
from the Jeweler project, which was never designed to be shared(!).
I.e. Jeweler was subverted for our purposes.

Raoul put this together. The code I refer to lives, for example, in my
forked project:

  https://github.com/pjotrp/bioruby-gem/blob/master/lib/bio-gem/mod/jeweler.rb

what you see here is that Biogem reopens the Jeweler::Generator class with

  class Jeweler
    class Generator 

      (...)

next, it overrides methods in that class. Because the file
(jeweler.rb) gets included *later* than the original class, the
overridden methods get called. The original methods can be renamed,
e.g.

  alias original_render_template render_template

and can be called from the overriding method.

This technique of reopening classes and overriding behaviour is used
in Ruby on Rails (indeed), but also in bio-logger and the
bio-alignment gems. In bio-alignment it is used to make the BioRuby
Bio::Sequence object enumerable, see

  https://github.com/pjotrp/bioruby-alignment/blob/master/lib/bio-alignment/bioruby.rb

Having Bio::Sequence enumerable allows you to use enumerable
functional methods, such as 'map'. For example I can now write

  @seq.map { |codon| codon.to_s }.join(' ')

There is a case to be made to have standard BioRuby use Enumerable.
However BioRuby has to support Ruby 1.8, which differs in this area.
By reopening the class I can make it look like it is part of BioRuby
anyway. I think it is a very powerful feature, and Raoul did the
right thing in making Jeweler work for Biogem. I think it is an
impressive example of beautiful code.

Pj



More information about the BioRuby mailing list