
image.png
sample_names <- unique(seu$sample_names)
head(sample_names)
# 获取positionEnrichment数据
positionEnrichment <- GetAssayData(object = seu, assay = "peaks", layer = "positionEnrichment")
enrichment.matrix <- positionEnrichment[["TSS"]]
enrichment.matrix <- as.data.frame(as.matrix(enrichment.matrix))
TSS_profile_df <- data.frame()
# 使用提供的sample_names
for (sample in sample_names) {
# 选择属于当前样本的细胞
# 假设cell_id的格式是"SAMPLE_NAME_CELLBARCODE"
sample_cells <- grepl(paste0("^", sample), rownames(enrichment.matrix))
if (sum(sample_cells) > 0) { # 确保有匹配的细胞
dataset_enrichment.matrix <- enrichment.matrix[sample_cells, ]
# 计算平均值并添加到结果数据框
sample_df <- data.frame(
pos = colnames(dataset_enrichment.matrix),
mean = colMeans(dataset_enrichment.matrix, na.rm = TRUE),
dataset = sample
)
TSS_profile_df <- rbind(TSS_profile_df, sample_df)
} else {
print(paste("No cells found for sample:", sample))
}
}
TSS_profile_df$pos <- as.numeric(TSS_profile_df$pos)
TSS_profile_df$dataset <- factor(TSS_profile_df$dataset, levels = sample_names)
head(TSS_profile_df)
# 设置调色
if (length(sample_names) <= 2) {
newpalette <- c("#F39B7FFF", "#E64B35FF")[1:length(sample_names)]
} else {
newpalette <- colorRampPalette(c("#F39B7FFF", "#E64B35FF"))(length(sample_names))
}
p1 <- ggplot(data = TSS_profile_df, aes(x = pos, y = mean, color = dataset)) +
geom_line(stat = "identity", size = 0.2) +
labs(x = "Distance from xxx motif (bp)", y = "Mean enrichment score") +
scale_color_manual(values = newpalette) +
theme_classic() +
theme(
axis.title.x = element_text(size = rel(2)),
axis.title.y = element_text(size = rel(2), vjust = 2),
axis.text = element_text(color = "black", size = rel(1.8)),
axis.line = element_line(colour = "black", size = 1),
axis.ticks = element_line(),
plot.margin = unit(c(0, 0.5, 0.5, 0.5), "cm"),
legend.position = "none")
print(p1)