Scientists put a lot of thought, time, energy, and resources into our experiments. Careful planning in the experimental design stage is necessary to avoid wasting our time and materials.
As scientists, we often run short-term, scaled-down versions of the experiments we are interested in conducting in order to test and improve our methods and hopefully yield better data when scaling up. I’ll refer to these practice rounds as “pilots” moving forward. We can use R to analyze changes that we want to make to the procedure of an experiment to optimize our work.
I will use the pilots of my current project, a series of long-term evolution experiments, to demonstrate how R can help in the experimental design phase of research.
I’m interested in seeing how microbial communities change over time. Using bacteria and yeast isolated from sourdough starters, I constructed synthetic communities that started out with equal amounts of each community member. Then, I allowed the communities to grow together and transferred them into fresh media at regular increments. I measured the changes in pH and community member ratios. My intention is to continue this process over a long period of time and see how different community combinations might result in member extinctions and favor the proliferation of some genetic mutations.
Given the long-term nature of my experiment, I’d hate to spend months transfering and measuring several communities only to realize that the method I used to quantify the bacteria was unreliable. I’ll show you how I avoided that here.
Reading in appropriate libraries
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggplot2)
Reading in the csv file
pilot = read.csv("/Users/csmurray/desktop/syncom_pilot_allresults.csv")
summary(pilot)
## community replicate transfer mrs_cfu_ul
## Length:147 Length:147 Min. :0.000 Min. : 0
## Class :character Class :character 1st Qu.:0.000 1st Qu.: 21250
## Mode :character Mode :character Median :1.000 Median : 62000
## Mean :1.411 Mean :115289
## 3rd Qu.:2.000 3rd Qu.:143500
## Max. :3.000 Max. :540000
## NA's :18 NA's :56
## ypd_cfu_ul pilot_round pH plating_method
## Min. : 0 Min. :1.000 Min. :3.315 Length:147
## 1st Qu.: 0 1st Qu.:2.000 1st Qu.:3.840 Class :character
## Median : 395 Median :3.000 Median :4.552 Mode :character
## Mean : 5536 Mean :2.438 Mean :4.997
## 3rd Qu.:10275 3rd Qu.:3.000 3rd Qu.:6.137
## Max. :32300 Max. :3.000 Max. :7.060
## NA's :55 NA's :17 NA's :26
## initial_parent_cfu
## Min. :2000
## 1st Qu.:2000
## Median :2000
## Mean :2567
## 3rd Qu.:4000
## Max. :4000
## NA's :20
Tidying up the dataframe
#turn pilot_round into a factor
pilot$pilot_round = factor(pilot$pilot_round)
#turn transfer into a factor
pilot$transfer = factor(pilot$transfer)
I’ve loaded in all three rounds of pilot experiments that I completed to prepare for my project, but I want to look at the first round only for now. If you have multiple rounds of pilot experiments, you can filter to the one you want to look at with the following code.
#filtering out round 1
library("dplyr")
round1 = filter(pilot, pilot_round == 1)
In my first pilot, I tested two different communities and a flour juice blank (FJ). I serially diluted the communities after each transfer and spotted out the dilutions onto plates that selected for bacterial growth (MRS) and yeast growth (YPD).
Let’s take a quick look at the data from round 1.
library(ggplot2)
#graphing cfu abundance
round1 %>%
filter(!is.na(mrs_cfu_ul)) %>%
ggplot(aes(transfer, mrs_cfu_ul)) +
geom_point(alpha = 0.5) +
facet_wrap(~community) +
labs(x = "Transfer",
y = "CFU Abundance (CFU/uL)") +
theme_classic()
#graphing cfu abundance with bacteria and yeast separated with different scales
round1 %>%
filter(!is.na(ypd_cfu_ul)) %>%
ggplot(aes(transfer, ypd_cfu_ul, color=community)) +
geom_point(alpha = 0.5) +
labs(x = "Transfer",
y = "CFU Abundance (CFU/uL)") +
theme_classic()
I didn’t take a lot of measurements in my first round. By quickly
visualizing the data I did collect, I can see that the graphs are messy
and show a lot of variation. I wouldn’t want my scaled-up experimental
data to look like this, so I’ll take that as a sign that I should make
some changes in the next round.
I’m not convinced of the spot plating as a reliable way to quantify the microbial communities. For the next round, I tried spreading out each dilution on a separate plate as well as spot plating. If spread plating is the more reliable method, there will be less variation between replicates in the same community.
With those changes, let’s look at pilot round 2.
round2 = filter(pilot, pilot_round == 2)
library(ggplot2)
#this data frame was a little messy, so I need to clean it up before I submit this markdown!
#graphing cfu abundance
round2 %>%
filter(!is.na(mrs_cfu_ul)) %>%
ggplot(aes(transfer, mrs_cfu_ul, color=plating_method)) +
geom_point(alpha = 0.5) +
facet_wrap(~community) +
labs(x = "Transfer",
y = "Bacteria CFU Abundance (CFU/uL)") +
theme_classic()
#graphing cfu abundance with bacteria and yeast separated with different scales
round2 %>%
filter(!is.na(ypd_cfu_ul)) %>%
ggplot(aes(transfer, ypd_cfu_ul, color=plating_method)) +
geom_point(alpha = 0.5) +
facet_wrap(~community) +
labs(x = "Transfer",
y = "Yeast CFU Abundance (CFU/uL)") +
theme_classic()
My data frame is messy and needs some additional tidying to fix some
inconsistencies in how I recorded the plating method. When it’s cleaned
up, I’ll compare the standard deviations to see which method results in
more precise data. For now, I can tell you that I didn’t record
measurements for half of the spotted plates because the spots bled
together and were uncountable! Not being able to count my plates is a
huge problem, so this tells me that moving forward, I should stick to
the spread plating method!
Now let’s look at round 3. I switched to spread plating only and I was curious about whether the initial microbial concentration would make a difference in quantifying the communities after transfers. I tried an initial concentration of 2000 CFU/parent and 4000 CFU/parent. I also want to see if pH is distinct enough between communities to use it as an indicator of contamination or extinction.
round3 = filter(pilot, pilot_round == 3)
As always, my data needs a bit of tidying. Then, I’ll graph my data.
#convert initial parent cfu to a factor
round3$initial_parent_cfu = factor(round3$initial_parent_cfu)
#graphing pH
library(ggplot2)
round3 %>%
ggplot(aes(transfer, pH, color=community)) +
geom_point(alpha = 0.7) +
theme_classic()
#graphing cfu abundance
round3 %>%
filter(!is.na(mrs_cfu_ul)) %>%
ggplot(aes(transfer, mrs_cfu_ul, color=initial_parent_cfu)) +
geom_point(alpha = 0.5) +
facet_wrap(~community) +
labs(x = "Transfer",
y = "Bacteria CFU Abundance (CFU/uL)") +
theme_classic()
round3 %>%
filter(!is.na(ypd_cfu_ul)) %>%
ggplot(aes(transfer, ypd_cfu_ul, color=initial_parent_cfu)) +
geom_point(alpha = 0.5) +
facet_wrap(~community) +
labs(x = "Transfer",
y = "Yeast CFU Abundance (CFU/uL)") +
theme_classic()
Finally, some pretty graphs! At a glance, I’d say pH is distinct between communities since I can’t see any overlap. On the other hand, it seems like there’s a lot of overlap between the initial parent concentrations. Of course, I’m not satisfied with that on its own, so I want to use R again to help me see if there’s a significant statistical difference in pH and initial concentration.
Let’s run a few ANOVAs to get a better idea.
#quick analysis
mrs.comm.anova <- aov(mrs_cfu_ul ~ community, data = round3)
summary(mrs.comm.anova)
## Df Sum Sq Mean Sq F value Pr(>F)
## community 1 5.326e+11 5.326e+11 49.24 8.58e-09 ***
## Residuals 46 4.975e+11 1.082e+10
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 24 observations deleted due to missingness
#significant
ypd.comm.anova <- aov(ypd_cfu_ul ~ community, data = round3)
summary(ypd.comm.anova)
## Df Sum Sq Mean Sq F value Pr(>F)
## community 1 3.783e+09 3.783e+09 214.6 <2e-16 ***
## Residuals 46 8.107e+08 1.762e+07
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 24 observations deleted due to missingness
#significant
pH.anova <- aov(pH ~ community, data = round3)
summary(pH.anova)
## Df Sum Sq Mean Sq F value Pr(>F)
## community 2 87.07 43.53 2104 <2e-16 ***
## Residuals 69 1.43 0.02
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#significant
pH.trans.anova <- aov(pH ~ community + transfer, data = round3)
summary(pH.trans.anova)
## Df Sum Sq Mean Sq F value Pr(>F)
## community 2 87.07 43.53 4858.8 < 2e-16 ***
## transfer 3 0.84 0.28 31.1 1.19e-12 ***
## Residuals 66 0.59 0.01
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#significant
mrs.concent.anova <- aov(mrs_cfu_ul ~ initial_parent_cfu, data = round3)
summary(mrs.concent.anova)
## Df Sum Sq Mean Sq F value Pr(>F)
## initial_parent_cfu 1 1.240e+09 1.240e+09 0.055 0.815
## Residuals 46 1.029e+12 2.237e+10
## 24 observations deleted due to missingness
#not significant
ypd.concent.anova <- aov(ypd_cfu_ul ~ initial_parent_cfu, data = round3)
summary(ypd.concent.anova)
## Df Sum Sq Mean Sq F value Pr(>F)
## initial_parent_cfu 1 1.240e+05 124033 0.001 0.972
## Residuals 46 4.593e+09 99849160
## 24 observations deleted due to missingness
#not significant
My ANOVA results show that pH is significantly different between communities, but that the initial parent concentration in a community does not significantly affect the CFU abundance of community members.
To really get a good picture of how the variation in data is explained, I’ll run an lm.
mrs.lm = lm(mrs_cfu_ul ~ community + transfer + initial_parent_cfu,
data = round3)
summary(mrs.lm)
##
## Call:
## lm(formula = mrs_cfu_ul ~ community + transfer + initial_parent_cfu,
## data = round3)
##
## Residuals:
## Min 1Q Median 3Q Max
## -157750 -61250 -6000 52917 213250
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 306750 32440 9.456 5.8e-12 ***
## community2 -210667 26487 -7.954 6.5e-10 ***
## transfer1 40333 37458 1.077 0.2877
## transfer2 -96083 37458 -2.565 0.0140 *
## transfer3 -71083 37458 -1.898 0.0646 .
## initial_parent_cfu4000 -10167 26487 -0.384 0.7030
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 91750 on 42 degrees of freedom
## (24 observations deleted due to missingness)
## Multiple R-squared: 0.6567, Adjusted R-squared: 0.6159
## F-statistic: 16.07 on 5 and 42 DF, p-value: 7.707e-09
ypd.lm = lm(ypd_cfu_ul ~ community + transfer + initial_parent_cfu,
data = round3)
summary(ypd.lm)
##
## Call:
## lm(formula = ypd_cfu_ul ~ community + transfer + initial_parent_cfu,
## data = round3)
##
## Residuals:
## Min 1Q Median 3Q Max
## -8111.2 -2351.0 344.6 1837.7 10755.4
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3688.8 1327.6 2.778 0.008132 **
## community2 17754.2 1084.0 16.378 < 2e-16 ***
## transfer1 -4466.7 1533.0 -2.914 0.005703 **
## transfer2 -5675.0 1533.0 -3.702 0.000617 ***
## transfer3 -4033.3 1533.0 -2.631 0.011854 *
## initial_parent_cfu4000 101.7 1084.0 0.094 0.925724
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3755 on 42 degrees of freedom
## (24 observations deleted due to missingness)
## Multiple R-squared: 0.8711, Adjusted R-squared: 0.8557
## F-statistic: 56.75 on 5 and 42 DF, p-value: < 2.2e-16
pH.lm = lm(pH ~ community + transfer,
data = round3)
summary(pH.lm)
##
## Call:
## lm(formula = pH ~ community + transfer, data = round3)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.177903 -0.064184 -0.009458 0.070694 0.235972
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3.41814 0.02732 125.093 < 2e-16 ***
## community2 0.93787 0.02732 34.323 < 2e-16 ***
## communityFJ 2.65571 0.02732 97.190 < 2e-16 ***
## transfer1 0.08189 0.03155 2.595 0.0116 *
## transfer2 0.07106 0.03155 2.252 0.0277 *
## transfer3 0.28900 0.03155 9.159 2.27e-13 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.09466 on 66 degrees of freedom
## Multiple R-squared: 0.9933, Adjusted R-squared: 0.9928
## F-statistic: 1962 on 5 and 66 DF, p-value: < 2.2e-16
Now I’m confident in the conclusions I drew from looking at my graph. When using R in the experimental design process, it’s best to visualize and then analyze. That way, you can get a good idea of what your data show AND be confident in the methods.
What takeaways did I have after completing 3 rounds of pilots?
No measurable difference between 2000 CFU/parent and 4000 CFU/parent at this transfer frequency. It’s possible that if there were a shorter time period between transfers, the initial parent concentration would affect the cfu abundance, but 3.5 days is enough time for the inoculum to grow to saturate the flour juice. Moving forward, I will use the 4000 CFU/parent simply because it will be easier to accurately dilute the experimental stocks to this concentration (higher volume of stock, more forgiving of pipette error).
High variation between replicates in community 1, community 2 was much more precise. Since the yeast dropped out so early in all replicates, I don’t think Community 1 would be a very interesting community to move forward with unless we wanted to deliberately use it as a comparison with a more stable community.
pH was significantly different between communities and the flour juice and trended upward slightly over time (though I’m unsure if this trend would continue in subsequent transfers). I do think pH is a good “canary” for contamination of the flour juice blank, as well as a decent indicator of changes in community dynamics. I’ll continue to measure it after each transfer.
This process allowed me to come up with a plan of what to do in the event of a contamination. If it happens in the LTEE, I won’t panic and I’ll know exactly what to do!
One replicate in Community 1 (well E) did become contaminated at Transfer 2. I PCR’d and sent off the contaminant for Sanger.The contaminant was L. brevis, so it’s likely that the contamination came from Community 2. The chances of this happening again will decrease when the communities are all plated on separate 96-well plates.
Here is my suggested protocol for handling suspected contamination:
Assume contamination if flour juice is cloudy, pH changes drastically between transfers, or multiple colony morphologies are observed on plated dilutions.
Serially dilute and plate out the contents of the contaminated well.
For plating flour juice, if microbial growth is observed, cease transfers of that 96-well plate.
Select colonies of each morphology (if multiple present) and PCR. Send for Sanger.
Check Sanger results on Blast.
If all the Blast hits for the well are consistent with the expected identity of community member, proceed with transfers of the replicate in that well, but consider sending for genomic sequencing.
If Blast hits do not match the expected identity or are inconsistent, cease transfers of contaminated well. If three or more wells become contaminated, cease transfers of community.
Adding a positive control - wells containing the bacteria and flour juice only.
Freezing a sample of the communities at regular increments.
Reisolating members after several transfers and sending for genome analysis.
Using a few simple codes in R can eliminate a lot of the uncertainty that comes along with planning an experiment.
In the planning phase, here are my recommendations:
Start small. While a small sample size has less statistical power, you aren’t likely to publish the results of a pilot experiment. Have at least three replicates for each experimental group, but there’s no need to go overboard.
Test out multiple methods of measuring your variables. This will allow you to select the method that will give you the most trustworthy data, and you won’t need to waste time taking extra measurements when you scale up.
Take a look at your data in R after each round of pilot experiments. It’s always best to avoid letting your data sit in a spreadsheet for a long time after you’ve completed an experiment, or you might have trouble remembering exactly what you did at each step. Taking good notes in your lab notebook will help avoid this as well.
Start by making a quick graph of your data in R. You don’t need to spend hours perfecting and polishing the figures you generate in R during the design phase, this is just to help you figure out what to do next.
Choose one or two statistical tests that are appropriate for your data and use them as a guide for what variables you may want to change in subsequent experiments.
Don’t be discouraged if your pilot data is messy. Making a mess now and learning from it will help avoid a bigger mess down the road. Remember that science is a process of trial and error, and if you aren’t getting some error, it’s probably because you aren’t trying.
Good luck and happy coding!