comparison Calculate_attributes.R @ 1:e4a55256547a draft

Uploaded
author bornea
date Wed, 18 Oct 2017 15:23:06 -0400
parents
children
comparison
equal deleted inserted replaced
0:c8a8a1e90a9d 1:e4a55256547a
1 library(igraph)
2 library(dplyr)
3 library(ggplot2)
4
5 NetworkAttributes <- function(edges,centrality = "eigenvector",community="fast.greedy") {
6 "This function takes in a SIF formatted dataframe and returns a node attribute dataframe."
7 "It performs community optimization and calculates a centrality metric."
8 nodes <- data.frame(V1 = unique(c(as.character(edges[,1]),as.character(edges[,2]))))
9 graph <- graph.data.frame(edges, directed = FALSE, vertices = nodes)
10 graph = simplify(graph)
11 if(community == "optimal"){V(graph)$comm <- membership(optimal.community(graph))} # computationally intensive
12 if(community == "fast.greedy"){V(graph)$comm <- membership(fastgreedy.community(graph))} # for larger datasets\
13 if(community == "edge.betweenness"){V(graph)$comm <- membership(edge.betweenness.community(graph))}
14 if(community == "walk.trap"){V(graph)$comm <- membership(walktrap.community(graph))}
15 if(community == "spin.glass"){V(graph)$comm <- membership(spinglass.community(graph))}
16 if(community == "leading.eigenvector"){V(graph)$comm <- membership(leading.eigenvector.community(graph))}
17 if(community == "label.propagation"){V(graph)$comm <- membership(label.propagation.community(graph))}
18 if(community == "multilevel"){V(graph)$comm <- membership(multilevel.community(graph))}
19
20 if(centrality == "closeness"){V(graph)$closeness <- centralization.closeness(graph)$res}
21 if(centrality == "betweenness"){V(graph)$betweenness <- centralization.betweenness(graph)$res}
22 if(centrality == "eigenvector"){V(graph)$eigen <- centralization.evcent(graph)$vector}
23 if(centrality == "PageRank"){V(graph)$page <- page_rank(graph)$vector}
24
25 return(get.data.frame(graph, what = "vertices"))
26 }
27 #calculate_attributes(nodes,edgelist,centrality = "betweenness",community="fastgreedy")
28 args <- commandArgs(trailingOnly = TRUE)
29 edgelist <- read.table(file=as.character(args[1]), stringsAsFactors = FALSE,sep="\t",header=FALSE)
30 write.table(NetworkAttributes(edgelist,args[2],args[3]),"node_attr.txt",sep="\t",quote=FALSE,row.names=FALSE)