-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrequent_words_with_mismatches.py
56 lines (42 loc) · 1.4 KB
/
frequent_words_with_mismatches.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 itertools
import genome
def mutations(word, hamming_distance, charset='ATCG'):
for indices in itertools.combinations(range(len(word)), hamming_distance):
for replacements in itertools.product(charset, repeat=hamming_distance):
mutation = list(word)
for index, replacement in zip(indices, replacements):
mutation[index] = replacement
yield "".join(mutation)
def generateMatches(pattern, dict, d):
dict.update(mutations(pattern, d))
def addMatchesToResults(results, dict):
for mutation in dict:
if mutation in results:
results[mutation] += 1
else:
results[mutation] = 1
text = 'ACGTTGCATGTCGCATGATGCATGAGAGCT'
k = 10
d = 3
inFile = open('dataset_8_6.txt', 'r')
#inFile.readline()
text = inFile.readline().strip()
# k = int(inFile.readline().strip())
# d = int(inFile.readline().strip())
inFile.close()
i=0
results={}
while i <= len(text) - k:
section = text[i:i + k]
dict = set()
generateMatches(section, dict, d)
addMatchesToResults(results, dict)
i += 1
for (k, v) in results.items():
complement = genome.reversecomplement(k)
if (complement in results) and (k < complement):
results[k] += results[complement]
results[complement] += v
highestFrequency = max(results.values())
for i in [k for k, v in results.items() if v == highestFrequency]:
print i,