-
Notifications
You must be signed in to change notification settings - Fork 5
/
plot_alignment.R
145 lines (122 loc) · 5.01 KB
/
plot_alignment.R
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
plot_alignment <- function(
g_filepath,
h_filepath,
alignment_filepath,
output_filepath,
st = 0,
hide_singletons = FALSE
) {
# # Load necessary libraries
# required_packages <- c("igraph", "ggraph", "ggplot2", "readr", "dplyr")
# installed_packages <- rownames(installed.packages())
# for (pkg in required_packages) {
# if (!pkg %in% installed_packages) {
# install.packages(pkg, dependencies = TRUE)
# }
# }
library(igraph)
library(ggraph)
library(ggplot2)
library(readr)
library(dplyr)
# Helper function to read adjacency matrix
read_adjacency <- function(filepath) {
df <- read_csv(filepath, show_col_types = FALSE)
mat <- as.matrix(df[, -1])
rownames(mat) <- df[[1]]
return(mat)
}
# Read adjacency matrices
g_mat <- read_adjacency(g_filepath)
h_mat <- read_adjacency(h_filepath)
# Create igraph objects (assuming undirected graphs; adjust 'mode' if needed)
g_graph <- graph_from_adjacency_matrix(g_mat, mode = "undirected", diag = FALSE)
h_graph <- graph_from_adjacency_matrix(h_mat, mode = "undirected", diag = FALSE)
# Read alignment matrix
alignment_df <- read_csv(alignment_filepath, show_col_types = FALSE)
alignment_mat <- as.matrix(alignment_df[, -1])
rownames(alignment_mat) <- alignment_df[[1]]
# Extract alignment pairs with similarity > the similarity threshold
alignment_pairs <- which(alignment_mat > st, arr.ind = TRUE)
align_df <- data.frame(
g_node = rownames(alignment_mat)[alignment_pairs[, 1]],
h_node = colnames(alignment_mat)[alignment_pairs[, 2]],
similarity = alignment_mat[alignment_pairs]
)
if (hide_singletons) {
# Remove nodes in G and H with degree 0
deg_g <- degree(g_graph)
non_single_g <- names(deg_g[deg_g > 0])
g_graph <- induced_subgraph(g_graph, vids = non_single_g)
deg_h <- degree(h_graph)
non_single_h <- names(deg_h[deg_h > 0])
h_graph <- induced_subgraph(h_graph, vids = non_single_h)
# Update alignment_df to include only existing nodes after subsetting
align_df <- align_df %>%
filter(g_node %in% V(g_graph)$name & h_node %in% V(h_graph)$name)
}
# Rename nodes to ensure uniqueness
V(g_graph)$name <- paste0("G_", V(g_graph)$name)
V(h_graph)$name <- paste0("H_", V(h_graph)$name)
combined_graph <- disjoint_union(g_graph, h_graph)
# Prepare alignment edges
if (nrow(align_df) > 0) {
align_edges <- align_df %>%
mutate(
from = paste0("G_", g_node),
to = paste0("H_", h_node)
)
} else {
align_edges <- data.frame(from = character(0), to = character(0), similarity = numeric(0))
}
# Create a layout with G on the left and H on the right
set.seed(123) # For reproducibility
layout_g <- layout_with_fr(g_graph)
layout_h <- layout_with_fr(h_graph)
# Shift H layout to the right of G
shift_x <- max(layout_g[, 1]) - min(layout_h[, 1]) + 5
layout_h[, 1] <- layout_h[, 1] + shift_x
# Combine layouts
combined_layout <- rbind(layout_g, layout_h)
V(combined_graph)$x <- combined_layout[, 1]
V(combined_graph)$y <- combined_layout[, 2]
# Prepare data for alignment edges
if (nrow(align_edges) > 0) {
align_coords <- align_edges %>%
mutate(
from_x = V(combined_graph)$x[match(from, V(combined_graph)$name)],
from_y = V(combined_graph)$y[match(from, V(combined_graph)$name)],
to_x = V(combined_graph)$x[match(to, V(combined_graph)$name)],
to_y = V(combined_graph)$y[match(to, V(combined_graph)$name)]
)
}
# Plot #
# Adjust the plot size based on the number of nodes
num_nodes <- vcount(combined_graph)
plot_width <- max(2000, 100 + 20 * num_nodes)
plot_height <- max(1000, 100 + 10 * num_nodes)
png(filename = output_filepath, width = plot_width, height = plot_height, res = 150)
p <- ggraph(combined_graph, layout = "manual", x = V(combined_graph)$x, y = V(combined_graph)$y) +
# Internal edges within G and H
geom_edge_link(aes(alpha = 0.5), color = "grey", linewidth = 0.5) +
# G nodes in red, H in blue
geom_node_point(aes(color = ifelse(startsWith(name, "G_"), "G", "H")), size = 3) +
# Node labels without the prefix
geom_node_text(aes(label = gsub("^[GH]_", "", name)), repel = TRUE, size = 3)
# Color mapping
scale_color_manual(values = c("G" = "red", "H" = "blue")) +
theme_void() +
theme(legend.position = "none")
if (nrow(align_edges) > 0) {
# Add alignment edges
p <- p +
geom_segment(
data = align_coords,
aes(x = from_x, y = from_y, xend = to_x, yend = to_y),
color = "purple", linewidth = 0.5, alpha = 0.6
)
}
print(p)
dev.off()
message("Plot saved to ", output_filepath)
}