VSEARCH Documentation

repository·master·Indexed 20 days ago

https://github.com/torognes/vsearch

An open-source, high-performance alternative to USEARCH for biological sequence analysis. VSEARCH utilizes SIMD vectorization and multi-threading for accurate alignments, featuring de novo and reference-based chimera detection, clustering, dereplication, and paired-end read merging. It provides a C/C++ API (version 0.1.0) via libvsearch.a for integration into custom applications, supporting functions such as chimera_detect_single(), search_session_single(), and cluster_assign_single().

Tokens
102.5K
Snippets
297
Records
424
Agent score
66%

What's inside VSEARCH

  1. What is VSEARCH?

    master

    VSEARCH is an open-source, high-performance alternative to USEARCH designed for large-scale biological sequence analysis. It is a 64-bit tool that utilizes SIMD vectorization and multi-threading to perform accurate alignments at high speed.

    Key features include:

    • de novo and reference-based chimera detection
    • Clustering, dereplication, and rereplication
    • All-vs-all pairwise global alignment
    • Exact and global alignment searching
    • FASTQ file analysis, filtering, and merging of paired-end reads

    Unlike USEARCH's default heuristic approach, VSEARCH uses an optimal global aligner (full dynamic programming Needleman-Wunsch), providing improved sensitivity and accuracy, particularly for alignments containing gaps.

  2. Use vsearch for microbiome and metagenomics analysis

    master

    vsearch is a versatile open-source tool designed for various bioinformatics tasks including:

    • Chimera detection (de novo and reference-based)
    • Clustering (fast, size-based, and denoising via UNOISE3)
    • Dereplication and rereplication (merging identical sequences)
    • Sequence extraction (by label or subsequence)
    • File processing (FASTA, FASTQ, SFF conversion and filtering)
    • Alignment and searching (pairwise and database searches)
    • Taxonomic classification (using the SINTAX algorithm)
    • Database management (UDB format handling)
  3. Understand why reads fail to merge

    master

    When vsearch reports failed merges, the reasons fall into two categories:

    User-adjustable reasons (controlled by flags):

    • reads too short (after truncation): Controlled by --fastq_minlen.
    • reads too long (after truncation): Controlled by --fastq_maxlen.
    • too many N's: Controlled by --fastq_maxns.
    • too many differences: Controlled by --fastq_maxdiffs.
    • too high percentage of differences: Controlled by --fastq_maxdiffpct.
    • overlap too short: Controlled by --fastq_minovlen.
    • expected error too high: Controlled by --fastq_maxee.
    • merged fragment too short: Controlled by --fastq_minmergelen.
    • merged fragment too long: Controlled by --fastq_maxmergelen.
    • staggered read pairs: Occurs if not using --fastq_allowmergestagger.

    Internal alignment heuristics (not directly controllable):

    • too few kmers found on same diagonal: No candidate overlap could be located via k-mer matching.
    • multiple potential alignments: The overlap region is ambiguous (e.g., contains tandem repeats).
    • alignment score too low, or score drop too high: The alignment was found but failed internal quality/stability thresholds (often due to indels or clustered mismatches).
  4. Generate OTU tables for QIIME and other software

    master

    When using clustering or searching commands, you can generate OTU tables in several formats using the following options:

    • --biomout: Output in BIOM format (useful for QIIME).
    • --mothur_shared_out: Output for Mothur.
    • --otutabout: Output an OTU table.
  5. Understand Database memory management and allocation

    master

    The Database implementation uses std::vector for its primary storage (data_ and seqindex_) to ensure RAII-compliant memory management.

    Memory Behavior:

    • Allocator: The vectors are parameterized with a FatalAllocator (utils/fatal_allocator.hpp). This ensures that if an Out-Of-Memory (OOM) condition occurs, the program calls fatal() (via xmalloc/xfree) rather than throwing a std::bad_alloc exception. This is critical because vsearch is often compiled with -fno-exceptions.
    • Growth Policy: When adding records via add(), the database grows in chunks using reserve_in_chunks to optimize allocation frequency.
  6. Use the Dbindex API for database indexing

    master

    The Dbindex API has transitioned from a global singleton model to an RAII-based class model. Instead of using global functions like the_index or dbindex_* free functions, users should now manage a Dbindex instance.

    Key Changes

    • No more globals: The singleton pattern and global extern declarations have been removed.
    • RAII Pattern: Use the Dbindex class to manage the lifecycle of the index.
    • Session Initialization: Library session and batch entry points (such as search_session_init, search_batch, cluster_session_init, and chimera_detect_init) now require the caller to pass a reference to a Dbindex object.

    API Usage Patterns

    • Search/Batch: Pass const Dbindex & to initialization functions.
    • Clustering: Pass a mutable Dbindex & to cluster_session_init, as the session adds centroids incrementally.
    • Accessing Data: Use member functions on the Dbindex instance (e.g., dbindex.wordlength) rather than global getters.
    // Example of the new pattern (conceptual)
    Dbindex my_index;
    // ... prepare index ...
    
    // Passing the index to a search session
    search_session_init(..., my_index);
  7. Understand Expected Error (EE) in FASTQ sequences

    master

    The expected error (EE) is a quality summary metric for FASTQ reads. It represents the sum of per-base error probabilities across the entire sequence length $L$.

    $$EE = P_e(1) + P_e(2) + \dots + P_e(L)$$

    Where $P_e(i)$ is the probability that base $i$ was incorrectly sequenced. This probability is derived from the Phred quality score $Q$ using the formula:

    $$P_e = 10^{(-Q / 10)}$$

    Key Characteristics

    • Scale: EE is always greater than zero and at most equal to the sequence length (if every base has an error probability of 1.0).
    • Mathematical Advantage: Unlike averaging Phred scores (which is mathematically incorrect because scores are logarithmic), EE sums linear error probabilities, providing a meaningful global quality estimate.
    • Poisson Interpretation: Because sequencing errors are approximately independent, EE can be used as the $\lambda$ (lambda) parameter of a Poisson distribution to estimate the probability of observing $k$ errors in a read. For example, a read with $EE = 1.0$ has a 36.8% chance of zero errors and a 36.8% chance of exactly one error.
  8. Rules for CIGAR run-length encoding

    master

    Consecutive columns of the same operation type are grouped into a single run.

    • Implicit 1: A run-length of 1 can be omitted. M is equivalent to 1M.
    • Leading Zeros: Accepted (e.g., 03M is 3M).
    • Zero-length: A run-length of 0 is accepted and produces a zero-length operation.
    • Limits: Run-lengths must be positive integers up to the maximum value of a C int (typically 2,147,483,647).
  9. Understand the differences in FASTA and FASTQ line parsing

    master

    When working with or extending the line-parsing logic in vsearch, be aware of the following behavioral differences between FASTA and FASTQ formats that the line-scanning abstraction must preserve:

    FASTA Parsing

    • Sequence Copying: FASTA sequences (Loop B) are copied raw during the loop. Filtering (e.g., removing illegal characters) happens in a second pass via fasta_filter_sequence rather than inline.
    • Line Numbering: The FASTA sequence loop does not increment lineno during the scan; newlines are accounted for later via Action::count.
    • EOF Policy: A FASTA header must be terminated with a newline; otherwise, it is treated as a fatal error.

    FASTQ Parsing

    • Inline Filtering: FASTQ sequences and quality scores are filtered inline during the copy process using buffer_filter_extend.
    • Line Numbering: All FASTQ loops (header, sequence, + line, and quality) increment lineno upon encountering an LF.
    • EOF Policy: Unexpected EOF in headers, sequences, or the + line is treated as a fatal error (fastq_fatal).
    • Record Boundaries: The quality loop (Loop F) includes an extra guard to break early if the quality string length exceeds the sequence length.
  10. Cluster sequences by abundance using --cluster_size

    master

    The --cluster_size command performs abundance-based greedy clustering (AGC). It groups FASTA sequences into clusters using a heuristic, centroid-based algorithm.

    How it works:

    1. Sorting: Input sequences are automatically sorted by decreasing abundance.
    2. Centroid Selection: For each query sequence (from most to least abundant), vsearch compares it to existing cluster centroids using global pairwise alignment (Needleman-Wunsch).
    3. Assignment: If the query is similar enough to a centroid (defined by --id), it joins that cluster. Otherwise, the query becomes the centroid of a new cluster.
    4. Precedence: Because sequences are processed by abundance, more abundant sequences are more likely to become centroids.

    Abundance Information: To use abundance-based clustering, abundance information must be present in the FASTA headers (e.g., ;size=integer;). You must use the --sizein option to tell vsearch to read this information. If no abundance information is found, the behavior defaults to --cluster_fast (sorting by length instead of abundance).

    Comparison to other clustering modes:

    • --cluster_fast: Sorts by decreasing length instead of abundance.
    • --cluster_smallmem: Expects the input to be already sorted (skips the initial sorting step).
    vsearch --cluster_size sequences.fasta --id 0.97 --sizein --sizeout --centroids centroids.fasta
  11. Use pipes and compressed input with vsearch

    master

    Starting from version 2.0.0, vsearch supports reading from pipes. To read compressed input from a pipe, you must specify either --gzip_decompress or --bzip2_decompress. These options are not required when reading from ordinary files.

    To use standard input or standard output, use the - character as the filename. Note that the vsearch header, which was previously written to stdout, is now written to stderr to allow piping results directly to other tools.

    # Example: Reading compressed input from a pipe
    cat data.fastq.gz | vsearch --gzip_decompress --fastx_filter --fastaout - > filtered.fasta