-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCell_Type_Composition_Comparison.R
More file actions
140 lines (129 loc) · 5.71 KB
/
Cell_Type_Composition_Comparison.R
File metadata and controls
140 lines (129 loc) · 5.71 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
################################################################################
# barplot_function
# Purpose:
# Generate group-level and sample-level barplots and boxplots showing cell
# state proportions (%) across experimental groups (e.g., PbPP vs PbPPCd276).
# Performs statistical comparisons (Wilcoxon or t-test).
#
# Arguments:
# metadata - Data frame with columns [Group, sample_label, cellstate].
# lable - String prefix for output files.
# cell_type_colors - Named vector of colors for cell states.
# method - Statistical test ("wilcox.test" or "t.test").
# cellstate - Column with cell state annotation.
# boxplot_width - Width of boxplot PDF panels.
################################################################################
barplot_function = function(metadata,
lable,
cell_type_colors,
method = "wilcox.test",
cellstate,
boxplot_width = length(unique(metadata$cellstate))) {
# Group-level % composition
group_cellstate_df <- metadata %>%
group_by(Group, cellstate) %>%
summarise(n = n(), .groups = "drop") %>%
group_by(Group) %>%
mutate(percent = n / sum(n) * 100)
write.csv(group_cellstate_df, paste0(lable, "_group_df.csv"), row.names = FALSE)
# Sample-level % composition
cellstate_pct <- metadata %>%
group_by(sample_label, Group, cellstate) %>%
summarise(n = n(), .groups = "drop") %>%
group_by(sample_label) %>%
mutate(percent = n / sum(n) * 100)
write.csv(cellstate_pct, paste0(lable, "_sample_df.csv"), row.names = FALSE)
# Group barplot
p <- ggplot(group_cellstate_df, aes(x = Group, y = percent, fill = cellstate)) +
geom_bar(stat = "identity") +
scale_fill_manual(values = cell_type_colors) +
labs(y = "Percentage (%)", fill = "Cell Annotation") +
theme_classic()
pdf(paste0(lable, "_group_barplot.pdf"), height = 5, width = 3)
print(p + theme(legend.position = "none"))
dev.off()
# Save legend separately
legend <- cowplot::get_legend(p)
pdf(paste0(lable, "_group_barplot_legend.pdf"), width = 4, height = 4)
grid::grid.newpage()
grid::grid.draw(legend)
dev.off()
# Sample barplot
pdf(paste0(lable, "_sample_barplot.pdf"), height = 5, width = 6)
p2 <- ggplot(cellstate_pct, aes(x = sample_label, y = percent, fill = cellstate)) +
geom_bar(stat = "identity") +
scale_fill_manual(values = cell_type_colors) +
labs(y = "Percentage (%)") +
theme_classic() +
theme(axis.text.x = element_text(angle = 90, hjust = 1, size = 12),
legend.position = "none")
print(p2)
dev.off()
# Boxplot
pdf(paste0(lable, "_", method, "_sample_ggboxplot.pdf"), height = 5, width = boxplot_width)
p3 <- ggboxplot(cellstate_pct, x = "cellstate", y = "percent",
color = "Group", add = "jitter",
palette = c("PbPP" = "#45A5ED", "PbPPCd276" = "#FF8D7D")) +
stat_compare_means(aes(group = Group), method = method, label = "p.signif") +
labs(y = "Percentage (%)") +
theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 12))
print(p3)
dev.off()
}
################################################################################
# barplot_function_treatment
# Purpose:
# Same as barplot_function, but customized for treatment datasets with
# predefined `treatment_col` colors for groups.
################################################################################
barplot_function_treatment = function(metadata,
lable,
cell_type_colors,
method = "wilcox.test",
cellstate,
boxplot_width = length(unique(metadata$cellstate))) {
# Group-level % composition
group_cellstate_df <- metadata %>%
group_by(Group, cellstate) %>%
summarise(n = n(), .groups = "drop") %>%
group_by(Group) %>%
mutate(percent = n / sum(n) * 100)
write.csv(group_cellstate_df, paste0(lable, "_group_df.csv"), row.names = FALSE)
# Sample-level % composition
cellstate_pct <- metadata %>%
group_by(sample_label, Group, cellstate) %>%
summarise(n = n(), .groups = "drop") %>%
group_by(sample_label) %>%
mutate(percent = n / sum(n) * 100)
write.csv(cellstate_pct, paste0(lable, "_sample_df.csv"), row.names = FALSE)
# Group barplot
p <- ggplot(group_cellstate_df, aes(x = Group, y = percent, fill = cellstate)) +
geom_bar(stat = "identity") +
scale_fill_manual(values = cell_type_colors) +
labs(y = "Percentage (%)", fill = "Cell Annotation") +
theme_classic()
pdf(paste0(lable, "_group_barplot.pdf"), height = 5, width = 3)
print(p + theme(legend.position = "none"))
dev.off()
# Sample barplot
pdf(paste0(lable, "_sample_barplot.pdf"), height = 5, width = 6)
p2 <- ggplot(cellstate_pct, aes(x = sample_label, y = percent, fill = cellstate)) +
geom_bar(stat = "identity") +
scale_fill_manual(values = cell_type_colors) +
labs(y = "Percentage (%)") +
theme_classic() +
theme(axis.text.x = element_text(angle = 90, hjust = 1, size = 12),
legend.position = "none")
print(p2)
dev.off()
# Boxplot
pdf(paste0(lable, "_", method, "_sample_ggboxplot.pdf"), height = 5, width = boxplot_width)
p3 <- ggboxplot(cellstate_pct, x = "cellstate", y = "percent",
color = "Group", add = "jitter",
palette = treatment_col) +
stat_compare_means(aes(group = Group), method = method, label = "p.signif") +
labs(y = "Percentage (%)") +
theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 12))
print(p3)
dev.off()
}