-
Notifications
You must be signed in to change notification settings - Fork 0
/
step_analysis.py
294 lines (238 loc) · 14.5 KB
/
step_analysis.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import streamlit as st
import numpy as np
import pandas as pd
import os
import matplotlib.pyplot as plt
from scipy.signal import savgol_filter
from scipy.ndimage import median_filter
from scipy.spatial.distance import cdist
from scipy.interpolate import *
from scipy.optimize import fmin_cobyla
from foldometer.tools.region_manipulation import get_cycle_region_by_cycle_previous_retracting_and_next_early_retracting
from analysis_functionality.step_analysis.adjacent_sliding_windows_median_difference import adjacent_sliding_windows_median_difference
from scipy.optimize import curve_fit
from analysis_functionality.tools.str_analysis import int_to_000str
from copy import deepcopy
from analysis_functionality.tools.streamlit_wide_mode import wide_mode
from analysis_functionality.tools.str_analysis import str_extension_remove, compact_file_name
folderProject = 'S:/projects/Luca/dmMBP'
def step_analysis(folderProject=folderProject):
folderData = folderProject + "/wlc_manual_fit"
folderSave = folderProject + "/step_analysis"
print("step_analysis")
st.set_option('deprecation.showPyplotGlobalUse', False)
if not os.path.exists(folderSave):
os.mkdir(folderSave)
# wide_mode()
fileNameList = [os.path.splitext(fileName)[0] for fileName in os.listdir(folderData) if not "Power Spectrum" in fileName]
fileNameShortList = [compact_file_name(fileName) for fileName in fileNameList if not "Power Spectrum" in fileName]
fileNameTemp = st.sidebar.selectbox("Select your file", fileNameShortList)
name = [name for (name, nameShort) in zip(fileNameList, fileNameShortList) if nameShort==fileNameTemp][0]
SAVE = st.sidebar.button("Save")
data = pd.read_csv(folderData + "/" + name + ".csv")
if data["surfaceSepX"].mean()>2500:
bpDNA = st.sidebar.number_input("bpDNA", None, None, 9000.)
else:
bpDNA = st.sidebar.number_input("bpDNA", None, None, 5000.)
st.sidebar.header("Parameters to cut out steps")
minDistance = st.sidebar.number_input("Minimum distance", 0, 100000, int((3200*bpDNA/10000)*0.95), step=50)
maxForce = st.sidebar.number_input("Maximum force", 0, 100000, 60)
maxDistance = st.sidebar.number_input("Maximum distance", 0, 100000, int((3700*bpDNA/10000)*1.025), step=10)
maxTime = st.sidebar.number_input("Maximum time", 0, 100000, int(np.round(data["time"].max())), step=10)
st.sidebar.header("Threshold")
N_SIGMA_THRESHOLD = st.sidebar.number_input("Threshold: number of sigma", 0., 100000., 5., step=0.25)
THRESHOLD_OFFSET = st.sidebar.number_input("THRESHOLD_OFFSET", 0., 100000., 3., step=0.2)
WINDOW_SIZE = st.sidebar.number_input("WINDOW_SIZE", None,None, 31, step=1)
maskMinDistance = data["surfaceSepX"] >= minDistance
maskMaxForce = data["forceX"] <= maxDistance
maskMaxDistance = data["surfaceSepX"] <= maxDistance
maskMaxTime = data["time"] <= maxTime
maskCondition = maskMaxForce * maskMinDistance * maskMaxDistance * maskMaxTime
plt.plot(data["surfaceSepX"], savgol_filter(data["forceX"], 51, 1))
plt.plot([minDistance, minDistance], [0, data["forceX"].max()], color="k")
plt.plot([maxDistance, maxDistance], [0, data["forceX"].max()], color="k")
plt.plot([minDistance, maxDistance], [maxForce, maxForce], color="k")
st.pyplot()
time = np.array(data["time"])
Lc = np.array(data["proteinLc"])
force = np.array(data["forceX"])
diffLc = data["proteinLc"].diff()
# plt.plot(data["proteinLc"])
# plt.ylim((-200, 200))
# st.pyplot()
Lc_savgol = pd.DataFrame(savgol_filter(data["proteinLc"].loc[maskMinDistance], 51, 1)) # Savitzky-Golay filter
# time_resample =
# Lc_resample = data["proteinLc"].resample("Timedelta")
diffLc_med_sliding_windows = adjacent_sliding_windows_median_difference(Lc)
#------------------------------------Median filter----------------------------------------------------------------------
def standard_deviation_estimation(f, aa=15, bb=-2.2):
return aa / (f + bb)
@st.cache(suppress_st_warning=True)
def minimization_function(std_f_force_array, a, b):
force = range(len(std_f_force_array))
std_model = standard_deviation_estimation(force, a, b)
return ((std_f_force_array - std_model) ** 2).mean()
@st.cache(suppress_st_warning=True)
def threshold_estimation(Lc, force):
sizeWindow = WINDOW_SIZE
Lc_median_filter = median_filter(Lc, size=sizeWindow)
median_diff = Lc_median_filter[sizeWindow:] - Lc_median_filter[:-sizeWindow] # median_diff_temp = Lc_median_filter[sizeWindow:] - Lc_median_filter[:-sizeWindow]
# median_diff_tenp2 = np.concatenate([median_diff_temp[:sizeWindow] - median_diff_temp[sizeWindow:sizeWindow*2], median_diff_temp[:-sizeWindow] - median_diff_temp[sizeWindow:]])
# median_diff = [(np.median(median_diff_tenp2[i-25:i]) - np.median(median_diff_tenp2[i+1:i+26])) * np.exp(-0.05*(np.median(median_diff_tenp2[i-25:i]) + np.median(median_diff_tenp2[i+1:i+26]))**2) for (i,y) in enumerate(median_diff_tenp2)]
diffLc_median_filter = pd.DataFrame(np.concatenate([np.zeros(int(sizeWindow/2)), median_diff, np.zeros(int(sizeWindow/2)+1)]))
##---------------------------------threshold estimation---------------------------------------------------------------
# The noise depend of the force. The threshold should depend of the force
df = pd.DataFrame({"diffLc_median_filter": np.concatenate([np.zeros(int(sizeWindow/2)), median_diff, np.zeros(int(sizeWindow/2)+1)]),
"forceRound": [np.round(f, 0) for f in force]
})
std_f_force = df.groupby(['forceRound']).std()
std_f_force_array = [std_f_force.iloc[i - int(df["forceRound"].min())] for i in range(int(df["forceRound"].max()))]
df["std_f_force"] = [std_f_force["diffLc_median_filter"].iloc[int(np.round(f, 0)) - int(df["forceRound"].min())].item() for f in force]
# return (diffLc_median_filter, df, std_f_force_array)
# (diffLc_median_filter, df, std_f_force_array) = threshold_estimation(Lc, force)
maskThresholdEstimation = (df["forceRound"]<=40) * (df["std_f_force"]<=200)
plt.figure(figsize=(10,4))
y = np.array(df["forceRound"].loc[maskMinDistance*maskThresholdEstimation])
x = np.array(df["std_f_force"].loc[maskMinDistance*maskThresholdEstimation])
maskNan = df["forceRound"].notna() * df["std_f_force"].notna()
popt, pcov = curve_fit(minimization_function, xdata=df["forceRound"].loc[maskMinDistance*maskNan], ydata=df["std_f_force"].loc[maskMinDistance*maskNan], p0=[15, -2.2])
[a, b] = popt
return (diffLc_median_filter, a, b, df) # use a and b in standard_deviation_estimation(f, a=a, b=b) to get the treshold in function of Force
(diffLc_median_filter, aa, bb, df) = threshold_estimation(Lc, force)
forceStd = np.linspace(0, 70, 1000)
thresholdStd = standard_deviation_estimation(forceStd, aa, bb)
plt.plot(thresholdStd, forceStd,"r")
plt.plot(df["std_f_force"].loc[maskMinDistance], df["forceRound"].loc[maskMinDistance], "b")
plt.xlabel("Standard deviation (nm)")
plt.ylabel("Force (pN)")
plt.xlim((0,100))
plt.ylim((0,40))
st.pyplot()
cycles = get_cycle_region_by_cycle_previous_retracting_and_next_early_retracting(data)
region_name = ["retracting", "stationary", "pulling", "nextRetracting"]
column_names = ["dataName", "cycleNumber", "region", "deltaLc", "forceBefore", "forceAfter", "lcBefore", "lcAfter", "maxSustainableForce", "time"]
listUnfolding = []
threshold = None
for (i, cycle) in enumerate(cycles):
st.text("Cycle: "+str(i))
(pullingCycle, _0, maskRetracting) = cycle["retracting"]
(pullingCycle, _1, maskStationary) = cycle["stationary"]
(pullingCycle, _2, maskPulling) = cycle["pulling"]
(pullingCycle, _3, maskNextRetracting) = cycle["nextRetracting"]
mask = (maskPulling + maskNextRetracting) * maskMinDistance
maskConditionZoom = np.array(pd.DataFrame(maskCondition).loc[mask])
if len(data["time"].loc[mask]) <= 51:
continue
threshold = standard_deviation_estimation(pd.DataFrame(savgol_filter(data["forceX"], 201, 1)).loc[mask], aa, bb) * N_SIGMA_THRESHOLD + THRESHOLD_OFFSET
maskDetection = (diffLc_median_filter.loc[mask].abs() - threshold >= 0)
plt.figure(figsize=(8,10))
plt.subplot(411)
plt.plot(data["time"].loc[mask], data["forceX"].loc[mask])
plt.ylim((0, data["forceX"].loc[maskMinDistance].max()))
plt.ylabel("Force (pN)")
plt.subplot(412)
plt.plot(data["time"].loc[mask], data["proteinLc"].loc[mask], color="lightgrey", zorder=0)
plt.plot(data["time"].loc[mask], savgol_filter(data["proteinLc"].loc[mask], 51, 1), color="k", alpha=0.7, zorder=2)
plt.ylim((data["proteinLc"].loc[maskMinDistance].min(), data["proteinLc"].loc[maskMinDistance].max()))
plt.ylabel("Lc (nm)")
indexDetection = np.where(maskDetection * maskConditionZoom)[0]
# st.text(np.where(maskDetection * maskConditionZoom)[0])
# st.text(indexDetection)
if indexDetection != []:
i_1 = -1
indexUnfolding = []
listIndexUnfolding = []
for index in indexDetection:
if index == i_1 + 1:
indexUnfolding.append(index)
else:
listIndexUnfolding.append(deepcopy(indexUnfolding))
indexUnfolding = []
indexUnfolding.append(index)
i_1 = index
listIndexUnfolding.append(deepcopy(indexUnfolding))
listIndexUnfolding = listIndexUnfolding[1:]
wentInTheForLoop = False
for IndexUnfolding in listIndexUnfolding:
wentInTheForLoop = True
maskUnfolding = [False for i in range(np.sum(mask))]
st.text(IndexUnfolding)
maskUnfolding[IndexUnfolding] = True
plt.plot(data["time"].loc[mask].loc[maskUnfolding], data["proteinLc"].loc[mask].loc[maskUnfolding], "lightcoral", zorder=1)
maskBeforeUnfolding = np.zeros(len(data["time"].loc[mask]), dtype=bool)
maskBeforeUnfolding[min(IndexUnfolding)-100:min(IndexUnfolding)] = True
maskAfterUnfolding = np.zeros(len(data["time"].loc[mask]), dtype=bool)
maskAfterUnfolding[max(IndexUnfolding):max(IndexUnfolding)+100] = True
forceBefore = data["forceX"].loc[mask].loc[maskBeforeUnfolding].mean()
lcBefore = data["proteinLc"].loc[mask].loc[maskBeforeUnfolding].mean()
forceAfter = data["forceX"].loc[mask].loc[maskAfterUnfolding].mean()
lcAfter = data["proteinLc"].loc[mask].loc[maskAfterUnfolding].mean()
deltaLc = lcAfter - lcBefore
region = data["region"].loc[mask].loc[maskUnfolding].head(1).item()
time = data["time"].loc[mask].loc[maskBeforeUnfolding].mean()
if region == "pulling":
maxSustainableForce = forceBefore
elif region == "retracting":
maxSustainableForce = data["forceX"].loc[mask].max()
else:
maxSustainableForce = forceBefore
# unfolding = {"dataName": name,
# "cycleNumber": i,
# "region": region,
# "deltaLc": deltaLc,
# "forceBefore": forceBefore,
# "forceAfter": forceAfter,
# "lcBefore": lcBefore,
# "lcAfter": lcAfter,
# "maxSustainableForce": maxSustainableForce}
unfolding = [name, i, region, deltaLc, forceBefore, forceAfter, lcBefore, lcAfter, maxSustainableForce, time]
listUnfolding.append(unfolding)
else: # if it didnt go once in the loop for it needs to create a line in dfUnfolding to account for the cycle
forceMean = data["forceX"].loc[mask].max()
lcMean = data["proteinLc"].loc[mask].loc[(data["forceX"]>=15)*(data["forceX"]<=30)].mean()
# noUnfolding = {"dataName": name,
# "cycleNumber": i,
# "region": "pulling",
# "deltaLc": 0,
# "forceBefore": forceMean,
# "forceAfter": forceMean,
# "lcBefore": lcMean,
# "lcAfter": lcMean,
# "maxSustainableForce": forceMean}
noUnfolding = [name, i, "pulling", 0, forceMean, forceMean, lcMean, lcMean, forceMean]
listUnfolding.append(noUnfolding)
plt.subplot(413)
plt.plot(np.array(data["time"].loc[mask]), np.array(threshold), "r")
plt.plot(np.array(data["time"].loc[mask]), np.array(-threshold), "r")
plt.plot(np.array(data["time"].loc[mask]), np.array(diffLc_median_filter.loc[mask]), "yellowgreen")
plt.ylim(( float(diffLc_median_filter.loc[maskMinDistance].min()), float(diffLc_median_filter.loc[maskMinDistance].max()) ))
plt.ylabel("median filter diff (nm)")
plt.subplot(414)
plt.plot(np.array(data["time"].loc[mask]), np.array(maskDetection * maskConditionZoom), color="r")
plt.ylim((-0.1, 1.1))
plt.xlabel("Time (s)")
plt.ylabel("Step Detected (bool)")
if SAVE:
if not os.path.exists(folderSave+"/figure"):
os.mkdir(folderSave+"/figure")
plt.savefig(folderSave+"/figure"+"/"+name+"_"+int_to_000str(i)+".png")
st.pyplot()
dfUnfolding = pd.DataFrame(listUnfolding, columns=column_names)
allUnfolding = []
st.sidebar.header("Select unfolding events")
num = 0
for cycleNumber in dfUnfolding["cycleNumber"].unique():
st.sidebar.text("cycle: "+str(cycleNumber))
mask = dfUnfolding["cycleNumber"] == cycleNumber
for index, unfolding in dfUnfolding.loc[mask].iterrows():
bool = st.sidebar.checkbox(str(num)+" "+str(round(unfolding["time"], 2))+"s " + str(round(unfolding["forceBefore"], 2))+"pN " + str(round(unfolding["deltaLc"], 2))+"nm", True)
num += 1
if bool:
allUnfolding.append(unfolding)
dfAllUnfolding = pd.DataFrame(allUnfolding)
st.dataframe(dfAllUnfolding)
if SAVE:
dfAllUnfolding.to_csv(folderSave + "/" + name + ".csv")
st.balloons()
if __name__ == '__main__':
step_analysis()