-
Notifications
You must be signed in to change notification settings - Fork 1
/
DiversityFunctions.r
288 lines (265 loc) · 9.63 KB
/
DiversityFunctions.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
################################################################################
# #
# Diversity Functions Source Code: Functions for Microbial Community Analyses #
# #
################################################################################
# #
# Written by: Mario Muscarella #
# #
# Created: 2012/08/22 #
# #
# Last update: 2014/09/04 #
# #
################################################################################
# #
# Notes: This code provides numerous functions to be used in the analysis of #
# 16S rRNA sequence data post mothur anlaysis #
# The ideas in the code are the products of a project between #
# Mario Muscarella and Tracy Teal #
# #
# Issues: Slow performance reading in OTU tables (common R issue) #
# #
# Recent Changes: #
# 1. Added Coverage Function #
# 2. Minor Updates #
# 3. Added Various Evenness Metrics #
# #
# Future Changes (To-Do List): #
# 1. Design functions to work with shared files in memory #
# 2. Add warnings #
# #
################################################################################
# Needed packages
# require("vegan")||install.packages("vegan");require("vegan")
# Import OTU matrix
read.otu <- function(shared = " ",
cutoff = "0.03"){
matrix <- read.table(shared, header=T, fill=TRUE, comment.char="", sep="\t")
matrix.cutoff <- subset(matrix, matrix$label == cutoff)
matrix.out <- as.matrix(matrix.cutoff[1:dim(matrix.cutoff)[1],
4:(3+mean(matrix.cutoff$numOtus))])
row.names(matrix.out) <- matrix.cutoff$Group
return(matrix.out)
}
# Count All Groups
count.groups <- function(otu.matrix = " "){
counts <- rowSums(otu.matrix)
return(counts)
}
# rarefy function from Vegan (version 2.0-10)
rrarefy.1 <- function (x, sample) {
if (!identical(all.equal(x, round(x)), TRUE))
stop("function is meaningful only for integers (counts)")
x <- as.matrix(x)
if (ncol(x) == 1)
x <- t(x)
if (length(sample) > 1 && length(sample) != nrow(x))
stop(gettextf("length of 'sample' and number of rows of 'x' do not match"))
sample <- rep(sample, length = nrow(x))
colnames(x) <- colnames(x, do.NULL = FALSE)
nm <- colnames(x)
for (i in 1:nrow(x)) {
row <- sample(rep(nm, times = x[i, ]), sample[i])
row <- table(row)
ind <- names(row)
x[i, ] <- 0
x[i, ind] <- row
}
return(x)
}
# Subsampling wrapper
sub.sample <- function(otu.matrix = " ",
sample.size = "min(count.groups(test))"){
counts <- count.groups(otu.matrix)
if (sample.size == " "){
sample.size = counts}
statement <- counts > sample.size # Add warning message
otu.matrix <- subset(otu.matrix, rowSums(otu.matrix)>sample.size)
x <- rrarefy.1(otu.matrix, sample.size)
return(x)
}
# Calculate Sample Depth or Coverage for Resampled Matrix
coverage <- function(input= " ", cutoff = " ", size = " ", shared = "TRUE"){
if(shared == TRUE){
otu.matrix <- read.otu(input, cutoff)
} else {
otu.matrix <- input
}
counts <- count.groups(otu.matrix)
statement <- counts > size # Add warning message
otu.matrix <- subset(otu.matrix, rowSums(otu.matrix)>size)
cover <- matrix(NA, dim(otu.matrix)[1], 1)
rownames(cover) <- rownames(otu.matrix)
colnames(cover) <- "Coverage"
temp.matrix <- sub.sample(otu.matrix, size)
for (i in 1:dim(temp.matrix)[1]){
cover[i,] <- 1 - ((length(which(temp.matrix[1,] == 1))) /
(length(which(temp.matrix[i,] > 0))))
}
return(cover)
}
# Diversity/Evenness Measures
# Calculate Shannon Diversity Index
shan <- function(SAD = " "){
SAD <- subset(SAD, SAD > 0)
S <- length(SAD)
N <- sum(SAD)
X <- rep(NA, S)
for (i in 1:S){
X[i] <- (SAD[i]/N * log(SAD[i]/N))
}
H <- -sum(X)
return(H)
}
# Calculate Simpsons Diversity
simp <- function(SAD = " "){
SAD <- subset(SAD, SAD > 0)
S <- length(SAD)
N <- sum(SAD)
X <- rep(NA, S)
for (i in 1:S){
X[i] <- SAD[i]*(SAD[i] - 1) / (N * (N - 1))
}
D <- sum(X)
return(D)
}
# Calculate Inverse Simpson
inv_simp <- function(SAD = " "){
D <- simp(SAD)
inv <- 1/D
return(inv)
}
# Calculates Smith and Wilson's evenness index - E var
# Smith & Wilson (1996) A consumer's guide to evenness indices. Oikos
e_var <- function(SAD = " "){
SAD <- subset(SAD, SAD > 0)
P <- log(SAD)
S <- length(SAD)
X <- rep(NA, S)
for (i in 1:S){
X[i] <- ((P[i] - mean(P))^2)/S
}
evar <- 1 - (2/(pi * atan(sum(X))))
return(evar)
}
# Calculates Simpsons Evenness
simp_even <- function(SAD = " "){
SAD <- subset(SAD, SAD > 0)
S <- length(SAD)
N <- sum(SAD)
X <- rep(NA, S)
for (i in 1:S){
X[i] <- (SAD[i]*(SAD[i] - 1)) / (N * (N - 1))
}
D <- sum(X)
e_d <- (1/D)/S
return(e_d)
}
# Calculates Pielou's Evenness
# Pielou 1969, 1975
pielou <- function(SAD = " "){
SAD <- subset(SAD, SAD > 0)
S <- length(SAD)
N <- sum(SAD)
X <- rep(NA, S)
for (i in 1:S){
X[i] <- (SAD[i]/N * log(SAD[i]/N))
}
H <- -sum(X)
j <- H/log(S)
return(j)
}
# Heip's Evenness
heip <- function(SAD = " "){
SAD <- subset(SAD, SAD > 0)
S <- length(SAD)
N <- sum(SAD)
X <- rep(NA, S)
for (i in 1:S){
X[i] <- (SAD[i]/N * log(SAD[i]/N))
}
H <- -sum(X)
heip_e <- (exp(H) - 1) / (S - 1)
return(heip_e)
}
# resampling function for calculating species richness
richness.iter <- function(input = " ",
cutoff = " ",
size = " ",
iters = " ",
shared = "TRUE"){
if(shared == TRUE){
otu.matrix <- read.otu(input, cutoff)
}else{
otu.matrix <- input
}
counts <- count.groups(otu.matrix)
statement <- counts > size # Add warning message
if(iters > 1){
otu.matrix <- subset(otu.matrix, rowSums(otu.matrix)>size)
rich.matrix <- matrix(NA, dim(otu.matrix)[1], iters)
rownames(rich.matrix) <- rownames(otu.matrix)
for (i in 1:iters){
temp.matrix <- sub.sample(otu.matrix, size)
rich.matrix[,i] <- rowSums((temp.matrix>0)*1)
}
}else{
rich.matrix <- rowSums((input>0)*1)
}
return(rich.matrix)
}
# resampling function for calculating species evenness
evenness.iter <- function(input = " ",
cutoff = " ",
size = " ",
iters = " ",
shared = "TRUE",
method = "e_var"){
if(shared == TRUE){
otu.matrix <- read.otu(input, cutoff)
}else{
otu.matrix <- input
}
counts <- count.groups(otu.matrix)
statement <- counts > size # Add warning message
if(iters > 1){
otu.matrix <- subset(otu.matrix, rowSums(otu.matrix)>size)
even.matrix <- matrix(NA, dim(otu.matrix)[1], iters)
rownames(even.matrix) <- rownames(otu.matrix)
for (i in 1:iters){
temp.matrix <- sub.sample(otu.matrix, size)
even.matrix[,i] <- apply(temp.matrix, 1, method)
}
}else{
even.matrix <- apply(input, 1, method)
}
return(even.matrix)
}
# resampling function for calculating diversity
diversity.iter <- function(input = " ",
index = "shannon",
cutoff = " ",
size = " ",
iters = " ",
shared = "TRUE",
method = "shan"){
if(shared == TRUE){
otu.matrix <- read.otu(input, cutoff)
}else{
otu.matrix <- input
}
counts <- count.groups(otu.matrix)
statement <- counts > size # Add warning message
if(iters > 1){
otu.matrix <- subset(otu.matrix, rowSums(otu.matrix)>size)
div.matrix <- matrix(NA, dim(otu.matrix)[1], iters)
rownames(div.matrix) <- rownames(otu.matrix)
for (i in 1:iters){
temp.matrix <- sub.sample(otu.matrix, size)
div.matrix[,i] <- apply(temp.matrix, 1, method)
}
}else{
div.matrix <- apply(input, 1, method)
}
return(div.matrix)
}