Monday, February 09, 2009

Ruby Love

I've dissed Ruby here, here and here. But, I really like Ruby, so it's time to show Ruby some love. Here's some little snippets that I came up with that I think are slicker than shit.

You could hardly ask for a more concise yet readable way for taking the reverse compliment of a piece of DNA sequence.

# given a DNA sequence in a string return its reverse complement
def reverse_complement(seq)
  return seq.reverse().tr!('ATCGatcg','TAGCtagc')
end

Next, I wanted to parse out a pair of coordinates of the form "1600481-1600540". The first number is the start position and the second is the end. The interval is inclusive and one-based. I want the coordinates converted to zero-based integers.

s,e = coords.split("-").collect {|x| x.to_i - 1}

On second thought, why doesn't Ruby's Range class have a from_string method? Oops, another quirk.

No comments:

Post a Comment