-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeconvolution_bulk_zebrafish_to_whole_zebrafish.R
More file actions
132 lines (110 loc) · 4.89 KB
/
deconvolution_bulk_zebrafish_to_whole_zebrafish.R
File metadata and controls
132 lines (110 loc) · 4.89 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
################################################################################
# ZEBRAFISH BULK TO WHOLE ZEBRAFISH
################################################################################
# -------------------- PART 0: Load Required Libraries -------------------------
library(Seurat)
library(SingleCellExperiment)
library(MuSiC)
library(Matrix)
library(dplyr)
library(ggplot2)
library(pheatmap)
library(reshape2)
library(scuttle)
library(Biobase)
# -------------------- PART 1: Process Bulk RNA-seq Data ------------------------
setwd("~/Gene_count_files")
files <- list.files(pattern = "gene_counts_data_.*", full.names = TRUE)
process_file <- function(file) {
data <- read.delim(file, header = TRUE)
data <- data %>% select(-Chr, -Start, -End, -Strand, -Length)
return(data)
}
raw_gene_counts <- lapply(files, process_file)
names(raw_gene_counts) <- gsub("gene_counts_data_|\\.txt$", "", basename(files))
# -------------------- PART 2: Prepare Bulk RNA-seq Data ------------------------
data_file <- 1 # ****CHANGE as necessary
selected_data <- raw_gene_counts[[data_file]]
file_name <- names(raw_gene_counts)[data_file]
print(file_name)
selected_data <- selected_data[!is.na(selected_data$Geneid), ]
selected_data <- selected_data[!duplicated(selected_data$Geneid), ]
rownames(selected_data) <- selected_data$Geneid
selected_data <- selected_data[, -which(names(selected_data) == "Geneid")]
bulk_matrix <- as.matrix(selected_data)
print(head(bulk_matrix))
# -------------------- PART 3: Load Daniocell Single-Cell Data ------------------
setwd("~/")
daniocell <- readRDS("Daniocell2023_SeuratV4.rds")
daniocell.sce <- as.SingleCellExperiment(daniocell)
clusters_col <- "ident"
samples_col <- "orig.ident"
# -------------------- Filter to Shared Genes ------------------------
intersect_genes <- intersect(rownames(bulk_matrix), rownames(daniocell.sce))
bulk_matrix <- bulk_matrix[intersect_genes, , drop = FALSE]
daniocell.sce <- daniocell.sce[intersect_genes, , drop = FALSE]
# -------------------- Convert Bulk to ExpressionSet ------------------------
bulk_metadata <- data.frame(sampleID = colnames(bulk_matrix), row.names = colnames(bulk_matrix))
bulk_eset <- ExpressionSet(
assayData = bulk_matrix,
phenoData = AnnotatedDataFrame(bulk_metadata)
)
print(bulk_eset)
# -------------------- Convert SC to ExpressionSet ------------------------
sc_counts <- assay(daniocell.sce, "logcounts")
sc_metadata <- colData(daniocell.sce)
rownames(sc_metadata) <- colnames(sc_counts)
sc_eset <- ExpressionSet(
assayData = as.matrix(sc_counts),
phenoData = AnnotatedDataFrame(as.data.frame(sc_metadata))
)
print(sc_eset)
# -------------------- PART 4: Run MuSiC Deconvolution ------------------------
Est.prop.bulk_matrix <- music_prop(
bulk.eset = bulk_eset,
sc.eset = sc_eset,
clusters = clusters_col,
samples = samples_col,
verbose = TRUE
)
# View results
print(Est.prop.bulk_matrix$Est.prop.weighted)
# -------------------- PART 5: Jitter Plot -------------------------------------
jitter.fig <- Jitter_Est(
list(
data.matrix(Est.prop.bulk_matrix$Est.prop.weighted),
data.matrix(Est.prop.bulk_matrix$Est.prop.allgene)
),
method.name = c('MuSiC', 'NNLS'),
title = 'Jitter plot of Est Proportions'
)
print(jitter.fig)
ggsave(filename = paste0("jitter_plot_est_prop_wholeZebrafish_", file_name, ".png"),
plot = jitter.fig, width = 8, height = 6, dpi = 300)
# -------------------- PART 6: Heatmap ------------------------------------------
heatmap_data <- data.matrix(Est.prop.bulk_matrix$Est.prop.weighted)
write.csv(heatmap_data, file = paste0("est_prop_wholeZebrafish_matrix_", file_name, ".csv"), row.names = TRUE)
heatmap <- pheatmap(heatmap_data,
cluster_rows = TRUE,
cluster_cols = TRUE,
color = colorRampPalette(c("navy", "white", "firebrick"))(50),
main = "Heatmap of Estimated Cell Type Proportions",
fontsize_row = 8,
fontsize_col = 8)
print(heatmap)
ggsave(filename = paste0("heatmap_est_prop_wholeZebrafish_", file_name, ".png"),
plot = heatmap, width = 6, height = 6, dpi = 300)
# -------------------- PART 7: Stacked Bar Plot ---------------------------------
heatmap_data_df <- as.data.frame(heatmap_data)
heatmap_data_df$Sample <- rownames(heatmap_data_df)
heatmap_data_long <- melt(heatmap_data_df, id.vars = "Sample",
variable.name = "CellType", value.name = "Proportion")
stacked_bar_plot <- ggplot(heatmap_data_long, aes(x = Sample, y = Proportion, fill = CellType)) +
geom_bar(stat = "identity") +
labs(title = "Proportions of Cell Types Across Samples",
x = "Samples", y = "Proportion") +
scale_fill_brewer(palette = "Set3") +
theme_minimal()
print(stacked_bar_plot)
ggsave(filename = paste0("stacked_bar_plot_est_prop_wholeZebrafish_", file_name, ".png"),
plot = stacked_bar_plot, width = 6, height = 6, dpi = 300)