#Chemtax analysis for HPLC photosynthetic pigment data
library(limSolve)
The input ratio matrix Ratio contains the pigment compositions (columns) for each algal group (rows);
Chla = matrix( c(1,1,1,1,1,1,1,1,1,0), nrow=10, ncol=1) DVChla = matrix( c(0,0,0,0,0,0,0,0,0,1), nrow=10, ncol=1)
Chlb = matrix( c(0,0,0,0,0,0,1.05,0.2,0,0.14), nrow=10, ncol=1)
ChlC1.2 = matrix( c(0.32,0.32,0.19,0.2,0.22,0.17,0,0,0,0), nrow=10, ncol=1)
ChlC3 = matrix( c(0,0,0,0.33,0.24,0.49,0,0,0,0), nrow=10, ncol=1)
Perid = matrix( c(0, 0.6,0,0,0,0,0,0,0,0), nrow = 10, ncol = 1)
But19 = matrix( c(0, 0, 0, 1.12, 0.01, 0.02, 0, 0, 0, 0), nrow = 10, ncol = 1)
Fucox = matrix( c(0.6,0,0,0.1,0.35,0.29,0,0,0,0), nrow = 10, ncol = 1)
Neox = matrix( c(0,0,0,0,0,0,0.1,0.01,0,0), nrow = 10, ncol = 1)
Prasin = matrix( c(0,0,0,0,0,0,0.24,0,0,0), nrow = 10, ncol = 1)
Viol = matrix( c(0,0,0,0,0,0,0.10,0.11,0,0), nrow = 10, ncol = 1)
Hex19 = matrix( c(0,0,0,0.1,1.34,0.36,0,0,0,0), nrow = 10, ncol = 1)
DD_DT = matrix( c(0.09,0.2,0,0.96,0.19,0.06,0,0,0,0), nrow = 10, ncol = 1)
Allox = matrix( c(0,0,0.29,0,0,0,0,0,0,0), nrow = 10, ncol = 1)
Zeax = matrix( c(0,0,0,0,0,0,0.03,0.07,0.4,0.05), nrow = 10, ncol = 1)
Lutein = matrix( c(0,0,0,0,0,0,0.01,0.71,0,0), nrow = 10, ncol = 1)
ratios <- cbind(Chla, DVChla, Chlb, ChlC1.2, ChlC3, Perid, But19, Fucox, Neox, Prasin, Viol, Hex19, DD_DT, Allox, Zeax, Lutein) colnames(ratios) <- c('Chla', 'DVChla', 'Chlb', 'ChlC1.2', 'ChlC3', 'Perid', '19But', 'Fucox', 'Neo', 'Pras', 'Viol', '19Hex', 'DD_DT', 'Allox', 'Zeax', 'Lutein') rownames(ratios) <- c('Diatoms', 'Dinoflagellates', 'Crytophytes', 'Pelagophytes', 'T3 haptophytes', 'T4 haptophytes', 'Prasinophytes', 'Chlorophytes', 'Synechococcus', 'Prochlorophytes') str(ratios) subset(ratios[,1:16])
palette(rainbow(12, s = 0.6, v = 0.75))
mp <- apply(Chemtax$Ratio, MARGIN = 2, max) # Named num [1:12] pstars <- rbind(t(t(Chemtax$Ratio)/mp) , # num [1:9, 1:12] sample = Chemtax$Field/max(Chemtax$Field)) stars(pstars, len = 0.9, key.loc = c(7.2, 1.7),scale=FALSE,ncol=4, main = "CHEMTAX pigment composition", draw.segments = TRUE, flip.labels=FALSE)
Nx <-nrow(Chemtax$Ratio) # int 8
EE <- rep(1, Nx) # num [1:8] FF <- 1 # num 1
GG <- diag(nrow = Nx) # num [1:8, 1:8] HH <- rep(0, Nx) # num [1:8]
AA <- Chemtax$Ratio/rowSums(Chemtax$Ratio) # num [1:8, 1:12] --> matrix BB <- Chemtax$Field/sum(Chemtax$Field) # num [1:12]
X <- lsei(t(AA), BB, EE, FF, GG, HH)$X (Sample <- data.frame(Algae = rownames(Chemtax$Ratio), fraction = X))
barplot(X, names = rownames(Chemtax$Ratio), col = heat.colors(8), cex.names = 0.8, main = "Chemtax example solved with lsei")
Hello Joaquin, Silvia and Joaquin, Here is the script that Allanah wrote to analyze HPLC data. You cna use it as a basis to develop your own script to analyze the HPLC data from Gran Canaria 2018 (Joaquin) and MSM80 2019 (Silvia). Each of you can create your own project to work on it and invite Allanah and me as collaborators so that we can help you with tracking the changes. The aim at the end is to have a general Chemtax script that anyone can use to analyze HPLC data.
setwd("V:/PhD/Experimental/MSM80 - CUSCO-Humboldt tipping/Data/HPLC") # change as necessary
Field <- read.table(file = "Field.txt", # needs to be converted or extracted as a vector header = TRUE) Ratio <- read.table(file = "Ratio.txt", # needs to be a matrix header = TRUE) # be careful here that the group names are not in the matrix themselves...
Ratio.n <- Ratio[,-1] # removes the first column of names rownames(Ratio.n) <- Ratio[,1] # selects the 1st column of old matrix as new row names Ratio <- as.matrix(Ratio.n) # ensure this remains as a matrix! This is important for further lines of script below.
Field.n <- Field[,-1] Field <- Field.n[1,] # select the first sample Field.v <- as.vector(unlist(Field)) # extracts data for 1st sample as vector called Field.v
CHEMTAX <- list(Ratio = Ratio, Field = Field.v)
mp <- apply(CHEMTAX$Ratio, MARGIN = 2, max) pstars <- rbind(t(t(CHEMTAX$Ratio)/mp), sample = CHEMTAX$Field/max(CHEMTAX$Field))
stars(pstars, len = 0.9, key.loc = c(7.2, 1.7),scale=FALSE,ncol=4, main = "CHEMTAX pigment composition", draw.segments = TRUE, flip.labels=FALSE)
Nx <- nrow(CHEMTAX$Ratio) # int 10
EE <- rep(1, Nx) ## must equal 1 as full fraction # num [1:10] FF <- 1 # num 1
GG <- diag(nrow = Nx) # num [1:10, 1:10] HH <- rep(0, Nx) # num [1:10]
AA <- CHEMTAX$Ratio/rowSums(CHEMTAX$Ratio) # num [1:10, 1:16]
BB <- CHEMTAX$Field/sum(CHEMTAX$Field) # Named num [1:12] --> should be 10! or 16?
X <- lsei(t(AA), BB, EE, FF, GG, HH)$X # t is for transposition of matrix AA (Sample <- data.frame(Algae = rownames(CHEMTAX$Ratio), fraction = X))
barplot(X, names = rownames(CHEMTAX$Ratio), col = heat.colors(8), cex.names = 0.8, main = "CHEMTAX analysis",las=2, ylab = 'Contribution to Chla')
Field <- read.table(file = "Field.txt", # needs to be converted or extracted as a vector header = TRUE) Ratio <- read.table(file = "Ratio.txt", # needs to be a matrix header = TRUE) # be careful here that the group names are not in the matrix themselves...
Ratio.n <- Ratio[,-1] # removes the first column of names rownames(Ratio.n) <- Ratio[,1] # selects the 1st column of old matrix as new row names Ratio <- as.matrix(Ratio.n) # ensure this remains as a matrix! This is important for further lines of script below.
Field.n <- Field[,-1] # removes first column as sample number
extract.field.dat <- function(i) { Field <- Field.n[i,] # select the first sample Field.v <- as.vector(unlist(Field)) # extracts data for 1st sample as vector called Field.v }
CHEMTAX.Field.dat <- lapply(seq_along(Field[,1]), extract.field.dat) # apply function along sample list
#This is called CHEMTAX.l where l = list.
CHEMTAX.l <- list() # create list
list.CHEMTAX <- function(i) { Field.temp = CHEMTAX.Field.dat[[i]] CHEMTAX.l[[i]] <- list(ratios = Ratio, field = Field.temp)
}
CHEMTAX.list <- lapply(seq_along(CHEMTAX.Field.dat), list.CHEMTAX)
names(CHEMTAX.list) <- Field[,1]
CHEMTAX.output <- list() # creates list for output CHEMTAX.analysis <- function(i){ mp <- apply(CHEMTAX.list[[i]]$ratios, MARGIN = 2, max) pstars <- rbind(t(t(CHEMTAX.list[[i]]$ratios)/mp), sample = CHEMTAX.list[[i]]$field/max(CHEMTAX.list[[i]]$field))
# main = "CHEMTAX pigment composition", draw.segments = TRUE,
# flip.labels=FALSE)
Nx <- nrow(CHEMTAX.list[[i]]$ratios) # int 10
EE <- rep(1, Nx) ## must equal 1 as full fraction # num [1:10] FF <- 1 # num 1
GG <- diag(nrow = Nx) # num [1:10, 1:10] HH <- rep(0, Nx) # num [1:10]
AA <- CHEMTAX.list[[i]]$ratios/rowSums(CHEMTAX.list[[i]]$ratios) # num [1:10, 1:16]
BB <- CHEMTAX.list[[i]]$field/sum(CHEMTAX.list[[i]]$field) # Named num [1:12] --> should be 10! or 16?
X <- lsei(t(AA), BB, EE, FF, GG, HH)$X # t is for transposition of matrix AA Sample <- data.frame(Algae = rownames(CHEMTAX.list[[i]]$ratios), fraction = X) }
CHEMTAX.output <- lapply(seq_along(CHEMTAX.list), CHEMTAX.analysis)
names(CHEMTAX.output) <- Field[,1]
output.extract <- function(i) { Fraction <- unlist(CHEMTAX.output[[i]]$fraction) }
CHEMTAX.output.list <- lapply(seq_along(CHEMTAX.output), output.extract)
CHEMTAX.output.cbind.df <- as.data.frame(do.call(cbind, CHEMTAX.output.list)) # can also make matrix
phyto.names <- CHEMTAX.output[[47]]$Algae rownames(CHEMTAX.output.cbind.df) <- phyto.names colnames(CHEMTAX.output.cbind.df) <- Field[,1] CHEMTAX.output.cbind.df$Phyto.group<-CHEMTAX.output[[47]]$Algae #adds phyto group names as a column str(CHEMTAX.output.cbind.df)
library(reshape2) melted <- melt(CHEMTAX.output.cbind.df) str(melted)
stacked.bar <- ggplot(melted, aes(x = variable, y = value, fill = Phyto.group)) + geom_bar(stat = 'identity') + ggtitle('CHEMTAX analyses') + scale_x_discrete(name = 'Sample no.') + scale_y_continuous(name = 'Contribution to Chl a', #limits = c(0,1), expand = c(0,0)) + theme_bw() + theme(axis.text.x = element_text(angle = 90, vjust = 0.3))
stacked.bar
####### END ############