No project description provided
Project description
CNV From BAM
cnv_from_bam
is a Rust library developed to efficiently calculate dynamic Copy Number Variation (CNV) profiles from sequence alignments contained in BAM files. It seamlessly integrates with Python using PyO3, making it an excellent choice for bioinformatics workflows involving genomic data analysis.
Features
- Efficient Processing: Optimized for handling large genomic datasets in BAM format.
- Python Integration: Built with PyO3 for easy integration into Python-based genomic analysis workflows.
- Multithreading Support: Utilizes Rust's powerful concurrency model for improved performance.
- Dynamic Binning: Bins the genome dynamically based on total read counts and genome length.
- CNV Calculation: Accurately calculates CNV values for each bin across different contigs.
- Directory Support: Supports processing of multiple BAM files in a directory. (Requires alignment to the same reference in all BAM files)
Installation
To use cnv_from_bam
in your Rust project, add the following to your Cargo.toml
file:
[dependencies]
cnv_from_bam = "0.1.0" # Replace with the latest version
Usage
Here's a quick example of how to use the iterate_bam_file
function:
use cnv_from_bam::iterate_bam_file;
use std::path::PathBuf;
let bam_path = PathBuf::from("path/to/bam/file.bam");
// Iterate over the BAM file and calculate CNV values for each bin. Number of threads is set to 4 and mapping quality filter is set to 60.
// If number of threads is not specified, it defaults to the number of logical cores on the machine.
let result = iterate_bam_file(bam_path, Some(4), Some(60), None, None);
// Process the result...
The results in this case are returned as a CnvResult, which has the following structure:
pub struct CnvResult {
pub cnv: FnvHashMap<String, Vec<f64>>,
pub bin_width: usize,
pub genome_length: usize,
}
Where result.cnv
is a hash map containing the Copy Number for each bin of bin_width
bases for each contig in the reference genome, result.bin_width
is the width of the bins in bases, and result.genome_length
is the total length of the genome.
[!NOTE] Note: Only the main primary mapping alignment start is binned, Supplementary and Secondary alignments are ignored.
Directory analysis
To analyse a directory of BAM files, use the iterate_bam_dir
function:
use cnv_from_bam::iterate_bam_dir;
use std::path::PathBuf;
let bam_path = PathBuf::from("path/to/bam_directory/");
// Iterate over the BAM files in teh directory and calculate CNV values for the whole. Number of threads is set to 4 and mapping quality filter is set to 60.
// If number of threads is not specified, it defaults to the number of logical cores on the machine.
let result = iterate_bam_file(bam_path, Some(4), Some(60));
This again returns a CnvResult, but this time the CNV values are summed across all BAM files in the directory. The bin width and genome length are calculated based on the first BAM file in the directory.
[!NOTE] Note: All BAM files in the directory must be aligned to the same reference genome.
Python Integration
cnv_from_bam
can be used in Python using the PyO3 bindings. To install the Python bindings, run:
pip install cnv_from_bam
The same iterate_bam_file
is available in python, accepting a path to a BAM file or a directory of BAM files, the number of threads (set to None
to use the optimal number of threads for the machine), and the mapping quality filter.
Example simple plot in python
```python
from matplotlib import pyplot as plt
import matplotlib as mpl
from pathlib import Path
import numpy as np
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(8, 3))
total = 0
bam_path = Path("path/to/bam/file.bam");
# Iterate over the BAM file and calculate CNV values for each bin. Number of threads is set to 4 and mapping quality filter is set to 60.
# If number of threads is not specified, it defaults to the optimal number of threads for the machine.
result = iterate_bam_file(bam_path, _threads=4, mapq_filter=60);
for contig, cnv in result.cnv.items():
ax.scatter(x=np.arange(len(cnv)) + total, y=cnv, s =0.1)
total += len(cnv)
ax.set_ylim((0,8))
ax.set_xlim((0, total))
Should look something like this. Obviously the cnv data is just a dictionary of lists, so you can do whatever you want with it vis a vis matplotlib, seaborn, etc.
Output
This is new in version >= 0.3. If you just want raw stdout from rust and no faffing with loggers, use v0.2.
Progress Bar
By default, a progress bar is displayed, showing the progress of the iteration of each BAM file. To disable the progress bar, set the CI
environment variable to 1
in your python script:
import os
os.environ["CI"] = "1"
Logging
We use the log
crate for logging. By default, the log level is set to INFO
, which means that the program will output the progress of the iteration of each BAM file. To disable all but warning and error logging, set the log level to WARN
on the iterate_bam_file
function:
import logging
from cnv_from_bam import iterate_bam_file
iterate_bam_file(bam_path, _threads=4, mapq_filter=60, log_level=int(logging.WARN))
getLevelName
is a function from the logging
module that converts the log level to the integer value of the level. These values are
Level | Value |
---|---|
CRITICAL | 50 |
ERROR | 40 |
WARNING | 30 |
INFO | 20 |
DEBUG | 10 |
NOTSET | 0 |
[!NOTE] In v0.3 a regression was introduced, whereby keeping the GIL for logging meant that BAM reading was suddenly single threaded again. Whilst it was possible to fix this and keep
PyO3-log
, I decided to go for truly maximum speed instead. The only drawback to the removal ofPyO3-log
is that log messages will not be handled by python loggers, so they won't be written out by a file handler, for example.
Documentation
To generate the documentation, run:
cargo doc --open
Contributing
Contributions to cnv_from_bam
are welcome!
We use pre-commit hooks (particularly cargo-fmt
and ruff
) to ensure that code is formatted correctly and passes all tests before being committed. To install the pre-commit hooks, run:
git clone https://github.com/Adoni5/cnv_from_bam.git
cd cnv_from_bam
pip install -e .[dev]
pre-commit install -t pre-commit -t post-checkout -t post-merge
pre-commit run --all-files
Changelog
v0.4.1
- Add
exclude_supplementary
parameter toiterate_bam_file
, to exclude supplementary alignments
v0.4.0
- Remove
PyO3-log
for maximum speed. This means that log messages will not be handled by python loggers. Can set log level on call toiterate_bam_file
v0.3.0
- Introduce
PyO3-log
for logging. This means that log messages can be handled by python loggers, so they can be written out by a file handler, for example. - HAS A LARGE PERFORMANCE ISSUE
- Can disable progress bar display by setting
CI
environment variable to1
in python script.
v0.2.0
- Purely rust based BAM parsing, using noodles.
- Uses a much more sensible number for threading if not provided.
- Allows iteration of BAMS in a directory
v0.1.0
- Initial release
- Uses
rust-bio/rust-htslib
for BAM parsing. Has to bind C code, is a faff.
License
This project is licensed under the Mozilla Public License 2.0.
Project details
Release history Release notifications | RSS feed
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
Hashes for cnv_from_bam-0.4.1-pp310-pypy310_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e0ee0dcb2b4e2d1c57ced48d7f17fa7f475be77e67d02becab7b36378bf09764 |
|
MD5 | b3aec062def0e85e62ff0c50c44b379e |
|
BLAKE2b-256 | af13936974165598bb28072228d5076c0efc25b980b92f84f837d4c2c48e60e9 |
Hashes for cnv_from_bam-0.4.1-pp310-pypy310_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b393f887151d7c604a6529cbba2e75f11c44c5140ffa948da163b77aef403be3 |
|
MD5 | 0c84c0b58bc5e0082541a59a65185434 |
|
BLAKE2b-256 | 417e72ee92f515d5612937245a88799df60803bff64a846a069af5944bd1ef62 |
Hashes for cnv_from_bam-0.4.1-pp310-pypy310_pp73-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 31ae80aca208ec707fd52e31a4f657e58cef60b212985ce89e458548d3f608e3 |
|
MD5 | e17ebc9be37ac765b666257dc0b2e0c8 |
|
BLAKE2b-256 | 32c455bfdc9442a606e14e03796a303bfd54a64aeaee6413cc989cfe50550a6c |
Hashes for cnv_from_bam-0.4.1-pp310-pypy310_pp73-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0701fac2d13afe93d5717dfb9a43f29c38626c4a9461ac88bf454be95393c63b |
|
MD5 | b11abedb977542070dd8e4e55b40094a |
|
BLAKE2b-256 | 2f09d42fc3daf18ce2221c7ce18380d46e5fadc010b4787c6efd10a7c8a5bed3 |
Hashes for cnv_from_bam-0.4.1-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f8d9892920ba312081d8d66fc35f07cbebb6a8a3d9dbcede6c39f715e2ecc53f |
|
MD5 | c6b7607d942e9d0f53a60ee7d0ce0531 |
|
BLAKE2b-256 | 107c46e8f84f1125012fb63a7b44f749f546c62a762d06229aae6c7d1739bdc4 |
Hashes for cnv_from_bam-0.4.1-pp39-pypy39_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e38b08a7133cb3d97131e024f6a39b1ecc97054b3a3277deeac6d572e24bba0d |
|
MD5 | ee20cba619296e0e65e680973a2c7400 |
|
BLAKE2b-256 | 9503dbe2cd1559aedb5b618c752ed60c3b3323ab0ea0a80f9f81477381ea48a8 |
Hashes for cnv_from_bam-0.4.1-pp39-pypy39_pp73-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d62b839c8f1a031b82a99541ec1b993789c07e19fa7b847315e980ca1acf9aea |
|
MD5 | 8c21f745c3cba1458d068822bc727c8f |
|
BLAKE2b-256 | f5b68189383cc47c0556ad3b0f3dbc2a1e24cd79f4be99fb96d4a128ffb5d8d7 |
Hashes for cnv_from_bam-0.4.1-pp39-pypy39_pp73-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6e9db153c48a0c7cdc2b1c5ebd61b671159bfc017df3df47e6d5c5154ad5b44f |
|
MD5 | 678f20266f7bbc93fe5f3409801c7f51 |
|
BLAKE2b-256 | 5ad9161ae50db67df035c088b41f995eed685cb83211e72ebbd6e7b59fcfa26b |
Hashes for cnv_from_bam-0.4.1-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1c243f34d0a528e776257d910a090599eb68f459bc8fcd1c57dd548ab4cbda0d |
|
MD5 | adc083da661aac888ae5cf208ed411ed |
|
BLAKE2b-256 | a20807b3fd18ca58d37b8bdaa4a2cc8d0f51006743c5f41481c54f9c17c05112 |
Hashes for cnv_from_bam-0.4.1-pp38-pypy38_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2a03a5e437618174fe5b7cffe4593dcda53daed44a5bb936374caeb16cd1df64 |
|
MD5 | 20d96cca9084f984fe7cefe486eb0f45 |
|
BLAKE2b-256 | df0eebbc5ac3453af0e40b657e97af4a5eb2e2a327af2d6e21f5d099a419bf23 |
Hashes for cnv_from_bam-0.4.1-pp38-pypy38_pp73-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a1d7a23536b0381e5e4b10664e268ce38ee7e0ede285249412875dbab03ae106 |
|
MD5 | 10d86e90283204193168ecfa4c94deb9 |
|
BLAKE2b-256 | 2b2f13d6833ddeff6253ce9510c42d028898b4740bafed4b95b9623d42b6602a |
Hashes for cnv_from_bam-0.4.1-pp38-pypy38_pp73-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 874a18eaac76940e86f11a93719b17fb414970acda6e759b7acaa5c0fcf770c2 |
|
MD5 | 26b26b96c40aff36c099885fff04ff08 |
|
BLAKE2b-256 | 2eda5f224390baefab23f69577545b159e0dbe424ae53bafb8d2030ea945e57f |
Hashes for cnv_from_bam-0.4.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 829ea1b5c94f56208bcf61965dbac9f86f614c208754450f0d9416b96aa9cb5a |
|
MD5 | 1499bf9ecc3a2a5e088a62bd7dee63e9 |
|
BLAKE2b-256 | ac3084e5b0ff4d7ac2f82c66b392529e106c7337f57725509947188e2175904a |
Hashes for cnv_from_bam-0.4.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3f614f2bae9b3f3ef0a3147c2d6b3cb5f56763ec3de4af8dcfbabbb3d0cb6554 |
|
MD5 | 3a315c12b4e16d51e0a7cb120a218956 |
|
BLAKE2b-256 | b536a82a45e260914a5a0e3fa8e462baf004100b9a01ad6b23951a0bc6f3920c |
Hashes for cnv_from_bam-0.4.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a5a868e9e05b3598ef5cc94726aa93a1670f6402bb02dae834a1fbc61fd34bda |
|
MD5 | 82a8f6a96230b65b52fcf90dd97facd2 |
|
BLAKE2b-256 | d07d30f818a2993a53c0f0e0bc159b340a7ebd96ee043f73983f58f1ac1d2476 |
Hashes for cnv_from_bam-0.4.1-cp312-cp312-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d5f70ed385d952aabf3956205118ab46b3f5065469e9cfcedd9d133d022b688b |
|
MD5 | 7f210754502db3280c8a78d9f4ad57fb |
|
BLAKE2b-256 | 6fb63b8de5975813796351d7562684572d8da6dba15738c7a7d369662e70604d |
Hashes for cnv_from_bam-0.4.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 61e53bf466858cf973356a514667d8834ee270604d54b7d32a76060636966886 |
|
MD5 | fa474f4428b6ae4458e0c72132c35250 |
|
BLAKE2b-256 | 4b04c456d1924f2d2b815e8807180bf2603dea5129f4dad02afc64a53fba9743 |
Hashes for cnv_from_bam-0.4.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1cddee34dcdbf8a748a9d0dba78ccc82f70bc2a0233d0c3975efe126b2be9321 |
|
MD5 | ee2fd2e9759eb9a5e76665995f89bf04 |
|
BLAKE2b-256 | 4bf732d352ab9fdd8e97d75528fb4e935f024500e3666f4a5d48c335f86e3c4f |
Hashes for cnv_from_bam-0.4.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3c6375fb57b384f429c186a0ce608734c28c0e135f1846c001f52e7e7920cb33 |
|
MD5 | 14e390546ddc0bc28c3e5fa46d6d77fb |
|
BLAKE2b-256 | 666cf1e96ffe1b112d4304c682060b822c36185ccfce127b804b3c30535d1428 |
Hashes for cnv_from_bam-0.4.1-cp311-cp311-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4b9717b8b147cf6300433148c5afd4aa8501b441a3db9ac62675943d018c3f26 |
|
MD5 | 2f55756d2ff7cd226944b006b4f62dba |
|
BLAKE2b-256 | b118f34c86ad7fa968eb2db1fca19396ad0f6a487b8ef8cd7c0a7856eebc1386 |
Hashes for cnv_from_bam-0.4.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e0bf05f1eae8483cb7d3bde3e1aed7c53bf1b920ac5e5b75c5dc3b071a999aa4 |
|
MD5 | a36d6f17297cff1d7707d12574dc3805 |
|
BLAKE2b-256 | a76e1121f6c6b6ed61f807487c25c9fc8094531d22e55c39e879f727ccb056a7 |
Hashes for cnv_from_bam-0.4.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7bf45649c406718526407b67aacb7f8d0a08dc41a967efe4b5eef8a9e2b3ef07 |
|
MD5 | 6dec902a4c545dcd74b588c22b5beef4 |
|
BLAKE2b-256 | 34c63b7b373569c67e23a62384cafaa6aed4f8073a6ae39ccb81d41f53877c6d |
Hashes for cnv_from_bam-0.4.1-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 86b3a1a84031c7a38ba6413c85ee719a9c2f0d54cdec6fde7050980e2e0bc303 |
|
MD5 | 46c29e56e0ee0b83ae33d1500ff1bdee |
|
BLAKE2b-256 | 28a3b711880251b140604ce2dafbecaf34e43a9c5fc83e258572b6f00691ce21 |
Hashes for cnv_from_bam-0.4.1-cp310-cp310-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0b9b271b3e3bbde6d3c3dfd41cec983ac9234a6cd354954aeae98ff4f9f3b619 |
|
MD5 | 6dc96ff5a6af87dd0f2e5d09b5ce15ad |
|
BLAKE2b-256 | 8d442d7c82f94438388d9219c99e99573b5efdf1a5611bcb0614b11a166f1638 |
Hashes for cnv_from_bam-0.4.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 69373f7afb316b44699cdddef86bed21d93cbac4107236ffe8f460d78dea5cca |
|
MD5 | 19ece3891726eee210a0e087fa6fceab |
|
BLAKE2b-256 | 5a648df8a3a4f25a9b7ee9f5401b2abe367af2c87e19c75725f9c25ef41dc676 |
Hashes for cnv_from_bam-0.4.1-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e6f042b26ba181ded3ec2a88c2b9f24e7d31e982394e7a97986852ca6e38ef3d |
|
MD5 | 01b2676552e3976aa340df643ed6bea4 |
|
BLAKE2b-256 | 6c9c216733f824d93400f5708b04bc7fe134f3732c28457f8f9675397208e5a7 |
Hashes for cnv_from_bam-0.4.1-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7e42d5f1a5d85edd70cd9ec5e7d1ab1b5751ef1b6abd517916a86d566cd5c5c3 |
|
MD5 | 8ddb03a9345fe8ba4af366850888b7f1 |
|
BLAKE2b-256 | ca3db5ff9824ef64e8e66b13966997540334f68c3c77e90612da65e014a3cd10 |
Hashes for cnv_from_bam-0.4.1-cp39-cp39-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fce1fdc19606196ea938e345618fef3a2dd80508a1e42b2c40465a94d0404de2 |
|
MD5 | b163c421d923b3008fa059ca9f27b55e |
|
BLAKE2b-256 | 31123cb2288880e30099776ec214c56b5a51e17d88c5b9e1f76290a5c3e9a03d |
Hashes for cnv_from_bam-0.4.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 947a2bb45b536997d62b623dadba7765a62dafeeb7f98c4aac789b55097bf134 |
|
MD5 | 39317e73009ad6214544343c5ebecaed |
|
BLAKE2b-256 | da5cbece9e7d1b2031069212a2f1969bb9c22da6f654d1fd833c3ac47172b17f |
Hashes for cnv_from_bam-0.4.1-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | eb4416433d8709a702df68a674135c1f70ad0493eecc89cf853e172bd827d197 |
|
MD5 | 0287fed448c7fb147bf212d6987587e3 |
|
BLAKE2b-256 | 15892c4032dddd5b409e756452ba454ae9d72fbc53799ec4f5213f317b130dc8 |
Hashes for cnv_from_bam-0.4.1-cp38-cp38-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6e36df653a7ddfbf81d91a22e58e67f978ed3ecd14cd71ac28a6eaf498cefbd7 |
|
MD5 | 96b81c40c6038a8b50e5b6b936543947 |
|
BLAKE2b-256 | b83f5a882f1ea23126f7f05cfb2ff9120a2ba9ca4966ffcde40393c663cdc393 |
Hashes for cnv_from_bam-0.4.1-cp38-cp38-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | aae44eecc5154d7493aa0c7804f2d13823f613fa687ea7a93075aa67b6989b6b |
|
MD5 | e3d11333964ce8403cc570154c0e1085 |
|
BLAKE2b-256 | 8994926a06b45c31e5b9d7078454ed9462187912d5710233a9572317e914e467 |