forked from mtarbit/Rosalind-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe020-revp.py
executable file
·65 lines (48 loc) · 1.28 KB
/
e020-revp.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
57
58
59
60
61
62
63
64
#!/usr/bin/env python
# coding=utf-8
# Locating Restriction Sites
# ==========================
#
# A DNA string is a reverse palindrome if it is equal to its reverse complement.
# For instance, GCATGC is a reverse palindrome because its reverse complement is
# GCATGC.
#
# Given: A DNA string of length at most 1 kbp.
#
# Return: The position and length of every reverse palindrome in the string
# having length between 4 and 8.
#
# Sample Dataset
# --------------
# TCAATGCATGCGGGTCTATATGCAT
#
# Sample Output
# -------------
# 4 6
# 5 4
# 6 6
# 7 4
# 17 4
# 18 4
# 20 6
# 21 4
def reverse_complement(s):
complements = {'A':'T', 'T':'A', 'G':'C', 'C':'G'}
return ''.join([complements[c] for c in reversed(s)])
def reverse_palindromes(s):
results = []
l = len(s)
for i in range(l):
for j in range(4, 9):
if i + j > l:
continue
s1 = s[i:i+j]
s2 = reverse_complement(s1)
if s1 == s2:
results.append((i + 1, j))
return results
if __name__ == "__main__":
small_dataset = "TCAATGCATGCGGGTCTATATGCAT"
large_dataset = open('datasets/rosalind_revp.txt').read().strip()
results = reverse_palindromes(large_dataset)
print "\n".join([' '.join(map(str, r)) for r in results])