Rails Biosql Bioruby Biographics

Rails + BioSQL + bioruby + biographics

Summary of this excellent tutorial series by George Githinji, wrt Ubuntu and Rails 2.2.2.

Part 1

1. If rails, rake and rubygems were installed via apt-get, apt-get remove them
2. Install rubygems from source: sudo ruby setup.rb
3. sudo gem install rails (will install rake too), bio, bio-graphics, (optionally - rails_sql_views)
3. MySQL: create database biosql_development
5. Download and install BioSQL schema: source /path/to/biosqldb-mysql.sql
6. Download and install bioperl-db and load NCBI taxon data
7. Create new Rails app, either in IDE (e.g. NetBeans) or from command line
8. Edit file configurations/initializers/override_rails.rb:

class ActiveRecord::Base
  self.pluralize_table_names = false
  self.primary_key_prefix_type = :table_name_with_underscore
end

9. Edit file configurations/initializers/external_libraries.rb:
require 'rubygems'
#load the bioinformatics library
require 'bio'
#load the biographics library
require 'bio-graphics'
#load the sql views extension library
gem 'rails_sql_views'
require 'rails_sql_views'

10. Optional: create Rails-based BioSQL schema - rake db:schema:dump

Note
May have to edit less /usr/lib/ruby/gems/1.8/gems/rails_sql_views-0.6.1/lib/rails_sql_views.rb as follows:

require 'rubygems'
#unless Kernel.respond_to?(:gem)
#  Kernel.send :alias_method, :gem, :require_gem
#end

Part 2

1. Delete public/index.html (in Rails app root directory)
2. Add to configurations/routes.rb:

map.root :controller => "biosequences"

3. For each database table (e.g. biodatabase, taxon, bioentry, biosequence, seqfeature, location), create scaffolds, e.g.:
./generate scaffold Bioentry

File routes.rb should now contain:
map.resources :seqfeatures
map.resources :locations
map.resources :bioentries
map.resources :biosequences
map.resources :taxons
map.resources :biodatabases

4. Edit models
biodatabase.rb
has_many :bioentries #a biodatabase is associated with many bioentries
validates_uniqueness_of :name  #The name foe each biodatabase is unique!

bioentry.rb
belongs_to :biodatabase
belongs_to :taxon
has_one :biosequence

taxon.rb
has_one :bioentry

biosequence.rb
set_primary_key :bioentry_id #biosequence uses bioentry_id as a primary key!
belongs_to :bioentry

location.rb
belongs_to :seqfeature

seqfeature.rb
belongs_to :bioentry
has_many :locations

5. Import some data to BioSQL database using e.g. bp_load_seqdatabase.pl
6. Edit /biosequences/show.html.erb:
<h2><%= @biosequence.bioentry.name%>(<%= @biosequence.alphabet %>)</h2>
<p>Sequence</p>
<%= @biosequence.seq %><br/>
<%= link_to 'Edit', edit_biosequence_path(@biosequence) %>

- should now be able to view http://localhost:3000/biosequences/1 and http://localhost:3000/biosequences/1.xml

7. Add the following lines to top of biosequence.rb:

require 'stringio'
require 'base64'

and edit Class as follows:
def self.draw_graphic(value)
    #get the name and length of the main feature to be drawn
    main_feature = Bioentry.find(value)
    len = main_feature.biosequence.length.to_i
    name = main_feature.name
 
    #create a Biographics panel and add a track
    @my_panel = Bio::Graphics::Panel.new(len,:width=> 900)
    @track = @my_panel.add_track("#{name}",:glyph=>'directed_generic')
 
    #specify the range for the main feature
    main_feature_range = "1..#{len}"
    @track.add_feature(Bio::Feature.new("#{name}",main_feature_range), :label=>" ")
 
    #write the output to memory
    output = StringIO.new
    @my_panel.draw(output)
    return output.string
end

8. Edit biosequence_controller.rb:
def to_image
    begin
      image = Biosequence.draw_graphic(Biosequence.find(params[:id]))
      send_data(image, :filename => "graphic.svg", :disposition => "inline")
      rescue  ActiveRecord::RecordNotFound
      add_error("Error:Attempt to call image without specifying a biosequence  ID")
      redirect_to :action=>'index'
    end
end

9. Add to routes.rb:
map.root :controller => "biosequences"

10. Edit helpers/biosequences_helper.rb:
def render_image(feature_obj)
  image_tag(url_for({:action=>'to_image',:id=>feature_obj}))
end

11. Add to /views/biosequences/show.html.erb:
<%= render_image(@biosequence) %><br/>

Now navigate to http://localhost:3000/biosequences/show/1 where you should see a bio graphic.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License