-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgating.R
More file actions
executable file
·130 lines (101 loc) · 5.17 KB
/
gating.R
File metadata and controls
executable file
·130 lines (101 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#######################
#gating.R
#
#started: 10/23/2016
#
#author1: D Gresham
######################
###########################################################################################################################
#This script is intended to read in .fcs files and perform manual gating for i) single cells, ii) debris and iii) fluorescence
#
#Gating is performed with untransformed data
#
#Individual gates are saved in a file gates.Rdata for use with the Gresham Lab Flow Cytometry Analysis.Rmd pipeline
###########################################################################################################################
##To be run the first time if packages are not installed.
#source("http://bioconductor.org/biocLite.R")
#biocLite("flowViz")
#biocLite("flowCore")
#Load libraries
library(flowCore)
library(flowViz)
#Read in the data
flowData <- read.flowSet(path = ".", pattern=".fcs", alter.names=TRUE)
#Check how many cells were counted in each fcs file
fsApply(flowData, each_col, length)
##############################
#1. Generate gate for singlet cells
#this gate is defined on the basis of the relationship between forward scatter height and area
plot(flowData[[1]], c('FSC.H','FSC.A'), xlim=c(0,3e6), ylim=c(0,3e6),smooth=F)
Agate <- locator(10)
gm.1 <- matrix(,10,2)
colnames(gm.1) <- c('FSC.H','FSC.A')
gm.1[,1] <- Agate$x
gm.1[,2] <- Agate$y
pg.singlets <- polygonGate(filterId="singlets",.gate=gm.1)
#test that the singlet gate looks reasonable for the sample
xyplot(FSC.A~FSC.H,data=flowData[[1]],xlim=c(0,3e6), ylim=c(0,3e6), smooth=F, filter=pg.singlets, outline=T)
#test that the gate looks reasonable over all the samples
xyplot(FSC.A~FSC.H, data=flowData, xlim=c(0,3e6), ylim=c(0,3e6),
smooth=F, filter=pg.singlets, outline=T, displayFilter=TRUE,
stat=T, pos=0.5, abs=T)
##############################
#2. Generate Gate for debris based on forward scatter and side scatter
plot(flowData[[1]], c('FSC.A','SSC.A'), xlim=c(0,3e6), ylim=c(0,3e5), smooth=F)
Bgate <- locator(10)
gm.2 <- matrix(,10,2)
colnames(gm.2) <- c('FSC.A','SSC.A')
gm.2[,1] <- Bgate$x
gm.2[,2] <- Bgate$y
pg.nondebris <- polygonGate(filterId="nonDebris",.gate=gm.2)
#test that the debris gate looks reasonable for the sample
xyplot(SSC.A ~ FSC.A, data=flowData[[1]], displayFilter=TRUE, xlim=c(0,3e6), ylim=c(0,3e6), filter=pg.nondebris, smooth=F, xbin=1024, stat=T, pos=0.5, abs=T)
#test that the decris gate looks reasonable over all the samples
xyplot(SSC.A ~ FSC.A, data=flowData, displayFilter=TRUE, xlim=c(0,3e5), ylim=c(0,3e6), filter=pg.nondebris, smooth=F, xbin=1024, stat=T, pos=0.5, abs=T)
##############################
#3. Generate Gate for non-fluorescencing cells
plot(flowData[[1]], c('FSC.A','FL1.A'), xlim=c(0,5e6), ylim=c(0,5e4), smooth=F)
Cgate <- locator(10)
gm.3 <- matrix(,10,2)
colnames(gm.3) <- c('FSC.A','FL1.A')
gm.3[,1] <- Cgate$x
gm.3[,2] <- Cgate$y
pg.nongfp <- polygonGate(filterId="GFPneg",.gate=gm.3)
#test that the non-fluorescing gate looks reasonable for the sample
xyplot(FL1.A~FSC.A,data=flowData[[1]], xlim=c(0,5e6), ylim=c(0,5e4), filter=pg.nongfp, smooth=F, xbin=1024, stat=T, pos=0.5, abs=T)
#test that the non-fluorescing gate looks reasonable over all the samples
xyplot(FL1.A~FSC.A,data=flowData, xlim=c(0,5e6), ylim=c(0,5e4), filter=pg.nongfp, smooth=F, xbin=1024, stat=T, pos=0.5, abs=T)
##############################
#4. Generate Gate for fluorescencing cells
plot(flowData[[2]], c('FSC.A','FL1.A'), xlim=c(0,5e6), ylim=c(0,5e4), smooth=F)
Dgate <- locator(10)
gm.4 <- matrix(,10,2)
colnames(gm.4) <- c('FSC.A','FL1.A')
gm.4[,1] <- Dgate$x
gm.4[,2] <- Dgate$y
pg.gfp <- polygonGate(filterId="GFPpos",.gate=gm.4)
#test that the fluorescing gate looks reasonable for the sample
xyplot(FL1.A~FSC.A,data=flowData[[1]], xlim=c(0,5e6), ylim=c(0,5e4), filter=pg.gfp, smooth=F, xbin=1024, stat=T, pos=0.5, abs=T)
#test that the fluorescing gate looks reasonable over all the samples
xyplot(FL1.A~FSC.A,data=flowData, xlim=c(0,5e6), ylim=c(0,5e4), filter=pg.gfp, smooth=F, xbin=1024, stat=T, pos=0.5, abs=T)
################################
#5. Generate Gate for high fluorescencing cells
plot(flowData[[6]], c('FSC.A','FL1.A'), xlim=c(0,5e6), ylim=c(0,5e4), smooth=F)
Egate <- locator(10)
gm.5 <- matrix(,10,2)
colnames(gm.5) <- c('FSC.A','FL1.A')
gm.5[,1] <- Egate$x
gm.5[,2] <- Egate$y
pg.hi.gfp <- polygonGate(filterId="hiGFPpos",.gate=gm.5)
#test that the high fluorescing gate looks reasonable for the sample
xyplot(FL1.A~FSC.A,data=flowData[[6]], xlim=c(0,5e6), ylim=c(0,5e4), filter=pg.hi.gfp, smooth=F, xbin=1024, stat=T, pos=0.5, abs=T)
#test that the high fluorescing gate looks reasonable over all the samples
xyplot(FL1.A~FSC.A,data=flowData, xlim=c(0,5e6), ylim=c(0,5e4), filter=pg.hi.gfp, smooth=F, xbin=1024, stat=T, pos=0.5, abs=T)
################################
##In some cases a rectangle gate may be desired, which can be generated as follows
#rectGate <- rectangleGate("FSC.A"= c(0,3.5e6),"SSC.A" = c(0,4.5e6))
#xyplot(SSC.A~FSC.A,data=flowData, xlim=c(0,5e6), ylim=c(0,5e4), filter=rectGate, smooth=F, xbin=1024, stat=T, pos=0.5, abs=T)
################################
#6. Save the gate information to an R data file
rm(list=c("flowData"))
save.image(file="gates.Rdata")