我正在尝试使用igraph来实现Kou算法来识别R中的Steiner树.
Kou的算法可以这样描述:
前两个步骤很简单:
g <- erdos.renyi.game(100, 1/10) # graph
V(g)$name <- 1:100
# Some steiner nodes
steiner.points <- sample(1:100, 5)
# Complete distance graph G'
Gi <- graph.full(5)
V(Gi)$name <- steiner.points
# Find a minimum spanning tree T' in G'
mst <- minimum.spanning.tree(Gi)
Run Code Online (Sandbox Code Playgroud)
但是,我不知道如何用G'中的最短路径替换T'中的边缘.我知道get.shortest.paths我可以vpath从一对节点获取,但是我如何用shortest.pathG 中的T'替换和边缘?
提前谢谢了
一个简单的问题,
我有一个带数字的向量,例如:
values <- c(0.104654225, 0.001781299, 0.343747296, 0.139326617, 0.375521201, 0.101218053)
Run Code Online (Sandbox Code Playgroud)
我想为这个向量分配一个渐变比例,我知道我可以用它做
gray(values/(max(values))
Run Code Online (Sandbox Code Playgroud)
但我想使用蓝色标度,例如使用colorRampPalette或类似的东西,
谢谢
我注意到有几个问题与类似的主题,但他们映射间隔,我想将颜色映射到数值
我有一个mygraph有10,000个节点和~145,000个边的igraph对象,我需要从这个图中创建一些子图,但是它们的大小不同.我需要的是从确定的大小(从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
}
Run Code Online (Sandbox Code Playgroud)
第二步是将函数应用于实际图形
### 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 …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用热图,heatmap.2但我没有获得单元格边框.如果我设置参数sepwidth并且sepcolor它不起作用,我必须包括colsep和rowsep参数,但仍然这样做,一些单元格边框没有绘制,任何想法?
heatmap.2(as.matrix(df), key=F, trace="none", ColSideColors=colorside,
cexRow=0.6, breaks=bk1, col=colors2,
lmat=rbind(c(0,0), c(0,4), c(0,1), c(3,2), c(0,0)),
lhei=c(0.4,0.3,0.05,0.4,0.6),
sepwidth=c(0.01, 0.01), sepcolor="black",
colsep=1:length(df), rowsep=1:length(df))
Run Code Online (Sandbox Code Playgroud) 我想知道,使用时igraph,可以根据不同边缘属性的值将边添加到图形中.
我有一个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))
Run Code Online (Sandbox Code Playgroud)
我想在图(g …
我想知道在使用foreach %dopar%循环后是否可以输出两个不同的对象.
我会尝试解释我在寻找什么.假设我在循环中有几个操作,我有两个data.frame:
library(doMC)
library(parallel)
registerDoMC(cores=4)
result <- foreach(i=1:100) %dopar% {
#### some code here
#### some code here
vec1 <- result_from_previous code # It would be the 1st object I'd like to ouput
vec2 <- result_from_previous code # It would be the 2nd object I'd like to output
}
Run Code Online (Sandbox Code Playgroud)
我想要的输出是长度为2的data.frames列表,例如:
dim(result[[1]]) # equals to nrow=length(vec1) and ncol=100
dim(result[[2]]) # equals to nrow=length(vec2) and ncol=100
Run Code Online (Sandbox Code Playgroud)
我从前一篇文章中尝试了这个保存foreach dopar loop的多个输出:
comb <- function(x, ...) {
lapply(seq_along(x), function(i) c(x[[i]], …Run Code Online (Sandbox Code Playgroud) 我正在尝试根据从某些节点属性计算的分数来改进构建网络的功能.该函数尝试从最大化节点属性的乘积的图中找到最佳子网.
该函数在随机节点中开始并在第一个邻居中开始搜索,如果有一些邻居的节点得分足以达到阈值,则将邻居添加到第一个节点并继续该过程直到不再添加(添加邻居没有在分数中产生所需的增量).如果第一个邻居中没有节点产生得分的增量,那么该函数将查找第二度邻居.在这种情况下,很可能有几条路径连接节点(第二度邻居),在这种特定情况下,所选路径将是具有最高权重的最短路径(其中一个节点属性).
我可以做一些代码的并行化,虽然我不知道如何在这种类型的函数中实现它.
功能如下:
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)) …Run Code Online (Sandbox Code Playgroud) 我有一个列表,我想将其转换为带有两列的data.frame.问题是列表元素的长度不相等,这是我的数据的外观示例:
my.list
$A1CF
[1] "A1CF" "APOBEC1" "CUGBP2" "KHSRP" "SYNCRIP" "TNPO2"
$A2LD1
[1] "A2LD1" "PRPSAP2" "RPL15" "TANC1"
$A2M
[1] "A2M" "ADAM19" "ADAMTS1" "AMBP" "ANXA6" "APOE"
Run Code Online (Sandbox Code Playgroud)
此列表来自以前的data.frame:
my.list <- split(df$V2, df$V1)
df
V1 V2
1 A1BG A1BG
2 A1BG CRISP3
3 A1CF A1CF
4 A1CF APOBEC1
5 A1CF CUGBP2
6 A1CF KHSRP
7 A1CF SYNCRIP
8 A1CF TNPO2
9 A2LD1 A2LD1
10 A2LD1 PRPSAP2
11 A2LD1 RPL15
12 A2LD1 TANC1
13 A2M A2M
14 A2M ADAM19
15 A2M ADAMTS1
16 A2M AMBP …Run Code Online (Sandbox Code Playgroud) 我有一个图 G(V,E) 未加权、无向且连通的图,具有 12744 个节点和 166262 个边。我有一组节点 (sub_set),它是 V 的子集。我有兴趣提取最小的连通子图,其中 sub_set 是这个新图的一部分。我已经设法获得一个子图,其中包含我的节点子集,但我想知道是否有办法最小化该图。
这是我的代码(改编自http://sidderb.wordpress.com/2013/07/16/irefr-ppi-data-access-from-r/)
library('igraph')
g <- erdos.renyi.game(10000, 0.003) #graph for illustrating my propose
sub_set <- sample(V(g), 80)
order <- 1
edges <- get.edges(g, 1:(ecount(g)))
neighbours.vid <- unique(unlist(neighborhood(g, order, which(V(g) %in% sub_set))))
rel.vid <- edges[intersect(which(edges[,1] %in% neighbours.vid), which(edges[,2] %in% neighbours.vid)),]
rel <- as.data.frame(cbind(V(g)[rel.vid[,1]], V(g)[rel.vid[,2]]), stringsAsFactors=FALSE)
names(rel) <- c("from", "to")
subgraph <- graph.data.frame(rel, directed=F)
subgraph <- simplify(subgraph)
Run Code Online (Sandbox Code Playgroud)
我读过这篇文章
包含给定节点集的最小连通子图,所以我猜我的问题可能是“斯坦纳树问题”,有没有办法尝试使用 igraph 找到次优解决方案?