-
Notifications
You must be signed in to change notification settings - Fork 0
2. Analysis
Created by Kelsy Cain
To start your analysis, open the R script gating.R
- Load the required R packages.
library(viridis)
library(tidyverse)
library(FCSplankton)
-
Set the working directory to the path to the project folder.
setwd("/PATH_TO_PROJECT/")
-
Select whether the samples are stained or unstained
unstained <- TRUE # or FALSE
. This changes the gating pipeline to pull raw data and save data in/unstained
or/stained
folders. -
Load the list of
.fcs
files in the current working directory.
if(unstained){folder <- "unstained"
}else{folder <- "stained"}
file_list <- list.files(paste0(folder,"/raw"), pattern = ".fcs$", full.names=T)
Note that you can return the list of files within the current working folder by entering file_list
into the R console.
The name of each PMT channel is user-defined, so the first step is to make sure that the names of the PMT channels are consistent across samples. To do so, follow these steps:
- Read one file to create the data frame with the raw data. If the raw data is log-amplified, make sure
transformation = TRUE
. If data is NOT log-amplified, thentransformation = FALSE
.
this_file <- file_list[1]
fcs <- read_influx(this_file, transformation = TRUE)
print(names(fcs))
- Select the PMT channels for forward scatter (fsc), red fluorescence (692), orange fluorescence (580), and green fluorescence (530).
id <- c(2,3,4,5) ## replace number by column indices of FSC, 692, 580 and 530 respectively
names(fcs)[id]
names_pmt <- names(fcs)[id] # original names of FSC, 692, 580 and 530 respectively
- Create a folder for where to save gating parameters and plots.
system(paste0("mkdir ",folder, "/gating"))
Here are the steps to set individual gates on each .fcs
file:
- Make sure that
gating <- TRUE
- Gate the
beads
. This is necessary to normalize light scatter and fluorescence values to the mean bead values and thereby facilitate the gating of microbial populations.
gates.log <- set_gating_params(fcs, "beads", "scatter", "orange")
This will open up a quartz window displaying the cytogram and which population you are gating. To gate, simply click around the population of interest in the quartz window creating a polygon shape. To close a gate, simultaneously press control/command and click.
- Classify bead particles from an FCS data frame using the gating scheme provided by
gates.log
.
fcs <- classify_fcs(fcs, gates.log[][1])
- Normalize light scatter and fluorescence values to the mean bead values.
beads <- fcs[which(fcs$pop == "beads"),]
fcs$norm.scatter <- fcs$scatter / mean(beads$scatter)
fcs$norm.orange <- fcs$orange / mean(beads$orange)
fcs$norm.red <- fcs$red / mean(beads$red)
fcs$norm.green <- fcs$green / mean(beads$green)
- Gate populations of interest. Change channels as needed to best gate the populations of interest. Save the gating parameters in
gates.log
. NOTE: Do not change population names. Simply comment out populations you do not want wish to gate.
gates.log <- set_gating_params(fcs, "synecho", "norm.scatter", "norm.orange", gates.log)
gates.log <- set_gating_params(fcs, "prochloro", "norm.scatter", "norm.red", gates.log)
gates.log <- set_gating_params(fcs, "picoeuk", "norm.scatter", "norm.red", gates.log)
- Apply gates and classify particles from the fcs data frame using the gating schemes saved in gates.log.
fcs <- classify_fcs(fcs, gates.log)
- Save gating parameters as an
.Rdata file
save(gates.log, file=sub("raw","gating",paste0(this_file, ".RData")))
- Save cytogram plots of the gated populations. Change the channels plotted as needed depending on the channels used to gate.
png(sub("raw","gating",paste0(this_file, ".png")),width=12, height=12, unit="in", res=200)
par(mfrow=c(2,2), pty="s", cex=1.2, oma=c(0,0,1,0), mar=c(5,5,1,1))
plot_vct_cytogram(fcs, "norm.scatter","norm.red", ylab="red\n (normalized to beads)", xlab="scatter\n (normalized to beads)")
plot_vct_cytogram(fcs, "norm.scatter","norm.orange", ylab="orange\n (normalized to beads)", xlab="scatter\n (normalized to beads)")
plot_vct_cytogram(fcs, "norm.scatter","norm.green", ylab="green\n (normalized to beads)", xlab="scatter\n (normalized to beads)")
dev.off()
Here are the steps to perform aggregate statistics on each .fcs
file:
- Calculate the count and mean of normalized forward scatter and fluorescence values:
stat_table <- NULL
for(population in unique(fcs$pop)){
p <- subset(fcs, pop == population)
count <- nrow(p)
if(count == 0) {
scatter <- 0
red <- 0
}else{
scatter <- round(mean(p$norm.scatter),6)
red <- round(mean(p$norm.red),6)
orange <- round(mean(p$norm.orange),6)
if(folder == "stained"){
green <- round(mean(p$norm.green),6)
}else{green <- NA}
}
var <- cbind(population,count,scatter,red,orange,green)
stat_table <- rbind(stat_table, var)
}
# Create an entry in a summary table to store the aggregate statistics data for this_file.
table <- data.frame(cbind(stat_table, file=basename(this_file)))
- At the end of all files, you will get
summary_table
that has the aggregate statistics for each file.
summary_table <- rbind(summary_table, table)
- Save the summary table as a
.csv
to the current working folder.
write_csv(summary_table,path=paste0(folder, "/summary.csv"))
The saved gating parameters will later be used to generate particle size distribution (PSD) data and then curated into a single excel spreadsheet formatted specifically for Simons CMAP ingestion.
Next step is generating the PSD. Code available here.
Authors: Kelsy Cain, François Ribalet
Maintainer: Francois Ribalet ribalet@uw.edu