+}
+
+#from :
+#http://gopalakrishna.palem.in/iGraphExport.html#GexfExport
+# Converts the given igraph object to GEXF format and saves it at the given filepath location
+# g: input igraph object to be converted to gexf format
+# filepath: file location where the output gexf file should be saved
+#
+saveAsGEXF = function(g, filepath="converted_graph.gexf")
+{
+ require(igraph)
+ require(rgexf)
+
+ # gexf nodes require two column data frame (id, label)
+ # check if the input vertices has label already present
+ # if not, just have the ids themselves as the label
+ if(is.null(V(g)$label))
+ V(g)$label <- as.character(V(g))
+
+ # similarily if edges does not have weight, add default 1 weight
+ if(is.null(E(g)$weight))
+ E(g)$weight <- rep.int(1, ecount(g))
+
+ nodes <- data.frame(cbind(1:vcount(g), V(g)$label))
+ nodes[,1] <- as.character(nodes[,1])
+ nodes[,2] <- as.character(nodes[,2])
+ edges <- t(Vectorize(get.edge, vectorize.args='id')(g, 1:ecount(g)))
+
+ # combine all node attributes into a matrix (and take care of & for xml)
+ vAttrNames <- setdiff(list.vertex.attributes(g), "label")
+ for (val in c("x","y","color")) {
+ vAttrNames <- setdiff(vAttrNames, val)
+ }
+ nodesAtt <- data.frame(sapply(vAttrNames, function(attr) sub("&", "&",get.vertex.attribute(g, attr))))
+ for (i in 1:ncol(nodesAtt)) {
+ nodesAtt[,i] <- as.character(nodesAtt[,i])
+ }
+
+ # combine all edge attributes into a matrix (and take care of & for xml)
+ eAttrNames <- setdiff(list.edge.attributes(g), "weight")
+ edgesAtt <- data.frame(sapply(eAttrNames, function(attr) sub("&", "&",get.edge.attribute(g, attr))))
+
+ # combine all graph attributes into a meta-data
+ graphAtt <- sapply(list.graph.attributes(g), function(attr) sub("&", "&",get.graph.attribute(g, attr)))
+ ll <- length(V(g)$x)
+ cc <- t(sapply(V(g)$color, col2rgb, alpha=TRUE))
+ cc[,4] <- cc[,4]/255
+ # generate the gexf object
+ output <- write.gexf(nodes, edges,
+ edgesWeight=E(g)$weight,
+ edgesAtt = edgesAtt,
+ #edgesVizAtt = list(size=as.matrix(E(g)$weight)),
+ nodesAtt = nodesAtt,
+ nodesVizAtt=list(color=cc, position=cbind(V(g)$x,V(g)$y, rep(0,ll)), size=V(g)$weight),
+ meta=c(list(creator="iramuteq", description="igraph -> gexf converted file", keywords="igraph, gexf, R, rgexf"), graphAtt))
+
+ print(output, filepath, replace=T)
+}
+
+
+merge.graph <- function(graphs) {
+ library(colorspace)
+ ng <- graph.union(graphs, byname=T)
+ V.weight <- V(ng)$weight_1
+ E.weight <- E(ng)$weight_1
+ cols <- rainbow(length(graphs))
+ V.color <- rep(cols[1], length(V.weight))
+ for (i in 2:length(graphs)) {
+ tw <- paste('weight_', i, sep='')
+ tocomp <- get.vertex.attribute(ng,tw)
+ totest <- intersect(which(!is.na(V.weight)), which(!is.na(tocomp)))
+ maxmat <- cbind(V.weight[totest], tocomp[totest])
+ resmax <- apply(maxmat, 1, which.max)
+ ncolor <- c(cols[(i-1)], cols[i])
+ #rbgcol1 <- col2rgb(cols[(i-1)])
+ #rbgcol1 <- rbgcol1/255
+ #rgbcol1 <- RGB(rbgcol1[1],rbgcol1[2],rbgcol1[3])
+ rbgcol2 <- col2rgb(cols[i])
+ rbgcol2 <- rbgcol2/255
+ #rgbcol2 <- RGB(rbgcol2[1],rbgcol2[2],rbgcol2[3])
+ for (j in totest) {
+ alpha <- tocomp[j] /(V.weight[j] + tocomp[j])
+ rbgcol1 <- col2rgb(V.color[j])
+ rbgcol1 <- rbgcol1/255
+ #mix.col <- mixcolor(alpha,rbgcol1, rbgcol2)
+ mix.col <- mixcolor(alpha, RGB(rbgcol1[1],rbgcol1[2],rbgcol1[3]), RGB(rbgcol2[1],rbgcol2[2],rbgcol2[3]))
+ V.color[j] <- adjustcolor(hex(mix.col), 0.6)
+ }
+ #to.change <- totest[which(resmax == 2)]
+ #V.color[to.change] <- cols[i]
+ V.weight[totest] <- apply(maxmat, 1, max)
+ nas <- which(is.na(V.weight))
+ nas2 <- which(is.na(tocomp))
+ fr2 <- setdiff(nas,nas2)
+ V.weight[fr2] <- tocomp[fr2]
+ V.color[fr2] <- cols[i]
+ tocomp <- get.edge.attribute(ng, tw)
+ totest <- intersect(which(!is.na(E.weight)), which(!is.na(tocomp)))
+ maxmat <- cbind(E.weight[totest], tocomp[totest])
+ resmax <- apply(maxmat, 1, which.max)
+ E.weight[totest] <- apply(maxmat, 1, max)
+ nas <- which(is.na(E.weight))
+ nas2 <- which(is.na(tocomp))
+ fr2 <- setdiff(nas,nas2)
+ E.weight[fr2] <- tocomp[fr2]
+ }
+ V(ng)$weight <- V.weight
+ V(ng)$color <- V.color
+ E(ng)$weight <- E.weight
+ ng