[BioRuby] Beautiful code for Bioinformatics
Pjotr Prins
pjotr.public14 at thebird.nl
Mon Feb 13 07:54:30 UTC 2012
OK, here another candidate for the price of beautiful code:
https://github.com/trevor/bioruby-restriction_enzyme/blob/master/lib/bio/util/restriction_enzyme/range/sequence_range/calculated_cuts.rb
Trevor has implemented some hairy logic into the RE code. I mean
hairy, that if it were done by someone else it would become spaghetti
code (plenty of examples there in the real world!). You can see, that
even when choosing sensible names, and explaining the code with good
comments, it may still be hard to understand! But I think
def add_cuts_from_cut_ranges(cut_ranges)
pretty much sums it up :). Still, it is beautiful, because it is hard
to think of doing it better. The Ruby code is short and self
explanatory and RE library has almost become a DSL for cutting
sequences using restriction enzymes. That is beautiful.
Pj.
On Sat, Feb 11, 2012 at 11:27:44PM +0300, George Githinji wrote:
> Hi All
> Beauty is in the eyes of the beholder!
> The Bio-Alignment plugin can read and interconvert a nucleotide
> alignment to an amino acid alignment. I liked the simplicity of how
> PJ has implemented the codon to amino acid conversion helper method
> while taking care of the gaps or undefined aa translations.
>
> # lazily convert to Amino acid (once only)
> def to_aa
> aa = translate
> if not aa
> if gap?
> return '-'
> elsif undefined?
> return 'X'
> else
> raise 'What?'
> end
> end
> aa
> end
>
> This method does not have any ruby 'magic' and is self documenting.
> The gap? and undefined? methods are implemented as simple one line
> standalone methods.
>
> Again I like this simple 'trick' of getting an array of codons from a
> sequence in the codonsequence class.
>
> seq.scan(/\S\S\S/) #gets an array of codons
>
> The longer alternative would be to create a bio::sequence::NA object
> and iterate
> seq = Bio::Sequence::NA.new("blahahahha")
> seq.window_search(3, 3) do |subseq|
> puts subseq
> end
>
> It seems more intuitive to represent a sequence as an array of codon
> objects. In this way the codons have some state and can carry
> 'luggage'. getting the string representation of the sequence is as
> simple as
> def to_s
> @seq.map { |codon| codon.to_s }.join(' ')
> end
>
> To be more DRY, the to_nt method in the same class could be aliased
> from the to_s method
>
> It seems the bio-plugins are a rich source of tricks and great learning.... !
>
>
> On Sat, Feb 11, 2012 at 10:08 PM, Peter Cock <p.j.a.cock at googlemail.com> wrote:
> > On Sat, Feb 11, 2012 at 5:46 PM, Pjotr Prins <pjotr.public14 at thebird.nl> wrote:
> >> Correct me if I am wrong, but has everyone moved across to BioPython
> >> now? Or even to the dark side?
> >>
> >> Pj.
> >
> > I haven't noticed any BioRuby developers posting on the Biopython
> > mailing lists recently - but you'd be welcome ;)
> >
> > On a related topic, my first BioRuby pull request was merged, so
> > there is a little direct cross project contribution going on :)
> >
> > https://github.com/bioruby/bioruby/commit/f33abf9bbd90c3c1e320f06447fdb54ffd094c5d
> >
> > Peter
> > _______________________________________________
> > BioRuby Project - http://www.bioruby.org/
> > BioRuby mailing list
> > BioRuby at lists.open-bio.org
> > http://lists.open-bio.org/mailman/listinfo/bioruby
>
>
>
> --
> ---------------
> Sincerely
> George
> Skype: george_g2
> Blog: http://biorelated.wordpress.com/
> Twitter: http://twitter.com/#!/george_l
>
More information about the BioRuby
mailing list