Search code examples
rigraphgephinetwork-analysiswritexl

Transfering network data from R to Gephi


I have generated a microbial network based on microbial abundance correlation in R using igraph, hmisc and matrix packages. Now i want to work on it in Gephi. For that, I have to transfer my data to Gephi. I tried to prepare CSV file form this data but found this error:

Error in write_delim(x, path, delim = ",", na = na, append = append, col_names = col_names, : is.data.frame(x) is not TRUE

Here is my complete code:

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")

Kindly tell me how do I transfer this data to Gephi?


Solution

    1. You should consider Cytoscape, which is far more efficient than Gephi (e.g., Cytoscape prompt you at the import of a big network with: "this is a very huge network, are you sure you want to plot that ?" vs. Gephi made you computer to freeze and crash with the same network).

    2. In order to import your network in a program, you have to write the edges-list and (eventually) nodes-list on a csv or xls file. From an Igraph objet in you R environment, export your graph to a data.frame with: igraph::as_edgelist(some_igraph_network_objet, names = T), which return "a standard representation of a graph" aka edges-list (see https://igraph.org/r/doc/as_edgelist.html). Then write it on your machine, with some write.csv or some excel files (e.g., xlsx::write.xlsx). Same idea for nodeslist, if needed.

    3. import that 'edges-list' or 'nodes-list' in your favorite network-analysis-program (Cytoscape only need a few indications for importing a graph from an excel edges-list, and show you some pop-up of your data: you see a sample of your data before importing). You could import a graph as an edges-list (csv or xls), in both cytoscape or in Gephi (e.g., tutorial to import edges-list.csv)

    PS: If I miss the goal of 'convert from igraph to Gephi', according to this 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.