分享

ggCLusterNet更新-模块相似性与模块互联

 微生信生物 2024-01-22 发布于北京

写在前面

之前好多人咨询上图的做法,这个有点麻烦,因为要在不同的facet之间进行绘图,所以用到了grid,当时我就自己用了一下,没有放到ggClusterNet里面,昨天到今天我重新写了一下,从此开始多网络比对,facet之间绘图的新篇章;还是要提醒大家一下,使用下面代码之前先更新ggClusterNwet包;还有就是这个功能只是初步功能,我应该会在接下来的一个月内进行大量的更新;



install.packages("BiocManager")library(BiocManager)install("remotes")install("tidyverse")install("tidyfst")install("igraph")install("sna")install("phyloseq")install("ggalluvial")install("ggraph")install("WGCNA")install("ggnewscale")install("pulsar")install("patchwork")remotes::install_github("taowenmicro/EasyStat")remotes::install_github("taowenmicro/ggClusterNet")


实战

#--更新流程,自由度更高的网络分析流程
library(tidyverse)
library(ggClusterNet)
library(phyloseq)
library(igraph)
library("ggalt")
library(ggnewscale)
library(gtable)
library(grid)

network.pip函数计算网络

#方法计算相关矩阵#----
tab.r = network.pip(
ps = ps16s,
N = 500,
# ra = 0.05,
big = TRUE,
select_layout = FALSE,
layout_net = "model_maptree2",
r.threshold = 0.6,
p.threshold = 0.05,
maxnode = 2,
method = "spearman",
label = FALSE,
lab = "elements",
group = "Group",
fill = "Phylum",
size = "igraph.degree",
zipi = TRUE,
ram.net = TRUE,
clu_method = "cluster_fast_greedy",
step = 100,
R=10,
ncpus = 1
)

# 从nerwork.pip中提出来的节点和矩阵#-------
dat = tab.r[[2]]
node = dat$net.cor.matrix$node
edge = dat$net.cor.matrix$edge

cortab = dat$net.cor.matrix$cortab
library(tidyfst)
library(grid)

计算模块及其相似模块

res1 = module.compare.m(
ps = NULL,
corg = cortab,
zipi = FALSE,
zoom = 0.2,
padj = F,
n = 3)

#不同分组使用一个圆圈展示,圆圈内一个点代表一个模块,相连接的模块代表了相似的模块。p1 = res1[[1]]
# p1
#--提取模块的OTU,分组等的对应信息
dat1 = res1[[2]]
head(dat1)
colnames(dat1)[2] = "group.module"
#模块相似度结果表格
dat2 = res1[[3]]
head(dat2)
dat2$module2 = row.names(dat2) %>% strsplit("[-]") %>%
sapply(`[`, 2)
dat2$module1 = row.names(dat2) %>% strsplit("[-]") %>%
sapply(`[`, 1)
dat2$m1 = dat2$module1 %>% strsplit("model") %>%
sapply(`[`, 1)
dat2$m2 = dat2$module2 %>% strsplit("model") %>%
sapply(`[`, 1)
dat2$cross = paste(dat2$m1,dat2$m2,sep = "_Vs_")
# head(dat2)
dat2 = dat2 %>% filter(module1 != "none")

相似模块合并

# 相似模块合并#-------
id = dat2$module1 %>% unique()
for (i in 1:length(id)) {
tem1 = dat2 %>% filter(module1 == id[i]) %>% filter(p_adj < 0.05) %>% .$module2
tem2 = c(id[i],tem1)
tem3 = data.frame(ID = tem2,class = paste0("sameM.",i))

if (i == 1) {
tem4 = tem3
} else{
tem4 = rbind(tem4,tem3)
}
}

tem4$ID %>%
unique() %>%
length()

id = tem4$ID %>% table() %>% as.data.frame() %>%
arrange(desc(Freq)) %>%
filter(Freq > 1) %>%
.$`.` %>% as.character()

for (i in 1:length(id)) {
tem5 = tem4 %>% filter(ID == id[i]) %>%.$class
print( tem5)

sid = tem5[str_detect(tem5,"MNEW")]
if (length(sid)> 0) {
for (ii in 1:length(tem5)) {
tem4$class = gsub(tem5[ii],sid,tem4$class)
}
} else{
for (ii in 1:length(tem5)) {
tem4$class = gsub(tem5[ii],paste0("MNEW",i),tem4$class)
}
}
tem4 = tem4 %>% distinct(ID,class,.keep_all = TRUE)
}

tem4$class %>% unique()

节点信息和模块信息合并

node3 = node %>% left_join(dat1,by = c("ID","Group"))
head(node3)

node4 = node3 %>% left_join(tem4,by = c("group.module" = "ID"))
head(node4)
node4 = add.id.facet(node4,"Group")
head(node4)
tt.1 = node4$group.module %>% table() %>% as.data.frame() %>% arrange(desc(Freq)) %>%.$`.` %>%
head(12) %>% as.character()

node4$label = factor(node4$label,levels =as.character(unique( node4$label)[1:3]))
node4$Group = factor(node4$Group,levels = node4$Group %>% unique())

# p <- ggplot() + geom_segment(aes(x = X1, y = Y1, xend = X2, yend = Y2,color = cor
# ),
# data = edge, size = 0.03,alpha = 0.3) +
# geom_point(aes(X1, X2,
# fill = class,
# size = igraph.degree),
# pch = 21, data = node4,color = "gray40") +
# ggnewscale::new_scale_fill() +
# geom_encircle(aes(X1, X2,group = group.module,fill = group.module),
# linetype = 2,alpha = 0.1, data = node4 %>% filter(group.module %in%tt.1),
# s_shape = 1, expand=0
# ) +
# facet_wrap(.~ Group,scales="free_y",nrow = 1) +
# # geom_text_repel(aes(X1, X2,label = elements),pch = 21, data = nodeG) +
# # geom_text(aes(X1, X2,label = ID),pch = 21, data = node4,size = 2) +
# scale_colour_manual(values = c("#6D98B5","#D48852")) +
# # scale_fill_hue()+
# scale_size(range = c(0.8, 5)) +
# scale_x_continuous(breaks = NULL) +
# scale_y_continuous(breaks = NULL) +
# theme(panel.background = element_blank(),
# plot.title = element_text(hjust = 0.5)
# ) +
# theme(axis.title.x = element_blank(),
# axis.title.y = element_blank()
# ) +
# theme(legend.background = element_rect(colour = NA)) +
# theme(panel.background = element_rect(fill = "white", colour = NA)) +
# theme(panel.grid.minor = element_blank(), panel.grid.major = element_blank())

p <- ggplot() + geom_segment(aes(x = X1, y = Y1, xend = X2, yend = Y2,color = cor
),
data = edge, size = 0.03,alpha = 0.3) +
geom_point(aes(X1, X2,
fill = class,
size = igraph.degree),
pch = 21, data = node4,color = "gray40") +
ggnewscale::new_scale_fill() +
geom_encircle(aes(X1, X2,group = group.module,fill = group.module),
linetype = 2,alpha = 0.1, data = node4 %>% filter(group.module %in%tt.1),
expand = 0.03
) +
facet_wrap(.~ Group,scales="free_y",nrow = 1) +
# geom_text_repel(aes(X1, X2,label = elements),pch = 21, data = nodeG) +
# geom_text(aes(X1, X2,label = ID),pch = 21, data = node4,size = 2) +
scale_colour_manual(values = c("#6D98B5","#D48852")) +
# scale_fill_hue()+
scale_size(range = c(0.8, 5)) +
scale_x_continuous(breaks = NULL) +
scale_y_continuous(breaks = NULL) +
theme(panel.background = element_blank(),
plot.title = element_text(hjust = 0.5)
) +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank()
) +
theme(legend.background = element_rect(colour = NA)) +
theme(panel.background = element_rect(fill = "white", colour = NA)) +
theme(panel.grid.minor = element_blank(), panel.grid.major = element_blank())

ggsave("cs0.pdf",p,width = 20,height = 6)

g = p
id = node4$class %>% unique()
id = id[!is.na(id)]
for (j in 1:length(id)) {

node4 %>% filter(class == id[j]) %>% distinct(class,group,.keep_all = TRUE)
iid = node4 %>% filter(class == id[j]) %>% distinct(class,group,.keep_all = TRUE) %>% .$id.facet



iid2 = iid %>% strsplit( "[_]") %>% sapply(`[`, 2) %>%
as.numeric()

iid3 = iid %>% strsplit( "[_]") %>% sapply(`[`, 1)
# iid3 = c("Group3","Group1")
iid3 = levels(node4$Group) [levels(node4$Group) %in% iid3]
iid4 = (1:length(levels(node4$Group)))[levels(node4$Group) %in% iid3]
tab = data.frame(Group = iid3,facet = iid4)
tab2 = tab$facet[tab$Group%in%c(node4 %>% filter(class == id[j]) %>% distinct(class,group,.keep_all = TRUE) %>% .$group)]


tem = data.frame(ID = c(0,iid2[1:(length(iid2)-1)]),ID2 = iid2,facet =c(0, tab2[1:(length(tab2)-1)]),tab2)
tem2 = tem [-1,]
tem2


for (i in 1:dim(tem2)[1]) {
g <- line.across.facets.network(g, from=tem2[i,3], to=tem2[i,4], from_point_id=tem2[i,1], to_point_id=tem2[i,2])

}
}

# grid.draw(g)

ggsave("cs12.pdf",g,width = 20,height = 6)

根际互作生物学研究室 简介

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约