A site to help Biochemists learn R.

Starting points

Wednesday, 8 July 2015

Opening and plotting some flow cytometry data...

Bioconductor is a very valuable resource for biochemists, biologists and bioinformaticians using R. It contains over 1000 software packages for the opening, analysing and visualising biomedical and biological data. 

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. 

To use the script, you will have to download this sample file:
Put it somewhere sensible and set the working directory (setwd()) of R so that the read.FCS() function can find it.

Here is the script:

#load the package flowCore which allows the import of FCS files. 
source("http://bioconductor.org/biocLite.R")
biocLite("flowCore")
library(flowCore)

# import the data 
# this read.FCS() function imports the flow data
setwd() # add the appropriate destination for your directory. 
ntl<-read.FCS("NTLA01.fcs", alter.names = TRUE)
n <- as.data.frame(exprs(ntl))  # extract expression values and put into a data frame

#use ggplot2 to draw dot plot
library(ggplot2)

#with colours indicating density
colfunc <- colorRampPalette(c("white", "lightblue", "green", "yellow", "red"))
# this colour palette can be changed to your taste 

ggplot(n, 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
 


2 comments:

  1. Hi, i cant create ggplot because i've got error on that: "ggplot2 doesn't know how to deal with data of class flowFrame"
    maybe 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

    ReplyDelete
  2. Thanks for your comment MacGabe.
    To 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

    ReplyDelete

Comments and suggestions are welcome.