-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathphenotype_gridpoints.R
173 lines (135 loc) · 6.39 KB
/
phenotype_gridpoints.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
#!/usr/bin/env Rscript
require(docopt)
require(methods)
"
Usage:
phenotype_gridpoints.R (-h | --help | --version)
phenotype_gridpoints.R DIR
Description: This script phenotypes grid points from FlowSOM with FlowType and performs consensus clustering
Options:
--version Show the current version.
Arguments:
DIR Provide directory where cyttools.args.Rdata file is located
" -> 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")
##########################################################################
############################ R code goes here ############################
##########################################################################
# read in panel design
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()
}
# read in clusterd FCS files
cluster_dir <- args$CLUSTERDIR # grabs directory from initial cyttools call
file <- list.files(cluster_dir ,pattern='.fcs$', full=TRUE) # captures all FCS files in the directory
lineage_markers <- targets$name[targets$Lineage == 1]
functional_markers <- targets$name[targets$Functional == 1]
cluster.flowSet.trans <- read.flowSet(file, transformation = F, truncate_max_range = F)
# read in phenotyped FCS files
pheno_dir <- args$PHENODIR # grabs directory from initial cyttools call
pheno_file <- list.files(pheno_dir ,pattern='.fcs$', full=TRUE) # captures all FCS files in the directory
pheno.flowSet.trans <- read.flowSet(pheno_file, transformation = F, truncate_max_range = F)
mappings <- cluster.flowSet.trans %>%
fsApply(function(x){return(as.data.frame(exprs(x)))}, simplify = F) %>%
bind_rows(.id = "FileNames") %>%
select(Mapping) %>%
bind_cols(pheno.flowSet.trans %>%
fsApply(function(x){return(as.data.frame(exprs(x)))}, simplify = F) %>%
bind_rows(.id = "FileNames") %>%
select(contains("Phenotype_")))
map_counts <- mappings %>%
gather(PhenoCodes,
Phenotype,
-Mapping) %>%
group_by(Mapping, PhenoCodes, factor(Phenotype)) %>%
tally()
diff_counts <- map_counts %>%
spread(`factor(Phenotype)`,
`n`,
fill = NA)
if(str_detect(colnames(diff_counts)[length(colnames(diff_counts))], "3")){
diff_counts <- diff_counts
}else{
diff_counts <- diff_counts %>%
ungroup() %>%
mutate(`3` = rep(as.double(NA), nrow(.)))
}
reduced_phenocodes <- diff_counts %>%
mutate(MarkerCode = case_when(is.na(`3`) & (`2`/(`1` + `2`)) > 0.75 ~ 2,
is.na(`3`) & (`1`/(`1` + `2`)) > 0.75 ~ 1,
is.na(`3`) == F & (`3`/(`3` + `2` + `1`)) > 0.75 ~ 3,
is.na(`3`) == F & (`2`/(`3` + `2` + `1`)) > 0.75 ~ 2,
is.na(`3`) == F & (`1`/(`3` + `2` + `1`)) > 0.75 ~ 1,
TRUE ~ 0)) %>%
select(Mapping, PhenoCodes, MarkerCode) %>%
#mutate(MarkerCode = factor(MarkerCode)) %>%
spread(PhenoCodes, MarkerCode) %>%
select(starts_with("Phenotype_"),
Mapping)
phenocode_matrix <- lapply(seq_along(colnames(reduced_phenocodes %>%
ungroup() %>%
select(-Mapping))),
function(x){
marker <- gsub("Phenotype_", "", colnames(reduced_phenocodes)[x])
phenocode_vector <- reduced_phenocodes[,x]
if(max(as.numeric(as.character(phenocode_vector[[1]]))) == 3){
phenocode_vector[phenocode_vector == 3] <- paste0(marker, "hi")
phenocode_vector[phenocode_vector == 2] <- paste0(marker, "lo")
phenocode_vector[phenocode_vector == 1] <- paste0(marker, "-")
phenocode_vector[phenocode_vector == 0] <- ""
}else{
phenocode_vector[phenocode_vector == 2] <- paste0(marker, "+")
phenocode_vector[phenocode_vector == 1] <- paste0(marker, "-")
phenocode_vector[phenocode_vector == 0] <- ""
}
return(phenocode_vector)
}) %>%
bind_cols()
lineage_lists <- phenocode_matrix %>%
apply(1, function(x){
reduced_x <- x[grep("^$", x, invert = T)]
return(lapply(seq_along(1:length(reduced_x)), function(y){
combn(reduced_x, y, simplify = T) %>%
apply(2, paste, collapse = " ") %>%
return()
}))
})
annotated_mappings <- data_frame(Mapping = 1:length(lineage_lists),
Immunophenotypes = lineage_lists)
annotated_mappings_file <- paste(RESULTS_DIR, "annotated_mappings.txt", sep = "")
distinct_mappings_file <- paste(RESULTS_DIR, "distinct_mappings.txt", sep = "")
annotated_mappings %>%
unnest() %>%
unnest() %>%
group_by(Immunophenotypes) %>%
summarise(Mappings = paste0(Mapping, collapse = ",")) %>%
mutate(Num_Markers = str_count(Immunophenotypes, "\\+|\\-|lo|hi"),
Num_GridPoints = str_count(Mappings, ",") + 1) %>%
arrange(desc(Num_Markers)) %>%
distinct(Mappings,
.keep_all = T) %>%
write_tsv(distinct_mappings_file)
annotated_mappings %>%
unnest() %>%
unnest() %>%
write_tsv(annotated_mappings_file)
# immunophenotypes_order <- order(str_count(row.names(filtered_count_table)), decreasing = T)
#
# unique_filtered_count_table <- unique(filtered_count_table[immunophenotypes_order,])
# uniq_immunophenotypes <- row.names(filtered_count_table)[immunophenotypes_order][!duplicated(filtered_count_table[immunophenotypes_order,])]
##########################################################################
############################ End code ############################
##########################################################################
# workspaceFile <- paste(RESULTS_DIR, "phenotype_gridpoints.Workspace.Rdata", sep = "")
# save.image(file = workspaceFile)