#!/usr/bin/env Rscript
# This script will be used to convert raw qBiCo CFX96 data into qBiCo indices.
#Raw qPCR data to qBiCo indices####
{#dependencies####
# install.packages("xlsx")
library(xlsx)
library(plyr)
library(dplyr)
library(hash)
library(zeallot)
library(gtable)
library(gridExtra)
library(grid)
library(gridtext)
library(ggplot2)
library(stringr)}

#Preprocessing####
{
#Calculate Cy5 Cq from raw data
Get_Cq_Cy5 = function(x,threshold_Cy5){
  cq_above_threshold <- suppressWarnings(min(which(x>threshold_Cy5)))
  if (cq_above_threshold == 1){ # crossing the threshold can not occur in the first cycle
    return(p=NA)
  }
  cq_below_threshold <- cq_above_threshold - 1
  a=x[cq_below_threshold]
  b=x[cq_above_threshold]
  p= cq_above_threshold - (b - threshold_Cy5)/(b-a)
  return(p = p)
}
#Calculate Cy5_5 Cq from raw data
Get_Cq_Cy5_5 = function(x,threshold_Cy5_5){
  cq_above_threshold <- suppressWarnings(min(which(x[5:length(x)]>threshold_Cy5_5))) # Cy5_5 with IPC can start with an RFU above 0; therefore we exclude the first 5 cycles here.
  if (cq_above_threshold == 1){ # crossing the threshold can not occur in the first cycle
    return(p=NA)
  }
  cq_below_threshold <- cq_above_threshold - 1
  a=x[cq_below_threshold]
  b=x[cq_above_threshold]
  p= cq_above_threshold - (b - threshold_Cy5_5)/(b-a)
  return(p = p)
}
#Calculate FAM Cq from raw data
Get_Cq_FAM = function(x,threshold_FAM){
  cq_above_threshold <- suppressWarnings(min(which(x>threshold_FAM)))
  if (cq_above_threshold == 1){ # crossing the threshold can not occur in the first cycle
    return(p=NA)
  }
  cq_below_threshold <- cq_above_threshold - 1
  a=x[cq_below_threshold]
  b=x[cq_above_threshold]
  p= cq_above_threshold - (b - threshold_FAM)/(b-a)
  return(p = p)
}
#Calculate HEX Cq from raw data
Get_Cq_HEX = function(x,threshold_HEX){
  cq_above_threshold <- suppressWarnings(min(which(x>threshold_HEX)))
  if (cq_above_threshold == 1){ # crossing the threshold can not occur in the first cycle
    return(p=NA)
  }
  cq_below_threshold <- cq_above_threshold - 1
  a=x[cq_below_threshold]
  b=x[cq_above_threshold]
  p= cq_above_threshold - (b - threshold_HEX)/(b-a) 
  return(p = p)
}
#Calculate TEX615 Cq from raw data
Get_Cq_TEX615 = function(x,threshold_TEX615){
  cq_above_threshold <- suppressWarnings(min(which(x>threshold_TEX615)))
  if (cq_above_threshold == 1){ # crossing the threshold can not occur in the first cycle
    return(p=NA)
  }
  cq_below_threshold <- cq_above_threshold - 1
  a=x[cq_below_threshold]
  b=x[cq_above_threshold]
  p= cq_above_threshold - (b - threshold_TEX615)/(b-a) 
  return(p = p)
}

auto_treshold = function(input_rfu, quantification_file, lower_ratio_threshold, higher_ratio_threshold, cycle_threshold, target_name){
  #Find wells with standards
  if (target_name == 'hTERT_long' | target_name == 'TPT1_long'){
    quantification_file <- subset(quantification_file,Content == 'Std' & Target == 'hTERT_long' | Content == 'Std' & Target == 'TPT1_long')
} else {
  quantification_file <- subset(quantification_file,Content == 'Std' & Target == target_name)
}
  quantification_file <- subset(quantification_file,!grepl('G',Sample) & !grepl('H',Sample))
  #correct well names
  alb <- c('A','B','C','D','E','F','G','H')
  short_wells <- do.call(c,lapply(c('1','2','3','4','5','6','7','8','9','10','11','12'),X = alb,FUN = paste0))
  long_wells <- do.call(c,lapply(c('01','02','03','04','05','06','07','08','09','010','011','012'),X = alb,FUN = paste0))
  
  quantification_file$Well <- as.vector(sapply(quantification_file$Well, mapvalues, from = long_wells, to = short_wells, warn_missing = FALSE))
  
  input_rfu_std <- input_rfu[which(colnames(input_rfu) %in% quantification_file$Well | colnames(input_rfu) == 'Cycle')]
  
  input_rfu_std <- subset(input_rfu_std, Cycle > cycle_threshold)
  
  df_delta_rfu <- input_rfu_std[which(input_rfu_std$Cycle > cycle_threshold+1),]/ input_rfu_std[which(input_rfu_std$Cycle < 35),]
  
  val_list <- input_rfu_std[which(input_rfu_std$Cycle < 35),][df_delta_rfu > lower_ratio_threshold & df_delta_rfu < higher_ratio_threshold]
  threshold <- sum(val_list)/length(val_list)
  return(p = threshold)
}

Create_3x_standards_df = function(x){
  standards_df <- data.frame(x)
  colnames(standards_df) <- 'StdA'
  rownames(standards_df) <- c('hTERT_long','hTERT_short','LINE1_converted','LINE1_genomic','IPC')
  standards_df$StdB <- as.integer(round(standards_df$StdA/3))
  standards_df$StdC <- as.integer(round(standards_df$StdB/3))
  standards_df$StdD <- as.integer(round(standards_df$StdC/3))
  standards_df$StdE <- as.integer(round(standards_df$StdD/3))
  standards_df$StdF <- as.integer(round(standards_df$StdE/3))
  standards_df$StdG <- as.integer(round(standards_df$StdF/3))
  standards_df$StdH <- as.integer(round(standards_df$StdG/3))
  return(p = standards_df)
}

Create_3x_standards_df_old = function(x){
  standards_df <- data.frame(x)
  colnames(standards_df) <- 'StdA'
  rownames(standards_df) <- c('TPT1_long','hTERT_short','LINE1_converted','LINE1_genomic','IPC')
  standards_df$StdB <- as.integer(round(standards_df$StdA/3))
  standards_df$StdC <- as.integer(round(standards_df$StdB/3))
  standards_df$StdD <- as.integer(round(standards_df$StdC/3))
  standards_df$StdE <- as.integer(round(standards_df$StdD/3))
  standards_df$StdF <- as.integer(round(standards_df$StdE/3))
  standards_df$StdG <- as.integer(round(standards_df$StdF/3))
  standards_df$StdH <- as.integer(round(standards_df$StdG/3))
  return(p = standards_df)
}

get_Cq_from_raw <- function(filename, output_folder, labels, elution_volume, title, standards_df, threshold_Cy5 = NULL, threshold_Cy5_5 = NULL, threshold_FAM = NULL, threshold_HEX = NULL, threshold_TEX615 = NULL, manual_exluded_wells_list = NULL, amount_of_standards = NULL) {
  
  ####
  ## COUPLE WELL TO STANDARD OR SAMPLE NAME  ##
  ####
  input_rfu_Cy5 <- read.csv(paste(filename,"-  Quantification Amplification Results_Cy5.csv"))
  input_rfu_Cy5_5 <- read.csv(paste(filename,"-  Quantification Amplification Results_Cy5-5.csv"))
  input_rfu_FAM <- read.csv(paste(filename,"-  Quantification Amplification Results_FAM.csv"))
  input_rfu_HEX <- read.csv(paste(filename,"-  Quantification Amplification Results_HEX.csv"))
  input_rfu_TEX615 <- read.csv(paste(filename,"-  Quantification Amplification Results_TEX615.csv"))
  
  quantification_file <- read.csv(paste(filename,"-  Quantification Summary.csv"))  # likely needs different reading method depending on machine
  quantification_file$SQ[quantification_file$Content!='Std'] <- NaN #Do not use automatically calculated copy numbers and CQ values by CFX machine.
  
  
  
  # assign standards_df copy numbers to SQ column in quantification_file
  for (standard in colnames(standards_df)){
    for (target in rownames(standards_df))  {
      quantification_file$SQ[Reduce('&',list(quantification_file$Target==target,quantification_file$Sample==standard))] <- standards_df[target,standard]
    }
  }
  
  
  quantification_file$Cq <- NaN
  df_sample_well <- data.frame(quantification_file$Well,quantification_file$Sample)
  colnames(df_sample_well) <- c('Well','Sample')
  
  
  ####
  ## CALCULATE STANDARD CURVES AND CQ VALUES  ##
  ####
  
  
  
  ####
  ## SET THRESHOLDS FOR EACH FLUOROPHORE  ##
  ####
  #If threshold is not set perform autothreshold for that fluorophore. #find exponentional location: ratio between 1.9 and 2.1 and cycle above 15.
  if(is.null(threshold_Cy5)){threshold_Cy5 <- auto_treshold(input_rfu_Cy5, quantification_file, 1.9, 2.1, 15, target_name = 'LINE1_genomic')}
  if(is.null(threshold_Cy5_5)){threshold_Cy5_5 <- auto_treshold(input_rfu_Cy5_5, quantification_file, 1.9, 2.1, 15, target_name = 'IPC')}
  if(is.null(threshold_FAM)){threshold_FAM <- auto_treshold(input_rfu_FAM, quantification_file, 1.9, 2.1, 15, target_name = 'TPT1_long')}
  if(is.null(threshold_HEX)){threshold_HEX <- auto_treshold(input_rfu_HEX, quantification_file, 1.9, 2.1, 15, target_name = 'hTERT_short')}
  if(is.null(threshold_TEX615)){threshold_TEX615 <- auto_treshold(input_rfu_TEX615, quantification_file, 1.9, 2.1, 15, target_name = 'LINE1_converted')}
  
  threshold_list <- c(threshold_Cy5,threshold_Cy5_5,threshold_HEX,threshold_HEX,threshold_TEX615)
  
  input_rfu_Cy5 <- subset(input_rfu_Cy5, select = -c(X,Cycle))
  input_rfu_Cy5_5 <- subset(input_rfu_Cy5_5, select = -c(X,Cycle))
  input_rfu_FAM <- subset(input_rfu_FAM, select = -c(X,Cycle))
  input_rfu_HEX <- subset(input_rfu_HEX, select = -c(X,Cycle))
  input_rfu_TEX615 <- subset(input_rfu_TEX615, select = -c(X,Cycle))
  
  Cq_Cy5 <- apply(input_rfu_Cy5,2,Get_Cq_Cy5, threshold_Cy5 = threshold_Cy5)
  Cq_Cy5 <- data.frame(Cq_Cy5)
  
  Cq_Cy5_5 <- apply(input_rfu_Cy5_5,2,Get_Cq_Cy5_5, threshold_Cy5_5 = threshold_Cy5_5)
  Cq_Cy5_5 <- data.frame(Cq_Cy5_5)
  
  Cq_FAM <- apply(input_rfu_FAM,2,Get_Cq_FAM, threshold_FAM = threshold_FAM)
  Cq_FAM <- data.frame(Cq_FAM)
  
  Cq_HEX <- apply(input_rfu_HEX,2,Get_Cq_HEX, threshold_HEX = threshold_HEX)
  Cq_HEX <- data.frame(Cq_HEX)
  
  Cq_TEX615 <- apply(input_rfu_TEX615,2,Get_Cq_TEX615, threshold_TEX615 = threshold_TEX615)
  Cq_TEX615 <- data.frame(Cq_TEX615)
  
  df_Cq <- data.frame(Cq_Cy5,Cq_Cy5_5,Cq_FAM,Cq_HEX,Cq_TEX615)
  df_Cq
  df_Cq['Well'] <- row.names(df_Cq)
  
  #Add the right copy number per standard to the df_Cq dataframe
  df_Cy5<- subset(quantification_file,Fluor=='Cy5')
  colnames(df_Cy5)[8]<- c('SQ_Cy5')
  df_Cy5_5<- subset(quantification_file,Fluor=='Cy5-5')
  colnames(df_Cy5_5)[8]<- c('SQ_Cy5_5')
  df_FAM<- subset(quantification_file,Fluor=='FAM')
  colnames(df_FAM)[8]<- c('SQ_FAM')
  df_HEX<- subset(quantification_file,Fluor=='HEX')
  colnames(df_HEX)[8]<- c('SQ_HEX')
  df_TEX615<- subset(quantification_file,Fluor=='TEX615')
  colnames(df_TEX615)[8]<- c('SQ_TEX615')
  
  alb <- c('A','B','C','D','E','F','G','H')
  short_wells <- do.call(c,lapply(c('1','2','3','4','5','6','7','8','9','10','11','12'),X = alb,FUN = paste0))
  long_wells <- do.call(c,lapply(c('01','02','03','04','05','06','07','08','09','010','011','012'),X = alb,FUN = paste0))
  
  df_Cy5$Well <-as.vector(sapply(df_Cy5$Well, mapvalues, from = long_wells, to = short_wells, warn_missing = FALSE))
  df_Cy5_5$Well <-as.vector(sapply(df_Cy5_5$Well, mapvalues, from = long_wells, to = short_wells, warn_missing = FALSE))
  df_FAM$Well <-as.vector(sapply(df_FAM$Well, mapvalues, from = long_wells, to = short_wells, warn_missing = FALSE))
  df_HEX$Well <-as.vector(sapply(df_HEX$Well, mapvalues, from = long_wells, to = short_wells, warn_missing = FALSE))
  df_TEX615$Well <-as.vector(sapply(df_TEX615$Well, mapvalues, from = long_wells, to = short_wells, warn_missing = FALSE))
  
  df_Cq <- merge(df_Cq,select(df_Cy5,Well,Content, Sample, SQ_Cy5),by = "Well")
  df_Cq <- merge(df_Cq,select(df_Cy5_5,Well,SQ_Cy5_5),by = "Well")
  df_Cq <- merge(df_Cq,select(df_FAM,Well,SQ_FAM),by = "Well")
  df_Cq <- merge(df_Cq,select(df_HEX,Well,SQ_HEX),by = "Well")
  df_Cq <- merge(df_Cq,select(df_TEX615,Well,SQ_TEX615),by = "Well")
  
  df_Cq$Cq_Cy5 <- round(df_Cq$Cq_Cy5,2)
  df_Cq$Cq_Cy5_5 <- round(df_Cq$Cq_Cy5_5,2)
  df_Cq$Cq_FAM <- round(df_Cq$Cq_FAM,2)
  df_Cq$Cq_HEX <- round(df_Cq$Cq_HEX,2)
  df_Cq$Cq_TEX615 <- round(df_Cq$Cq_TEX615,2)
  
  #Automatically exlude a well from the standards which is an outlier based on standard deviation.
  #subset only wells with standards
  
  #Initialize Exclusion list
  #Start with Well list
  df_Excluded <- data.frame('Well' = rep(df_Cq$Well,5)[order(rep(df_Cq$Well,5))])
  #Add fluor
  df_Excluded$Fluor <- rep(c('Cy5','Cy5_5','FAM','HEX','TEX615'), length(df_Cq$Well))
  df_Excluded$Excluded <- 0
  
  
  df_std_Cq <- subset(df_Cq,Content=='Std')
  if(!is.null(amount_of_standards)){
    standards = c('StdA','StdB','StdC', 'StdD', 'StdE', 'StdF', 'StdG', 'StdH')
    df_std_Cq <- subset(df_std_Cq, Sample %in% standards[1:amount_of_standards])
  } 
  df_std_Cq$Exclude <- FALSE
  
  #if manual exlusion list exists, then ONLY exclude the ones that were manually provided.
  if(!is.null(manual_exluded_wells_list)){
    for (well in manual_exluded_wells_list){
      df_std_Cq$Exclude[df_std_Cq$Well==well]=TRUE
      df_Excluded$Excluded[df_Excluded$Well == well] <- 1
    }  
    df_std_Cq_raw <- data.frame(df_std_Cq)
  } else {
    #exclude all wells were one value is not detected (NaN) #should we exclude it for all standard curves although it does work with the others?
    #row_indices_na <- which(is.na(df_std_Cq),arr.ind=TRUE)
    #df_std_Cq$Exclude[data.frame(row_indices_na)$row]=TRUE
    #df_std_Cq_na <- subset(df_std_Cq,Exclude==TRUE)
    #df_std_Cq <- subset(df_std_Cq,Exclude==FALSE)
    
    
    
    for (level in levels(factor(df_std_Cq$Sample)))
    {
      level_ind = df_std_Cq$Sample==level
      
      # For each fluorophore: check whether there is an outlier in Cq to exlude one standard well.
      for (Cq_fluor in colnames(df_std_Cq)[Reduce('&',list(grepl("Cq",colnames(df_std_Cq)),colnames(df_std_Cq)!='Cq_Cy5_5'))]) 
      {
        fluor <- str_split(Cq_fluor,'_')[[1]][2]
        sq_label = paste('SQ_',strsplit(Cq_fluor,'_')[[1]][2], sep = '')
        #calculate average Cq
        average <- sum(df_std_Cq[[Cq_fluor]][level_ind], na.rm = TRUE)/length(df_std_Cq[[Cq_fluor]][level_ind][!is.na(df_std_Cq[[Cq_fluor]][level_ind])]) #calculate average Cq
        #calculate standard deviation of Cq values
        standard_dev <- sd(df_std_Cq[[Cq_fluor]][df_std_Cq$Sample==level][!is.na(df_std_Cq[[Cq_fluor]][df_std_Cq$Sample==level])])
        #if std/average_cq > 0.02 exclude the well which has the highest difference from the average_cq.
        
        #exclude each value which is outside the range of 2 SD from the average Cq.
        for (Cq_val in df_std_Cq[[Cq_fluor]][df_std_Cq$Sample==level]){
          if(is.na(Cq_val)| is.na(average) | is.na(standard_dev)) {
            if (!is.na(Cq_val)){
            } else {
              bool_list <- is.na(df_std_Cq[[Cq_fluor]][df_std_Cq$Sample==level])
              well <- df_std_Cq$Well[df_std_Cq$Sample==level][bool_list]
              df_std_Cq[[sq_label]][which(df_std_Cq$Well %in% well)] = NA
              df_Excluded$Excluded[Reduce("&",list(df_Excluded$Well == well,df_Excluded$Fluor == fluor))] <- 1
            }
            
          } else if (Cq_val > average + 2 * standard_dev | Cq_val < average - 2 * standard_dev ){
            bool_list <- df_std_Cq[[Cq_fluor]][df_std_Cq$Sample==level] == Cq_val
            well <- df_std_Cq$Well[df_std_Cq$Sample==level][bool_list]
            df_std_Cq[[sq_label]][which(df_std_Cq$Well %in% well)] = NA
            df_Excluded$Excluded[Reduce("&",list(df_Excluded$Well == well,df_Excluded$Fluor == fluor))] <- 1
          }
        }
        
      }
      
      
    }
    
    
    df_std_Cq_raw <- data.frame(df_std_Cq)
    #df_std_Cq_raw <- rbind(df_std_Cq_na, df_std_Cq_raw)
  }
  
  #Use not-excluded wells for creating standard curves.
  #df_std_Cq <- subset(df_std_Cq,Exclude==FALSE)
  
  #Average the Cq values per standard, calculate the log10 values and perform linear regression.
  df_std_Cq_avg_Cy5 <- df_std_Cq %>% group_by(Sample) %>% dplyr::summarise(Avg = mean(head(Cq_Cy5), na.rm = TRUE))
  colnames(df_std_Cq_avg_Cy5) <- c('Std','Cq_Cy5')
  df_std_Cq_avg_Cy5_5 <- df_std_Cq %>% group_by(Sample) %>% dplyr::summarise(Avg = mean(head(Cq_Cy5_5), na.rm = TRUE))
  colnames(df_std_Cq_avg_Cy5_5) <- c('Std','Cq_Cy5_5')
  df_std_Cq_avg_FAM <- df_std_Cq %>% group_by(Sample) %>% dplyr::summarise(Avg = mean(head(Cq_FAM), na.rm = TRUE))
  colnames(df_std_Cq_avg_FAM) <- c('Std','Cq_FAM')
  df_std_Cq_avg_HEX <- df_std_Cq %>% group_by(Sample) %>% dplyr::summarise(Avg = mean(head(Cq_HEX), na.rm = TRUE))
  colnames(df_std_Cq_avg_HEX) <- c('Std','Cq_HEX')
  df_std_Cq_avg_TEX615 <- df_std_Cq %>% group_by(Sample) %>% dplyr::summarise(Avg = mean(head(Cq_TEX615), na.rm = TRUE))
  colnames(df_std_Cq_avg_TEX615) <- c('Std','Cq_TEX615')
  df_std_Cq_avg <- merge(merge(merge(merge(df_std_Cq_avg_Cy5,df_std_Cq_avg_Cy5_5),df_std_Cq_avg_FAM),df_std_Cq_avg_HEX),df_std_Cq_avg_TEX615)
  
  new_col <- t(log10(standards_df[rownames(standards_df)[4],][1:amount_of_standards]))
  colnames(new_col) = 'SQ_Cy5_log'
  df_std_Cq_avg$SQ_Cy5_log <- new_col
  new_col <- t(log10(standards_df[rownames(standards_df)[5],][1:amount_of_standards]))
  colnames(new_col) = 'SQ_Cy5_5_log'
  df_std_Cq_avg$SQ_Cy5_5_log <- new_col
  new_col <-t(log10(standards_df[rownames(standards_df)[1],][1:amount_of_standards]))
  colnames(new_col) <- 'SQ_FAM_log'
  df_std_Cq_avg$SQ_FAM_log <- new_col
  new_col <- t(log10(standards_df[rownames(standards_df)[2],][1:amount_of_standards]))
  colnames(new_col) <- 'SQ_HEX_log'
  df_std_Cq_avg$SQ_HEX_log <- new_col
  new_col <- t(log10(standards_df[rownames(standards_df)[3],][1:amount_of_standards]))
  colnames(new_col) <- 'SQ_TEX615_log'
  df_std_Cq_avg$SQ_TEX615_log <- new_col
  
  df_std_Cq$SQ_Cy5_log <- log10(df_std_Cq$SQ_Cy5)
  df_std_Cq$SQ_Cy5_5_log <- log10(df_std_Cq$SQ_Cy5_5)
  df_std_Cq$SQ_FAM_log <- log10(df_std_Cq$SQ_FAM)
  df_std_Cq$SQ_HEX_log <- log10(df_std_Cq$SQ_HEX)
  df_std_Cq$SQ_TEX615_log <- log10(df_std_Cq$SQ_TEX615)
  
  Cy5.lm <- lm(Cq_Cy5 ~ SQ_Cy5_log, data = df_std_Cq_avg)
  Cy5_5.lm <- lm(Cq_Cy5_5 ~ SQ_Cy5_5_log, data = df_std_Cq_avg)
  FAM.lm <- lm(Cq_FAM ~ SQ_FAM_log, data = df_std_Cq_avg)
  HEX.lm <- lm(Cq_HEX ~ SQ_HEX_log, data = df_std_Cq_avg)
  TEX615.lm <- lm(Cq_TEX615 ~ SQ_TEX615_log, data = df_std_Cq_avg)
  
  Cy_5_efficiency <- (-1 + 10^(-1/Cy5.lm$coefficients[2]))*100
  Cy_5_5_efficiency <- (-1 + 10^(-1/Cy5_5.lm$coefficients[2]))*100
  FAM_efficiency <- (-1 + 10^(-1/FAM.lm$coefficients[2]))*100
  HEX_efficiency <- (-1 + 10^(-1/HEX.lm$coefficients[2]))*100
  TEX615_efficiency <- (-1 + 10^(-1/TEX615.lm$coefficients[2]))*100
  
  intercepts <- c(Cy5.lm$coefficients[1],Cy5_5.lm$coefficients[1],FAM.lm$coefficients[1],HEX.lm$coefficients[1],TEX615.lm$coefficients[1])
  slopes <- c(Cy5.lm$coefficients[2],Cy5_5.lm$coefficients[2],FAM.lm$coefficients[2],HEX.lm$coefficients[2],TEX615.lm$coefficients[2])
  df_std <- data.frame(c(Cy_5_efficiency,Cy_5_5_efficiency,FAM_efficiency,HEX_efficiency,TEX615_efficiency),c(summary(Cy5.lm)$r.squared,summary(Cy5_5.lm)$r.squared,summary(FAM.lm)$r.squared,summary(HEX.lm)$r.squared,summary(TEX615.lm)$r.squared),intercepts,slopes)
  colnames(df_std) <- c('PCR efficiency (%)','r_squared','intercept','slope')
  
  #Create plots for the five assays
  #one plot of regression lines X: Cq (cyle), Y: SQ (copy number) 
  #
  # ggplot(df_std_Cq,aes(SQ_FAM_log,Cq_FAM))
  p1 <- ggplot(df_std_Cq,aes(SQ_FAM_log,Cq_FAM)) + 
    geom_point() + geom_abline(aes(intercept = df_std$intercept[3], slope = df_std$slope[3])) +
    scale_y_continuous(limits = c(20,35)) + scale_x_continuous(limits = c(1.5,4.5)) + 
    theme(axis.title.x=element_blank()) +
    ylab(title) +
    annotate(geom="text", x=4.5, y=35, label=paste("Efficiency = ",round(FAM_efficiency,2),"% \n R^2 = ",round(df_std$r_squared[3],3),"\n Slope = ", round(df_std$slope[3],2),"\n Intercept = ",round(df_std$intercept[3],2)),hjust='right',vjust='top')
  p2 <- ggplot(df_std_Cq,aes(SQ_HEX_log,Cq_HEX)) + 
    geom_point() + geom_abline(aes(intercept = df_std$intercept[4], slope = df_std$slope[4])) +
    scale_y_continuous(limits = c(20,35)) + scale_x_continuous(limits = c(2.0,4.5)) + 
    theme(axis.title.x=element_blank(),
          axis.title.y=element_blank()) +
    annotate(geom="text", x=4.5, y=35, label=paste("Efficiency = ",round(HEX_efficiency,2),"% \n R^2 = ",round(df_std$r_squared[4],3),"\n Slope = ", round(df_std$slope[4],2),"\n Intercept = ",round(df_std$intercept[4],2)),hjust='right',vjust='top')
  p3 <- ggplot(df_std_Cq,aes(SQ_TEX615_log,Cq_TEX615)) +
    geom_point() + geom_abline(aes(intercept = df_std$intercept[5], slope = df_std$slope[5])) +
    scale_y_continuous(limits = c(18,35)) + scale_x_continuous(limits = c(3.7,6.0)) + 
    theme(axis.title.x=element_blank(),
          axis.title.y=element_blank()) +
    annotate(geom="text", x=6, y=35, label=paste("Efficiency = ",round(TEX615_efficiency,2),"% \n R^2 = ",round(df_std$r_squared[5],3),"\n Slope = ", round(df_std$slope[5],2),"\n Intercept = ",round(df_std$intercept[5],2)),hjust='right',vjust='top')
  p4 <- ggplot(df_std_Cq,aes(SQ_Cy5_log,Cq_Cy5)) +
    geom_point() + geom_abline(aes(intercept = df_std$intercept[1], slope = df_std$slope[1])) +
    scale_y_continuous(limits = c(20,35)) + scale_x_continuous(limits = c(2,5)) + 
    theme(axis.title.x=element_blank(),
          axis.title.y=element_blank()) +
    annotate(geom="text", x=5, y=35, label=paste("Efficiency = ",round(Cy_5_efficiency,2),"% \n R^2 = ",round(df_std$r_squared[1],3),"\n Slope = ", round(df_std$slope[1],2),"\n Intercept = ",round(df_std$intercept[1],2)),hjust='right',vjust='top')
  #p5 <- ggplot(df_std_Cq,aes(Sample,Cq_Cy5_5)) +
  #  geom_point() +#+ geom_abline(aes(intercept = df_std$intercept[2], slope = df_std$slope[2])) +
    #scale_y_continuous(limits = c(20,35)) + scale_x_continuous(limits = c(2.3,6.0)) + 
#    theme(axis.title.x=element_blank(),
#          axis.title.y=element_blank()) #+
    #annotate(geom="text", x=max(df_std_Cq_avg$SQ_Cy5_5_log), y=max(df_std_Cq$Cq_Cy5_5), label=paste("Efficiency = ",round(Cy_5_5_efficiency,2),"% \n R^2 = ",round(df_std$r_squared[2],3),"\n Slope = ", round(df_std$slope[2],2),"\n Intercept = ",round(df_std$intercept[2],2)),hjust='right',vjust='top')
  g <- arrangeGrob(p1,p2,p3,p4, nrow = 1)
  
  #one plot with IPC only
  
  
  df_Cq$SQ_Cy5[df_Cq$Content!='Std'] <- 10^((df_Cq$Cq_Cy5[df_Cq$Content!='Std'] - Cy5.lm$coefficients[1])/Cy5.lm$coefficients[2]) 
  df_Cq$SQ_Cy5_5[df_Cq$Content!='Std'] <- 10^((df_Cq$Cq_Cy5_5[df_Cq$Content!='Std'] - Cy5_5.lm$coefficients[1])/Cy5_5.lm$coefficients[2])
  df_Cq$SQ_FAM[df_Cq$Content!='Std'] <- 10^((df_Cq$Cq_FAM[df_Cq$Content!='Std'] - FAM.lm$coefficients[1])/FAM.lm$coefficients[2])
  df_Cq$SQ_HEX[df_Cq$Content!='Std'] <- 10^((df_Cq$Cq_HEX[df_Cq$Content!='Std'] - HEX.lm$coefficients[1])/HEX.lm$coefficients[2])
  df_Cq$SQ_TEX615[df_Cq$Content!='Std'] <- 10^((df_Cq$Cq_TEX615[df_Cq$Content!='Std'] - TEX615.lm$coefficients[1])/TEX615.lm$coefficients[2])
  
  df_Cq_samples <- df_Cq
  df_Cq_samples$amount <- with(labels, amount[match(df_Cq_samples$Sample, labels$plate_label)])
  
  #Create dataframe with standard setup: Well, Fluor, Target, Sample, Cq, DNA input, Elution, excluded
  #Start with Well list
  df_Cq_output <- data.frame('Well' = rep(df_Cq_samples$Well,5)[order(rep(df_Cq_samples$Well,5))])
  #Add fluor
  df_Cq_output$Fluor <- rep(c('Cy5','Cy5_5','FAM','HEX','TEX615'), length(df_Cq_samples$Well))
  #Add target
  df_Cq_output$Target <- rep(c('Genomic','IPC','Long','Short','Converted'), length(df_Cq_samples$Well))
  #Add all samples in run
  sample_list <- c()
  #append matching Cq value to dataframe
  Well_list <- df_Cq_output$Well[!duplicated(df_Cq_output$Well)]
  Cq_list <- c()
  Amount_list <- c()
  exclusion_list <- c()
  for (well in Well_list){
    for (fluor in df_Cq_output$Fluor[df_Cq_output$Well == well]){
      sample <- as.character(df_Cq_samples$Sample[df_Cq_samples$Well == well])
      sample_list <- c(sample_list,sample)
      Cq_label <- paste0('Cq_',fluor)
      Cq_value <- df_Cq_samples[[Cq_label]][df_Cq_samples$Well == well]
      Cq_list <- c(Cq_list,Cq_value)
      amount <- df_Cq_samples$amount[df_Cq_samples$Well == well]
      Amount_list <- c(Amount_list,amount)
      excluded <- df_Excluded$Excluded[Reduce("&",list(df_Excluded$Well == well,df_Excluded$Fluor == fluor))]
      exclusion_list <- c(exclusion_list,excluded)
    }
  }
  df_Cq_output$Sample <- sample_list
  df_Cq_output$Cq <- Cq_list
  df_Cq_output$DNA_input_ng <- Amount_list
  
  df_Cq_output$Elution_volume_ul <- 20

  df_Cq_output$excluded <- exclusion_list
  
  
  df_Cq_output <- subset(df_Cq_output,!is.na(df_Cq_output$Sample))
  
  df_Cq_output$Sample <- with(labels, universal_label[match(df_Cq_output$Sample, labels$plate_label)])
  output_list <- list(df_Cq_output,df_std,df_std_Cq_raw, df_std_Cq_avg ,g, threshold_list)
return(p=output_list)}

foldername <- "/media/nw_disk1/roy/comparative_study/data"
}
#BISULFITE CONVERSION####
{ #BC_exp1####
  ## SET THESE INPUT PARAMETERS  ##
  
  # the excel file as obtained from the pcr machine
  filename <- paste(foldername,"/BC1/220302 comparative study BC exp1",sep ="")
  # make sure the following 6 files are present:
  # "-  Quantification Amplification Results_Cy5.csv"
  # "-  Quantification Amplification Results_Cy5-5.csv"
  # "-  Quantification Amplification Results_FAM.csv"
  # "-  Quantification Amplification Results_HEX.csv"
  # "-  Quantification Amplification Results_TEX615.csv"
  # "-  Quantification Summary.csv"
  
  # name of a folder were all the output will be deposited. This folder will be created relative to the current working directory
  output_folder <- "test"
  
  # generalize sample labels by the following dictionary
  labels= data.frame(c('StdA','StdB','StdC','StdD','StdE','StdF','SHS1 100','SHS1 20','SHS1 10','SHS1 5','SHS1 1','SHS2 100','SHS2 20','SHS2 10','SHS2 5','SHS2 1','SHS3 100','SHS3 20','SHS3 10','SHS3 5','SHS3 1','SHS1 100d','SHS1 20d','SHS1 10d','SHS1 5d','SHS1 1d','SHS2 100d','SHS2 20d','SHS2 10d','SHS2 5d','SHS2 1d','SHS3 100d','SHS3 20d','SHS3 10d','SHS3 5d','SHS3 1d','SHS1 100t','SHS2 100t','SHS3 100t'),c(NA,NA,NA,NA,NA,NA,100,20,10,5,1,100,20,10,5,1,100,20,10,5,1,100,20,10,5,1,100,20,10,5,1,100,20,10,5,1,100,100,100),c('StdA','StdB','StdC','StdD','StdE','StdF','SHS1_100','SHS1_20','SHS1_10','SHS1_5','SHS1_1','SHS2_100','SHS2_20','SHS2_10','SHS2_5','SHS2_1','SHS3_100','SHS3_20','SHS3_10','SHS3_5','SHS3_1','SHS1_100','SHS1_20','SHS1_10','SHS1_5','SHS1_1','SHS2_100','SHS2_20','SHS2_10','SHS2_5','SHS2_1','SHS3_100','SHS3_20','SHS3_10','SHS3_5','SHS3_1','SHS1_100','SHS2_100','SHS3_100'))
  colnames(labels) <- c('plate_label','amount', 'universal_label')
  elution_volume <- 20
  
  title <- 'BC 1'
  
  standards_df <- Create_3x_standards_df_old(c(6000,6000,384000,11400,3000))
  
  #manual_exluded_wells_list= c('A2','B2','C2','D1')
  #manual_exluded_wells_list <- c('D1', 'E1', 'E2', 'F2', 'G2')
  
  manual_exluded_wells_list <- c('A1', 'B1', 'B2','C2', 'F2', 'G1')
  
  t_FAM <- 250
  t_HEX <- 100
  t_TEX <- 100
  t_Cy5 <- 100
  t_Cy5_5 <- 100
  
  BC_1_output <- get_Cq_from_raw(filename, output_folder, labels, elution_volume, title, standards_df, manual_exluded_wells_list = manual_exluded_wells_list, amount_of_standards = 5, threshold_Cy5 = t_Cy5, threshold_Cy5_5 = t_Cy5_5, threshold_FAM = t_FAM, threshold_HEX = t_HEX, threshold_TEX615 = t_TEX)
  BC_1_output <- setNames(BC_1_output, c('df_Cq_output','df_std','df_std_Cq_raw', 'df_std_Cq_avg' ,'g', 'threshold_list'))
  BC_1 <- BC_1_output$df_Cq_output
  #BC_exp2####
  ## SET THESE INPUT PARAMETERS  ##
  
  # the excel file as obtained from the pcr machine
  filename <- paste(foldername,"/BC2/20220628 qBiCo BC exp 2",sep ="")
  # make sure the following 6 files are present:
  # "-  Quantification Amplification Results_Cy5.csv"
  # "-  Quantification Amplification Results_Cy5-5.csv"
  # "-  Quantification Amplification Results_FAM.csv"
  # "-  Quantification Amplification Results_HEX.csv"
  # "-  Quantification Amplification Results_TEX615.csv"
  # "-  Quantification Summary.csv"
  
  # name of a folder were all the output will be deposited. This folder will be created relative to the current working directory
  output_folder <- "test"
  
  # generalize sample labels by the following dictionary
  labels= data.frame(c('StdA','StdB','StdC','StdD','StdE','StdF','s1_100ng','s1_20ng','s1_10ng','s1_5ng','s1_1ng','s2_100ng','s2_20ng','s2_10ng','s2_5ng','s2_1ng','s3_100ng','s3_20ng','s3_10ng','s3_5ng','s3_1ng','s1_double','s2_double','s3_double','s1_1min','s2_1min','s3_1min','s1_5min','s2_5min','s3_5min'),c(NA,NA,NA,NA,NA,NA,100,20,10,5,1,100,20,10,5,1,100,20,10,5,1,100,100,100,100,100,100,100,100,100),c('StdA','StdB','StdC','StdD','StdE','StdF','SHS1_100','SHS1_20','SHS1_10','SHS1_5','SHS1_1','SHS2_100','SHS2_20','SHS2_10','SHS2_5','SHS2_1','SHS3_100','SHS3_20','SHS3_10','SHS3_5','SHS3_1','S1_double','S2_double','S3_double','S1_binding1','S2_binding1','S3_binding1','S1_binding5','S2_binding5','S3_binding5'))
  colnames(labels) <- c('plate_label','amount', 'universal_label')
  elution_volume <- 20
  
  title <- 'BC 2'
  
  standards_df <- Create_3x_standards_df(c(6000,12000,768000,22800,3000))
  manual_exluded_wells_list <- c('H6','H7')
  t_FAM <- 200
  t_HEX <- 100
  t_TEX <- 100
  t_Cy5 <- 100
  t_Cy5_5 <- 100
  
  BC_2_output <- get_Cq_from_raw(filename, output_folder, labels, elution_volume, title, standards_df, manual_exluded_wells_list = manual_exluded_wells_list, amount_of_standards = 5, threshold_Cy5 = t_Cy5, threshold_Cy5_5 = t_Cy5_5, threshold_FAM = t_FAM, threshold_HEX = t_HEX, threshold_TEX615 = t_TEX)
  BC_2_output <- setNames(BC_2_output, c('df_Cq_output','df_std','df_std_Cq_raw', 'df_std_Cq_avg' ,'g', 'threshold_list'))
  BC_2 <- BC_2_output$df_Cq_output
  #BC_exp3####
  ## SET THESE INPUT PARAMETERS  ##
  
  # the excel file as obtained from the pcr machine
  filename <- paste(foldername,"/BC3/20220629 qBiCo BC exp 3",sep ="")
  # make sure the following 6 files are present:
  # "-  Quantification Amplification Results_Cy5.csv"
  # "-  Quantification Amplification Results_Cy5-5.csv"
  # "-  Quantification Amplification Results_FAM.csv"
  # "-  Quantification Amplification Results_HEX.csv"
  # "-  Quantification Amplification Results_TEX615.csv"
  # "-  Quantification Summary.csv"
  
  # name of a folder were all the output will be deposited. This folder will be created relative to the current working directory
  output_folder <- "test"
  
  # generalize sample labels by the following dictionary
  labels= data.frame(c('StdA','StdB','StdC','StdD','StdE','StdF','s1_y','s2_y','s3_y','s1_x_1','s1_x_2','s1_x_3','s2_x_1','s2_x_2','s2_x_3','s3_x_1','s3_x_2','s3_x_3','s1HH','s1HM','s1HL','s1PH','s1PM','s1PL','s2HH','s2HM','s2HL','s2PH','s2PM','s2PL','s3HH','s3HM','s3HL','s3PH','s3PM','s3PL'),c(NA,NA,NA,NA,NA,NA,100,100,100,5,5,5,5,5,5,5,5,5,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100),c('StdA','StdB','StdC','StdD','StdE','StdF','SHS1_100','SHS2_100','SHS3_100','SHS1_5','SHS1_5d','SHS1_5t','SHS2_5','SHS2_5d','SHS2_5t','SHS3_5','SHS3_5d','SHS3_5t','S1_HH','S1_MH','S1_NH','S1_HP','S1_MP','S1_LP','S2_HH','S2_MH','S2_NH','S2_HP','S2_MP','S2_LP','S3_HH','S3_MH','S3_NH','S3_HP','S3_MP','S3_LP'))  
  colnames(labels) <- c('plate_label','amount', 'universal_label')
  elution_volume <- 20
  
  title <- 'BC 3'
  
  standards_df <- Create_3x_standards_df(c(6000,12000,768000,22800,3000))
  manual_exluded_wells_list <- NULL
  t_FAM <- 150
  t_HEX <- 100
  t_TEX <- 100
  t_Cy5 <- 200
  t_Cy5_5 <- 100
  BC_3_output <- get_Cq_from_raw(filename, output_folder, labels, elution_volume, title, standards_df, manual_exluded_wells_list = manual_exluded_wells_list, amount_of_standards = 5, threshold_Cy5 = t_Cy5, threshold_Cy5_5 = t_Cy5_5, threshold_FAM = t_FAM, threshold_HEX = t_HEX, threshold_TEX615 = t_TEX)
  BC_3_output <- setNames(BC_3_output, c('df_Cq_output','df_std','df_std_Cq_raw', 'df_std_Cq_avg' ,'g', 'threshold_list'))
  BC_3 <- BC_3_output$df_Cq_output
  #BC_exp4####
  ## SET THESE INPUT PARAMETERS  ##
  
  # the excel file as obtained from the pcr machine
  filename <- paste(foldername,"/BC4/20220630 qBiCo BC exp 4",sep ="")
  # make sure the following 6 files are present:
  # "-  Quantification Amplification Results_Cy5.csv"
  # "-  Quantification Amplification Results_Cy5-5.csv"
  # "-  Quantification Amplification Results_FAM.csv"
  # "-  Quantification Amplification Results_HEX.csv"
  # "-  Quantification Amplification Results_TEX615.csv"
  # "-  Quantification Summary.csv"
  
  # name of a folder were all the output will be deposited. This folder will be created relative to the current working directory
  output_folder <- "test"
  
  # generalize sample labels by the following dictionary
  labels= data.frame(c('StdA','StdB','StdC','StdD','StdE','StdF','y1','y2','y3','s1_x','s2_x','s3_x','s1_120','s2_120','s3_120','s1_60','s2_60','s3_60','s1_30','s2_30','s3_30','s1_0','s2_0','s3_0','s1_150','s2_150','s3_150','s1_500','s2_500','s3_500','s1_1000','s2_1000','s3_1000'),c(NA,NA,NA,NA,NA,NA,100,100,100,5,5,5,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100),c('StdA','StdB','StdC','StdD','StdE','StdF','SHS1_100','SHS2_100','SHS3_100','SHS1_5','SHS1_5','SHS1_5','S1_UV120','S2_UV120','S3_UV120','S1_UV60','S2_UV60','S3_UV60','S1_UV30','S2_UV30','S3_UV30','S1_SON0','S2_SON0','S3_SON0','S1_SON150','S2_SON150','S3_SON150','S1_SON500','S2_SON500','S3_SON500','S1_SON1000','S2_SON1000','S3_SON1000'))  
  colnames(labels) <- c('plate_label','amount', 'universal_label')
  elution_volume <- 20
  
  title <- 'BC 4'
  
  standards_df <- Create_3x_standards_df(c(6000,12000,768000,22800,3000))
  manual_exluded_wells_list <- c('G8','G9')
  t_FAM <- 200
  t_HEX <- 100
  t_TEX <- 100
  t_Cy5 <- 100
  t_Cy5_5 <- 100
  BC_4_output <- get_Cq_from_raw(filename, output_folder, labels, elution_volume, title, standards_df, manual_exluded_wells_list = manual_exluded_wells_list, amount_of_standards = 5, threshold_Cy5 = t_Cy5, threshold_Cy5_5 = t_Cy5_5, threshold_FAM = t_FAM, threshold_HEX = t_HEX, threshold_TEX615 = t_TEX)
  BC_4_output <- setNames(BC_4_output, c('df_Cq_output','df_std','df_std_Cq_raw', 'df_std_Cq_avg' ,'g', 'threshold_list'))
  BC_4 <- BC_4_output$df_Cq_output
  #BC_exp5####
  ## SET THESE INPUT PARAMETERS  ##
  
  # the excel file as obtained from the pcr machine
  filename <- paste(foldername,"/BC5/20220630 qBiCo BC exp 5",sep ="")
  # make sure the following 6 files are present:
  # "-  Quantification Amplification Results_Cy5.csv"
  # "-  Quantification Amplification Results_Cy5-5.csv"
  # "-  Quantification Amplification Results_FAM.csv"
  # "-  Quantification Amplification Results_HEX.csv"
  # "-  Quantification Amplification Results_TEX615.csv"
  # "-  Quantification Summary.csv"
  
  # name of a folder were all the output will be deposited. This folder will be created relative to the current working directory
  output_folder <- "test"
  
  # generalize sample labels by the following dictionary
  labels= data.frame(c('StdA','StdB','StdC','StdD','StdE','StdF','y1','y2','y3','s1_x','s2_x','s3_x','s1_12h','s2_12h','s3_12h','s1_20h','s2_20h','s3_20h','1HH','1HM','1HL','1PH','1PM','1PL','0%_1','25%_1','50%_1','75%_1','100%_1','0%_2','25%_2','50%_2','75%_2','100%_2'),c(NA,NA,NA,NA,NA,NA,100,100,100,5,5,5,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100),c('StdA','StdB','StdC','StdD','StdE','StdF','1Y','2Y','3Y','1X','2X','3X','S1_12h','S2_12h','S3_12h','S1_20h','S2_20h','S3_20h','S1_HH','S1_MH','S1_NH','S1_HP','S1_MP','S1_LP','M0','M25','M50','M75','M100','M0d','M25d','M50d','M75d','M100d'))  
  colnames(labels) <- c('plate_label','amount', 'universal_label')
  elution_volume <- 20
  
  title <- 'BC 5'
  
  standards_df <- Create_3x_standards_df(c(6000,12000,768000,22800,3000))
  manual_exluded_wells_list <- c('G6')
  t_FAM <- 200
  t_HEX <- 100
  t_TEX <- 100
  t_Cy5 <- 300
  t_Cy5_5 <- 100
  BC_5_output <- get_Cq_from_raw(filename, output_folder, labels, elution_volume, title, standards_df, manual_exluded_wells_list = manual_exluded_wells_list, amount_of_standards = 5, threshold_Cy5 = t_Cy5, threshold_Cy5_5 = t_Cy5_5, threshold_FAM = t_FAM, threshold_HEX = t_HEX, threshold_TEX615 = t_TEX)
  BC_5_output <- setNames(BC_5_output, c('df_Cq_output','df_std','df_std_Cq_raw', 'df_std_Cq_avg' ,'g', 'threshold_list'))
  BC_5 <- BC_5_output$df_Cq_output
  #BC_exp6####
  ## SET THESE INPUT PARAMETERS  ##
  
  # the excel file as obtained from the pcr machine
  filename <- paste(foldername,"/BC6/20220701 qBiCo BC exp 6",sep ="")
  # make sure the following 6 files are present:
  # "-  Quantification Amplification Results_Cy5.csv"
  # "-  Quantification Amplification Results_Cy5-5.csv"
  # "-  Quantification Amplification Results_FAM.csv"
  # "-  Quantification Amplification Results_HEX.csv"
  # "-  Quantification Amplification Results_TEX615.csv"
  # "-  Quantification Summary.csv"
  
  # name of a folder were all the output will be deposited. This folder will be created relative to the current working directory
  output_folder <- "test"
  
  # generalize sample labels by the following dictionary
  labels= data.frame(c('StdA','StdB','StdC','StdD','StdE','StdF','y1','y2','y3','y1_5ft','y2_5ft','y3_5ft','y1_10ft','y2_10ft','y3_10ft'),c(NA,NA,NA,NA,NA,NA,100,100,100,100,100,100,100,100,100),c('StdA','StdB','StdC','StdD','StdE','StdF','SHS1_100','SHS2_100','SHS3_100','S1_5ft','S1_10ft','S2_5ft', 'S2_10ft', 'S3_5ft', 'S3_10ft'))  
  colnames(labels) <- c('plate_label','amount', 'universal_label')
  elution_volume <- 20

  title <- 'BC 6'
  
  standards_df <- Create_3x_standards_df(c(6000,12000,768000,22800,3000))
  manual_exluded_wells_list <- NULL
  t_FAM <- 300
  t_HEX <- 100
  t_TEX <- 100
  t_Cy5 <- 300
  t_Cy5_5 <- 100
  BC_6_output <- get_Cq_from_raw(filename, output_folder, labels, elution_volume, title, standards_df, manual_exluded_wells_list = manual_exluded_wells_list, amount_of_standards = 5, threshold_Cy5 = t_Cy5, threshold_Cy5_5 = t_Cy5_5, threshold_FAM = t_FAM, threshold_HEX = t_HEX, threshold_TEX615 = t_TEX)
  BC_6_output <- setNames(BC_6_output, c('df_Cq_output','df_std','df_std_Cq_raw', 'df_std_Cq_avg' ,'g', 'threshold_list'))
  BC_6 <- BC_6_output$df_Cq_output
  #BC_exp7####
  ## SET THESE INPUT PARAMETERS  ##
  
  # the excel file as obtained from the pcr machine
  filename <- paste(foldername,"/BC7/admin_2022-07-26 12-32-05_CFX96_comparative_s1-s2-s3_4weeks",sep ="")
  # make sure the following 6 files are present:
  # "-  Quantification Amplification Results_Cy5.csv"
  # "-  Quantification Amplification Results_Cy5-5.csv"
  # "-  Quantification Amplification Results_FAM.csv"
  # "-  Quantification Amplification Results_HEX.csv"
  # "-  Quantification Amplification Results_TEX615.csv"
  # "-  Quantification Summary.csv"
  
  # name of a folder were all the output will be deposited. This folder will be created relative to the current working directory
  output_folder <- "test"
  
  # generalize sample labels by the following dictionary
  labels= data.frame(c('StdA','StdB','StdC','StdD','StdE','StdF','sh1_4weeks', 'sh2_4weeks', 'sh3_4weeks'),c(NA,NA,NA,NA,NA,NA,100,100,100),c('StdA','StdB','StdC','StdD','StdE','StdF','1Y','2Y','3Y'))  
  colnames(labels) <- c('plate_label','amount', 'universal_label')
  elution_volume <- 20
  
  title <- 'BC 7'
  
  standards_df <- Create_3x_standards_df(c(6000,12000,768000,22800,3000))
  manual_exluded_wells_list <- NULL
  t_FAM <- 200
  t_HEX <- 100
  t_TEX <- 100
  t_Cy5 <- 200
  t_Cy5_5 <- 100
  BC_7_output <- get_Cq_from_raw(filename, output_folder, labels, elution_volume, title, standards_df, manual_exluded_wells_list = manual_exluded_wells_list, amount_of_standards = 5, threshold_Cy5 = t_Cy5, threshold_Cy5_5 = t_Cy5_5, threshold_FAM = t_FAM, threshold_HEX = t_HEX, threshold_TEX615 = t_TEX)
  BC_7_output <- setNames(BC_7_output, c('df_Cq_output','df_std','df_std_Cq_raw', 'df_std_Cq_avg' ,'g', 'threshold_list'))
  BC_7 <- BC_7_output$df_Cq_output
  }
#ENZYMATIC CONVERSION####
{ #EC_exp1####
## SET THESE INPUT PARAMETERS  ##

# the excel file as obtained from the pcr machine
filename <- paste(foldername,"/EC1/220216 EC experiment1 Marta",sep ="")
# make sure the following 6 files are present:
# "-  Quantification Amplification Results_Cy5.csv"
# "-  Quantification Amplification Results_Cy5-5.csv"
# "-  Quantification Amplification Results_FAM.csv"
# "-  Quantification Amplification Results_HEX.csv"
# "-  Quantification Amplification Results_TEX615.csv"
# "-  Quantification Summary.csv"

# name of a folder were all the output will be deposited. This folder will be created relative to the current working directory
output_folder <- "test"

# generalize sample labels by the following dictionary
labels= data.frame(c('StdA','StdB','StdC','StdD','StdE','StdF','SHS1 100','SHS1 20','SHS1 10','SHS1 5','SHS1 1','SHS2 100','SHS2 20','SHS2 10','SHS2 5','SHS2 1','SHS3 100','SHS3 20','SHS3 10','SHS3 5','SHS3 1','SHS1 100d','SHS1 20d','SHS1 10d','SHS1 5d','SHS1 1d','SHS2 100d','SHS2 20d','SHS2 10d','SHS2 5d','SHS2 1d','SHS3 100d','SHS3 20d','SHS3 10d','SHS3 5d','SHS3 1d','SHS1 100t','SHS2 100t','SHS3 100t'),c(NA,NA,NA,NA,NA,NA,100,20,10,5,1,100,20,10,5,1,100,20,10,5,1,100,20,10,5,1,100,20,10,5,1,100,20,10,5,1,100,100,100),c('StdA','StdB','StdC','StdD','StdE','StdF','S1_100','SHS1_20','SHS1_10','SHS1_5','SHS1_1','SHS2_100','SHS2_20','SHS2_10','SHS2_5','SHS2_1','SHS3_100','SHS3_20','SHS3_10','SHS3_5','SHS3_1','SHS1_100','SHS1_20','SHS1_10','SHS1_5','SHS1_1','SHS2_100','SHS2_20','SHS2_10','SHS2_5','SHS2_1','SHS3_100','SHS3_20','SHS3_10','SHS3_5','SHS3_1','SHS1_100','SHS2_100','SHS3_100'))
colnames(labels) <- c('plate_label','amount', 'universal_label')
elution_volume <- 20

title = 'EC 1'
standards_df <- Create_3x_standards_df_old(c(6000,6000,384000,11400,3000))
manual_exluded_wells_list <- NULL
t_FAM <- 200
t_HEX <- 200
t_TEX <- 100
t_Cy5 <- 200
t_Cy5_5 <- 100
EC_1_output <- get_Cq_from_raw(filename, output_folder, labels, elution_volume, title, standards_df, manual_exluded_wells_list = manual_exluded_wells_list, amount_of_standards = 5, threshold_Cy5 = t_Cy5, threshold_Cy5_5 = t_Cy5_5, threshold_FAM = t_FAM, threshold_HEX = t_HEX, threshold_TEX615 = t_TEX)
EC_1_output <- setNames(EC_1_output, c('df_Cq_output','df_std','df_std_Cq_raw', 'df_std_Cq_avg' ,'g', 'threshold_list'))
EC_1 <- EC_1_output$df_Cq_output
  #EC_exp2####
## SET THESE INPUT PARAMETERS  ##

# the excel file as obtained from the pcr machine
filename <- paste(foldername,"/EC2/220301 comparative study ec experiment 2",sep ="")
# make sure the following 6 files are present:
# "-  Quantification Amplification Results_Cy5.csv"
# "-  Quantification Amplification Results_Cy5-5.csv"
# "-  Quantification Amplification Results_FAM.csv"
# "-  Quantification Amplification Results_HEX.csv"
# "-  Quantification Amplification Results_TEX615.csv"
# "-  Quantification Summary.csv"

# name of a folder were all the output will be deposited. This folder will be created relative to the current working directory
output_folder <- "test"

# generalize sample labels by the following dictionary
labels= data.frame(c('StdA','StdB','StdC','StdD','StdE','StdF','SHS1 100','SHS1 20','SHS1 10','SHS1 5','SHS1 1','SHS2 100','SHS2 20','SHS2 10','SHS2 5','SHS2 1','SHS3 100','SHS3 20','SHS3 10','SHS3 5','SHS3 1','SHS1 5ft','SHS1 10ft','SHS2 5ft', 'SHS2 10ft', 'SHS3 5ft', 'SHS3 10ft', 'SHS1 binding1', 'SHS1 binding5', 'SHS2 binding1', 'SHS2 binding5', 'SHS3 binding1', 'SHS3 binding5'),c(NA,NA,NA,NA,NA,NA,100,20,10,5,1,100,20,10,5,1,100,20,10,5,1,100,100,100,100,100,100,100,100,100,100,100,100),c('StdA','StdB','StdC','StdD','StdE','StdF','SHS1_100','SHS1_20','SHS1_10','SHS1_5','SHS1_1','SHS2_100','SHS2_20','SHS2_10','SHS2_5','SHS2_1','SHS3_100','SHS3_20','SHS3_10','SHS3_5','SHS3_1','S_5ft','S1_10ft','S2_5ft', 'S2_10ft', 'S3_5ft', 'S3_10ft', 'S1_binding1', 'S1_binding5', 'S2_binding1', 'S2_binding5', 'S3_binding1', 'S3_binding5'))
colnames(labels) <- c('plate_label','amount', 'universal_label')
elution_volume <- 20

title = 'EC 2'
standards_df <- Create_3x_standards_df_old(c(6000,6000,384000,11400,3000))
manual_exluded_wells_list <- NULL
t_FAM <- 250
t_HEX <- 200
t_TEX <- 100
t_Cy5 <- 200
t_Cy5_5 <- 100
EC_2_output <- get_Cq_from_raw(filename, output_folder, labels, elution_volume, title, standards_df, manual_exluded_wells_list = manual_exluded_wells_list, amount_of_standards = 5, threshold_Cy5 = t_Cy5, threshold_Cy5_5 = t_Cy5_5, threshold_FAM = t_FAM, threshold_HEX = t_HEX, threshold_TEX615 = t_TEX)
EC_2_output <- setNames(EC_2_output, c('df_Cq_output','df_std','df_std_Cq_raw', 'df_std_Cq_avg' ,'g', 'threshold_list'))
EC_2 <- EC_2_output$df_Cq_output

#EC_exp3####
## SET THESE INPUT PARAMETERS  ##

# the excel file as obtained from the pcr machine
filename <- paste(foldername,"/EC3/220408 qbico ngs samples plate 2",sep ="")
# make sure the following 6 files are present:
# "-  Quantification Amplification Results_Cy5.csv"
# "-  Quantification Amplification Results_Cy5-5.csv"
# "-  Quantification Amplification Results_FAM.csv"
# "-  Quantification Amplification Results_HEX.csv"
# "-  Quantification Amplification Results_TEX615.csv"
# "-  Quantification Summary.csv"

# name of a folder were all the output will be deposited. This folder will be created relative to the current working directory
output_folder <- "test"

# generalize sample labels by the following dictionary
labels= data.frame(c('StdA','StdB','StdC','StdD','StdE','StdF','shs2 5ng','shs2 1ng','shs2 5ng dup','shs2 1ng dup','56','57','58','59','60','61','62','63','64','65'),c(NA,NA,NA,NA,NA,NA,5,1,5,1,100,100,100,100,100,100,100,100,100,100),c('StdA','StdB','StdC','StdD','StdE','StdF','SHS1_5','SHS1_1','SHS1_5','SHS1_1','M0','M25','M50','M75','M100','M0d','M25d','M50d','M75d','M100d'))
colnames(labels) <- c('plate_label','amount', 'universal_label')
elution_volume <- 20

title ='EC 3'
standards_df <- Create_3x_standards_df_old(c(6000,6000,384000,11400,3000))
manual_exluded_wells_list <- NULL
t_FAM <- 100
t_HEX <- 200
t_TEX <- 100
t_Cy5 <- 200
t_Cy5_5 <- 100
EC_3_output <- get_Cq_from_raw(filename, output_folder, labels, elution_volume, title, standards_df, manual_exluded_wells_list = manual_exluded_wells_list, amount_of_standards = 5, threshold_Cy5 = t_Cy5, threshold_Cy5_5 = t_Cy5_5, threshold_FAM = t_FAM, threshold_HEX = t_HEX, threshold_TEX615 = t_TEX)
EC_3_output <- setNames(EC_3_output, c('df_Cq_output','df_std','df_std_Cq_raw', 'df_std_Cq_avg' ,'g', 'threshold_list'))
EC_3 <- EC_3_output$df_Cq_output
  #EC_exp5 (4 week storage)####
## SET THESE INPUT PARAMETERS  ##

# the excel file as obtained from the pcr machine
filename <- paste(foldername,"/EC5/20220720 qbico EC exp 5",sep ="")
# make sure the following 6 files are present:
# "-  Quantification Amplification Results_Cy5.csv"
# "-  Quantification Amplification Results_Cy5-5.csv"
# "-  Quantification Amplification Results_FAM.csv"
# "-  Quantification Amplification Results_HEX.csv"
# "-  Quantification Amplification Results_TEX615.csv"
# "-  Quantification Summary.csv"

# name of a folder were all the output will be deposited. This folder will be created relative to the current working directory
output_folder <- "test"

# generalize sample labels by the following dictionary
labels= data.frame(c('StdA','StdB','StdC','StdD','StdE','StdF','Y1','Y2','Y3'),c(NA,NA,NA,NA,NA,NA,100,100,100),c('StdA','StdB','StdC','StdD','StdE','StdF','SHS1_100','SHS2_100','SHS3_100'))
colnames(labels) <- c('plate_label','amount', 'universal_label')
elution_volume <- 20

title = 'EC 5'
standards_df <- Create_3x_standards_df(c(6000,12000,768000,22800,3000))
manual_exluded_wells_list <- NULL
t_FAM <- 100
t_HEX <- 100
t_TEX <- 50
t_Cy5 <- 50
t_Cy5_5 <- 100
EC_5_output <- get_Cq_from_raw(filename, output_folder, labels, elution_volume, title, standards_df, manual_exluded_wells_list = manual_exluded_wells_list, amount_of_standards = 5, threshold_Cy5 = t_Cy5, threshold_Cy5_5 = t_Cy5_5, threshold_FAM = t_FAM, threshold_HEX = t_HEX, threshold_TEX615 = t_TEX)
EC_5_output <- setNames(EC_5_output, c('df_Cq_output','df_std','df_std_Cq_raw', 'df_std_Cq_avg' ,'g', 'threshold_list'))
EC_5 <- EC_5_output$df_Cq_output
  #EC_exp6####
## SET THESE INPUT PARAMETERS  ##

# the excel file as obtained from the pcr machine
filename <- paste(foldername,"/EC6/20220620 qbico EC exp 6",sep ="")
# make sure the following 6 files are present:
# "-  Quantification Amplification Results_Cy5.csv"
# "-  Quantification Amplification Results_Cy5-5.csv"
# "-  Quantification Amplification Results_FAM.csv"
# "-  Quantification Amplification Results_HEX.csv"
# "-  Quantification Amplification Results_TEX615.csv"
# "-  Quantification Summary.csv"

# name of a folder were all the output will be deposited. This folder will be created relative to the current working directory
output_folder <- "test"

# generalize sample labels by the following dictionary
labels= data.frame(c('StdA','StdB','StdC','StdD','StdE','StdF','1Y','2Y','3Y','1X1','1X2','1X3','2X1','2X2','2X3','3X1','3X2','3X3','1HH','1HM','1HL','1PH','1PM','1PL','2HH','2HM','2HL','2PH','2PM','2PL','3HH','3HM','3HL','3PH','3PM','3PL'),c(NA,NA,NA,NA,NA,NA,100,100,100,10,10,10,10,10,10,10,10,10,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100),c('StdA','StdB','StdC','StdD','StdE','StdF','SHS1_100','SHS2_100','SHS3_100','SHS1_10','SHS1_10','SHS1_10','SHS2_10','SHS2_10','SHS2_10','SHS3_10','SHS3_10','SHS3_10','S1_HH','S1_MH','S1_NH','S1_HP','S1_MP','S1_LP','S2_HH','S2_MH','S2_NH','S2_HP','S2_MP','S2_LP','S3_HH','S3_MH','S3_NH','S3_HP','S3_MP','S3_LP'))
colnames(labels) <- c('plate_label','amount', 'universal_label')
elution_volume <- 20

title = 'EC 6'
standards_df <- Create_3x_standards_df(c(6000,12000,768000,22800,3000))
manual_exluded_wells_list <- c('B3','B4', 'E3','E4')
t_FAM <- 100
t_HEX <- 200
t_TEX <- 100
t_Cy5 <- 250
t_Cy5_5 <- 100
EC_6_output <- get_Cq_from_raw(filename, output_folder, labels, elution_volume, title, standards_df, manual_exluded_wells_list = manual_exluded_wells_list, amount_of_standards = 5, threshold_Cy5 = t_Cy5, threshold_Cy5_5 = t_Cy5_5, threshold_FAM = t_FAM, threshold_HEX = t_HEX, threshold_TEX615 = t_TEX)
EC_6_output <- setNames(EC_6_output, c('df_Cq_output','df_std','df_std_Cq_raw', 'df_std_Cq_avg' ,'g', 'threshold_list'))
EC_6 <- EC_6_output$df_Cq_output
  #EC_exp7####
## SET THESE INPUT PARAMETERS  ##

# the excel file as obtained from the pcr machine
filename <- paste(foldername,"/EC7/20220622 qbico EC exp 7",sep ="")
# make sure the following 6 files are present:
# "-  Quantification Amplification Results_Cy5.csv"
# "-  Quantification Amplification Results_Cy5-5.csv"
# "-  Quantification Amplification Results_FAM.csv"
# "-  Quantification Amplification Results_HEX.csv"
# "-  Quantification Amplification Results_TEX615.csv"
# "-  Quantification Summary.csv"

# name of a folder were all the output will be deposited. This folder will be created relative to the current working directory
output_folder <- "test"

# generalize sample labels by the following dictionary
labels= data.frame(c('StdA','StdB','StdC','StdD','StdE','StdF','Y1','Y2','Y3','X1','X2','X3','TET1','TET2','TET3','APOBEC1','APOBEC2','APOBEC3','120s1','120s2','120s3','60s1','60s2','60s3','30s1','30s2','30s3'),c(NA,NA,NA,NA,NA,NA,100,100,100,10,10,10,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100),c('StdA','StdB','StdC','StdD','StdE','StdF','SHS1_100','SHS2_100','SHS3_100','SHS1_10','SHS2_10','SHS3_10','S1_BT_TET','S2_BT_TET','S3_BT_TET','S1_BT_APOBEC','S2_BT_AOPBEC','S3_BT_APOBEC','S1_UV120','S2_UV120','S3_UV120','S1_UV60','S2_UV60','S3_UV60','S1_UV30','S2_UV30','S3_UV30'))
colnames(labels) <- c('plate_label','amount', 'universal_label')
elution_volume <- 20

title = 'EC 7'
standards_df <- Create_3x_standards_df(c(6000,12000,768000,22800,3000))
manual_exluded_wells_list <- NULL
t_FAM <- 100
t_HEX <- 100
t_TEX <- 100
t_Cy5 <- 200
t_Cy5_5 <- 100
EC_7_output <- get_Cq_from_raw(filename, output_folder, labels, elution_volume, title, standards_df, manual_exluded_wells_list = manual_exluded_wells_list, amount_of_standards = 5, threshold_Cy5 = t_Cy5, threshold_Cy5_5 = t_Cy5_5, threshold_FAM = t_FAM, threshold_HEX = t_HEX, threshold_TEX615 = t_TEX)
EC_7_output <- setNames(EC_7_output, c('df_Cq_output','df_std','df_std_Cq_raw', 'df_std_Cq_avg' ,'g', 'threshold_list'))
EC_7 <- EC_7_output$df_Cq_output
  #EC_exp8####
## SET THESE INPUT PARAMETERS  ##

# the excel file as obtained from the pcr machine
filename <- paste(foldername,"/EC8/20220624 qBiCo EC exp 8",sep ="")
# make sure the following 6 files are present:
# "-  Quantification Amplification Results_Cy5.csv"
# "-  Quantification Amplification Results_Cy5-5.csv"
# "-  Quantification Amplification Results_FAM.csv"
# "-  Quantification Amplification Results_HEX.csv"
# "-  Quantification Amplification Results_TEX615.csv"
# "-  Quantification Summary.csv"

# name of a folder were all the output will be deposited. This folder will be created relative to the current working directory
output_folder <- "test"

# generalize sample labels by the following dictionary
labels= data.frame(c('StdA','StdB','StdC','StdD','StdE','StdF','Y1','Y2','Y3','X1','X2','X3','0-1','0-2','0-3','150-1','150-2','150-3','500-1','500-2','500-3','1000-1','1000-2','1000-3'),c(NA,NA,NA,NA,NA,NA,100,100,100,10,10,10,100,100,100,100,100,100,100,100,100,100,100,100),c('StdA','StdB','StdC','StdD','StdE','StdF','SHS1_100','SHS2_100','SHS3_100','SHS1_10','SHS2_10','SHS3_10','S1_SON0','S2_SON0','S3_SON0','S1_SON150','S2_SON150','S3_SON150','S1_SON500','S2_SON500','S3_SON500','S1_SON1000','S2_SON1000','S3_SON1000'))
colnames(labels) <- c('plate_label','amount', 'universal_label')
elution_volume <- 20

title <- 'EC 8'

standards_df <- Create_3x_standards_df(c(6000,12000,768000,22800,3000))
manual_exluded_wells_list <- c('E6','E7')
t_FAM <- 150
t_HEX <- 100
t_TEX <- 100
t_Cy5 <- 100
t_Cy5_5 <- 100
EC_8_output <- get_Cq_from_raw(filename, output_folder, labels, elution_volume, title, standards_df, manual_exluded_wells_list = manual_exluded_wells_list, amount_of_standards = 5, threshold_Cy5 = t_Cy5, threshold_Cy5_5 = t_Cy5_5, threshold_FAM = t_FAM, threshold_HEX = t_HEX, threshold_TEX615 = t_TEX)
EC_8_output <- setNames(EC_8_output, c('df_Cq_output','df_std','df_std_Cq_raw', 'df_std_Cq_avg' ,'g', 'threshold_list'))
EC_8 <- EC_8_output$df_Cq_output}

#Export Cq values per run####
filename <-  paste0(foldername, '/Comparative_study_Cq_values.xlsx')
run_list <- list(BC_1,BC_2,BC_3,BC_4,BC_5,BC_6,BC_7,EC_1,EC_2,EC_3,EC_5,EC_6,EC_7,EC_8)
label_list <- c('BC_1','BC_2','BC_3','BC_4','BC_5','BC_6','BC_7','EC_1','EC_2','EC_3','EC_5','EC_6','EC_7','EC_8')
  for (i in seq(length(run_list))) {
    run <- run_list[i]
    sheetname <- label_list[i]
    write.xlsx(run, filename, sheetName=sheetname,
               col.names=TRUE, row.names=FALSE, append=TRUE)
  }



#Export standard curves####

bottom <- richtext_grob("Log(Copy number)", gp = gpar(fontsize = 24))
yleft <- richtext_grob("Cq value", rot=90, gp = gpar(fontsize = 24))

Long_t <- richtext_grob("Long", gp = gpar(fontsize = 12))
Short_t <- richtext_grob("Short", gp = gpar(fontsize = 12))
Converted_t <- richtext_grob("Converted", gp = gpar(fontsize = 12))
Genomic_t <- richtext_grob("Genomic", gp = gpar(fontsize = 12))
#IPC_t <- richtext_grob("IPC", gp = gpar(fontsize = 12))

header_grid <- grid.arrange(Long_t,Short_t,Converted_t,Genomic_t,nrow =1) #,IPC_t,nrow =1)
BC_standards <- grid.arrange(header_grid,BC_1_output$g,BC_2_output$g,BC_3_output$g,BC_4_output$g,BC_5_output$g,BC_6_output$g,BC_7_output$g, nrow = 8, bottom = bottom, left = yleft, heights = c(1/36,5/36,5/36,5/36,5/36,5/36,5/36,5/36))
EC_standards <- grid.arrange(header_grid,EC_1_output$g,EC_2_output$g,EC_3_output$g,EC_5_output$g,EC_6_output$g,EC_7_output$g,EC_8_output$g, nrow = 8, bottom = bottom, left = yleft, heights = c(1/36,5/36,5/36,5/36,5/36,5/36,5/36,5/36))

width_png <- 12
height_png <- 16
foldername_png <- paste0(foldername, '/Std')
x_label <- c('Long','Short','Converted','Genomic','IPC')
#Export BC standard curves
ggsave(file = paste0(foldername_png,"/BC_std.png"), BC_standards, width = width_png, height = height_png, limitsize = TRUE)
#Export EC standard curves
ggsave(file = paste0(foldername_png,"/EC_std.png"), EC_standards, width = width_png, height = height_png, limitsize = TRUE)


#Export standard curve parameters####
df_std_export <- data.frame()
experiment_label <- c('BC_1','BC_2','BC_3','BC_4','BC_5','BC_6','BC_7','EC_1','EC_2','EC_3','EC_5','EC_6','EC_7','EC_8')
i=1
for (df_std in list(BC_1_output$df_std,BC_2_output$df_std,BC_3_output$df_std,BC_4_output$df_std,BC_5_output$df_std,BC_6_output$df_std,BC_7_output$df_std,EC_1_output$df_std,EC_2_output$df_std,EC_3_output$df_std,EC_5_output$df_std,EC_6_output$df_std,EC_7_output$df_std,EC_8_output$df_std)){
  df_std$experiment <- experiment_label[i]
  df_std$Fluor <- c('Cy5','Cy5_5','FAM','HEX','TEX615')
  rownames(df_std) <- c('Genomic','IPC','Long','Short','Converted')
  df_std_export <- rbind(df_std_export,df_std)
  i = i + 1
}

filename <- paste0(foldername, '/Std/standard_curve_parameters.xlsx')
wb <- createWorkbook()
sheet_nr <- createSheet(wb)
addDataFrame(df_std_export,sheet = sheet_nr, row.names=TRUE)
saveWorkbook(wb,file = filename)







