-
Notifications
You must be signed in to change notification settings - Fork 1
/
CytobankGraphs.R
349 lines (267 loc) · 11.8 KB
/
CytobankGraphs.R
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#########################################################
### Installing and loading required packages
#########################################################
if (!require("svDialogs")) {
install.packages("svDialogs", dependencies = TRUE)
library(svDialogs)
}
if (!require("flowCore")) {
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager")
BiocManager::install("flowCore")
}
if (!require("tidyverse")) {
install.packages("tidyverse", dependencies = TRUE)
library(tidyverse)
}
if (!require("reshape2")) {
install.packages("reshape2", dependencies = TRUE)
library(reshape2)
}
if (!require("ggplot2")) {
install.packages("ggplot2", dependencies = TRUE)
library(ggplot2)
}
if (!require("tcltk")) {
install.packages("tcltk", dependencies = TRUE)
library(tcltk)
}
#########################################################
### Script starts here
#########################################################
# Clear environment
rm(list = ls(all = TRUE))
# Data Import from file chosen by user
#library(svDialogs) # Moved to top
# Get user input for file
testfile <- dlg_open()
# Convert to string value
testfile <- capture.output(testfile)[7]
{
if ((testfile)=="character(0)")
stop("File input cancelled")
#Remove invalid characters from file input location
testfile <- gsub("[\"]","",testfile)
testfile<-substring (testfile,5)
#Set file and directory
filename <- basename (testfile)
dir <- dirname (testfile)
# Set working directory accoding to file chosen
setwd(dir)
#library(flowCore) #Moved to top
# this read.FCS() function imports the flow data:
raw_fcs<-read.FCS(filename, alter.names = TRUE)
# Preparation work for arcsinh transform (columns is also used later for naming changes)
# Create list of parameters
columns<-colnames(raw_fcs)
# Remove "Time" column to avoid it being transformed
columns<-setdiff(columns,"Time")
# Remove "Cell_Length" and Gaussians column to avoid it being transformed
columns<-setdiff(columns,"Event_length")
columns<-setdiff(columns,"Cell_length")
columns<-setdiff(columns,"Center")
columns<-setdiff(columns,"Offset")
columns<-setdiff(columns,"Width")
columns<-setdiff(columns,"Residual")
## Remove FSC and SSC
removefscssc<-grep("FSC|SSC",columns,value=TRUE)
columns<-columns[! columns %in% removefscssc]
# Read data into a data frame
FCSDATA <- as.data.frame(exprs(raw_fcs))
############ Optional Data Transform section
#Remove comments from code lines to transform using logicle
## Automatically estimate the logicle transformation based on the data
#lgcl <- estimateLogicle(raw_fcs, channels = c(columns))
## transform parameters using the estimated logicle transformation
#raw_fcs_trans <- transform(raw_fcs, lgcl)
# Load into data frame
#FCSDATA <- as.data.frame(exprs(raw_fcs_trans))
########### End of optional Data Transform section
#Remove unnecessary parameter text
names(FCSDATA)[-1] <- sub("Di", "", names(FCSDATA)[-1])
names(FCSDATA)[-1] <- sub("Dd", "", names(FCSDATA)[-1])
# Create list of channel / parameter descriptions
params<-parameters(raw_fcs)[["desc"]]
# Replace parameters with descriptions, keeping things like Time, Event Length unchanged
colnames(FCSDATA)[!is.na(params)] <- na.omit(params)
# Determine whether data is CyTOF or Flow by presence of FSC
# isflow will be 0 for a CyTOF or greater than 1 if flow
isflow <-sum(grep("FSC",colnames(FCSDATA)))
# Determine whether data is pre CyTOF 3 (Helios) by presence of "Cell_length", rather than "Event_length"
isCyTOF2 <-sum(grep("Cell_length",colnames(FCSDATA)))
## Remove Time, Event_Length & Gaussian Parameters
removecolumns <- c("Event_length", "Center", "Offset", "Width", "Residual", "Cell_length")
FCSDATA <- FCSDATA[,!(names(FCSDATA) %in% removecolumns)]
## Remove FSC and SSC
# library(tidyverse) # Moved to top
FCSDATA <- FCSDATA %>% select(-contains("FSC"))
FCSDATA <- FCSDATA %>% select(-contains("SSC"))
# Get number of cell events (based on "193" - i.e. Iridium)
if(isflow==0){
# Note that this only works correctly because "Time" has been removed by a previous step - otherwise the position would be wrong.
irpos <- grep("193",columns)
cellevents <- sum(FCSDATA[,irpos]!=0)
kcellevents <- round(cellevents/1000,0)
}
#For converting FCS time to mins - flow uses 10ms units, CyTOF uses ms
if(isflow>0){
div = (60*100)
}else{
div = (60*1000)
}
# Find total acquisition time
maxtime<-round(max(FCSDATA$Time)/div,1)
# Now that we have the total time, we can calculate the number of cell events/sec
if(isflow==0){
eventspersec <- round(cellevents/maxtime/60,0)
}
# Create number formatted list of intensity values and event counts
Medianintensitylist<-c(format(c(round(apply(FCSDATA,2,FUN=median)),1),big.mark=",",trim=TRUE))
Meanintensitylist <- c(format(c(round(colMeans(FCSDATA)),1),big.mark = ",",trim=TRUE))
EventSecList <- c(format(c(round((colSums(FCSDATA !=0)/(maxtime*60)),0),trim=TRUE)))
# Remove the last row that is added by format
Meanintensitylist<-Meanintensitylist[-length(Meanintensitylist)]
Medianintensitylist<-Medianintensitylist[-length(Medianintensitylist)]
EventSecList<-EventSecList[-length(EventSecList)]
# Create data frame for labels to print mean intensity on plots
datalabels <- data.frame(
Meanintensity=c(Meanintensitylist),
Medianintensity=c(Medianintensitylist),
parameter = c(colnames(FCSDATA)),
EventsPerSec = c(EventSecList)
)
#Calculate size of dataset
DataSizeM <- (ncol(FCSDATA)*nrow(FCSDATA))/1000000
#Subsample if dataset is large
if (DataSizeM>3){
#using random 10% of original rows
#FCSDATA <- FCSDATA[sample(nrow(FCSDATA),nrow(FCSDATA)/10),]
#OR
#Subsample using a number of random rows, where the number is defined by numrows
numrows <- 10000
FCSDATA <- FCSDATA[sample(nrow(FCSDATA),numrows),]
}
# Get position of Time in dataset (to handle Flow and CyTOF)
TimePos <- which(colnames(FCSDATA)=="Time")
# Add a blank to the columns list to match its length to that of FCSDATA (i.e. the time row)
columns <- append(columns,"Time",after=TimePos-1)
# Add back the original marker names
datalabels[,"OrigMarkers"] <- columns
# Remove Di / Dd
datalabels$OrigMarkers <- sub("Di", "", datalabels$OrigMarkers)
datalabels$OrigMarkers <- sub("Dd", "", datalabels$OrigMarkers)
# This is needed for pre-Helios data to ensure we don't mess with the parameter names
if (isCyTOF2>1){
# Remove other symbols
datalabels$OrigMarkers <- gsub("[[:punct:]]", "", datalabels$OrigMarkers)
# Create a function to extract the last n characters from a string
substrRight <- function(x, n){
substr(x, nchar(x)-n+1, nchar(x))
}
# Extract only last 5 characters (i.e the element and mass) - this is clumsy and doesn't work well for flow data, which may have longer names for markers
# But I can't figure out a way to remove duplicate text
datalabels$OrigMarkers<-substrRight(datalabels$OrigMarkers,5)
# Compare columns and keep only original markers if they are different
datalabels$OrigMarkers<-ifelse(datalabels$parameter==datalabels$OrigMarkers,"",paste("/",datalabels$OrigMarkers))
# Replace parameters column with orignal marker names / parameters
datalabels[,"parameter"]<-paste(datalabels$parameter,datalabels$OrigMarkers)
} #End of flow data name comparison / CyTOF paramater rename loop
# Remove the OrigMarkers column as it's no longer needed
datalabels<-datalabels[,-5]
# Make sure the FCSDATA matches the datalabels
colnames(FCSDATA)<-datalabels$parameter
#Trim the trailing whitespace added by paste
colnames(FCSDATA)<-trimws(colnames(FCSDATA),"r")
datalabels$parameter<-trimws(datalabels$parameter,"r")
# Remove Time from labels
datalabels <- datalabels[!(rownames(datalabels) %in% "Time"),]
# Change rownames to numeric
rownames(datalabels) <- 1:nrow(datalabels)
# Change parameters to factors to control facet order
datalabels$parameter<-as.factor(datalabels$parameter)
# Ask user which parameters to plot
markerlist<-tk_select.list(colnames(FCSDATA[-TimePos]), multiple=TRUE,title="Select Markers to plot. Hit cancel to use all.")
# If user cancels dialog box, use all markers.
if(length(markerlist)==0 ){
markerlist <- colnames(FCSDATA[-TimePos])
}
# Create list of positions of the user-selected markers
# V and $ and gsub are used to ensure grep only matches exact / full names
marker_cols<-NULL
for (m in markerlist){
m <- paste ("^",m,"$")
m <- gsub("\\s","",m)
marker_cols <- c(marker_cols,grep(m,colnames(FCSDATA)))
}
# Remove markers that are not selected but keep first column (time)
FCSDATA <- FCSDATA[,c(TimePos,marker_cols)]
# Do the same for datalabels, which are stored in rows
marker_rows<-NULL
for (m in markerlist){
m <- paste ("^",m,"$")
m <- gsub("\\s","",m)
marker_rows<-c(marker_rows,which(grepl(m,datalabels$parameter)))
}
datalabels <- datalabels[marker_rows,]
# Melt the data into a continuous table, keeping Time for all values.
# This allows plotting all parameters using facet_wrap in the next section
# library(reshape2) # Moved to op
fcsmelted <- melt(FCSDATA, id.var="Time", value.name = "intensity", variable.name="parameter")
#use ggplot2 to draw dot plot
# library(ggplot2) # Moved to top
# Create colour scale for plot
# The extra "black" is needed to make the colour scale appear decent - otherwise it doesn't show
colfunc <- colorRampPalette(c("black", "black","black", "black", "black", "black", "black", "black",
"purple4", "purple4",
"red", "yellow"))
## Plot x as Time and Y as intensity
ggplot(fcsmelted, aes(x=Time/div, y=intensity)) +
# Only label 0 and max on X Axis
scale_x_continuous(breaks=seq(0,maxtime,maxtime)) +
# Plot all points
geom_point(shape=".",alpha=0.5)+
# Fill with transparent colour fill using density stats
# ndensity scales each graph to its own min/max values
stat_density2d(geom="raster", aes(fill=..ndensity.., alpha = ..ndensity..), contour = FALSE) +
# Produces a colour scale based on the colours in the colfunc list
scale_fill_gradientn(colours=colfunc(128)) +
# Contour lines if desired
# geom_density2d(colour="black", bins=3) +
# Repeat for all parameters...
facet_wrap("parameter") +
# ...and allow each to be on their own Y axis scale
# Note that free scales increases the processing time substantially
#facet_wrap(scales="free")+
# Force Y axis to start at zero
#ylim(0,NA) +
# And scale y to log, displaying numbers rather than notation
#scale_y_log10(labels=scales::comma) +
# Use scientific notation
scale_y_log10(labels=scales::trans_format('log10',scales::math_format(10^.x))) +
# Zoom plot to only the values of interest
coord_cartesian(ylim=c(1, max(fcsmelted$intensity)))+
# Hide Y axis values if desired
#theme(axis.text.y = element_blank(), axis.ticks = element_blank()) +
# Hide legend
theme(legend.position = "none") +
# Hide Y axis label
ylab(NULL) +
# Change X axis label
xlab("Time (min)")+
ggtitle(filename) +
# Add mean intensity values from previously calculated data table
geom_label(data=datalabels,
colour="black",
fontface="bold",
size=2.5,
alpha=0.5,
mapping=aes(maxtime/2,(max(fcsmelted$intensity))/1000,
label=paste("Mean =",Meanintensity,", Median =",Medianintensity,", Events/sec =",EventsPerSec)))
} # End of file cancel loop
# Create a pop-up with the cell (Ir) Events and rate.
if ((testfile)=="character(0)" || isflow>0 || length(cellevents)==0){
#stop("No cell events detected")
}else{
message <- paste(kcellevents,"thousand cell events","and",eventspersec,"events/sec")
dlg_message(message)
}