Python bindings for Nanalogue: single-molecule BAM/Mod-BAM analysis
Project description
pynanalogue
PyNanalogue = Python Nucleic Acid Analogue.
Nanalogue is a tool to parse or analyse BAM/Mod BAM files with a single-molecule focus. We expose some of Nanalogue's functions through a python interface here.
A common pain point in genomics analyses is that BAM files are information-dense which makes it difficult to gain insight from them. PyNanalogue hopes to make it easy to extract and process this information, with a particular focus on single-molecule aspects and DNA/RNA modifications. Despite this focus, some of pynanalogue's functions are quite general and can be applied to almost any BAM file.
We can process any type of DNA/RNA modifications occuring in any pattern (single/multiple mods, spatially-isolated/non-isolated etc.). All we require is that the data is stored in a BAM file in the mod BAM format (i.e. using MM/ML tags as laid down in the specifications).
Table of Contents
Note: these links work in Github but may not work on PyPI. But the table of contents is correct.
Requirements
- Python 3.9 or higher
- Rust toolchain (for building from source)
Installation
PyNanalogue should be available on PyPI. You can run the following command or an equivalent to install it.
pip install pynanalogue
More details
Common wheels (manylinux/mac) are available in PyPI.
Please open an issue if you want more wheels!
Note on musllinux
Although we have a musllinux wheel, a package we depend on (polars-runtime-32) does not
have one (see this issue), which means
you may have to build the wheel yourself during installation. Please open an issue if you
spot that this has changed! This issue affects only those that use an Alpine Linux distribution
or similar. If you are using Ubuntu/Debian/Fedora etc., this issue should not affect you.
Functions
Our package exposes the following python functions.
They usually have lots of optional arguments.
Among other operations, the options allow you to subsample the BAM file (sample_fraction),
restrict read and/or modification data to a specific genomic region (region or mod_region),
restrict by one or several read ids (read_ids),
a specific mapping type (read_filter), filter modification data suitably
(min_mod_qual, reject_mod_qual_non_inclusive) etc.
Please see each section below for the list of options, which you can access by
reading the docstring of each function.
Read info
Prints information about reads in JSON. In this section, we show how to get documentation about the function, a sample execution, and a sample output snippet.
Documentation
The function has lots of helpful optional arguments. Please run the command below to see what they are.
import pynanalogue as pn
print(pn.read_info.__doc__)
Sample input and output
A sample execution and output snippet follows.
import pynanalogue as pn
import json
result_bytes = pn.read_info("tests/data/examples/example_1.bam")
decoded_output = json.loads(result_bytes)
A record from the decoded output might look like the following. You will get one record per alignment.
[
{
"read_id": "a4f36092-b4d5-47a9-813e-c22c3b477a0c",
"sequence_length": 48,
"contig": "dummyIII",
"reference_start": 23,
"reference_end": 71,
"alignment_length": 48,
"alignment_type": "primary_forward",
"mod_count": "T+T:3;(probabilities >= 0.5020, PHRED base qual >= 0)"
}
]
Window reads
Output windowed modification densities of reads as a polars dataframe. In this section, we show how to get documentation about the function, a sample execution, and a sample output snippet.
Documentation
The function has lots of helpful optional arguments and some required arguments like window size and step size. Please run the command below to see what they are.
import pynanalogue as pn
print(pn.window_reads.__doc__)
Sample input and output
A sample execution and output snippet follows.
import pynanalogue as pn
import polars as pl
df = pn.window_reads("tests/data/examples/example_1.bam",win = 2,step = 1)
The output is a polars dataframe. If printed in tsv format, a few rows may look like this. (This was generated from a file without basecalling quality information, which is why we show 255s here).
#contig ref_win_start ref_win_end read_id win_val strand base mod_strand mod_type win_start win_end basecall_qual
dummyI 9 13 5d10eb9a-aae1-4db8-8ec6-7ebb34d32575 0 + T + T 0 4 255
dummyI 12 14 5d10eb9a-aae1-4db8-8ec6-7ebb34d32575 0 + T + T 3 5 255
dummyI 13 17 5d10eb9a-aae1-4db8-8ec6-7ebb34d32575 0 + T + T 4 8 255
dummyIII 26 32 a4f36092-b4d5-47a9-813e-c22c3b477a0c 1 + T + T 3 9 255
dummyIII 31 51 a4f36092-b4d5-47a9-813e-c22c3b477a0c 0.5 + T + T 8 28 255
dummyIII 50 63 a4f36092-b4d5-47a9-813e-c22c3b477a0c 0 + T + T 27 40 255
dummyIII 62 71 a4f36092-b4d5-47a9-813e-c22c3b477a0c 0.5 + T + T 39 48 255
dummyII 15 17 fffffff1-10d2-49cb-8ca3-e8d48979001b 0 - T + T 12 14 255
dummyII 16 20 fffffff1-10d2-49cb-8ca3-e8d48979001b 0 - T + T 13 17 255
dummyII 19 23 fffffff1-10d2-49cb-8ca3-e8d48979001b 0 - T + T 16 20 255
dummyII 22 24 fffffff1-10d2-49cb-8ca3-e8d48979001b 0.5 - T + T 19 21 255
. -1 -1 a4f36092-b4d5-47a9-813e-c22c3b477a0c 0 . G - 7200 28 30 255
. -1 -1 a4f36092-b4d5-47a9-813e-c22c3b477a0c 0 . G - 7200 29 31 255
. -1 -1 a4f36092-b4d5-47a9-813e-c22c3b477a0c 0 . G - 7200 30 33 255
. -1 -1 a4f36092-b4d5-47a9-813e-c22c3b477a0c 0 . G - 7200 32 44 255
. -1 -1 a4f36092-b4d5-47a9-813e-c22c3b477a0c 0 . G - 7200 43 45 255
. -1 -1 a4f36092-b4d5-47a9-813e-c22c3b477a0c 1 . T + T 3 9 255
. -1 -1 a4f36092-b4d5-47a9-813e-c22c3b477a0c 0.5 . T + T 8 28 255
. -1 -1 a4f36092-b4d5-47a9-813e-c22c3b477a0c 0 . T + T 27 40 255
. -1 -1 a4f36092-b4d5-47a9-813e-c22c3b477a0c 0.5 . T + T 39 48 255
Polars bam mods
Output raw modification data as a polars dataframe. In this section, we show how to get documentation about the function, a sample execution, and a sample output snippet. Please note that as we report every modified position per molecule as a separate row in a dataframe, the data size could get very big. So, we recommend querying per region or subsampling the BAM file in order to not run into memory issues -- there are options in this function to do so. We may develop an iterable version of this function in the future. Please open an issue if you are interested in this, or a pull request if you can do this!
Documentation
The function has lots of helpful optional arguments. Please run the command below to see what they are.
import pynanalogue as pn
print(pn.polars_bam_mods.__doc__)
Sample input and output
A sample execution and output snippet follows.
import pynanalogue as pn
import polars as pl
df = pn.polars_bam_mods("tests/data/examples/example_1.bam")
The output is a polars dataframe. If printed in tsv format, it might look like this. Mod quality is a probability represented as a number between 0 and 255, where 0 means not modified and 255 means modified with certainty. This is how modification data is stored in the mod BAM format.
read_id seq_len alignment_type align_start align_end contig contig_id base is_strand_plus mod_code position ref_position mod_quality
5d10eb9a-aae1-4db8-8ec6-7ebb34d32575 8 primary_forward 9 17 dummyI 0 T true T 0 9 4
5d10eb9a-aae1-4db8-8ec6-7ebb34d32575 8 primary_forward 9 17 dummyI 0 T true T 3 12 7
5d10eb9a-aae1-4db8-8ec6-7ebb34d32575 8 primary_forward 9 17 dummyI 0 T true T 4 13 9
5d10eb9a-aae1-4db8-8ec6-7ebb34d32575 8 primary_forward 9 17 dummyI 0 T true T 7 16 6
a4f36092-b4d5-47a9-813e-c22c3b477a0c 48 primary_forward 23 71 dummyIII 2 T true T 3 26 221
a4f36092-b4d5-47a9-813e-c22c3b477a0c 48 primary_forward 23 71 dummyIII 2 T true T 8 31 242
a4f36092-b4d5-47a9-813e-c22c3b477a0c 48 primary_forward 23 71 dummyIII 2 T true T 27 50 3
a4f36092-b4d5-47a9-813e-c22c3b477a0c 48 primary_forward 23 71 dummyIII 2 T true T 39 62 47
a4f36092-b4d5-47a9-813e-c22c3b477a0c 48 primary_forward 23 71 dummyIII 2 T true T 47 70 239
fffffff1-10d2-49cb-8ca3-e8d48979001b 33 primary_reverse 3 36 dummyII 1 T true T 12 15 3
fffffff1-10d2-49cb-8ca3-e8d48979001b 33 primary_reverse 3 36 dummyII 1 T true T 13 16 3
fffffff1-10d2-49cb-8ca3-e8d48979001b 33 primary_reverse 3 36 dummyII 1 T true T 16 19 4
fffffff1-10d2-49cb-8ca3-e8d48979001b 33 primary_reverse 3 36 dummyII 1 T true T 19 22 3
fffffff1-10d2-49cb-8ca3-e8d48979001b 33 primary_reverse 3 36 dummyII 1 T true T 20 23 182
a4f36092-b4d5-47a9-813e-c22c3b477a0c 48 unmapped G false 7200 28 -1 0
a4f36092-b4d5-47a9-813e-c22c3b477a0c 48 unmapped G false 7200 29 -1 0
a4f36092-b4d5-47a9-813e-c22c3b477a0c 48 unmapped G false 7200 30 -1 0
a4f36092-b4d5-47a9-813e-c22c3b477a0c 48 unmapped G false 7200 32 -1 0
a4f36092-b4d5-47a9-813e-c22c3b477a0c 48 unmapped G false 7200 43 -1 77
a4f36092-b4d5-47a9-813e-c22c3b477a0c 48 unmapped G false 7200 44 -1 0
a4f36092-b4d5-47a9-813e-c22c3b477a0c 48 unmapped T true T 3 -1 221
a4f36092-b4d5-47a9-813e-c22c3b477a0c 48 unmapped T true T 8 -1 242
a4f36092-b4d5-47a9-813e-c22c3b477a0c 48 unmapped T true T 27 -1 0
a4f36092-b4d5-47a9-813e-c22c3b477a0c 48 unmapped T true T 39 -1 47
a4f36092-b4d5-47a9-813e-c22c3b477a0c 48 unmapped T true T 47 -1 239
Simulate mod bam
If you are a developer who needs BAM files with defined single-molecule modification patterns to help develop/test your tool, nanalogue can also help you create BAM files from scratch using artificial data created using parameters defined by you.
import pynanalogue as pn
print(pn.simulate_mod_bam.__doc__)
Example
This example generates two contigs with random DNA sequences with the given properties and stores them in a fasta file. Then, it generates a BAM file with the modification pattern and other properties as shown. In this example, the reads are methylated, with 5Cs with a probability drawn randomly in the range 30-70% and three Cs with a probability in the range 10%-50%. This 5C, 3C pattern repeats throughout the read. You can then analyze this pattern with your tool and test its functionality. You can set up multiple modifications etc. Please have a look at the documentation here for the options available in the json configuration.
import pynanalogue
json_config = '''
{
"contigs": {
"number": 2,
"len_range": [100, 200],
"repeated_seq": "ACGTACGT"
},
"reads": [
{
"number": 10,
"mapq_range": [10, 30],
"base_qual_range": [20, 40],
"len_range": [0.1, 0.9],
"barcode": "ACGTAA",
"mods": [{
"base": "C",
"is_strand_plus": true,
"mod_code": "m",
"win": [5, 3],
"mod_range": [[0.3, 0.7], [0.1, 0.5]]
}]
}
]
}
'''
pynanalogue.simulate_mod_bam(
json_config=json_config,
bam_path="output.bam",
fasta_path="output.fasta"
)
Further documentation
In addition to this repository, we are developing a companion cookbook here.
Changelog
For a detailed list of changes in each version, please see CHANGELOG.md in the repository.
Versioning
We use Semantic Versioning (SemVer) for version numbers.
Current Status: Pre-1.0 (0.x.y)
While in 0.x.y versions:
- The API may change without notice
- Breaking changes can occur in minor version updates
- This is a development phase with no stability guarantees
After 1.0.0 Release:
Once we reach version 1.0.0, we will guarantee:
- No breaking changes in minor (x.Y.z) or patch (x.y.Z) releases
- Clear migration guides for major version updates
- Deprecation warnings at least one minor version before removal of features
Acknowledgments
This software was developed at the Earlham Institute in the UK. This work was supported by the Biotechnology and Biological Sciences Research Council (BBSRC), part of UK Research and Innovation, through the Core Capability Grant BB/CCG2220/1 at the Earlham Institute and the Earlham Institute Strategic Programme Grant Cellular Genomics BBX011070/1 and its constituent work packages BBS/E/ER/230001B (CellGen WP2 Consequences of somatic genome variation on traits). The work was also supported by the following response-mode project grants: BB/W006014/1 (Single molecule detection of DNA replication errors) and BB/Y00549X/1 (Single molecule analysis of Human DNA replication). This research was supported in part by NBI Research Computing through use of the High-Performance Computing system and Isilon storage.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pynanalogue-0.1.4.tar.gz.
File metadata
- Download URL: pynanalogue-0.1.4.tar.gz
- Upload date:
- Size: 94.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
458de385bbd884dfda0577fb0fc93e932a854a7e1a4240663a815a589817105f
|
|
| MD5 |
7e79e0c3883a431ba892e47d6afe7508
|
|
| BLAKE2b-256 |
623ac1957d8cbdaf77e5617a10794f1fcb55d88e00d518854c107b66dd9db4b4
|
File details
Details for the file pynanalogue-0.1.4-cp39-abi3-musllinux_1_2_x86_64.whl.
File metadata
- Download URL: pynanalogue-0.1.4-cp39-abi3-musllinux_1_2_x86_64.whl
- Upload date:
- Size: 8.0 MB
- Tags: CPython 3.9+, musllinux: musl 1.2+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
27f9684483aea51b4da7633692c4f1980be87de01db3f4392bf827fd1bd1d17f
|
|
| MD5 |
37eb457edbf80992ad920253091af968
|
|
| BLAKE2b-256 |
4c83f2e04c9d7b8308738d260c9639a02fcc5a7c49b2b1d4cc7cfa794cb7dd0e
|
File details
Details for the file pynanalogue-0.1.4-cp39-abi3-musllinux_1_2_aarch64.whl.
File metadata
- Download URL: pynanalogue-0.1.4-cp39-abi3-musllinux_1_2_aarch64.whl
- Upload date:
- Size: 7.8 MB
- Tags: CPython 3.9+, musllinux: musl 1.2+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eb35ee795ebfb5f03af05d990b33bac436ca58c550c6a041006485a1d73049ea
|
|
| MD5 |
f507cdc1624001fd814a58c5271b9069
|
|
| BLAKE2b-256 |
459cce390bb6ae0306d579d89e4e22035ca367c6c7d93525eb417a81bba67958
|
File details
Details for the file pynanalogue-0.1.4-cp39-abi3-manylinux_2_34_x86_64.whl.
File metadata
- Download URL: pynanalogue-0.1.4-cp39-abi3-manylinux_2_34_x86_64.whl
- Upload date:
- Size: 8.0 MB
- Tags: CPython 3.9+, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7ddd64173a9cb3ff5f6ad9b22035dc0107a5a6eee76ddde7237630261efd092b
|
|
| MD5 |
c560bf752f80dbf97a5ecdb345d35c88
|
|
| BLAKE2b-256 |
67757415efd810f44956e98157e07b4936f0b6ef6d0940603d6e108acf4e4bad
|
File details
Details for the file pynanalogue-0.1.4-cp39-abi3-manylinux_2_34_aarch64.whl.
File metadata
- Download URL: pynanalogue-0.1.4-cp39-abi3-manylinux_2_34_aarch64.whl
- Upload date:
- Size: 7.8 MB
- Tags: CPython 3.9+, manylinux: glibc 2.34+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
27c7df62deb913fa35a6b58fb32810bed22c2585ee95e18262468551f1c862c9
|
|
| MD5 |
037cc9a3fad50e9c8a2bea59ca972027
|
|
| BLAKE2b-256 |
44cff82c548a45d2dc7022706b359aa105f9eba1bb78e3777e9a310479771d54
|
File details
Details for the file pynanalogue-0.1.4-cp39-abi3-manylinux_2_28_x86_64.whl.
File metadata
- Download URL: pynanalogue-0.1.4-cp39-abi3-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 8.0 MB
- Tags: CPython 3.9+, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8be9824d47f61cd4be5bee1c4c0533464d61db463816cafb6f727eeb76157bb4
|
|
| MD5 |
fb19941d67d1d6aa81fad7cbba11a5a7
|
|
| BLAKE2b-256 |
38904edacca6ab0fabd351546674e3c9e84e95c8905c76c523a94b8f87fcac37
|
File details
Details for the file pynanalogue-0.1.4-cp39-abi3-manylinux_2_28_aarch64.whl.
File metadata
- Download URL: pynanalogue-0.1.4-cp39-abi3-manylinux_2_28_aarch64.whl
- Upload date:
- Size: 7.8 MB
- Tags: CPython 3.9+, manylinux: glibc 2.28+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
45b0b99af56d8ff6d5fa15d38d5f0d7be0d18cf4acda4bbe6a887905b8101fd2
|
|
| MD5 |
75e8528419a93e09e63b05d6c3ab8b98
|
|
| BLAKE2b-256 |
235cac5ad29eef133479db840175ab80a09989bcbd540c93b6f2e70d91518c8c
|
File details
Details for the file pynanalogue-0.1.4-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: pynanalogue-0.1.4-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 8.0 MB
- Tags: CPython 3.9+, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
032d039502db439103f4b5b0f1458274f876ee5ba88b187fec7f38857531213c
|
|
| MD5 |
1020cf88e19d4544862c7e7699a1a1fd
|
|
| BLAKE2b-256 |
690e50e0f0e275fa393bf46c66e40b4f2140030a0c13aab40c71c843455a1170
|
File details
Details for the file pynanalogue-0.1.4-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.
File metadata
- Download URL: pynanalogue-0.1.4-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 7.8 MB
- Tags: CPython 3.9+, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
96310ed7a7c1cda5af436b23f014723f4a50e5c0efa702c35f008743ff03ec56
|
|
| MD5 |
71ffd209fcdc96971bd09a369a8935a4
|
|
| BLAKE2b-256 |
9ead9bbc61d64710ecc6a5c3f08b5ade2c6318d521e43276647db1be4300068c
|
File details
Details for the file pynanalogue-0.1.4-cp39-abi3-macosx_11_0_arm64.whl.
File metadata
- Download URL: pynanalogue-0.1.4-cp39-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 7.1 MB
- Tags: CPython 3.9+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
96f9f540c72b0c21bce781320db40fc6ee93e63d03bf5b4c298515f9502e8ff1
|
|
| MD5 |
dece47055688c0874d22991aea835a3f
|
|
| BLAKE2b-256 |
e4661b4f252e4bdaf9b60c3889c0edcd970f85d8a478609fc31cd5890af84c64
|
File details
Details for the file pynanalogue-0.1.4-cp39-abi3-macosx_10_12_x86_64.whl.
File metadata
- Download URL: pynanalogue-0.1.4-cp39-abi3-macosx_10_12_x86_64.whl
- Upload date:
- Size: 7.3 MB
- Tags: CPython 3.9+, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
08d7b77d5517647ecfa85adfd5985c9b751860c99f08d511202f4f3f5fba3676
|
|
| MD5 |
3d00fcd22c2c5ca28fcf212e65e4a2d3
|
|
| BLAKE2b-256 |
6135a6f8eedd44c04cc282742355f8d1d94329ab7bf9868b6fc23f45dd270c60
|