-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathvegemite.R
148 lines (148 loc) · 5.64 KB
/
vegemite.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
146
147
148
`vegemite` <-
function (x, use, scale, sp.ind = NULL, site.ind = NULL, zero = ".",
select, diagonalize = FALSE, ...)
{
if (!missing(use)) {
## derived index should be based on transformed & tabulated data
xprime <- if (missing(scale)) x
else coverscale(x, scale = scale, character = FALSE)
if (!is.list(use) && is.vector(use)) {
if (is.null(site.ind))
site.ind <- order(use)
if (is.null(sp.ind))
sp.ind <- order(wascores(use, xprime))
}
else if (inherits(use, c("hclust", "twins"))) {
if (inherits(use, "twins")) {
use <- as.hclust(use)
}
if (diagonalize) {
wts <- scores(cca(xprime), choices=1, display = "wa")
use <- reorder(use, wts)
}
if (is.null(site.ind))
site.ind <- use$order
if (is.null(sp.ind))
sp.ind <- order(wascores(order(site.ind), xprime))
}
else if (inherits(use, "dendrogram")) {
if (diagonalize) {
wts <- scores(cca(xprime), choices=1, display = "wa")
use <- reorder(use, wts)
}
if (is.null(site.ind)) {
site.ind <- seq_len(nrow(x))
names(site.ind) <- rownames(x)
site.ind <- site.ind[labels(use)]
}
if (is.null(sp.ind))
sp.ind <- order(wascores(order(site.ind), xprime))
}
else if (is.list(use)) {
tmp <- scores(use, choices = 1, display = "sites")
if (is.null(site.ind))
site.ind <- order(tmp)
if (is.null(sp.ind))
sp.ind <- try(order(scores(use, choices = 1,
display = "species")))
if (inherits(sp.ind, "try-error"))
sp.ind <- order(wascores(tmp, xprime))
}
else if (is.matrix(use)) {
tmp <- scores(use, choices = 1, display = "sites")
if (is.null(site.ind))
site.ind <- order(tmp)
if (is.null(sp.ind))
sp.ind <- order(wascores(tmp, xprime))
}
else if (is.factor(use)) {
tmp <- as.numeric(use)
if (diagonalize) {
ord <- scores(cca(xprime, use), choices = 1,
display = c("lc","wa","sp"))
if (cor(tmp, ord$constraints, method = "spearman") < 0) {
ord$constraints <- -ord$constraints
ord$sites <- -ord$sites
ord$species <- -ord$species
}
## order factors and sites within factor levels
site.ind <- order(round(ord$constraints, 6), ord$sites)
if (is.null(sp.ind))
sp.ind <- order(ord$species)
}
if (is.null(site.ind))
site.ind <- order(tmp)
if (is.null(sp.ind))
sp.ind <- order(wascores(tmp, xprime))
}
} # end of handling 'use'
if (!is.null(sp.ind) && is.logical(sp.ind))
sp.ind <- seq_len(ncol(x))[sp.ind]
if (!is.null(site.ind) && is.logical(site.ind))
site.ind <- seq_len(nrow(x))[site.ind]
if (is.null(sp.ind))
sp.ind <- seq_len(ncol(x))
if (is.null(site.ind))
site.ind <- seq_len(nrow(x))
if (!missing(select)) {
if (!is.logical(select))
select <- sort(site.ind) %in% select
stake <- colSums(x[select, , drop = FALSE]) > 0
site.ind <- site.ind[select[site.ind]]
site.ind <- site.ind[!is.na(site.ind)]
}
else {
stake <- colSums(x[site.ind,, drop = FALSE ]) > 0
}
sp.ind <- sp.ind[stake[sp.ind]]
x <- x[site.ind, sp.ind, drop = FALSE]
if (!missing(scale))
x <- coverscale(x, scale, ...)
usedscale <- attr(x, "scale")
if (any(apply(x, 1, nchar) > 1))
stop("cowardly refusing to use longer than one-character symbols:\nUse scale")
x <- as.matrix(x)
x <- t(x)
sp.nam <- rownames(x)
sp.len <- max(nchar(sp.nam))
nst <- ncol(x)
nlen <- max(nchar(colnames(x)))
page.width <- getOption("width")
per.page <- page.width - sp.len - 3
istart <- seq(1, nst, by = per.page)
iend <- pmin(istart + per.page - 1, nst)
for (st in seq_along(istart)) {
tbl <- apply(x[, istart[st]:iend[st], drop = FALSE],
1, paste, sep = "", collapse = "")
names(tbl) <- NULL
tbl <- gsub("0", zero, tbl)
tbl <- cbind(sp.nam, tbl)
st.nam <- colnames(x)[istart[st]:iend[st]]
mathead <- matrix(" ", nrow = length(st.nam), ncol = nlen)
for (i in seq_along(st.nam)) {
tmp <- unlist(strsplit(st.nam[i], NULL))
start <- nlen - length(tmp) + 1
mathead[i, start:nlen] <- tmp
}
head <- cbind(apply(mathead, 2, paste, sep = "", collapse = ""))
tbl <- rbind(cbind(matrix(" ", nrow = nrow(head), 1),
head), tbl)
d <- list()
l <- 0
for (i in dim(tbl)) {
d[[l <- l + 1]] <- rep("", i)
}
dimnames(tbl) <- d
print(noquote(tbl))
## collect all pages for output table
if (exists(".tabout", inherits = FALSE))
.tabout[,2] <- paste0(.tabout[,2], tbl[,2])
else
.tabout <- tbl
}
out <- list(sites = site.ind, species = sp.ind, table = .tabout)
cat(length(out$sites), "sites,", length(out$species), "species\n")
if (!is.null(usedscale))
cat("scale: ", usedscale, "\n")
invisible(out)
}