-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstream.py
executable file
·274 lines (238 loc) · 8.15 KB
/
stream.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import collections
from collections import OrderedDict
from matplotlib import pyplot as plt
from matplotlib import cm
import pylab
import math
import pandas as pd
import streamlit as st
from scipy.stats import spearmanr,kendalltau,pearsonr
import time
import numpy as np
import os
from matplotlib.backends.backend_agg import RendererAgg
import cv2
from skimage.metrics import structural_similarity as ssim
import matplotlib
matplotlib.use("Agg")
st.set_option('deprecation.showPyplotGlobalUse', False)
l = os.getcwd()
if(l!='/app/chaos-game-representation_bioseq/data/'):
os.chdir('/app/chaos-game-representation_bioseq/data/')
class TimerError(Exception):
"""A custom exception used to report errors in use of Timer class"""
class Timer:
def __init__(self):
self._start_time = None
def start(self):
"""Start a new timer"""
if self._start_time is not None:
raise TimerError(f"Timer is running. Use .stop() to stop it")
self._start_time = time.perf_counter()
def stop(self):
"""Stop the timer, and report the elapsed time"""
if self._start_time is None:
raise TimerError(f"Timer is not running. Use .start() to start it")
elapsed_time = time.perf_counter() - self._start_time
self._start_time = None
return (f"Elapsed time: {elapsed_time:0.4f} seconds")
class CGR():
K = 0
c = None
h = ""
Data = ""
N=2
F=""
i = 0
def __init__(self,a):
self.i=a
def param(self,k,n):
self.K = k
self.N = n
def get_family(self):
return self.F
def read_fasta(self,family,locz):
x = pd.read_excel(family,engine = 'openpyxl',header=None)
x.set_index(0, inplace = True)
data = x.loc[locz].tolist()
self.F = family
return data,locz
def count_kmers(self,sequence, k):
d = collections.defaultdict(int)
for i in range(len(self.Data)-(k-1)):
d[sequence[i:i+k]] +=1
d.pop("N",None)
return d
def probabilities(self,kmer_count, k):
probabilities = collections.defaultdict(float)
N = len(self.Data)
for key, value in kmer_count.items():
probabilities[key] = float(value) / (N - k + 1)
return probabilities
def chaos_game_representation(self,probabilities, k):
array_size = int(math.sqrt(4**k))
chaos = []
for i in range(array_size):
chaos.append([0]*array_size)
maxx = array_size
maxy = array_size
posx = 1
posy = 1
for key, value in probabilities.items():
for char in key:
if char == "T":
posx += maxx/2
elif char == "C":
posy += maxy/2
elif char == "G":
posx += maxx/2
posy += maxy/2
maxx /= 2
maxy /= 2
chaos[int(posy-1)][int(posx-1)] = value
maxx = array_size
maxy = array_size
posx = 1
posy = 1
#chaos = chaos/np.amax(chaos)
#if(k>7):
# return chaos*(k/2)
#else:
return chaos
def load_fasta(self,data,head):
self.Data = data
t = Timer()
t.start()
f4 = self.count_kmers(data, self.K)
f4_prob = self.probabilities(f4, self.K)
chaos_k4 = self.chaos_game_representation(f4_prob, self.K)
s = t.stop()
self.c = chaos_k4
self.h = head
return chaos_k4,s
def show(self):
pylab.figure(figsize=(12,12))
pylab.title('CGR of '+str(self.K)+'-mers for '+self.h[2:])
pylab.imshow(self.c, cmap=cm.gray_r,filterrad=10,vmin=0,vmax=2)
ax = pylab.gca()
ax.axes.xaxis.set_visible(False)
ax.axes.yaxis.set_visible(False)
pylab.axis("off")
pylab.savefig(str(self.i)+".PNG",bbox_inches='tight', pad_inches=0)
pylab.show()
def file_selector(F):
x = pd.read_excel(F ,engine = 'openpyxl',header=None)
acc = x[0].values.tolist()
def format_func(option):
return option.split(',')[0]
selected_filename = st.selectbox('Select a Sequence', acc,format_func=format_func)
print(selected_filename)
x.set_index(0, inplace = True)
a = x.loc[selected_filename].tolist()
return a[0],selected_filename
def file_selector_1(F):
x = pd.read_excel(F ,engine = 'openpyxl',header=None)
acc = x[0].values.tolist()
def format_func(option):
return option.split(',')[0]
selected_filenam = st.selectbox('Select comparision Sequence', acc,format_func=format_func)
x.set_index(0, inplace = True)
a = x.loc[selected_filenam].tolist()
return a[0],selected_filenam
if __name__ == '__main__':
_lock = RendererAgg.lock
st.title("Chaos Game Representation of Sequences")
st.write(" ________ ")
K = st.sidebar.slider(
'Select K Value',
2, 20, 10
)
A = CGR("F1")
B = CGR("S1")
A.param(K,2)
B.param(K,2)
def format_func(option):
return option[:-5]
filenames = os.listdir()
family = st.selectbox('Select Family', filenames,format_func=format_func)
filename,h1 = file_selector(family)
family1 = st.selectbox('Select Comparision Family', filenames,format_func=format_func)
filename_1,h2 = file_selector_1(family1)
cg,t1 = A.load_fasta(filename,h1)
cg_1,t2 = B.load_fasta(filename_1,h2)
st.write("_________")
st.write(A.h)
row3_space1, row3_1, row3_space2, row3_2, row3_space3 = st.beta_columns(
(.1, 1, .1, 1, .1))
with row3_1, _lock:
A.show()
st.pyplot()
with row3_2, _lock:
t = np.array(cg)
plt.matshow(t)
ax = plt.gca()
ax.axes.xaxis.set_visible(False)
ax.axes.yaxis.set_visible(False)
plt.show()
st.pyplot()
st.write("_________")
st.write(B.h)
row4_space1, row4_1, row4_space2, row4_2, row4_space3 = st.beta_columns(
(.1, 1, .1, 1, .1))
with row4_1, _lock:
B.show()
st.pyplot()
with row4_2, _lock:
t_1 = np.array(cg)
plt.matshow(t_1)
ax = plt.gca()
ax.axes.xaxis.set_visible(False)
ax.axes.yaxis.set_visible(False)
plt.show()
st.pyplot()
st.write("_________")
imageA = cv2.imread("F1.PNG")
imageB = cv2.imread("S1.PNG")
grayA = cv2.cvtColor(imageA, cv2.COLOR_BGR2GRAY)
grayB = cv2.cvtColor(imageB, cv2.COLOR_BGR2GRAY)
(score,_) = ssim(grayA, grayB, full=True)
a = max(cg[0])
b = max(cg[1])
CG = list(map(lambda x,y:((x*(1/a))+(y*(1/b))*10)**0.5, cg[0],cg[1]))
a = max(cg_1[0])
b = max(cg_1[1])
CG_1 = list(map(lambda x,y:((x*(1/a))+(y*(1/b))*10)**0.5, cg_1[0],cg_1[1]))
st.sidebar.text("Accuracy:")
add_selectbox = st.sidebar.selectbox(
'Similarity Method',
('Spearmans correlation', 'Kendalls tau','Pearsons correlation')
)
if(add_selectbox=='Spearmans correlation'):
corr, _ = spearmanr(CG,CG_1)
st.sidebar.text(" ")
st.sidebar.text(f'Spearmans correlation: %.3f' % corr)
elif(add_selectbox=='Kendalls tau'):
corr, _ = kendalltau(CG,CG_1)
st.sidebar.text(" ")
st.sidebar.text(f'Kendalls tau: %.3f' % corr)
else:
corr, _ = pearsonr(CG,CG_1)
st.sidebar.text('Pearsons correlation: %.3f' % corr)
st.sidebar.text("SSIM Score:")
ims = 1-score
if (ims==0):
st.sidebar.text(score)
else:
st.sidebar.text(ims)
st.sidebar.text(".")
st.sidebar.text("Run-Time:")
st.sidebar.text("CGR - "+t1)
st.sidebar.text(".")
st.sidebar.text("Credits:")
st.sidebar.text("""
Team - O
. Aaditya Jain
. Anirudh Bhaskar
. Ankam Srikanth
. Rohith Ramakrishnan
""")