##Install dependencies for Demonstration

library('mvtnorm')
library("igraph")
library("gplots")
library("graphsim")
Rprof("test_graph_network.out", interval = 0.001, memory.profiling = F)
#Rprof("test_graph_network.mem.out", interval = 0.001, memory.profiling = T)

##set up simulated graphs

graph_test4_edges <- rbind(c("A", "C"), c("B", "C"), c("C", "D"), c("D", "E"), c("D", "F"), c("F", "G"), c("F", "I"), c("H", "I"))
graph_test4 <- graph.edgelist(graph_test4_edges, directed = T)
plot_directed(graph_test4, layout = layout.kamada.kawai)

plot of chunk unnamed-chunk-2

#Generated simulated expression data from graph

##Adjacency matrix

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

plot of chunk unnamed-chunk-3

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

plot of chunk unnamed-chunk-3

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

plot of chunk unnamed-chunk-3

##Distance matrix

shortest.paths(graph_test4)
##   A C B D E F G I H
## A 0 1 2 2 3 3 4 4 5
## C 1 0 1 1 2 2 3 3 4
## B 2 1 0 2 3 3 4 4 5
## D 2 1 2 0 1 1 2 2 3
## E 3 2 3 1 0 2 3 3 4
## F 3 2 3 1 2 0 1 1 2
## G 4 3 4 2 3 1 0 2 3
## I 4 3 4 2 3 1 2 0 1
## H 5 4 5 3 4 2 3 1 0
heatmap.2(shortest.paths(graph_test4), scale = "none", trace = "none", col = bluered(50), colsep = 1:length(V(graph_test4)), rowsep = 1:length(V(graph_test4)))

plot of chunk unnamed-chunk-4

(diameter(graph_test4)-shortest.paths(graph_test4))/diameter(graph_test4)
##       A    C     B    D    E    F    G    I     H
## A  1.00 0.75  0.50 0.50 0.25 0.25 0.00 0.00 -0.25
## C  0.75 1.00  0.75 0.75 0.50 0.50 0.25 0.25  0.00
## B  0.50 0.75  1.00 0.50 0.25 0.25 0.00 0.00 -0.25
## D  0.50 0.75  0.50 1.00 0.75 0.75 0.50 0.50  0.25
## E  0.25 0.50  0.25 0.75 1.00 0.50 0.25 0.25  0.00
## F  0.25 0.50  0.25 0.75 0.50 1.00 0.75 0.75  0.50
## G  0.00 0.25  0.00 0.50 0.25 0.75 1.00 0.50  0.25
## I  0.00 0.25  0.00 0.50 0.25 0.75 0.50 1.00  0.75
## H -0.25 0.00 -0.25 0.25 0.00 0.50 0.25 0.75  1.00
heatmap.2((diameter(graph_test4)-shortest.paths(graph_test4))/diameter(graph_test4), scale = "none", trace = "none", col = bluered(50), colsep = 1:length(V(graph_test4)), rowsep = 1:length(V(graph_test4)))

plot of chunk unnamed-chunk-4

make_distance_graph(graph_test4, absolute = F)
##            A          C          B          D          E          F          G
## A 1.00000000 0.25000000 0.12500000 0.12500000 0.08333333 0.08333333 0.06250000
## C 0.25000000 1.00000000 0.25000000 0.25000000 0.12500000 0.12500000 0.08333333
## B 0.12500000 0.25000000 1.00000000 0.12500000 0.08333333 0.08333333 0.06250000
## D 0.12500000 0.25000000 0.12500000 1.00000000 0.25000000 0.25000000 0.12500000
## E 0.08333333 0.12500000 0.08333333 0.25000000 1.00000000 0.12500000 0.08333333
## F 0.08333333 0.12500000 0.08333333 0.25000000 0.12500000 1.00000000 0.25000000
## G 0.06250000 0.08333333 0.06250000 0.12500000 0.08333333 0.25000000 1.00000000
## I 0.06250000 0.08333333 0.06250000 0.12500000 0.08333333 0.25000000 0.12500000
## H 0.05000000 0.06250000 0.05000000 0.08333333 0.06250000 0.12500000 0.08333333
##            I          H
## A 0.06250000 0.05000000
## C 0.08333333 0.06250000
## B 0.06250000 0.05000000
## D 0.12500000 0.08333333
## E 0.08333333 0.06250000
## F 0.25000000 0.12500000
## G 0.12500000 0.08333333
## I 1.00000000 0.25000000
## H 0.25000000 1.00000000
make_distance_graph(graph_test4, absolute = T)
##       A    C     B    D    E    F    G    I     H
## A  1.00 0.75  0.50 0.50 0.25 0.25 0.00 0.00 -0.25
## C  0.75 1.00  0.75 0.75 0.50 0.50 0.25 0.25  0.00
## B  0.50 0.75  1.00 0.50 0.25 0.25 0.00 0.00 -0.25
## D  0.50 0.75  0.50 1.00 0.75 0.75 0.50 0.50  0.25
## E  0.25 0.50  0.25 0.75 1.00 0.50 0.25 0.25  0.00
## F  0.25 0.50  0.25 0.75 0.50 1.00 0.75 0.75  0.50
## G  0.00 0.25  0.00 0.50 0.25 0.75 1.00 0.50  0.25
## I  0.00 0.25  0.00 0.50 0.25 0.75 0.50 1.00  0.75
## H -0.25 0.00 -0.25 0.25 0.00 0.50 0.25 0.75  1.00

##Sigma matrix

#sigma from adj mat
make_sigma_mat_graph(graph_test4, 0.8)
##     A   C   B   D   E   F   G   I   H
## A 1.0 0.8 0.0 0.0 0.0 0.0 0.0 0.0 0.0
## C 0.8 1.0 0.8 0.8 0.0 0.0 0.0 0.0 0.0
## B 0.0 0.8 1.0 0.0 0.0 0.0 0.0 0.0 0.0
## D 0.0 0.8 0.0 1.0 0.8 0.8 0.0 0.0 0.0
## E 0.0 0.0 0.0 0.8 1.0 0.0 0.0 0.0 0.0
## F 0.0 0.0 0.0 0.8 0.0 1.0 0.8 0.8 0.0
## G 0.0 0.0 0.0 0.0 0.0 0.8 1.0 0.0 0.0
## I 0.0 0.0 0.0 0.0 0.0 0.8 0.0 1.0 0.8
## H 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.8 1.0
heatmap.2(make_sigma_mat_graph(graph_test4, 0.8), scale = "none", trace = "none", col = bluered(50), colsep = 1:length(V(graph_test4)), rowsep = 1:length(V(graph_test4)))

plot of chunk unnamed-chunk-5

#sigma from comm mat
make_sigma_mat_graph(graph_test4, 0.8, comm = T)
##     A   C   B   D   E   F   G   I   H
## A 1.0 0.0 0.8 0.8 0.0 0.0 0.0 0.0 0.0
## C 0.0 1.0 0.0 0.0 0.8 0.8 0.0 0.0 0.0
## B 0.8 0.0 1.0 0.8 0.0 0.0 0.0 0.0 0.0
## D 0.8 0.0 0.8 1.0 0.0 0.0 0.8 0.8 0.0
## E 0.0 0.8 0.0 0.0 1.0 0.8 0.0 0.0 0.0
## F 0.0 0.8 0.0 0.0 0.8 1.0 0.0 0.0 0.8
## G 0.0 0.0 0.0 0.8 0.0 0.0 1.0 0.8 0.0
## I 0.0 0.0 0.0 0.8 0.0 0.0 0.8 1.0 0.0
## H 0.0 0.0 0.0 0.0 0.0 0.8 0.0 0.0 1.0
heatmap.2(make_sigma_mat_graph(graph_test4, 0.8, comm = T), scale = "none", trace = "none", col = bluered(50), colsep = 1:length(V(graph_test4)), rowsep = 1:length(V(graph_test4)))

plot of chunk unnamed-chunk-5

# sigma from distance matrix
make_sigma_mat_dist_graph(graph_test4, 0.8, absolute = T)
##           A         C         B         D         E         F         G
## A 1.0000000 0.8000000 0.5333333 0.5333333 0.2666667 0.2666667 0.0000000
## C 0.8000000 1.0000000 0.8000000 0.8000000 0.5333333 0.5333333 0.2666667
## B 0.5333333 0.8000000 1.0000000 0.5333333 0.2666667 0.2666667 0.0000000
## D 0.5333333 0.8000000 0.5333333 1.0000000 0.8000000 0.8000000 0.5333333
## E 0.2666667 0.5333333 0.2666667 0.8000000 1.0000000 0.5333333 0.2666667
## F 0.2666667 0.5333333 0.2666667 0.8000000 0.5333333 1.0000000 0.8000000
## G 0.0000000 0.2666667 0.0000000 0.5333333 0.2666667 0.8000000 1.0000000
## I 0.0000000 0.2666667 0.0000000 0.5333333 0.2666667 0.8000000 0.5333333
## H 0.0000000 0.0000000 0.0000000 0.2666667 0.0000000 0.5333333 0.2666667
##           I         H
## A 0.0000000 0.0000000
## C 0.2666667 0.0000000
## B 0.0000000 0.0000000
## D 0.5333333 0.2666667
## E 0.2666667 0.0000000
## F 0.8000000 0.5333333
## G 0.5333333 0.2666667
## I 1.0000000 0.8000000
## H 0.8000000 1.0000000
make_sigma_mat_dist_graph(graph_test4, 0.8, absolute = F)
##           A         C         B         D         E         F         G
## A 1.0000000 0.8000000 0.4000000 0.4000000 0.2666667 0.2666667 0.2000000
## C 0.8000000 1.0000000 0.8000000 0.8000000 0.4000000 0.4000000 0.2666667
## B 0.4000000 0.8000000 1.0000000 0.4000000 0.2666667 0.2666667 0.2000000
## D 0.4000000 0.8000000 0.4000000 1.0000000 0.8000000 0.8000000 0.4000000
## E 0.2666667 0.4000000 0.2666667 0.8000000 1.0000000 0.4000000 0.2666667
## F 0.2666667 0.4000000 0.2666667 0.8000000 0.4000000 1.0000000 0.8000000
## G 0.2000000 0.2666667 0.2000000 0.4000000 0.2666667 0.8000000 1.0000000
## I 0.2000000 0.2666667 0.2000000 0.4000000 0.2666667 0.8000000 0.4000000
## H 0.1600000 0.2000000 0.1600000 0.2666667 0.2000000 0.4000000 0.2666667
##           I         H
## A 0.2000000 0.1600000
## C 0.2666667 0.2000000
## B 0.2000000 0.1600000
## D 0.4000000 0.2666667
## E 0.2666667 0.2000000
## F 0.8000000 0.4000000
## G 0.4000000 0.2666667
## I 1.0000000 0.8000000
## H 0.8000000 1.0000000
heatmap.2(make_sigma_mat_dist_graph(graph_test4, 0.8, absolute = T), scale = "none", trace = "none", col = bluered(50), colsep = 1:length(V(graph_test4)), rowsep = 1:length(V(graph_test4)))

plot of chunk unnamed-chunk-5

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

plot of chunk unnamed-chunk-5

#Simulated expression and observed correlation

#simulate expression data
#adj mat
expr <- generate_expression(100, graph_test4, cor = 0.8, mean = 0, comm =F) # unable to generate from adj mat ## fixed with positive definite correction
## Warning in generate_expression(100, graph_test4, 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:length(V(graph_test4)), rowsep = 1:length(V(graph_test4)))

plot of chunk unnamed-chunk-6

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

plot of chunk unnamed-chunk-6

#comm mat
expr <- generate_expression(100, graph_test4, cor = 0.8, mean = 0, comm =T) #expression from comm mat
## Warning in generate_expression(100, graph_test4, cor = 0.8, mean = 0, comm = T):
## sigma matrix was not positive definite, nearest approximation used.
heatmap.2(expr, scale = "none", trace = "none", col = bluered(50), colsep = 1:length(V(graph_test4)), rowsep = 1:length(V(graph_test4)))

plot of chunk unnamed-chunk-6

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

plot of chunk unnamed-chunk-6

#absolute dist
expr<- generate_expression(100, graph_test4, 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_test4, 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:length(V(graph_test4)), rowsep = 1:length(V(graph_test4)))

plot of chunk unnamed-chunk-6

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

plot of chunk unnamed-chunk-6

# relative dist
expr<- generate_expression(100, graph_test4, cor = 0.8, mean = 0, comm = F, dist = T, absolute = F)
## Warning in generate_expression(100, graph_test4, 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:length(V(graph_test4)), rowsep = 1:length(V(graph_test4)))

plot of chunk unnamed-chunk-6

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

plot of chunk unnamed-chunk-6