-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdna.py
55 lines (38 loc) · 961 Bytes
/
dna.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
import array
import string
legal_dna = "ACGTN"
def is_DNA(seq):
"""
Returns 1 if it contains only legal values for a DNA sequence.
c.f. http://www.ncbi.nlm.nih.gov/BLAST/fasta.html
"""
for ch in seq:
if ch not in legal_dna:
return 0
return 1
def reverse_complement(s):
"""
Build reverse complement of 's'.
"""
s = s.upper()
assert is_DNA(s), "Your sequence must be DNA!"
r = reverse(s)
rc = complement(r)
return rc
rc = reverse_complement # alias 'rc' to 'reverse_complement'
try:
__complementTranslation = str.maketrans('ACTG', 'TGAC')
except AttributeError:
__complementTranslation = string.maketrans('ACTG', 'TGAC')
def complement(s):
"""
Return complement of 's'.
"""
c = s.translate(__complementTranslation)
return c
def reverse(s):
"""
Return reverse of 's'.
"""
r = "".join(reversed(s))
return r