【发布时间】:2015-10-20 15:07:22
【问题描述】:
我正在尝试改进一项功能,以根据从某些节点属性计算的分数来构建网络。该函数试图从最大化节点属性乘积的图中找到最佳子网络。
函数从一个随机节点开始,在第一个邻居中开始搜索,如果有一些邻居节点的分数满足阈值,则将邻居添加到第一个节点并继续该过程直到不再添加(邻居的添加不会在分数中产生所需的增量)。如果第一个邻居中没有节点会产生分数的增量,则该函数将查找二阶邻居。在这种情况下,很可能有多个路径可以连接节点(2 度邻居),在这种特定情况下,选择的路径将是最短且权重最高的路径(节点属性之一)。
我可以对代码做一些并行化,虽然我不知道如何在这种类型的函数中实现它。
函数如下:
build_network <-
function (G, seed, d= 2){
net <- G
d <- d
score.fun<-function(g){
Za <- sum(V(g)$weight*V(g)$RWRNodeweight)/sqrt(sum(V(g)$RWRNodeweight^2))
k <- vcount(g)
tmp <- genesets.length.null.stat[[as.character(k)]] # genesets.length.null.stat is a list with the median of Za and sd of Za calculated for 1000 replicates of networks of size k
Sa <- (Za-tmp[1])/tmp[2]
}
best.fun<-function(in.nodes,out.nodes) {
score<-(-Inf); best<-character()
for(node in out.nodes){
subG.update<-induced.subgraph(net, c(in.nodes,node))
if( score.fun(subG.update) > score ){
score<-score.fun(subG.update)
best<-node
}
}
list("node"=best,"score"=score)
}
subG <- induced.subgraph(net, seed)
if (!is.connected(subG)) { #the seed must be connected
stop("Input seeds are disjoint")
}
while (TRUE) {
in.nodes <- V(subG)$name
node_num <- vcount(subG)
subsum <- score.fun(subG)
#subx <- V(subG)$name
for (rad in 1:d) {
tmp.neigh <- unlist(neighborhood(net, order = rad, nodes = V(subG)$name))
pot.nodes <- V(net)[tmp.neigh]$name
out.nodes <- setdiff(pot.nodes, in.nodes)
if (length(out.nodes) == 0) break
best_node<-best.fun(in.nodes, out.nodes)
new_score<-best_node$score
best_node<-best_node$node
if (new_score > subsum + 0.01) {
tmp <- unlist(lapply(best_node, function(x) node2treePath(net,V(subG)$name, x))) # node2treePath is a function to retrieve the shortest path with the highest node weights
in.nodes <- c(tmp, V(subG)$name)
subG <- induced.subgraph(net, in.nodes)
break
}
}
if (node_num == vcount(subG)) break
}
return(subG)
}
我正在尝试将此函数应用于约 10,000 个节点的图形。下面是运行函数的近似代码
### generate some example data
library(igraph)
my_graph <- erdos.renyi.game(10000, 0.0003)
V(my_graph)$name <- 1:vcount(my_graph)
V(my_graph)$weight <- rnorm(10000)
V(my_graph)$RWRNodeweight <- runif(10000, min=0, max=0.05)
### Run the function
sublist = list()
for (node in V(G)$name) {
subnet <- build_network(G, node, d)
sublist[[node]] <- subnet }
编辑:这是head(genesets.length.null.stat) 的dput
structure(list(`1` = c(1.01397367504035, 1.18858228819048), `2` = c(1.61970348041337, 1.30189433386605), `3` = c(2.11767222957028, 1.36222065695878), `4` = c(2.47710421934929, 1.36968129959296), `5` = c(2.776011866622, 1.36318885187196), `6` = c(3.16885126246671, 1.42577861995897)), .Names = c("1", "2", "3", "4", "5", "6"))
这里是node2treePath 函数:
node2treePath <- function (G, Tnodes, node){
tmp.path <- get.all.shortest.paths(G, node, Tnodes)$res
tmp.l <- unlist(lapply(tmp.path, length))
index <- which(tmp.l == min(tmp.l))
tmp.path = tmp.path[index]
tmp.sum <- unlist(lapply(tmp.path, function(x)return(sum(V(G)[x]$weight))))
index <- which(tmp.sum == max(tmp.sum))
selected.path = tmp.path[index]
collect <- unlist(lapply(selected.path, function(x)return(V(G)[x]$name)))
return(collect)
}
【问题讨论】:
-
你能让这个例子可重现吗,例如
genesets.length.null.stat不存在。 -
genesets.length.null.stat是一个包含 500 个元素的列表,每个元素中存储两个值(平均值和标准差) -
@user2380782 你能提供一个
genesets.length.null.stat的例子吗,例如dput(head(genesets.length.null.stat)),或者可能是一些生成随机genesets.length.null.stat对象的代码? -
@josilber,实际上
genesets.length.null.stat只是运行我之前的帖子stackoverflow.com/questions/33084860/… 中的josilber.rcpp函数后的一个列表,例如genesets.length.null.stat <- lapply(1:500, function(x)josilber.rcpp(x,1000,my_graph),然后计算每个分量的平均值和标准差名单。我已按照您的建议添加了他dput。非常感谢,如果您需要其他任何东西,请告诉我。 -
你没有定义
node2treePath。此外,best_fun应该是best.fun。即使有一个赏金问题,您仍然需要编写可重现的示例,以便人们可以实际运行代码来解决您陈述的特定问题。您可以提供等效的虚拟函数,也可以提供真实的东西。