##Install dependencies for Demonstration
library('mvtnorm')
library("igraph")
library("gplots")
library("graphsim")
##set up simulated graphs
graph_test3_edges <- rbind(c("A", "B"), c("B", "C"), c("B", "D"),c("C", "E"), c("D", "E"), c("E", "F"))
graph_test3 <- graph.edgelist(graph_test3_edges, directed = T)
plot_directed(graph_test3, layout = layout.kamada.kawai)
#Generated simulated expression data from graph
##Adjacency matrix
adj_mat <- make_adjmatrix_graph(graph_test3)
heatmap.2(make_adjmatrix_graph(graph_test3), scale = "none", trace = "none", col = colorpanel(3, "grey75", "white", "blue"), colsep = 1:6, rowsep = 1:6)
heatmap.2(make_adjmatrix_graph(graph_test3, directed = T), scale = "none", trace = "none", col = colorpanel(3, "grey75", "white", "blue"), colsep = 1:6, rowsep = 1:6)
comm_mat <- make_commonlink_graph(graph_test3)
heatmap.2(make_commonlink_graph(graph_test3), scale = "none", trace = "none", col = bluered(50), colsep = 1:6, rowsep = 1:6)
##Distance matrix
shortest.paths(graph_test3)
## A B C D E F
## A 0 1 2 2 3 4
## B 1 0 1 1 2 3
## C 2 1 0 2 1 2
## D 2 1 2 0 1 2
## E 3 2 1 1 0 1
## F 4 3 2 2 1 0
heatmap.2(shortest.paths(graph_test3), scale = "none", trace = "none", col = bluered(50), colsep = 1:6, rowsep = 1:6)
(diameter(graph_test3)-shortest.paths(graph_test3))/diameter(graph_test3)
## A B C D E F
## A 1.00 0.75 0.50 0.50 0.25 0.00
## B 0.75 1.00 0.75 0.75 0.50 0.25
## C 0.50 0.75 1.00 0.50 0.75 0.50
## D 0.50 0.75 0.50 1.00 0.75 0.50
## E 0.25 0.50 0.75 0.75 1.00 0.75
## F 0.00 0.25 0.50 0.50 0.75 1.00
heatmap.2((diameter(graph_test3)-shortest.paths(graph_test3))/diameter(graph_test3), scale = "none", trace = "none", col = bluered(50), colsep = 1:6, rowsep = 1:6)
make_distance_graph(graph_test3, absolute = F)
## A B C D E F
## A 1.00000000 0.25000000 0.125 0.125 0.08333333 0.06250000
## B 0.25000000 1.00000000 0.250 0.250 0.12500000 0.08333333
## C 0.12500000 0.25000000 1.000 0.125 0.25000000 0.12500000
## D 0.12500000 0.25000000 0.125 1.000 0.25000000 0.12500000
## E 0.08333333 0.12500000 0.250 0.250 1.00000000 0.25000000
## F 0.06250000 0.08333333 0.125 0.125 0.25000000 1.00000000
make_distance_graph(graph_test3, absolute = T)
## A B C D E F
## A 1.00 0.75 0.50 0.50 0.25 0.00
## B 0.75 1.00 0.75 0.75 0.50 0.25
## C 0.50 0.75 1.00 0.50 0.75 0.50
## D 0.50 0.75 0.50 1.00 0.75 0.50
## E 0.25 0.50 0.75 0.75 1.00 0.75
## F 0.00 0.25 0.50 0.50 0.75 1.00
##Sigma matrix
#sigma from adj mat
make_sigma_mat_graph(graph_test3, 0.8)
## A B C D E F
## A 1.0 0.8 0.0 0.0 0.0 0.0
## B 0.8 1.0 0.8 0.8 0.0 0.0
## C 0.0 0.8 1.0 0.0 0.8 0.0
## D 0.0 0.8 0.0 1.0 0.8 0.0
## E 0.0 0.0 0.8 0.8 1.0 0.8
## F 0.0 0.0 0.0 0.0 0.8 1.0
heatmap.2(make_sigma_mat_graph(graph_test3, 0.8), scale = "none", trace = "none", col = bluered(50), colsep = 1:6, rowsep = 1:6)
#sigma from comm mat
make_sigma_mat_graph(graph_test3, 0.8, comm = T)
## A B C D E F
## A 1.0 0.0 0.4 0.4 0.0 0.0
## B 0.0 1.0 0.0 0.0 0.8 0.0
## C 0.4 0.0 1.0 0.8 0.0 0.4
## D 0.4 0.0 0.8 1.0 0.0 0.4
## E 0.0 0.8 0.0 0.0 1.0 0.0
## F 0.0 0.0 0.4 0.4 0.0 1.0
heatmap.2(make_sigma_mat_graph(graph_test3, 0.8, comm = T), scale = "none", trace = "none", col = bluered(50), colsep = 1:6, rowsep = 1:6)
# sigma from distance matrix
make_sigma_mat_dist_graph(graph_test3, 0.8, absolute = T)
## A B C D E F
## A 1.0000000 0.8000000 0.5333333 0.5333333 0.2666667 0.0000000
## B 0.8000000 1.0000000 0.8000000 0.8000000 0.5333333 0.2666667
## C 0.5333333 0.8000000 1.0000000 0.5333333 0.8000000 0.5333333
## D 0.5333333 0.8000000 0.5333333 1.0000000 0.8000000 0.5333333
## E 0.2666667 0.5333333 0.8000000 0.8000000 1.0000000 0.8000000
## F 0.0000000 0.2666667 0.5333333 0.5333333 0.8000000 1.0000000
make_sigma_mat_dist_graph(graph_test3, 0.8, absolute = F)
## A B C D E F
## A 1.0000000 0.8000000 0.4 0.4 0.2666667 0.2000000
## B 0.8000000 1.0000000 0.8 0.8 0.4000000 0.2666667
## C 0.4000000 0.8000000 1.0 0.4 0.8000000 0.4000000
## D 0.4000000 0.8000000 0.4 1.0 0.8000000 0.4000000
## E 0.2666667 0.4000000 0.8 0.8 1.0000000 0.8000000
## F 0.2000000 0.2666667 0.4 0.4 0.8000000 1.0000000
heatmap.2(make_sigma_mat_dist_graph(graph_test3, 0.8, absolute = T), scale = "none", trace = "none", col = bluered(50), colsep = 1:6, rowsep = 1:6)
heatmap.2(make_sigma_mat_dist_graph(graph_test3, 0.8, absolute = F), scale = "none", trace = "none", col = bluered(50), colsep = 1:6, rowsep = 1:6)
#Simulated expression and observed correlation
#simulate expression data
#adj mat
expr <- generate_expression(100, graph_test3, cor = 0.8, mean = 0, comm =F) # unable to generate from adj mat ## fixed with positive definite correction
## Warning in generate_expression(100, graph_test3, 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:6, rowsep = 1:6)
heatmap.2(cor(t(expr)), scale = "none", trace = "none", col = bluered(50), colsep = 1:6, rowsep = 1:6)
#comm mat
expr <- generate_expression(100, graph_test3, cor = 0.8, mean = 0, comm =T) #expression from comm mat
heatmap.2(expr, scale = "none", trace = "none", col = bluered(50), colsep = 1:6, rowsep = 1:6)
heatmap.2(cor(t(expr)), scale = "none", trace = "none", col = bluered(50), colsep = 1:6, rowsep = 1:6)
#absolute dist
expr<- generate_expression(100, graph_test3, 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_test3, 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:6, rowsep = 1:6)
heatmap.2(cor(t(expr)), scale = "none", trace = "none", col = bluered(50), colsep = 1:6, rowsep = 1:6)
# relative dist
expr<- generate_expression(100, graph_test3, cor = 0.8, mean = 0, comm = F, dist = T, absolute = F)
## Warning in generate_expression(100, graph_test3, 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:6, rowsep = 1:6)
heatmap.2(cor(t(expr)), scale = "none", trace = "none", col = bluered(50), colsep = 1:6, rowsep = 1:6)