Wednesday, October 15, 2008

Perl BioGolf

Do you know what is a Perl Golf problem? It's a general problem formulated and you try to resolve with a minimal number of characters in a perl script, who writes less win. Some times is a good habit to see, admire and think in this beautiful pearls. Commonly there are a lot in the Perl Monks website.

Today I was looking for a more simple and effective subroutine to translate a DNA/RNA sequence into the corresponding peptide version using the typical genetic code, I used the typical solution with a hash storing the code and call the sequence in block with substr or pop/shift.

I found this solutions in a Perl Golf challenge:

# Typical solution hashing the codes:
sub f0 { #by tadman
my %g = (
# . - Stop
# A - Alanine
# C - Cysteine
# D - Aspartic Acid
# E - Glutamic Acid
# F - Phenylalanine
# G - Glycine
# H - Histidine
# I - Isoleucine
# K - Lysine
# L - Leucine
# M - Methionine
# N - Asparagine
# P - Proline
# Q - Glutamine
# R - Arginine
# S - Serine
# T - Threonine
# V - Valine
# W - Tryptophan
# Y - Tyrosine

# Second solution using the non-specific code.
sub f2{ #by MeowChow
my @r = qw(UA[AG]|UGA GC. - UG[UC] GA[UC] GA[AG] UU[UC] GG. CA[UC] AU[^G] - AA[AG] CU.|UU[AG] AUG AA[UC] - CC. CA[AG] CG.|AG[AG] UC.|AG[UC] AC. - GU. UGG - UA[UC] ^);
((my$t=pop)=~s|..?.?|chr 64+(grep$&=~/$r[$_]/,0..26)[0]|eg);$t=~y/@Z/./d;

# Third solution including regex and substitutions
sub f3 { #by no_slogan
s/[a-z]/uc$&x4/eg;@x=/./g;join"",@x[map{$x=0;$x=$x*4|6&ord for/./g;$x/2}pop=~/.../g]

# Fourth solution similar to 3rd.
sub f4 { #by srawls
join"",(/./g)[map{$x=0;$x=$x*4|6&ord for/./g;$x/2}pop=~/.../g]

# Fifth solution inverse from 3rd and 4th
sub f5 { #by tachyon

# Sixth solution and this is the fastest solution
sub f5{ #by tadman
All solutions have less bytes but I added some break-lines to present a more clear code (really?).

I use the last solution, just change the code for ATGC (DNA code) and not AUGC (RNA code).

That's why Perl rules in Bioinformatic.

No comments:

Post a Comment