##Install dependencies for Demonstration

library('mvtnorm')
library("igraph")
library("gplots")
library("graphsim")

##set up simulated graphs

graph_test1_edges <- rbind(c("A", "B"), c("B", "C"), c("B", "D"))
graph_test1 <- graph.edgelist(graph_test1_edges, directed = T)
plot_directed(graph_test1, layout = layout.kamada.kawai)

plot of chunk unnamed-chunk-2

#Generated simulated expression data from graph

##Adjacency matrix

adj_mat <- make_adjmatrix_graph(graph_test1)
heatmap.2(make_adjmatrix_graph(graph_test1), scale = "none", trace = "none", col = colorpanel(3, "grey75", "white", "blue"), colsep = 1:4, rowsep = 1:4)

plot of chunk unnamed-chunk-3

heatmap.2(make_adjmatrix_graph(graph_test1, directed = T), scale = "none", trace = "none", col = colorpanel(3, "grey75", "white", "blue"), colsep = 1:4, rowsep = 1:4)

plot of chunk unnamed-chunk-3

comm_mat <- make_commonlink_graph(graph_test1)
heatmap.2(make_commonlink_graph(graph_test1), scale = "none", trace = "none", col = bluered(50), colsep = 1:4, rowsep = 1:4)

plot of chunk unnamed-chunk-3

##Distance matrix

shortest.paths(graph_test1)
##   A B C D
## A 0 1 2 2
## B 1 0 1 1
## C 2 1 0 2
## D 2 1 2 0
heatmap.2(shortest.paths(graph_test1), scale = "none", trace = "none", col = bluered(50), colsep = 1:4, rowsep = 1:4)

plot of chunk unnamed-chunk-4

(diameter(graph_test1)-shortest.paths(graph_test1))/diameter(graph_test1)
##     A   B   C   D
## A 1.0 0.5 0.0 0.0
## B 0.5 1.0 0.5 0.5
## C 0.0 0.5 1.0 0.0
## D 0.0 0.5 0.0 1.0
heatmap.2((diameter(graph_test1)-shortest.paths(graph_test1))/diameter(graph_test1), scale = "none", trace = "none", col = bluered(50), colsep = 1:4, rowsep = 1:4)

plot of chunk unnamed-chunk-4

make_distance_graph(graph_test1, absolute = F)
##      A   B    C    D
## A 1.00 0.5 0.25 0.25
## B 0.50 1.0 0.50 0.50
## C 0.25 0.5 1.00 0.25
## D 0.25 0.5 0.25 1.00
make_distance_graph(graph_test1, absolute = T)
##     A   B   C   D
## A 1.0 0.5 0.0 0.0
## B 0.5 1.0 0.5 0.5
## C 0.0 0.5 1.0 0.0
## D 0.0 0.5 0.0 1.0

##Sigma matrix

#sigma from adj mat
make_sigma_mat_graph(graph_test1, 0.8)
##     A   B   C   D
## A 1.0 0.8 0.0 0.0
## B 0.8 1.0 0.8 0.8
## C 0.0 0.8 1.0 0.0
## D 0.0 0.8 0.0 1.0
heatmap.2(make_sigma_mat_graph(graph_test1, 0.8), scale = "none", trace = "none", col = bluered(50), colsep = 1:4, rowsep = 1:4)

plot of chunk unnamed-chunk-5

#sigma from comm mat
make_sigma_mat_graph(graph_test1, 0.8, comm = T)
##     A B   C   D
## A 1.0 0 0.8 0.8
## B 0.0 1 0.0 0.0
## C 0.8 0 1.0 0.8
## D 0.8 0 0.8 1.0
heatmap.2(make_sigma_mat_graph(graph_test1, 0.8, comm = T), scale = "none", trace = "none", col = bluered(50), colsep = 1:4, rowsep = 1:4)

plot of chunk unnamed-chunk-5

# sigma from distance matrix
make_sigma_mat_dist_graph(graph_test1, 0.8, absolute = T)
##     A   B   C   D
## A 1.0 0.8 0.0 0.0
## B 0.8 1.0 0.8 0.8
## C 0.0 0.8 1.0 0.0
## D 0.0 0.8 0.0 1.0
make_sigma_mat_dist_graph(graph_test1, 0.8, absolute = F)
##     A   B   C   D
## A 1.0 0.8 0.4 0.4
## B 0.8 1.0 0.8 0.8
## C 0.4 0.8 1.0 0.4
## D 0.4 0.8 0.4 1.0
heatmap.2(make_sigma_mat_dist_graph(graph_test1, 0.8, absolute = T), scale = "none", trace = "none", col = bluered(50), colsep = 1:4, rowsep = 1:4)

plot of chunk unnamed-chunk-5

heatmap.2(make_sigma_mat_dist_graph(graph_test1, 0.8, absolute = F), scale = "none", trace = "none", col = bluered(50), colsep = 1:4, rowsep = 1:4)

plot of chunk unnamed-chunk-5

#Simulated expression and observed correlation

#simulate expression data
#adj mat
expr <- generate_expression(100, graph_test1, cor = 0.8, mean = 0, comm =F) # unable to generate from adj mat ## fixed with positive definite correction
## Warning in generate_expression(100, graph_test1, cor = 0.8, mean = 0, comm = F):
## sigma matrix was not positive definite, nearest approximation used.
heatmap.2(expr, scale = "none", trace = "none", col = bluered(50), colsep = 1:4, rowsep = 1:4)

plot of chunk unnamed-chunk-6

heatmap.2(cor(t(expr)), scale = "none", trace = "none", col = bluered(50), colsep = 1:4, rowsep = 1:4)

plot of chunk unnamed-chunk-6

#comm mat
expr <- generate_expression(100, graph_test1, cor = 0.8, mean = 0, comm =T) #expression from comm mat
heatmap.2(expr, scale = "none", trace = "none", col = bluered(50), colsep = 1:4, rowsep = 1:4)

plot of chunk unnamed-chunk-6

heatmap.2(cor(t(expr)), scale = "none", trace = "none", col = bluered(50), colsep = 1:4, rowsep = 1:4)

plot of chunk unnamed-chunk-6

#absolute dist
expr<- generate_expression(100, graph_test1, cor = 0.8, mean = 0, comm = F, dist = T, absolute = T) # unable to generate from adj mat ## fixed PD
## Warning in generate_expression(100, graph_test1, cor = 0.8, mean = 0, comm =
## F, : sigma matrix was not positive definite, nearest approximation used.
heatmap.2(expr, scale = "none", trace = "none", col = bluered(50), colsep = 1:4, rowsep = 1:4)

plot of chunk unnamed-chunk-6

heatmap.2(cor(t(expr)), scale = "none", trace = "none", col = bluered(50), colsep = 1:4, rowsep = 1:4)

plot of chunk unnamed-chunk-6

# relative dist
expr<- generate_expression(100, graph_test1, cor = 0.8, mean = 0, comm = F, dist = T, absolute = F)
## Warning in generate_expression(100, graph_test1, cor = 0.8, mean = 0, comm =
## F, : sigma matrix was not positive definite, nearest approximation used.
heatmap.2(expr, scale = "none", trace = "none", col = bluered(50), colsep = 1:4, rowsep = 1:4)

plot of chunk unnamed-chunk-6

heatmap.2(cor(t(expr)), scale = "none", trace = "none", col = bluered(50), colsep = 1:4, rowsep = 1:4)

plot of chunk unnamed-chunk-6