R/plotCC.R

Defines functions plotCC

#' @export plotCC
plotCC<- function(files, calculateCC = 50, modelUmbral = 0.85, ...){
  nFiles <- length(list.files(files))
  if (nFiles > 0) {
    file <- list.files(files, full.names = TRUE)
  } else{
    file <- files
  }
  dataPlot <- lapply(file,function(file){
    set.seed(1)
    data <- read.table(file,header = TRUE)
    data <- data[,paste0("X",sort(as.numeric(gsub("X","",colnames(data)))))]
    data <- cData(data)
    data <- sExperiments(data)
    data <- unstack(do.call(rbind.data.frame,data))
    data <- data[,paste0("X",sort(as.numeric(gsub("X","",colnames(data)))))]
    points <- apply(data,2,mean,na.rm=TRUE)
    conc <- as.numeric(gsub("X","",colnames(data)))
    sD <- apply(data,2,sd,na.rm=TRUE)
    sE <- sD/sqrt(apply(!is.na(data),2,sum))
    dataPlot <- cbind(conc,points,sD,sE)
    return(dataPlot)
  })
  par(xpd=FALSE)
  plot(1,type = "n",ylim=c(0,1), xlim = range(unlist(lapply(dataPlot,function(dPlot){dPlot[,"conc"]}))),...)
  lapply(seq_along(dataPlot),function(line){
    lines(dataPlot[[line]][,"conc"],dataPlot[[line]][,"points"],type = "b",lty=line, pch=14+line)
    suppressWarnings(arrows(dataPlot[[line]][,"conc"],dataPlot[[line]][,"points"]+  dataPlot[[line]][,"sD"],dataPlot[[line]][,"conc"],dataPlot[[line]][,"points"]-  dataPlot[[line]][,"sD"],length = 0.05,angle = 90,code=3))
  })
  if (length(file)==1){
    CC <- suppressWarnings(fileCC(file = file,calculateCC = calculateCC, modelUmbral = modelUmbral))
    if(is.numeric(CC[1])){
      points(CC[[1]],1-calculateCC/100,pch=16,col="red")
      arrows(CC[[1]]+CC[[2]],1-calculateCC/100,CC[[1]]-CC[[2]],1-calculateCC/100,pch=16,length = 0.05,angle=90,code=3,col="red")
      par(xpd=TRUE)
      text(CC[1]+(CC[2]),1-calculateCC/100,paste(paste0("CC",calculateCC,":"),ifelse(test = is.na(CC[1]),yes = paste(">",max(conc)),no = round(CC[1],2)),ifelse(test = is.na(CC[2]),yes = "",no = paste("±", round(CC[2],2)))),cex=0.5,pos = 4,col="red")
    }
    # } else {
    #   # lapply(seq_along(file),function(nFile){
    #   #   file <- file[nFile]
    #   #   CC <- suppressWarnings(fileCC(file = file,calculateCC = calculateCC, modelUmbral = modelUmbral))
    #   #   if(is.numeric(CC[1])){
    #   #     points(CC[[1]],1-calculateCC/100,pch=nFile,col="gray40")
    #   #     arrows(CC[[1]]+CC[[2]],1-calculateCC/100,CC[[1]]-CC[[2]],1-calculateCC/100,pch=16,length = 0.05,angle=90,code=3,col="gray40")
    #   #     par(xpd=TRUE)
    #   #     text(CC[1]+(CC[2]),1-calculateCC/100,paste(paste0("CC",calculateCC,":"),ifelse(test = is.na(CC[1]),yes = paste(">",max(conc)),no = round(CC[1],2)),ifelse(test = is.na(CC[2]),yes = "",no = paste("±", round(CC[2],2)))),cex=0.5,pos = round(runif(1,min = 1,max = 4)),col="gray40")
    #   #   }
    #   # })
  }
  legend("topright",legend = sub(pattern = "(.*)\\..*$", replacement = "\\1", basename(file)),bty = "n", lty = seq_along(file), pch = 14+seq_along(file))
}
dosorio/ccCINTROP documentation built on May 5, 2019, 11:09 p.m.