| 1 |
Metadata-Version: 2.4 |
| 2 |
Name: ncls |
| 3 |
Version: 0.0.70 |
| 4 |
Summary: A fast interval tree-like implementation in C, wrapped for the Python ecosystem. |
| 5 |
Author-email: Endre Bakken Stovner <[email protected]> |
| 6 |
License: MIT |
| 7 |
Project-URL: Homepage, http://github.com/pyranges/ncls |
| 8 |
Keywords: ncls,interval-tree,genomics |
| 9 |
Classifier: Programming Language :: Python :: 3 |
| 10 |
Classifier: Development Status :: 4 - Beta |
| 11 |
Classifier: Environment :: Other Environment |
| 12 |
Classifier: Intended Audience :: Developers |
| 13 |
Classifier: Intended Audience :: Science/Research |
| 14 |
Classifier: License :: OSI Approved :: MIT License |
| 15 |
Classifier: Operating System :: POSIX :: Linux |
| 16 |
Classifier: Operating System :: MacOS :: MacOS X |
| 17 |
Classifier: Topic :: Scientific/Engineering |
| 18 |
Description-Content-Type: text/markdown |
| 19 |
License-File: LICENSE |
| 20 |
Requires-Dist: numpy |
| 21 |
Provides-Extra: dev |
| 22 |
Requires-Dist: black; extra == "dev" |
| 23 |
Requires-Dist: bumpver; extra == "dev" |
| 24 |
Requires-Dist: isort; extra == "dev" |
| 25 |
Requires-Dist: pip-tools; extra == "dev" |
| 26 |
Requires-Dist: pytest; extra == "dev" |
| 27 |
Dynamic: license-file |
| 28 |
|
| 29 |
# Nested containment list |
| 30 |
|
| 31 |
## Deprecation notice |
| 32 |
|
| 33 |
While I'll continue maintaining this library I suggest you change to [ruranges](https://github.com/pyranges/ruranges/) which is a more lightweight and faster library with many more operations than NCLS. |
| 34 |
|
| 35 |
## NCLS |
| 36 |
|
| 37 |
[](https://travis-ci.org/hunt-genes/ncls) [](https://badge.fury.io/py/ncls) |
| 38 |
|
| 39 |
The Nested Containment List is a datastructure for interval overlap queries, |
| 40 |
like the interval tree. It is usually an order of magnitude faster than the |
| 41 |
interval tree both for building and query lookups. |
| 42 |
|
| 43 |
The implementation here is a revived version of the one used in the now defunct |
| 44 |
PyGr library, which died of bitrot. I have made it less memory-consuming and |
| 45 |
created wrapper functions which allows batch-querying the NCLS for further speed |
| 46 |
gains. |
| 47 |
|
| 48 |
It was implemented to be the cornerstone of the PyRanges project, but I have made |
| 49 |
it available to the Python community as a stand-alone library. Enjoy. |
| 50 |
|
| 51 |
Original Paper: https://academic.oup.com/bioinformatics/article/23/11/1386/199545 |
| 52 |
Cite: http://dx.doi.org/10.1093/bioinformatics/btz615 |
| 53 |
|
| 54 |
## Cite |
| 55 |
|
| 56 |
If you use this library in published research cite |
| 57 |
|
| 58 |
http://dx.doi.org/10.1093/bioinformatics/btz615 |
| 59 |
|
| 60 |
## Install |
| 61 |
|
| 62 |
``` |
| 63 |
pip install ncls |
| 64 |
``` |
| 65 |
|
| 66 |
## Usage |
| 67 |
|
| 68 |
```python |
| 69 |
from ncls import NCLS |
| 70 |
|
| 71 |
import pandas as pd |
| 72 |
|
| 73 |
starts = pd.Series(range(0, 5)) |
| 74 |
ends = starts + 100 |
| 75 |
ids = starts |
| 76 |
|
| 77 |
subject_df = pd.DataFrame({"Start": starts, "End": ends}, index=ids) |
| 78 |
|
| 79 |
print(subject_df) |
| 80 |
# Start End |
| 81 |
# 0 0 100 |
| 82 |
# 1 1 101 |
| 83 |
# 2 2 102 |
| 84 |
# 3 3 103 |
| 85 |
# 4 4 104 |
| 86 |
|
| 87 |
ncls = NCLS(starts.values, ends.values, ids.values) |
| 88 |
|
| 89 |
# python API, slower |
| 90 |
it = ncls.find_overlap(0, 2) |
| 91 |
for i in it: |
| 92 |
print(i) |
| 93 |
# (0, 100, 0) |
| 94 |
# (1, 101, 1) |
| 95 |
|
| 96 |
starts_query = pd.Series([1, 3]) |
| 97 |
ends_query = pd.Series([52, 14]) |
| 98 |
indexes_query = pd.Series([10000, 100]) |
| 99 |
|
| 100 |
query_df = pd.DataFrame({"Start": starts_query.values, "End": ends_query.values}, index=indexes_query.values) |
| 101 |
|
| 102 |
query_df |
| 103 |
# Start End |
| 104 |
# 10000 1 52 |
| 105 |
# 100 3 14 |
| 106 |
|
| 107 |
|
| 108 |
# everything done in C/Cython; faster |
| 109 |
l_idxs, r_idxs = ncls.all_overlaps_both(starts_query.values, ends_query.values, indexes_query.values) |
| 110 |
l_idxs, r_idxs |
| 111 |
# (array([10000, 10000, 10000, 10000, 10000, 100, 100, 100, 100, |
| 112 |
# 100]), array([0, 1, 2, 3, 4, 0, 1, 2, 3, 4])) |
| 113 |
|
| 114 |
print(query_df.loc[l_idxs]) |
| 115 |
# Start End |
| 116 |
# 10000 1 52 |
| 117 |
# 10000 1 52 |
| 118 |
# 10000 1 52 |
| 119 |
# 10000 1 52 |
| 120 |
# 10000 1 52 |
| 121 |
# 100 3 14 |
| 122 |
# 100 3 14 |
| 123 |
# 100 3 14 |
| 124 |
# 100 3 14 |
| 125 |
# 100 3 14 |
| 126 |
print(subject_df.loc[r_idxs]) |
| 127 |
# Start End |
| 128 |
# 0 0 100 |
| 129 |
# 1 1 101 |
| 130 |
# 2 2 102 |
| 131 |
# 3 3 103 |
| 132 |
# 4 4 104 |
| 133 |
# 0 0 100 |
| 134 |
# 1 1 101 |
| 135 |
# 2 2 102 |
| 136 |
# 3 3 103 |
| 137 |
# 4 4 104 |
| 138 |
|
| 139 |
# return intervals in python (slow/mem-consuming) |
| 140 |
intervals = ncls.intervals() |
| 141 |
intervals |
| 142 |
# [(0, 100, 0), (1, 101, 1), (2, 102, 2), (3, 103, 3), (4, 104, 4)] |
| 143 |
``` |
| 144 |
|
| 145 |
There is also an experimental floating point version of the NCLS called FNCLS. |
| 146 |
See the examples folder. |
| 147 |
|
| 148 |
## Benchmark |
| 149 |
|
| 150 |
Test file of 100 million intervals (created by subsetting gencode gtf with replacement): |
| 151 |
|
| 152 |
| Library | Function | Time (s) | Memory (GB) | |
| 153 |
| --- | --- | --- | --- | |
| 154 |
| bx-python | build | 161.7 | 2.5 | |
| 155 |
| ncls | build | 3.15 | 0.5 | |
| 156 |
| bx-python | overlap | 148.4 | 4.3 | |
| 157 |
| ncls | overlap | 7.2 | 0.5 | |
| 158 |
|
| 159 |
Building is 50 times faster and overlap queries are 20 times faster. Memory |
| 160 |
usage is one fifth and one ninth. |
| 161 |
|
| 162 |
## Original paper |
| 163 |
|
| 164 |
> Alexander V. Alekseyenko, Christopher J. Lee; Nested Containment List (NCList): a new algorithm for accelerating interval query of genome alignment and interval databases, Bioinformatics, Volume 23, Issue 11, 1 June 2007, Pages 1386–1393, https://doi.org/10.1093/bioinformatics/btl647 |