简体   繁体   English

使用igraph基于边缘属性添加多个边缘

[英]add more than one edge based on edge attributes using igraph

I would like to know wether, using igraph , it is possible to add edges to a graph depending on the values of different edge attributes. 我想知道,使用igraph ,可以根据不同边缘属性的值向图形添加边缘。

I have a data.frame, which dput is the following: 我有一个data.frame,其dput如下:

df <- structure(list(nodeA = c("CFTR", "CFTR", "CFTR", "CFTR", "CFTR", 
"CFTR"), nodeB = c("CYP7A1", "KRT16", "ABCA3", "SLC22A11", 
"PBK", "ACSM1"), score = c(0.239, 0.24, 0.292, 0.269, 
0.233, 0.168), text = c(129L, 0L, 287L, 246L, 
161L, 155L), mining = c(163L, 241L, 413L, 71L, 92L, 56L), 
experiments = c(0L, 0L, 101L, 0L, 75L, 0L), homologs =c(0L, 
0L, 609L, 0L, 0L, 0L)), .Names = c("nodeA", "nodeB", 
"score", "text", "mining","experiments", 
"homologs"), class = "data.frame", row.names = c(NA, 6L))

I would like to add new edges to the graph ( g <- graph.data.frame(df, directed=FALSE ) if the value of the edge attributes is different from 0, for example for the edge CFTR--CYP7A1 , I would like to add a pair of extra edges (one for the text and another for the mining attributes), I am not interested in score (it is the weight of my the graph) 如果边缘属性的值与0不同,我想在图形中添加新边( g <- graph.data.frame(df, directed=FALSE ),例如对于边缘CFTR--CYP7A1 ,我会喜欢添加一对额外的边(一个用于text ,另一个用于mining属性),我对score不感兴趣(这是我图的重量)

Here are a couple of ways. 这有几种方法。

First, rearranging your original data seems a bit easier. 首先,重新排列原始数据似乎更容易一些。 Put the data in to long format and assign colours based on the column names. 将数据放入长格式并根据列名称指定颜色。

library(reshape2)
# Data in long format 
# Create graph, with edges add when attributes / columns are greater than zero
m <- melt(df, id=1:2)
m <- m[m$value != 0, ] # keep non-zero values
g <- graph.data.frame(m, directed=FALSE)

# Add colours to the edges
cols = c(score="black", text="blue", mining="green", 
                                  experiments="red", homologs="yellow")
plot(g, edge.color=cols[E(g)$variable])

If you want to have the original graph and then add coloured edges for each attribute greater than zero, you can loop through the attributes ( edge_attr ), and add edges ( add_edges ) when the condition is met. 如果您想拥有原始图形,然后为每个大于零的属性添加彩色边缘,则可以遍历属性( edge_attr ),并在满足条件时添加边缘( add_edges )。

We can add the additional edges one at a time (shown for the text attribute) 我们可以一次添加一个附加边(显示为text属性)

g <- graph.data.frame(df, directed=FALSE)    
names(edge_attr(g)) # attributes

# Which edges should be added conditioned on text attribute being greater than zero
edge_attr(g, "text")
ats <- edge_attr(g, "text") > 0

#Set edges in graph already to black
E(g)$color <- "black"

# Get head and tail of all edges
ed <- get.edgelist(g)

# subset these by the attribute condition
# combine head and tail nodes in correct format for add_edges
# should be c(tail1, head1, tail2, head2, ..., tailn, headn)
ed <- t(ed[ats, 2:1])

# Add the additional edges
g  <- add_edges(g, ed,  color="blue")
plot(g)

Or add the additional edges in one go 或者一次性添加额外的边缘

g <- graph.data.frame(df, directed=FALSE)    

# Indicator of attribute > 0
ats <- unlist(edge_attr(g)) > 0

# Repeat the head & tail of each edge
# subset so the same length as relevant attributes
ed <- do.call(rbind, replicate(length(edge_attr(g)), get.edgelist(g), simplify=FALSE))
ed <- t(ed[ats, 2:1])
cols <- rep(c("black", "blue", "green", "red", "yellow"), each=length(E(g)))[ats]

g  <- add_edges(g, ed,  color=cols)
plot(g)

I think this gets you what you want with a bit of melting and casting: 我认为这可以通过一些融化和铸造来获得你想要的东西:

library(data.table)

setDT(df)

#get list of potential edges
tmp <- melt(df, id.vars = c("nodeA","nodeB","score"), measure.vars = c("text","mining","experiments","homologs"))

#Filter out zeros, create unique group for each edge
tmp <- tmp[value != 0, ][, ind := .I]

#Recast
tmp <- dcast(tmp, ind + nodeA + nodeB + score ~ variable, value.var = "value", fill = 0)

#get rid of index
tmp[, ind := NULL]

#join back to initial edge list
df <- rbindlist(list(df, tmp))
df
    nodeA    nodeB score text mining experiments homologs
 1:  CFTR   CYP7A1 0.239  129    163           0        0
 2:  CFTR    KRT16 0.240    0    241           0        0
 3:  CFTR    ABCA3 0.292  287    413         101      609
 4:  CFTR SLC22A11 0.269  246     71           0        0
 5:  CFTR      PBK 0.233  161     92          75        0
 6:  CFTR    ACSM1 0.168  155     56           0        0
 7:  CFTR   CYP7A1 0.239  129      0           0        0
 8:  CFTR    ABCA3 0.292  287      0           0        0
 9:  CFTR SLC22A11 0.269  246      0           0        0
10:  CFTR      PBK 0.233  161      0           0        0
11:  CFTR    ACSM1 0.168  155      0           0        0
12:  CFTR   CYP7A1 0.239    0    163           0        0
13:  CFTR    KRT16 0.240    0    241           0        0
14:  CFTR    ABCA3 0.292    0    413           0        0
15:  CFTR SLC22A11 0.269    0     71           0        0
16:  CFTR      PBK 0.233    0     92           0        0
17:  CFTR    ACSM1 0.168    0     56           0        0
18:  CFTR    ABCA3 0.292    0      0         101        0
19:  CFTR      PBK 0.233    0      0          75        0
20:  CFTR    ABCA3 0.292    0      0           0      609

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM