How to convert graphNEL object into Adjacency Matrix?
2
0
Entering edit mode
urwaCFC • 0
@urwacfc-12905
Last seen 6.6 years ago
Pakistan/Lahore/UETLahore

This is my graphNEL object

>mergedPathways_d
A graphNEL graph with undirected edges
Number of Nodes = 41
Number of Edges = 102

I have tried coerce. It did not work out. It asks to pre-define the matrix and To and From didnot work correctly, I had NA as entries in my adj matrix

I tried using edgeL. It did not work out. The edge list I got had numbers as edges and not the names of the genes. And I don't know how to map genes onto those numbers.

I tried converting it into igraph using igraph.from.graphNEL and then using as_adjacency_matrix. It did not work out. I got a dgCMatrix and not a normal matrix with accessible rows and columns and I am unable to convert it into a data frame either.

So right now an adjacency matrix (41 x 41) with rows ans columns being 41 gene names from the graphNEL object. OR an edge list with 2 columns and 102 rows. Each edge on one row.

Any help would be highly appreciated. It has been whole day trying to extract data from KEGG and now trying to manipulate into desired form for further application.

Thanks!

graph kegggraph xml • 3.1k views
2
Entering edit mode
Robert Castelo ★ 3.0k
@rcastelo
Last seen 1 hour ago
Barcelona/Universitat Pompeu Fabra

hi,

the way you formulate the question is not completely clear to me but i guess you're asking how to coerce a 'graphNEL' object to an adjacency matrix. this can be done using

as(g, "matrix")

where 'g' would be the 'graphNEL' object, here's an example:

library(graph)
g <- graphNEL(nodes=LETTERS[1:4], edgeL=list(A=list(edges="B"), B=list(edges="A"), C=list(edges="D"), D=list(edges="C")))
g
A graphNEL graph with undirected edges
Number of Nodes = 4
Number of Edges = 2
as(g, "matrix")
A B C D
A 0 1 0 0
B 1 0 0 0
C 0 0 0 1
D 0 0 1 0

cheers,

robert.

0
Entering edit mode

Hi Robert,

Good to know! I tried as.matrix(), which didn't work, so assumed that as( , "matrix") wouldn't work either so didn't try it.

H.

0
Entering edit mode
@herve-pages-1542
Last seen 21 hours ago
Seattle, WA, United States

Hi,

One way to get this is with something like:

adj <- matrix(FALSE, nrow=length(nodes(gR)), ncol=length(nodes(gR)),
dimnames=list(nodes(gR), nodes(gR)))
i <- rep(names(edges(gR)), lengths(edges(gR)))
j <- unlist(edges(gR), use.names=FALSE)
adj[cbind(i, j)] <- TRUE

There might be something in the graph package that does this already, don't know.

Cheers,

H.