-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDendrogram.Rmd
More file actions
168 lines (138 loc) · 5.03 KB
/
Copy pathDendrogram.Rmd
File metadata and controls
168 lines (138 loc) · 5.03 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# ============================================================
# AA vs Structural Tree Comparison (Tanglegram + Cluster Coloring)
#
# Description:
# Compares a FoldMason structural guide tree (.nw) with an
# IQ-TREE amino acid consensus tree (.contree) using a tanglegram.
# Clusters are cut from the LEFT (structural) dendrogram and used
# to color tip labels and connecting lines. Branches are neutral gray.
#
# Input:
# - Structural guide tree: <prefix>_structure.nw
# - AA consensus tree: <prefix>_AA.contree
#
# Output:
# - <prefix>_AA_vs_Struct_tanglegram.png
# - <prefix>_AA_vs_Struct_tree_metrics.csv
#
# Dependencies:
# install.packages(c("ape", "phangorn", "dendextend", "viridisLite"))
#
# Usage:
# Set `prefix` and `out_dir` below, then source the script.
# ============================================================
library(ape)
library(phangorn)
library(dendextend)
library(viridisLite)
# ----------------------------
# USER PARAMETERS
# ----------------------------
prefix <- "lysB" # shared filename prefix
data_dir <- "data" # directory containing input trees
out_dir <- "results" # directory for outputs
h_rel <- 0.25 # relative height for cluster cutoff (0–1)
random_seed <- 1 # for reproducible untangling
# ----------------------------
# #1 — Resolve file paths
# ----------------------------
tL_file <- file.path(data_dir, paste0(prefix, "_structure.nw"))
tR_file <- file.path(data_dir, paste0(prefix, "_AA.contree"))
stopifnot(file.exists(tL_file), file.exists(tR_file))
# ----------------------------
# #2 — Read trees
# ----------------------------
tL <- read.tree(tL_file)
tR <- read.tree(tR_file)
cat("Original tip counts:\n")
cat(" LEFT (structural):", length(tL$tip.label), "\n")
cat(" RIGHT (AA): ", length(tR$tip.label), "\n")
# ----------------------------
# #3 — Keep only shared tips
# ----------------------------
shared <- intersect(tL$tip.label, tR$tip.label)
tL <- keep.tip(tL, shared)
tR <- keep.tip(tR, shared)
cat("Shared tips:", length(shared), "\n")
stopifnot(setequal(tL$tip.label, tR$tip.label))
# Midpoint root both trees
tL <- midpoint(tL)
tR <- midpoint(tR)
# ----------------------------
# #4 — Resolve polytomies if needed
# ----------------------------
if (!is.binary(tL)) tL <- multi2di(tL)
if (!is.binary(tR)) tR <- multi2di(tR)
tL <- ladderize(tL)
tR <- ladderize(tR)
# ----------------------------
# #5 — Convert phylo → dendrogram via cophenetic distances
# (avoids ultrametric requirement)
# ----------------------------
dL <- as.dendrogram(hclust(as.dist(cophenetic(tL)), method = "average"))
dR <- as.dendrogram(hclust(as.dist(cophenetic(tR)), method = "average"))
# ----------------------------
# #6 — Define clusters on LEFT tree at relative height
# ----------------------------
maxh <- attr(dL, "height")
clL <- cutree(dL, h = h_rel * maxh)
k <- length(unique(clL))
pal <- setNames(viridis(k), sort(unique(clL)))
stopifnot(all(labels(dL) %in% names(clL)))
stopifnot(all(labels(dR) %in% names(clL)))
cat("Number of clusters (k =", k, ") at h_rel =", h_rel, "\n")
# ----------------------------
# #7 — Color tip labels by LEFT cluster membership
# ----------------------------
dL_col <- set(dL, "labels_col", pal[clL[labels(dL)]])
dR_col <- set(dR, "labels_col", pal[clL[labels(dR)]])
# Neutral branch styling
dL_col <- set(dL_col, "branches_col", "gray20")
dR_col <- set(dR_col, "branches_col", "gray20")
dL_col <- set(dL_col, "branches_lwd", 1)
dR_col <- set(dR_col, "branches_lwd", 1)
# ----------------------------
# #8 — Untangle to reduce line crossings
# ----------------------------
set.seed(random_seed)
unt <- untangle(dL_col, dR_col, method = "step2side")
dL_col <- unt[[1]]
dR_col <- unt[[2]]
# Color connecting lines by LEFT cluster
line_cols <- pal[clL[labels(dL_col)]]
# ----------------------------
# #9 — Plot tanglegram
# ----------------------------
dir.create(out_dir, showWarnings = FALSE, recursive = TRUE)
png_file <- file.path(out_dir, paste0(prefix, "_AA_vs_Struct_tanglegram.png"))
png(filename = png_file, width = 12, height = 9, units = "in", res = 300)
tanglegram(
dL_col, dR_col,
lab.cex = 1,
margin_inner = 5.3,
highlight_distinct_edges = FALSE,
common_subtrees_color_lines = FALSE,
lwd = 1,
color_lines = line_cols
)
dev.off()
cat("Wrote:", png_file, "\n")
# ----------------------------
# #10 — Tree comparison metrics
# ----------------------------
# Normalized Robinson–Foulds distance (topology only)
rf <- RF.dist(unroot(tL), unroot(tR), normalize = TRUE)
# Entanglement metric
ent <- entanglement(dL_col, dR_col)
cat("Normalized RF distance:", round(rf, 4), "\n")
cat("Entanglement: ", round(ent, 4), "\n")
csv_file <- file.path(out_dir, paste0(prefix, "_AA_vs_Struct_tree_metrics.csv"))
write.csv(
data.frame(
metric = c("Normalized_RF", "Entanglement"),
value = c(rf, ent)
),
csv_file,
row.names = FALSE
)
cat("Wrote:", csv_file, "\n")