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:
Gating strategy and polygon gates are not yet defined.\ Here, the focus is on establishing the gating strategy and manually drawing the polygon gates.
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.
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"))
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")
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;
gn
is the name of the gate we want to use to display data. As no gates are yet defined let alone added to the gating set, we leave that at the default 'root'.
pggId
specifies the name the new polygon gate should be saved under. If left at the default, 'polyGate' will be used.
channels
is specifying which channels we want to be displayed on the x and y axis. Defaults to 'c("FITC.A", "PerCP.A")'.
The lines displayed in the graphic have their origin in the bnd
argument, defining the boundaries to be displayed.
(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)
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.
The 'GateName' is, obviously, the name of the gate. Short names recommended.
'Parent' is the name of the parent-gate, i.e. the gate where the data are coming from. In the first row, this has to be 'root'.
'GateOnX' and GateOnY' define the channels to be used for that gate in the x and y axis.
'GateDefinition' is the name of the polygon gate manually drawn in the previous step.
'extractOn' defines on which channel, i.e. along which axis the fluorescence data should be extracted. Must be either the channel used for the x axis or the channel used for the y axis.
'minRange' and 'maxRange' define the lower and upper limit of the channel where data are extracted. It is recommended that gate definitions do not go beyond these limits. To facilitate this, use the boundary bnd
argument in drawGate()
.
'keepData' is denoting whether the data from this gate are to be kept or not. Set this field to FALSE when the data of this gate are not of interest and it is merely used to prepare the data for an other, a nested gate. There has to be at least one field in 'keepData' set to TRUE.
The column names must not be changed.
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
When more than one gate should be defined in the gating strategy, the circle of
drawing the gate on a single sample,
adding that gate to the gating strategy (file), and
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
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.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.