Thursday, 27 September 2007

Bio::Graphics and rails

As a follow up to my post on Bio::Graphics, I tried integrating this library in a rails application. After all, you'd get your data either from a file (like GFF) or a database. And let me tell you: it took me just 30 minutes or so to get a proof-of-concept running. This included installing rails itself, creating the rails app, creating the database, loading dummy data, and doing the coding itself. That 30 minutes was interrupted for a couple of hours, because I needed some advice from Kouhei Sutou, the author of rcairo, on how to write PNG images in memory instead of to a file.

So how do you do it? The proof-of-concept little database I created contained 3 tables:

  • chromosomes (columns: id, name, length)
  • tracks (columns: id, name, glyph, colour)
  • features (columns: id, chromosome_id, track_id, name, location, url)
Create some features for a couple of different tracks for a particular chromosome.

In views/chromosomes/show.rhtml, add the following line:

<%= @chromosome.to_png %>


My models/chromosome.rb looks like this:

require 'stringio'
require 'base64'
require_gem 'bio-graphics'

class Chromosome < through =""> :features

def to_png(width = 800, start = 1, stop = self.length)
return %{}
end

def draw(width, start, stop)
panel = Bio::Graphics::Panel.new(self.length, width, false, start, stop)
track_container = Hash.new
self.tracks.each do |track|
if ! track_container.has_key?(track.name)
track_container[track.name] = panel.add_track(track.name, track.colour.split(',').collect{|i| i.to_i}, track.glyph)
end
end

self.features.each do |feature|
track_container[feature.track.name].add_feature(feature.name, feature.location)
end

output = StringIO.new
panel.draw(output)
return output.string
end
end


UPDATE: Apparently, Blogger does not allow me to paste the correct code above. In the to_png method, replace the following ascii codes:

  • %7B with {

  • %28 with (

  • %29 with )

  • %7D with }



And that's it. I leave the integration of my ensembl-api, bio-graphics and rails as an exercise for the reader. We could make a ruby version of the Ensembl browser... and then: world domination. Mwahaha.

2 comments:

Roger said...

Ensembl uses ensembl-draw, not Bio::Graphics (which came later with Gbrowse).

Jan Aerts said...

Thanks for clearing that up, Roger. I've changed it in the post on the earlier post on ruby and graphics now.

Post a Comment