A short notebook for calculating relative electrolyte conductivity using data collected by Ruth Nichols in Julkowska lab.

The analysis pipeline developed by Magda Julkowska

Load and organize data:

data <- read.csv("Tray1_BFR.csv", head = T)
data

OK - so this is what we got for data on Tray 1 before cooking the samples - let’s organize them per cell.

in order to do that- we need to melt the dataset:

library(reshape2)
mdata <- melt(data)
## Using X as id variables
mdata$variable <- gsub("X", "", mdata$variable)
mdata$cell <- paste(mdata$X, mdata$variable, sep="")
mdata
mdata$Tray <- "Tray01"
mdata2 <- mdata[,c(5,4,3)]
colnames(mdata2)[3] <- "Conductivity.BFR"
mdata2

OK - now let’s add to this the matched data AFTER and CODING:

deta <- read.csv("Tray1_AFTR.csv", head = T)
mdeta <- melt(deta)
## Using X as id variables
mdeta$variable <- gsub("X", "", mdeta$variable)
mdeta$cell <- paste(mdeta$X, mdeta$variable, sep="")
mdeta$Tray <- "Tray01"
mdeta2 <- mdeta[,c(5,4,3)]
colnames(mdeta2)[3] <- "Conductivity.AFTR"
mdeta2

now let’s merge it:

Tray01 <- merge(mdata2, mdeta2, by = c("Tray", "cell"))
Tray01

OK - now let’s add coding to this too:

code <- read.csv("Tray1_Coding.csv", head = T)
code
mcode <- melt(code, id = "X")
mcode
mcode$variable <- gsub("X", "", mcode$variable)
mcode$cell <- paste(mcode$X, mcode$variable, sep="")
mcode$Tray <- "Tray01"
mcode2 <- mcode[,c(5,4,3)]
colnames(mcode2)[3] <- "Sample"
mcode2
Tray01 <- merge(Tray01, mcode2, by = c("Tray", "cell"))
Tray01

OK - now let’s also separate the sample into individual elements:

strsplit(Tray01$Sample[1], " ")[[1]][1]
## [1] "T2D4"
for(i in 1:nrow(Tray01)){
  Tray01$Rep[i] <- strsplit(Tray01$Sample[i], " ")[[1]][1]
  Tray01$Genotype[i] <- strsplit(Tray01$Sample[i], " ")[[1]][2]
  Tray01$Genotype2[i] <- strsplit(Tray01$Sample[i], " ")[[1]][3]
  Tray01$Condition[i] <- strsplit(Tray01$Sample[i], " ")[[1]][4]
}

Tray01

OK - this is a bit messy - since Col-0 is having less spaces in its name - so let’s separate Col-0 from this dataset:

Col <- subset(Tray01, Tray01$Genotype == "(Col-0")
mut <- subset(Tray01, Tray01$Genotype != "(Col-0")
colnames(Col)[8] <- "Condition"
Col1 <- Col[,c(6:8,1:4)]
mut1 <- mut[,c(6, 8:9, 1:4)]

unique(Col1$Genotype)
## [1] "(Col-0"
Col1$Genotype <- "Col-0"
Col1
colnames(mut1)[2] <- "Genotype"
mut1
mut1$Genotype <- gsub("2M", "akr-2M", mut1$Genotype)
mut1$Genotype <- gsub("3M", "akr-3M", mut1$Genotype)
mut1$Genotype <- gsub("1K1", "akr-1K1", mut1$Genotype)
mut1$Condition <- gsub(")", "", mut1$Condition)


Col1$Condition <- gsub(")", "", Col1$Condition)

Col1
mut1
Tray01 <- rbind(Col1, mut1)
Tray01

OK - now let’s do the same thing but for Tray 03

data <- read.csv("Tray3_BFR.csv", head = T)
mdata <- melt(data)
## Using X as id variables
mdata$variable <- gsub("X", "", mdata$variable)
mdata$cell <- paste(mdata$X, mdata$variable, sep="")
mdata$Tray <- "Tray03"
mdata2 <- mdata[,c(5,4,3)]
colnames(mdata2)[3] <- "Conductivity.BFR"
mdata2
deta <- read.csv("Tray3_AFTR.csv", head = T)
mdeta <- melt(deta)
## Using X as id variables
mdeta$variable <- gsub("X", "", mdeta$variable)
mdeta$cell <- paste(mdeta$X, mdeta$variable, sep="")
mdeta$Tray <- "Tray03"
mdeta2 <- mdeta[,c(5,4,3)]
colnames(mdeta2)[3] <- "Conductivity.AFTR"
mdeta2

now let’s merge it:

Tray03 <- merge(mdata2, mdeta2, by = c("Tray", "cell"))
Tray03

OK - now let’s add coding to this too:

code <- read.csv("Tray3_Coding.csv", head = T)
code
mcode <- melt(code, id = "X")
mcode
mcode$variable <- gsub("X", "", mcode$variable)
mcode$cell <- paste(mcode$X, mcode$variable, sep="")
mcode$Tray <- "Tray03"
mcode2 <- mcode[,c(5,4,3)]
colnames(mcode2)[3] <- "Sample"
mcode2
Tray03 <- merge(Tray03, mcode2, by = c("Tray", "cell"))
Tray03

OK - now let’s also separate the sample into individual elements:

strsplit(Tray03$Sample[1], " ")[[1]][1]
## [1] "T4D3"
for(i in 1:nrow(Tray03)){
  Tray03$Rep[i] <- strsplit(Tray03$Sample[i], " ")[[1]][1]
  Tray03$Genotype[i] <- strsplit(Tray03$Sample[i], " ")[[1]][2]
  Tray03$Genotype2[i] <- strsplit(Tray03$Sample[i], " ")[[1]][3]
  Tray03$Condition[i] <- strsplit(Tray03$Sample[i], " ")[[1]][4]
}

Tray03

OK - this is a bit messy - since Col-0 is having less spaces in its name - so let’s separate Col-0 from this dataset:

Col <- subset(Tray03, Tray03$Genotype == "(Col-0")
mut <- subset(Tray03, Tray03$Genotype != "(Col-0")
colnames(Col)[8] <- "Condition"
Col1 <- Col[,c(6:8,1:4)]
mut1 <- mut[,c(6, 8:9, 1:4)]

unique(Col1$Genotype)
## [1] "(Col-0"
Col1$Genotype <- "Col-0"
Col1
colnames(mut1)[2] <- "Genotype"
mut1
mut1$Genotype <- gsub("2M", "akr-2M", mut1$Genotype)
mut1$Genotype <- gsub("3M", "akr-3M", mut1$Genotype)
mut1$Genotype <- gsub("1K1", "akr-1K1", mut1$Genotype)
mut1$Condition <- gsub(")", "", mut1$Condition)


Col1$Condition <- gsub(")", "", Col1$Condition)

Col1
mut1
Tray03 <- rbind(Col1, mut1)
Tray03

OK - cool - now let’s get the two trays together:

all_data <- rbind(Tray01, Tray03)
all_data

Calculate relative conductivity

all_data$RelativeConductivity <- all_data$Conductivity.BFR / all_data$Conductivity.AFTR
# All of the values where electrolyte leakage is larger BEFORE than AFTER are clearly a mistakes - so let's remove all relative values > 1
all_data <- subset(all_data, all_data$RelativeConductivity < 1)
all_data

calculate average per sample (per replicate)

library(doBy)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
sum_data <- summaryBy(RelativeConductivity ~ Rep + Genotype + Condition + Tray + cell, data = all_data)
sum_data

plotting

Great - now let’s plot this stuff:

library(ggplot2)
library(ggpubr)
library(ggsci)

Leakage <- ggerrorplot(sum_data, x = "Genotype", y = "RelativeConductivity.mean", color = "Genotype", fill = "Genotype", 
                       facet.by = "Condition", desc_stat = "mean_sd", add = "jitter",
                       xlab="", ylab= "Fraction of Electrolyte Leakage (a.u.)", add.params = list(color = "darkgray")) + scale_color_d3("category10") + stat_compare_means(method = "anova", label.y = 1)
Leakage <- Leakage + rremove("legend") + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
Leakage