Biopython Documentation

repository·master·Indexed 41 days ago

https://github.com/biopython/biopython.github.io

Official documentation and website source for Biopython, a set of tools for biological computation. Includes guides on sequence I/O via Bio.SeqIO, phylogenetic tree processing with Bio.Phylo, PDB parsing, and detailed instructions for release management, including building wheels and distributing to PyPI.

Tokens
62.5K
Snippets
205
Records
288
Agent score
48%

What's inside Biopython

  1. Overview of the PopGen module

    master
    The Bio.PopGen module is designed to handle population genetics data, applications, and algorithms. Its primary focus is on core population genetics functionality, specifically statistics. Rather than reinventing existing tools, the module often provides wrappers for established, widely used software (for example, providing wrappers for the SIMCOAL2 coalescent simulator).
  2. Introduction to Biopython

    master

    Biopython is a collection of freely available Python tools and libraries designed for biological computation and bioinformatics. It is a collaborative project distributed under the Biopython License, which is highly permissive and compatible with most other licenses.

    Key resources for users:

  3. Access Population Genetics functionality via Bio.PopGen

    master

    Biopython provides population genetics tools through the Bio.PopGen module. This module serves as an interface for several specialized methods and external software integrations:

    1. Genepop methods: Provides access to tests for Hardy–Weinberg equilibrium, population differentiation, genotypic disequilibrium, F-statistics, null allele frequencies, and allele size-based statistics for microsatellites.
    2. Coalescent simulation: Interfaces with SIMCOAL2 and fastsimcoal2.
    3. Selection detection: Interfaces with fdist2.

    For specific implementation details on using Genepop, refer to the dedicated Genepop documentation. For tutorials on interacting with SIMCOAL2 and fdist2, consult the PopGen chapter in the Biopython Tutorial.

  4. Use Bio.AlignIO for multiple sequence alignments

    master

    Bio.AlignIO is the primary module for reading and writing multiple sequence alignments in Biopython. It provides a uniform interface for various alignment file formats, similar to how Bio.SeqIO works for individual sequences.

    When working with alignments, use Bio.AlignIO as your first choice. If you only need the raw sequences from an alignment file and don't care about the alignment structure, you can use Bio.SeqIO directly on the file.

  5. What is a SeqRecord object?

    master

    A SeqRecord object is the primary container in Biopython used to hold a biological sequence (as a Seq object) along with its associated metadata. It includes identifiers (id and name), a description, and optional annotations and features (sub-features).

    While most users obtain SeqRecord objects by parsing files using the Bio.SeqIO module, they can also be instantiated manually.

    Key components of a SeqRecord include:

    • Sequence: The actual biological sequence, stored as a Bio.Seq.Seq object.
    • Identifiers: String attributes for id, name, and description.
    • Annotations: A dictionary containing metadata (e.g., taxonomy, source, or references).
    • Features: A list of SeqFeature objects representing specific regions of the sequence.
    • Database Cross-references: A list of database identifiers (dbxrefs).
    from Bio.Seq import Seq
    from Bio.SeqRecord import SeqRecord
    
    record = SeqRecord(
        Seq("MKQHKAMIVALIVICITAVVAALVTRKDLCEVHIRTGQTEVAVF"),
        id="YP_025292.1",
        name="HokC",
        description="toxic membrane protein, small",
    )
    print(record)
  6. Dimensions for developing Population Genetics statistics

    master

    When implementing new statistics for the Population Genetics module, developers must account for several distinct dimensions to ensure the framework is flexible enough for different research communities (e.g., cancer studies vs. conservation).

    Key dimensions to consider include:

    • Marker Dependency: Determine if the statistic is marker independent (e.g., $F_{ST}$ which relies on allele counts) or marker dependent (e.g., Tajima's D which requires sequences/RFLPs, or Allelic range which requires microsatellites/STRs).
    • Population Scope: Distinguish between Intra-Population statistics (e.g., observed heterozygosity, which has no notion of population structure) and Inter-population statistics (e.g., $F_{ST}$, used to measure population differentiation).
    • Data Type: Identify the required input format:
      • Haplotypic
      • Genotypic (phase known)
      • Genotypic (phase unknown)
      • Genotypic dominant
      • Frequency only (e.g., sufficient for expected heterozygosity, but not for observed heterozygosity).
    • Locus Scale: Specify if the statistic is Single locus (e.g., allelic richness, $F_{ST}$) or Multi-loci (e.g., linkage disequilibrium (LD) or EHH).
    • Temporal Aspect: Determine if the statistic is Temporal/longitudinal (e.g., temporal-$F_{ST}$) or a Single point in time.

    A robust implementation should support a wide range of marker densities and dataset sizes, from a few loci to millions of SNPs.

  7. How Bio.SearchIO handles sequence coordinates

    master

    Bio.SearchIO uses standardized conventions for coordinates to ensure consistency across different search formats and to allow direct use with Python slicing.

    Python-style coordinates

    Coordinates use zero-based, half-open intervals.

    • Example: If a BLAST XML file reports HSP start/end as 10 and 28, Bio.SearchIO stores them as 9 and 28.
    • This allows you to slice a sequence directly: sequence[start:end].
    • When writing to a file via Bio.SearchIO.write, these values are automatically converted back to the original format's convention.

    Coordinate order

    In some formats (like BLAST plain text on the minus strand), the start coordinate is larger than the end coordinate.

    • In Bio.SearchIO, start is always smaller than end, regardless of the strand.
    • This convention is enforced at the HSPFragment level. The order of fragments within an HSP object still follows the original file's order.
    • Like Python-style coordinates, these are restored to the original format's convention upon writing.
  8. Optional software for Biopython features

    master

    Certain Biopython functionalities require additional Python libraries or third-party command-line tools:

    Python Libraries

    • ReportLab: Used for PDF graphics code.
    • psycopg: Used for BioSQL with a PostgreSQL database.
    • mysql-connector or MySQLdb: Used for BioSQL with a MySQL database.

    Third-party Command Line Tools

    Biopython includes wrapper code for several tools. To use these features, the tools must be installed on your system:

    • Wise2: For the dnal command line tool.
    • NCBI Standalone BLAST: For running BLAST locally.
    • Clustalw: For building sequence alignments.
    • SIMCOAL2 and FDist: For population genetics.
    • EMBOSS: A collection of useful command line tools.
  9. Use _BitString for clade representation and comparison

    master

    The _BitString class is an internal utility in Bio.Phylo.Consensus used to represent clades as binary strings. This allows for efficient counting and comparison of clades across multiple trees.

    Capabilities:

    • Binary Operations: Supports & (AND), | (OR), ^ (XOR), and ~ (NOT).
    • Inspection: index_one() returns indices of '1's; index_zero() returns indices of '0's.
    • Comparison: contains(), independent(), and iscompatible() methods for bitstring relationship testing.

    Manual Clade to BitString Conversion: To represent a clade as a _BitString relative to a set of terminal names:

    1. Get all terminal names from the reference tree.
    2. For a specific clade, check which terminal names it contains.
    3. Construct a string of '1's and '0's based on those matches.
    # Example: Creating a bitstring for a clade
    term_names = [term.name for term in trees[0].get_terminals()]
    clade_term_names = [term.name for term in clade.get_terminals()]
    boolvals = [name in clade_term_names for name in term_names]
    bitstr = _BitString("".join(map(str, map(int, boolvals))))