-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.R
296 lines (244 loc) · 8.7 KB
/
utils.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
289
290
291
292
293
294
295
296
#########################
# Helper functions for
# MicroExplorer Shiny App
#########################
# +++++++++++++++++++++++
# csv2dat
# +++++++++++++++++++++++
# returns countData, taxaData, sampleData
# from uploaded csv files
csv2dat <- function(countFilePath, taxaFilePath, sampleFilePath) {
countData <- read_csv(countFilePath) %>%
rename(otu = 1) %>%
column_to_rownames(var="otu")
taxaData <- read_csv(taxaFilePath) %>%
rename(otu = 1) %>%
column_to_rownames(var="otu")
sampleData <- read_csv(sampleFilePath) %>%
rename(sname = 1) %>%
column_to_rownames(var="sname")
dataList <- list("countData" = countData,
"taxaData" = taxaData,
"sampleData"= sampleData)
return(dataList)
}
# +++++++++++++++++++++++
# biom2dat
# +++++++++++++++++++++++
# returns countData, taxaData, sampleData
# from input BIOM file
biom2dat <- function(biomFilePath) {
}
# +++++++++++++++++++++++
# validateInputs
# +++++++++++++++++++++++
# Validate input files.
# checks if -
# i) sample names match in countData and sampleData
# AND
# ii) taxa names match in countData and taxaData
validateInputs <- function(countData, taxaData, sampleData) {
snameCheck <- is_empty(setdiff(colnames(countData), rownames(sampleData))) &
is_empty(setdiff(rownames(sampleData), colnames(countData)))
otuCheck <- is_empty(setdiff(rownames(countData), rownames(taxaData))) &
is_empty(setdiff(rownames(taxaData), rownames(countData)))
if (snameCheck & otuCheck) {
msg <- "Valid Input files! Please proceed to filtering step."
} else if (snameCheck == FALSE) {
msg <- "ERROR: Sample names do not match in count data and sample data. Please re-upload your data."
} else if (otuCheck == FALSE) {
msg <- "ERROR: OTU IDs do not match in count data and taxa data. Please re-upload your data"
} else {
msg <- "ERROR: Unable to validate input files. Please re-upload your data."
}
dataList = list("msg" = msg,
"countData" = countData,
"taxaData" = taxaData,
"sampleData" = sampleData
)
return(dataList)
}
# +++++++++++++++++++++++
# filterData
# +++++++++++++++++++++++
# filters the valid data based on user inputs:
# i) min seq depth
# ii) min taxa abundance
# iii) min taxa prevalence
filterData <- function(countData, taxaData, sampleData, seqDepth, minTaxaAbund, minTaxaPrev) {
# remove samples with seq depth below threshold
countDataMinDepth <- countData %>%
select(names(which(colSums(countData) >= seqDepth)))
# transform counts to percentage
countDataMinDepthPerc <- countDataMinDepth * 100 / colSums(countDataMinDepth)[col(countDataMinDepth)]
# filter low abundance/prevalence taxa
taxa2keep <- replace(countDataMinDepthPerc, countDataMinDepthPerc < minTaxaAbund, NA) %>%
rownames_to_column(var="otu") %>%
filter(rowSums(is.na(.))/ncol(.) * 100.0 < (100.0 - minTaxaPrev)) %>%
column_to_rownames(var="otu") %>%
rownames()
# filtered data
countDataFiltered <- countDataMinDepth %>%
rownames_to_column(var="otu") %>%
filter(otu %in% taxa2keep) %>%
column_to_rownames(var="otu")
taxaDataFiltered <- taxaData %>%
rownames_to_column(var="otu") %>%
filter(otu %in% taxa2keep) %>%
column_to_rownames(var="otu")
sampleDataFiltered <- sampleData %>%
rownames_to_column(var="sample") %>%
filter(sample %in% colnames(countDataMinDepth)) %>%
column_to_rownames(var="sample")
dataList = list("countData" = countDataFiltered,
"taxaData" = taxaDataFiltered,
"sampleData" = sampleDataFiltered
)
return(dataList)
}
# +++++++++++++++++++++++
# sortSamplesByDissimilarity
# +++++++++++++++++++++++
# sorts samples by dissimilarity
# cluster bray dissimilarity distances using ward.D2 method
# input proportional data: Samples in rows, Taxa in column, fill be relative abundance
# returns sample order
sortSamplesByDissimilarity <- function(propData) {
bcdist <- vegdist(propData, method="bray")
hclustBC <- hclust(bcdist, method="ward.D2")
sorder <- hclustBC$labels[c(hclustBC$order)]
return(sorder)
}
# +++++++++++++++++++++++
# sortSamplesByDescTaxaAbund
# +++++++++++++++++++++++
# sorts samples by decreasing taxa abundances
# returns sample order
sortSamplesByDescTaxaAbund <- function(propData) {
# identify top taxa for each sample
maxTaxa <- propData %>%
melt() %>%
set_colnames(c("Sample", "Taxa", "RelAb")) %>%
group_by(Sample) %>%
filter(RelAb == max(RelAb))
# identify most prevalent taxas
taxaPrevOrder <- maxTaxa %>%
ungroup(Sample) %>%
count(Taxa) %>%
arrange(-n)
# arrange samples by taxaprev, followed by relab
sorder <- maxTaxa %>%
mutate(Taxa = factor(Taxa, levels = taxaPrevOrder$Taxa)) %>%
arrange(Taxa, desc(RelAb))
### return the custom sample order
return(sorder$Sample)
}
# +++++++++++++++++++++++
# assignColor
# +++++++++++++++++++++++
# assigns color to use with ggplot
#
assignColor <- function(myTaxas) {
}
# +++++++++++++++++++++++
# plotStackedBar
# +++++++++++++++++++++++
# This function generates a stacked bar plot
# of community composition
plotStackedBar <- function(countData, taxaData, sampleData,
taxaLevel, taxa2Plot, numTaxa2Plot = NULL,
sortMethod, facetField = "None"){
# get taxa level
mytaxaData <- taxaData %>%
rownames_to_column(var="otu") %>%
select(otu, taxaLevel)
# create metadata
metadata <- sampleData %>%
rownames_to_column(var="Sample")
# create the plot data frame
plotDF <- countData %>%
rownames_to_column(var="otu") %>%
melt(.) %>%
left_join(mytaxaData, by="otu") %>%
set_colnames(c("OTU", "Sample", "Count", "Taxa")) %>%
mutate(Taxa2 = ifelse(grepl("unclassified", Taxa), "unclassified", Taxa)) %>%
group_by(Sample, Taxa2) %>%
tally(Count) %>%
mutate(RelAb = n / sum(n) * 100.0) %>%
left_join(., metadata, by="Sample")
# propData
propData <- acast(plotDF, Sample~Taxa2, value.var = "RelAb", fill=0.0)
# get sampleOrder
if (sortMethod == "Cluster by Dissimilarity"){
sorder <- sortSamplesByDissimilarity(propData)
} else {
sorder <- sortSamplesByDescTaxaAbund(propData)
}
# set sample levels
plotDF$Sample <- factor(plotDF$Sample, levels = sorder)
# plot
ntaxa <- length(unique(plotDF$Taxa2))
p <- ggplot(plotDF, aes(Sample, RelAb, fill=Taxa2, group=RelAb)) +
geom_bar(stat="identity", position="stack") +
labs(x="Samples", "Relative Abundance") +
scale_fill_manual(values = colorRampPalette(pal_ucscgb()(7))(ntaxa)) +
theme_bw() +
theme(axis.title = element_text(size=10, face="bold"),
axis.text.x = element_blank(),
strip.text = element_text(size=10, face="bold"),
legend.position = "bottom") +
guides(fill=guide_legend(ncol = 3, title = taxaLevel,
title.position = "top",override.aes = list(size=5)))
# add facet
if (!(facetField == "None")) {
p <- p + facet_grid(~eval(parse(text=facetField)), scales = "free_x", space = "free_x")
}
ggplotly(p)
}
# +++++++++++++++++++++++
# plotHeatMap
# +++++++++++++++++++++++
plotHeatMap <- function(countData, taxaData, sampleData,
taxaLevel, taxa2Plot, numTaxa2Plot = NULL,
sortMethod, facetField = "None"){
cdf <- countData %>%
rownames_to_column(var="otu")
tdf <- taxaData %>%
rownames_to_column(var="otu") %>%
dplyr::select(otu, taxaLevel)
propData <- cdf %>%
column_to_rownames(var="otu") %>%
as.matrix() %>%
prop.table(.,2) %>%
as.data.frame() %>%
rownames_to_column(var="otu")
# tax table for heatmap rows
tdf2 <- left_join(tdf, propData, by="otu") %>%
dplyr::select(-otu) %>%
set_colnames(c("Taxa", colnames(.)[2:ncol(.)])) %>%
dplyr::group_by(Taxa) %>%
summarise_all(funs(sum)) %>%
melt(.) %>%
set_colnames(c("Taxa", "Sample", "Prop"))
# plot
p <- ggplot(tdf2, aes(Sample, Taxa)) +
geom_tile(aes(fill=Prop), color="black") +
scale_fill_gradient(low="white", high = "firebrick") +
ylab(taxaLevel) +
xlab("Samples") +
theme(legend.title = element_text(size = 10),
legend.text = element_text(size = 12),
plot.title = element_text(size=16),
axis.title=element_text(size=14,face="bold"),
axis.text.x = element_blank()) +
labs(fill = "Prop")
heatmap <- ggplotly(p)
return(heatmap)
}
# +++++++++++++++++++++++
# plotSeqDepth
# +++++++++++++++++++++++
plotSeqDepth <- function(countData) {
seqDepth <- colSums(countData)
hist(seqDepth, breaks = 20)
}