PyHMMER

repository·master·Indexed 41 days ago

https://github.com/althonos/pyhmmer

Cython bindings and a high-level Python interface to HMMER3 for biological sequence analysis using profile hidden Markov models. Version 0.12.3 provides in-memory processing, flexible input via the easel module, and parallelized search capabilities through pyhmmer.hmmsearch. It includes a daemonized service for remote operations and a comprehensive API for handling biological alphabets, sequences, Multiple Sequence Alignments (MSA), and linear algebra operations.

Tokens
22K
Snippets
66
Records
142
Agent score
48%

What's inside PyHMMER

  1. Overview of pyhmmer advantages

    master

    pyhmmer provides Cython bindings to HMMER3, allowing direct interaction with HMMER internals. Key advantages over CLI wrappers include:

    • Single dependency: No need to manage HMMER binaries separately; it can be a direct dependency in your Python package.
    • In-memory processing: No intermediate files are required. Inputs and outputs (via pyhmmer.plan7.TopHits) are handled as Python objects.
    • Flexible input: You can build pyhmmer.easel.DigitalSequence objects manually, making it easy to use sequences already in memory from libraries like Biopython or Pyrodigal.
    • No output parsing: Avoids the difficulty of parsing HMMER3's fixed-width tabular output files.
    • Efficiency: Performance is comparable to the hmmsearch binary. pyhmmer.hmmer.hmmsearch uses a specific parallelization strategy that can be more efficient for annotating smaller databases.
  2. Overview of pyhmmer

    master

    PyHMMER is a Python module implemented using Cython that provides direct bindings to HMMER3. Unlike CLI wrappers, it interacts directly with HMMER internals, offering several advantages:

    • Batteries-included: Can be installed as a pip or conda dependency without requiring external HMMER binaries.
    • Flexible: Allows creating pyhmmer.easel.Sequence and pyhmmer.plan7.HMM objects via the API or loading them from files.
    • Practical: Nested results can be retrieved as pyhmmer.plan7.TopHits objects for file writing or Python-based analysis.
    • Fast: Supports parallel execution of hmmsearch using an efficient threading model that can outperform the original HMMER in certain use cases.
    • Shareable: Supports distributing and loading pyhmmer.plan7.HMM objects directly from within Python packages.
    • Transparent: Provides access to pyhmmer.plan7.HMM internals, allowing inspection and manual editing of transition or emission scores.
  3. Overview of the Plan7 module (`pyhmmer.plan7`)

    master
    The pyhmmer.plan7 module provides high-level interfaces for working with Plan7 Hidden Markov Models (HMMs). It includes tools for profiling HMMs, parsing HMM files, executing search pipelines, managing iterative searches, and handling alignment results and traces.
  4. Use the pyhmmer daemon for remote services

    master
    The pyhmmer.daemon module provides the infrastructure for running HMMER operations as a daemonized service. This allows users to interact with HMMER tools via a client-server model, which is useful for managing long-running searches or providing HMMER capabilities over a network/local socket.
  5. PyHMMER in production: Ecosystem and use cases

    master

    PyHMMER is a core dependency for several established bioinformatics tools and pipelines. Common use cases include:

    • Genome Annotation: Used by Bakta, Pharokka, VEBA, and GECCO to annotate proteins with Pfam, AntiFam, KOfam, or PHROGS domains.
    • Gene Cluster Detection: Used by GECCO and BiG-SLICE for identifying Biosynthetic Gene Clusters.
    • Homology Searching: Used by duomolog and cinful for sequence homology and alignment-based identification.
    • Specialized Pipelines: Used in BUSCOlite for discovering Conserved Orthologous Genes and FastAAI for estimating Average Amino Acid Identities.
    • High-Performance Implementations: Optimized tools like PyHMMSearch and PyKOfamSearch leverage PyHMMER for high-memory system performance.
  6. Explore the pyhmmer.easel API

    master

    The pyhmmer.easel module provides a high-performance framework for handling biological data, including sequences, alignments, and linear algebra operations. It is organized into several functional areas:

    • Biological Alphabets: Defines Alphabet types such as DNA, RNA, and AA (Amino Acids).
    • Sequences: Core sequence representations including Sequence, TextSequence, and DigitalSequence.
    • Sequence Blocks: Specialized containers for sequence segments like SequenceBlock, TextSequenceBlock, and DigitalSequenceBlock.
    • Alignments: Support for Multiple Sequence Alignments (MSA) via MSA, TextMSA, and DigitalMSA.
    • Parsers: Tools for reading biological files, specifically SequenceFile and MSAFile.
    • Linear Algebra: High-performance math primitives including Vector and Matrix types with various precision levels (D for double, F for float, U8 for unsigned 8-bit).
    • Data Structures: Low-level utilities like Bitfield and KeyHash.
  7. Understand Hit statistics: Bit-score, P-value, and E-value

    master

    When performing HMM searches or scans, hits are reported with three primary statistics. Understanding these is essential for filtering results:

    • Hit Bit-score (Hit.score): The score of the alignment of a sequence to an HMM, expressed in bits. This is calculated after the sequence passes all pipeline filters. If a domain definition workflow is run, corrected bit-scores are calculated for both the sequence level and individual domains.
    • Hit P-value: The probability $P(X \ge \text{score})$ that a score at least as high as the observed score would occur by chance. It is computed using the HMM's calibration parameters ($\tau$ and $\lambda$).
    • Hit E-value (Hit.evalue): The expected number of hits with a score at least as high as the observed score. It is computed by multiplying the Hit.pvalue by the number of testing hypotheses $Z$ (usually the number of targets) using Bonferroni's method to correct for multiple testing.
  8. Profile Hidden Markov Models in Plan7

    master

    The pyhmmer.plan7 module provides several classes for representing and interacting with HMM profiles:

    • HMM: The base representation of a Hidden Markov Model.
    • Profile: A standard profile object.
    • OptimizedProfile: An optimized version of a profile for faster computation.
    • OptimizedProfileBlock: A block-based optimized profile representation.
  9. Understand Domain statistics: Bit-score, P-value, and E-values

    master

    When the 'domain definition workflow' identifies multiple HMM matches to the same sequence, PyHMMER reports statistics at the domain level:

    • Domain Bit-score: The bit score computed based on the local alignment of the specific domain only.
    • Domain P-value: Computed identically to the hit P-value, but using the domain bit-score and the HMM's E-value parameters.
    • Domain E-values: Because only hits passing pipeline filters (defined by Pipeline parameters $F_1$, $F_2$, and $F_3$) reach the domain workflow, HMMER provides two types of E-value corrections:
      • Conditional E-value (Domain.c_evalue): Takes into account only the number of domains passing the first pipeline stages (denoted as domZ). This is generally more statistically accurate but harder to replicate without knowing domZ a-priori.
      • Independent E-value (Domain.i_evalue): Takes into account all sequence/HMM comparisons independently (denoted as $Z$).
  10. Work with Multiple Sequence Alignments (MSA) using pyhmmer.easel

    master

    The pyhmmer.easel module provides classes for handling Multiple Sequence Alignments (MSA).

    • pyhmmer.easel.MSA: The base class for MSA objects.
    • pyhmmer.easel.TextMSA: A subclass of MSA used for handling MSAs represented as text.
    • pyhmmer.easel.DigitalMSA: A subclass of MSA used for handling digital/binary MSAs. Note that DigitalMSA does not support the .sample() method.
  11. How PyHMMER parallelizes HMM searches

    master

    PyHMMER uses a different parallelization strategy than the standard HMMER C binaries to achieve better CPU utilization, especially when searching multiple HMMs against multiple targets.

    HMMER (Standard Binary) Strategy

    • Serial mode: Reads HMMs and sequences one by one. Low memory usage but becomes I/O-bound because sequence files must be re-read for every HMM.
    • Threaded mode: Clones HMM profiles for worker threads and reads sequences in fixed blocks (e.g., 1000 sequences). This can suffer from load imbalance where some threads idle while others process large blocks, and it only achieves true parallelism if the number of targets is very large ($1000 imes n_{cpus}$).

    PyHMMER Strategy

    PyHMMER pre-fetches target sequences into memory and passes them by reference to worker threads. Each worker receives an HMM from the main thread and processes the entire set of targets for that HMM. This allows the pipeline to run multiple HMMs in parallel simultaneously, preventing threads from idling and ensuring better scaling across available CPUs.

    Note: This optimized parallelization strategy is most effective when performing multiple queries (e.g., annotating a large protein dataset with a subset of the Pfam HMM library).

  12. Parallelization best practices for HMMER searches

    master

    Based on benchmarks, the optimal number of CPUs to use depends on the type of search:

    • phmmer: Should typically be run with the number of logical cores.
    • hmmsearch: Should typically be run with the number of physical cores (or fewer).

    This is because HMMER's platform-specific code may require many SIMD registers per thread, which can limit the benefits of simultaneous multi-threading (SMT) on some architectures.