Repository navigation
Expand file tree
/
Copy path260423_CoexpressionPlot.R
More file actions
161 lines (139 loc) · 4.2 KB
/
Copy path260423_CoexpressionPlot.R
File metadata and controls
161 lines (139 loc) · 4.2 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
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
library(Seurat)
library(dplyr)
library(ggplot2)
CoexpressionPlot <- function(
object,
gene1,
gene2,
reduction = "umap",
assay = NULL,
layer = "data",
group.by = NULL,
expr.cutoff1 = 0,
expr.cutoff2 = 0,
pt.size = 1,
alpha = 0.9,
cols = c(
"No expression" = "lightgrey",
"Gene1 only" = "#3B78E7",
"Gene2 only" = "#32CD32",
"All co-expressed" = "red"
),
label = TRUE,
label.size = 4,
label.color = "black",
label.box = FALSE,
title = NULL
) {
if (!inherits(object, "Seurat")) {
stop("object must be a Seurat object.")
}
if (is.null(assay)) {
assay <- DefaultAssay(object)
}
if (!reduction %in% Reductions(object)) {
stop(paste0("Reduction '", reduction, "' not found in object."))
}
expr_mat <- GetAssayData(object = object, assay = assay, layer = layer)
missing_genes <- setdiff(c(gene1, gene2), rownames(expr_mat))
if (length(missing_genes) > 0) {
stop(
paste0(
"These gene(s) were not found in assay='", assay,
"', layer='", layer, "': ",
paste(missing_genes, collapse = ", ")
)
)
}
cells_use <- colnames(object)
expr_df <- data.frame(
cell = cells_use,
gene1_expr = as.numeric(expr_mat[gene1, cells_use]),
gene2_expr = as.numeric(expr_mat[gene2, cells_use]),
stringsAsFactors = FALSE
)
emb <- Embeddings(object[[reduction]])
emb <- as.data.frame(emb)
if (ncol(emb) < 2) {
stop(paste0("Reduction '", reduction, "' has fewer than 2 dimensions."))
}
emb <- emb[cells_use, 1:2, drop = FALSE]
colnames(emb)[1:2] <- c("dim1", "dim2")
emb$cell <- rownames(emb)
df <- left_join(expr_df, emb, by = "cell")
if (!is.null(group.by)) {
if (!group.by %in% colnames(object@meta.data)) {
stop(paste0("group.by column '", group.by, "' not found in object@meta.data"))
}
df$group_label <- object@meta.data[df$cell, group.by]
}
df <- df %>%
mutate(
expr_group = case_when(
gene1_expr <= expr.cutoff1 & gene2_expr <= expr.cutoff2 ~ "No expression",
gene1_expr > expr.cutoff1 & gene2_expr <= expr.cutoff2 ~ "Gene1 only",
gene1_expr <= expr.cutoff1 & gene2_expr > expr.cutoff2 ~ "Gene2 only",
gene1_expr > expr.cutoff1 & gene2_expr > expr.cutoff2 ~ "All co-expressed"
)
)
df$expr_group <- factor(
df$expr_group,
levels = c("No expression", "Gene1 only", "Gene2 only", "All co-expressed")
)
df <- df %>% arrange(expr_group)
legend_labels <- c(
"No expression" = "No expression",
"Gene1 only" = paste0(gene1, " only"),
"Gene2 only" = paste0(gene2, " only"),
"All co-expressed" = "All co-expressed"
)
if (is.null(title)) {
title <- paste0(gene1, " & ", gene2, " co-expression")
}
p <- ggplot(df, aes(x = dim1, y = dim2, color = expr_group)) +
geom_point(size = pt.size, alpha = alpha) +
scale_color_manual(
values = cols,
breaks = names(legend_labels),
labels = legend_labels
) +
labs(
title = title,
x = paste0(toupper(reduction), " 1"),
y = paste0(toupper(reduction), " 2"),
color = NULL
) +
theme_classic(base_size = 14)
if (label && !is.null(group.by)) {
label_df <- df %>%
dplyr::filter(!is.na(group_label)) %>%
dplyr::group_by(group_label) %>%
dplyr::summarise(
dim1 = median(dim1, na.rm = TRUE),
dim2 = median(dim2, na.rm = TRUE),
.groups = "drop"
) %>%
dplyr::mutate(label = as.character(group_label))
if (label.box) {
p <- p +
geom_label(
data = label_df,
mapping = aes(x = dim1, y = dim2, label = label),
inherit.aes = FALSE,
size = label.size,
color = label.color
)
} else {
p <- p +
geom_text(
data = label_df,
mapping = aes(x = dim1, y = dim2, label = label),
inherit.aes = FALSE,
size = label.size,
color = label.color,
fontface = "bold"
)
}
}
return(p)
}