4 Force spectroscopy curves basic fitting plugin.
5 Licensed under the GNU GPL version 2
7 Non-standard Dependencies:
8 procplots.py (plot processing plugin)
10 from hooke.libhooke import WX_GOOD, ClickedPoint
13 wxversion.select(WX_GOOD)
14 #from wx import PostEvent
15 #from wx.lib.newevent import NewEvent
24 global EVT_MEASURE_WLC
26 #measure_wlc, EVT_MEASURE_WLC = NewEvent()
28 global events_from_fit
29 events_from_fit=Queue.Queue() #GUI ---> CLI COMMUNICATION
32 class fitCommands(object):
36 self.wlccontact_point=None
37 self.wlccontact_index=None
39 def wlc_fit(self,clicked_points,xvector,yvector, pl_value, T=293, return_errors=False):
41 Worm-like chain model fitting.
42 The function is the simple polynomial worm-like chain as proposed by C.Bustamante, J.F.Marko, E.D.Siggia
43 and S.Smith (Science. 1994 Sep 9;265(5178):1599-600.)
47 clicked_points[0] = contact point (calculated or hand-clicked)
48 clicked_points[1] and [2] are edges of chunk
51 #STEP 1: Prepare the vectors to apply the fit.
54 if pl_value is not None:
55 pl_value=pl_value/(10**9)
57 #indexes of the selected chunk
58 first_index=min(clicked_points[1].index, clicked_points[2].index)
59 last_index=max(clicked_points[1].index, clicked_points[2].index)
61 #getting the chunk and reverting it
62 xchunk,ychunk=xvector[first_index:last_index],yvector[first_index:last_index]
65 #put contact point at zero and flip around the contact point (the fit wants a positive growth for extension and force)
66 xchunk_corr_up=[-(x-clicked_points[0].graph_coords[0]) for x in xchunk]
67 ychunk_corr_up=[-(y-clicked_points[0].graph_coords[1]) for y in ychunk]
70 xchunk_corr_up=scipy.array(xchunk_corr_up)
71 ychunk_corr_up=scipy.array(ychunk_corr_up)
74 #STEP 2: actually do the fit
76 #Find furthest point of chunk and add it a bit; the fit must converge
78 xchunk_high=max(xchunk_corr_up)
79 xchunk_high+=(xchunk_high/10)
81 #Here are the linearized start parameters for the WLC.
82 #[lambd=1/Lo , pii=1/P]
84 p0=[(1/xchunk_high),(1/(3.5e-10))]
85 p0_plfix=[(1/xchunk_high)]
88 fixme: remove these comments after testing
92 def f_wlc(params,x,T=T):
94 wlc function for ODR fitting
99 y=(therm*pii/4.0) * (((1-(x*lambd))**-2) - 1 + (4*x*lambd))
102 def f_wlc_plfix(params,x,pl_value=pl_value,T=T):
104 wlc function for ODR fitting
110 y=(therm*pii/4.0) * (((1-(x*lambd))**-2) - 1 + (4*x*lambd))
114 realdata=scipy.odr.RealData(xchunk_corr_up,ychunk_corr_up)
116 model=scipy.odr.Model(f_wlc_plfix)
117 o = scipy.odr.ODR(realdata, model, p0_plfix)
119 model=scipy.odr.Model(f_wlc)
120 o = scipy.odr.ODR(realdata, model, p0)
122 o.set_job(fit_type=2)
124 fit_out=[(1/i) for i in out.beta]
126 #Calculate fit errors from output standard deviations.
127 #We must propagate the error because we fit the *inverse* parameters!
128 #The error = (error of the inverse)*(value**2)
130 for sd,value in zip(out.sd_beta, fit_out):
131 err_real=sd*(value**2)
132 fit_errors.append(err_real)
134 def wlc_eval(x,params,pl_value,T):
136 Evaluates the WLC function
146 Kb=(1.38065e-23) #boltzmann constant
147 therm=Kb*T #so we have thermal energy
149 return ( (therm*pii/4.0) * (((1-(x*lambd))**-2.0) - 1 + (4.0*x*lambd)) )
151 #STEP 3: plotting the fit
153 #obtain domain to plot the fit - from contact point to last_index plus 20 points
154 thule_index=last_index+10
155 if thule_index > len(xvector): #for rare cases in which we fit something at the END of whole curve.
156 thule_index = len(xvector)
157 #reverse etc. the domain
158 xfit_chunk=xvector[clicked_points[0].index:thule_index]
160 xfit_chunk_corr_up=[-(x-clicked_points[0].graph_coords[0]) for x in xfit_chunk]
161 xfit_chunk_corr_up=scipy.array(xfit_chunk_corr_up)
163 #the fitted curve: reflip, re-uncorrect
164 yfit=wlc_eval(xfit_chunk_corr_up, out.beta, pl_value,T)
165 yfit_down=[-y for y in yfit]
166 yfit_corr_down=[y+clicked_points[0].graph_coords[1] for y in yfit_down]
169 return fit_out, yfit_corr_down, xfit_chunk, fit_errors
171 return fit_out, yfit_corr_down, xfit_chunk, None
173 def fjc_fit(self,clicked_points,xvector,yvector, pl_value, T=293, return_errors=False):
175 Freely-jointed chain function
176 ref: C.Ray and B.B. Akhremitchev; http://www.chem.duke.edu/~boris/research/force_spectroscopy/fit_efjc.pdf
179 clicked_points[0] is the contact point (calculated or hand-clicked)
180 clicked_points[1] and [2] are edges of chunk
183 #STEP 1: Prepare the vectors to apply the fit.
184 if pl_value is not None:
185 pl_value=pl_value/(10**9)
187 #indexes of the selected chunk
188 first_index=min(clicked_points[1].index, clicked_points[2].index)
189 last_index=max(clicked_points[1].index, clicked_points[2].index)
191 #getting the chunk and reverting it
192 xchunk,ychunk=xvector[first_index:last_index],yvector[first_index:last_index]
195 #put contact point at zero and flip around the contact point (the fit wants a positive growth for extension and force)
196 xchunk_corr_up=[-(x-clicked_points[0].graph_coords[0]) for x in xchunk]
197 ychunk_corr_up=[-(y-clicked_points[0].graph_coords[1]) for y in ychunk]
201 xchunk_corr_up=scipy.array(xchunk_corr_up)
202 ychunk_corr_up=scipy.array(ychunk_corr_up)
205 #STEP 2: actually do the fit
207 #Find furthest point of chunk and add it a bit; the fit must converge
209 xchunk_high=max(xchunk_corr_up)
210 xchunk_high+=(xchunk_high/10)
212 #Here are the linearized start parameters for the WLC.
213 #[lambd=1/Lo , pii=1/P]
215 p0=[(1/xchunk_high),(1/(3.5e-10))]
216 p0_plfix=[(1/xchunk_high)]
219 fixme: remove these comments after testing
225 return (np.exp(2*z)+1)/(np.exp(2*z)-1)
227 def x_fjc(params,f,T=T):
229 fjc function for ODR fitting
235 #x=(therm*pii/4.0) * (((1-(x*lambd))**-2) - 1 + (4*x*lambd))
236 x=(1/lambd)*(coth(f*(1/pii)/therm) - (therm*pii)/f)
239 def x_fjc_plfix(params,f,pl_value=pl_value,T=T):
241 fjc function for ODR fitting
247 #y=(therm*pii/4.0) * (((1-(x*lambd))**-2) - 1 + (4*x*lambd))
248 x=(1/lambd)*(coth(f*(1/pii)/therm) - (therm*pii)/f)
252 realdata=scipy.odr.RealData(ychunk_corr_up,xchunk_corr_up)
254 model=scipy.odr.Model(x_fjc_plfix)
255 o = scipy.odr.ODR(realdata, model, p0_plfix)
257 model=scipy.odr.Model(x_fjc)
258 o = scipy.odr.ODR(realdata, model, p0)
260 o.set_job(fit_type=2)
262 fit_out=[(1/i) for i in out.beta]
264 #Calculate fit errors from output standard deviations.
265 #We must propagate the error because we fit the *inverse* parameters!
266 #The error = (error of the inverse)*(value**2)
268 for sd,value in zip(out.sd_beta, fit_out):
269 err_real=sd*(value**2)
270 fit_errors.append(err_real)
272 def fjc_eval(y,params,pl_value,T):
274 Evaluates the WLC function
284 Kb=(1.38065e-23) #boltzmann constant
285 therm=Kb*T #so we have thermal energy
286 #return ( (therm*pii/4.0) * (((1-(x*lambd))**-2.0) - 1 + (4.0*x*lambd)) )
287 return (1/lambd)*(coth(y*(1/pii)/therm) - (therm*pii)/y)
290 #STEP 3: plotting the fit
291 #obtain domain to plot the fit - from contact point to last_index plus 20 points
292 thule_index=last_index+10
293 if thule_index > len(xvector): #for rare cases in which we fit something at the END of whole curve.
294 thule_index = len(xvector)
295 #reverse etc. the domain
296 ychunk=yvector[clicked_points[0].index:thule_index]
299 y_evalchunk=np.linspace(min(ychunk),max(ychunk),100)
301 #Empty y-chunk. It happens whenever we set the contact point after a recognized peak,
302 #or other buggy situations. Kludge to live with it now...
303 ychunk=yvector[:thule_index]
304 y_evalchunk=np.linspace(min(ychunk),max(ychunk),100)
306 yfit_down=[-y for y in y_evalchunk]
307 yfit_corr_down=[y+clicked_points[0].graph_coords[1] for y in yfit_down]
308 yfit_corr_down=scipy.array(yfit_corr_down)
310 #the fitted curve: reflip, re-uncorrect
311 xfit=fjc_eval(yfit_corr_down, out.beta, pl_value,T)
314 xfit_chunk_corr_up=[-(x-clicked_points[0].graph_coords[0]) for x in xfit]
316 #xfit_chunk_corr_up=scipy.array(xfit_chunk_corr_up)
317 #deltay=yfit_down[0]-yvector[clicked_points[0].index]
319 #This is a terrible, terrible kludge to find the point where it should normalize (and from where it should plot)
321 for index in scipy.arange(1,len(xfit_chunk_corr_up),1):
322 xxxdists.append((clicked_points[0].graph_coords[0]-xfit_chunk_corr_up[index])**2)
323 normalize_index=xxxdists.index(min(xxxdists))
326 deltay=yfit_down[normalize_index]-clicked_points[0].graph_coords[1]
327 yfit_corr_down=[y-deltay for y in yfit_down]
330 return fit_out, yfit_corr_down[normalize_index+1:], xfit_chunk_corr_up[normalize_index+1:], fit_errors
332 return fit_out, yfit_corr_down[normalize_index+1:], xfit_chunk_corr_up[normalize_index+1:], None
335 def do_wlc(self,args):
344 def do_fjc(self,args):
353 def do_fit(self,args):
357 Fits an entropic elasticity function to a given chunk of the curve.
359 First you have to click a contact point.
360 Then you have to click the two edges of the data you want to fit.
362 The fit function depends on the fit_function variable. You can set it with the command
363 "set fit_function wlc" or "set fit_function fjc" depending on the function you prefer.
365 For WLC, the function is the simple polynomial worm-like chain as proposed by
366 C.Bustamante, J.F.Marko, E.D.Siggia and S.Smith (Science. 1994
367 Sep 9;265(5178):1599-600.)
370 C.Ray and B.B. Akhremitchev; http://www.chem.duke.edu/~boris/research/force_spectroscopy/fit_efjc.pdf
373 pl=[value] : Use a fixed persistent length (WLC) or Kuhn length (FJC) for the fit. If pl is not given,
374 the fit will be a 2-variable
375 fit. DO NOT put spaces between 'pl', '=' and the value.
376 The value must be in nanometers.
378 t=[value] : Use a user-defined temperature. The value must be in
379 kelvins; by default it is 293 K.
380 DO NOT put spaces between 't', '=' and the value.
382 noauto : allows for clicking the contact point by
383 hand (otherwise it is automatically estimated) the first time.
384 If subsequent measurements are made, the same contact point
387 reclick : redefines by hand the contact point, if noauto has been used before
388 but the user is unsatisfied of the previously choosen contact point.
390 Syntax: fit [pl=(value)] [t=value] [noauto]
393 T=self.config['temperature']
394 for arg in args.split():
395 #look for a persistent length argument.
397 pl_expression=arg.split('=')
398 pl_value=float(pl_expression[1]) #actual value
399 #look for a T argument. FIXME: spaces are not allowed between 'pl' and value
400 if ('t=' in arg[0:2]) or ('T=' in arg[0:2]):
401 t_expression=arg.split('=')
402 T=float(t_expression[1])
404 #use the currently displayed plot for the fit
405 displayed_plot=self._get_displayed_plot()
407 #handle contact point arguments correctly
408 if 'reclick' in args.split():
409 print 'Click contact point'
410 contact_point=self._measure_N_points(N=1, whatset=1)[0]
411 contact_point_index=contact_point.index
412 self.wlccontact_point=contact_point
413 self.wlccontact_index=contact_point.index
414 self.wlccurrent=self.current.path
415 elif 'noauto' in args.split():
416 if self.wlccontact_index==None or self.wlccurrent != self.current.path:
417 print 'Click contact point'
418 contact_point=self._measure_N_points(N=1, whatset=1)[0]
419 contact_point_index=contact_point.index
420 self.wlccontact_point=contact_point
421 self.wlccontact_index=contact_point.index
422 self.wlccurrent=self.current.path
424 contact_point=self.wlccontact_point
425 contact_point_index=self.wlccontact_index
427 cindex=self.find_contact_point()
428 contact_point=ClickedPoint()
429 contact_point.absolute_coords=displayed_plot.vectors[1][0][cindex], displayed_plot.vectors[1][1][cindex]
430 contact_point.find_graph_coords(displayed_plot.vectors[1][0], displayed_plot.vectors[1][1])
431 contact_point.is_marker=True
433 print 'Click edges of chunk'
434 points=self._measure_N_points(N=2, whatset=1)
435 points=[contact_point]+points
437 if self.config['fit_function']=='wlc':
438 params, yfit, xfit, fit_errors = self.wlc_fit(points, displayed_plot.vectors[1][0], displayed_plot.vectors[1][1],pl_value,T, return_errors=True )
439 name_of_charlength='Persistent length'
440 elif self.config['fit_function']=='fjc':
441 params, yfit, xfit, fit_errors = self.fjc_fit(points, displayed_plot.vectors[1][0], displayed_plot.vectors[1][1],pl_value,T, return_errors=True )
442 name_of_charlength='Kuhn length'
444 print 'No recognized fit function defined!'
445 print 'Set your fit function to wlc or fjc.'
449 print 'Fit not possible. Probably wrong interval -did you click two *different* points?'
452 #FIXME: print "Kuhn length" for FJC
453 print 'Fit function:',self.config['fit_function']
454 print 'Contour length: ',params[0]*(1.0e+9),' nm'
455 to_dump='contour '+self.current.path+' '+str(params[0]*(1.0e+9))+' nm'
456 self.outlet.push(to_dump)
457 if len(params)==2: #if we did choose 2-value fit
458 print name_of_charlength+': ',params[1]*(1.0e+9),' nm'
459 to_dump='persistent '+self.current.path+' '+str(params[1]*(1.0e+9))+' nm'
460 self.outlet.push(to_dump)
463 fit_nm=[i*(10**9) for i in fit_errors]
464 print 'Standard deviation (contour length)', fit_nm[0]
466 print 'Standard deviation ('+name_of_charlength+')', fit_nm[1]
469 #add the clicked points in the final PlotObject
470 clickvector_x, clickvector_y=[], []
472 clickvector_x.append(item.graph_coords[0])
473 clickvector_y.append(item.graph_coords[1])
475 #create a custom PlotObject to gracefully plot the fit along the curves
477 fitplot=copy.deepcopy(displayed_plot)
478 fitplot.add_set(xfit,yfit)
479 fitplot.add_set(clickvector_x,clickvector_y)
481 #FIXME: this colour/styles stuff must be solved at the root!
482 if fitplot.styles==[]:
483 fitplot.styles=[None,None,None,'scatter']
485 fitplot.styles+=[None,'scatter']
487 if fitplot.colors==[]:
488 fitplot.colors=[None,None,None,None]
490 fitplot.colors+=[None,None]
492 self._send_plot([fitplot])
499 def find_contact_point(self,plot=False):
501 Finds the contact point on the curve.
503 The current algorithm (thanks to Francesco Musiani, francesco.musiani@unibo.it and Massimo Sandal) is:
504 - take care of the PicoForce trigger bug - exclude retraction portions with too high standard deviation
505 - fit the second half of the retraction curve to a line
506 - if the fit is not almost horizontal, take a smaller chunk and repeat
507 - otherwise, we have something horizontal
508 - so take the average of horizontal points and use it as a baseline
510 Then, start from the rise of the retraction curve and look at the first point below the
513 FIXME: should be moved, probably to generalvclamp.py
519 outplot=self.subtract_curves(1)
520 xret=outplot.vectors[1][0]
521 ydiff=outplot.vectors[1][1]
523 xext=plot.vectors[0][0]
524 yext=plot.vectors[0][1]
525 xret2=plot.vectors[1][0]
526 yret=plot.vectors[1][1]
528 #taking care of the picoforce trigger bug: we exclude portions of the curve that have too much
529 #standard deviation. yes, a lot of magic is here.
531 monlength=len(xret)-int(len(xret)/20)
534 monchunk=scipy.array(ydiff[monlength:finalength])
535 if abs(np.std(monchunk)) < 2e-10:
537 else: #move away from the monster
538 monlength-=int(len(xret)/50)
539 finalength-=int(len(xret)/50)
542 #take half of the thing
543 endlength=int(len(xret)/2)
548 xchunk=yext[endlength:monlength]
549 ychunk=yext[endlength:monlength]
550 regr=scipy.stats.linregress(xchunk,ychunk)[0:2]
551 #we stop if we found an almost-horizontal fit or if we're going too short...
552 #FIXME: 0.1 and 6 here are "magic numbers" (although reasonable)
553 if (abs(regr[1]) > 0.1) and ( endlength < len(xret)-int(len(xret)/6) ) :
559 ymean=np.mean(ychunk) #baseline
564 #find the first point below the calculated baseline
570 #The algorithm didn't find anything below the baseline! It should NEVER happen
578 def find_contact_point2(self, debug=False):
580 TO BE DEVELOPED IN THE FUTURE
581 Finds the contact point on the curve.
583 FIXME: should be moved, probably to generalvclamp.py
586 #raw_plot=self.current.curve.default_plots()[0]
587 raw_plot=self.plots[0]
588 '''xext=self.plots[0].vectors[0][0]
589 yext=self.plots[0].vectors[0][1]
590 xret2=self.plots[0].vectors[1][0]
591 yret=self.plots[0].vectors[1][1]
593 xext=raw_plot.vectors[0][0]
594 yext=raw_plot.vectors[0][1]
595 xret2=raw_plot.vectors[1][0]
596 yret=raw_plot.vectors[1][1]
598 first_point=[xext[0], yext[0]]
599 last_point=[xext[-1], yext[-1]]
601 #regr=scipy.polyfit(first_point, last_point,1)[0:2]
602 diffx=abs(first_point[0]-last_point[0])
603 diffy=abs(first_point[1]-last_point[1])
605 #using polyfit results in numerical errors. good old algebra.
607 b=first_point[1]-(a*first_point[0])
608 baseline=scipy.polyval((a,b), xext)
610 ysub=[item-basitem for item,basitem in zip(yext,baseline)]
612 contact=ysub.index(min(ysub))
614 return xext,ysub,contact
616 #now, exploit a ClickedPoint instance to calculate index...
618 dummy.absolute_coords=(x_intercept,y_intercept)
619 dummy.find_graph_coords(xret2,yret)
622 return dummy.index, regr, regr_contact
628 def x_do_contact(self,args):
630 DEBUG COMMAND to be activated in the future
632 xext,ysub,contact=self.find_contact_point2(debug=True)
634 contact_plot=self.plots[0]
635 contact_plot.add_set(xext,ysub)
636 contact_plot.add_set([xext[contact]],[self.plots[0].vectors[0][1][contact]])
637 #contact_plot.add_set([first_point[0]],[first_point[1]])
638 #contact_plot.add_set([last_point[0]],[last_point[1]])
639 contact_plot.styles=[None,None,None,'scatter']
640 self._send_plot([contact_plot])
644 index,regr,regr_contact=self.find_contact_point2(debug=True)
647 raw_plot=self.current.curve.default_plots()[0]
648 xret=raw_plot.vectors[0][0]
649 #nc_line=[(item*regr[0])+regr[1] for item in x_nc]
650 nc_line=scipy.polyval(regr,xret)
651 c_line=scipy.polyval(regr_contact,xret)
654 contact_plot=self.current.curve.default_plots()[0]
655 contact_plot.add_set(xret, nc_line)
656 contact_plot.add_set(xret, c_line)
657 contact_plot.styles=[None,None,None,None]
658 #contact_plot.styles.append(None)
659 contact_plot.destination=1
660 self._send_plot([contact_plot])