superintervals 0.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) [2024] [Kez Cleal]
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,8 @@
1
+ Metadata-Version: 2.1
2
+ Name: superintervals
3
+ Version: 0.2.0
4
+ Summary: Rapid interval intersections
5
+ Author: Kez Cleal
6
+ Author-email: Kez Cleal <clealk@cardiff.ac.uk>
7
+ License-File: LICENSE
8
+ Requires-Dist: Cython
@@ -0,0 +1,279 @@
1
+ SuperIntervals
2
+ ==============
3
+
4
+ A fast, memory-efficient data structure for interval intersection queries.
5
+ SuperIntervals uses a novel superset-index approach that maintains
6
+ intervals in position-sorted order, enabling cache-friendly searches and SIMD-optimized counting.
7
+
8
+ ### Features:
9
+
10
+ - Linear-time index construction from sorted intervals
11
+ - Cache-friendly querying
12
+ - SIMD acceleration (AVX2/Neon) for counting operations
13
+ - Minimal memory overhead (one size_t per interval)
14
+ - Available for C++, Rust, Python, and C
15
+ - Optional Eytzinger memory layout for slightly faster queries (C++/Rust only)
16
+ - No dependencies, header only
17
+
18
+
19
+ ## Quick Start
20
+
21
+ - Intervals are considered end-inclusive
22
+ - The index() function must be called before any queries
23
+ - Found intervals are returned in reverse position-sorted order
24
+
25
+ ### 🐍 Python
26
+
27
+ ```python
28
+ from superintervals import IntervalSet
29
+
30
+ iset = IntervalSet()
31
+ iset.add(10, 20, 'A')
32
+ iset.index()
33
+ overlaps = iset.find_overlaps(8, 20)
34
+ ```
35
+
36
+ ### ⚙️ C++
37
+ ```cpp
38
+ #include "SuperIntervals.hpp"
39
+
40
+ SuperIntervals<int, std::string> intervals;
41
+ intervals.add(1, 5, "A");
42
+ intervals.index();
43
+ std::vector<std::string> results;
44
+ intervals.findOverlaps(4, 9, results);
45
+ ```
46
+
47
+ ### 🦀 Rust
48
+
49
+ ```rust
50
+ use super_intervals::SuperIntervals;
51
+
52
+ let mut intervals = SuperIntervals::new();
53
+ intervals.add(1, 5, "A");
54
+ intervals.index();
55
+ let mut results = Vec::new();
56
+ intervals.find_overlaps(4, 11, &mut results);
57
+ ```
58
+
59
+
60
+ ## Test programs
61
+ Test programs expect plain text BED files and only assess chr1 records - other chromosomes are ignored.
62
+
63
+ C++ program compares SuperIntervals, ImplicitIntervalTree, IntervalTree and NCLS:
64
+ ```
65
+ cd test; make
66
+ ./run-cpp-libs a.bed b.bed
67
+ ```
68
+
69
+ Rust program:
70
+ ```
71
+ RUSTFLAGS="-Ctarget-cpu=native" cargo run --release --example bed-intersect-si
72
+ cargo run --release --example bed-intersect-si a.bed b.bed
73
+ ```
74
+
75
+ ## Benchmark
76
+
77
+ SuperIntervals (SI) was compared with:
78
+ - Coitrees (Rust: https://github.com/dcjones/coitrees)
79
+ - Implicit Interval Tree (C++: https://github.com/lh3/cgranges)
80
+ - Interval Tree (C++: https://github.com/ekg/intervaltree)
81
+ - Nested Containment List (C: https://github.com/pyranges/ncls/tree/master/ncls/src)
82
+
83
+ Main results:
84
+ - Roughly ~2-3x faster than the next best library (Coitrees for Rust, Implicit Interval Tree for C++)
85
+
86
+ ### Datasets:
87
+ 1. Random regions generated using bedtools
88
+ 2. RNA-seq reads and annotations from cgranges repository
89
+ 3. ONT reads from sample PAO33946 (chr1, chrM)
90
+ 4. Paired-end reads from sample DB53, NCBI BioProject PRJNA417592, (chr1, chrM)
91
+ 5. UCSC genes from hg19
92
+
93
+ Test programs use internal timers and print data to stdout, measuring the
94
+ index time, and time to find all intersections. Other steps such as file IO are ignored. Test programs also
95
+ only assess chr1 bed records - other chromosomes are ignored. For 'chrM' records,
96
+ the M was replaced with 1 using sed. Data were assessed in position sorted and random order.
97
+ Datasets can be found on the Releases page, and the `test/run_tools.sh` script has instructions
98
+ for how to repeat the benchmark.
99
+
100
+ Timings were in microseconds using an i9-11900K, 64 GB, 2TB NVMe machine.
101
+
102
+
103
+ ### 1. Finding interval intersections
104
+
105
+ - Coitrees-s uses the `SortedQuerent` version of coitrees
106
+ - SI = superintervals. Eytz refers to the eytzinger layout. `-rs` is the Rust implementation.
107
+
108
+ #### Intervals in sorted order
109
+
110
+ | | Coitrees | Coitrees-s | SI-rs | SI-rs | ImplicitITree-C++ | IntervalTree-C++ | NCLS-C | SI-C++ | SI-Eytz-C++ |
111
+ | --------------------- | -------- | ---------- |-------------|-----------| ----------------- | ---------------- | -------- |---------|-------------|
112
+ | DB53 reads, ONT reads | 1649.6 | 3169 | 732 | **729** | 3802.6 | 46393.8 | 10833.6 | 1391.6 | **1365.6** |
113
+ | DB53 reads, genes | 54.2 | 82.8 | **21** | **21** | 121.6 | 108 | 292.8 | 43 | **40.2** |
114
+ | ONT reads, DB53 reads | 6487.2 | 3437.2 | 534.6 | **533.6** | 18067.4 | 12448 | 31466.2 | 5333.2 | **4545.2** |
115
+ | anno, rna | 49.6 | 33.6 | 17.2 | **17** | 127.2 | 91.2 | 210.6 | 31.2 | **21.2** |
116
+ | genes, DB53 reads | 1171 | 992.8 | 270 | **269.2** | 3141 | 1339.8 | 1768 | 441.8 | **315** |
117
+ | mito-b, mito-a | 35046.2 | 35134 | **13115.2** | 13117.2 | 95137.4 | 108567.8 | 250671.8 | 33703.8 | **33298.6** |
118
+ | rna, anno | 31.8 | 22.6 | **4** | **4** | 71.2 | 54 | 238.8 | 29.4 | **27.2** |
119
+
120
+ #### Intervals in random order
121
+
122
+ | | Coitrees | Coitrees-s | SI-rs | SI-Eytz-rs | ImplicitITree-C++ | IntervalTree-C++ | NCLS-C | SI-C++ | SI-Eytz-C++ |
123
+ | --------------------- | -------- | ---------- |-----------|------------| ----------------- | ---------------- | -------- |------------|-------------|
124
+ | DB53 reads, ONT reads | 2939.6 | 4746.6 | 1323 | **1273** | 6654.6 | 46771.8 | 12082.4 | 2544.4 | **2180.2** |
125
+ | DB53 reads, genes | 75.2 | 131 | 26.6 | **26** | 168.2 | 122.8 | 308.2 | 56.4 | **51.4** |
126
+ | ONT reads, DB53 reads | 17100.6 | 19309.2 | 3815 | **3714.6** | 40490.8 | 28633.2 | 55317.6 | 24047 | **23664** |
127
+ | anno, rna | 89.6 | 110 | 42.2 | **41.8** | 188.8 | 150.2 | 299.4 | **58** | **58** |
128
+ | genes, DB53 reads | 2217.6 | 2448.8 | 1343.8 | **1331.6** | 4495.8 | 2747.2 | 3632.2 | **1265.2** | 1730.8 |
129
+ | mito-b, mito-a | 39002.8 | 88901.8 | **13540** | 13541.8 | 128507.2 | 120712 | 261409.2 | 43682 | **42576.8** |
130
+ | rna, anno | 51 | 69.2 | 12 | **11.8** | 140.4 | 84.4 | 323.8 | 54.2 | **53** |
131
+
132
+ ### 2. Counting interval intersections
133
+
134
+ #### Intervals in sorted order
135
+
136
+ | | Coitrees | SI-rs | SI-Eytz-rs | SI-C++ | SI-Eytz-C++ |
137
+ | --------------------- | -------- |-----------|------------|-----------|-------------|
138
+ | DB53 reads, ONT reads | 551.4 | 337.6 | 338 | **239.4** | 265 |
139
+ | DB53 reads, genes | 26 | 10.6 | 10.8 | 8 | **7** |
140
+ | ONT reads, DB53 reads | 2517.2 | **795.4** | 796.6 | 2234.2 | 1414.2 |
141
+ | anno, rna | 26.8 | 13.4 | 13.2 | 22.6 | **12** |
142
+ | genes, DB53 reads | 737.4 | **292.6** | 294.6 | 459.6 | 338.2 |
143
+ | mito-b, mito-a | 7030 | 6634.6 | 6633.4 | 3065.6 | **2991.8** |
144
+ | rna, anno | 9 | **4** | **4** | 12 | 10 |
145
+
146
+ #### Intervals in random order
147
+
148
+ | | Coitrees | SI-rs | SI-Eytz-rs | SI-C++ | SI-Eytz-C++ |
149
+ | --------------------- | -------- | ------ | ---------- | ------ | ----------- |
150
+ | DB53 reads, ONT reads | 1990 | 937.2 | 883.4 | 1018.8 | **789.6** |
151
+ | DB53 reads, genes | 49.2 | 16 | 15 | 15.2 | **13.4** |
152
+ | ONT reads, DB53 reads | 6835 | 4037.8 | **3964.4** | 8547.8 | 10153.8 |
153
+ | anno, rna | 52 | 39 | **38.6** | 47 | 46 |
154
+ | genes, DB53 reads | 1523.6 | 1261 | 1269 | **1119.4** | 1519.6 |
155
+ | mito-b, mito-a | 15001.2 | 7290.6 | 7298.4 | 4493.6 | **4452.4** |
156
+ | rna, anno | 22 | **12** | **12** | 25.2 | 25.4 |
157
+
158
+ ## Python
159
+
160
+ Install using `pip install .`
161
+
162
+ ```
163
+ from superintervals import IntervalSet
164
+
165
+ iset = IntervalSet()
166
+
167
+ # Add interval start, end, identifier. Integer values are supported
168
+ iset.add(10, 20, 0)
169
+ iset.add(19, 18, 1)
170
+ iset.add(8, 11, 2)
171
+
172
+ # Index method must be called before queries
173
+ iset.index()
174
+
175
+ iset.any_overlaps(8, 20)
176
+ # >>> True
177
+
178
+ iset.count_overlaps(8, 20)
179
+ # >>> 3
180
+
181
+ iset.find_overlaps(8, 20)
182
+ # >>> [1, 0, 2]
183
+
184
+ iset.set_search_interval(8, 20)
185
+ for itv in iset:
186
+ print(itv)
187
+
188
+ # >>> (19, 18, 1)
189
+ # >>> (10, 20, 0)
190
+ # >>> (8, 11, 2)
191
+
192
+ ```
193
+
194
+ ## Cpp
195
+
196
+ ```cpp
197
+ #include <iostream>
198
+ #include <vector>
199
+ #include "SuperIntervals.hpp"
200
+
201
+ int main() {
202
+ // Create a SuperIntervals instance for integer intervals with string data
203
+ // Specify with S, T template types
204
+ SuperIntervals<int, std::string> intervals;
205
+
206
+ // Add some intervals
207
+ intervals.add(1, 5, "Interval A");
208
+ intervals.add(3, 7, "Interval B");
209
+ intervals.add(6, 10, "Interval C");
210
+ intervals.add(8, 12, "Interval D");
211
+
212
+ // Index the intervals (must be called before querying)
213
+ intervals.index();
214
+
215
+ // Find overlaps for the range [4, 9]
216
+ std::vector<std::string> overlaps;
217
+ intervals.findOverlaps(4, 9, overlaps);
218
+
219
+ // Print the overlapping intervals
220
+ for (const auto& interval : overlaps) {
221
+ std::cout << interval << std::endl;
222
+ }
223
+
224
+ // Count the intervals instead
225
+ std::cout << "Count: " << intervals.countOverlaps(4, 9) << std::endl;
226
+
227
+ // Count stabbed intervals at point 7
228
+ std::cout << "Number of intervals containing point 7: " << intervals.countStabbed(7) << std::endl;
229
+
230
+ return 0;
231
+ }
232
+ ```
233
+ There is also a `SuperIntervalsEytz` subclasses that can be used. `SuperIntervalsEytz`
234
+ uses an Eytzinger memory layout that can sometimes offer faster query times at the cost of higher memory
235
+ usage and slower indexing time.
236
+
237
+ ## Rust
238
+
239
+ ```
240
+ use super_intervals::SuperIntervals;
241
+
242
+ fn main() {
243
+ // Create a new instance of SuperIntervals
244
+ let mut intervals = SuperIntervals::new();
245
+
246
+ // Add some intervals with associated data of type T
247
+ intervals.add(1, 5, "Interval A");
248
+ intervals.add(10, 15, "Interval B");
249
+ intervals.add(7, 12, "Interval C");
250
+
251
+ // Call index() to prepare the intervals for queries
252
+ intervals.index();
253
+
254
+ // Query for overlapping intervals with a range (4, 11)
255
+ let mut found_intervals = Vec::new();
256
+ intervals.find_overlaps(4, 11, &mut found_intervals);
257
+
258
+ // Display found intervals
259
+ for interval in found_intervals {
260
+ println!("Found overlapping interval: {}", interval);
261
+ }
262
+
263
+ // Count overlaps with a range (4, 11)
264
+ let overlap_count = intervals.count_overlaps(4, 11);
265
+ println!("Number of overlapping intervals: {}", overlap_count);
266
+ }
267
+ ```
268
+ There is also `SuperIntervalsEytz` implementation. `SuperIntervalsEytz`
269
+ uses an Eytzinger memory layout that can sometimes offer faster query times at the cost of higher memory
270
+ usage and slower indexing time.
271
+
272
+ ## Acknowledgements
273
+
274
+ - The rust test program borrows heavily from the coitrees package
275
+ - The superset-index implemented here exploits a similar interval ordering as described in
276
+ Schmidt 2009 "Interval Stabbing Problems in Small Integer Ranges". However, the superset-index has several advantages including
277
+ 1. An implicit memory layout
278
+ 1. General purpose implementation (not just small integer ranges)
279
+ 1. SIMD counting algorithm
@@ -0,0 +1,14 @@
1
+ [build-system]
2
+ requires = [
3
+ "setuptools>=42",
4
+ "wheel",
5
+ "Cython"
6
+ ]
7
+ build-backend = "setuptools.build_meta"
8
+
9
+ [project]
10
+ name = "superintervals"
11
+ version = "0.2.0"
12
+ description = "Rapid interval intersections"
13
+ dependencies = ['Cython']
14
+ authors = [{name = "Kez Cleal", email = "clealk@cardiff.ac.uk"}]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,23 @@
1
+ from setuptools import setup, find_packages, Extension
2
+ from Cython.Build import cythonize
3
+
4
+ ext_modules = [
5
+ Extension("superintervals.intervalset",
6
+ ["src/superintervals/intervalset.pyx"],
7
+ include_dirs=["src"],
8
+ language="c++",
9
+ extra_compile_args=["-std=c++17", "-march=native"])
10
+ ]
11
+
12
+ print('PAKCAGES', find_packages(where='src')) # Add this line for debugging
13
+
14
+ setup(
15
+ name='superintervals',
16
+ description="Rapid interval intersections",
17
+ author="Kez Cleal",
18
+ author_email="clealk@cardiff.ac.uk",
19
+ packages=find_packages(where='src'),
20
+ package_dir={"": "src"},
21
+ install_requires=['Cython'],
22
+ ext_modules=cythonize(ext_modules),
23
+ )
@@ -0,0 +1,3 @@
1
+ from .intervalset import (
2
+ IntervalSet
3
+ )