将网络数据从 R 传输到 Gephi

Transfering network data from R to Gephi

我使用 igraph、hmisc 和矩阵包在 R 中生成了一个基于微生物丰度相关性的微生物网络。现在我想在 Gephi 中处理它。为此,我必须将我的数据传输到 Gephi。我试图从这些数据中准备 CSV 文件,但发现了这个错误:

错误 write_delim(x, path, delim = ",", na = na, append = append, col_names = col_names, : is.data.frame(x) 不正确

这是我的完整代码:

library(igraph)
library(Hmisc)
library(Matrix)
library(writexl)

otu.table <- read.csv(file.choose(), header = T, row.names = 1)
tax <- read.csv(file.choose(), header = T, row.names = 1)

dim(otu.table)
otu.table.filter <- otu.table[ ,colSums(otu.table) >= 0.1]
dim(tax)
otu.cor <- rcorr(as.matrix(otu.table), type="spearman", )

otu.pval <- forceSymmetric(otu.cor$P)
sel.tax <- tax[rownames(otu.pval),,drop=FALSE]
all.equal(rownames(sel.tax), rownames(otu.pval))
p.yes <- otu.pval<0.001

r.val = otu.cor$r>0.8 # select all the correlation values 
p.yes.r <- r.val*p.yes
p.yes.r <- abs(p.yes.r)>0.8 # output is logical vector
p.yes.rr <- p.yes.r*r.val 
adjm <- as.matrix(p.yes.rr)
colnames(adjm) <- as.vector(sel.tax$Phylum)
rownames(adjm) <- as.vector(sel.tax$Phylum)

net.grph=graph.adjacency(adjm,mode="undirected",weighted=TRUE,diag=FALSE)
edgew<-E(net.grph)$weight
V(net.grph)$color <- tax$Phylum
bad.vs<-V(net.grph)[degree(net.grph) == 0] 

net.grph <-delete.vertices(net.grph, bad.vs)

plot(net.grph,
 vertex.size=8,
 vertex.frame.color="black",
 edge.curved=F,
 edge.width=edgew,
 layout=layout.fruchterman.reingold,
 edge.color=ifelse(edgew > 1,"red","blue"),
 vertex.label=NA,
 vertex.label.color="black",
 vertex.label.family="Times New Roman",
 vertex.label.font=0.1)

 write_csv(net.graph,"Documents\R Analysis\mydata.xlsx")

请告诉我如何将这些数据传输到 Gephi?

  1. 你应该考虑 Cytoscape,它比 Gephi 更有效(例如,Cytoscape 在导入大型网络时提示你:"this is a very huge network, are you sure you want to plot that ?" vs. Gephi made您的计算机在同一网络中冻结和崩溃)。

  2. 为了在程序中导入您的网络,您必须在 csv 或 xls 文件中编写边缘列表和(最终)节点列表。从您的 R 环境中的 Igraph objet,将您的图形导出到 data.frame,其中:igraph::as_edgelist(some_igraph_network_objet, names = T),其中 return "a standard representation of a graph" 又名 edges-list(参见 https://igraph.org/r/doc/as_edgelist.html)。然后将其写入您的机器,使用一些 write.csv 或一些 excel 文件(例如,xlsx::write.xlsx)。如果需要,nodeslist 的想法相同。

  3. 在您最喜欢的网络分析程序中导入 'edges-list' 或 'nodes-list'(Cytoscape 只需要一些指示即可从 excel 边导入图形-list,并向您显示一些数据的弹出窗口:您会在导入前看到数据样本)。您可以在 cytoscape 或 Gephi 中将图形作为边列表(csv 或 xls)导入(例如,tutorial to import edges-list.csv

PS:如果我错过了'convert from igraph to Gephi'的目标,根据这个website, "Gephi has its own package, the rgexf package for R, that provides some support for creating Gephi styled graphs out of custom matrix/dataframe data" (you should take care of two columns, needed for each of your nodeslist and your edgeslist). This seems experimental but maybe useful in your case, see here and here