-
Notifications
You must be signed in to change notification settings - Fork 1
/
compression.py
198 lines (175 loc) · 4.83 KB
/
compression.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
# -*- coding: utf-8 -*-
# @Time : 23.10.20
# @Authors : Chenxi N. / Guillaume T.
# @FileName: compression.py
## IMPORT
from input import read_fa
from bwt import Burrows_Wheeler_Transform
## FUNCTIONS
def save_string(string, filename):
'''
Save a string in a .txt file
:param string: string to save
:param filename: name of the save file (not the path)
:return: Nothing
'''
directory = "A:/Mes documents (HDD)/CBM/Project 1/save/" #to change for each computer
file = directory + filename + ".txt"
f = open(file, 'w')
f.write(string)
f.close()
def rle_compression(seq):
'''
Does a basic Run-Lenght Encoding
:param seq: sequence to compress (string)
:return: the compressed sequence (string)
'''
n = len(seq)
i = 1
res = ''
previous = seq[0]
count = 1
while(i<n):
if seq[i] != previous:
if count == 1:
res += previous
else:
res += (str(count) + previous)
count = 1
previous = seq[i]
else:
count += 1
i += 1
if count == 1:
res += previous
else:
res += (str(count) + previous)
count = 1
return res
def rle_decompression(seq):
'''
Does the Run-Lenght Encoding decompression
:param seq: sequence to decompress (string)
:return: the decompressed sequence (string)
'''
res = ""
n = 0
count = []
for i in range(len(seq)):
try: #the character is a number, so we add that to count
count.append(int(seq[i]))
except: #the character is a letter, and we append the result
if count != []:
for k in range(len(count)): #convert the figures of the count into the real number
n *= 10
n += count[k]
res += seq[i]*n
count = []
n = 0
else:
res += seq[i]
return res
#Dicts used for the binary (de)compression
code_dict = {"A" : "000",
"C" : "001",
"G" : "010",
"N" : "011",
"T" : "100",
"$" : "101"}
decode_dict = {"000" : "A",
"001" : "C",
"010" : "G",
"011" : "N",
"100" : "T",
"101" : "$"}
def binary(s):
'''
Given a binary sequence, computes the corresponding decimal number
:param s: binary sequence (string of 0s and 1s)
:return: the decimal number (int)
'''
c = 1
r = 0
for i in range(len(s)-1,-1,-1):
if s[i] == "1":
r += c
c *= 2
return r
def debinary(num):
'''
Given a integer <127, computes its binary sequence (7 bits)
:param num: integer to get into binary
:return: the binary sequence (string)
'''
n = num
k = 64
res = ''
while(k>=1):
if n>=k:
res += '1'
n = n-k
else:
res += '0'
k /= 2
return res
def binary_compression(seq):
'''
Compresses a genome sequence using binary sequences and ASCII characters
:param seq: genome sequence to compress (string)
:return: compressed sequence (string)
'''
n = len(seq)
i = 0
current = ""
res = ""
while(i<n):
char = seq[i]
current += code_dict[char]
if len(current)>=7:
code = binary(current[0:7])
current = current[7:]
res += chr(code)
i+=1
res = current + '\n' + res
return res
def binary_decompression(seq):
'''
Decompresses a genome sequence using binary sequences and ASCII characters
:param seq: genome sequence to decompress (string)
:return: decompressed sequence (string)
'''
n = len(seq)
i = 0
current = ""
res = ""
end = ""
while(seq[i]!="\n"):
end += seq[i]
i += 1
i += 1
while(i<n):
current += debinary(ord(seq[i]))
i += 1
while(len(current)>2):
res += decode_dict[current[0:3]]
current = current[3:]
current = current + end
while(len(current)>0):
res += decode_dict[current[0:3]]
current = current[3:]
return res
## EXAMPLES
'''
fa_example = "A:/Mes documents (HDD)/CBM/Project 1/data_small/-CSCO-3h--genome.chr22.5K.fa"
genome = read_fa(fa_example)
bwt, rotated_genome = Burrows_Wheeler_Transform(genome)
rle = rle_compression(bwt)
bin = binary_compression(bwt)
print()
print("Original length : ", len(bwt))
print("RLE length : ", len(rle))
print("Binary length : ", len(bin))
print()
print(rle_decompression(rle) == bwt)
print(binary_decompression(bin) == bwt)
'''