R/ana_method_AP.R

Defines functions curAP_

#' @describeIn cursormethods 
#' analyse cardiac action potentials (APD90 etc) 
#' @export
curAP_ <- function(name) {
  
  # we define a single method that returns a data frame
  AP<-function(cut, s){
    # how to turn CAP's into cursor methods: 
    # caps get the whole trace and a cursor, 
    # cursormethods get "cut", which is already the cutout....
    # so we have to map cut, s to d 
    d=NULL
    d$Y=cut$y[,s]
    d$X=cut$x[,s]
    
    # then, most CAP's have the following line or alike:
    ## get indices of cursor range
    # range_total<-findxRange(d$X,d$APrange)
    # we already havwe cut data, so we have to modify this into:
    range_total<-1:length(d$Y)
    
    
    
    last<-function(x) rev(rev(x)[1])
    threshold=-30
    
    max_in_range<-which.max(d$Y[range_total])
    #print(max_in_range)
    range_pre=range_total[1:max_in_range]
    range_post=range_total[max_in_range:length(range_total)]
    
    max_postion=range_total[max_in_range]
    min_position=range_post[which.min(d$Y[range_post])]
    
    
    dVdT<-function(p, show=F){
      d_=5 #segment
      upstroke_voltage<-(d$Y[p+d_]-d$Y[p])*1e-3
      upstroke_time<-(d$X[p+d_]-d$X[p])
      
      velocity<-upstroke_voltage/upstroke_time
      if(show) 
        arrows(d$X[p],d$Y[p],d$X[p+d_],d$Y[p+d_], col=max(1,floor(velocity)),lty=1,len=0, lwd=3)
      velocity
      
    }
    velocites=sapply(range_pre, dVdT)
    max_velocity=max(velocites)
    max_velocity_position<-range_pre[which.max(velocites)]
    
    
    amplitude<-d$Y[max_postion]-d$Y[min_position]
    Vm<-d$Y[min_position]
    threshold=max(threshold, Vm+10)
    
    
    level90<-d$Y[max_postion]-amplitude*.9
    onset_position=range_pre[which(d$Y[range_pre]>threshold)]
    onset_position=min(onset_position,max_velocity_position)
    APD90_end<-range_post[which(d$Y[range_post]<level90)[1]]
    APD90=d$X[APD90_end]-d$X[max_postion]
    
    level50<-d$Y[max_postion]-amplitude*.5
    APD50_end<-range_post[which(d$Y[range_post]<level50)[1]]
    APD50=d$X[APD50_end]-d$X[max_postion]
    
    level30<-d$Y[max_postion]-amplitude*.3
    APD30_end<-range_post[which(d$Y[range_post]<level30)[1]]
    APD30=d$X[APD30_end]-d$X[max_postion]
    
    #title(sub=paste(xdistance,ydistance))
    data.frame(APD90,
               APD50,
               APD30,
               max_velocity, 
               Vm, 
               amplitude, 
               peak=d$Y[max_postion],
               .min_position=d$X[min_position], 
               .max_postion=d$X[max_postion], 
               .onset_position=d$X[onset_position],
               .APD90_end=d$X[APD90_end], 
               .level_90=level90, 
               .APD50_end=d$X[APD50_end], 
               .level_50=level50,
               .APD30_end=d$X[APD30_end], 
               .level_30=level30
    )
    
  }
  
  #include the method in the methods list
  methods <- list(AP)
  names(methods)<-name
  #showresults we leave first empty
  #show it
  showresults <- function(cursor, results, sweeps){
    # extract values of *this* cursor from results
    .<-function(resultname){
      valname <- names(cursor$analyse.methods)
      results[sweeps, paste(valname,resultname, sep=".")]
    }
    
    # show Vm as baseline across the cursor region
    arrows(cursor$range[1],.("Vm"),cursor$range[2], col="blue",lty=3,len=0, lwd=1)
    
    # mark the point where Vm is picked from
    points(.(".min_position"),.("Vm"))
    
    # show onset position as vertical line
    arrows(x0=.(".onset_position"), y0=.("peak"), y1=.("Vm"), col="blue",lty=3,len=0, lwd=1)
    
    # show apd90,50,30
    arrows(.(".onset_position"),.(".level_30"),.(".APD30_end"), col="orange",lty=1,len=.1, lwd=1, code = 3)
    arrows(.(".onset_position"),.(".level_50"),.(".APD50_end"), col="orange",lty=1,len=.1, lwd=1, code = 3)
    arrows(.(".onset_position"),.(".level_90"),.(".APD90_end"), col="red",lty=1,len=.1, lwd=1, code = 3)
    
    APD90=sprintf("%.3g ms", .("APD90")*1e3)
    APD50=sprintf("%.3g ms", .("APD50")*1e3)
    APD30=sprintf("%.3g ms", .("APD30")*1e3)
    Vm=sprintf("%.3g mV", .("Vm"))
    mid<-function(a,b){a+(b-a)/2}
    text(x = mid(.(".onset_position"),.(".APD90_end")), .(".level_90"), labels =APD90,  pos = 3, offset=0.2, cex=0.8, col="red")
    text(x = mid(.(".onset_position"),.(".APD50_end")), .(".level_50"), labels = APD50, pos = 3, offset=0.2, cex=0.7, col="orange")
    text(x = mid(.(".onset_position"),.(".APD30_end")), .(".level_30"), labels = APD30, pos = 3, offset=0.2, cex=0.7, col="orange")
    text(x = mid(.(".onset_position"),.(".APD90_end")), .("Vm"),        labels = Vm,    pos = 3, offset=0.2, cex=0.8, col="blue")
    
  }
  attr(methods, "plot.fun.last") <- showresults
  attr(methods, "name") <- name 
  methods
}
tdanker/ephys2 documentation built on Aug. 11, 2019, 12:12 p.m.