(generalvclamp.py, hooke.conf) autopeak WLC fit interval - now also in nanometers
[hooke.git] / generalvclamp.py
index 4a8cd66874ae598550b08f433875a0c9fd3ceb10..78ee8498c318debbbc80be334f353a31199fe326 100644 (file)
@@ -44,6 +44,8 @@ class generalvclampCommands:
         else:
             dx,unitx,dy,unity=self._delta(set=1)
             print str(dx*(10**9))+' nm'
+            to_dump='distance '+self.current.path+' '+str(dx*(10**9))+' nm'
+            self.outlet.push(to_dump)
 
 
     def do_force(self,args):
@@ -59,6 +61,8 @@ class generalvclampCommands:
             return
         dx,unitx,dy,unity=self._delta(set=1)
         print str(dy*(10**12))+' pN'
+        to_dump='force '+self.current.path+' '+str(dy*(10**12))+' pN'
+        self.outlet.push(to_dump)
         
         
     def do_forcebase(self,args):
@@ -114,6 +118,8 @@ class generalvclampCommands:
         avg=np.mean(to_average)
         forcebase=abs(y-avg)
         print str(forcebase*(10**12))+' pN'
+        to_dump='forcebase '+self.current.path+' '+str(forcebase*(10**12))+' pN'
+        self.outlet.push(to_dump)
         
     
     def plotmanip_flatten(self, plot, current, customvalue=False):
@@ -146,11 +152,13 @@ class generalvclampCommands:
             max_cycles=self.config['flatten'] #Using > 1 usually doesn't help and can give artefacts. However, it could be useful too.
         
         contact_index=self.find_contact_point()
+        
         valn=[[] for item in range(max_exponent)]
         yrn=[0.0 for item in range(max_exponent)]
         errn=[0.0 for item in range(max_exponent)]
         
         for i in range(int(max_cycles)):
+            
             x_ext=plot.vectors[0][0][contact_index+delta_contact:]
             y_ext=plot.vectors[0][1][contact_index+delta_contact:]
             x_ret=plot.vectors[1][0][contact_index+delta_contact:]
@@ -160,8 +168,9 @@ class generalvclampCommands:
                     valn[exponent]=sp.polyfit(x_ext,y_ext,exponent)
                     yrn[exponent]=sp.polyval(valn[exponent],x_ret)
                     errn[exponent]=sp.sqrt(sum((yrn[exponent]-y_ext)**2)/float(len(y_ext)))
-                except TypeError:
+                except Exception,e:
                     print 'Cannot flatten!'
+                    print e
                     return plot
 
             best_exponent=errn.index(min(errn))
@@ -224,6 +233,8 @@ class generalvclampCommands:
         # Outputs the relevant slope parameter
         print 'Slope:'
         print str(parameters[0])
+        to_dump='slope '+self.current.path+' '+str(parameters[0])
+        self.outlet.push(to_dump)
                 
         # Makes a vector with the fitted parameters and sends it to the GUI
         xtoplot=parameters[2]
@@ -307,6 +318,8 @@ class generalvclampCommands:
         reclick : redefines by hand the contact point, if noauto has been used before
                   but the user is unsatisfied of the previously choosen contact point.
         
+        usepoints : fit interval by number of points instead than by nanometers
+        
         When you first issue the command, it will ask for the filename. If you are giving the filename
         of an existing file, autopeak will resume it and append measurements to it. If you are giving
         a new filename, it will create the file and append to it until you close Hooke.
@@ -321,9 +334,14 @@ class generalvclampCommands:
         pl_value=None
         T=self.config['temperature']
         
-        fit_points=int(self.config['auto_fit_points'])
+        if 'usepoints' in args.split():
+            fit_points=int(self.config['auto_fit_points'])
+            usepoints=True
+        else:
+            fit_points=None
+            usepoints=False
+            
         slope_span=int(self.config['auto_slope_span'])
-        
         delta_force=10
         rebase=False #if true=we select rebase
         
@@ -345,23 +363,24 @@ class generalvclampCommands:
                 t_expression=arg.split('=')
                 T=float(t_expression[1])
                
+                
         #Handle contact point arguments
-        #FIXME: code duplication
-        if 'reclick' in args.split():
-            print 'Click contact point'
+        def pickup_contact_point():
+            '''macro to pick up the contact point by clicking'''
             contact_point=self._measure_N_points(N=1, whatset=1)[0]
             contact_point_index=contact_point.index
             self.wlccontact_point=contact_point
             self.wlccontact_index=contact_point.index
             self.wlccurrent=self.current.path
+            return contact_point, contact_point_index
+                
+        if 'reclick' in args.split():
+            print 'Click contact point'
+            contact_point, contact_point_index = pickup_contact_point()
         elif 'noauto' in args.split():
             if self.wlccontact_index==None or self.wlccurrent != self.current.path:
                 print 'Click contact point'
-                contact_point=self._measure_N_points(N=1, whatset=1)[0]
-                contact_point_index=contact_point.index
-                self.wlccontact_point=contact_point
-                self.wlccontact_index=contact_point.index
-                self.wlccurrent=self.current.path
+                contact_point , contact_point_index = pickup_contact_point()
             else:
                 contact_point=self.wlccontact_point
                 contact_point_index=self.wlccontact_index
@@ -402,6 +421,22 @@ class generalvclampCommands:
         forces=[]
         slopes=[]
         
+        def fit_interval_nm(start_index,plot):
+            '''
+            Calculates the number of points to fit, given a fit interval in nm
+            '''
+            nm=self.config['auto_fit_nm']
+            x_vect=plot.vectors[1][0]
+            
+            c=0
+            i=start_index
+            start=x_vect[start_index]
+            while abs(x_vect[i]-x_vect[start_index])*(10**9) < nm:
+                i-=1
+                c+=1
+            
+            return c
+        
         #Cycle between peaks and do analysis.
         for peak in peak_location:
             #Do WLC fits.
@@ -410,6 +445,10 @@ class generalvclampCommands:
             peak_point=ClickedPoint()
             peak_point.absolute_coords=displayed_plot.vectors[1][0][peak], displayed_plot.vectors[1][1][peak]
             peak_point.find_graph_coords(displayed_plot.vectors[1][0], displayed_plot.vectors[1][1])    
+            
+            if not usepoints:
+                fit_points=fit_interval_nm(peak, displayed_plot)
+            
             #-create a clicked point for the other fit point
             other_fit_point=ClickedPoint()
             other_fit_point.absolute_coords=displayed_plot.vectors[1][0][peak-fit_points], displayed_plot.vectors[1][1][peak-fit_points]