1 ## makeLabel.R (2008-07-03)
5 ## Copyright 2008 Emmanuel Paradis
7 ## This file is part of the R-package `ape'.
8 ## See the file ../COPYING for licensing issues.
10 makeLabel <- function(x, ...) UseMethod("makeLabel")
12 makeLabel.character <- function(x, len = 99, space = "_",
13 make.unique = TRUE, illegal = "():;,[]", quote = FALSE, ...)
15 x <- gsub("[[:space:]]", space, x)
17 illegal <- unlist(strsplit(illegal, NULL))
18 for (i in illegal) x <- gsub(i, "", x, fixed = TRUE)
20 if (quote) len <- len - 2
22 if (any(nc)) x[nc] <- substr(x[nc], 1, len)
24 if (all(tab == 1)) make.unique <- FALSE
26 dup <- tab[which(tab > 1)]
28 for (i in 1:length(dup)) {
29 j <- which(x == nms[i])
31 ## w: number of characters to be added as suffix
32 w <- floor(log10(dup[i])) + 1
33 suffix <- formatC(1:dup[i], width = w, flag = "0")
36 substr(x[j], start, end) <- suffix
37 } else x[j] <- paste(x[j], suffix, sep = "")
40 if (quote) x <- paste('"', x, '"', sep = "")
44 makeLabel.phylo <- function(x, tips = TRUE, nodes = TRUE, ...)
47 x$tip.label <- makeLabel.character(x$tip.label, ...)
48 if (!is.null(x$node.label) && nodes)
49 x$node.label <- makeLabel.character(x$node.label, ...)
53 makeLabel.multiPhylo <- function(x, tips = TRUE, nodes = TRUE, ...)
55 y <- attr(x, "TipLabel")
57 for (i in 1:length(x))
58 x[[i]] <- makeLabel.phylo(x[[i]], tips = tips, nodes = nodes, ...)
60 attr(x, "TipLabel") <- makeLabel.character(y, ...)
65 makeLabel.DNAbin <- function(x, ...)
67 if (is.vector(x) || is.list(x))
68 names(x) <- makeLabel.character(names(x), ...)
69 else rownames(x) <- makeLabel.character(rownames(x), ...)