Skip to content

Instantly share code, notes, and snippets.

@antonkratz
Last active July 20, 2018 18:44
Show Gist options
  • Save antonkratz/b3b968017e12ca900693edebe242d19d to your computer and use it in GitHub Desktop.
Save antonkratz/b3b968017e12ca900693edebe242d19d to your computer and use it in GitHub Desktop.
determine the # of components in a graph
#!/usr/bin/Rscript
# author: Anton Kratz
# created: Fri Jul 20 11:00:06 PDT 2018
# given a graph in edge list with score field, i.e.:
# FROM_NODE TO_NODE SCORE
# determine the # of components in this graph
suppressPackageStartupMessages(library(igraph))
args = commandArgs(trailingOnly=TRUE)
# test if there is at least one argument: if not, return an error
if (length(args) == 0) {
stop("specify graph edge list input filename", call.=FALSE)
}
rf <- read.table(args[1], sep="\t")
# mysubset <- rf[which(rf$V3>0.4),]
mysubset <- rf
akg <- graph_from_data_frame(mysubset, directed = FALSE, vertices = NULL)
coakg <- components(akg)
cat (paste("# of components of ", args[1], ": ", coakg$no, "\n", sep = ""))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment