-
Notifications
You must be signed in to change notification settings - Fork 0
/
rarefaction_loberas.R
154 lines (111 loc) · 4.47 KB
/
rarefaction_loberas.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
ranks <- c('Kingdom', 'Phylum', 'Class', 'Order', 'Family', 'Genus', 'Species', 'pplacer_sp')
sampleName <- c('Cantiles', 'Coloradito', 'Granito', 'Machos', 'Partido', 'Rasito')
dir <- '~/metagenomics/Loberas_MG/'
fileNames <- list.files(dir, pattern = "xls", full.names = T)
prepare_ps <- function(filename, agg = T) {
ranks <- c('Kingdom', 'Phylum', 'Class', 'Order', 'Family', 'Genus', 'Species')
readXLS <- function(x) {
group <- paste(sapply(strsplit(basename(x), "_"), `[`, 1),collapse = '_')
readxl::read_xlsx(x, na = 'NA') %>% mutate(group = group)
}
features <- readXLS(filename)
names(features)[9] <- 'pplacer_sp'
features %>%
mutate(Relationship = recode_factor(Relationship, P = 'Pathogenic',
NC = 'Inconsistent', C = 'non-pathogenic')) %>%
mutate_all(., funs(str_replace_all(., c("Bacteroides tectu$" = "Bacteroides tectus")))) %>%
mutate_at(sampleName, as.double) -> features
dat <- features %>% select_at(sampleName) %>% data.frame(row.names = 1:nrow(features))
features %>% select(Relationship, pplacer_sp) -> pplacer
# hay problemas con la taxonomia que francesco curo, por tanto tener cuidado al usar datos aglomerados por taxonomia
features %>%
select_at(all_of(ranks)) %>%
mutate(id = 1:nrow(features)) %>%
pivot_longer(cols = ranks) %>% fill(value) %>%
pivot_wider(names_from = name) %>%
cbind(., pplacer) %>%
mutate(Species = ifelse(Relationship %in% 'Pathogenic', pplacer_sp, Species)) %>%
select(-id, -Relationship, -pplacer_sp) %>%
data.frame(row.names = 1:nrow(features)) -> tax
# identical(rownames(dat), rownames(tax))
# and parse
ps = phyloseq(otu_table(dat, taxa_are_rows = TRUE),
tax_table(as(tax, 'matrix')))
if(agg) {
microbiome::aggregate_taxa(ps, level = 'Species')
} else
return(ps)
}
prepare_ps(fileNames[1], agg = F)
dataList <- lapply(fileNames, function(x) {
y <- prepare_ps(x)
return(y)})
saveRDS(dataList, file = paste0(dir, '/phyloseqList.rds'))
# consistencia de pplacer %% rdp??
features <- readXLS(fileNames[1])
features %>%
separate(pplacer_sp, into = c('pplacer', 'prefix'), sep = ' ') %>%
mutate(
type = case_when(
pplacer == Genus ~ 'Consistent',
TRUE ~ "other"
)) %>%
select(Relationship, type, Family:prefix, - Species)
features %>% filter_all(grepl('Psychrobacte', .)) %>% select_at(ranks)
library(microbiome)
tab <- alpha(ps, index = "all")
# fantaxtic
library(fantaxtic)
# Necesitamos obtener las taxa más abundantes, en este caso el top 15
top15 <- get_top_taxa(physeq_obj = ps, n = 15, relative = T,
discard_other = T, other_label = "Other")
# Ya que no todas las taxa fueron clasificadas a nivel de especie, generamos etiquetas compuestas de distintos rangos taxonómicos para el gráfico
top15 <- name_taxa(top15, label = "", species = F, other_label = "Other")
# Finalmente graficamos
fantaxtic_bar(top15, color_by = "Family",
label_by = "Genus", facet_by = NULL, grid_by = NULL,
other_color = "Grey", palette = ) -> ptop15
#
# Ranked abundance distribution models for a random plot. The best model has the lowest aic.
mod <- radfit(t(ab))
mod
plot(mod, pch=".")
# or
mod <- rad.lognormal(t(ab)[1,]);plot(mod)
mod <- radfit(t(ab)[1,])
## Standard plot overlaid for all models
## Preemption model is a line
plot(mod)
## log for both axes: Zipf model is a line
plot(mod, log = "xy")
## Lattice graphics separately for each model
radlattice(mod)
# Figure: Renyi diversities in six randomly selected
# plots. The plot uses Trellis graphics with a separate
# panel for each site. The dots show the values for
# sites, and the lines the extremes and median in the
# data set.
df <- t(ab)
mod <- renyi(df)
plot(mod)
mod <- renyiaccum(df)
plot(mod, as.table=TRUE, col = c(1, 2, 2))
persp(mod)
# rarefac
rs <- rowSums(df)
quantile(rs)
Srar <- rarefy(df, min(rs))
head(Srar)
rarecurve(df, sample = min(rs))
# install.packages('iNEXT')
library(iNEXT)
# https://cran.r-project.org/web/packages/iNEXT/vignettes/Introduction.html
# test <- t(ab)[1,]
# i.zero <- which(test == 0)
# test.no.zero <- test[-i.zero]
x <- apply(ab, 2, function(x) x[-which(x == 0)])
out <- iNEXT(x, q=c(0), datatype="abundance")
# Sample-size-based R/E curves, separating by "site""
ggiNEXT(out, type=1, facet.var="none", grey = T)
ggiNEXT(out, type=2, facet.var="site", grey = T)
ggiNEXT(out, type=2, facet.var="none", color.var="site")