This repository has been archived by the owner on May 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cullMatrix.R
103 lines (96 loc) · 2.27 KB
/
cullMatrix.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
# Steve Holland's Culling Functions
cullMatrix <- function(x, minOccurrences=2, minDiversity=2) {
y <- x
while (diversityFlag(y, minDiversity) | occurrencesFlag(y, minOccurrences)) {
y <- cullTaxa(y, minOccurrences)
y <- cullSamples(y, minDiversity)
}
y
}
# Dependency of cullMatrix()
cullTaxa <- function(x, minOccurrences) {
PA <- x
PA[PA>0] <- 1
occurrences <- apply(PA, MARGIN=2, FUN=sum)
aboveMinimumOccurrences <- occurrences >= minOccurrences
y <- x[ ,aboveMinimumOccurrences]
if (length(y)==0) {print("no taxa left!")}
y
}
# Dependency of cullMatrix()
cullSamples <- function(x, minDiversity) {
PA <- x
PA[PA>0] <- 1
diversity <- apply(PA, MARGIN=1, FUN=sum)
aboveMinimumDiversity <- diversity >= minDiversity
y <- x[aboveMinimumDiversity, ]
if (length(y[,1])==0) {print("no samples left!")}
y
}
# Dependency of cullMatrix()
occurrencesFlag <- function(x, minOccurrences) {
PA <- x
PA[PA>0] <- 1
occurrences <- apply(PA, MARGIN=2, FUN=sum)
if (min(occurrences) < minOccurrences) {
flag <- 1
}
else {
flag <- 0
}
flag
}
# Dependency of cullMatrix()
diversityFlag <- function(x, minDiversity) {
PA <- x
PA[PA>0] <- 1
diversity <- apply(PA, MARGIN=1, FUN=sum)
if (min(diversity) < minDiversity) {
flag <- 1
}
else {
flag <- 0
}
flag
}
# Alternative to cullMatrix( ) that does not thrown an error, but returns a single NA
softCull <- function(x, minOccurrences=2, minDiversity=2) {
y <- x
while (diversityFlag(y, minDiversity) | occurrencesFlag(y, minOccurrences)) {
y <- softTaxa(y, minOccurrences)
if (length(y)==1) {
return(NA)
}
y <- softSamples(y, minDiversity)
if (length(y)==1) {
return(NA)
}
}
return(y)
}
# Dependency of softCull()
softTaxa <- function(x, minOccurrences) {
PA <- x
PA[PA>0] <- 1
occurrences <- apply(PA, MARGIN=2, FUN=sum)
aboveMinimumOccurrences <- occurrences >= minOccurrences
y <- x[ ,aboveMinimumOccurrences]
if (length(y)==0) {
y<-NA;
return(NA)
}
return(y)
}
# Dependency of softCull()
softSamples <- function(x, minDiversity) {
PA <- x
PA[PA>0] <- 1
diversity <- apply(PA, MARGIN=1, FUN=sum)
aboveMinimumDiversity <- diversity >= minDiversity
y <- x[aboveMinimumDiversity, ]
if (length(y[,1])==0) {
y<-NA;
return(NA)
}
return(y)
}