-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
224 lines (199 loc) · 7.35 KB
/
main.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
import zlib
import numpy
import math
import random
amino = ['A', 'C', 'G', 'T']
def convert_to_base_four(number, length):
result = ""
for i in range(length):
x = str(number % 4)
result = x + result
number = number // 4
return result
def make_G_c_th_column(input_length, c):
p = [i + 1 for i in range(input_length)]
t = 0
while 2 ** t - 1 < len(p):
# print(p)
p.insert(2 ** t - 1, -(t + 1))
t += 1
# print(p)
p1 = [0 for i in range(len(p))]
q1 = [0 for i in range(input_length)]
for i in range(2 ** (c - 1) - 1, len(p), 2 ** c):
for j in range(2 ** (c - 1)):
if i + j < len(p):
p1[i + j] = 1
for i in range(len(p)):
if p1[i] == 1 and p[i] > 0:
q1[p[i] - 1] = 1
return q1
def make_G_matrix(input_length):
t = 0
while 2 ** t - 1 - t < input_length:
t += 1
g_matrix = numpy.identity(input_length)
# print(t)
for i in range(1, t + 1):
# print(make_G_c_th_column(input_length, i))
# print(g_matrix.shape[1])
# print(type(i), i)
g_matrix = numpy.insert(g_matrix, g_matrix.shape[1], numpy.array(make_G_c_th_column(input_length, i)), 1)
return g_matrix
def encode(data_string):
ascii_codes = [ord(x) for x in data_string]
# print(ascii_codes)
base_four_string = ''.join([convert_to_base_four(x, 4) for x in ascii_codes]) # m in documentation
# print(base_four_string)
hashed = zlib.crc32(bytes(base_four_string, 'utf-8'))
# print(hashed)
# print(convert_to_base_four(hashed, 16), convert_to_base_four(hashed, 16)[10:])
a_string = base_four_string + convert_to_base_four(hashed, 16)[10:]
# print(a_string, " a")
g_matrix = make_G_matrix(len(a_string))
a_array = numpy.array([int(x) for x in a_string])
# print(g_matrix)
b_array = numpy.matmul(a_array, g_matrix)
b_string = ''.join(str(int(x) % 4) for x in b_array)
# print(b_string, " b")
parity_quad = sum([int(x) for x in b_string]) % 4
c_string = str(parity_quad) + b_string
# print(c_string, " c")
dna_string = [amino[int(x)] for x in c_string]
return ''.join(dna_string)
def make_H_matrix(input_length):
t = math.floor(math.log(input_length, 2)) + 1
A_arr = [make_G_c_th_column(input_length - t, i) for i in range(1, t + 1)]
A_matrix = numpy.array(A_arr).transpose()
# print(A_matrix.shape)
# print(A_matrix)
h_matrix = numpy.insert(A_matrix, A_matrix.shape[0], numpy.identity(t) * (-1), 0)
# print(h_matrix)
return h_matrix
def correct_errors(res_string, error_vector):
# print(res_string, " res")
value = -1
error_location = 0
for i in range(len(error_vector)):
if error_vector[i] != 0:
if value == -1:
value = error_vector[i]
error_location += 2 ** i
elif value != error_vector[i]:
return -1
else:
error_location += 2 ** i
if value == -1:
return res_string
# print(error_location, "loc")
# print(2 ** (math.floor(math.log(error_location, 2))), "check")
t = 0
# print(error_location, "1")
if error_location == 2 ** (math.floor(math.log(error_location, 2))):
# print(error_location, "1")
error_location = math.floor(math.log(error_location, 2))
# print(error_location, "2")
# if error_location == 0 :
error_location = len(res_string) - len(error_vector) + error_location
# print(error_location, "1")
# print( res_string, error_location)
# print(res_string, "***********")
# c_s = res_string[:error_location] + str((int(res_string[error_location]) - value) % 4) + res_string[error_location + 1:]
# print(''.join(amino[int(x)] for x in c_s))
# return res_string
else:
while 2 ** t - 1 < error_location:
t += 1
error_location -= (t + 1)
# print("one error corrected")
# print(error_location, error_vector, t, "2")
if error_location >= len(res_string):
return -1
correct_value = (int(res_string[error_location]) - value) % 4
# print(value, " val")
# print(error_location, " loc")
# print(correct_value)
correct_string = res_string[:error_location] + str(correct_value) + res_string[error_location + 1:]
# correct_string[error_location] = str(correct_value)
return correct_string
def convert_to_base_ten(number):
result = 0
i = 0
while number > 0:
result = result + (number % 10) * 4 ** i
number = number // 10
i = i + 1
return result
def decode(dna_string):
c_string = ''.join([str(amino.index(x)) for x in dna_string])
# print(c_string, " c")
parity_quad = int(c_string[0])
b_string = c_string[1:]
# print(b_string, " b")
h_matrix = make_H_matrix(len(b_string))
b_array = numpy.array([int(x) for x in b_string])
# print(numpy.matrix(b_array), numpy.array(h_matrix).shape)
e_array = numpy.matmul(b_array, h_matrix)
# print(e_array)
e_vector = [x % 4 for x in e_array]
e_string = ''.join([str(int(x) % 4) for x in e_array])
# print(e_vector)
t = h_matrix.shape[1]
# res_string = b_string[:len(b_string)-t]
# print(res_string)
correct_string = correct_errors(b_string, e_vector)
# print(correct_string, "b_1")
if correct_string == -1:
return "can't restore : more than one error"
res_parity = sum([int(x) for x in correct_string]) % 4
if res_parity != parity_quad:
return "parity mismatch"
a_string = correct_string[:len(correct_string) - t]
m_string = a_string[:len(a_string) - 6]
crc_string = a_string[len(a_string) - 6:]
hashed = zlib.crc32(bytes(m_string, 'utf-8'))
crc_check = convert_to_base_four(hashed, 16)[10:]
# print(b_string)
# print(a_string)
# print(m_string)
# print(crc_string)
# print(crc_check)
if crc_string != crc_check:
return "crc mismatch"
m_array = []
for i in range(0, len(m_string), 4):
m_array.append(m_string[i:i + 4])
# print(m_array)
ascii_array = [convert_to_base_ten(int(x)) for x in m_array]
# print(ascii_array)
s_array = [chr(x) for x in ascii_array]
# print(s_array)
return ''.join(s_array)
if __name__ == '__main__':
testing_string = "la_Project" # the string that will be coded and decoded
tests_count = 100 # number of copies made from tested_string
errors_count = 50 # number of quads that would change in all strings
testing_times = 200 # count of times we run the test
total_fail = 0
total_wrong = 0
correct_dna = encode(testing_string)
# print(encode(testing_string))
for k in range(testing_times):
a = []
for i in range(tests_count):
a.append([x for x in correct_dna])
# print(a)
for j in range(errors_count):
x = random.randint(0, len(a) - 1)
y = random.randint(0, len(a[0]) - 1)
a[x][y] = amino[random.randint(0, 3)]
for i in range(tests_count):
string = ''.join(a[i])
if string == correct_dna:
continue
# print(string)
total_wrong += 1
if decode(string) != testing_string:
total_fail += 1
print(total_wrong / testing_times) # average error occurrence
print(total_fail / testing_times) # average failure