-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBatchFlowTypeDataMerge.R
195 lines (147 loc) · 7.13 KB
/
BatchFlowTypeDataMerge.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
#!/usr/bin/env Rscript
require(docopt)
require(methods)
"
Usage:
BatchFlowType.R (-h | --help | --version)
BatchFlowType.R DIR
Description: This script applies the flowType algorthim to high parameter cytometry data
Options:
--version Show the current version.
Arguments:
DIR Provide directory for cytools.args.Rdata to be found, automatically generated by invoking cyttools.R
" -> doc
args <- docopt(doc)
ARGS_DIR <- args$DIR
cat("\nLoading arguments from", ARGS_DIR, "\n")
load(paste(ARGS_DIR, "cyttools.args.Rdata", sep = ""))
RESULTS_DIR <- args$OUT
source("cyttoolsFunctions.R")
dir <- args$DIR # grabs directory from initial cyttools call
file <- list.files(dir, pattern='.fcs$', full=TRUE) # captures all FCS files in the directory
targets <- read.delim(args$PANEL)
colsToCheck <- c("Ignore", "TransformCofactor", "Lineage", "Functional", "NRS")
if(checkDesignCols(targets, colsToCheck)){
missingCols <- colsToCheck[which(colsToCheck %in% colnames(targets) == F)]
cat("\n\nERROR: PANEL file does not include required columns.
\n\nMissing Columns:", missingCols,
"\n\nPlease run cyttools.R --makePanelBlank and cyttools.R --computeNRS to generate compatible panel file.\n\nStopping cyttools.R\n\n")
q()
}
lineage_markers <- targets$name[targets$Lineage == 1]
functional_markers <- targets$name[targets$Functional == 1]
if(args$transform == T){
flowSet.trans <- read.flowSet.transVS(targets, file)
}else{
flowSet.trans <- read.flowSet(file)
}
# order the markers using NRS, dropping markers set to "1" in Ignore column of panel design
lineage_markers_ord <- targets$name
lineage_markers_ord <- lineage_markers_ord[lineage_markers_ord %in% targets$name[which(targets$Ignore == 0)]]
lineage_markers_ord <- lineage_markers_ord[order(targets$NRS[which(targets$name %in% lineage_markers_ord)], decreasing = T)]
if(length(lineage_markers_ord) > 12){
lineage_markers_ord <- lineage_markers_ord[1:12]
}
colsToUse <- which(targets$name %in% lineage_markers_ord == T)
dir <- args$BATCH_DIR # grabs directory from initial cyttools call
fileList <- list.files(dir, pattern='\\_[0-9]+\\.Rdata$', full=TRUE) # captures all FCS files in the directory
panelDesign <- targets
# GRRRRRR
ResListAll <- list()
for ( i in 1:length(fileList)){
fffile <- fileList[i]
load(fffile)
ResListAll <- c(ResListAll, ResList)
}
ResList <- ResListAll
rm(ResListAll)
fileOrder <- basename(fileList)
fileNumbers <- gsub(".*\\_|\\.Rdata", "", fileOrder) %>% as.numeric()
ResList <- ResList[order(fileNumbers, decreasing = F)]
names(ResList) <- sampleNames(phenoData(flowSet.trans))
phenotype.names <- unlist(lapply(ResList[[1]]@PhenoCodes,function(x){
return(decodePhenotype(x,
as.character(ResList[[1]]@MarkerNames),
ResList[[1]]@PartitionsPerMarker))}))
names(ResList[[1]]@PhenoCodes) <- phenotype.names
nodeExprTable <- lapply(ResList, function(x){return(apply(x@Partitions, 1, paste, collapse =""))})
allExprData <- fsApply(flowSet.trans, function(x){return(x)}, use.exprs = T)
nodeMappings <- unlist(nodeExprTable) %>% data.frame(CellId = names(.), Mapping = .)
nodeExprTable <- cbind(allExprData, nodeMappings)
nodeExprTable$NodeNames <- recoderFunc(nodeExprTable$Mapping,
ResList[[1]]@PhenoCodes,
names(ResList[[1]]@PhenoCodes))
nodeExprTable$FileNames <- gsub(".fcs[0-9]*", ".fcs", nodeExprTable$CellId)
ResultsTableFile <- paste(RESULTS_DIR, "BatchFlowTypeDataMergeResultsTable.txt", sep = "")
write.table(nodeExprTable, ResultsTableFile, sep = "\t", quote = F, row.names = F)
# subPopsExprTable <- list()
# length(subPopsExprTable) <- length(phenotype.names)
# for ( i in 1:length(ResList[[1]]@PhenoCodes)){
# subPopCode <- gsub("0", ".", ResList[[1]]@PhenoCodes[i])
# subPopData <- nodeExprTable[grep(subPopCode, nodeExprTable$Mapping),]
# subPopExprTable <- melt(subPopData,
# measure.vars = colnames(subPopData)[colnames(subPopData) %in% panelDesign$name[panelDesign$Ignore == 0] == T],
# id.vars = colnames(subPopData)[colnames(subPopData) %in% panelDesign$name == F],
# variable.name = "Metal",
# value.name = "Intensity"
# )
# subPopExprTable$Mapping <- rep(gsub("\\.", "0", subPopCode), nrow(subPopExprTable))
# subPopNodeExprTable <- acast(subPopExprTable,
# Mapping + Metal ~ FileNames,
# fun.aggregate = median,
# value.var = "Intensity"
# )
# subPopsExprTable[[i]] <- subPopNodeExprTable
# }
#
# orderedSubPopsExprTable <- lapply(subPopsExprTable, function(x){
# if(ncol(x) != length(names(ResList))){
# missingCol <- names(ResList)[which(names(ResList) %in% colnames(x) == F)]
# oldColnames <- colnames(x)
# subPopNodeExprTable <- cbind(x, matrix(nrow = nrow(x),
# ncol = length(missingCol)))
# colnames(subPopNodeExprTable) <- c(oldColnames, missingCol)
# x <- subPopNodeExprTable
# }
# x <- x[,orderVectorByVector(colnames(x), names(ResList))]
# return(x)
# })
#
# subPopsExprTable <- do.call(rbind, orderedSubPopsExprTable)
#
# nodeExprTableFile <- paste(RESULTS_DIR, "nodeExpressionFeatureTable.txt", sep = "")
#
# write.table(subPopsExprTable, nodeExprTableFile, sep = "\t", quote = F, row.names = T)
all.proportions <- matrix(0,length(ResList[[1]]@CellFreqs),length(ResList))
for (i in 1:length(ResList))
all.proportions[,i] = ResList[[i]]@CellFreqs / ResList[[i]]@CellFreqs[1]
colnames(all.proportions) <- names(ResList)
row.names(all.proportions) <- phenotype.names
countTable <- matrix(0,length(ResList[[1]]@CellFreqs),length(ResList))
for (i in 1:length(ResList))
countTable[,i] = ResList[[i]]@CellFreqs
colnames(countTable) <- names(ResList)
row.names(countTable) <- phenotype.names
nodeAbndncFeatureTableFile <- paste(RESULTS_DIR, "nodeAbundanceFeatureTable.txt", sep = "")
write.table(all.proportions, nodeAbndncFeatureTableFile, sep = "\t", quote = F, row.names = T)
nodeCountFeatureTableFile <- paste(RESULTS_DIR, "nodeCountFeatureTable.txt", sep = "")
write.table(countTable, nodeCountFeatureTableFile, sep = "\t", quote = F, row.names = T)
PhenoCodes <- data.frame(PhenoCodes = ResList[[1]]@PhenoCodes,
Names = names(ResList[[1]]@PhenoCodes))
PhenoCodesFile <- paste(RESULTS_DIR, "PhenoCodes.txt", sep = "")
write.table(PhenoCodes, PhenoCodesFile, sep = "\t", quote = F, row.names = F)
dir.create(paste0(RESULTS_DIR, "PHENOTYPED_FCS/"),
showWarnings = F)
for( files in file){
rawFCS <- read.FCS(files, transformation = F)
clusterData <- nodeExprTable %>%
filter(FileNames == basename(files)) %>%
select(Mapping)
clusterFCS <- flowCore::cbind2(rawFCS, as.matrix(clusterData))
row.names(pData(parameters(clusterFCS))) <- paste0("$P", c(1:nrow(pData(parameters(clusterFCS)))))
out.fcs.file <- paste0(RESULTS_DIR, "PHENOTYPED_FCS/phenotyped_", basename(files))
write.FCS(clusterFCS, out.fcs.file)
}
# workspaceFile <- paste(RESULTS_DIR, "BatchFlowTypeDataMergeWorkspace.Rdata", sep = "")
#
# save.image(file = workspaceFile)