前言

這份文件是一個完整的生物資訊分析流程教學,主要目標是利用 Seurat 套件進行差異基因表現 (Differentially Expressed Genes, DEGs) 分析,並接著使用 clusterProfiler 套件對找出的差異基因進行基因功能富集分析 (Gene Ontology, GO)。

整個流程將涵蓋資料的前處理、差異基因的篩選、熱圖 (Heatmap) 的繪製、基因 ID 的轉換,以及最終的功能富集分析與視覺化。這份教學適合對 R 語言有基本認識,並想學習如何分析轉錄體資料的初學者。

分析流程概覽

  1. 環境準備: 清理環境、載入必要的 R 套件。
  2. 資料讀取與前處理: 讀取 Seurat 物件,並進行標準化、尋找變異基因、規模化等步驟。
  3. 差異基因分析 (DEG Analysis):
    • 使用 FindMarkers 找出特定群組間的差異基因。
    • 使用 FindAllMarkers 找出所有群組特有的標記基因 (marker genes)。
  4. 視覺化:
    • 使用 DoHeatmap 繪製熱圖,視覺化呈現基因表現模式。
  5. 基因列表準備:
    • 篩選顯著的差異基因 (例如,依據 avg_log2FC 排序並選取前500名)。
    • 使用 bitr 函數將基因名稱 (Symbol) 轉換為後續分析所需的 ENTREZ ID。
  6. 功能富集分析 (ORA):
    • 使用 clusterProfilercompareCluster 函數,對不同群組的基因列表進行 GO 富集分析。
  7. 結果匯出與視覺化:
    • 將富集分析的結果儲存為 RDS 與文字檔。
    • 使用 dotplot (點圖) 與 emapplot (網絡圖) 進行視覺化。
    • 將結果整理並匯出成 Excel 檔案。
  8. 結果簡化 (Simplify):
    • 對於 GO 分析結果,使用 simplify 函數去除冗餘的 GO terms,讓結果更易於解讀。

步驟一:尋找差異表現基因 (DEGs)

在這個章節,我們將從一個已經前處理過的 Seurat 物件開始,找出實驗組 (3D) 相對於對照組 (2D) 的差異表現基因。

1.1 環境設定與資料載入

首先,我們需要清理 R 的工作環境,並載入本次分析所需的核心套件。

 1# 清理工作環境中所有現存的物件,確保一個乾淨的開始
 2rm(list = ls(all = T))
 3
 4# 載入分析所需的核心 R 套件
 5library(dplyr)         # 提供強大的資料處理與操作功能 (如 filter, mutate, arrange)
 6library(Seurat)        # 單細胞 RNA 定序數據分析的核心工具
 7library(ggplot2)       # 繪製高品質圖形的基礎套件
 8library(patchwork)     # 組合多個 ggplot 圖形的工具
 9library(sctransform)   # Seurat 中用於正規化數據的另一種方法
10library(clusterProfiler) # 進行基因功能富集分析 (GO, KEGG) 的主要工具
11library(cellcall)      # (此腳本中載入但未使用,推測為其他分析流程所需)
12
13# 設定工作目錄,所有檔案的讀取與儲存都將以此路徑為基準
14# 請務必將 "C:/Users/TPOW31714/Desktop/..." 修改為您自己的檔案路徑
15setwd("C:/Users/TPOW31714/Desktop/20250224 Human microarray for CY_add cell/4. RDS/")
16
17# 從 RDS 檔案中讀取 Seurat 物件
18# RDS 是 R 語言特有的檔案格式,可以儲存任何 R 物件
19da0 <- readRDS("2. GPL17586_12_sample_250625.rds", refhook = NULL)

readRDS() 函數說明

  • 用途: readRDS 用於讀取一個 .rds 檔案,並將其還原為 R 中的物件。這對於儲存複雜的物件 (如 Seurat 物件) 非常有用。
  • 使用方式: readRDS(file, refhook = NULL)
  • 參數:
    • file: 字串,指定要讀取的 .rds 檔案路徑。
    • refhook: 通常設定為 NULL 即可。

1.2 資料前處理

在進行差異基因分析之前,需要對數據進行標準的 Seurat 前處理流程,包括正規化、尋找變異基因與規模化。

 1# 1. 正規化 (Normalization)
 2#    目的:消除因測序深度不同造成的技術性差異
 3da0 <- NormalizeData(da0)
 4
 5# 2. 尋找高變異基因 (Find Variable Features)
 6#    目的:找出在不同細胞間表現量差異最大的基因,這些基因最可能用來區分細胞類型
 7da0 <- FindVariableFeatures(da0)
 8
 9# 3. 規模化 (Scaling)
10#    目的:將每個基因的表現量進行標準化 (平均值為0,變異數為1),避免高表現量基因主導後續分析
11da0 <- ScaleData(da0, features = rownames(da0))

Seurat 前處理函數說明

  • NormalizeData(object, ...):
    • 用途: 對基因表現數據進行正規化。預設方法是 “LogNormalize”,計算公式為 log1p( (count / total_counts) * 10000 )
  • FindVariableFeatures(object, ...):
    • 用途: 根據基因的表現量變異程度,找出高變異基因 (HVGs)。
  • ScaleData(object, ...):
    • 用途: 對指定的基因進行線性轉換 (規模化),使每個基因的平均表現量為 0,變異數為 1。這是降維分析 (如 PCA) 前的必要步驟。
    • features = rownames(da0): 指定對所有基因進行規模化。

1.3 找出 3D vs. 2D 的差異基因

設定好細胞群組後,我們使用 FindMarkers 來找出 “3D” 細胞群相對於 “2D” 細胞群的差異表現基因。

 1# 設定預設要分析的 Assay (通常是 'RNA')
 2DefaultAssay(da0) <- 'RNA'
 3
 4# 設定細胞的身分 (Identity),這裡我們使用元數據 (metadata) 中的 "Group_1" 欄位
 5# "Group_1" 欄位應包含 "2D" 和 "3D" 的標籤
 6Idents(da0) <- factor(da0$Group_1)
 7
 8# 檢查各組的細胞數量
 9table(da0$Group_1) %>% as.data.frame()
10
11# 定義要比較的目標群組
12cell_groups <- c("3D")
13
14# 使用 for 迴圈來找出差異基因
15# 雖然這裡只比較一組,但迴圈的寫法提供了未來擴充的彈性
16for (cell_group in cell_groups) {
17  # 核心步驟:使用 FindMarkers 找差異基因
18  da0_markers_1 <- FindMarkers(da0, 
19                               ident.1 = cell_group, # 實驗組
20                               ident.2 = "2D",       # 對照組 (此處腳本原為NULL,改為"2D"更明確)
21                               only.pos = FALSE)     # only.pos=F 表示同時找出上調與下調基因
22
23  # 設定輸出目錄
24  setwd("C:/Users/TPOW31714/Desktop/20250224 Human microarray for CY_add cell/3. Data/20250625 data_ORA/1. 3Dvs2D DEGs_12samples")
25  
26  # 將結果寫入文字檔
27  write.table(da0_markers_1, 
28              paste0("1. Findmarkers_", cell_group , " vs 2D_20250625.txt"), 
29              sep = "\t",           # 使用 tab 作為分隔符
30              row.names = TRUE,    # 保留基因名稱作為列名
31              col.names = TRUE,    # 保留欄位名稱
32              quote = FALSE)       # 不對字串加上引號
33}
34
35# 清理不再需要的變數
36rm(da0_markers_1)

FindMarkers() 函數說明

  • 用途: 找出兩群細胞之間的差異表現基因。
  • 使用方式: FindMarkers(object, ident.1, ident.2, ...)
  • 重要參數:
    • object: Seurat 物件。
    • ident.1: 指定第一組細胞 (實驗組) 的身分。
    • ident.2: 指定第二組細胞 (對照組) 的身分。如果設為 NULL,則會與所有其他細胞進行比較。
    • only.pos: 布林值。如果為 TRUE,只回報在 ident.1 中表現量較高的基因 (上調基因)。預設為 FALSE
    • logfc.threshold: 篩選 log-fold change 的閾值,預設為 0.25。
    • min.pct: 基因必須在兩組細胞中至少其中一組的 min.pct 比例的細胞中被檢測到,預設為 0.1。

步驟二:找出各群組的標記基因 (Marker Genes) 與視覺化

在這個章節,我們將找出每個細胞群組 (2D 和 3D) 特有的標記基因,並使用熱圖 (Heatmap) 進行視覺化,以觀察基因表現的模式。

2.1 找出所有群組的 Marker Genes

我們使用 FindAllMarkers 函數,一次找出所有群組中,相對於其他所有群組的差異上調基因。

 1# 清理環境,重新載入必要的套件
 2rm(list = ls(all = T))
 3library(dplyr)
 4library(Seurat)
 5library(ggplot2)
 6library(patchwork)
 7library(clusterProfiler)
 8library(enrichplot)
 9library(org.Hs.eg.db) # 人類基因註解資料庫
10
11# 重新讀取與前處理資料 (此為重複步驟,在實際流程中可省略)
12setwd("C:/Users/TPOW31714/Desktop/20250224 Human microarray for CY_add cell/4. RDS/")
13da0 <- readRDS("2. GPL17586_12_sample_250625.rds", refhook = NULL)
14da0 <- NormalizeData(da0)
15da0 <- FindVariableFeatures(da0)
16da0 <- ScaleData(da0, features = rownames(da0))
17DefaultAssay(da0) <- 'RNA'
18Idents(da0) <- factor(da0$Group_1)
19
20# 核心步驟:使用 FindAllMarkers 找出各群組的標記基因
21# only.pos = TRUE 表示我們只關心在該群組中表現量顯著較高的基因
22da0_markers <- FindAllMarkers(da0, only.pos = TRUE)

FindAllMarkers() 函數說明

  • 用途: 對於 Seurat 物件中的每一個細胞群組,找出其相對於所有其他細胞群組的差異表現基因 (標記基因)。
  • 使用方式: FindAllMarkers(object, ...)
  • 重要參數:
    • object: Seurat 物件。
    • only.pos: 布林值。若為 TRUE,則只回報上調的基因。
    • logfc.threshold: log-fold change 的閾值。
    • min.pct: 最小細胞比例閾值。

2.2 篩選 Top 500 基因並繪製熱圖

為了讓熱圖更清晰,我們從每個群組的標記基因中,選出 avg_log2FC 最高的 500 個基因來進行繪製。

 1# 使用 dplyr 篩選每個 cluster 中 avg_log2FC 最高的 500 個基因
 2TOP_DEGs <- da0_markers %>%
 3  group_by(cluster) %>% # 根據 cluster (即 2D, 3D) 進行分組
 4  slice_max(order_by = avg_log2FC, n = 500, with_ties = FALSE) %>% # 選出 avg_log2FC 最大的前 500 個
 5  ungroup() # 取消分組
 6
 7# 使用 DoHeatmap 繪製熱圖
 8p1 <- DoHeatmap(da0, features = TOP_DEGs$gene) + NoLegend() # NoLegend() 移除圖例
 9p1 # 顯示圖形
10
11# 設定存圖目錄並儲存
12setwd("C:/Users/TPOW31714/Desktop/20250224 Human microarray for CY_add cell/3. Data/20250625 data_ORA/1. 3Dvs2D DEGs_12samples/RDS/")
13ggsave(file = "HEATMAP_12samples_DEG_TOP500_250625v1.png",
14       plot = p1, width = 9, height = 15, dpi = 300, limitsize = FALSE)
15
16# 清理變數
17rm(da0_markers, TOP_DEGs, p1)

DoHeatmap() 函數說明

  • 用途: 繪製基因表現量的熱圖。
  • 使用方式: DoHeatmap(object, features, ...)
  • 重要參數:
    • object: Seurat 物件。
    • features: 一個包含基因名稱的向量,指定要在熱圖上顯示的基因。
    • group.by: 指定用來分組細胞的元數據欄位,預設使用 Idents(object)
    • NoLegend(): 一個 ggplot2 的輔助函數,用來移除圖例。

步驟三:準備基因列表以進行 ORA 分析

ORA (Over-Representation Analysis) 是一種常見的功能富集分析方法。為了進行 ORA,我們需要準備一個包含 ENTREZ ID 的基因列表。

3.1 產生基因列表並轉換 ID

這個區塊的程式碼會:

  1. FindAllMarkers 的結果中,為 “2D” 和 “3D” 群組分別建立基因列表。
  2. 篩選出上調的基因 (avg_log2FC > 0)。
  3. 使用 clusterProfiler::bitr 將基因的 SYMBOL 轉換為 ENTREZ ID。
  4. 將結果整理成 compareCluster 所需的格式 (一個 named list)。
 1# 建立輸出目錄
 2dir.create("C:/Users/TPOW31714/Desktop/20250224 Human microarray for CY_add cell/3. Data/20250625 data_ORA/1. 3Dvs2D DEGs_12samples/RDS/", recursive = TRUE)
 3
 4# 重新執行 FindAllMarkers,這次包含下調基因 (only.pos = F)
 5da0_markers <- FindAllMarkers(da0, only.pos = FALSE)
 6
 7# 定義要處理的細胞群組
 8cell_groups <- c("2D", "3D")
 9# 初始化一個空的 list 來儲存結果
10results_list <- list()
11
12# 透過迴圈處理每個細胞群組
13for (cell_group in cell_groups) {
14  
15  # 1. 從總表中篩選出當前群組的 markers
16  markers <- da0_markers %>% filter(cluster == cell_group)
17  
18  # 2. 對 markers 進行處理
19  filtered_markers <- markers %>%
20    mutate(change = ifelse(avg_log2FC > 0, "UP", "DOWN")) %>% # 增加 UP/DOWN 標籤
21    arrange(desc(avg_log2FC)) # 根據 logFC 降序排列
22  
23  # 3. 將基因 SYMBOL 轉換為 ENTREZID
24  de <- filtered_markers$gene
25  ids <- bitr(de, 
26              fromType = "SYMBOL",   # 來源ID類型
27              toType = c("ENTREZID"),# 目標ID類型
28              OrgDb = "org.Hs.eg.db") # 指定物種為人類
29  
30  # 4. 將轉換後的 ID 加回資料框,並過濾掉無法轉換的基因
31  filtered_markers <- filtered_markers %>%
32    left_join(ids, by = c("gene" = "SYMBOL")) %>% # 將 ids 合併進來
33    filter(!is.na(ENTREZID)) # 移除沒有對應 ENTREZID 的基因
34  
35  # 5. 建立最終的基因列表 (只取上調基因)
36  gene_list <- filtered_markers %>%
37    filter(avg_log2FC > 0) %>%
38    arrange(desc(avg_log2FC)) %>%
39    head(500) %>% # 使用前500上調基因
40    dplyr::select(ENTREZID, avg_log2FC)
41  
42  # 6. 建立 ORA 所需的 "named vector"
43  gene_list_values <- gene_list$avg_log2FC
44  names(gene_list_values) <- gene_list$ENTREZID
45  
46  # 7. 將結果存入 results_list
47  results_list[[cell_group]] <- gene_list_values
48}
49
50# 將準備好的基因列表儲存為 RDS 檔案
51setwd("C:/Users/TPOW31714/Desktop/20250224 Human microarray for CY_add cell/3. Data/20250625 data_ORA/1. 3Dvs2D DEGs_12samples/RDS/")
52saveRDS(results_list, file = "1. Combined_list_12samples_DEGs_POS_TOP500_250625.rds")

bitr() 函數說明

  • 用途: bitr (biological ID translator) 是 clusterProfiler 中的一個非常有用的函數,專門用來轉換不同類型的生物 ID。
  • 使用方式: bitr(geneID, fromType, toType, OrgDb, ...)
  • 重要參數:
    • geneID: 一個包含基因 ID 的向量。
    • fromType: 來源 ID 的類型,例如 “SYMBOL”, “ENTREZID”, “ENSEMBL”。
    • toType: 目標 ID 的類型。
    • OrgDb: 指定物種的註解資料庫,例如 org.Hs.eg.db (人類) 或 org.Mm.eg.db (小鼠)。

步驟四:執行 GO 富集分析與視覺化

有了基因列表後,我們就可以使用 clusterProfiler 進行功能富集分析了。

4.1 執行 compareCluster

compareCluster 函數可以同時對多個基因列表進行富集分析,非常適合用來比較不同細胞群組的功能差異。

 1# 清理環境並載入套件
 2rm(list = ls(all = T))
 3library(dplyr)
 4library(ggplot2)
 5library(clusterProfiler)
 6library(enrichplot)
 7library(org.Hs.eg.db)
 8
 9# 設定工作目錄並讀取基因列表
10setwd("C:/Users/TPOW31714/Desktop/20250224 Human microarray for CY_add cell/3. Data/20250625 data_ORA/1. 3Dvs2D DEGs_12samples/RDS/")
11markers_list <- readRDS("1. Combined_list_12samples_DEGs_POS_TOP500_250625.rds", refhook = NULL)
12
13# 將 list 中的 named vector 轉換成只包含基因 ID 的 character vector
14markers_list_converted <- lapply(markers_list, function(x) names(x))
15str(markers_list_converted) # 檢查轉換後的格式
16
17# 核心步驟:執行 GO 富集分析
18ora.out <- compareCluster(geneClusters = markers_list_converted,
19                          fun = "enrichGO",         # 指定使用 GO 分析
20                          OrgDb = org.Hs.eg.db,     # 指定物種為人類
21                          ont = "BP",               # 指定 GO 的本體 (BP, MF, CC, ALL)
22                          pvalueCutoff = 0.05,      # p-value 閾值
23                          pAdjustMethod = "none")   # p-value 校正方法
24
25# 儲存分析結果
26setwd("C:/Users/TPOW31714/Desktop/20250224 Human microarray for CY_add cell/3. Data/20250625 data_ORA/1. 3Dvs2D DEGs_12samples/RDS/")
27saveRDS(ora.out, file = "1-2. GOBP_compare_12samples_DEGs_POS_TOP500_250625.rds")

compareCluster() 函數說明

  • 用途: 對一個包含多個基因列表的 list 進行功能富集分析的比較。
  • 使用方式: compareCluster(geneClusters, fun, ...)
  • 重要參數:
    • geneClusters: 一個 list,其中每個元素都是一個包含基因 ID 的向量。listnames 會被用作 Cluster 的名稱。
    • fun: 指定要使用的富集分析函數,例如 “enrichGO”, “enrichKEGG”, “enrichDO”。
    • OrgDb, ont, pvalueCutoff 等參數會被傳遞給 fun 所指定的函數。

4.2 結果視覺化與匯出

分析完成後,我們需要將結果視覺化並匯出成容易閱讀的格式。

 1# ... (此處省略重複的 rm 和 library 載入) ...
 2# 讀取 ORA 結果
 3xx <- readRDS("1-2. GOBP_compare_12samples_DEGs_POS_TOP500_250625.rds", refhook = NULL)
 4
 5# 1. 繪製點圖 (Dot Plot)
 6p1 <- dotplot(xx, showCategory = 10, label_format = 80)
 7p1 <- p1 + xlab("") + ylab("") + theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1))
 8p1
 9# 存檔
10ggsave("Dotplot_GOBP_TOP10_ORA_DEGs_POS_TOP500_250625.png", p1, width = 12, height = 16, dpi = 600)
11
12# 2. 將基因 ID 轉回可讀的 Symbol 並匯出文字檔
13xx2 <- setReadable(xx, 'org.Hs.eg.db', 'ENTREZID')
14write.table(xx2@compareClusterResult, "1-2. GOBP_compare_12samples_DEGs_POS_TOP500_250625.txt", sep = "\t", row.names = T, col.names = T, quote = F)
15
16# 3. 繪製 Emapplot (Enrichment Map)
17#    Emapplot 可以將功能相似的 GO terms 聚合在一起,形成網絡圖
18#    首先需要計算 GO terms 之間的相似性
19xx2_sim <- enrichplot::pairwise_termsim(xx2)
20
21# 篩選出特定群組 (例如 "2D") 的結果來繪圖
22Three_Dim_result <- xx2_sim
23Three_Dim_result@compareClusterResult <- Three_Dim_result@compareClusterResult %>%
24                                         filter(Cluster == "2D")
25
26# 當結果只剩一個群組時,可以像處理 enrichResult 物件一樣繪圖
27p_emap <- emapplot(Three_Dim_result, layout = "kk", cex_category = 1.5)
28p_emap

enrichplot 視覺化函數

  • dotplot():
    • 用途: 繪製點圖,X 軸通常是 GeneRatio 或 -log10(p.adjust),Y 軸是 GO term,點的大小代表基因數量,顏色代表 p-value。
    • showCategory: 顯示的 GO term 數量。
  • emapplot():
    • 用途: 繪製網絡圖,每個節點是一個 GO term,節點之間的連線代表它們共享的基因數量。功能相似的 term 會被聚集在一起。
    • 需要先用 pairwise_termsim() 計算相似性矩陣。
  • setReadable():
    • 用途: 將富集結果中的基因 ID (如 ENTREZID) 轉換回更容易閱讀的基因名稱 (SYMBOL)。

4.3 分組繪製 Dotplot 並匯出 Excel

為了更清楚地比較不同群組的結果,我們可以分別為 “2D” 和 “3D” 群組繪製點圖,並將詳細數據匯出到 Excel 的不同工作表中。

 1# ... (省略 rm 和 library) ...
 2library(readxl)
 3library(openxlsx)
 4library(scales)
 5
 6cell_groups <- c("2D", "3D")
 7plots_list <- list()
 8data_list <- list()
 9
10# 讀取 enrichGO 分析結果 (這裡假設已將 txt 轉存為 Excel)
11# 注意:原腳本此處讀取 Excel,若無此檔案會報錯。
12# 我們直接使用上一步的 R 物件 `xx`
13go_analysis_1 <- xx@compareClusterResult
14
15for (cell_group in cell_groups) {
16  
17  # 篩選出當前群組的結果
18  go_analysis_2 <- go_analysis_1 %>%
19    filter(Cluster == cell_group) %>%
20    arrange(p.adjust) %>%
21    mutate(Description = factor(Description, levels = rev(unique(Description))))
22  
23  # 繪製點圖
24  p1 <- ggplot(go_analysis_2, aes(x = -log10(p.adjust), y = Description)) +
25    geom_point(aes(size = Count, color = p.adjust)) +
26    theme_bw() +
27    scale_color_gradient(low = "red", high = "blue") +
28    labs(title = paste("GO BP Enrichment for", cell_group),
29         x = "-log10(p.adjust)", y = "")
30  
31  plots_list[[cell_group]] <- p1
32  data_list[[cell_group]] <- go_analysis_2
33  
34  # 顯示圖形
35  print(p1)
36}
37
38# 將各群組結果寫入同一個 Excel 檔案的不同工作表
39output_file <- "C:/Users/TPOW31714/Desktop/20250224 Human microarray for CY_add cell/3. Data/20250625 data_ORA/1. 3Dvs2D DEGs_12samples/RDS/1. Origianl GO_DEGs_POS/POS_results.xlsx"
40wb <- createWorkbook()
41for (cell_group in names(data_list)) {
42  addWorksheet(wb, sheetName = cell_group)
43  writeData(wb, sheet = cell_group, x = data_list[[cell_group]])
44}
45saveWorkbook(wb, file = output_file, overwrite = TRUE)

步驟五:簡化 GO 結果 (Simplify)

GO 資料庫中的條目 (terms) 存在階層關係,導致富集分析的結果中常有很多語義上相似或冗餘的條目。simplify 函數可以幫助我們去除這些冗餘,讓結果更聚焦。

5.1 執行 simplify

我們的策略是:

  1. 找出在所有群組中都顯著的 “共同” pathway。
  2. 對 “非共同” 的 pathway 進行 simplify
  3. 將 “共同” pathway 與簡化後的 “非共同” pathway 合併。
 1# ... (省略 rm 和 library) ...
 2# 讀取 ORA 結果
 3xx <- readRDS("1-2. GOBP_compare_12samples_DEGs_POS_TOP500_250625.rds", refhook = NULL)
 4
 5# 1. 取得資料框
 6df <- xx@compareClusterResult
 7
 8# 2. 找出共同的 pathway ID
 9all_clusters <- unique(df$Cluster)
10num_clusters <- length(all_clusters)
11common_ids <- df %>%
12  group_by(ID) %>%
13  summarise(n = n_distinct(Cluster)) %>%
14  filter(n == num_clusters) %>%
15  pull(ID)
16
17# 3. 分割資料
18df_common <- df %>% filter(ID %in% common_ids)
19df_noncommon <- df %>% filter(!ID %in% common_ids)
20
21# 4. 對非共同部分進行 simplify
22xx_noncommon <- xx
23xx_noncommon@compareClusterResult <- df_noncommon
24xx_noncommon_simplified <- simplify(xx_noncommon,
25                                    cutoff = 0.7, # 相似度閾值
26                                    by = "p.adjust", # 根據 p.adjust 選擇代表性 term
27                                    select_fun = min,
28                                    measure = "Wang") # 計算語義相似度的方法
29
30# 5. 合併結果
31df_combined <- rbind(xx_noncommon_simplified@compareClusterResult, df_common)
32xx_combined <- xx
33xx_combined@compareClusterResult <- df_combined
34
35# 儲存簡化後的結果
36saveRDS(xx_combined, file = "1-3. GOBP_SIMPLIFY_12samples_DEGs_POS_TOP500_250625.rds")

simplify() 函數說明

  • 用途: 去除 enrichGO 結果中的冗餘 GO terms。
  • 使用方式: simplify(x, cutoff, by, select_fun, measure)
  • 重要參數:
    • x: enrichResultcompareClusterResult 物件。
    • cutoff: 數值,介於 0 到 1 之間。語義相似度高於此閾值的 terms 將被視為冗餘。
    • by: 用來選擇哪個 term 作為代表的指標,通常是 p.adjust
    • select_fun: 一個函數,用來從一組相似的 terms 中挑選代表,例如 min (挑選 by 指標最小的)。
    • measure: 計算 GO terms 之間語義相似度的方法,常用的有 “Wang”, “Resnik”, “Lin”, “Jiang”, “Rel”。

5.2 視覺化簡化後的結果

最後,我們可以像之前一樣,對簡化後的結果進行視覺化與匯出。

1# 讀取簡化後的結果
2xx_simplified <- readRDS("1-3. GOBP_SIMPLIFY_12samples_DEGs_POS_TOP500_250625.rds", refhook = NULL)
3
4# 繪製點圖
5p_simplified <- dotplot(xx_simplified, showCategory = 10, label_format = 80)
6p_simplified
7
8# 同樣可以分組繪製 Emapplot 或匯出成 Excel
9# ... (程式碼與步驟 4.2, 4.3 類似,此處省略) ...

結論

透過本教學,您學會了:

  • 如何使用 Seurat 進行基本的單細胞數據前處理與差異基因分析。
  • 如何將基因 ID 進行轉換,以及如何簡化富集分析的結果,使其更具可讀性。
  • 如何使用 clusterProfiler 進行 GO 富集分析,並比較不同群組間的功能差異。
  • 如何使用 ggplot2enrichplot 進行高品質的視覺化。

希望這份教學對您的研究有所幫助!