knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
#
library(flowdex)
src <- "https://github.com/bpollner/data/raw/main/flowdex_tutorial/flowdex_tutorial.zip"
td <- tempdir()
# this is downloading and unzipping the tutorial data if not already present:
flowdex::check_download_data(where = td, data_source = src, dsname = "flowdex_tutorial")
#
exp_home <- paste0(td, "/tap_water_home") 
dir.create(exp_home, showWarnings = FALSE)
flowdex::genfs(exp_home) # create the required folder structure in 'tap_water_home'
#
from <- list.files(paste0(td, "/flowdex_tutorial/fcsFiles"), full.names = TRUE)
to <- paste0(td, "/tap_water_home/fcsFiles")
cs <- file.copy(from, to)
#
from <- list.files(paste0(td, "/flowdex_tutorial/dictionary"), full.names = TRUE)
to <- paste0(td, "/tap_water_home/dictionary")
file.copy(from, to)
#
knitr::opts_knit$set(root.dir = paste0(tempdir(), "/tap_water_home"))

There are two basic scenarios when using flowdex that further define the appropriate workflow:

  1. Gating strategy and polygon gates are not yet defined.\ Here, the focus is on establishing the gating strategy and manually drawing the polygon gates.

  2. Gating strategy and polygon gates are already defined.\ Here, the focus is on using the previously established gating strategy to extract fluorescence distributions, visualise them and export them to file. All this (except visualisation) is conveniently done via flowdexit(). Look at Quickstart for an immediate example.


Workflow 1

If not already done, set up the tutorial data as described in Get Started.

The next step after data acquisition is to visualize the raw-counts, to decide which sample to use for drawing the gate, and then to manually draw the polygon gate(s) and safe them for later use.\ Each row in the gating strategy file defines a single gate along with its polygon gate definition. Gates can be nested or 'standalone'.

If not already done, set the working directory to the 'tap_water_home' folder:

td <- tempdir()
setwd(paste0(td, "/tap_water_home"))

Read in fcs Files and Make Gating Set

Before drawing a polygon gate on a single sample, we have to read in some fcs files and produce the gating set.

gsAll <- makeGatingSet()

If everything was left at the default, this will read in all 108 fcs-files contained in the folder 'fcsFiles'.\ For the purpose of this demonstration we will restrict the fcs files to be read in to a lower number by defining a filename pattern: Only fcs files with matching pattern will be read in.

gsRed <- makeGatingSet(patt="GPos_T6_th2")
gsRed

Note the printout of the samples contained in the gating set and of the available channels.\ Only fcs files from the experiment group from day 6 (second third, i.e. 6 beakers) were read in.

We can now use this reduced gating set to visualise the raw fcs data by specifying the channel we want for the x and for the y axis:

plotgates(gsRed, toPdf = FALSE, x="FITC.A", y="PerCP.A")

Manually Draw Polygon Gate

All 6 samples seem to have a good representation of our desired population, the stained bacteria.\ Lets assume we choose beaker 'GPos_T6_th2_b4', which is the 4th sample in this gating set, as sample to manually draw the polygon gate on:

drawGate(gsRed, flf=4, gn="root", pggId="BactStainV1", channels=".")
# point and click to draw a polygon gate around the population of interest;

(All default values can be easily changed in the 'flowdex_settings.R' file located at '\~/desktop/flowdex_SH'.)

If you are not satisfied with the polygon gate definition, you can draw it again while simultaneously displaying an other (the old) gate:

drawGate(gsRed, flf=4, gn="root", pggId="BactStainV1", show="BactStainV1") 
# point and click to draw an improved polygon gate around the population of interest

Polygon gate definitions get automatically saved under the specified name in the folder 'gating'.

You can leave the gate definition of 'BactStainV1' as you were drawing it, or you can copy the gate definition that was originally drawn for this project:

from <- paste0(td, "/flowdex_tutorial/gating/BactStainV1")
to <- paste0(td, "/tap_water_home/gating")
file.copy(from, to, overwrite = TRUE)

And now visualize the gate, this time without using the locator:

drawGate(gsRed, flf=4, gn="root", show="BactStainV1", useLoc = FALSE) 

Write Gating Strategy

For each gate that you want to apply to your gating set, there has to be one row in the gating strategy file. Now copy the template for the gating strategy file into the experiment home folder:

from <- paste0(td, "/tap_water_home/templates/gateStrat.xlsx")
to <- paste0(td, "/tap_water_home/gating")
file.copy(from, to)

Open the copied file. For every gate, i.e. every row, all 8 fields have to filled in.

For the example at hand, fill in the gating strategy file as follows:

GateName: DNA+
Parent: foot
GateOnX: FITC.A
GateOnY: PerCP.A
GateDefinition: BactStainV1
extractOn: FITC.A
minRange: 1250
maxRange: 4000
keepData: TRUE

Or copy the already filled out gating strategy file defining the gate 'DNA+' with its polygon gate definition 'BactStainV1' from the tutorial folder:

from <- paste0(td, "/flowdex_tutorial/gating/gateStrat.xlsx")
to <- paste0(td, "/tap_water_home/gating")
file.copy(from, to, overwrite=TRUE)

Now everything should be ready to apply the gating strategy to the gating set:

gsRed_ga <- addGates(gsRed) # gsRed was created above
flowWorkspace::plot(gsRed_ga) # to view the gate hierarchy 
plotgates(gsRed_ga, toPdf = FALSE) # to view the gated data

Draw Gate, Add to Gating Strategy, Add Gate to Gating Set -- and Repeat

When more than one gate should be defined in the gating strategy, the circle of

  1. drawing the gate on a single sample,

  2. adding that gate to the gating strategy (file), and

  3. adding that gate to the gating set (object)

is repeated for every gate that should be applied to the fcs data.

In the next iteration, i.e. when for example a second gate within the data from the first gate should be defined, one would call:

drawGate(gsRed_ga, flf=5, gn="DNA+", pggId="pg2", channels = c("FITC.A", "SSC.A"))
# point and click to draw a polygon gate around the population of interest

Then go modify the gating strategy file (extracting along SSC is probably not meaningful, but just to show that it can be done):

GateName: FooGate
Parent: DNA+
GateOnX: FITC.A
GateOnY: SSC.A
GateDefinition: pg2
extractOn: SSC.A
minRange: 0
maxRange: 4000
keepData: TRUE
gsRed_ga <- addGates(gsRed_ga) # add the gate to the gating set 

Repeat these three steps to define any set of possibly nested gates. You can always view the gating hierarchy and the gated data via:

flowWorkspace::plot(gsRed_ga) # to view the gating hierarchy
plotgates(gsRed_ga, toPdf = F) # to view the gated data

Use a Ready-Made Example for Nested Gates

Copy a ready made example for a gating strategy holding more than one gate from the tutorial folder in the experiment home folder

from <- list.files(paste0(td, "/flowdex_tutorial/gating"), full.names = TRUE)
to <- paste0(td, "/tap_water_home/gating")
file.copy(from, to, overwrite=TRUE) # this might overwrite the polygon gate definition you created above

and apply it to a gating set. For ease of demonstration, we will again use a subset of the fcs files as above. Reading in the fcs files and adding the gates can be conveniently done using makeAddGatingSet():

# we are not using the default name for gating strategy any more:
gsRed2 <- makeAddGatingSet(patt="GPos_T6_th2", gateStrat = "gateStrat_2")
flowWorkspace::plot(gsRed2) # to view the gating hierarchy
plotgates(gsRed2, toPdf = FALSE) # to view the gated data

Note that only those gated data get displayed in plotgates() where the field 'keepData' in the gating strategy file is set to 'TRUE'.\ (Admittedly, the practical value of this gating hierarchy is rather non-existent, but it is merely for demonstrating the setup of nested gates...)

Continue to Workflow 2.



bpollner/flowdex documentation built on March 31, 2022, 3:21 a.m.