Exploring Bioinformatics with Ruby

Update, April 4th: I wrote a quick explanation for some of the code here

I wrote a script some time ago that ported the examples in this Python article to Ruby(check out BioRuby for a large scale approach to Bioinformatics). Some sparse documentation can be found at my old(but still usable website) here. Note that I do not actively update the website where the documentation is being kept.

NOTE WELL:

This code is not very well-tested yet, so watch your head. This code is licensed under GPLv2. You can read the license and disclaimers here. VIEWING TIP: In Mac Firefox press Apple++ to enlarge the viewing size.

#!/usr/bin/env ruby
#==Bioinformatics Usage Examples
#Instantiate a DNA object
#=Constructor
#d = DNA.new(“CGGGG”) => DNA object
#=Reversing a strand
#d.reverse => “GGGGC”
#=Finding the complement
#d.complement => “CCCCG”
#=Find the codons in a given strand
#d.codons => [“CGG”]

class DNA
#Holds the base complement information for all instances of the class
@@basecomplement = {a => t, c => g, t => a, g => c}

def initialize(s)
#Example: d = DNA.new
@seq = s.downcase
@len = s.length
end

#Return as RNA string
#Example d.transcribe
def transcribe
@seq.gsub(t,u)
end

#Return DNA string in reverse order
def reverse
@seq.reverse
end

#Internal function (do not use)
def reverse!
@seq.reverse!
end

#Complement function
def complement
compl(@seq)
end

#Internal function (do not use)
def compl(s)
s = s.split(/\B/).to_a.collect!{|base| @@basecomplement[base]}.to_s
end

#reversecomplement function
def reversecomplement
reverse!
complement
end

#statistics function
def gc
gc = @seq.count(g) + @seq.count(c)
gc * 100.0 / @seq.length()
end

#Get patterns of three bases
def codons
codons = Array.new
ending = @len – (@len % 3) – 1
0.step(ending,3){|i| codons.push(@seq[i..i+2])}
return codons
end
end

if __FILE__ == $0
d = DNA.new(CGG)
puts(d.reverse())
end

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s