[Shootout-list] python revcomp

bradford powell bradford powell <bradford.powell@gmail.com>
Wed, 23 Mar 2005 11:59:18 -0500


Hello,

The existing python revcomp benchmark only reverses at the line level.
I suggest the following instead:

--- cut here
#!/usr/bin/python
#
# Contributed by Bradford Powell
#
import sys
import string

revtable = string.maketrans('wsatugcyrkmbdhvnATUGCYRKMBDHVN',
'WSTAACGRYMKVHDBNTAACGRYMKVHDBN')

def revcomp(s):
	return s[::-1].translate(revtable)

def printseq(desc, s, linewidth = 60):
	print desc,
	for i in range(0, len(s), linewidth):
		print s[i:i+linewidth]

desc = ''
s = []
for line in sys.stdin:
	if line.startswith('>'):
		if desc:
			printseq(desc, revcomp(''.join(s)))
			s = []
		desc = line
	else:
		s += line.strip()
if desc:
	printseq(desc, revcomp(''.join(s)))