-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReplication.py
206 lines (184 loc) · 5.96 KB
/
Replication.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
def PatternCount(Text,Pattern):
count = 0
for i in range(len(Text)-len(Pattern)+1):
if Text[i:i+len(Pattern)] == Pattern:
count = count+1
return count
def FrequencyMap(Text, k):
freq = {}
n = len(Text)
for i in range(n-k+1):
Pattern = Text[i:i+k]
freq[Pattern] = 0
for i in range(n-k+1):
Pattern = Text[i:i+k]
freq[Pattern] += 1
return freq
def FindClumps(Text, k, L, t):
Patterns = set()
n = len(Text)
for i in range(n - L):
Window = Text[i:i+L]
freqMap = FrequencyMap(Window, k)
for kmer,freq in freqMap.items():
if freq >= t:
Patterns.add(kmer)
return Patterns
def BetterFrequentWords(Text, k):
frequentPatterns = []
freqMap = FrequencyMap(Text, k)
max_freq = max(freqMap.values())
for kmer,freq in freqMap.items():
if freq == max_freq:
frequentPatterns.append(kmer)
return frequentPatterns
def FrequentWords(Text, k):
words = []
freq = FrequencyMap(Text, k)
m = max(freq.values())
for key in freq:
if freq[key] == m:
words.append(key)
return words
def Reverse(Pattern):
rev = ''
for char in Pattern:
rev = char + rev
return rev
def Complement(Pattern):
complement_char = {"A":"T","T":"A","G":"C","C":"G"}
com = ''
for char in Pattern:
com += complement_char[char]
return com
def Transcription(DNA):
RNA = DNA.replace('T', 'U')
return RNA
def ReverseComplement(Pattern):
Pattern = Reverse(Pattern)
Pattern = Complement(Pattern)
return Pattern
def PatternMatching(Pattern, Genome):
positions = [] # output variable
k = len(Pattern)
for i in range(len(Genome) - k +1):
if Genome[i:i + k] == Pattern:
positions.append(i)
return positions
def SymbolArray(Genome, symbol):
array = {}
n = len(Genome)
ExtendedGenome = Genome + Genome[0:n//2]
for i in range(n):
array[i] = PatternCount(symbol, ExtendedGenome[i:i+(n//2)])
return array
def FasterSymbolArray(Genome, symbol):
array = {}
n = len(Genome)
ExtendedGenome = Genome + Genome[0:n//2]
# look at the first half of Genome to compute first array value
array[0] = PatternCount(symbol, Genome[0:n//2])
for i in range(1, n):
# start by setting the current array value equal to the previous array value
array[i] = array[i-1]
# the current array value can differ from the previous array value by at most 1
if ExtendedGenome[i-1] == symbol:
array[i] = array[i]-1
if ExtendedGenome[i+(n//2)-1] == symbol:
array[i] = array[i]+1
return array
def SkewArray(Genome):
array = [0]
for i in range(len(Genome)):
chr = Genome[i]
if chr in ["A" ,"T"]:
array.append(array[i])
if chr == "G":
num = array[i]+1
array.append(num)
if chr == "C":
num = array[i]-1
array.append(num)
return array
def MinimumSkew(Genome):
array = SkewArray(Genome)
min_Skew = min(array)
positions = [i for i in range(len(array)) if array[i] == min_Skew]
return positions
def HammingDistance(p, q):
count = 0
for i,j in zip(p,q):
if i != j:
count += 1
return count
def ApproximatePatternMatching(Pattern, Text, d):
positions = []
for i in range(len(Text) - len(Pattern) + 1):
if HammingDistance(Pattern,Text[i:i+len(Pattern)]) <= d:
positions.append(i)
return positions
def ApproximatePatternCount(Pattern, Text, d):
positions = []
for i in range(len(Text) - len(Pattern) + 1):
if HammingDistance(Pattern,Text[i:i+len(Pattern)]) <= d:
positions.append(i)
return len(positions)
def Neighbors(Pattern, d):
if d == 0:
return {Pattern}
if len(Pattern) == 1:
return {"A", "C", "G", "T"}
Neighborhood = set()
Suffix = Pattern[1:]
SuffixNeighbors = Neighbors(Suffix, d)
for SuffixNeighbor in SuffixNeighbors:
if HammingDistance(Suffix, SuffixNeighbor) < d:
for x in ["A","G","T","C"]:
Neighborhood.add(x + SuffixNeighbor)
else:
Neighborhood.add(Pattern[0] + SuffixNeighbor)
return Neighborhood
def FrequentWordsWithMismatches(Text, k, d):
Patterns = []
freqMap = {}
n = len(Text)
for i in range(n-k):
Pattern = Text[i:i+k]
neighborhood = Neighbors(Pattern, d)
for neighbor in neighborhood:
if not neighbor in freqMap:
freqMap[neighbor] = 1
else:
freqMap[neighbor] += 1
m = max(freqMap.values())
for Pattern, freq in freqMap.items():
if freq == m:
Patterns.append(Pattern)
return Patterns
def FrequentWordsWithMismatchesWithRC(Text, k, d):
Patterns = []
freqMap = {}
freqMapwithRC = {}
n = len(Text)
for i in range(n-k):
Pattern = Text[i:i+k]
neighborhood = Neighbors(Pattern, d)
for neighbor in neighborhood:
if not neighbor in freqMap:
freqMap[neighbor] = 1
else:
freqMap[neighbor] += 1
for Pattern, freq in freqMap.items():
if ReverseComplement(Pattern) == Pattern:
freqMapwithRC[Pattern] = freqMap[Pattern]
continue
elif not ReverseComplement(Pattern) in freqMap:
freqMapwithRC[Pattern] = freqMap[Pattern]
continue
else:
freqMapwithRC[Pattern] = freqMap[Pattern] + freqMap[ReverseComplement(Pattern)]
m = max(freqMapwithRC.values())
for Pattern, freq in freqMapwithRC.items():
if freq == m:
Patterns.append(Pattern)
return Patterns