Edited: 20 October 2020. Access to these files have changed and Bioconductor has changed too.
Today, I want to show how this can be used to open a file containing some flow cytometry data and how to generate a plot with the data.
This is the plot, I am going to generate:
It uses the Bioconductor package flowCore. The how-to manual is here and the full reference manual is here. I'm just showing how to open and plot one file which is just a starting point for flow cytometry data. There is a longer example with more plots here.
A full flow cytometry work flow will allow opening multiple files, normalising and detailed statistics. There is lots of other packages for flow cytometry on Bioconductor.
I found this interesting: bioinformatics.ca, the home to all things bioinformatics in Canada. They ran a course in 2013 entitled Flow Cytometry Data Analysis using R (2013) and the material is available to view for free.
Here is the script:
### START
## ----download_from_Bioconductor-----------------------------------
# install BiocManager
# install.packages("BiocManager")
# BiocManager::install("flowCore")
library("flowCore")
library(ggplot2)
link <- "https://github.com/brennanpincardiff/R4Biochemists201/blob/master/data/cfse_data_20111028_Bay_d7/A01.fcs?raw=true"
download.file(url=link, destfile="file.fcs", mode="wb")
data <- flowCore::read.FCS("file.fcs", alter.names = TRUE)
#with colours indicating density
colfunc <- colorRampPalette(c("white", "lightblue", "green", "yellow", "red"))
# this colour palette can be changed to your taste
vals <- as.data.frame(exprs(data))
ggplot(vals, aes(x=FSC.A, y=SSC.A)) +
ylim(0, 500000) +
xlim(0,5000000) +
stat_density2d(geom="tile", aes(fill = ..density..), contour = FALSE) +
scale_fill_gradientn(colours=colfunc(400)) + # gives the colour plot
geom_density2d(colour="black", bins=5) # draws the lines inside
# exclude debris using the filter package
vals_f <- dplyr::filter(vals, FSC.A>1000000)
# we still have 25,499 events.
# repeat the plot
ggplot(vals_f, aes(x=FSC.A, y=SSC.A)) +
ylim(0, 500000) +
xlim(0,5000000) +
stat_density2d(geom="tile", aes(fill = ..density..), contour = FALSE) +
scale_fill_gradientn(colours=colfunc(400)) + # gives the colour plot
geom_density2d(colour="black", bins=5) # draws the lines inside
=== END ===
Hi, i cant create ggplot because i've got error on that: "ggplot2 doesn't know how to deal with data of class flowFrame"
ReplyDeletemaybe missing library?
ggplot2(fcs1, aes(x=FSC.A, y=SSC.A)) +
ylim(0, 5000) +
xlim(0, 5000) +
stat_density2d(geom="tile", aes(fill = ..density..), contour = FALSE) +
scale_fill_gradientn(colours=colfunc(400)) + # gives the colour plot
geom_density2d(colour="black", bins=5) # draws the lines inside
Thanks for your comment MacGabe.
ReplyDeleteTo make your code work, you need to extract the FCS expression values and put them into a data frame.
The code:
n <- as.data.frame(exprs(ntl))
does this.
It's the exprs() function that is the key. Then the as.data.frame() function works to create a data frame that the ggplot2() function can then use.
Does that make sense?
Have a go and see if it works, please.
Best wishes,
Paul