align_pq: Align the reference sequences of a phyloseq object

View source: R/align_pq.R

align_pqR Documentation

Align the reference sequences of a phyloseq object

Description

lifecycle-experimental

Build a multiple sequence alignment from the refseq slot of a phyloseq-class object, or from a set of sequences given directly. Two backends are available:

  • "decipher" (default), the pure-R DECIPHER::AlignSeqs(), which needs no external program;

  • "mafft", the MAFFT command-line aligner driven through ips::mafft(), which is considerably faster on large sets of sequences and is the usual choice for metabarcoding-scale data.

The alignment is returned as a DNAStringSet, not as a phyloseq object. Gap characters are valid DNA letters, so an alignment can technically be written back into a refseq slot, but the sequences of that slot are consumed as unaligned elsewhere in the pqverse (clustering, BLAST, primer trimming), so MiscMetabar never does it for you. Feed the returned alignment to build_phytree_pq() or Biostrings::writeXStringSet() instead.

Usage

align_pq(
  x,
  method = c("decipher", "mafft"),
  exec = NULL,
  mafft_method = "auto",
  thread = -1,
  force = FALSE,
  verbose = FALSE,
  ...
)

Arguments

x

(required) A phyloseq-class object with a non-empty refseq slot, a DNAStringSet (or any XStringSet), or a DNAbin object.

method

One of "decipher" (default) or "mafft".

exec

Path to the MAFFT executable. Only used when method = "mafft". Default to NULL, in which case MAFFT is looked up with is_mafft_installed(): first the MiscMetabar.mafftpath option, then the system PATH.

mafft_method

The MAFFT strategy passed to ips::mafft(), e.g. "auto" (default, lets MAFFT pick from the size of the input), "localpair" (L-INS-i), "globalpair" (G-INS-i) or "retree 2".

thread

Number of threads given to MAFFT. Default to -1, i.e. MAFFT detects the number of available cores itself.

force

Logical, if TRUE sequences that are already all of the same length are realigned anyway. Default to FALSE, in which case such sequences are assumed to be aligned already and returned untouched.

verbose

Logical, if TRUE report the aligner used and the width of the resulting alignment. Default to FALSE.

...

Additional arguments passed on to DECIPHER::AlignSeqs() or ips::mafft(), depending on method.

Value

A DNAStringSet of aligned sequences, all of the same width, in the order of the input.

Installing MAFFT

MAFFT is an external program and is not installed by MiscMetabar. It is available from https://mafft.cbrc.jp/alignment/software/, and from the usual package managers:

  • Debian / Ubuntu: ⁠sudo apt install mafft⁠

  • macOS (Homebrew): ⁠brew install mafft⁠

  • conda: ⁠conda install -c bioconda mafft⁠

  • Windows: use the installer from the MAFFT website, or run it under WSL.

If the executable is not on the PATH, either pass its path with exec or set it once for the session with options(MiscMetabar.mafftpath = "/path/to/mafft").

Author(s)

Adrien Taudière

See Also

is_mafft_installed(), build_phytree_pq()

Examples


data(data_fungi_mini)
df <- subset_taxa_pq(data_fungi_mini, taxa_sums(data_fungi_mini) > 12000)

# Pure-R alignment, no external program required
ali <- align_pq(df, method = "decipher", verbose = TRUE)
ali



if (MiscMetabar::is_mafft_installed()) {
  # The same alignment with MAFFT, much faster on large refseq slots
  ali_mafft <- align_pq(df, method = "mafft", verbose = TRUE)

  # A more accurate (and slower) MAFFT strategy; `exec` gives an explicit
  # path to the executable
  ali_linsi <- align_pq(df, method = "mafft", mafft_method = "localpair")

  # Sequences can also be given directly
  align_pq(phyloseq::refseq(df), method = "mafft")

  # Feed the alignment to a tree builder or write it to a FASTA file
  Biostrings::writeXStringSet(ali_mafft, file.path(tempdir(), "refseq_aligned.fasta"))
}


MiscMetabar documentation built on Nov. 16, 2026, 5:06 p.m.