Spaces:
Sleeping
Sleeping
File size: 5,741 Bytes
19d6aaf a7626a2 19d6aaf a7626a2 19d6aaf a7626a2 19d6aaf a7626a2 19d6aaf bc52a9c 19d6aaf a7626a2 bc52a9c a7626a2 bc52a9c a7626a2 bc52a9c a7626a2 bc52a9c a7626a2 bc52a9c a7626a2 bc52a9c a7626a2 19d6aaf a7626a2 bc52a9c 35e13e5 a7626a2 35e13e5 a7626a2 19d6aaf |
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 |
library(shiny)
library(bslib)
library(dplyr)
library(ggplot2)
library(data.table)
library(tidyr)
library(stringr)
library(Seurat)
library(readr)
library(purrr)
library(harmony)
library(scales)
ui <- page_sidebar(
theme = bs_theme(bootswatch = "minty"),
title = "EV Data Analysis and Visualization",
sidebar = sidebar(
fileInput("evFiles", "Choose EV CSV Files", multiple = TRUE, accept = ".csv"),
),
plotOutput("scatter")
)
server <- function(input, output, session) {
subsetted <- reactive({
req(input$evFiles)
})
output$scatter <- renderPlot(
{
seurat_list <- list()
if(is.null(input$evFiles))
return()
for (i in 1:nrow(input$evFiles)) {
row <- input$evFiles[i,]
message("Processing: ", row$name)
# === 读取长格式数据 ===
sc_data <- fread(row$datapath, data.table = FALSE)
colnames(sc_data) <- c("symbol", "ev", "variable", "value")
sc_data$value <- as.numeric(sc_data$value)
sample_name <- unique(sc_data$symbol)
if (length(sample_name) != 1) {
warning("Multiple or missing sample names in file: ", basename(file))
next
}
# === 长转宽格式 ===
dat_wide <- pivot_wider(sc_data,
id_cols = ev,
names_from = variable,
values_from = value,
values_fill = 0)
rownames(dat_wide) <- dat_wide$ev
dat_wide <- dat_wide[, -1, drop = FALSE]
# === 筛选 EV(至少1个蛋白表达值 ≥ 1)===
valid_ev_idx <- apply(dat_wide, 1, function(x) sum(x >= 1) >= 2)
dat_filtered <- dat_wide[valid_ev_idx, , drop = FALSE]
n_ev <- nrow(dat_filtered)
message("Number of EVs in ", sample_name, ": ", n_ev)
group_size <- 10
num_groups <- floor(n_ev / group_size)
if (num_groups == 0) {
message("EVs < group size in ", sample_name, ", skip.")
next
}
# === 随机分组合并生成伪EV ===
set.seed(123)
sampled_ev_idx <- sample(1:n_ev, num_groups * group_size, replace = FALSE)
dat_sub <- dat_filtered[sampled_ev_idx, , drop = FALSE]
groups <- rep(1:num_groups, each = group_size)
pseudo_ev_list <- lapply(unique(groups), function(g) {
rows <- which(groups == g)
colSums(dat_sub[rows, , drop = FALSE])
})
names(pseudo_ev_list) <- paste0(sample_name, "_sEV", seq_along(pseudo_ev_list))
# === 合并为矩阵(蛋白为行,伪EV为列) ===
pseudo_matrix <- do.call(cbind, pseudo_ev_list)
pseudo_matrix <- as.data.frame(pseudo_matrix)
pseudo_matrix <- pseudo_matrix[order(rownames(pseudo_matrix)), ]
# === SeuratObject ===
seu <- CreateSeuratObject(counts = pseudo_matrix,
project = sample_name,
min.features = 2,
min.cells = 1)
seurat_list[[sample_name]] <- seu
}
if (length(seurat_list) >= 2) {
message("Merging ", length(seurat_list), " Seurat objects...")
merged_seu <- merge(seurat_list[[1]], y = seurat_list[-1], project = "MergedPseudoEVs")
merged_seu$orig.ident <- sapply(strsplit(colnames(merged_seu), "_"), `[`, 1)
merged_seu <- SCTransform(merged_seu, return.only.var.genes = FALSE, verbose = TRUE)
merged_seu <- RunPCA(merged_seu, npcs = 30, assay = "SCT", verbose = FALSE)
merged_seu <- RunHarmony(merged_seu, group.by.vars = "orig.ident", plot_convergence = TRUE)
dims_use <- 1:ncol(Embeddings(merged_seu, "harmony"))
merged_seu <- merged_seu %>%
RunUMAP(reduction = "harmony", dims = dims_use, n.neighbors = 20, min.dist = 0.2, spread = 1) %>%
RunTSNE(reduction = "harmony", dims = dims_use, perplexity = 50, learning.rate = 1000, theta = 0.5, check_duplicates = FALSE) %>%
FindNeighbors(reduction = "harmony", dims = dims_use) %>%
FindClusters(resolution = 0.4)
Idents(merged_seu) <- "seurat_clusters"
merged_seu$seurat_clusters <- factor(merged_seu$seurat_clusters )
cluster_table <- table(merged_seu$seurat_clusters )
cluster_prop <- prop.table(cluster_table) * 100
cluster_labels <- paste0(names(cluster_prop), " (", round(cluster_prop, 1), "%)")
names(cluster_labels) <- names(cluster_prop)
merged_seu$cluster_with_pct <- plyr::mapvalues(
merged_seu$seurat_clusters ,
from = names(cluster_labels),
to = cluster_labels
)
tsne_coords <- Embeddings(merged_seu, "tsne")
tsne_df <- data.frame(
cell = rownames(tsne_coords),
tSNE_1 = tsne_coords[, 1],
tSNE_2 = tsne_coords[, 2],
cluster = merged_seu$cluster_with_pct
)
#write_json(tsne_df, path = file.path(output_matrix_path, "tsne_results.json"), pretty = TRUE)
p <- DimPlot(
merged_seu,
reduction = "tsne",
group.by = "cluster_with_pct",
pt.size = 0.5,
raster = TRUE
) +
ggtitle("t-SNE") +
theme_minimal() +
theme(
panel.background = element_rect(fill = "gray95", color = NA),
plot.title = element_text(hjust = 0.5, size = 18),
legend.text = element_text(size = 12)
)
p
}
},
res = 100
)
}
shinyApp(ui, server)
|