...
This R code aims to replace a previous excel spreadsheet that was being used by members of the lab. The functionality should be identical as the code is based on the calculations made in the spreadsheet. The code is written as follows:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
aequorin_calibration_function<-function(file_path,rep_type,rep_custom,data_type,plot_offset){ #Assuming all data is of the same type data_file <- read.csv(file_path, header=T) if(is.na(data_file[1,dim(data_file)][2])==TRUE){ data_length<-dim(data_file)[2]-4} else{ data_length<-dim(data_file)[2]-3} rep1<-list() if(rep_type=="Numbers"){ num_u<-unique(data_file[,2]) for (num_u_i in 1:length(num_u)){ rep1[[num_u_i]]<-(which(data_file[,2]==num_u[num_u_i]))} }else if(rep_type=="Letters"){ let_u<-unique(data_file[,3]) for (let_u_i in 1:length(let_u)){ rep1[[let_u_i]]<-(which(data_file[,3]==let_u[let_u_i]))} } else if(rep_type=="Custom"){ rep1<-rep_custom } else{ return("ERROR: You have not specified replicate type in the second arguement. \"Numbers\" indicate replicates are ordered by numbers, \"Letters\" by letters. Choose \"Custom\" if replicates are ordered by some other means and please specify which in the next argument.") } # Define constants - Note only works right now if all samples are of the same data type, would need to specify a list of "WT" or "Mut" etc. result_mat1_list<-list() for (ml1 in 1:length(rep1)){ if(data_type[ml1]=="WT"){ KR22<-7330000 KR37<-KR22*sqrt(2) KTR<-120 Slope<-2.99 Ratec<-1 }else if(data_type[ml1]=="Mut"){ KR22<-1.61*10^7 KR37<-KR22*sqrt(2) KTR<-22008 Slope<-1.43 Ratec<-1 }else if(data_type[ml1]=="Mut_coeln"){ KR22<-8.47*10^7 KR37<-KR22*sqrt(2) KTR<-165600 Slope<-1.2038 Ratec<-0.138 }else{ return("ERROR: data type has not been specified, pleas put either \"WT\", \"Mut\" or \"Mut_coeln\".") } result_mat1<-matrix(0,data_length,4*length(rep1[[ml1]]),) for (i in 1:length(rep1[[ml1]])){ result_mat1[,i]<-as.numeric(data_file[rep1[[ml1]][i],][-c(1:3,184)]) min_ratio<-min(result_mat1[,i]) result_mat1[1,i+length(rep1[[ml1]])]<-((result_mat1[1,i]-(min_ratio*Ratec))/(sum(result_mat1[,i])))^(1/Slope) result_mat1[1,i+(2*length(rep1[[ml1]]))]<-(result_mat1[1,i+length(rep1[[ml1]])]+(result_mat1[1,i+length(rep1[[ml1]])]*KTR)-1)/(KR37*(1-result_mat1[1,i+length(rep1[[ml1]])])) result_mat1[1,i+(3*length(rep1[[ml1]]))]<-result_mat1[1,i+(2*length(rep1[[ml1]]))]*10^6 for (j in 2:data_length){ result_mat1[j,i+length(rep1[[ml1]])]<-((result_mat1[j,i]-(min_ratio*Ratec))/(sum(result_mat1[-c(1:(j-1)),i])))^(1/Slope) result_mat1[j,i+(2*length(rep1[[ml1]]))]<-(result_mat1[j,i+length(rep1[[ml1]])]+(result_mat1[j,i+length(rep1[[ml1]])]*KTR)-1)/(KR37*(1-result_mat1[j,i+length(rep1[[ml1]])])) result_mat1[j,i+(3*length(rep1[[ml1]]))]<-result_mat1[j,i+(2*length(rep1[[ml1]]))]*10^6 } } result_mat1_list[[(ml1*2)-1]]<-result_mat1 a<-plot_offset RN<-rep(1,(data_length-a)) ratio<-result_mat1[c(1:(data_length-a)),length(rep1[[ml1]])+1] C2pC<-result_mat1[c(1:(data_length-a)),length(rep1[[ml1]])*3+1] for (i1 in 2:length(rep1[[ml1]])){ RN<-c(RN,rep(i1,(data_length-a))) ratio<-c(ratio,result_mat1[c(1:(data_length-a)),length(rep1[[ml1]])+i1]) C2pC<-c(C2pC,result_mat1[c(1:(data_length-a)),length(rep1[[ml1]])*3+i1]) } DF_plot<-data.frame(time=rep(c(1:(data_length-a)),length(rep1[[ml1]])),ReplicateNumber=RN,Ratio=ratio,Ca2plusConc=C2pC) result_mat1_list[[(ml1*2)]]<-ggplot(DF_plot, aes(x=time, y=Ca2plusConc, colour=factor(ReplicateNumber), group=factor(ReplicateNumber)))+ scale_colour_brewer(palette="Set1")+ theme_bw()+ geom_line(size=1) + guides(color=guide_legend(title="Replicates"))+ geom_point() +ylab("Ca2+ concentration") +xlab("Time") +labs(title=as.character(ml1)) } return(result_mat1_list) } |
...