Skip to main content

Parallelizing operations on SAM/BAM files

SAM/BAM files are typically large, thus, operations on these files are time intensive. This project provides tools to parallelize operations on SAM/BAM files. The workflow follows:

  1. Split BAM/SAM file in n chunks
  2. Perform operation in each chunk in a dedicated process and save resulting SAM/BAM chunk
  3. Merge results back into a single SAM/BAM file

Depends on:

  1. Samtools, can be installed with conda

Installation

pip install parallelbam

or

  1. Git clone project
  2. cd to cloned project directory
  3. sudo python3 setup.py install

Better to install within an environment, such as a conda environment, to avoid path conflicts with the included bash scripts.

Usage

There is one main function named parallelizedBAMoperation. This function takes as mandatory arguments:

  1. path to original bam file (should be ordered)
  2. a callable function to perform the operation on each bam file chunk

The callable function must accept the following two first arguments:

  1. path to input bam file and
  2. path to resulting output bam file

in this order.

TODO

  1. The current way to include bash scripts in the package, while working, seems awkward. Perhaps including bash code directly in subprocess would be simpler
  2. Having permission error in some installations upon calling splitBAM.sh, can one make it executable during installation?
from parallelbam.parallelbam import parallelizeBAMoperation, getNumberOfReads

As an example, let's create a function that simply copies a bam file to another directory (does nothing to the bam file). When calling this function in parallelizeBAMoperation it will imply split the BAM file in chunks and the merge them back into a single BAM, whih sould be identical to the first one. We will split the BAM file in 8 chunks, and are dummy function will be called in separate process for each chunk.

import shutil

def foo(input_bam, output_bam):
    shutil.copyfile(input_bam, output_bam)
    
    
parallelizeBAMoperation('sample.bam',
                        foo, output_dir=None,
                        n_processes=8)

By default the BAM is name-sorted before splitting so that reads sharing a query name (e.g. mates) end up in the same chunk. This sort is a full, serial pass over the whole file and is often the dominant cost on large inputs. If your callback processes each read independently (e.g. filtering by per-read identity), pass sort_by_name=False to skip the sort and split the file as-is, which is substantially faster:

parallelizeBAMoperation('sample.bam',
                        foo, output_path=None,
                        n_processes=8,
                        sort_by_name=False)

To check that the processed bam file, after merging the 8 chunks, contains the same number of reads we can call getNumberOfReads.

getNumberOfReads('sample.bam')
11825588
getNumberOfReads('processed.bam')
11825588

Metadata

Release files for parallelbam-tools 0.0.20

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for parallelbam-tools 0.0.20
File Size Uploaded
parallelbam_tools-0.0.20.tar.gz 10.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for parallelbam-tools 0.0.20
File Interpreter ABI Platform
parallelbam_tools-0.0.20-py3-none-any.whl Python 3 none any Details

Total release size: 22.9 kB

Release files / parallelbam_tools-0.0.20.tar.gz

Download URL parallelbam_tools-0.0.20.tar.gz
Size 10.9 kB
Tags Source
SHA-256 checksum
How to use checksums
bbf7a9314e37ae2416ab21778f55c7f740e04fb74d28ea79679e7ce5e162e387
BLAKE2b-256 checksum
How to use checksums
b3ca71762ae49ac8431902a1627f110d932f769cfe95677d731677a54ca926e3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 19, 2026.

Transparency log

Release files / parallelbam_tools-0.0.20-py3-none-any.whl

Download URL parallelbam_tools-0.0.20-py3-none-any.whl
Size 12.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d42bcc1d12e3b4b29db9c3c8f53dd4aa0408c18e54f38c5466ce87c1675701d3
BLAKE2b-256 checksum
How to use checksums
fbef33b08d0251734a96464fc1088d0f335875690bf7214afec35f8fdedf1800
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 19, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.0.20 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page