我有一个igraph对象mygraph
,其中包含大约10,000个节点和大约145,000条边,我需要从这个图形中创建许多具有不同大小的子图。
我需要创建一系列指定大小的子图(从5个节点到500个节点),每个子图中所有节点都相互连接。我需要为每个大小创建大约1,000个子图(即大小为5的1000个子图,大小为6的1000个子图等等),然后根据不同的节点属性计算每个图的一些值。
我有一些代码,但是它需要很长时间才能完成所有计算。我考虑使用graphlets
函数来获取不同的大小,但每次在我的电脑上运行它时,由于内存问题而崩溃。
以下是我正在使用的代码:
第一步是创建一个函数来创建不同大小的子图并执行所需的计算。
random_network<-function(size,G){
score_fun<-function(g){
subsum <- sum(V(g)$weight*V(g)$RWRNodeweight)/sqrt(sum(V(g)$RWRNodeweight^2))
subsum
}
genes.idx <- V(G)$name
perm <- c()
while(length(perm)<1000){
seed<-sample(genes.idx,1)
while( length(seed)<size ){
tmp.neigh <- V(G)[unlist(neighborhood(G,1,seed))]$name
tmp.neigh <- setdiff(tmp.neigh, seed)
if( length(tmp.neigh)>0 )
seed<-c(seed,sample(tmp.neigh,1)) else break
}
if( length(seed)==size )
perm <- c(perm,score_fun(induced.subgraph(G,seed)))
}
perm
}
第二步是将该函数应用于实际的图形。
### 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 code to get the subgraphs from different size and do calculations based on nodes
genesets.length<- seq(5:500)
genesets.length.null.dis <- list()
for(k in 5:max(genesets.length){
genesets.length.null.dis[[as.character(k)]] <- random_network(size=k,G=my_graph)
}
names=FALSE
传递给get.edgelist
。 - josliber