假设存在称为 1 和 2 的 OTU 1,但只有第一个出现在样本组 BL 和 SC 中。然后您可以创建一个 phyloseq 对象,该对象仅包含选定的 OTU 及其在所有样本中的丰度,如下所示:
library(phyloseq)
#> Creating a generic function for 'nrow' from package 'base' in package 'biomformat'
#> Creating a generic function for 'ncol' from package 'base' in package 'biomformat'
#> Creating a generic function for 'rownames' from package 'base' in package 'biomformat'
#> Creating a generic function for 'colnames' from package 'base' in package 'biomformat'
library(tidyverse)
otus <- tribble(
~otu_id, ~s1, ~s2, ~s3,
1, 10, 10, 20,
2, 5, 5, 0
)
samples <- tribble(
~sample_id, ~group,
"s1", "BL",
"s2", "BL",
"s3", "SC"
)
selected_otus <-
otus %>%
pivot_longer(-otu_id, names_to = "sample_id", values_to = "abundance") %>%
left_join(samples) %>%
group_by(otu_id, group) %>%
summarise(abundance = sum(abundance)) %>%
pivot_wider(names_from = group, values_from = abundance) %>%
# must be found in both sample groups
filter(BL > 0 & SC > 0) %>%
pull(otu_id) %>%
unique()
#> Joining, by = "sample_id"
#> `summarise()` has grouped output by 'otu_id'. You can override using the `.groups` argument.
selected_otus
#> [1] 1
phy <- phyloseq(
otus %>%
filter(otu_id %in% selected_otus) %>%
column_to_rownames("otu_id") %>%
otu_table(taxa_are_rows = TRUE),
samples %>%
column_to_rownames("sample_id") %>%
sample_data()
)
phy
#> phyloseq-class experiment-level object
#> otu_table() OTU Table: [ 1 taxa and 3 samples ]
#> sample_data() Sample Data: [ 3 samples by 1 sample variables ]
由reprex 包于 2021-12-16 创建(v2.0.1)