dan dan - 16 days ago 6
R Question

Convert graphNEL graph to a network graph

I'm trying to convert a

graphNEL
graph to
network
graph.

Here's my example using
topGO
's
vignette
:

library(topGO)
library(ALL)
data(ALL)
data(geneList)
affyLib <- paste(annotation(ALL),"db",sep= ".")
library(package=affyLib,character.only=TRUE)
topgo.obj <- new("topGOdata",description="Simple session",ontology="BP",allGenes=geneList,geneSel=topDiffGenes,nodeSize=10,annot=annFUN.db,affyLib=affyLib)
res.fisher <- runTest(topgo.obj,algorithm="classic",statistic="fisher")
res.df <- GenTable(topgo.obj,classicFisher=res.fisher,orderBy="classicFisher",topNodes=length(score(res.fisher)))


library(network)
library(sna)
library(scales)
library(igraph)
library(intergraph)

topgo.igraph <- graph_from_graphnel(topgo.graph,name=TRUE,weight=TRUE,unlist.attrs=TRUE)
topgo.network <- asNetwork(topgo.igraph,amap=attrmap())
ggnet2(net=topgo.network,size=10,arrow.size=12,arrow.gap=0.025)

Answer

This is happening because of the "gene" attribute. If you view it using V(topgo.igraph)$gene, you will see it return a list of environments rather than a vector. When deep in the intergraph code, it tries to coerce the vertex attributes into a data frame, which it cannot do. (This happens in the dumpAttr() function -- see getAnywhere(dumpAttr.igraph).)

To solve this, you can simple delete the attribute:

topgo.igraph <- delete_vertex_attr(topgo.igraph,"genes")
topgo.network <- asNetwork(topgo.igraph,amap=attrmap())

The argument unlist.attrs=T I think is designed to prevent the exact problem above, but it is not working in this case. This might be due to the naming convention used by the genes in the network.

If you look at the attributes from the original graphNEL object, you will notice that it consists of objects of class environment:

> head(graph::nodeData(topgo.graph, attr = "genes"))
$`GO:0000003`
<environment: 0x15c005ae0>

$`GO:0000070`
<environment: 0x15c136bf0>

$`GO:0000075`
<environment: 0x15c118a70>

$`GO:0000077`
<environment: 0x15c13ae70>

$`GO:0000079`
<environment: 0x163145670>

$`GO:0000082`
<environment: 0x16313d148>)

You could also alter the attribute data in the original topGO object to solve the problem as well:

nodeData(topgo.graph, attr = "genes") <- topgo.obj@graph@nodes
topgo.igraph <- graph_from_graphnel(topgo.graph,name=TRUE,weight=TRUE,unlist.attrs=TRUE)
topgo.network <- asNetwork(topgo.igraph,amap=attrmap())

This preserves the genes a vertex attributes, if you want that:

> head(network::get.vertex.attribute(topgo.network, "genes"))
[1] "GO:0000003" "GO:0000070" "GO:0000075" "GO:0000077" "GO:0000079" "GO:0000082"
Comments