#####
# Script for cleaning, analyzing your microplate nutrient results
#####
# Ellen Esch, 25 Sept 2019
#####
# Set-up
#####
library(tidyverse)
library(readxl)
setwd("/Users/ellen/Downloads")
# uncomment and edit the number of reps per sample (will be 3 or 4), and the number of plates so that it accurately reflects your run
# nreps = 3
# nplates = 4
# uncomment which standard curve you are using
# # low, triplicate
# standard = tibble (row = c(rep("A",12), rep("B", 6)),
# column = c(1:12, 1:6),
# concentration = rep(c(0,.05, .1, .2, .5, 1), each=3))
# #high, triplicate
# standard = tibble (row = c(rep("A",12), rep("B", 6)),
# column = c(1:12, 1:6),
# concentration = rep(c(0,.5, 1, 2, 5, 10), each=3) )
# #low, quarduplicate
# standard = tibble (row = c(rep("A",12), rep("B", 12)),
# column = c(1:12, 1:12),
# concentration = rep(c(0,.05, .1, .2, .5, 1), each=4))
# #high, quadruplicate
# standard = tibble (row = c(rep("A",12), rep("B", 12)),
# column = c(1:12, 1:12),
# concentration = rep(c(0,.5, 1, 2, 5, 10), each=4) )
df_names <- read_xlsx("./PhosTestIDs.xlsx")
df <- read_xlsx("./PhosTest.xlsx") %>%
set_names('row','1','2','3','4','5','6','7','8','9','10','11','12','nm','notes') %>% # if you don't have any notes, delete that column;
filter(!is.na(`1`)) %>%# get rid of any space between runs
filter(`1`!='1',`2`!='2',`3`!='3') %>% # remove the duplicate row names that are associated with each plate run
mutate(plate = rep(1 : (nrow(.) / 8), each = 8)) %>% #info about plates
gather(column,absorbance,-plate, -row,-nm,-notes) %>% # again, if you dont have notes, delete
mutate(column = as.numeric(column)) %>%
arrange(plate, row, column) %>% #must arrange correctly to assign replicates
full_join(standard) # join with standard curve layout
####
#Check standard curve
####
calibration <- df %>%
filter(!is.na(concentration)) %>%
group_by(plate, concentration) %>%
summarise(absorbance_mean = mean(absorbance), absorbance_se = sd(absorbance)/sqrt(n()))
calibration %>% ggplot(aes(x=concentration,y=absorbance_mean, col= as.factor(plate))) +
geom_point()+
geom_smooth(method = 'lm') +
geom_errorbar(aes(ymin = absorbance_mean - absorbance_se, ymax = absorbance_mean + absorbance_se))
# you might have to do this separately for each plate (if the standard curves are quite different between plates).
model <- summary(lm(absorbance_mean~concentration, data=calibration))
intercept <- model$coefficients[1,1]
slope <- model$coefficients[2,1]
####
#Compute ppm of samples
####
samples <- df %>%
filter(is.na(concentration)) %>%
arrange(plate, row, column) %>% #must arrange correctly to assign replicates
mutate(unique = rep(1 : (nrow(.) / 3), each = nreps)) %>% #unique id for your samples
group_by(unique, plate, nm) %>%
summarise(absorbance_mean = mean(absorbance), absorbance_se = sd(absorbance)/sqrt(n())) %>%
full_join(df_names) %>%
mutate(concentration = (absorbance_mean - intercept)/slope) %>%
mutate(nutrient = ifelse(nm == 880, 'Orthophosphate'))
samples %>%
ggplot(aes(y=concentration, x = name, col=as.factor(plate))) +
geom_point() +
theme(axis.text.x = element_text(angle = 90))