-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModuleScore.R
318 lines (241 loc) · 14.5 KB
/
ModuleScore.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
library(Seurat)
library(tidyverse)
library(gridExtra)
library(quantmod)
library(grid)
library(data.table)
suppressMessages(library(dplyr))
SO_length <- length([email protected])
samples = eval(parse(text=gsub('\\[\\]','c()','c("X_ICC_18_Adjacent","X_ICC_18_Tumor","X_ICC_20_Tumor","X_ICC_23_Adjacent","X_ICC_23_Tumor","X_ICC_24_Tumor1","X_ICC_24_Tumor2","X_ICC_25_Adjacent")')))
sample_to_display <- c("X_ICC_18_Adjacent","X_ICC_18_Tumor","X_ICC_20_Tumor","X_ICC_23_Adjacent","X_ICC_23_Tumor","X_ICC_24_Tumor1","X_ICC_24_Tumor2","X_ICC_25_Adjacent")
if (length(samples) == 0) {
samples = unique([email protected]$sample_name)
}
colnames([email protected]) <- gsub("orig_ident","orig.ident",colnames([email protected]))
if("active.ident" %in% slotNames(SO)){
sample_name = as.factor([email protected]$orig.ident)
names(sample_name)=names([email protected])
[email protected] <- as.factor(vector())
[email protected] <- sample_name
SO.sub = subset(SO, ident = samples)
} else {
sample_name = as.factor([email protected]$orig.ident)
names(sample_name)=names([email protected])
[email protected] <- as.factor(vector())
[email protected] <- sample_name
SO.sub = subset(SO, ident = samples)
}
rm(SO)
SO.sub_clean <- SO.sub
Markers <- unlist(Gene_list)
if (FALSE){
protein_markers <- Markers[grepl("_prot",Markers)]
protein_orig_markers <- gsub("_prot.*","",protein_markers)
protein_markers_name <- paste(protein_orig_markers,
"_prot", sep = "")
i = 0
protein_array <- list()
for (i in seq_along(protein_orig_markers)){
protein_array[[i]] <- SO.sub@assays$Protein[protein_orig_markers[i],]
rownames(protein_array[[i]]) <- protein_markers_name[i]
}
protein_array_comp <- do.call(rbind,protein_array)
SO.sub@assays$SCT@data <- rbind(SO.sub@assays$SCT@data,protein_array_comp)
}
### Add negative/low identifiers to Module Scores
neg_markers_names <- Markers[grepl("_neg",Markers)]
orig_markers <- gsub("_neg.*","",neg_markers_names)
# Append neg_markers_names to rownames of SO.sub
neg_markers_list <- list() # Create a list for storage and retrieval
# Calculate adjusted expression for negative markers
for (i in seq_along(orig_markers)){
# Format the data so that it can rbinded with [email protected]
neg_markers_list[[i]] <- t(matrix(max(SO.sub@assays$SCT@data[orig_markers[i],]) - SO.sub@assays$SCT@data[orig_markers[i],]))
row.names(neg_markers_list[[i]]) <- neg_markers_names[i]
colnames(neg_markers_list[[i]]) <- colnames(SO.sub@assays$SCT@data)
# Append new Negative/low marker (w Expression Count) to SO slot
SO.sub@assays$SCT@data <- rbind(SO.sub@assays$SCT@data, neg_markers_list[[i]])
}
select_vector <- c("Malignant_Cells","Cholangiocytes","Hepatocytes","B_Cells","T_Cells","NK_Cells","Macrophages","Dendritic_Cells","Fibroblasts","Endothelial_Cells","CD8","CD4")
marker = select(Gene_list, select_vector)
marker.list = as.list(marker)
thres_vec <- c(0.13,0.65,0.07,0.1,0.25,0.12,0.15,0.17,0.08,0.12,0.55,0.5)
if (length(thres_vec) != length(select_vector)){
if (sum(thres_vec) == 0){
thres_vec <- rep(0, length(select_vector))
print("Manual threshold set to zero - outputing preliminary data")
} else {
stop("Manual threshold length does not match number of celltypes to analyze - please check manual thresholds")
}}
figures <- list()
exclude_cells <- c()
i = 0
j = 1
for (i in seq_along(marker.list)) {
print(names(marker.list[i]))
present=lapply(marker.list[[i]], function(x) x %in% rownames(SO.sub)) # apply function(x) x %in% rownames(SO.sub) to each element of marker.list
absentgenes = unlist(marker.list[[i]])[present==FALSE]
presentgenes = unlist(marker.list[[i]])[present==TRUE]
print(paste0("Genes not present: ",paste0(absentgenes,collapse=",")))
print(paste0("Genes present: ",paste0(presentgenes,collapse=",")))
if(length(presentgenes) == 0){
print(paste0(names(marker.list[i]), " genes were not found in SO and will not be analyzed"))
exclude_cells[j] <- i
j = j + 1
}}
if (length(exclude_cells) > 0){
marker.list <- marker.list[-exclude_cells]} else {
marker.list <- marker.list
}
for (i in seq_along(marker.list)) {
SO.sub=AddModuleScore(SO.sub,marker.list[i],name = names(marker.list[i]))
m = 0 # m will be plugged into for Seurat Object
m = paste0(names(marker.list[i]),"1")
[email protected][[m]] <- scales::rescale([email protected][[m]], to=c(0,1))
clusid = [email protected][[m]]
d <- density(clusid) # create a density plot for ModScore vs Number of cells
#hist(clusid[!is.na(clusid)], breaks=100, main=m)
#abline(v=midpt,col="red",lwd=2)
reduction = "tsne"
if(reduction=="tsne"){
p1 <- DimPlot(SO.sub, reduction = "tsne", group.by = "ident")
} else if(reduction=="umap"){
p1 <- DimPlot(SO.sub, reduction = "umap", group.by = "ident")
} else {
p1 <- DimPlot(SO.sub, reduction = "pca", group.by = "ident")
}
if(reduction=="tsne"){
clusmat=data.frame(ident=p1$data$ident,umap1=p1$data$tSNE_1,umap2=p1$data$tSNE_2, clusid=as.numeric([email protected][[m]]))
} else if(reduction=="umap"){
clusmat=data.frame(ident=p1$data$ident,umap1=p1$data$UMAP_1,umap2=p1$data$UMAP_2, clusid=as.numeric([email protected][[m]]))
} else {
clusmat=data.frame(ident=p1$data$ident,umap1=p1$data$PC_1,umap2=p1$data$PC_2, clusid=as.numeric([email protected][[m]]))
}
clusmat <- mutate(clusmat, sample_clusid = clusmat$clusid * grepl(paste(sample_to_display, collapse = "|"), clusmat$ident))
clusmat %>% group_by(clusid) %>% summarise(umap1.mean=mean(umap1), umap2.mean=mean(umap2)) -> umap.pos
title=as.character(m)
clusmat %>% dplyr::arrange(clusid) -> clusmat
# Dimension reduction
g <- ggplot(clusmat, aes(x=umap1, y=umap2)) +
theme_bw() +
theme(legend.title=element_blank()) +
geom_point(aes(colour=sample_clusid),alpha=0.5,shape = 20,size=1) +
#scale_color_gradient2(low = "blue4", mid = "white", high = "red",
# midpoint = midpt[[p]], na.value="grey",limits = c(0, 1)) +
scale_color_gradientn(colours = c("blue4","lightgrey", "red"), values = scales::rescale(c(0,thres_vec[i]/2,thres_vec[i],(thres_vec[i]+1)/2,1), limits = c(0, 1))) + guides(colour = guide_legend(override.aes = list(size=5, alpha = 1))) +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank()) +
ggtitle(paste0("Number of present genes: ", length(presentgenes))) +
xlab("tsne-1") + ylab("tsne-2")
clusid.df <- data.frame([email protected]$orig.ident,[email protected][[m]])
g1 = RidgePlot(SO.sub,features=m,group.by="orig.ident") + theme(legend.position = "none", title = element_blank(), axis.text.x = element_text(size = 6)) + geom_vline(xintercept = thres_vec[i], linetype = "dashed", color = "red3") + scale_x_continuous(breaks = seq(0,1,0.1))
# Violin Plot
clusid.df <- data.frame([email protected]$orig.ident,[email protected][[m]])
g2 = ggplot(clusid.df,aes(x=id,y=ModuleScore)) + geom_violin(aes(fill=id)) + theme_classic() + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),legend.title = element_blank(), panel.background = element_blank(), axis.text.x=element_blank(),legend.text=element_text(size=rel(0.8)),legend.position="top", axis.text.y = element_text(size = 6)) + guides(colour = guide_legend(override.aes = list(size=5, alpha = 1))) + geom_hline(yintercept = thres_vec[i], linetype = "dashed", color = "red3") + scale_y_continuous(breaks = seq(0,1,0.1))
# Color gradient density plot
g3 = ggplot(data.frame(x = d$x, y = d$y), aes(x, y)) + xlab("ModuleScore") + ylab("Density") + geom_line() +
geom_segment(aes(xend = d$x, yend = 0, colour = x)) + scale_y_log10() +
scale_color_gradientn(colours = c("blue4","lightgrey", "red"), values = scales::rescale(c(0,thres_vec[i]/2,thres_vec[i],(thres_vec[i]+1)/2,1), limits = c(0, 1))) + geom_vline(xintercept = thres_vec[i], linetype = "dashed", color = "red3") + geom_vline(xintercept = thres_vec[i], linetype = "dashed", color = "red3") + scale_x_continuous(breaks = seq(0,1,0.1)) + theme(legend.title = element_blank(), axis.text.x = element_text(size = 6))
# Set title for grid.arranged final figure
last.figure = grid.arrange(g,g1,g2,g3, ncol=2, top=textGrob(names(marker.list[i]), gp = gpar(fontsize = 14, fontface = "bold")))
figures[[i]] <- last.figure}
### Housekeeping before classification
## Name threshold vectors for easier retrieval in downstream steps
names(thres_vec) <- select_vector
## Rename meta.data Module Score columns so that it no longer has "1" at its end
names_repl <- substr(colnames([email protected][(SO_length +1): length([email protected])]), 1, nchar(colnames([email protected][(SO_length +1): length([email protected])]))-1) # nchar returns a vector containing the size of the character vector that was used as an argument. Subtracting 1 from the total size effectively allows you to omit the last character
names_orig <- colnames([email protected][(SO_length +1): length([email protected])]) # names to be replaced
setnames([email protected], names_orig, names_repl) # remove the "1" from the end of the Module Score columns
### New Approach:
General_class <- c("Malignant_Cells","Cholangiocytes","Hepatocytes","B_Cells","T_Cells","NK_Cells","Macrophages","Dendritic_Cells","Fibroblasts","Endothelial_Cells")
#subset the columns of the metadata containing module scores only
SO_Trunc_Metadata_General <- [email protected][General_class] # subsetted metadata
General_thres_vec <- thres_vec[General_class]
## See if elements in each ModScore column exceeds CellType threshold, set elements below threshold to zero. Keep values of elements above threshold
storage_list_MS_calls <- list()
Predict_Cell_from_ModScore <- function(ModScore_Metadata,thres_vec,rejection){
thres_ls <- list()
for (i in 1:ncol(ModScore_Metadata)){
thres_ls[[i]]<- rep(thres_vec[i],nrow(ModScore_Metadata))
}
thres_df <- data.frame(matrix(unlist(thres_ls),nrow = nrow(ModScore_Metadata)))
thres_filter <- ModScore_Metadata > thres_df
ModScore_Metadata_post_thres_filter <- ModScore_Metadata * thres_filter
## Find column number with highest modscore
max_col_vector <- max.col(ModScore_Metadata_post_thres_filter)
# If a row contains all zeroes, they will be labeled with unknown
all_zero_filter <- as.integer(!apply(ModScore_Metadata_post_thres_filter, 1, function(find_zero_rows) all(find_zero_rows == 0)))
# Final filtering:
final_filter <- (max_col_vector * all_zero_filter) + 1
# Original names appended to "unknown" classification for cells with ModScores below threshold
appended_names <- c(rejection, names(ModScore_Metadata))
# Added the names into a Likely_CellType Column
dupl_data <- ModScore_Metadata
dupl_data[,"Likely_CellType"] <- appended_names[final_filter]
return(dupl_data)
}
General_output <- Predict_Cell_from_ModScore(SO_Trunc_Metadata_General,General_thres_vec,rejection = "unknown")
table(General_output$Likely_CellType)
if (TRUE){
## Subclass Identification
Sub_class_storage <- c("T_Cells-CD8","T_Cells-CD4")
Sub_class_calls <- list()
parent_class <- unique(gsub("(.*)-(.*)","\\1",Sub_class_storage))
for (parent in parent_class){
Sub_class <- Sub_class_storage[grepl(parent,Sub_class_storage)]
children_class <- gsub("(.*)-(.*)","\\2",Sub_class)
# Subset out cells predicted to be CD8. CD8_Alloreactive cells will be screened from this population
parents <- rownames(General_output[General_output$Likely_CellType == parent,])
SO_Trunc_Metadata_parents <- [email protected][parents,] %>% select(children_class)
for (children in children_class){
plot_title <- paste("Density plot for",children,"Module Scores within", parent,"population", sep = " ")
adjusted_density_plot <- ggplot(SO_Trunc_Metadata_parents, aes_string(x = children)) + geom_density() + ggtitle(plot_title) + geom_vline(xintercept = thres_vec[children], linetype = "dashed", color = "red3")
figures[[length(figures) + 1]] <- adjusted_density_plot
}
# Create general annotation with barcoded cell and corresponding identity. M1 and M2 identities will be appended to this table after second round of classification
SO_Trunc_Metadata_no_parents <- General_output[!General_output$Likely_CellType == parent,]
non_parents <- rownames(SO_Trunc_Metadata_no_parents)
gen_annot_table <- data.frame(cells = non_parents, identity = SO_Trunc_Metadata_no_parents$Likely_CellType)
# Repeat Module Score Comparison and Cell Prediction with Macrophage Subset:
children_thres_vec <- thres_vec[children_class]
Sub_class_calls[[match(parent,parent_class)]] <- Predict_Cell_from_ModScore(SO_Trunc_Metadata_parents,children_thres_vec,rejection = parent) %>% select(Likely_CellType)
}
## Updating CellType(s) in metadata with subclass calls
final_subclass_results <- do.call(rbind,Sub_class_calls)
parent_class_exc_vec <- paste(unique(parent_class),collapse = "|")
General_output_final <- General_output[!grepl(parent_class_exc_vec,General_output$Likely_CellType),] %>% select(Likely_CellType)
appendable_results <- rbind(General_output_final,final_subclass_results)
subset_vector <- colnames([email protected])[!colnames([email protected]) %in% colnames([email protected])]
[email protected]$Likely_CellType <- appendable_results$Likely_CellType[match(rownames([email protected]),rownames(appendable_results))]} else {
subset_vector <- colnames([email protected])[!colnames([email protected]) %in% colnames([email protected])]
[email protected]$Likely_CellType <- General_output$Likely_CellType[match(rownames([email protected]),rownames(General_output))]
}
## Set Image Parameters for Module Score figures
n = ceiling(length(marker.list)^0.5)
m = ceiling(length(marker.list)/n)
imageWidth = 3200*m
imageHeight = 1600*n
dpi = 300
if (imageType == 'png') {
png(
filename=graphicsFile,
width=imageWidth,
height=imageHeight,
units="px",
pointsize=4,
bg="white",
res=dpi,
type="cairo")
} else {
library(svglite)
svglite::svglite(
file=graphicsFile,
width=round(imageWidth/dpi,digits=2),
height=round(imageHeight/dpi,digits=2),
pointsize=1,
bg="white")
}
Arranged_figures <- do.call("grid.arrange", c(figures))
print(Arranged_figures)
return(RFoundryObject(SO.sub_clean))