-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReplication.py
230 lines (153 loc) · 4.86 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
"""
Pattern Count Problem: Count all occurrences of a pattern in a string.
Input: A DNA string Pattern.
Output: The number of occurrences of a pattern in the DNA string.
"""
def PatternCount(Pattern, Text):
count = 0
for i in range(len(Text)-len(Pattern)+1):
if Text[i:i+len(Pattern)] == Pattern:
count = count+1
return count
"""
Frequent Words Problem:
Find the most frequent k-mers in a string.
Input: A string Text and an integer k.
Output: All most frequent k-mers in Text.
"""
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 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
"""
Reverse Complement Problem: Find the reverse complement of a DNA string.
Input: A DNA string Pattern.
Output: The reverse complement of Pattern.
"""
def ReverseComplement(Pattern):
Pattern = Reverse(Pattern)
Pattern = Complement(Pattern)
return Pattern
def Reverse(Pattern):
rev = ""
for char in Pattern:
rev = char + rev
return rev
def Complement(Pattern):
com = ""
for char in Pattern:
if char == 'A':
com += 'T'
elif char == 'T':
com += 'A'
elif char == 'C':
com += 'G'
elif char == 'G':
com += 'C'
return com
"""
Pattern Matching Problem: Find all occurrences of a pattern in a string.
Input: Strings Pattern and Genome.
Output: All starting positions in Genome where Pattern appears as a substring.
"""
def PatternMatching(Pattern, Genome):
positions = []
for i in range(len(Genome)-len(Pattern)+1):
if Genome[i:i+len(Pattern)] == Pattern:
positions.append(i)
return positions
"""
Symbol Array Problem: Find the number of occurrences of a symbol encountered in each window of the genome
Input: Symbol and Genome.
Output: Symbol array of Genome corresponding to symbol.
"""
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]
array[0] = PatternCount(symbol, Genome[0:n//2])
for i in range(1, n):
array[i] = array[i-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
"""
Skew Array Problem: Find the skew array of genome.
Input: String Genome.
Output: The skew array of Genome as a list.
"""
def SkewArray(Genome):
skew = [0]
for i in range(len(Genome)):
if Genome[i] == 'A' or Genome[i] == 'T':
skew.append(skew[i])
elif Genome[i] == 'G':
skew.append(skew[i] + 1)
elif Genome[i] == 'C':
skew.append(skew[i] - 1)
return skew
"""
Minimum Skew Problem: Find a position in a genome where the skew diagram attains a minimum.
Input: A DNA string Genome.
Output: All integer(s) i minimizing Skew[i] among all values of i (from 0 to len(Genome)).
"""
def MinimumSkew(Genome):
positions = []
count = 0
skew = SkewArray(Genome)
minimum = min(skew)
for i in skew:
if i == minimum:
positions.append(count)
count += 1
return positions
"""
Hamming Distance Problem:
Compute the Hamming distance between two strings.
Input: Two strings of equal length.
Output: The Hamming distance between these strings.
"""
def HammingDistance(p, q):
count = 0
for i in range(len(p)):
if p[i] != q[i]:
count += 1
return count
"""
Approximate Pattern Matching Problem:
Find all approximate occurrences of a pattern in a string.
Input: Strings Pattern and Text along with an integer d.
Output: All starting positions where Pattern appears as a substring of Text with at most d mismatches.
"""
def ApproximatePatternMatching(Pattern, Text, d):
positions = []
for i in range(len(Text)-len(Pattern)+1):
if HammingDistance(Text[i:i+len(Pattern)], Pattern) <= d:
positions.append(i)
return positions
def ApproximatePatternCount(Text, Pattern, d):
count = 0
for i in range(len(Text)-len(Pattern)+1):
if HammingDistance(Text[i:i+len(Pattern)], Pattern) <= d:
count += 1
return count