Single cell analysis #7 데이터 시각화 (GSEA-1)
# 필요한 패키지 로드
library(fgsea)
library(msigdbr)
library(patchwork)
library(ggplot2) # ggplot2 패키지 로드
# 유전자 랭킹 생성
ranks_ht04 <- markers_ht04$avg_log2FC
names(ranks_ht04) <- rownames(markers_ht04)
# HALLMARK 경로 가져오기
gene_sets_hallmark <- msigdbr(species = "Homo sapiens", category = "H")
hallmark_pathways <- split(x = gene_sets_hallmark$gene_symbol, f = gene_sets_hallmark$gs_name)
# FGSEA 실행
fgsea_results_ht04_hallmark <- fgsea(pathways = hallmark_pathways, stats = ranks_ht04, nperm = 1000)
# 상위 경로 선택 (예: p-value 기준 상위 3개 경로 선택)
top_pathways <- fgsea_results_ht04_hallmark[order(fgsea_results_ht04_hallmark$pval), ]$pathway[1:6]
# 여러 경로에 대한 plotEnrichment 결과 생성
p1 <- plotEnrichment(hallmark_pathways[[top_pathways[1]]], ranks_ht04) +
labs(title = top_pathways[1]) +
theme_minimal() +
theme(plot.title = element_text(size = 14, face = "bold")) + # 제목 스타일 조정
scale_color_manual(values = c("blue")) + # 선 색상 변경
theme(legend.position = "none") # 범례 제거
p2 <- plotEnrichment(hallmark_pathways[[top_pathways[2]]], ranks_ht04) +
labs(title = top_pathways[2]) +
theme_minimal() +
theme(plot.title = element_text(size = 14, face = "bold")) +
scale_color_manual(values = c("blue")) +
theme(legend.position = "none")
p3 <- plotEnrichment(hallmark_pathways[[top_pathways[3]]], ranks_ht04) +
labs(title = top_pathways[3]) +
theme_minimal() +
theme(plot.title = element_text(size = 14, face = "bold")) +
scale_color_manual(values = c("blue")) +
theme(legend.position = "none")
p4 <- plotEnrichment(hallmark_pathways[[top_pathways[4]]], ranks_ht04) +
labs(title = top_pathways[4]) +
theme_minimal() +
theme(plot.title = element_text(size = 14, face = "bold")) +
scale_color_manual(values = c("blue")) +
theme(legend.position = "none")
p5 <- plotEnrichment(hallmark_pathways[[top_pathways[5]]], ranks_ht04) +
labs(title = top_pathways[5]) +
theme_minimal() +
theme(plot.title = element_text(size = 14, face = "bold")) +
scale_color_manual(values = c("blue")) +
theme(legend.position = "none")
p6 <- plotEnrichment(hallmark_pathways[[top_pathways[6]]], ranks_ht04) +
labs(title = top_pathways[6]) +
theme_minimal() +
theme(plot.title = element_text(size = 14, face = "bold")) +
scale_color_manual(values = c("blue")) +
theme(legend.position = "none")
# 여러 plot을 patchwork로 결합
(p1 / p2 | p3 / p4 | p5 / p6) # 가로로 배치
# NES score가 -1 이하인 경로 선택
bottom_pathways <- fgsea_results_ht04_hallmark[fgsea_results_ht04_hallmark$NES <= -1, ]
# 하위 경로가 있는지 확인하고, 경로 이름을 가져오기
if (nrow(bottom_pathways) > 0) {
bottom_pathway_names <- bottom_pathways$pathway
# 각 하위 경로에 대한 plotEnrichment 결과 생성
plots <- lapply(bottom_pathway_names, function(pathway) {
plotEnrichment(hallmark_pathways[[pathway]], ranks_ht04) +
labs(title = pathway) +
theme_minimal() +
theme(plot.title = element_text(size = 14, face = "bold")) + # 제목 스타일 조정
scale_color_manual(values = c("blue")) + # 선 색상 변경
theme(legend.position = "none") # 범례 제거
})
# 여러 plot을 patchwork로 결합
combined_plot <- wrap_plots(plots) # 여러 플롯을 결합
print(combined_plot) # 결과 출력
} else {
cat("NES score가 -1 이하인 경로가 없습니다.\n")
}
댓글
댓글 쓰기