code for Computer Language Shootout

Robert Kern rkern at ucsd.edu
Wed Mar 16 00:38:50 EST 2005


Here's my solution to the problem[1]:

[1] http://shootout.alioth.debian.org/benchmark.php?test=revcomp

import sys
import string

basetable = string.maketrans('ACBDGHKMNSRUTWVYacbdghkmnsrutwvy',
                              'TGVHCDMKNSYAAWBRTGVHCDMKNSYAAWBR')
def revcomp(seqlines, linelength=60, basetable=basetable):
     seq = ''.join(seqlines)
     complement = seq.translate(basetable)
     revcomplement = complement[::-1]
     for i in xrange(0, len(revcomplement), linelength):
         print revcomplement[i:i+linelength]

def main():
     seqlines = []
     for line in sys.stdin:
         if line.startswith('>') or line.startswith(';'):
             if seqlines:
                 revcomp(seqlines)
             sys.stdout.write(line)
             seqlines = []
         else:
             seqlines.append(line.strip())
     revcomp(seqlines)

if __name__ == '__main__':
     main()

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter



More information about the Python-list mailing list