咱们使用与人类HIV相关的蛋白质互做数据hunam-HIV PPI.csv来构建这个蛋白质互做网络。算法
在R中,咱们能够从存储在R环境外部的文件读取数据。还能够将数据写入由操做系统存储和访问的文件。 R能够读取和写入各类文件格式,如:csv,excel,xml等。spring
想要读取csv文件,咱们须要:网络
代码以下:app
setwd("/Users/.../Documents/...") data <- read.csv("HIV-human PPI.csv")
这样,咱们就获得了蛋白质互做数据并存储在了data
中。dom
接下来,咱们使用
igraph
包来构建该网络。(由于数据中只有两列表示两个有链接的顶点,所以我没有构建数据帧用于存放顶点的特征)yii
edges <- data.frame(from=data[,1],to=data[,2]) g <- graph.data.frame(edges, directed = FALSE)
graph.data.frame
(也可写做graph_from_data_frame
)函数有许多参数,具体内容以下:函数
graph_from_data_frame(edges,direced,vertices)
如今,咱们已经创建了图形g
,若是你想看看它的样子,能够简单地经过plot(g)
来作到。布局
在许多复杂网络中,对于模块(或称为社区)的划分是很是有意义的。模块发现,或称为社群发现主要有五种模型。优化
社群结构特色:社群内边密度要高于社群间边密度,社群内部链接相对紧密,各个社群之间链接相对稀疏。
社群模型 | 概念 | 效果 |
---|---|---|
点链接 | 某点与某社群有关系就是某社群的 | 最差,经常是某一大类超级多 |
随机游走 | 利用距离类似度,用合并层次聚类方法创建社群 | 运行时间短,可是效果不是特别好,也会出现某类巨多 |
自旋玻璃 | 关系网络当作是随机网络场,利用能量函数来进行层次聚类 | 耗时长,适用较为复杂的状况 |
中间中心度 | 找到中间中心度最弱的删除,并以此分裂至到划分不一样的大群落 | 耗时长,参数设置很重要 |
标签传播 | 经过相邻点给本身打标签,相同的标签一个社群 | 跟特征向量能够组合应用,适用于话题类 |
其中,中间中心度的模型,为Gievan-Newman(GN)算法的思想相同。其他模型的详细状况不做更多介绍,此处,参考了R语言︱SNA-社会关系网络—igraph包(社群划分、画图)。
下面,咱们介绍GN算法的基本思想:
1.计算网络中全部边的中介中心性;
2.去除中介中心性最高的边;
3.从新计算去除边后的网络中全部边的中介中心性;
4.跳至步骤2,从新计算,直至网络中没有边存在。
能够看到,这个算法的思想很是简单。可是,这个算法何时终止,才能使得社群划分的结构最优?在Newman and Girvan 2004
中,他们提出了Modularity Q
(全局模块度)的概念,进一步完善了这个算法。通常认为,Q的取值在0.3~0.7之间最优,可是,也需具体状况具体考虑。
如今模块划分有很是多的算法,不少都已集成在igrah中。在library("igraph")
以后,咱们能够调用许多包中已实现的函数对网络g
划分模块。
算法 | 做者 | 年份 | 复杂度 |
---|---|---|---|
GN | Newman & Girvan | 2004 | |
CFinder | 2005 | ||
随机游走方法 | Pons & Latapy | 2005 | |
自旋玻璃社群发现 | Reichardt & Bornholdt | 2006 | |
LPA(标签传播算法) | Raghavan et al | 2007 | O(m) |
Fast Unfolding | Vincent D. Blondel | 2008 | |
LFM | 2009 | O(n^2) | |
EAGLE | 2009 | O(s*n^2) | |
GIS | 2009 | O(n^2) | |
HANP(Hop Attenuation & Node Preferences) | Lan X.Y. & Leung | 2009 | O(m) |
GCE | 2010 | O(mh) | |
COPRA | 2010 | ||
NMF | 2010 | ||
Link | 2010 | ||
SLPA/GANXis(Speaker-listener Label Propagation) | Jierui Xie | 2011 | |
BMLPA(Balanced Multi-label Propagation) | 武志昊(北交大) | 2012 | O(n*logn) |
cluster_fast_greedy
该方法经过直接优化模块度来发现模块。
cluster_fast_greedy(graph, merges = TRUE, modularity = TRUE,
membership = TRUE, weights = E(graph)$weight)
graph
待划分模块的图。
merges
是否返回合并后的模型。
modularity
是否将每次合并时的模块度以向量返回。
membership
是否在每次合并时考虑全部可能的模块结构,对应最大的模块度计算成员向量。
weights
若是非空,则是一个边权重的向量。
return
一个communities
对象。
一个例子:
cfg <- cluster_fast_greedy(g) plot(cfg, g)
生成的图形以下所示:
edge.betweenness.community
该方法经过中间中心度找到网络中相互关联最弱的点,删除它们之间的边,并以此对网络进行逐层划分,就能够获得愈来愈小的模块。在适当的时候终止这个过程,就能够获得合适的模块划分结果。
member <-edge.betweenness.community(g.undir,weight=E(g)
$weight,directed=F)
有默认的边权重weight,而且默认边是无向的,directed=T时表明有向。
调用这个方法并将其图形展现和保存的代码以下:
## #• Community structure in social and biological networks # M. Girvan and M. E. J. Newman #• New to version 0.6: FALSE #• Directed edges: TRUE #• Weighted edges: TRUE #• Handles multiple components: TRUE #• Runtime: |V||E|^2 ~稀疏:O(N^3) ## ec <- edge.betweenness.community(g) V(g)$size = 1 #我将大部分顶点的大小设置为1 V(g)[degree(g)>=300]$size = 5 #但度很大的顶点更大 png('/Users/.../Documents/.../protein.png',width=1800,height=1800)# 指明接下来要作的图形的格式和长宽 plot(ec,g) dev.off() # 关闭图形设备 print(modularity(ec))
这样,图片保存为了protein.png
,还输出了模块度。
walktrap.community
## #• Computing communities in large networks using random walks # Pascal Pons, Matthieu Latapy #• New to version 0.6: FALSE #• Directed edges: FALSE #• Weighted edges: TRUE #• Handles multiple components: FALSE #• Runtime: |E||V|^2 ## system.time(wc <- walktrap.community(g)) print(modularity(wc)) #membership(wc) plot(wc , g)
leading.eigenvector.community
Newman快速算法将每一个节点看做是一个社团,每次迭代选择产生最大Q值的两个社团合并,直至整个网络融合成一个社团。整个过程可表示成一个树状图,从中选择Q值最大的层次划分获得最终的社团结构。该算法的整体时间复杂度为O(m(m+n))
## #• Finding community structure in networks using the eigenvectors of matrices # MEJ Newman # Phys Rev E 74:036104 (2006) #• New to version 0.6: FALSE #• Directed edges: FALSE #• Weighted edges: FALSE #• Handles multiple components: TRUE #• Runtime: c|V|^2 + |E| ~N(N^2) ## system.time(lec <-leading.eigenvector.community(g)) print(modularity(lec)) plot(lec,g)
fastgreedy.community
## #• Finding community structure in very large networks # Aaron Clauset, M. E. J. Newman, Cristopher Moore #• Finding Community Structure in Mega-scale Social Networks # Ken Wakita, Toshiyuki Tsurumi #• New to version 0.6: FALSE #• Directed edges: FALSE #• Weighted edges: TRUE #• Handles multiple components: TRUE #• Runtime: |V||E| log |V| ## system.time(fc <- fastgreedy.community(g)) print(modularity(fc)) plot(fc, g)
multilevel.community
## #• Fast unfolding of communities in large networks # Vincent D. Blondel, Jean-Loup Guillaume, Renaud Lambiotte, Etienne Lefebvre #• New to version 0.6: TRUE #• Directed edges: FALSE #• Weighted edges: TRUE #• Handles multiple components: TRUE # Runtime: “linear” when |V| \approx |E| ~ sparse; (a quick glance at the algorithm \ # suggests this would be quadratic for fully-connected graphs) system.time(mc <- multilevel.community(g, weights=NA)) print(modularity(mc)) plot(mc, g)
label.propagation.community
## #• Near linear time algorithm to detect community structures in large-scale networks. # Raghavan, U.N. and Albert, R. and Kumara, S. # Phys Rev E 76, 036106. (2007) #• New to version 0.6: TRUE #• Directed edges: FALSE #• Weighted edges: TRUE #• Handles multiple components: FALSE # Runtime: |V| + |E| system.time(lc <- label.propagation.community(g)) print(modularity(lc)) plot(lc , g)
spinglass.community
member<-spinglass.community(g.undir,weights=E(g.undir)$weight,spins=2) #须要设置参数weights,由于无默认值
为了更好地理解GN算法,咱们固然要尝试本身实现一个GN算法。
plot
画图函数
plot(g, layout = layout.fruchterman.reingold, vertex.size = V(g)$size+2,vertex.color=V(g)$color,vertex.label=V(g)$label,vertex.label.cex=1,edge.color = grey(0.5), edge.arrow.mode = "-",edge.arrow.size=5)
layout
设置图的布局方式
layout、layout.auto、layout.bipartite、layout.circle、layout.drl、layout.fruchterman.reingold、layout.fruchterman.reingold.grid、layout.graphopt、layout.grid、layout.grid.3d、layout.kamada.kawai、layout.lgl、layout.mds、layout.merge、layout.norm、layout.random、layout.reingold.tilford、layout.sphere、layout.spring、layout.star、layout.sugiyama、layout.svd
vertex.size
设置节点的大小
de<-read.csv("c:/degree-info.csv",header=F)
V(g)$deg<-de[,2]
V(g)$size=2
V(g)[deg>=1]$size=4
V(g)[deg>=2]$size=6
V(g)[deg>=3]$size=8
V(g)[deg>=4]$size=10
V(g)[deg>=5]$size=12
V(g)[deg>=6]$size=14
vertex.color
设置节点的颜色
color<-read.csv("c:/color.csv",header=F)
col<-c("red","skyblue")
V(g)$color=col[color[,1]]
vertex.label
设置节点的标记
V(g)$label=V(g)$name
vertex.label=V(g)$label
vertex.label.cex
设置节点标记的大小edge.color
设置边的颜色
E(g)$color="grey"
for(i in 1:length(pa3[,1])){
E(g,path=pa3[i,])$color="red"
}
edge.color=E(g)$color
edge.arrow.mode
设置边的链接方式edge.arrow.size
设置箭头的大小E(g)$width=1
设置边的宽度system.time(ec <- edge.betweenness.community(g)) print(modularity(ec)) plot(ec, g,vertex.size=5,vertex.label=NA)
system.time(wc <- walktrap.community(g)) print(modularity(wc)) #membership(wc) plot(wc , g,vertex.size=5,vertex.label=NA)
system.time(lec <-leading.eigenvector.community(g)) print(modularity(lec)) plot(lec,g,vertex.size=5,vertex.label=NA)
system.time(fc <- fastgreedy.community(g)) print(modularity(fc)) plot(fc, g,vertex.size=5,vertex.label=NA)
system.time(mc <- multilevel.community(g, weights=NA)) print(modularity(mc)) plot(mc, g,vertex.size=5,vertex.label=NA)
system.time(lc <- label.propagation.community(g)) print(modularity(lc)) plot(lc , g,vertex.size=5,vertex.label=NA)
zz<-file("d:/test.txt","w") cat(x,file=zz,sep="\n") close(zz)
mode(x) length(x)
1.易百R语言教程
2.R语言igraph包构建网络图——详细展现构建图的基本过程
4.官方R语言手册
6.模块度(Modularity)与Fast Newman算法讲解
7.模块发现算法综述