Skip to content

2. Analysis

Kelsy Cain edited this page Mar 28, 2024 · 1 revision

Created by Kelsy Cain

To start your analysis, open the R script gating.R

Set up the analysis

  1. Load the required R packages.
library(viridis)
library(tidyverse)
library(FCSplankton)
  1. Set the working directory to the path to the project folder. setwd("/PATH_TO_PROJECT/")

  2. 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.

  3. 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.

Get consistent naming of PMT channels across FCS files

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:

  1. 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, then transformation = FALSE.
this_file <- file_list[1]
fcs <- read_influx(this_file, transformation = TRUE)
print(names(fcs))
  1. 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
  1. Create a folder for where to save gating parameters and plots.
system(paste0("mkdir ",folder, "/gating"))

Get set - GATE!

Here are the steps to set individual gates on each .fcs file:

  1. Make sure that gating <- TRUE
  2. 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.

  1. Classify bead particles from an FCS data frame using the gating scheme provided by gates.log.
fcs <- classify_fcs(fcs, gates.log[][1])
  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)
  1. 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)
  1. Apply gates and classify particles from the fcs data frame using the gating schemes saved in gates.log.
fcs <- classify_fcs(fcs, gates.log)
  1. Save gating parameters as an .Rdata file
save(gates.log, file=sub("raw","gating",paste0(this_file, ".RData")))
  1. 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()

Perform aggregate statistics

Here are the steps to perform aggregate statistics on each .fcs file:

  1. 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)))
  1. 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)
  1. 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.