Skip to content

Commit 103face

Browse files
committed
Add documentation and README
1 parent 927576e commit 103face

12 files changed

Lines changed: 1759 additions & 104 deletions

.Rbuildignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
^mutationtree\.Rproj$
22
^\.Rproj\.user$
33
^LICENSE\.md$
4+
^\.github$

.github/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.html

.github/workflows/R-CMD-check.yaml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
2+
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
3+
on:
4+
push:
5+
branches: [main, master]
6+
pull_request:
7+
8+
name: R-CMD-check.yaml
9+
10+
permissions: read-all
11+
12+
jobs:
13+
R-CMD-check:
14+
runs-on: ${{ matrix.config.os }}
15+
16+
name: ${{ matrix.config.os }} (${{ matrix.config.r }})
17+
18+
strategy:
19+
fail-fast: false
20+
matrix:
21+
config:
22+
- {os: macos-latest, r: 'release'}
23+
- {os: windows-latest, r: 'release'}
24+
- {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'}
25+
- {os: ubuntu-latest, r: 'release'}
26+
- {os: ubuntu-latest, r: 'oldrel-1'}
27+
28+
env:
29+
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
30+
R_KEEP_PKG_SOURCE: yes
31+
32+
steps:
33+
- uses: actions/checkout@v4
34+
35+
- uses: r-lib/actions/setup-pandoc@v2
36+
37+
- uses: r-lib/actions/setup-r@v2
38+
with:
39+
r-version: ${{ matrix.config.r }}
40+
http-user-agent: ${{ matrix.config.http-user-agent }}
41+
use-public-rspm: true
42+
43+
- uses: r-lib/actions/setup-r-dependencies@v2
44+
with:
45+
extra-packages: any::rcmdcheck
46+
needs: check
47+
48+
- uses: r-lib/actions/check-r-package@v2
49+
with:
50+
upload-snapshots: true
51+
build_args: 'c("--no-manual","--compact-vignettes=gs+qpdf")'

DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ License: MIT + file LICENSE
88
Encoding: UTF-8
99
Roxygen: list(markdown = TRUE)
1010
RoxygenNote: 7.3.2
11-
Imports:
11+
Imports:
1212
ape,
1313
dplyr,
1414
fs,
@@ -17,6 +17,7 @@ Imports:
1717
labbook,
1818
magrittr,
1919
purrr,
20+
seqUtils,
2021
stats,
2122
stringr,
2223
sysfonts

R/draw_mutation_tree.R

Lines changed: 92 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,82 @@
1-
#' Draw a mutation annotated tree
1+
#' Draw a mutation-annotated phylogenetic tree
22
#'
3-
#' @param tree_and_sequences_asr ...
4-
#' @param file ...
5-
#' @param width ...
6-
#' @param height_per_sequence ...
7-
#' @param lines ...
8-
#' @param modified_tip_labels ...
9-
#' @param modified_node_labels ...
10-
#' @param additional_node_labels ...
11-
#' @param tip_text_cex ...
12-
#' @param tip_text_label_color ...
13-
#' @param tip_text_nudge ...
14-
#' @param tip_text_adj ...
15-
#' @param node_text_cex ...
16-
#' @param node_text_nudge ...
17-
#' @param node_text_adj ...
18-
#' @param tip_node_text_aa_mutation_color ...
19-
#' @param tip_node_text_aa_mutation_special_colors ...
20-
#' @param tip_node_text_syn_mutation_color ...
21-
#' @param line_width ...
22-
#' @param line_color ...
23-
#' @param line_text_color ...
24-
#' @param line_text_cex ...
25-
#' @param line_text_adj ...
26-
#' @param line_text_nudge ...
27-
#' @param node_addtext_cex ...
28-
#' @param node_addtext_color ...
29-
#' @param node_addtext_nudge ...
30-
#' @param node_addtext_adj ...
31-
#' @param node_point_color ...
32-
#' @param node_point_cex ...
33-
#' @param edge_color ...
34-
#' @param edge_width ...
35-
#' @param x_lim_expand ...
36-
#' @param y_lim_expand ...
37-
#' @param node_nums ...
3+
#' Creates a visualization of a phylogenetic tree with non-synonymous (amino acid)
4+
#' and synonymous mutations labeled on branches. Supports extensive customization
5+
#' of labels, colors, and annotations.
386
#'
39-
#' @returns ...
7+
#' @param tree_and_sequences_asr A tree_and_sequences object with ancestral sequence
8+
#' reconstruction, created by \code{convergence::addASRusher()}.
9+
#' @param file Output file path. If NULL, displays interactively. Supported formats:
10+
#' PNG (600 dpi) or PDF.
11+
#' @param width Figure width in inches. Default: 6.
12+
#' @param height_per_sequence Height per tip in inches. Total height is calculated
13+
#' as \code{height_per_sequence * n_tips}. Default: 0.03.
14+
#'
15+
#' @section Annotation parameters:
16+
#' @param lines List of lists defining horizontal reference lines. Each element:
17+
#' \code{list(height, text, line_color, text_color, line_width, text_cex, text_adj, text_nudge)}.
18+
#' @param modified_tip_labels List of lists to customize tip labels. Each element:
19+
#' \code{list(old_label, new_label, text_color, text_cex)}.
20+
#' @param modified_node_labels List of lists to customize node mutation labels. Each element:
21+
#' \code{list(node, text_color, text_cex)}.
22+
#' @param additional_node_labels List of lists to add extra annotations. Each element:
23+
#' \code{list(node, text, text_color, text_cex, text_adj, text_nudge, point_color, point_cex)}.
24+
#'
25+
#' @section Tip label styling:
26+
#' @param tip_text_cex Tip label text size. Default: 0.2.
27+
#' @param tip_text_label_color Tip label text color. Default: "grey70".
28+
#' @param tip_text_nudge Tip label position offset as \code{c(x, y)} fraction of plot range. Default: \code{c(0.002, 0)}.
29+
#' @param tip_text_adj Tip label text justification. Default: \code{c(0, 0.5)}.
30+
#'
31+
#' @section Node mutation styling:
32+
#' @param node_text_cex Node mutation label text size. Default: 0.2.
33+
#' @param node_text_nudge Node label position offset as \code{c(x, y)} fraction of plot range. Default: \code{c(-0.002, -0.015)}.
34+
#' @param node_text_adj Node label text justification. Default: \code{c(1, 1)}.
35+
#' @param tip_node_text_aa_mutation_color Default color for amino acid mutations. Default: "grey30".
36+
#' @param tip_node_text_aa_mutation_special_colors Named list mapping colors to amino acid positions
37+
#' to highlight, e.g. \code{list(red = c(145, 155), blue = c(189))}. Default: H3 epitope positions.
38+
#' @param tip_node_text_syn_mutation_color Color for synonymous mutation counts. Default: "grey70".
39+
#'
40+
#' @section Reference line styling:
41+
#' @param line_width Width of horizontal reference lines. Default: 0.3.
42+
#' @param line_color Color of horizontal reference lines. Default: "grey50".
43+
#' @param line_text_color Color of reference line text. Default: "black".
44+
#' @param line_text_cex Reference line text size. Default: 1.
45+
#' @param line_text_adj Reference line text justification. Default: \code{c(1, 1)}.
46+
#' @param line_text_nudge Reference line text position offset. Default: \code{c(-0.01, -0.003)}.
47+
#'
48+
#' @section Additional node annotation styling:
49+
#' @param node_addtext_cex Additional node label text size. Default: \code{node_text_cex * 4}.
50+
#' @param node_addtext_color Additional node label text color. Default: "black".
51+
#' @param node_addtext_nudge Additional node label position offset. Default: \code{c(-0.004, -0.006)}.
52+
#' @param node_addtext_adj Additional node label text justification. Default: \code{c(1, 1)}.
53+
#' @param node_point_color Additional node point color. Default: "black".
54+
#' @param node_point_cex Additional node point size. Default: 0.8.
55+
#'
56+
#' @section Tree styling:
57+
#' @param edge_color Color of tree edges. Default: "grey80".
58+
#' @param edge_width Width of tree edges. If NULL, uses ape default. Default: NULL.
59+
#' @param x_lim_expand Plot x-axis expansion as \code{c(left, right)} fractions. Default: \code{c(0.02, 0.2)}.
60+
#' @param y_lim_expand Plot y-axis expansion as \code{c(bottom, top)} fractions. Default: \code{c(0.02, 0.02)}.
61+
#' @param node_nums Logical; show node numbers for debugging. Default: FALSE.
62+
#'
63+
#' @returns If \code{file} is specified, returns the file path. Otherwise, displays plot interactively.
64+
#'
65+
#' @examples
66+
#' \dontrun{
67+
#' # Basic usage
68+
#' draw_mutation_tree(tree_with_asr)
69+
#'
70+
#' # Save to file
71+
#' draw_mutation_tree(tree_with_asr, file = "tree.png")
72+
#'
73+
#' # With annotations
74+
#' draw_mutation_tree(
75+
#' tree_with_asr,
76+
#' lines = list(list(height = 20, text = "Clade A")),
77+
#' modified_tip_labels = list(list(old_label = "seq1", text_color = "red"))
78+
#' )
79+
#' }
4080
#'
4181
#' @importFrom dplyr filter
4282
#' @export
@@ -57,13 +97,13 @@ draw_mutation_tree = function(
5797
tip_text_adj = c(0, 0.5),
5898

5999
node_text_cex = 0.2,
60-
node_text_nudge = c(-0.001, -0.001),
100+
node_text_nudge = c(-0.002, -0.015),
61101
node_text_adj = c(1, 1),
62102

63103
tip_node_text_aa_mutation_color = "grey30",
64104
tip_node_text_aa_mutation_special_colors = list(
65-
red = c(145, 155, 156, 158, 159, 189, 193),
66-
orange = c(44L, 45L, 46L, 47L, 48L, 50L, 51L, 53L, 54L, 57L, 59L, 62L, 63L, 67L, 75L, 78L, 80L, 81L, 82L, 83L, 86L, 87L, 88L, 91L, 92L, 94L, 96L, 102L, 103L, 109L, 117L, 121L, 122L, 124L, 126L, 128L, 129L, 130L, 131L, 132L, 133L, 135L, 137L, 138L, 140L, 142L, 143L, 144L, 145L, 146L, 150L, 152L, 155L, 156L, 157L, 158L, 159L, 163L, 165L, 167L, 168L, 170L, 171L, 172L, 173L, 174L, 175L, 176L, 177L, 179L, 182L, 186L, 187L, 188L, 189L, 190L, 192L, 193L, 194L, 196L, 197L, 198L, 201L, 203L, 207L, 208L, 209L, 212L, 213L, 214L, 215L, 216L, 217L, 218L, 219L, 226L, 227L, 228L, 229L, 230L, 238L, 240L, 242L, 244L, 246L, 247L, 248L, 260L, 261L, 262L, 265L, 273L, 275L, 276L, 278L, 279L, 280L, 294L, 297L, 299L, 300L, 304L, 305L, 307L, 308L, 309L, 310L, 311L, 312L) # fmt: skip
105+
red = seqUtils::h3_epitope_positions[["koel"]],
106+
orange = seqUtils::h3_epitope_positions[["wolf"]]
67107
),
68108
tip_node_text_syn_mutation_color = "grey70",
69109

@@ -153,20 +193,20 @@ draw_mutation_tree = function(
153193
)
154194

155195
segments(
156-
x0 = 0,
157-
x1 = 0 + 0.01 * x_lim[[2]] + 1,
158-
y0 = y_lim[[1]] + 0.05 * diff(y_lim),
159-
y1 = y_lim[[1]] + 0.05 * diff(y_lim),
196+
x0 = 0 + 0.01 * x_lim[[2]],
197+
x1 = 0 + 0.01 * x_lim[[2]] + 0.001,
198+
y0 = y_lim[[1]] + 0.1 * diff(y_lim),
199+
y1 = y_lim[[1]] + 0.1 * diff(y_lim),
160200
lwd = 0.7,
161-
col = "grey60"
201+
col = "grey40"
162202
)
163203
text(
164-
x = 0.5,
165-
y = y_lim[[1]] + 0.05 * diff(y_lim),
166-
label = "1 mutation",
167-
cex = 0.5,
168-
adj = c(0.5, -0.5),
169-
col = "grey60"
204+
x = 0 + 0.01 * x_lim[[2]] + 0.001 / 2,
205+
y = y_lim[[1]] + 0.1 * diff(y_lim),
206+
label = "1/1000 s/s",
207+
cex = 0.3,
208+
adj = c(0.5, -0.8),
209+
col = "grey40"
170210
)
171211

172212
# node mutation labels ---
@@ -347,7 +387,7 @@ draw_mutation_tree = function(
347387
x = ape::node.depth.edgelength(tree_and_sequences_asr$tree)[[node]]
348388
y = ape::node.height(tree_and_sequences_asr$tree)[[node]]
349389

350-
if (line_width > 0) {
390+
if (point_cex > 0) {
351391
points(
352392
x = x,
353393
y = y,

0 commit comments

Comments
 (0)