r - Convert a graphNEL graph to a network graph -
i'm trying convert graphnel
graph network
graph.
here's 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) topgo.graph <- attr(topgo.obj,"graph")
and trying convert topgo.graph
network through intergraph
library(network) library(sna) library(scales) library(igraph) library(intergraph) topgo.igraph <- graph_from_graphnel(topgo.graph,name=true,weight=true,unlist.attrs=true)
and finally
topgo.network <- asnetwork(topgo.igraph,amap=attrmap())
throws error:
error in as.data.frame.default(x[[i]], optional = true) : cannot coerce class ""environment"" data.frame
when try intergraph
's example:
asnetwork(exigraph)
it works fine , far can tell exigraph
, topgo.igraph similar:
> exigraph igraph d--- 15 11 -- + attr: label (v/c), label (e/c) + edges: [1] 2-> 1 3-> 1 4-> 1 5-> 1 6-> 7 8-> 9 10->11 11->12 12->13 13->14 14->12 > topgo.igraph igraph dnw- 1017 2275 -- + attr: name (v/c), genes (v/x), weight (e/n) + edges (vertex names): [1] go:0000003->go:0008150 go:0000070->go:0000278 go:0000070->go:0007067 go:0000070->go:1903047 go:0000070->go:0000819 [6] go:0000075->go:0022402 go:0000077->go:0031570 go:0000077->go:0006974 go:0000079->go:1904029 go:0000079->go:0071900 [11] go:0000082->go:0044772 go:0000082->go:0044843 go:0000086->go:0000278 go:0000086->go:0044772 go:0000086->go:0044839 [16] go:0000122->go:0006357 go:0000122->go:0045892 go:0000122->go:0006366 go:0000165->go:0035556 go:0000165->go:0023014 [21] go:0000187->go:0032147 go:0000187->go:0043406 go:0000209->go:0016567 go:0000226->go:1902589 go:0000226->go:0007010 [26] go:0000226->go:0007017 go:0000278->go:0007049 go:0000280->go:0048285 go:0000302->go:0006979 go:0000302->go:1901700 [31] go:0000723->go:0006259 go:0000723->go:0032200 go:0000723->go:0060249 go:0000819->go:1902589 go:0000819->go:0098813 [36] go:0000819->go:0051276 go:0000902->go:0032989 go:0000910->go:0022402 go:0000910->go:0051301 go:0001501->go:0048731 + ... omitted several edges
any idea?
this happening because of "gene" attribute. if view using v(topgo.igraph)$gene
, see return list of environments rather vector. when deep in intergraph
code, tries coerce vertex attributes data frame, cannot do. (this happens in dumpattr()
function -- see getanywhere(dumpattr.igraph)
.)
to solve this, can simple delete attribute:
topgo.igraph <- delete_vertex_attr(topgo.igraph,"genes") topgo.network <- asnetwork(topgo.igraph,amap=attrmap())
the argument unlist.attrs=t
think designed prevent exact problem above, not working in case. might due naming convention used genes in network.
if @ attributes original graphnel object, notice 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 alter attribute data in original topgo object solve problem 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 genes vertex attributes, if want that:
> head(network::get.vertex.attribute(topgo.network, "genes")) [1] "go:0000003" "go:0000070" "go:0000075" "go:0000077" "go:0000079" "go:0000082"
Comments
Post a Comment