-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpopulationInfo.R
57 lines (46 loc) · 2.06 KB
/
populationInfo.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
calculateShannon <- function(dereplicated){
library("vegan")
shannonInput <- as.data.frame(mcols(dereplicated)[,c("posid", "Timepoint", "estAbund")])
diversity(acast(shannonInput, Timepoint~posid, fill=0, value.var="estAbund",
fun.aggregate=sum))
}
calculateChao <- function(replicatedSites, dereplicatedSites){
library("vegan")
chaoInput <- as.data.frame(merge(mcols(replicatedSites[,c("posid", "replicate")]),
mcols(dereplicatedSites[,c("posid", "estAbund")])))
counts <- acast(chaoInput, posid~replicate, fun.aggregate=sum,
value.var="estAbund", fill=0)
if(all(dim(counts)>2) & is.matrix(counts)) {
# do jackknife correction if more than 1 samples/replicates are available
pseudo <- ncol(counts)*
estimateR(rowSums(counts))-(ncol(counts)-1)*
sapply(1:ncol(counts), function(y) estimateR(rowSums(counts[,-y])))
round(rowMeans(pseudo)["S.chao1"])
} else {
pseudo <- estimateR(rowSums(counts))
round(pseudo["S.chao1"])
}
}
calculateGini <- function(dereplicated){
library("reldist")
gini(dereplicated$estAbundProp)
}
getPopulationInfo <- function(replicated, dereplicated, splitBy){
stopifnot((splitBy %in% names(mcols(replicated))) &
(splitBy %in% names(mcols(dereplicated))))
replicated <- split(replicated, mcols(replicated)[,splitBy])
dereplicated <- split(dereplicated, mcols(dereplicated)[,splitBy])
stopifnot(length(replicated) == length(dereplicated))
populationInfo <- lapply(names(replicated), function(name){
#can iterate through standardizedReplicatedSites and standardizedDereplicatedSites using GTSP#
replicatedSites <- replicated[[name]]
dereplicatedSites <- dereplicated[[name]]
data.frame("group"=name,
"S.chao1"=calculateChao(replicatedSites, dereplicatedSites),
"Gini"=calculateGini(dereplicatedSites),
"Shannon"=calculateShannon(dereplicatedSites))
})
populationInfo <- do.call(rbind, populationInfo)
rownames(populationInfo) <- NULL
populationInfo
}