Sunbeam Metagenomic Sequencing Pipeline

repository·main·Indexed 41 days ago

https://github.com/sunbeam-labs/sunbeam

A robust, extensible metagenomic sequencing pipeline built on Snakemake. Sunbeam automates core analysis tasks including quality control, taxonomic assignment via Kraken, assembly with Megahit, contig annotation using BLAST and Diamond, mapping to reference genomes, and ORF prediction with Prodigal. It features a modular extension system allowing researchers to add custom analysis and reporting capabilities using .smk rules, configuration files, and Conda environments.

Tokens
8.2K
Snippets
30
Records
40
Agent score
48%

What's inside Sunbeam

  1. Overview of Sunbeam metagenomic sequencing pipeline

    main
    Sunbeam is a robust, extensible metagenomic sequencing pipeline built on snakemake. It automates common metagenomic analysis tasks such as quality control (adaptor trimming, host read removal, and quality filtering) and decontamination of host-contaminated reads. The pipeline is designed to be modular, allowing users to add extensions for assembly, annotation, read alignments, and taxonomic classifications.
  2. What is Sunbeam?

    main

    Sunbeam is a robust, extensible metagenomic sequencing pipeline built on snakemake. It automates various stages of metagenomic analysis, including quality control, taxonomic assignment, assembly, and mapping. It is designed to be modular, allowing users to extend its functionality using specialized modules (e.g., sbx_kraken, sbx_assembly).

    Sunbeam automates the following core tasks:

    • Quality control: Includes adapter trimming, host read removal, and quality filtering.
    • Taxonomic assignment: Assigns reads to databases using Kraken (via sbx_kraken).
    • Assembly: Assembles reads into contigs using Megahit (via sbx_assembly).
    • Contig annotation: Uses BLAST[n/p/x] and Diamond (via sbx_assembly).
    • Mapping: Maps to reference genomes (via sbx_mapping).
    • ORF prediction: Predicts Open Reading Frames using Prodigal (via sbx_assembly).
  3. How Sunbeam extensions are loaded and managed

    main

    Sunbeam uses an extension system to modularize functionality. At runtime, Sunbeam loads extensions from a directory specified by the $SUNBEAM_EXTENSIONS environment variable, which defaults to ~/.sunbeam/extensions.

    When running the pipeline, you can control which extensions are included or excluded using environment variables or arguments passed to sunbeam run:

    • SUNBEAM_EXTS_INCLUDE: Includes only the specified extension and excludes all others. Useful for testing a single extension.
    • SUNBEAM_EXTS_EXCLUDE: Excludes specific extensions. Useful for resolving namespace collisions between extensions.
  4. How Sunbeam extensions work

    main

    Sunbeam extensions are Snakemake workflows that can be layered on top of the core Sunbeam workflow. They allow you to add new functionality, integrate external tools, or customize the pipeline.

    By default, Sunbeam searches for extensions in ~/.sunbeam/extensions. You can override this location by setting the $SUNBEAM_EXTENSIONS environment variable.

  5. Best practices for importing sunbeam in Snakemake

    main

    While many functions in the sunbeam package (like parse_fastq()) are designed for use in scripts and Snakemake, avoid importing sunbeam directly into a Snakemake rule that uses the conda: keyword.

    If sunbeam is installed in a virtual environment but the rule executes in a separate Conda environment, the module will not be available. To avoid this, split your logic into separate rules: one for the Python logic requiring sunbeam and another for the rule requiring specific Conda dependencies.

  6. Run an installed extension

    main

    To execute an extension within an existing project, use the sunbeam run command with the --profile flag pointing to your project directory, specifying the extension's profile name.

    For sbx_kraken, the command is:

    sunbeam run --profile my_project/ all_kraken

    Sunbeam is intelligent enough to recognize which steps (like QC or decontamination) have already been completed and will only run the new extension on the appropriate processed reads. Outputs for sbx_kraken are stored in [project_dir]/sunbeam_output/classify/kraken/.

  7. Update project configuration after installing an extension

    main

    After installing an extension, you must update your project's configuration file to include the new extension's specific parameters. Use the sunbeam config update command pointing to your project's configuration file.

    Example for sbx_kraken:

    sunbeam config update my_project/sunbeam_config.yml

    This will append the necessary keys to your sunbeam_config.yml. You must then manually edit the file to provide the required values (e.g., file paths for databases).

  8. System requirements for Sunbeam

    main

    To run Sunbeam, your environment must meet the following minimum requirements:

    • Operating System: A reasonably modern GNU/Linux computer.
    • Python Version: Python 3.11 or higher.
    • Internet Access: Required to retrieve dependencies.
    • Memory (RAM): At least 4GB of RAM.
    • Storage: Disk space requirements vary based on the databases, tasks, and data size selected.

    Note that RAM and disk space requirements may increase depending on the specific databases and tasks you choose to run.

  9. Initialize a Sunbeam project

    main

    Use sunbeam init to create a new project directory. You must provide the path to your sequencing reads using the --data_fp flag. Reads must be in .fastq.gz format.

    Initialization Modes:

    • Standard: Uses Conda for local compute.
    • Slurm: Uses a Slurm cluster (requires snakemake-executor-plugin-slurm installed). Use --profile slurm.
    • Apptainer/Singularity: Uses Apptainer/Singularity for local compute. Use --profile apptainer.
    • Docker: Run inside a container. You must mount local data and output directories using -v flags. Paths in the command must correspond to the paths inside the container.

    Project Output: Initialization creates a project folder containing:

    • config.yaml: A Snakemake profile for running the project.
    • sunbeam_config.yml: Configuration parameters for each pipeline step.
    • samples.csv: A list of discovered samples and their absolute paths to FASTQ files.
    ### Standard (Conda, local)
    sunbeam init my_project --data_fp /sequencing/project/reads
    
    ### Slurm
    pip install snakemake-executor-plugin-slurm
    sunbeam init my_project --data_fp /sequencing/project/reads --profile slurm
    
    ### Apptainer/Singularity
    sunbeam init my_project --data_fp /sequencing/project/reads --profile apptainer
    
    ### Docker
    docker run --rm -v /local/path/to/data/:/data/ -v /local/path/to/outputs/:/projects/ sunbeamlabs/sunbeam:latest sunbeam init --data_fp /data/reads/ /projects/my_project
  10. Install Sunbeam

    main

    Sunbeam can be installed via pip, conda, git, or Docker.

    Requirements:

    • Python 3.11+ for pip installation.
    • Gzipped FASTQ format (.fastq.gz) for sequencing reads.

    Installation Methods:

    • pip: Install sunbeamlib from PyPI into a virtual environment.
    • conda: Install sunbeamlib from the conda-forge and bioconda channels.
    • git: Clone the repository and install in editable mode. Use .[dev] to include development dependencies.
    • Docker: Pull the sunbeamlabs/sunbeam:latest image. There is also a slim version that requires manual conda environment management.
    ### pip
    python -m venv --python=python3.13 sunbeam_env/
    source sunbeam_env/bin/activate
    pip install sunbeamlib
    
    ### conda
    conda create --name sunbeam_env -c conda-forge -c bioconda sunbeamlib
    conda activate sunbeam_env
    
    ### git
    git clone https://github.com/sunbeam-labs/sunbeam.git
    cd sunbeam
    python -m venv --python=python3.13 sunbeam_env/
    source sunbeam_env/bin/activate
    pip install -e .[dev]
    
    ### docker
    docker pull sunbeamlabs/sunbeam:latest
    docker run --rm sunbeamlabs/sunbeam:latest sunbeam -h
  11. Write Snakemake rules for a Sunbeam extension

    main

    Extensions use Snakemake format to define how files are processed. A complete extension usually requires three components:

    1. Rules File (.smk): Defines the workflow logic, including target rules (the desired outputs) and runner rules (the actual execution steps).
    2. Configuration (config.yml): Defines parameters required by your rules.
    3. Environment (envs/*.yml): Specifies the Conda environment needed to run the tool.

    Example: Adding a tool (AwesomeTool)

    To add a tool that takes input FASTQ files and a database to produce an output, your .smk file would look like this:

    AWESOME_FP = CLASSIFY_FP / 'awesome'
    
    rule all_awesome_tool:
        input:
            expand(AWESOME_FP / "{sample}.awesome", sample=Samples.keys())
    
    rule run_awesome_tool:
        input:
            r1 = QC_FP / "decontam" / "{sample}_1.fastq.gz",
            r2 = QC_FP / "decontam" / "{sample}_2.fastq.gz",
            db = Cfg['all']['awesome_db']
        output:
            AWESOME_FP / "{sample}.awesome"
        conda:
            "awesome_env.yaml"
        shell:
            """
            AwesomeTool -i {input.r1} -d {input.db} -o {output}
            """

    Configuration and Environment

    Update your config.yml to include the new parameters:

    sbx_awesome_tool:
        awesome_db: /path/to/awesome_db

    And define the Conda environment in envs/sbx_awesome_tool_env.yml:

    name: awesome_env
    channels:
        - bioconda
        - conda-forge
    dependencies:
        - awesome_tool