在R中为SpiecEasi网络顶点分配元数据的技术问询
解决SpiecEasi网络添加Substrata元数据并高亮展示的方案
步骤1:提取Substrata元数据
根据你的TreeSE对象结构,先把样本层面的substrata信息提取出来;如果是微生物共现网络,需要把substrata信息关联到每个微生物类群(比如按类群在不同substrata中的丰度主导性标记)。
# 从TreeSE提取样本元数据 sample_meta <- as.data.frame(colData(你的TreeSE对象名)) substrata_col <- sample_meta$substrata # 替换为你的substrata列名 names(substrata_col) <- rownames(sample_meta) # 如果是微生物共现网络(顶点为属水平类群),计算每个类群的主导substrata library(mia) # 用你已聚合好的属水平TreeSE对象 mean_abund <- assay(你的属水平TreeSE对象, "relabundance") %>% t() %>% as.data.frame() %>% cbind(substrata = substrata_col) %>% dplyr::group_by(substrata) %>% dplyr::summarise(dplyr::across(everything(), mean)) %>% tibble::column_to_rownames("substrata") %>% t() %>% as.data.frame() # 为每个类群分配丰度最高的substrata标签 microbe_substrata <- apply(mean_abund, 1, function(x) names(x)[which.max(x)]) names(microbe_substrata) <- rownames(mean_abund)
步骤2:将SpiecEasi结果转为igraph并添加顶点属性
把SpiecEasi的拟合结果转成igraph对象,再把substrata信息绑定到顶点属性上:
library(igraph) library(SpiecEasi) # 从SpiecEasi结果中提取拟合好的邻接矩阵并转成igraph se_igraph <- adj2igraph(getRefit(你的SpiecEasi结果对象)) # 绑定属性:分两种场景 # 场景1:网络顶点为样本(样本共现网络) V(se_igraph)$substrata <- substrata_col[V(se_igraph)$name] # 场景2:网络顶点为微生物类群(共现网络) V(se_igraph)$substrata <- microbe_substrata[V(se_igraph)$name]
步骤3:高亮展示Substrata信息
用igraph绘图时,根据substrata属性给顶点着色,实现高亮:
# 定义颜色映射 substrata_types <- unique(V(se_igraph)$substrata) color_pal <- RColorBrewer::brewer.pal(length(substrata_types), "Set2") names(color_pal) <- substrata_types # 绘制网络 plot(se_igraph, vertex.color = color_pal[V(se_igraph)$substrata], vertex.label = NA, # 顶点过多时可隐藏标签 vertex.size = 7, edge.width = 0.4, main = "Microbial Network Colored by Substrata") # 添加图例 legend("topright", legend = substrata_types, fill = color_pal, title = "Substrata", cex = 0.8)
额外:验证聚类与Substrata的关联及桥接物种分析
# 用Louvain算法识别网络聚类 se_communities <- cluster_louvain(se_igraph) # 卡方检验聚类与substrata的相关性 cluster_sub_df <- data.frame( cluster = membership(se_communities), substrata = V(se_igraph)$substrata ) chisq.test(table(cluster_sub_df$cluster, cluster_sub_df$substrata)) # 筛选桥接物种(基于betweenness中心性) V(se_igraph)$betweenness <- betweenness(se_igraph) top_bridge_species <- V(se_igraph)[order(V(se_igraph)$betweenness, decreasing = T)[1:10]] # 查看桥接物种的substrata属性 data.frame( species = names(top_bridge_species), substrata = top_bridge_species$substrata, betweenness_score = top_bridge_species$betweenness )
内容的提问来源于stack exchange,提问作者Jan
相关产品推荐
相关产品推荐

