## ----Load packages - quick start, eval=TRUE, message=FALSE--------------------
#--- Load required packages for this section
library(igraph)
library(ggplot2)
library(RGraphSpace)
library(PathwaySpace)

## ----Making a toy igraph - 1, eval=TRUE, message=FALSE------------------------
# Make a 'toy' igraph object, either a directed or undirected graph
gtoy1 <- make_star(5, mode="undirected")

# Assign 'x' and 'y' coordinates to each vertex
# ..this can be an arbitrary unit in (-Inf, +Inf)
V(gtoy1)$x <- c(0, 2, -2, -4, -8)
V(gtoy1)$y <- c(0, 0,  2, -4,  0)

# Assign a 'name' to each vertex (here, from n1 to n5)
V(gtoy1)$name <- paste0("n", 1:5)

## ----GraphSpace constructor - 1, eval=TRUE, message=FALSE---------------------
# Check graph validity
gs1 <- GraphSpace(gtoy1)
# Normalize node coordinates 
gs1 <- normalizeGraphSpace(gs1, mar = 0.2)

## ----GraphSpace constructor - 2, eval=local_build, message=FALSE, fig.height=4.5, fig.width=4.5, out.width = '70%'----
# # Check the graph layout
# plotGraphSpace(gs1, add.labels = TRUE)

## ----PathwaySpace constructor - 1, eval=TRUE, message=FALSE-------------------
# Run the PathwaySpace constructor
p_space1 <- buildPathwaySpace(gs1)

## ----PathwaySpace constructor - 2, eval=TRUE, message=FALSE-------------------
# Check the number of vertices in a PathwaySpace object
gs_vcount(p_space1)

# Check vertex names
names(p_space1)

# Check signal (initialized with '0')
vertexSignal(p_space1)

## ----PathwaySpace constructor - 3, eval=TRUE, message=FALSE-------------------
# Set new signal to all vertices
vertexSignal(p_space1) <- c(1, 4, 2, 4, 3)

# Set a new signal to the 1st vertex
vertexSignal(p_space1)[1] <- 2

# Set a new signal to vertex "n1"
vertexSignal(p_space1)["n1"] <- 6

# Check updated signal values
vertexSignal(p_space1)

## ----Circular projection - 1, eval=local_build, message=FALSE, fig.height=4.5, fig.width=5, out.width = '75%'----
# # Run signal projection
# p_space1 <- circularProjection(p_space1, k = 1,
#   decay.fun = weibullDecay(pdist = 0.4))
# 
# # Plot a PathwaySpace image
# plotPathwaySpace(p_space1, add.marks = TRUE)

## ----Circular projection - 3, eval=local_build, message=FALSE, fig.height=4.5, fig.width=5, out.width = '75%'----
# # Re-run signal projection, adjusting Weibull's shape
# p_space1 <- circularProjection(p_space1, k = 2,
#   decay.fun = weibullDecay(shape = 2, pdist = 0.2))
# 
# # Plot PathwaySpace
# plotPathwaySpace(p_space1, marks = "n1", theme = "th2")

## ----Polar projection - 1, eval=TRUE, message=FALSE---------------------------
# Load a pre-processed directed igraph object
data("gtoy2", package = "RGraphSpace")
# Check graph validity
gs2 <- GraphSpace(gtoy2)
# Normalize node coordinates
gs2 <- normalizeGraphSpace(gs2, mar = 0.2)

## ----Polar projection - 2, eval=local_build, message=FALSE, fig.height=4.5, fig.width=4.5, out.width = '70%'----
# # Check the graph layout
# plotGraphSpace(gs2, add.labels = TRUE)

## ----Polar projection - 3, eval=TRUE, message=FALSE---------------------------
# Build a PathwaySpace for the 'gs2'
p_space2 <- buildPathwaySpace(gs2)

# Set '1s' as vertex signal
vertexSignal(p_space2) <- 1

## ----Polar projection - 4, eval=TRUE, message=FALSE---------------------------
# Modify decay function
# ..for all vertices
vertexDecay(p_space2) <- weibullDecay(shape=2, pdist = 1)
# ..for individual vertices
vertexDecay(p_space2)[["n6"]] <- weibullDecay(shape=3, pdist = 1)

## ----Polar projection - 5, eval=local_build, message=FALSE, fig.height=4.5, fig.width=5, out.width = '75%'----
# # Run signal projection using polar coordinates
# p_space2 <- polarProjection(p_space2, beta = 10)
# 
# # Plot PathwaySpace
# plotPathwaySpace(p_space2, theme = "th2", add.marks = TRUE)

## ----Polar projection - 6, eval=local_build, message=FALSE, fig.height=4.5, fig.width=5, out.width = '75%'----
# # Re-run signal projection using 'directional = TRUE'
# p_space2 <- polarProjection(p_space2,
#   beta = 10, directional = TRUE)
# 
# # Plot PathwaySpace
# plotPathwaySpace(p_space2, theme = "th2",
#   marks = c("n1","n3","n4","n5"))

## ----Signal types - 1, eval=TRUE, message=FALSE-------------------------------
# Set a negative signal to vertices "n3" and "n4"
vertexSignal(p_space1)[c("n3","n4")] <- c(-2, -4)

# Check updated signal vector
vertexSignal(p_space1)

## ----Signal types - 2, eval=local_build, message=FALSE, fig.height=4.5, fig.width=4.5, out.width = '75%'----
# # Re-run signal projection
# p_space1 <- circularProjection(p_space1,
#   decay.fun = weibullDecay(shape = 2))
# 
# # Plot PathwaySpace
# plotPathwaySpace(p_space1, bg.color = "white",
#   font.color = "grey20", add.marks = TRUE,
#   mark.color = "magenta", theme = "th3")

## ----label='Session information', eval=TRUE, echo=FALSE-----------------------
sessionInfo()

