| Title: | Tidyverse-Style Verbs for 'phyloseq' Objects |
|---|---|
| Description: | Tidyverse-style verbs for manipulating 'phyloseq' objects at four scales: samples, taxa, occurrences, and tree. Functions follow a consistent naming convention and support data masking for intuitive filtering, selection, and mutation operations. Designed for metabarcoding analyses, it integrates with 'MiscMetabar' and the 'pqverse' ecosystem. |
| Authors: | Adrien Taudière [aut, cre, cph] (ORCID: <https://orcid.org/0000-0003-1088-1182>) |
| Maintainer: | Adrien Taudière <[email protected]> |
| License: | AGPL-3 |
| Version: | 0.3.0.9000 |
| Built: | 2026-10-16 11:04:08 UTC |
| Source: | https://github.com/adrientaudiere/tidypq |
Reorder samples based on sample_data columns. Supports the . pronoun
to refer to the phyloseq object for sorting by computed values.
arrange_samples_pq(physeq, ..., clean_phyloseq_object = TRUE)arrange_samples_pq(physeq, ..., clean_phyloseq_object = TRUE)
physeq |
(phyloseq, required) A phyloseq object. |
... |
Variables to sort by. Use |
clean_phyloseq_object |
if TRUE (default), the resulting phyloseq object
is cleaned using |
A phyloseq object with reordered samples.
Adrien Taudière
library(MiscMetabar) # Arrange by a single column arrange_samples_pq(data_fungi, Height) # Arrange by sequencing depth (descending) arrange_samples_pq(data_fungi, dplyr::desc(sample_sums(.)))library(MiscMetabar) # Arrange by a single column arrange_samples_pq(data_fungi, Height) # Arrange by sequencing depth (descending) arrange_samples_pq(data_fungi, dplyr::desc(sample_sums(.)))
Reorder taxa based on tax_table columns or computed values. Supports the .
pronoun to refer to the phyloseq object for sorting by abundance.
arrange_taxa_pq(physeq, ..., clean_phyloseq_object = TRUE)arrange_taxa_pq(physeq, ..., clean_phyloseq_object = TRUE)
physeq |
(phyloseq, required) A phyloseq object. |
... |
Variables to sort by. Use |
clean_phyloseq_object |
if TRUE (default), the resulting phyloseq object
is cleaned using |
A phyloseq object with reordered taxa.
Adrien Taudière
library(MiscMetabar) # Arrange by taxonomy arrange_taxa_pq(data_fungi, Phylum, Class)@tax_table[, "Phylum"] # Arrange by total abundance (descending) arrange_taxa_pq(data_fungi, dplyr::desc(taxa_sums(.))) # Order of columns matters dfm_arr <- arrange_taxa_pq(data_fungi, Class, Genus)@tax_table[, c("Class", "Genus")] arrange_taxa_pq(data_fungi, Genus, Class)@tax_table[, c("Class", "Genus")]library(MiscMetabar) # Arrange by taxonomy arrange_taxa_pq(data_fungi, Phylum, Class)@tax_table[, "Phylum"] # Arrange by total abundance (descending) arrange_taxa_pq(data_fungi, dplyr::desc(taxa_sums(.))) # Order of columns matters dfm_arr <- arrange_taxa_pq(data_fungi, Class, Genus)@tax_table[, c("Class", "Genus")] arrange_taxa_pq(data_fungi, Genus, Class)@tax_table[, c("Class", "Genus")]
contam_tbl
A lightweight overview of which detectors flagged how many taxa. For
publication-grade contamination figures, richer plotting lives in the
ggplotpq package and consumes the same contam_tbl.
## S3 method for class 'contam_tbl' autoplot(object, ...) ## S3 method for class 'contam_tbl' plot(x, ...)## S3 method for class 'contam_tbl' autoplot(object, ...) ## S3 method for class 'contam_tbl' plot(x, ...)
object, x
|
(contam_tbl, required) A detector output. |
... |
Currently ignored. |
A ggplot object (autoplot) or x invisibly (plot).
Adrien Taudière
This function creates synthetic chimeric sequences by combining parts of
existing sequences from a phyloseq object. Useful for benchmarking chimera
detection methods like MiscMetabar::chimera_removal_vs() or
identify_contam_chimera_pq().
create_chimera_pq( physeq, n_chimeras = 5, prop_mean = 0.5, prop_sd = 0.15, prop_min = 0.1, seed = 123, median_abundance_multiplier = 0.1, min_parent_distance = 0.1 )create_chimera_pq( physeq, n_chimeras = 5, prop_mean = 0.5, prop_sd = 0.15, prop_min = 0.1, seed = 123, median_abundance_multiplier = 0.1, min_parent_distance = 0.1 )
physeq |
(phyloseq, required) A phyloseq object with a refseq slot containing DNA sequences. |
n_chimeras |
(integer, default: 5) Number of chimeric sequences to create. |
prop_mean |
(numeric, default: 0.5) Mean of the normal distribution used to sample the proportion of the first parent sequence. A value of 0.5 means chimeras will be centered around 50/50 splits. |
prop_sd |
(numeric, default: 0.15) Standard deviation of the normal distribution used to sample proportions. Higher values create more variable chimera breakpoints. |
prop_min |
(numeric, default: 0.1) Minimum proportion threshold. Proportions below this value (or above 1 - prop_min) are resampled to ensure each parent contributes meaningfully to the chimera. |
seed |
(integer, default: 123) Random seed for reproducibility. |
median_abundance_multiplier |
(numeric, default: 0.1) Multiplier to set the abundance of chimeric sequences relative to the median abundance of existing sequences. A value of 0.1 means chimeras will have approximately 10% of the median abundance. |
min_parent_distance |
(numeric, default: 0.1) Minimum sequence distance (proportion of differing positions) between parent1 and parent2. If 0, chimeras can be created from very similar parents, which may be harder to detect. In some cases, with min_parent_distance = 0, you may end up with chimeras that are identical to one of the parents. |
A list containing:
The new phyloseq object with added chimeric sequences
Character vector of chimera taxa names
Data frame with details about each chimera: chimera name, parent1, parent2, parent_distance, prop_parent1, breakpoint, seq_length
List of parameters used (prop_mean, prop_sd, prop_min, min_parent_distance)
Adrien Taudiere
MiscMetabar::chimera_removal_vs(), identify_contam_chimera_pq()
library(MiscMetabar) data(data_fungi) # Default: centered around 50% with some variation result <- create_chimera_pq(data_fungi, n_chimeras = 40) data_fungi_test <- result$physeq known_chimeras <- result$chimera_names # View the parent information and proportions print(result$parent_info) # More variable proportions (wider distribution) result2 <- create_chimera_pq(data_fungi, n_chimeras = 40, prop_mean = 0.5, prop_sd = 0.25 ) # Biased toward more of parent1 (e.g., 70/30 splits on average) result3 <- create_chimera_pq(data_fungi, n_chimeras = 40, prop_mean = 0.7, prop_sd = 0.1 ) # Benchmark chimera detection methods if (MiscMetabar::is_vsearch_installed()) { nochim_vs <- MiscMetabar::chimera_removal_vs(data_fungi_test) detected_vs <- known_chimeras[!known_chimeras %in% phyloseq::taxa_names(nochim_vs)] cat( "vsearch detected:", length(detected_vs), "/", length(known_chimeras), "chimeras\n" ) } # Visualize the distribution of proportions hist(result$parent_info$prop_parent1, main = "Distribution of parent1 proportions", xlab = "Proportion from parent1", xlim = c(0, 1) ) # Ensure parents are at least 15% different (more detectable chimeras) result4 <- create_chimera_pq(data_fungi, n_chimeras = 40, min_parent_distance = 0.15 ) # Disable parent distance filtering (allows similar parents) result5 <- create_chimera_pq(data_fungi, n_chimeras = 40, min_parent_distance = 0 )library(MiscMetabar) data(data_fungi) # Default: centered around 50% with some variation result <- create_chimera_pq(data_fungi, n_chimeras = 40) data_fungi_test <- result$physeq known_chimeras <- result$chimera_names # View the parent information and proportions print(result$parent_info) # More variable proportions (wider distribution) result2 <- create_chimera_pq(data_fungi, n_chimeras = 40, prop_mean = 0.5, prop_sd = 0.25 ) # Biased toward more of parent1 (e.g., 70/30 splits on average) result3 <- create_chimera_pq(data_fungi, n_chimeras = 40, prop_mean = 0.7, prop_sd = 0.1 ) # Benchmark chimera detection methods if (MiscMetabar::is_vsearch_installed()) { nochim_vs <- MiscMetabar::chimera_removal_vs(data_fungi_test) detected_vs <- known_chimeras[!known_chimeras %in% phyloseq::taxa_names(nochim_vs)] cat( "vsearch detected:", length(detected_vs), "/", length(known_chimeras), "chimeras\n" ) } # Visualize the distribution of proportions hist(result$parent_info$prop_parent1, main = "Distribution of parent1 proportions", xlab = "Proportion from parent1", xlim = c(0, 1) ) # Ensure parents are at least 15% different (more detectable chimeras) result4 <- create_chimera_pq(data_fungi, n_chimeras = 40, min_parent_distance = 0.15 ) # Disable parent distance filtering (allows similar parents) result5 <- create_chimera_pq(data_fungi, n_chimeras = 40, min_parent_distance = 0 )
Remove potential contaminants by setting OTU values to 0 when they are at or
below the level observed in negative/blank control samples for that particular OTU.
If multiple control are available, for each taxon a threshold is computed from the
control samples using a summary function (default: max). Occurrences in
non-control samples that are at or below this threshold are set to 0.
decontam_control_samples_pq( physeq, control_condition, fun = max, global_threshold = FALSE, remove_controls = FALSE, clean_phyloseq_object = TRUE, verbose = TRUE )decontam_control_samples_pq( physeq, control_condition, fun = max, global_threshold = FALSE, remove_controls = FALSE, clean_phyloseq_object = TRUE, verbose = TRUE )
physeq |
(phyloseq, required) A phyloseq object. |
control_condition |
An expression evaluated on sample_data
that returns TRUE for control samples. Use |
fun |
(function, default |
global_threshold |
(logical, default FALSE) If TRUE, compute a single
global threshold from all control occurrences instead of per-taxon thresholds.
This applies |
remove_controls |
(logical, default FALSE) Whether to remove the control samples from the output phyloseq object after decontamination. |
clean_phyloseq_object |
(logical, default TRUE) Whether to clean the
resulting phyloseq object using |
verbose |
(logical, default TRUE) Whether to print additional information. |
A phyloseq object with decontaminated OTU values.
Adrien Taudière
library(MiscMetabar) # Add a mock control column for demonstration using the 3 samples with lowest # total abundance as controls pq <- mutate_samdata_pq(data_fungi, is_control = sample_sums(.) < sort(sample_sums(.))[3]) # Decontaminate using max of controls as threshold (per-taxon) decontam_control_samples_pq(pq, is_control) # Use a global threshold (single value for all taxa) decontam_control_samples_pq(pq, is_control, global_threshold = TRUE) # Use mean instead of max (less conservative) decontam_control_samples_pq(pq, is_control, fun = mean) # Keep control samples in output decontam_control_samples_pq(pq, is_control, remove_controls = FALSE) # Use a custom function (e.g., 2x the max) decontam_control_samples_pq(pq, is_control, fun = \(x) 2 * max(x))library(MiscMetabar) # Add a mock control column for demonstration using the 3 samples with lowest # total abundance as controls pq <- mutate_samdata_pq(data_fungi, is_control = sample_sums(.) < sort(sample_sums(.))[3]) # Decontaminate using max of controls as threshold (per-taxon) decontam_control_samples_pq(pq, is_control) # Use a global threshold (single value for all taxa) decontam_control_samples_pq(pq, is_control, global_threshold = TRUE) # Use mean instead of max (less conservative) decontam_control_samples_pq(pq, is_control, fun = mean) # Keep control samples in output decontam_control_samples_pq(pq, is_control, remove_controls = FALSE) # Use a custom function (e.g., 2x the max) decontam_control_samples_pq(pq, is_control, fun = \(x) 2 * max(x))
Remove potential contaminants by using known control taxa (e.g., spike-ins,
synthetic sequences) to estimate background contamination levels. For each
sample, a threshold is computed from the control taxa using a summary function
(default: max). Occurrences of non-control taxa that are at or below this
threshold are set to 0.
decontam_control_taxa_pq( physeq, control_condition, fun = max, global_threshold = FALSE, remove_control_taxa = TRUE, clean_phyloseq_object = TRUE, verbose = TRUE )decontam_control_taxa_pq( physeq, control_condition, fun = max, global_threshold = FALSE, remove_control_taxa = TRUE, clean_phyloseq_object = TRUE, verbose = TRUE )
physeq |
(phyloseq, required) A phyloseq object. |
control_condition |
An expression evaluated on tax_table
that returns TRUE for control taxa. Use |
fun |
(function, default |
global_threshold |
(logical, default FALSE) If TRUE, compute a single global threshold from all control taxa occurrences instead of per-sample thresholds. |
remove_control_taxa |
(logical, default TRUE) Whether to remove the control taxa from the output phyloseq object after decontamination. |
clean_phyloseq_object |
(logical, default TRUE) Whether to clean the
resulting phyloseq object using |
verbose |
(logical, default TRUE) Whether to print additional information. |
A phyloseq object with decontaminated OTU values.
Adrien Taudière
library(MiscMetabar) # Using a condition on tax_table (e.g., select by Genus) decontam_control_taxa_pq(data_fungi, Genus == "Tintelnotia") # Using taxa names directly control_taxa <- phyloseq::taxa_names(data_fungi)[1:2] decontam_control_taxa_pq(data_fungi, taxa_names(.) %in% control_taxa) # Use a global threshold decontam_control_taxa_pq(data_fungi, Genus == "Tintelnotia", global_threshold = TRUE) # Keep control taxa in output decontam_control_taxa_pq(data_fungi, Genus == "Tintelnotia", remove_control_taxa = FALSE)library(MiscMetabar) # Using a condition on tax_table (e.g., select by Genus) decontam_control_taxa_pq(data_fungi, Genus == "Tintelnotia") # Using taxa names directly control_taxa <- phyloseq::taxa_names(data_fungi)[1:2] decontam_control_taxa_pq(data_fungi, taxa_names(.) %in% control_taxa) # Use a global threshold decontam_control_taxa_pq(data_fungi, Genus == "Tintelnotia", global_threshold = TRUE) # Keep control taxa in output decontam_control_taxa_pq(data_fungi, Genus == "Tintelnotia", remove_control_taxa = FALSE)
A convenience wrapper that runs identify_contam_chimera_pq() and then
filter_contam_pq(), returning the chimera-free phyloseq object directly.
Chimera removal is a near-ubiquitous cleaning step, so it gets a dedicated
verb; the other contaminant methods are composed explicitly via their
identify_contam_*_pq() detector and filter_contam_pq().
filter_chimera_pq( physeq, method = c("dada2", "vsearch_ref"), clean_phyloseq_object = TRUE, verbose = TRUE, ... )filter_chimera_pq( physeq, method = c("dada2", "vsearch_ref"), clean_phyloseq_object = TRUE, verbose = TRUE, ... )
physeq |
(phyloseq, required) A phyloseq object with a populated
|
method |
(character, default |
clean_phyloseq_object |
(logical, default |
verbose |
(logical, default |
... |
Additional arguments passed to |
A phyloseq object with chimeric taxa removed (unchanged when none are detected).
Adrien Taudière
identify_contam_chimera_pq() to inspect chimeras before removal,
filter_contam_pq() for the general removal verb.
library(MiscMetabar) data(data_fungi) data_nochim <- filter_chimera_pq(data_fungi, method = "dada2")library(MiscMetabar) data(data_fungi) data_nochim <- filter_chimera_pq(data_fungi, method = "dada2")
The single removal verb for contamination handling. It consumes any
contam_tbl produced by an identify_contam_*_pq() detector (or several,
combined with rbind()) and prunes the flagged taxa from physeq by name.
When the contam_tbl is empty, physeq is returned unchanged.
filter_contam_pq(physeq, contam, clean_phyloseq_object = TRUE, verbose = TRUE)filter_contam_pq(physeq, contam, clean_phyloseq_object = TRUE, verbose = TRUE)
physeq |
(phyloseq, required) A phyloseq object. |
contam |
(contam_tbl, required) Output of an |
clean_phyloseq_object |
(logical, default |
verbose |
(logical, default |
A phyloseq object with the flagged taxa removed.
Adrien Taudière
identify_contam_corr_pq() and the other identify_contam_*_pq()
detectors that produce a contam_tbl.
library(MiscMetabar) flagged <- identify_contam_corr_pq(data_fungi, verbose = FALSE) data_clean <- filter_contam_pq(data_fungi, flagged)library(MiscMetabar) flagged <- identify_contam_corr_pq(data_fungi, verbose = FALSE) data_clean <- filter_contam_pq(data_fungi, flagged)
Set OTU table values to 0 based on a condition. This is useful for removing singletons, low-abundance values, or other filtering operations at the cell level.
The condition is evaluated vectorized across the entire OTU matrix with access to special variables (all as matrices matching OTU dimensions):
. = cell values (the OTU matrix)
sample_total = sum of each sample (repeated per column)
taxon_total = sum of each taxon (repeated per row)
sample_mean = mean of each sample (repeated per column)
taxon_mean = mean of each taxon (repeated per row)
Values that do not satisfy the condition are set to 0.
filter_occurrences_pq(physeq, condition, clean_phyloseq_object = TRUE)filter_occurrences_pq(physeq, condition, clean_phyloseq_object = TRUE)
physeq |
(phyloseq, required) A phyloseq object. |
condition |
(required) An expression evaluated on the OTU matrix. Values where the
condition is FALSE (or NA) are set to 0. Use |
clean_phyloseq_object |
if TRUE (default), the resulting phyloseq object
is cleaned using |
A phyloseq object with filtered OTU values.
Adrien Taudière
library(MiscMetabar) # Remove singletons (abundance = 1) filter_occurrences_pq(data_fungi, . > 1) # Keep only values above 0.01% of sample total filter_occurrences_pq(data_fungi, . / sample_total > 0.0001) # Keep only values above taxon mean filter_occurrences_pq(data_fungi, . > taxon_mean)library(MiscMetabar) # Remove singletons (abundance = 1) filter_occurrences_pq(data_fungi, . > 1) # Keep only values above 0.01% of sample total filter_occurrences_pq(data_fungi, . / sample_total > 0.0001) # Keep only values above taxon mean filter_occurrences_pq(data_fungi, . > taxon_mean)
Filter samples using data masking on sample_data. Supports the . pronoun
to refer to the phyloseq object for use with functions like sample_sums().
filter_samples_pq(physeq, ..., clean_phyloseq_object = TRUE)filter_samples_pq(physeq, ..., clean_phyloseq_object = TRUE)
physeq |
(phyloseq, required) A phyloseq object. |
... |
Expressions that return a logical value, evaluated
in the context of sample_data. Multiple conditions are combined with |
clean_phyloseq_object |
if TRUE (default), the resulting phyloseq object
is cleaned using |
A phyloseq object with filtered samples.
Adrien Taudière
library(MiscMetabar) # Filter by sample metadata filter_samples_pq(data_fungi, Height == "Low") # Filter by sequencing depth filter_samples_pq(data_fungi, sample_sums(.) > 1000) # Combine multiple conditions filter_samples_pq(data_fungi, Height == "Low", sample_sums(.) > 5000) # Keep samples above median abundance filter_samples_pq(data_fungi, sample_sums(.) > median(sample_sums(.))) # Keep samples above half of the average abundance filter_samples_pq(data_fungi, sample_sums(.) > sum(sample_sums(.)) / phyloseq::nsamples(.) / 2)library(MiscMetabar) # Filter by sample metadata filter_samples_pq(data_fungi, Height == "Low") # Filter by sequencing depth filter_samples_pq(data_fungi, sample_sums(.) > 1000) # Combine multiple conditions filter_samples_pq(data_fungi, Height == "Low", sample_sums(.) > 5000) # Keep samples above median abundance filter_samples_pq(data_fungi, sample_sums(.) > median(sample_sums(.))) # Keep samples above half of the average abundance filter_samples_pq(data_fungi, sample_sums(.) > sum(sample_sums(.)) / phyloseq::nsamples(.) / 2)
Filter taxa using data masking on tax_table. Supports the . pronoun
to refer to the phyloseq object for use with functions like taxa_sums().
filter_taxa_pq(physeq, ..., clean_phyloseq_object = TRUE)filter_taxa_pq(physeq, ..., clean_phyloseq_object = TRUE)
physeq |
(phyloseq, required) A phyloseq object. |
... |
Expressions that return a logical value, evaluated
in the context of tax_table. Multiple conditions are combined with |
clean_phyloseq_object |
if TRUE (default), the resulting phyloseq object
is cleaned using |
A phyloseq object with filtered taxa.
Adrien Taudière
library(MiscMetabar) # Filter by taxonomy filter_taxa_pq(data_fungi, Phylum == "Basidiomycota") # Filter by total abundance filter_taxa_pq(data_fungi, taxa_sums(.) > 100) # Combine multiple conditions filter_taxa_pq(data_fungi, Phylum == "Basidiomycota", taxa_sums(.) > 100) # Keep taxa above median abundance filter_taxa_pq(data_fungi, taxa_sums(.) > median(taxa_sums(.)))library(MiscMetabar) # Filter by taxonomy filter_taxa_pq(data_fungi, Phylum == "Basidiomycota") # Filter by total abundance filter_taxa_pq(data_fungi, taxa_sums(.) > 100) # Combine multiple conditions filter_taxa_pq(data_fungi, Phylum == "Basidiomycota", taxa_sums(.) > 100) # Keep taxa above median abundance filter_taxa_pq(data_fungi, taxa_sums(.) > median(taxa_sums(.)))
Filter a phyloseq object to include only taxa that are present in the phylogenetic tree, or prune the tree to match the taxa in the phyloseq. Can also filter based on tree properties like tip labels matching a pattern.
filter_tree_pq( physeq, taxa = NULL, pattern = NULL, invert = FALSE, clean_phyloseq_object = TRUE )filter_tree_pq( physeq, taxa = NULL, pattern = NULL, invert = FALSE, clean_phyloseq_object = TRUE )
physeq |
(phyloseq, required) A phyloseq object with a phy_tree slot. |
taxa |
(character, optional) Character vector of taxa names to keep. If NULL, keeps all taxa that are present in both the OTU table and tree. |
pattern |
(character, optional) A regular expression pattern to match against tip labels. Only tips matching the pattern are kept. |
invert |
(logical, default FALSE) If TRUE and pattern is provided, keep tips that do NOT match the pattern. |
clean_phyloseq_object |
if TRUE (default), the resulting phyloseq object
is cleaned using |
A phyloseq object with filtered tree and matching taxa.
Adrien Taudière
library(MiscMetabar) library(ape) data(data_fungi) pg <- data_fungi pg@phy_tree <- ape::rtree(phyloseq::ntaxa(data_fungi), tip.label = phyloseq::taxa_names(data_fungi)) filter_tree_pq(pg, taxa = phyloseq::taxa_names(data_fungi)[1:10])library(MiscMetabar) library(ape) data(data_fungi) pg <- data_fungi pg@phy_tree <- ape::rtree(phyloseq::ntaxa(data_fungi), tip.label = phyloseq::taxa_names(data_fungi)) filter_tree_pq(pg, taxa = phyloseq::taxa_names(data_fungi)[1:10])
Flags taxa whose genus matches a blocklist of known reagent and laboratory contaminants. A taxonomy-only method requiring no negative controls, best used as a complementary, curation-oriented filter – review flagged taxa before removal, as several blocklist genera also occur as genuine environmental taxa.
This is a pure detector: it flags taxa and returns a contam_tbl without
modifying physeq. Use filter_contam_pq() to remove the flagged taxa.
identify_contam_blocklist_pq( physeq, genus_blocklist = known_contaminant_genera(), tax_rank = "Genus", extra_genera = NULL, ignore_case = TRUE, verbose = TRUE )identify_contam_blocklist_pq( physeq, genus_blocklist = known_contaminant_genera(), tax_rank = "Genus", extra_genera = NULL, ignore_case = TRUE, verbose = TRUE )
physeq |
(phyloseq, required) A phyloseq object with a |
genus_blocklist |
(character, default |
tax_rank |
(character, default |
extra_genera |
(character, default |
ignore_case |
(logical, default |
verbose |
(logical, default |
A contam_tbl with one row per flagged taxon, method = "blocklist",
and the evidence columns genus, total_reads, and prevalence. Empty
when no genus matches the blocklist.
Adrien Taudière
known_contaminant_genera() for the default blocklist,
filter_contam_pq() to remove flagged taxa.
library(MiscMetabar) data(data_fungi) # data_fungi is fungal, so the default bacterial blocklist matches nothing; # extend it with a genus present in the data for illustration. identify_contam_blocklist_pq(data_fungi, extra_genera = "Mortierella")library(MiscMetabar) data(data_fungi) # data_fungi is fungal, so the default bacterial blocklist matches nothing; # extend it with a genus present in the data for illustration. identify_contam_blocklist_pq(data_fungi, extra_genera = "Mortierella")
Flags chimeric taxa using either dada2::removeBimeraDenovo() de novo
detection (method = "dada2") or UCHIME reference-based detection via
vsearch (method = "vsearch_ref").
This is a pure detector: it flags taxa and returns a contam_tbl without
modifying physeq. Use filter_contam_pq() to remove the flagged taxa.
identify_contam_chimera_pq( physeq, method = c("dada2", "vsearch_ref"), dada2_method = "consensus", database = NULL, vsearchpath = "vsearch", vsearch_args = "--fasta_width 0", keep_temporary_files = FALSE, verbose = TRUE, ... )identify_contam_chimera_pq( physeq, method = c("dada2", "vsearch_ref"), dada2_method = "consensus", database = NULL, vsearchpath = "vsearch", vsearch_args = "--fasta_width 0", keep_temporary_files = FALSE, verbose = TRUE, ... )
physeq |
(phyloseq, required) A phyloseq object with a populated
|
method |
(character, default |
dada2_method |
(character, default |
database |
(character, default |
vsearchpath |
(character, default |
vsearch_args |
(character, default |
keep_temporary_files |
(logical, default |
verbose |
(logical, default |
... |
Additional arguments passed to |
A contam_tbl with one row per chimeric taxon, method = "chimera",
and the evidence columns chimera_method ("dada2" or "vsearch_ref")
and n_reads. Empty when no chimera is detected.
Adrien Taudière
filter_contam_pq() to remove flagged taxa, create_chimera_pq()
to generate synthetic chimeras for benchmarking.
library(MiscMetabar) data(data_fungi) flagged <- identify_contam_chimera_pq(data_fungi, method = "dada2") flaggedlibrary(MiscMetabar) data(data_fungi) flagged <- identify_contam_chimera_pq(data_fungi, method = "dada2") flagged
Flags potential contaminant taxa from the correlation between each taxon's relative abundance and total sample depth. Contaminants, introduced at a roughly constant level during processing, show a negative correlation (their proportion is inflated in low-biomass samples). Inspired by the GRIMER tool.
This is a pure detector: it flags taxa and returns a contam_tbl without
modifying physeq. Use filter_contam_pq() to remove the flagged taxa and
plot.contam_tbl() for a quick diagnostic.
identify_contam_corr_pq( physeq, min_total_reads = 1000, min_prevalence = 2, contam_threshold = -0.5, verbose = TRUE )identify_contam_corr_pq( physeq, min_total_reads = 1000, min_prevalence = 2, contam_threshold = -0.5, verbose = TRUE )
physeq |
(phyloseq, required) A phyloseq object. |
min_total_reads |
(numeric, default |
min_prevalence |
(integer, default |
contam_threshold |
(numeric, default |
verbose |
(logical, default |
A contam_tbl with one row per flagged taxon, method = "corr", and
the evidence columns total_reads, prevalence, cor_abs, cor_rel,
slope, and p_value. Empty when nothing is flagged.
Adrien Taudière
filter_contam_pq() to remove flagged taxa, contam_tbl for the
return type.
library(MiscMetabar) # Inject a constant-reads fake contaminant so the example flags something otu <- as(phyloseq::otu_table(data_fungi), "matrix") fake <- matrix(rep(50, nrow(otu)), ncol = 1, dimnames = list(rownames(otu), "fake_contaminant")) tax <- rbind( as.matrix(phyloseq::tax_table(data_fungi)), fake_contaminant = rep("Contaminant", ncol(phyloseq::tax_table(data_fungi))) ) data_fungi_c <- phyloseq::phyloseq( phyloseq::otu_table(cbind(otu, fake), taxa_are_rows = FALSE), phyloseq::tax_table(tax), phyloseq::sample_data(data_fungi) ) flagged <- identify_contam_corr_pq(data_fungi_c) flaggedlibrary(MiscMetabar) # Inject a constant-reads fake contaminant so the example flags something otu <- as(phyloseq::otu_table(data_fungi), "matrix") fake <- matrix(rep(50, nrow(otu)), ncol = 1, dimnames = list(rownames(otu), "fake_contaminant")) tax <- rbind( as.matrix(phyloseq::tax_table(data_fungi)), fake_contaminant = rep("Contaminant", ncol(phyloseq::tax_table(data_fungi))) ) data_fungi_c <- phyloseq::phyloseq( phyloseq::otu_table(cbind(otu, fake), taxa_are_rows = FALSE), phyloseq::tax_table(tax), phyloseq::sample_data(data_fungi) ) flagged <- identify_contam_corr_pq(data_fungi_c) flagged
Examines taxa detected in negative-control samples and assigns each a contamination sub-type from read abundance and occurrence patterns:
artifact: very low total reads AND present in very few samples – likely sequencing noise or cross-talk.
lab_contaminant: predominantly found in negative controls relative to real samples – likely introduced during library preparation.
sample_contaminant: detected in controls but also widespread in real samples – may represent genuine biological signal.
This is a pure detector: it flags taxa and returns a contam_tbl without
modifying physeq. Because sample_contaminant may be genuine signal, only
artifact and lab_contaminant are flagged by default (see
flag_categories). Use filter_contam_pq() to remove the flagged taxa.
identify_contam_negcontrol_pq( physeq, neg_control, min_reads_artifact = 10, max_samples_artifact = 2, max_ratio_lab_contam = 0.2, min_neg_samples_lab = 2, flag_categories = c("artifact", "lab_contaminant"), verbose = TRUE )identify_contam_negcontrol_pq( physeq, neg_control, min_reads_artifact = 10, max_samples_artifact = 2, max_ratio_lab_contam = 0.2, min_neg_samples_lab = 2, flag_categories = c("artifact", "lab_contaminant"), verbose = TRUE )
physeq |
(phyloseq, required) A phyloseq object. |
neg_control |
An expression evaluated on |
min_reads_artifact |
(numeric, default |
max_samples_artifact |
(integer, default |
max_ratio_lab_contam |
(numeric, default |
min_neg_samples_lab |
(integer, default |
flag_categories |
(character, default |
verbose |
(logical, default |
A contam_tbl with one row per flagged taxon, method = "negcontrol", and the evidence columns subtype (artifact,
lab_contaminant, or sample_contaminant), total_reads, reads_in_neg,
reads_in_samples, n_neg_samples, n_non_neg_samples, and
ratio_non_neg_to_neg. Empty when no eligible taxon is found.
Adrien Taudière
filter_contam_pq() to remove flagged taxa.
library(MiscMetabar) data(data_fungi) pq <- mutate_samdata_pq( data_fungi, is_control = sample_sums(.) < sort(sample_sums(.))[4] ) flagged <- identify_contam_negcontrol_pq(pq, is_control) flaggedlibrary(MiscMetabar) data(data_fungi) pq <- mutate_samdata_pq( data_fungi, is_control = sample_sums(.) < sort(sample_sums(.))[4] ) flagged <- identify_contam_negcontrol_pq(pq, is_control) flagged
For each taxon, checks whether the representative sequence in the @refseq
slot matches any primer in primers (forward or reverse complement) using
IUPAC-aware pattern matching. Taxa embedding a primer are likely chimeric or
otherwise artefactual.
This is a pure detector: it flags taxa and returns a contam_tbl without
modifying physeq. Use filter_contam_pq() to remove the flagged taxa.
identify_contam_primer_pq(physeq, primers, verbose = TRUE)identify_contam_primer_pq(physeq, primers, verbose = TRUE)
physeq |
(phyloseq, required) A phyloseq object with a populated
|
primers |
(named character vector, required) Primer sequences to search
for. IUPAC ambiguity codes are supported (e.g. |
verbose |
(logical, default |
A contam_tbl with one row per flagged taxon, method = "primer",
and the evidence columns matched_primers (comma-separated primer names)
and n_reads. Empty when no taxon embeds a primer.
Adrien Taudière
filter_contam_pq() to remove flagged taxa,
MiscMetabar::cutadapt_remove_primers() for cutadapt-based primer removal
of fastq files.
library(MiscMetabar) # Requires a phyloseq object with a populated @refseq slot. primers <- c( fwd = "CCCTACGGGGTGCASCAG", rev = "GGACTACVSGGGTATCTAAT" ) identify_contam_primer_pq(data_fungi, primers)library(MiscMetabar) # Requires a phyloseq object with a populated @refseq slot. primers <- c( fwd = "CCCTACGGGGTGCASCAG", rev = "GGACTACVSGGGTATCTAAT" ) identify_contam_primer_pq(data_fungi, primers)
A curated vector of bacterial genera repeatedly reported as reagent,
kit and laboratory contaminants in low-biomass sequencing studies. The
list is compiled from Salter et al. (2014), the micRoclean workflow
and the GRIMER contaminants.yml / human-related.yml resources. It is
intended as a starting blocklist for identify_contam_blocklist_pq();
users should adapt it to their own substrate and primers.
known_contaminant_genera()known_contaminant_genera()
A character vector of genus names.
Adrien Taudière
Salter SJ, Cox MJ, Turek EM, et al. (2014) Reagent and laboratory contamination can critically impact sequence-based microbiome analyses. BMC Biology 12:87. doi:10.1186/s12915-014-0087-z
head(known_contaminant_genera()) length(known_contaminant_genera())head(known_contaminant_genera()) length(known_contaminant_genera())
Combine several phyloseq objects that describe the same set of samples
sequenced with different markers (e.g. 16S and ITS, or ITS1 and ITS2)
into a single phyloseq object. The result is assembled slot by slot with
full control (it does not rely on phyloseq::merge_phyloseq(), which
merges phyloseq components rather than whole objects):
Samples are combined with a full join of the sam_data slots: the
output keeps the union of all sample names and the union of all metadata
columns. A sample shared by two markers keeps a single row; for a shared
column, the first non-missing value across the input objects is used.
Taxa are never merged. Taxa names are disambiguated by prefixing them
with the marker name (see disambiguate_taxa), so a taxon named "ASV_1"
in two markers becomes e.g. "16S_ASV_1" and "ITS_ASV_1" and stays as
two distinct taxa. The output otu_table is therefore the block-diagonal
union of the inputs.
A new tax_table column (named by marker_column) records the marker
of origin for every taxon.
An occurrence of a taxon in a sample that was not sequenced with that
taxon's marker is set to 0 (the taxon was simply not observable there).
refseq slots are concatenated when every input object has one;
otherwise refseq is dropped with a warning. Phylogenetic trees cannot be
meaningfully merged across markers and are dropped with a warning.
merge_markers_pq( listpq, marker_names = names(listpq), marker_column = "marker", sep = "_", disambiguate_taxa = TRUE )merge_markers_pq( listpq, marker_names = names(listpq), marker_column = "marker", sep = "_", disambiguate_taxa = TRUE )
listpq |
(named list of phyloseq, required) A list of at least two
phyloseq objects, one per marker. If the list is named, the names are used
as marker names (see |
marker_names |
(character, default |
marker_column |
(character of length 1, default |
sep |
(character of length 1, default |
disambiguate_taxa |
(logical, default |
A single phyloseq object stacking all input markers (union of
samples, disambiguated union of taxa, marker column in tax_table).
The taxa carried by different markers are intentionally kept apart. You must
not run any post-clustering step (e.g. MiscMetabar::postcluster_pq())
on the merged object, because that would collapse taxa coming from different
markers into a single taxon, mixing incomparable sequences (a 16S OTU and an
ITS OTU are not the same organism just because their sequences happen to be
similar). Keep the markers separate for any per-marker analysis and treat the
merged object as a stacked view for cross-marker exploration.
The only legitimate cross-marker merging is at the taxonomy level and
must be done explicitly on the tax_table (e.g. aggregating ITS1 and ITS2
taxa that resolve to the same species with MiscMetabar::merge_taxa_vec() or
phyloseq::tax_glom()), never through sequence-based clustering.
Adrien Taudière
library(phyloseq) # Two tiny fake markers describing the same three samples ------------------ make_marker <- function(taxa, ranks) { otu <- matrix( sample(0:20, length(taxa) * 3, replace = TRUE), nrow = length(taxa), dimnames = list(taxa, c("samp1", "samp2", "samp3")) ) tax <- matrix( paste(rep(ranks, each = length(taxa)), seq_along(taxa), sep = "_"), nrow = length(taxa), dimnames = list(taxa, ranks) ) sam <- data.frame( row.names = c("samp1", "samp2", "samp3"), site = c("A", "A", "B"), depth = c(10, 20, 30) ) phyloseq( otu_table(otu, taxa_are_rows = TRUE), tax_table(tax), sample_data(sam) ) } pq_16S <- make_marker(c("ASV_1", "ASV_2"), c("Kingdom", "Phylum")) # Note the SAME taxon name "ASV_1" reused in the ITS object: pq_ITS <- make_marker(c("ASV_1", "ASV_3"), c("Kingdom", "Phylum", "Genus")) merged <- merge_markers_pq(list("marker_16S" = pq_16S, "marker_ITS" = pq_ITS)) # Taxa are disambiguated and never merged: 4 taxa, not 3. taxa_names(merged) # The marker of origin is recorded in a new tax_table column. tax_table(merged)[, "marker"] # Providing marker names explicitly for an unnamed list: merge_markers_pq(list(pq_16S, pq_ITS), marker_names = c("16S", "ITS")) # WARNING: never post-cluster `merged` (e.g. MiscMetabar::postcluster_pq()). # That would wrongly merge a 16S taxon with an ITS taxon.library(phyloseq) # Two tiny fake markers describing the same three samples ------------------ make_marker <- function(taxa, ranks) { otu <- matrix( sample(0:20, length(taxa) * 3, replace = TRUE), nrow = length(taxa), dimnames = list(taxa, c("samp1", "samp2", "samp3")) ) tax <- matrix( paste(rep(ranks, each = length(taxa)), seq_along(taxa), sep = "_"), nrow = length(taxa), dimnames = list(taxa, ranks) ) sam <- data.frame( row.names = c("samp1", "samp2", "samp3"), site = c("A", "A", "B"), depth = c(10, 20, 30) ) phyloseq( otu_table(otu, taxa_are_rows = TRUE), tax_table(tax), sample_data(sam) ) } pq_16S <- make_marker(c("ASV_1", "ASV_2"), c("Kingdom", "Phylum")) # Note the SAME taxon name "ASV_1" reused in the ITS object: pq_ITS <- make_marker(c("ASV_1", "ASV_3"), c("Kingdom", "Phylum", "Genus")) merged <- merge_markers_pq(list("marker_16S" = pq_16S, "marker_ITS" = pq_ITS)) # Taxa are disambiguated and never merged: 4 taxa, not 3. taxa_names(merged) # The marker of origin is recorded in a new tax_table column. tax_table(merged)[, "marker"] # Providing marker names explicitly for an unnamed list: merge_markers_pq(list(pq_16S, pq_ITS), marker_names = c("16S", "ITS")) # WARNING: never post-cluster `merged` (e.g. MiscMetabar::postcluster_pq()). # That would wrongly merge a 16S taxon with an ITS taxon.
Apply a transformation to all values in the OTU table. This is useful for computing relative abundances, log transformations, or other value-level operations.
The expression is evaluated vectorized across the entire OTU matrix with access to special variables (all as matrices matching OTU dimensions):
. = cell values (the OTU matrix)
sample_total = sum of each sample (repeated per column)
taxon_total = sum of each taxon (repeated per row)
sample_mean = mean of each sample (repeated per column)
taxon_mean = mean of each taxon (repeated per row)
sample_median = median of each sample (repeated per column)
taxon_median = median of each taxon (repeated per row)
mutate_occurrences_pq(physeq, expr)mutate_occurrences_pq(physeq, expr)
physeq |
(phyloseq, required) A phyloseq object. |
expr |
An expression evaluated on the OTU matrix. The result replaces
the original values. Use |
A phyloseq object with transformed OTU values.
Adrien Taudière
library(MiscMetabar) # Convert to relative abundance (proportion of sample total) mutate_occurrences_pq(data_fungi, . / sample_total) # Log transformation (adding pseudocount) mutate_occurrences_pq(data_fungi, log1p(.)) # Center by taxon mean mutate_occurrences_pq(data_fungi, . - taxon_mean)library(MiscMetabar) # Convert to relative abundance (proportion of sample total) mutate_occurrences_pq(data_fungi, . / sample_total) # Log transformation (adding pseudocount) mutate_occurrences_pq(data_fungi, log1p(.)) # Center by taxon mean mutate_occurrences_pq(data_fungi, . - taxon_mean)
Create new columns or modify existing ones in sample_data using data masking.
Supports the . pronoun to refer to the phyloseq object.
This function only modifies the sample_data slot (columns/metadata). It cannot add or remove samples. The number of samples and sample names are preserved.
Unlike dplyr::mutate(), columns created in the same call cannot reference
each other (e.g., mutate_samdata_pq(pq, a = 1, b = a + 1) will not work
because a is not yet available when b is evaluated).
mutate_samdata_pq(physeq, ...)mutate_samdata_pq(physeq, ...)
physeq |
(phyloseq, required) A phyloseq object. |
... |
Name-value pairs. The name gives the name of the
column in the output. The value must be a vector of length 1 (recycled) or
exactly the same length as the number of samples. Use phyloseq object. |
A phyloseq object with modified sample_data (same samples, modified or new columns).
Adrien Taudière
library(MiscMetabar) # Add a new column based on sequencing depth mutate_samdata_pq(data_fungi, log_depth = log(sample_sums(.))) # Modify an existing column mutate_samdata_pq(data_fungi, Height = toupper(Height))library(MiscMetabar) # Add a new column based on sequencing depth mutate_samdata_pq(data_fungi, log_depth = log(sample_sums(.))) # Modify an existing column mutate_samdata_pq(data_fungi, Height = toupper(Height))
Create new columns or modify existing ones in tax_table using data masking.
Supports the . pronoun to refer to the phyloseq object.
This function only modifies the tax_table slot (columns/taxonomic ranks). It cannot add or remove taxa. The number of taxa and taxa names are preserved.
Unlike dplyr::mutate(), columns created in the same call cannot reference
each other (e.g., mutate_taxa_pq(pq, a = 1, b = a + 1) will not work
because a is not yet available when b is evaluated).
mutate_taxa_pq(physeq, ...)mutate_taxa_pq(physeq, ...)
physeq |
(phyloseq, required) A phyloseq object. |
... |
Name-value pairs. The name gives the name of the
column in the output. The value must be a vector of length 1 (recycled) or
exactly the same length as the number of taxa. Use |
A phyloseq object with modified tax_table (same taxa, modified or new columns).
Adrien Taudière
library(MiscMetabar) # Replace NA values in a column mutate_taxa_pq(data_fungi, Genus = ifelse(is.na(Genus), "Unknown", Genus)) # Add a new column based on abundance mutate_taxa_pq(data_fungi, total_abundance = taxa_sums(.))library(MiscMetabar) # Replace NA values in a column mutate_taxa_pq(data_fungi, Genus = ifelse(is.na(Genus), "Unknown", Genus)) # Add a new column based on abundance mutate_taxa_pq(data_fungi, total_abundance = taxa_sums(.))
Diagnostic scatter plot for the output of uncross2_pq() run with
return_scores = TRUE. Each point is one non-zero occurrence (an OTU in a
sample), placed by the OTU's total abundance across the object (x) and the
read count of that occurrence (y), and coloured by its UNCROSS2 score.
Occurrences scoring at or below tmin (kept as genuine signal) are drawn in
grey; those at or above tmin (flagged as cross-talk) are coloured on the
score scale. The grey 1:1 line marks where an occurrence equals the OTU
total: genuine "source" samples sit near it, cross-talk sits far below. Both
axes are on a log10 scale.
plot_uncross2_scores(scores_list, grey_non_significant = TRUE)plot_uncross2_scores(scores_list, grey_non_significant = TRUE)
scores_list |
(list, required) The list returned by |
grey_non_significant |
(logical, default |
A ggplot2::ggplot object.
Adrien Taudière
uncross2_pq(), which produces the scored list.
library(MiscMetabar) data(data_fungi) res <- uncross2_pq(data_fungi, return_scores = TRUE) plot_uncross2_scores(res)library(MiscMetabar) data(data_fungi) res <- uncross2_pq(data_fungi, return_scores = TRUE) plot_uncross2_scores(res)
pq_to_tidy() is the canonical phyloseq-to-tidy data-preparation module for
the pqverse. It owns the full pipeline: cleaning, flattening, optional
sample aggregation, optional abundance transformation, and fact/bifactor
resolution. Every ggplotpq plot function calls pq_to_tidy() instead of
reinventing its own data-prep pipeline.
The function builds the tidy tibble from scratch using tidyr::pivot_longer()
(it does not wrap phyloseq::psmelt()). Taxonomic rank grouping happens in
tibble space (no tax_glom()). Sample aggregation happens in tibble space
(no merge_samples() round-trip).
Alternative transform approaches: users may also transform the phyloseq
object before calling pq_to_tidy() via MiscMetabar::transform_pq(), or
transform the tibble after via tidyverse mutate() (e.g.
dplyr::mutate(abundance = abundance / sum(abundance))).
pq_to_tidy( physeq, fact = NULL, bifactor = NULL, merge_sample_by = NULL, transform = NULL, ranks = NULL, filter_zero = TRUE, verbose = TRUE )pq_to_tidy( physeq, fact = NULL, bifactor = NULL, merge_sample_by = NULL, transform = NULL, ranks = NULL, filter_zero = TRUE, verbose = TRUE )
physeq |
(phyloseq, required) A phyloseq object. Must have
|
fact |
(character or NULL, default NULL) Name of a |
bifactor |
(character or NULL, default NULL) Name of a |
merge_sample_by |
(character or NULL, default NULL) Name of a
|
transform |
(string, function, or NULL, default NULL) Abundance
transformation applied per |
ranks |
(character or NULL, default NULL) Taxonomic ranks to include
as columns. |
filter_zero |
(logical, default TRUE) Drop rows where |
verbose |
(logical, default TRUE) Print diagnostic messages. |
An ungrouped tibble with columns:
sample_id (character) — sample identifier or merge group label.
taxon_id (character) — OTU/ASV identifier.
abundance (numeric) — counts or transformed values.
abundance_raw (numeric) — pre-transform counts, present only
when transform is applied.
fact (character) — resolved primary grouping column.
bifactor (character) — resolved secondary grouping column.
Rank columns (character) — one per requested rank, NA replaced
by "Unknown". Present only if tax_table exists.
All other sample_data columns (original names and types).
Adrien Taudière
phyloseq::psmelt(), MiscMetabar::psmelt_samples_pq(),
MiscMetabar::clean_pq(), MiscMetabar::transform_pq()
library(MiscMetabar) # Basic tidy conversion tidy_df <- pq_to_tidy(data_fungi_mini) head(tidy_df) # With fact and bifactor tidy_df <- pq_to_tidy(data_fungi_mini, fact = "Height", bifactor = "Time") # With sample aggregation tidy_df <- pq_to_tidy(data_fungi_mini, merge_sample_by = "Height") # With log1p transform tidy_df <- pq_to_tidy(data_fungi_mini, transform = "log1p") # With selected ranks only tidy_df <- pq_to_tidy(data_fungi_mini, ranks = c("Phylum", "Genus"))library(MiscMetabar) # Basic tidy conversion tidy_df <- pq_to_tidy(data_fungi_mini) head(tidy_df) # With fact and bifactor tidy_df <- pq_to_tidy(data_fungi_mini, fact = "Height", bifactor = "Time") # With sample aggregation tidy_df <- pq_to_tidy(data_fungi_mini, merge_sample_by = "Height") # With log1p transform tidy_df <- pq_to_tidy(data_fungi_mini, transform = "log1p") # With selected ranks only tidy_df <- pq_to_tidy(data_fungi_mini, ranks = c("Phylum", "Genus"))
contam_tbl
Print a contam_tbl
## S3 method for class 'contam_tbl' print(x, ...)## S3 method for class 'contam_tbl' print(x, ...)
x |
(contam_tbl, required) The object to print. |
... |
Passed to the tibble print method. |
x, invisibly.
Adrien Taudière
contam_tbl objectsRow-binds detector outputs into a single contam_tbl. Evidence columns
missing from one detector are filled with NA.
## S3 method for class 'contam_tbl' rbind(..., deparse.level = 1)## S3 method for class 'contam_tbl' rbind(..., deparse.level = 1)
... |
(contam_tbl) One or more |
deparse.level |
Ignored; present for |
A contam_tbl holding the union of the flagged taxa.
Adrien Taudière
Rename columns in sample_data using tidyselect semantics.
rename_samples_pq(physeq, ...)rename_samples_pq(physeq, ...)
physeq |
(phyloseq, required) A phyloseq object. |
... |
Name-value pairs where the name is the new name
and the value is the old name. Use |
A phyloseq object with renamed sample_data columns.
Adrien Taudière
library(MiscMetabar) # Rename a single column rename_samples_pq(data_fungi, sample_height = Height) # Rename multiple columns rename_samples_pq(data_fungi, sample_height = Height, sample_time = Time)library(MiscMetabar) # Rename a single column rename_samples_pq(data_fungi, sample_height = Height) # Rename multiple columns rename_samples_pq(data_fungi, sample_height = Height, sample_time = Time)
Rename columns (taxonomic ranks) in tax_table using tidyselect semantics.
rename_taxa_pq(physeq, ...)rename_taxa_pq(physeq, ...)
physeq |
(phyloseq, required) A phyloseq object. |
... |
Name-value pairs where the name is the new name
and the value is the old name. Use |
A phyloseq object with renamed tax_table columns.
Adrien Taudière
library(MiscMetabar) # Rename a single rank rename_taxa_pq(data_fungi, tax_domain = Domain) # Rename multiple ranks rename_taxa_pq(data_fungi, tax_phylum = Phylum, tax_class = Class)library(MiscMetabar) # Rename a single rank rename_taxa_pq(data_fungi, tax_domain = Domain) # Rename multiple ranks rename_taxa_pq(data_fungi, tax_phylum = Phylum, tax_class = Class)
Select sample_data columns using tidyselect semantics.
select_samdata_pq(physeq, ...)select_samdata_pq(physeq, ...)
physeq |
(phyloseq, required) A phyloseq object. |
... |
One or more unquoted expressions separated by
commas. Variable names can be used as if they were positions in the data
frame, so expressions like |
A phyloseq object with selected sample_data columns.
Adrien Taudière
library(MiscMetabar) # Select specific columns select_samdata_pq(data_fungi, Height, Time) # Select a range of columns select_samdata_pq(data_fungi, Height:Time) # Exclude columns select_samdata_pq(data_fungi, !Sample_id)library(MiscMetabar) # Select specific columns select_samdata_pq(data_fungi, Height, Time) # Select a range of columns select_samdata_pq(data_fungi, Height:Time) # Exclude columns select_samdata_pq(data_fungi, !Sample_id)
Select tax_table columns (taxonomic ranks) using tidyselect semantics.
select_taxa_pq(physeq, ...)select_taxa_pq(physeq, ...)
physeq |
(phyloseq, required) A phyloseq object. |
... |
One or more unquoted expressions separated by
commas. Variable names can be used as if they were positions in the data
frame, so expressions like |
A phyloseq object with selected tax_table columns.
Adrien Taudière
library(MiscMetabar) # Select specific ranks select_taxa_pq(data_fungi, Phylum, Class) # Select a range of ranks select_taxa_pq(data_fungi, Phylum:Genus) # Exclude ranks select_taxa_pq(data_fungi, !Species)library(MiscMetabar) # Select specific ranks select_taxa_pq(data_fungi, Phylum, Class) # Select a range of ranks select_taxa_pq(data_fungi, Phylum:Genus) # Exclude ranks select_taxa_pq(data_fungi, !Species)
Select samples by their integer positions.
slice_samples_pq(physeq, ..., clean_phyloseq_object = TRUE)slice_samples_pq(physeq, ..., clean_phyloseq_object = TRUE)
physeq |
(phyloseq, required) A phyloseq object. |
... |
Integer row indices. Positive values select samples, negative values drop samples. |
clean_phyloseq_object |
if TRUE (default), the resulting phyloseq object
is cleaned using |
A phyloseq object with selected samples.
Adrien Taudière
library(MiscMetabar) # Select first 5 samples slice_samples_pq(data_fungi, 1:5) # Remove first 2 samples slice_samples_pq(data_fungi, -(1:2))library(MiscMetabar) # Select first 5 samples slice_samples_pq(data_fungi, 1:5) # Remove first 2 samples slice_samples_pq(data_fungi, -(1:2))
Select taxa by their integer positions.
slice_taxa_pq(physeq, ..., clean_phyloseq_object = TRUE)slice_taxa_pq(physeq, ..., clean_phyloseq_object = TRUE)
physeq |
(phyloseq, required) A phyloseq object. |
... |
Integer row indices. Positive values select taxa, negative values drop taxa. |
clean_phyloseq_object |
if TRUE (default), the resulting phyloseq object
is cleaned using |
A phyloseq object with selected taxa.
Adrien Taudière
library(MiscMetabar) # Select first 10 taxa slice_taxa_pq(data_fungi, 1:10) # Remove first 5 taxa slice_taxa_pq(data_fungi, -(1:5))library(MiscMetabar) # Select first 10 taxa slice_taxa_pq(data_fungi, 1:10) # Remove first 5 taxa slice_taxa_pq(data_fungi, -(1:5))
tax_table into a data.frame with numeric and list columnsThe tax_table slot of a phyloseq object is stored as a character
matrix, so numeric information (e.g. a bootstrap support, a trait value)
is silently coerced to text and list-valued cells cannot be represented
at all. This helper lifts those limitations by returning a tibble in
which columns that look numeric (or logical) are converted to their
natural type, and selected columns can be split into genuine list
columns.
It does not modify the phyloseq object; it produces a stand-alone tibble for downstream (tidyverse) manipulation.
tax_table_to_df( physeq, convert = TRUE, split = NULL, sep = "/", taxa_names_col = "taxon" )tax_table_to_df( physeq, convert = TRUE, split = NULL, sep = "/", taxa_names_col = "taxon" )
physeq |
(phyloseq or taxonomyTable, required) A phyloseq object
with a |
convert |
(logical, default TRUE) If TRUE, each column is passed
through |
split |
(character, optional) Names of columns to turn into list
columns by splitting each cell on |
sep |
(character, default |
taxa_names_col |
(character or NULL, default |
A tibble::tibble with one row per taxon and one column per
taxonomic rank, plus (unless taxa_names_col is NULL) a leading column
of taxa names.
Adrien Taudière
data_fungi_mini <- tidypq::mutate_taxa_pq(data_fungi_mini, Mol_Abundance = taxa_sums(.)) # Numeric-looking ranks are converted to numeric df <- tax_table_to_df(data_fungi_mini) df |> group_by(Class) |> summarize(mean=mean(Mol_Abundance)) ## Not run: # Split a packed column into a list column tax_table_to_df(data_fungi_mini, split = "Genus", sep = "_") ## End(Not run)data_fungi_mini <- tidypq::mutate_taxa_pq(data_fungi_mini, Mol_Abundance = taxa_sums(.)) # Numeric-looking ranks are converted to numeric df <- tax_table_to_df(data_fungi_mini) df |> group_by(Class) |> summarize(mean=mean(Mol_Abundance)) ## Not run: # Split a packed column into a list column tax_table_to_df(data_fungi_mini, split = "Genus", sep = "_") ## End(Not run)
Calculate the prevalence (number of samples in which a taxon is present) for each taxon in a phyloseq object.
taxa_prevalence(physeq, threshold = 0)taxa_prevalence(physeq, threshold = 0)
physeq |
(phyloseq, required) A phyloseq object. |
threshold |
(numeric, default 0) Minimum abundance to consider a taxon as present in a sample. Values > threshold are counted as present. |
A named numeric vector (class integer) with prevalence for each taxon.
Adrien Taudière
library(MiscMetabar) # Get prevalence for each taxon prev <- taxa_prevalence(data_fungi) head(prev) # Get prevalence with minimum abundance of 10 prev10 <- taxa_prevalence(data_fungi, threshold = 10) head(prev10) # Use in filter_taxa_pq # Keep taxa present in at least 5 samples filter_taxa_pq(data_fungi, taxa_prevalence(.) >= 5)library(MiscMetabar) # Get prevalence for each taxon prev <- taxa_prevalence(data_fungi) head(prev) # Get prevalence with minimum abundance of 10 prev10 <- taxa_prevalence(data_fungi, threshold = 10) head(prev10) # Use in filter_taxa_pq # Keep taxa present in at least 5 samples filter_taxa_pq(data_fungi, taxa_prevalence(.) >= 5)
Detect and remove per-cell cross-talk (also called tag-jumps)
in an OTU table using Edgar's UNCROSS2 score (Edgar 2018,
doi:10.1101/400762). Unlike tail-trimming approaches
(e.g. metabaR::tagjumpslayer()), the score scales the expected leak by each
OTU's total abundance: an abundant genuine OTU is expected to leak more
reads into wells where it is absent than a rare one.
For every OTU (row) an expected per-sample cross-talk count is computed as
z = f * N / n, where N is the OTU's total abundance across the whole
object and n is the total number of samples (Edgar's spreading model:
cross-talk leaks into all wells, mostly the zero/low ones). Each cell then
receives the UNCROSS2 score
where x is the observed count in that cell. The score tends to 1 as
x -> 0 (looks like cross-talk) and to 0 as x >> z (genuine signal).
Cells with s >= tmin are treated as cross-talk. The default tmin = 0.1
matches Edgar's "weak" (yellow) cut-off; use 0.4 for "strong" only.
uncross2_pq( physeq, f = 0.01, tmin = 0.1, k = 1, method = c("cut", "subtract"), return_scores = FALSE, clean_phyloseq_object = TRUE, verbose = TRUE )uncross2_pq( physeq, f = 0.01, tmin = 0.1, k = 1, method = c("cut", "subtract"), return_scores = FALSE, clean_phyloseq_object = TRUE, verbose = TRUE )
physeq |
(phyloseq, required) A phyloseq object with integer counts. |
f |
(numeric or |
tmin |
(numeric, default |
k |
(numeric, default |
method |
(character, default
|
return_scores |
(logical, default |
clean_phyloseq_object |
(logical, default |
verbose |
(logical, default |
By default a phyloseq object with flagged cross-talk removed. If
return_scores = TRUE, a list with:
old_physeq: the input phyloseq object (before cleaning).
new_physeq: the cleaned phyloseq object.
scores: taxa-by-samples matrix of UNCROSS2 scores.
tagjump: logical taxa-by-samples matrix of flagged cells (always
relative to tmin, even when method = "subtract").
Matrices are oriented taxa-as-rows regardless of the input orientation.
This function faithfully
implements the published UNCROSS2 score (eq. 3), verified cell-for-cell
against an independent reference. It differs from the usearch v11
otutab_xtalk binary in two ways that users should know:
Rate. f is fixed (default 0.01) unless f = "auto", whereas the
binary estimates it de novo. Use estimate_xtalk_rate_pq() (or
f = "auto") for a de novo estimate.
Aggressiveness. The binary's -otutabout filtering is far more
aggressive than eq. (3): empirically it zeroes essentially every count
below the per-OTU mean (N_i/n), keeping only the few "big" source
samples, and removed ~40x more cells than eq. (3) at the same rate on data.
That behaviour is undocumented and beyond the paper, so this function does
not reproduce the binary's filtered table; it is the gentler,
paper-faithful filter.
UNCROSS2 is defined for integer count tables. The usearch binary rejects
non-integer input (Invalid count); this function will process relative
abundances but the score's interpretation no longer holds, so prefer raw
counts.
Adrien Taudière
Edgar, R.C. (2018) UNCROSS2: identification of cross-talk in 16S rRNA OTU tables. bioRxiv 400762. doi:10.1101/400762
estimate_xtalk_rate_pq() for the de novo rate;
decontam_control_samples_pq() for blank-based decontamination.
library(MiscMetabar) data(data_fungi) # Remove weak cross-talk (default) unc_data_fungi <- uncross2_pq(data_fungi) # Estimate the rate de novo instead of assuming 1% uncross2_pq(data_fungi, f = "auto") # Only strong cross-talk, sharper transition uncross2_pq(data_fungi, tmin = 0.4, k = 2) # Inspect scores without committing to a threshold res <- uncross2_pq(data_fungi, return_scores = TRUE) summary(as.vector(res$scores)) summary_plot_pq(res$old_physeq) / summary_plot_pq(res$new_physeq)library(MiscMetabar) data(data_fungi) # Remove weak cross-talk (default) unc_data_fungi <- uncross2_pq(data_fungi) # Estimate the rate de novo instead of assuming 1% uncross2_pq(data_fungi, f = "auto") # Only strong cross-talk, sharper transition uncross2_pq(data_fungi, tmin = 0.4, k = 2) # Inspect scores without committing to a threshold res <- uncross2_pq(data_fungi, return_scores = TRUE) summary(as.vector(res$scores)) summary_plot_pq(res$old_physeq) / summary_plot_pq(res$new_physeq)