-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrevcomp.py
56 lines (38 loc) · 1.08 KB
/
revcomp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""Computer thre reverse complement of a DNA string
Original from http://onlamp.com/pub/a/python/2002/10/17/biopython.html
"""
import string
import sys
sys.stderr.write("use bio_helper instead of revcomp module\n")
# just use [::-1]
#def reverse(string):
# """
# Return a string in reverse order.
# """
#
# letters = list(string)
# letters.reverse()
# return ''.join(letters)
def dna_comp(dnaseq):
"""
Return the reverse complement of the dna string.
"""
dna_transtable = string.maketrans("ATCGatcg", "TAGCtagc")
return dnaseq.translate(dna_transtable)
def dna_revcomp(dnaseq):
"""
Return the reverse complement of the dna string.
"""
return dna_comp(dnaseq)[::-1]
def test():
"""
Test for correct answer
"""
dnaseq = 'CCGGAAGAGcttacttag'
dnarevcomp = dna_revcomp(dnaseq)
if dnarevcomp != 'ctaagtaagCTCTTCCGG':
raise ValueError, "Oops...revcomp gave wrong answer"
print "Reverse complement of %s is %s." % (
dnaseq, dnarevcomp)
if __name__ == "__main__":
test()