4 from libhooke import WX_GOOD, ClickedPoint
6 wxversion.select(WX_GOOD)
7 from wx import PostEvent
13 import libhookecurve as lhc
16 warnings.simplefilter('ignore',np.RankWarning)
19 class pclusterCommands:
26 def do_pcluster(self,args):
30 Automatically measures peaks and extracts informations for further clustering
31 (c)Paolo Pancaldi, Massimo Sandal 2009
33 #--Custom persistent length
35 for arg in args.split():
36 #look for a persistent length argument.
38 pl_expression=arg.split('=')
39 pl_value=float(pl_expression[1]) #actual value
43 #configuration variables
44 min_npks = self.convfilt_config['minpeaks']
45 min_deviation = self.convfilt_config['mindeviation']
47 pclust_filename=raw_input('Automeasure filename? ')
48 realclust_filename=raw_input('Coordinates filename? ')
50 f=open(pclust_filename,'w+')
51 f.write('Analysis started '+time.asctime()+'\n')
52 f.write('----------------------------------------\n')
53 f.write('; Contour length (nm) ; Persistence length (nm) ; Max.Force (pN) ; Slope (N/m) ; Sigma contour (nm) ; Sigma persistence (nm)\n')
56 f=open(realclust_filename,'w+')
57 f.write('Analysis started '+time.asctime()+'\n')
58 f.write('----------------------------------------\n')
59 f.write('; Peak number ; Mean delta (nm) ; Median delta (nm) ; Mean force (pN) ; Median force (pN) ; First peak length (nm) ; Last peak length (nm) ; Max force (pN) ; Min force (pN) ; Max delta (nm) ; Min delta (nm)\n')
61 # ------ FUNCTION ------
62 def fit_interval_nm(start_index,plot,nm,backwards):
64 Calculates the number of points to fit, given a fit interval in nm
65 start_index: index of point
67 backwards: if true, finds a point backwards.
69 whatset=1 #FIXME: should be decidable
70 x_vect=plot.vectors[1][0]
74 start=x_vect[start_index]
76 while abs(x_vect[i]-x_vect[start_index])*(10**9) < nm:
77 if i==0 or i==maxlen-1: #we reached boundaries of vector!
86 def plot_informations(itplot,pl_value):
89 contact_point.absolute_coords (2.4584142802103689e-007, -6.9647135616234017e-009)
90 peak_point.absolute_coords (3.6047748250571423e-008, -7.7142802788854212e-009)
91 other_fit_point.absolute_coords (4.1666139243838867e-008, -7.3759393477579707e-009)
92 peak_location [510, 610, 703, 810, 915, 1103]
93 peak_size [-1.2729111505202212e-009, -9.1632775347399312e-010, -8.1707438353929907e-010, -8.0335812578148904e-010, -8.7483955226387558e-010, -3.6269619757067322e-009]
94 params [2.2433999931959462e-007, 3.3230248825175678e-010]
95 fit_errors [6.5817195369767644e-010, 2.4415923138871498e-011]
97 fit_points=int(self.config['auto_fit_points']) # number of points to fit before the peak maximum <50>
99 T=self.config['temperature'] #temperature of the system in kelvins. By default it is 293 K. <301.0>
100 cindex=self.find_contact_point() #Automatically find contact point <158, libhooke.ClickedPoint>
101 contact_point=self._clickize(itplot[0].vectors[1][0], itplot[0].vectors[1][1], cindex)
103 base_index_0=peak_location[-1]+fit_interval_nm(peak_location[-1], itplot[0], self.config['auto_right_baseline'],False)
104 self.basepoints.append(self._clickize(itplot[0].vectors[1][0],itplot[0].vectors[1][1],base_index_0))
105 base_index_1=self.basepoints[0].index+fit_interval_nm(self.basepoints[0].index, itplot[0], self.config['auto_left_baseline'],False)
106 self.basepoints.append(self._clickize(itplot[0].vectors[1][0],itplot[0].vectors[1][1],base_index_1))
107 self.basecurrent=self.current.path
108 boundaries=[self.basepoints[0].index, self.basepoints[1].index]
110 to_average=itplot[0].vectors[1][1][boundaries[0]:boundaries[1]] #y points to average
111 avg=np.mean(to_average)
112 return fit_points, contact_point, pl_value, T, cindex, avg
114 def features_peaks(itplot, peak, fit_points, contact_point, pl_value, T, cindex, avg):
116 calculate informations for each peak and add they in
117 c_lengths, p_lengths, sigma_c_lengths, sigma_p_lengths, forces, slopes
127 slope_span=int(self.config['auto_slope_span'])
129 peak_point=self._clickize(itplot[0].vectors[1][0],itplot[0].vectors[1][1],peak)
130 other_fit_point=self._clickize(itplot[0].vectors[1][0],itplot[0].vectors[1][1],peak-fit_points)
132 points=[contact_point, peak_point, other_fit_point]
134 params, yfit, xfit, fit_errors = self.wlc_fit(points, itplot[0].vectors[1][0], itplot[0].vectors[1][1], pl_value, T, return_errors=True)
137 delta_to_measure=itplot[0].vectors[1][1][peak-delta_force:peak+delta_force]
138 y=min(delta_to_measure)
140 slope=self.linefit_between(peak-slope_span,peak)[0]
141 #check fitted data and, if right, add peak to the measurement
142 if len(params)==1: #if we did choose 1-value fit
144 c_leng=params[0]*(1.0e+9)
146 sigma_c_lengths=fit_errors[0]*(1.0e+9)
147 force = abs(y-avg)*(1.0e+12)
149 p_leng=params[1]*(1.0e+9)
150 #check if persistent length makes sense. otherwise, discard peak.
151 if p_leng>self.config['auto_min_p'] and p_leng<self.config['auto_max_p']:
153 p_lengths.append(p_leng)
154 c_lengths.append(params[0]*(1.0e+9))
155 sigma_c_lengths.append(fit_errors[0]*(1.0e+9))
156 sigma_p_lengths.append(fit_errors[1]*(1.0e+9))
157 forces.append(abs(y-avg)*(1.0e+12))
160 c_leng=params[0]*(1.0e+9)
161 sigma_c_leng=fit_errors[0]*(1.0e+9)
162 sigma_p_leng=fit_errors[1]*(1.0e+9)
163 force=abs(y-avg)*(1.0e+12)
167 #return c_lengths, p_lengths, sigma_c_lengths, sigma_p_lengths, forces, slopes
168 return c_leng, p_leng, sigma_c_leng, sigma_p_leng, force, slope
171 # ------ PROGRAM -------
173 for item in self.current_list:
175 item.identify(self.drivers)
176 itplot=item.curve.default_plots()
178 peak_location,peak_size=self.exec_has_peaks(item,min_deviation)
180 #We have troubles with exec_has_peaks (bad curve, whatever).
181 #Print info and go to next cycle.
182 print 'Cannot process ',item.path
185 if len(peak_location)==0:
188 fit_points, contact_point, pl_value, T, cindex, avg = plot_informations(itplot,pl_value)
189 print '\n\nCurve',item.path, 'is',c,'of',len(self.current_list),': found '+str(len(peak_location))+' peaks.'
191 #initialize output data vectors
199 #loop each peak of my curve
200 for peak in peak_location:
201 c_leng, p_leng, sigma_c_leng, sigma_p_leng, force, slope = features_peaks(itplot, peak, fit_points, contact_point, pl_value, T, cindex, avg)
202 for var, vector in zip([c_leng, p_leng, sigma_c_leng, sigma_p_leng, force, slope],[c_lengths, p_lengths, sigma_c_lengths, sigma_p_lengths, forces, slopes]):
206 #FIXME: We need a dictionary here...
207 allvects=[c_lengths, p_lengths, sigma_c_lengths, sigma_p_lengths, forces, slopes]
208 for vect in allvects:
210 for i in range(len(c_lengths)):
213 print 'Measurements for all peaks detected:'
214 print 'contour (nm)', c_lengths
215 print 'sigma contour (nm)',sigma_c_lengths
216 print 'p (nm)',p_lengths
217 print 'sigma p (nm)',sigma_p_lengths
218 print 'forces (pN)',forces
219 print 'slopes (N/m)',slopes
222 write automeasure text file
224 print 'Saving automatic measurement...'
225 f=open(pclust_filename,'a+')
226 f.write(item.path+'\n')
227 for i in range(len(c_lengths)):
228 f.write(' ; '+str(c_lengths[i])+' ; '+str(p_lengths[i])+' ; '+str(forces[i])+' ; '+str(slopes[i])+' ; '+str(sigma_c_lengths[i])+' ; '+str(sigma_p_lengths[i])+'\n')
232 calculate clustering coordinates
234 peak_number=len(c_lengths)
237 for i in range(len(c_lengths)-1):
238 deltas.append(c_lengths[i+1]-c_lengths[i])
240 delta_mean=np.mean(deltas)
241 delta_median=np.median(deltas)
243 force_mean=np.mean(forces)
244 force_median=np.median(forces)
246 first_peak_cl=c_lengths[0]
247 last_peak_cl=c_lengths[-1]
249 max_force=max(forces[:-1])
250 min_force=min(forces)
252 max_delta=max(deltas)
253 min_delta=min(deltas)
255 delta_stdev=np.std(deltas)
256 forces_stdev=np.std(forces[:-1])
259 print 'Peaks',peak_number
260 print 'Mean delta',delta_mean
261 print 'Median delta',delta_median
262 print 'Mean force',force_mean
263 print 'Median force',force_median
264 print 'First peak',first_peak_cl
265 print 'Last peak',last_peak_cl
266 print 'Max force',max_force
267 print 'Min force',min_force
268 print 'Max delta',max_delta
269 print 'Min delta',min_delta
270 print 'Delta stdev',delta_stdev
271 print 'Forces stdev',forces_stdev
274 write clustering coordinates
276 f=open(realclust_filename,'a+')
277 f.write(item.path+'\n')
278 f.write(' ; '+str(peak_number)+' ; '+str(delta_mean)+' ; '+str(delta_median)+' ; '+str(force_mean)+' ; '+str(force_median)+' ; '+str(first_peak_cl)+' ; '+str(last_peak_cl)+ ' ; '+str(max_force)+' ; '
279 +str(min_force)+' ; '+str(max_delta)+' ; '+str(min_delta)+ ' ; '+str(delta_stdev)+ ' ; '+str(forces_stdev)+'\n')
284 def do_pca(self,args):
287 Automatically measures pca from coordinates filename and shows two interactives plots
288 (c)Paolo Pancaldi, Massimo Sandal 2009
291 self.pca_myArray = []
299 for arg in args.split():
300 #look for a file argument.
302 file_temp=arg.split('=')[1] #actual coordinates filename
306 file_name = file_temp
307 print "Coordinates filename used: " + file_name
309 print "Impossibile to find " + file_temp + " in current directory"
310 print "Coordinates filename used: " + file_name
316 nPlotTot = nPlotTot+1
317 #plot_path_temp = row.split("/")[6][:-1]
319 if row[0]==" " and row.find('nan')==-1:
320 row = row[row.index(";",2)+2:].split(" ; ") # non considero la prima colonna col #picchi
321 row = [float(i) for i in row]
323 #0:Mean delta, 1:Median delta, 2:Mean force, 3:Median force, 4:First peak length, 5:Last peak length
324 #6:Max delta 7:Min delta 8:Max force 9:Min force 10:Std delta 11:Std force
325 if (row[0]<500 and row[1]<500 and row[2]<500 and row[3]<500 and row[4]<500 and row[5]<500):
326 if (row[0]>0 and row[1]>0 and row[2]>0 and row[3]>0 and row[4]>0 and row[5]>0 and row[6]>0 and row[7]>0 and row[8]>0 and row[9]>0):
327 nPlotGood = nPlotGood+1
328 self.pca_paths[nPlotGood] = plot_path_temp
329 #row=[row[1],row[2],row[4],row[10],row[11]]
330 #row=[row[10],row[11], row[0], row[2]]
331 self.pca_myArray.append(row)
335 print nPlotGood, "of", nPlotTot
337 # array convert, calculate PCA, transpose
338 self.pca_myArray = np.array(self.pca_myArray,dtype='float')
339 print self.pca_myArray.shape
340 self.pca_myArray = pca(self.pca_myArray, output_dim=2) #other way -> y = mdp.nodes.PCANode(output_dim=2)(gigi)
341 myArrayTr = np.transpose(self.pca_myArray)
346 clustplot=lhc.PlotObject()
347 clustplot.add_set(X,Y)
348 #clustplot.add_set(X[:14],Y[:14])
349 clustplot.normalize_vectors()
350 clustplot.styles=['scatter']
351 #clustplot.styles=['scatter',None]
352 clustplot.destination=1
354 self._send_plot([clustplot])
355 self.clustplot=clustplot
358 def do_pclick(self,args):
360 self._send_plot([self.clustplot]) #quick workaround for BAD problems in the GUI
362 point = self._measure_N_points(N=1, whatset=0)
363 indice = point[0].index
364 plot_file = self.pca_paths[indice]
365 dot_coord = self.pca_myArray[indice]
366 print "file: " + str(plot_file)
367 print "id: " + str(indice)
368 print "coord: " + str(dot_coord)
369 self.do_genlist(str(plot_file))
370 #self.do_jump(str(plot_file))