View source: R/umbrella_module_random_walk.R
1 |
dataset |
|
ontology |
|
start_node |
|
walk_length |
|
random_seed |
|
walk_mode |
|
stuck_response |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | ##---- Should be DIRECTLY executable !! ----
##-- ==> Define data, use random,
##-- or do help(data=index) for the standard data sets.
## The function is currently defined as
function (dataset, ontology, start_node, walk_length, random_seed = c(TRUE,
FALSE), walk_mode = c("in", "out", "all"), stuck_response = c("return",
"error"))
{
if (missing(dataset)) {
print(paste("ERROR: Argument 'dataset' has not been defined. Terminating",
"Random Walk."))
return()
}
if (missing(start_node)) {
print(paste("ERROR: Argument 'start_node' has not been defined.",
"Terminating Random Walk."))
return()
}
if (missing(walk_length)) {
walk_length <- round(vcount(dataset)/2)
print("NOTE: Argument 'walk_length' has not been defined.")
print(paste("NOTE: Automatically setting number of steps to ",
walk_length, ".", sep = ""))
}
if (isTRUE(random_seed)) {
umbrella::ApplyRandomSeed()
}
else if (isFALSE(random_seed)) {
print(paste("NOTE: Argument 'random_seed' set to 'FALSE'. Proceeding with",
"current pseudo-random seed."))
}
else if (missing(random_seed)) {
print(paste("NOTE: Argument 'random_seed' not specified. Proceeding with",
"current pseudo-random seed."))
}
else {
print(paste("ERROR: Argument 'random_seed' has not been defined.",
"Terminating Random Walk."))
return()
}
if (missing(walk_mode)) {
print(paste("NOTE: Argument 'walk_mode' not specified. Proceeding under ",
"'all' mode."))
walk_mode <- "all"
}
else if (walk_mode == "in") {
print("NOTE: Random Walk will be executed under 'in' mode.")
}
else if (walk_mode == "out") {
print("NOTE: Random Walk will be executed under 'out' mode.")
}
else if (walk_mode == "all") {
print("NOTE: Random Walk will be executed under 'all' mode.")
}
else {
print(paste("ERROR: Argument 'walk_mode' has not been defined. Terminating",
"Random Walk."))
return()
}
if (missing(stuck_response)) {
print(paste("ERROR: Argument 'stuck' has not been defined. Terminating",
"Random Walk."))
return()
}
n <- vcount(dataset)
tm <- matrix(sample(0:1, n^2, prob = c(0.95, 0.05), replace = TRUE),
n, n)
tm <- (tm == 1 | t(tm) == 1) * 1
diag(tm) <- 0
start <- start_node
len <- walk_length
path <- c(start, rep(NA, len))
for (i in 2:(len + 1)) {
idx <- tm[path[i - 1], ] != 0
if (any(idx)) {
path[i] <- resample(which(idx), 1, prob = tm[path[i -
1], idx])
}
else {
break
}
}
path
path <- igraph::graph_from_adj_list(path, mode = "all", duplicate = FALSE)
print(path)
print("NOTE: Plotting random walk on data set.")
plot(path, main = "Random Walk Graph / RandomWalk()", sub = paste("Umbrella",
packageVersion("umbrella")))
invisible(path)
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.