| 1 |
# import numpy as cnp |
| 2 |
import sys |
| 3 |
|
| 4 |
cimport cython |
| 5 |
from libc.stdint cimport int32_t, int64_t |
| 6 |
from libc.stdlib cimport malloc |
| 7 |
|
| 8 |
cimport ncls.src.cncls32 as cn |
| 9 |
|
| 10 |
# from cython.stdint import int32 |
| 11 |
|
| 12 |
import numpy as np |
| 13 |
|
| 14 |
|
| 15 |
cdef inline int int_max(int a, int b): return a if a >= b else b |
| 16 |
cdef inline int int_min(int a, int b): return a if a <= b else b |
| 17 |
# cdef inline int overlap(int a_start, int a_end, int b_start, b_end): int_max(0, int_min(a_end, b_end) - int_max(a_start, b_start)) |
| 18 |
# import ctypes as c |
| 19 |
|
| 20 |
try: |
| 21 |
dummy = profile |
| 22 |
except: |
| 23 |
profile = lambda x: x |
| 24 |
|
| 25 |
cdef class NCLS32: |
| 26 |
|
| 27 |
cdef cn.SublistHeader *subheader |
| 28 |
cdef cn.IntervalMap *im |
| 29 |
cdef int n, ntop |
| 30 |
cdef int nlists |
| 31 |
|
| 32 |
# build NCLS from array of starts, ends, values |
| 33 |
@cython.boundscheck(False) |
| 34 |
@cython.wraparound(False) |
| 35 |
@cython.initializedcheck(False) |
| 36 |
def __cinit__(self, const int32_t [::1] starts=None, const int32_t [::1] ends=None, const int64_t [::1] ids=None): |
| 37 |
|
| 38 |
if None in (starts, ends, ids): |
| 39 |
return |
| 40 |
|
| 41 |
if len(starts) == 0 or len(ends) == 0 or len(ids) == 0: |
| 42 |
return |
| 43 |
|
| 44 |
cdef int i |
| 45 |
cdef length = len(starts) |
| 46 |
self.close() # DUMP OUR EXISTING MEMORY |
| 47 |
self.n = len(starts) |
| 48 |
self.im = cn.interval_map_alloc(self.n) |
| 49 |
if self.im == NULL: |
| 50 |
raise MemoryError('unable to allocate IntervalMap[%d]' % self.n) |
| 51 |
i = 0 |
| 52 |
for i in range(length): |
| 53 |
self.im[i].start = starts[i] |
| 54 |
self.im[i].end = ends[i] |
| 55 |
self.im[i].target_id = ids[i] |
| 56 |
self.im[i].sublist = -1 |
| 57 |
|
| 58 |
self.subheader = cn.build_nested_list(self.im, self.n, &(self.ntop), &(self.nlists)) |
| 59 |
|
| 60 |
def intervals(self, take=None): |
| 61 |
intervals = [] |
| 62 |
|
| 63 |
cdef int i = 0 |
| 64 |
cdef int _take = int(take) if not take is None else len(self) |
| 65 |
if not self.im: # if empty |
| 66 |
return [] |
| 67 |
|
| 68 |
for i in range(_take): |
| 69 |
intervals.append((self.im[i].start, self.im[i].end, self.im[i].target_id)) |
| 70 |
|
| 71 |
return intervals |
| 72 |
|
| 73 |
@cython.boundscheck(False) |
| 74 |
@cython.wraparound(False) |
| 75 |
@cython.initializedcheck(False) |
| 76 |
cpdef all_overlaps_both(self, |
| 77 |
const int32_t [::1] starts, |
| 78 |
const int32_t [::1] ends, |
| 79 |
const int64_t [::1] indexes): |
| 80 |
|
| 81 |
cdef int i = 0 |
| 82 |
cdef int nhit = 0 |
| 83 |
cdef int length = len(starts) |
| 84 |
cdef int loop_counter = 0 |
| 85 |
cdef int nfound = 0 |
| 86 |
cdef int spent = 0 |
| 87 |
|
| 88 |
output_arr = np.zeros(length, dtype=np.int_) |
| 89 |
output_arr_other = np.zeros(length, dtype=np.int_) |
| 90 |
cdef long [::1] output |
| 91 |
cdef long [::1] output_other |
| 92 |
|
| 93 |
output = output_arr |
| 94 |
output_other = output_arr_other |
| 95 |
|
| 96 |
cdef cn.IntervalIterator *it |
| 97 |
cdef cn.IntervalIterator *it_alloc |
| 98 |
|
| 99 |
cdef cn.IntervalMap im_buf[1024] |
| 100 |
if not self.im: # if empty |
| 101 |
return [], [] |
| 102 |
|
| 103 |
it_alloc = cn.interval_iterator_alloc() |
| 104 |
it = it_alloc |
| 105 |
for loop_counter in range(length): |
| 106 |
|
| 107 |
spent = 0 |
| 108 |
# remember first pointer for dealloc |
| 109 |
while not spent: |
| 110 |
i = 0 |
| 111 |
cn.find_intervals(it, starts[loop_counter], ends[loop_counter], self.im, self.ntop, |
| 112 |
self.subheader, self.nlists, im_buf, 1024, |
| 113 |
&(nhit), &(it)) # GET NEXT BUFFER CHUNK |
| 114 |
|
| 115 |
# print("nhit", nhit) |
| 116 |
# print("length", length) |
| 117 |
# print("nfound", nfound) |
| 118 |
# print(nfound + nhit >= length) |
| 119 |
if nfound + nhit >= length: |
| 120 |
|
| 121 |
length = (length + nhit) * 2 |
| 122 |
output_arr = np.resize(output_arr, length) |
| 123 |
output_arr_other = np.resize(output_arr_other, length) |
| 124 |
output = output_arr |
| 125 |
output_other = output_arr_other |
| 126 |
|
| 127 |
while i < nhit: |
| 128 |
|
| 129 |
# print("length", length) |
| 130 |
# print("nfound", nfound) |
| 131 |
# print("loop_counter", loop_counter) |
| 132 |
output[nfound] = indexes[loop_counter] |
| 133 |
output_other[nfound] = im_buf[i].target_id |
| 134 |
|
| 135 |
nfound += 1 |
| 136 |
i += 1 |
| 137 |
|
| 138 |
if not nhit == 1024: |
| 139 |
spent = 1 |
| 140 |
|
| 141 |
|
| 142 |
cn.reset_interval_iterator(it_alloc) |
| 143 |
it = it_alloc |
| 144 |
|
| 145 |
cn.free_interval_iterator(it_alloc) |
| 146 |
|
| 147 |
return output_arr[:nfound], output_arr_other[:nfound] |
| 148 |
|
| 149 |
@cython.boundscheck(False) |
| 150 |
@cython.wraparound(False) |
| 151 |
@cython.initializedcheck(False) |
| 152 |
cpdef last_overlap_both(self, const int32_t [::1] starts, const int32_t [::1] ends, const long [::1] indexes): |
| 153 |
|
| 154 |
cdef int i = 0 |
| 155 |
cdef int nhit = 0 |
| 156 |
cdef int length = len(starts) |
| 157 |
cdef int loop_counter = 0 |
| 158 |
cdef int nfound = 0 |
| 159 |
cdef int max_end = -1 |
| 160 |
cdef int spent = 0 |
| 161 |
|
| 162 |
output_arr = np.zeros(length, dtype=np.int_) |
| 163 |
output_arr_other = np.zeros(length, dtype=np.int_) |
| 164 |
cdef long [::1] output |
| 165 |
cdef long [::1] output_other |
| 166 |
|
| 167 |
output = output_arr |
| 168 |
output_other = output_arr_other |
| 169 |
|
| 170 |
cdef cn.IntervalIterator *it |
| 171 |
cdef cn.IntervalIterator *it_alloc |
| 172 |
|
| 173 |
cdef cn.IntervalMap im_buf[1024] |
| 174 |
if not self.im: # if empty |
| 175 |
return [], [] |
| 176 |
|
| 177 |
it_alloc = cn.interval_iterator_alloc() |
| 178 |
it = it_alloc |
| 179 |
for loop_counter in range(length): |
| 180 |
|
| 181 |
# print("loop_counter", loop_counter) |
| 182 |
# remember first pointer for dealloc |
| 183 |
spent = 0 |
| 184 |
max_end = -1 |
| 185 |
while not spent: |
| 186 |
i = 0 |
| 187 |
cn.find_intervals(it, starts[loop_counter], ends[loop_counter], self.im, self.ntop, |
| 188 |
self.subheader, self.nlists, im_buf, 1024, |
| 189 |
&(nhit), &(it)) # GET NEXT BUFFER CHUNK |
| 190 |
|
| 191 |
|
| 192 |
# """Finding last overlap in NCLS: iterate from start, find last maximal end.""" |
| 193 |
|
| 194 |
if nhit: |
| 195 |
while i < nhit: |
| 196 |
if im_buf[i].end >= max_end: |
| 197 |
# print("max_end", im_buf[i].end) |
| 198 |
output[nfound] = indexes[loop_counter] |
| 199 |
output_other[nfound] = im_buf[i].target_id |
| 200 |
max_end = im_buf[i].end |
| 201 |
|
| 202 |
i += 1 |
| 203 |
|
| 204 |
nfound += 1 |
| 205 |
|
| 206 |
if nhit < 1024: |
| 207 |
spent = 1 |
| 208 |
|
| 209 |
cn.reset_interval_iterator(it_alloc) |
| 210 |
it = it_alloc |
| 211 |
|
| 212 |
cn.free_interval_iterator(it_alloc) |
| 213 |
|
| 214 |
return output_arr[:nfound], output_arr_other[:nfound] |
| 215 |
|
| 216 |
|
| 217 |
@cython.boundscheck(False) |
| 218 |
@cython.wraparound(False) |
| 219 |
@cython.initializedcheck(False) |
| 220 |
cpdef k_overlaps_both(self, const int32_t [::1] starts, const int32_t [::1] ends, const int64_t [::1] indexes, int k): |
| 221 |
|
| 222 |
cdef int i = 0 |
| 223 |
cdef int nhit = 0 |
| 224 |
cdef int length = len(starts) |
| 225 |
cdef int loop_counter = 0 |
| 226 |
cdef int nfound = 0 |
| 227 |
cdef int spent = 0 |
| 228 |
|
| 229 |
output_arr = np.zeros(length, dtype=np.int_) |
| 230 |
output_arr_other = np.zeros(length, dtype=np.int_) |
| 231 |
cdef long [::1] output |
| 232 |
cdef long [::1] output_other |
| 233 |
|
| 234 |
output = output_arr |
| 235 |
output_other = output_arr_other |
| 236 |
|
| 237 |
cdef cn.IntervalIterator *it |
| 238 |
cdef cn.IntervalIterator *it_alloc |
| 239 |
|
| 240 |
cdef cn.IntervalMap im_buf[1024] |
| 241 |
if not self.im: # if empty |
| 242 |
return [], [] |
| 243 |
|
| 244 |
it_alloc = cn.interval_iterator_alloc() |
| 245 |
it = it_alloc |
| 246 |
for loop_counter in range(length): |
| 247 |
|
| 248 |
# remember first pointer for dealloc |
| 249 |
spent = 0 |
| 250 |
while not spent: |
| 251 |
i = 0 |
| 252 |
cn.find_intervals(it, starts[loop_counter], ends[loop_counter], self.im, self.ntop, |
| 253 |
self.subheader, self.nlists, im_buf, 1024, |
| 254 |
&(nhit), &(it)) # GET NEXT BUFFER CHUNK |
| 255 |
|
| 256 |
# print("nhit", nhit) |
| 257 |
# print("length", length) |
| 258 |
# print("nfound", nfound) |
| 259 |
# print(nfound + nhit >= length) |
| 260 |
if nfound + nhit >= length: |
| 261 |
|
| 262 |
length = (length + nhit) * 2 |
| 263 |
output_arr = np.resize(output_arr, length) |
| 264 |
output_arr_other = np.resize(output_arr_other, length) |
| 265 |
output = output_arr |
| 266 |
output_other = output_arr_other |
| 267 |
|
| 268 |
if k < nhit: |
| 269 |
nhit = k |
| 270 |
|
| 271 |
while i < nhit: |
| 272 |
|
| 273 |
# print("length", length) |
| 274 |
# print("nfound", nfound) |
| 275 |
# print("loop_counter", loop_counter) |
| 276 |
output[nfound] = indexes[loop_counter] |
| 277 |
output_other[nfound] = im_buf[i].target_id |
| 278 |
|
| 279 |
nfound += 1 |
| 280 |
i += 1 |
| 281 |
|
| 282 |
if nhit < 1024: |
| 283 |
spent = 1 |
| 284 |
|
| 285 |
cn.reset_interval_iterator(it_alloc) |
| 286 |
it = it_alloc |
| 287 |
|
| 288 |
cn.free_interval_iterator(it_alloc) |
| 289 |
|
| 290 |
return output_arr[:nfound], output_arr_other[:nfound] |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
@cython.boundscheck(False) |
| 295 |
@cython.wraparound(False) |
| 296 |
@cython.initializedcheck(False) |
| 297 |
cpdef all_overlaps_self(self, const int32_t [::1] starts, const int32_t [::1] ends, const int64_t [::1] indexes): |
| 298 |
|
| 299 |
cdef int i |
| 300 |
cdef int nhit = 0 |
| 301 |
cdef int length = len(starts) |
| 302 |
cdef int loop_counter = 0 |
| 303 |
cdef int nfound = 0 |
| 304 |
cdef int spent = 0 |
| 305 |
|
| 306 |
output_arr = np.zeros(length, dtype=np.int_) |
| 307 |
cdef long [::1] output |
| 308 |
|
| 309 |
output = output_arr |
| 310 |
|
| 311 |
cdef cn.IntervalIterator *it |
| 312 |
cdef cn.IntervalIterator *it_alloc |
| 313 |
|
| 314 |
cdef cn.IntervalMap im_buf[1024] |
| 315 |
if not self.im: # if empty |
| 316 |
return [], [] |
| 317 |
|
| 318 |
it_alloc = cn.interval_iterator_alloc() |
| 319 |
it = it_alloc |
| 320 |
for loop_counter in range(length): |
| 321 |
|
| 322 |
# remember first pointer for dealloc |
| 323 |
|
| 324 |
spent = 0 |
| 325 |
while not spent: |
| 326 |
i = 0 |
| 327 |
cn.find_intervals(it, starts[loop_counter], ends[loop_counter], self.im, self.ntop, |
| 328 |
self.subheader, self.nlists, im_buf, 1024, |
| 329 |
&(nhit), &(it)) # GET NEXT BUFFER CHUNK |
| 330 |
|
| 331 |
if nfound + nhit >= length: |
| 332 |
|
| 333 |
length = (length + nhit) * 2 |
| 334 |
output_arr = np.resize(output_arr, length) |
| 335 |
output = output_arr |
| 336 |
|
| 337 |
for i in range(nhit): |
| 338 |
|
| 339 |
output[nfound] = indexes[loop_counter] |
| 340 |
|
| 341 |
nfound += 1 |
| 342 |
|
| 343 |
if nhit < 1024: |
| 344 |
spent = 1 |
| 345 |
|
| 346 |
cn.reset_interval_iterator(it_alloc) |
| 347 |
it = it_alloc |
| 348 |
|
| 349 |
cn.free_interval_iterator(it_alloc) |
| 350 |
|
| 351 |
return output_arr[:nfound] |
| 352 |
|
| 353 |
|
| 354 |
@cython.boundscheck(False) |
| 355 |
@cython.wraparound(False) |
| 356 |
@cython.initializedcheck(False) |
| 357 |
cpdef coverage(self, const int32_t [::1] starts, const int32_t [::1] ends, const int64_t [::1] indexes): |
| 358 |
|
| 359 |
# assumes the ncls to not contain any overlapping intervals |
| 360 |
|
| 361 |
cdef int i = 0 |
| 362 |
cdef int start = 0 |
| 363 |
cdef int end = 0 |
| 364 |
cdef int other_start = 0 |
| 365 |
cdef int other_end = 0 |
| 366 |
cdef int nhit = 0 |
| 367 |
cdef int length = len(starts) |
| 368 |
cdef int loop_counter = 0 |
| 369 |
cdef int nfound = 0 |
| 370 |
cdef int spent = 0 |
| 371 |
|
| 372 |
# output_arr = np.zeros(length, dtype=np.int_) |
| 373 |
output_arr_length = np.zeros(length, dtype=np.int_) |
| 374 |
# cdef long [::1] output |
| 375 |
cdef long [::1] output_length |
| 376 |
|
| 377 |
# output = output_arr |
| 378 |
output_length = output_arr_length |
| 379 |
|
| 380 |
cdef cn.IntervalIterator *it |
| 381 |
cdef cn.IntervalIterator *it_alloc |
| 382 |
|
| 383 |
cdef cn.IntervalMap im_buf[1024] |
| 384 |
if not self.im: # if empty |
| 385 |
return [], [] |
| 386 |
|
| 387 |
it_alloc = cn.interval_iterator_alloc() |
| 388 |
it = it_alloc |
| 389 |
for loop_counter in range(length): |
| 390 |
|
| 391 |
start = starts[loop_counter] |
| 392 |
end = ends[loop_counter] |
| 393 |
# remember first pointer for dealloc |
| 394 |
spent = 0 |
| 395 |
while not spent: |
| 396 |
i = 0 |
| 397 |
cn.find_intervals(it, starts[loop_counter], ends[loop_counter], self.im, self.ntop, |
| 398 |
self.subheader, self.nlists, im_buf, 1024, |
| 399 |
&(nhit), &(it)) # GET NEXT BUFFER CHUNK |
| 400 |
|
| 401 |
while i < nhit: |
| 402 |
output_length[loop_counter] += int_min(im_buf[i].end, end) - int_max(im_buf[i].start, start) |
| 403 |
i += 1 |
| 404 |
|
| 405 |
if nhit < 1024: |
| 406 |
spent = 1 |
| 407 |
|
| 408 |
cn.reset_interval_iterator(it_alloc) |
| 409 |
it = it_alloc |
| 410 |
|
| 411 |
cn.free_interval_iterator(it_alloc) |
| 412 |
|
| 413 |
return output_arr_length |
| 414 |
|
| 415 |
def __len__(self): |
| 416 |
return self.n |
| 417 |
|
| 418 |
def __str__(self): |
| 419 |
|
| 420 |
contents = ["Number intervals:", self.n, "Number of intervals in main list:", self.ntop, "Number of intervals with subintervals:", self.nlists, "Percentage in top-level interval", self.ntop/float(self.n)] |
| 421 |
return "NCLS32\n------\n" + "\n".join(str(c) for c in contents) |
| 422 |
|
| 423 |
def __dealloc__(self): |
| 424 |
'remember: dealloc cannot call other methods!' |
| 425 |
if self.subheader: |
| 426 |
cn.free(self.subheader) |
| 427 |
if self.im: |
| 428 |
cn.free(self.im) |
| 429 |
|
| 430 |
def close(self): |
| 431 |
if self.subheader: |
| 432 |
cn.free(self.subheader) |
| 433 |
if self.im: |
| 434 |
cn.free(self.im) |
| 435 |
self.subheader = NULL |
| 436 |
self.im = NULL |
| 437 |
|
| 438 |
return None |
| 439 |
|
| 440 |
def find_overlap(self, int32_t start, int32_t end): |
| 441 |
if not self.im: # RAISE EXCEPTION IF NO DATA |
| 442 |
return [] |
| 443 |
|
| 444 |
return NCLSIterator(start, end, self) |
| 445 |
|
| 446 |
|
| 447 |
cpdef has_overlap(self, int start, int end): |
| 448 |
cdef int nhit = 0 |
| 449 |
|
| 450 |
cdef cn.IntervalIterator *it |
| 451 |
cdef cn.IntervalMap im_buf[1024] |
| 452 |
if not self.im: # if empty |
| 453 |
return 0 |
| 454 |
|
| 455 |
it = cn.interval_iterator_alloc() |
| 456 |
|
| 457 |
while it: |
| 458 |
cn.find_intervals(it, start, end, self.im, self.ntop, |
| 459 |
self.subheader, self.nlists, im_buf, 1024, |
| 460 |
&(nhit), &(it)) # GET NEXT BUFFER CHUNK |
| 461 |
|
| 462 |
if nhit > 0: |
| 463 |
cn.free_interval_iterator(it) |
| 464 |
return True |
| 465 |
|
| 466 |
cn.free_interval_iterator(it) |
| 467 |
|
| 468 |
return False |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
@cython.boundscheck(False) |
| 473 |
@cython.wraparound(False) |
| 474 |
@cython.initializedcheck(False) |
| 475 |
cpdef set_difference_helper(self, const int32_t [::1] starts, const int32_t [::1] ends, const int64_t [::1] indexes, |
| 476 |
const int64_t[::1] nhits): |
| 477 |
|
| 478 |
cdef int i = 0 |
| 479 |
cdef int nhit = 0 |
| 480 |
cdef int nfound = 0 |
| 481 |
cdef int32_t nstart = 0 |
| 482 |
cdef int32_t nend = 0 |
| 483 |
cdef int length = len(starts) |
| 484 |
cdef int overlap_type_nb = 0 |
| 485 |
cdef int na = -1 |
| 486 |
cdef int spent = 0 |
| 487 |
cdef int max_i = 0 |
| 488 |
|
| 489 |
output_arr = np.zeros(length, dtype=np.int64) |
| 490 |
output_arr_start = np.zeros(length, dtype=np.int32) |
| 491 |
output_arr_end = np.zeros(length, dtype=np.int32) |
| 492 |
cdef int64_t [::1] output |
| 493 |
cdef int32_t [::1] output_start |
| 494 |
cdef int32_t [::1] output_end |
| 495 |
|
| 496 |
output = output_arr |
| 497 |
output_start = output_arr_start |
| 498 |
output_end = output_arr_end |
| 499 |
|
| 500 |
cdef cn.IntervalIterator *it |
| 501 |
cdef cn.IntervalIterator *it_alloc |
| 502 |
cdef cn.IntervalMap im_buf[1024] |
| 503 |
if not self.im: # if empty |
| 504 |
return [], [], [] |
| 505 |
|
| 506 |
it_alloc = cn.interval_iterator_alloc() |
| 507 |
it = it_alloc |
| 508 |
|
| 509 |
for loop_counter in range(length): |
| 510 |
|
| 511 |
nhit = nhits[loop_counter] |
| 512 |
nstart = starts[loop_counter] |
| 513 |
nend = ends[loop_counter] |
| 514 |
|
| 515 |
while nhit > 0: |
| 516 |
i = 0 |
| 517 |
cn.find_intervals(it, starts[loop_counter], ends[loop_counter], self.im, self.ntop, |
| 518 |
self.subheader, self.nlists, im_buf, 1024, |
| 519 |
&(na), &(it)) # GET NEXT BUFFER CHUNK |
| 520 |
|
| 521 |
if nfound + nhit >= length: |
| 522 |
length = (length + nhit) * 2 |
| 523 |
output_arr = np.resize(output_arr, length) |
| 524 |
output_arr_start = np.resize(output_arr_start, length) |
| 525 |
output = output_arr |
| 526 |
output_start = output_arr_start |
| 527 |
output_arr_end = np.resize(output_arr_end, length) |
| 528 |
output_end = output_arr_end |
| 529 |
|
| 530 |
# B covers whole of A; ignore |
| 531 |
if nhit == 1 and starts[loop_counter] > im_buf[i].start and ends[loop_counter] < im_buf[i].end: |
| 532 |
# print("ignore me!") |
| 533 |
output_start[nfound] = -1 |
| 534 |
output_end[nfound] = -1 |
| 535 |
output[nfound] = indexes[loop_counter] |
| 536 |
i = nhit |
| 537 |
nfound += 1 |
| 538 |
break |
| 539 |
|
| 540 |
max_i = 1024 if nhit > 1024 else nhit |
| 541 |
|
| 542 |
while i < max_i: |
| 543 |
# in case the start contributes nothing |
| 544 |
if nstart < im_buf[i].start: |
| 545 |
output[nfound] = indexes[loop_counter] |
| 546 |
output_start[nfound] = nstart |
| 547 |
output_end[nfound] = im_buf[i].start |
| 548 |
nfound += 1 |
| 549 |
nstart = im_buf[i].end |
| 550 |
|
| 551 |
i += 1 |
| 552 |
|
| 553 |
nhit = nhit - 1024 |
| 554 |
|
| 555 |
if nhit <= 0: |
| 556 |
i = i - 1 |
| 557 |
|
| 558 |
if im_buf[i].start <= nstart and im_buf[i].end >= ends[loop_counter]: |
| 559 |
# print("im_buf[i].start <= nstart and im_buf[i].end >= ends[loop_counter]") |
| 560 |
#print("we are here " * 10) |
| 561 |
|
| 562 |
output_start[nfound] = -1 |
| 563 |
output_end[nfound] = -1 |
| 564 |
output[nfound] = <long> indexes[loop_counter] |
| 565 |
nfound += 1 |
| 566 |
else: |
| 567 |
if im_buf[i].start > nstart: |
| 568 |
output[nfound] = indexes[loop_counter] |
| 569 |
output_start[nfound] = nstart |
| 570 |
output_end[nfound] = im_buf[i].start |
| 571 |
nfound += 1 |
| 572 |
|
| 573 |
if im_buf[i].end < ends[loop_counter]: |
| 574 |
output[nfound] = indexes[loop_counter] |
| 575 |
output_start[nfound] = im_buf[i].end |
| 576 |
output_end[nfound] = ends[loop_counter] |
| 577 |
nfound += 1 |
| 578 |
|
| 579 |
cn.reset_interval_iterator(it_alloc) |
| 580 |
it = it_alloc |
| 581 |
|
| 582 |
cn.free_interval_iterator(it_alloc) |
| 583 |
|
| 584 |
return output_arr[:nfound], output_arr_start[:nfound], output_arr_end[:nfound] |
| 585 |
|
| 586 |
|
| 587 |
# this one is actually slower!!!! |
| 588 |
@cython.boundscheck(False) |
| 589 |
@cython.wraparound(False) |
| 590 |
@cython.initializedcheck(False) |
| 591 |
cpdef first_overlap_both(self, const int32_t [::1] starts, const int32_t [::1] ends, const int64_t [::1] indexes): |
| 592 |
|
| 593 |
cdef int ix = 0 |
| 594 |
cdef int length = len(starts) |
| 595 |
cdef int loop_counter = 0 |
| 596 |
cdef int nfound = 0 |
| 597 |
|
| 598 |
output_arr = np.zeros(length, dtype=np.int_) |
| 599 |
output_arr_other = np.zeros(length, dtype=np.int_) |
| 600 |
cdef long [::1] output |
| 601 |
cdef long [::1] output_other |
| 602 |
|
| 603 |
output = output_arr |
| 604 |
output_other = output_arr_other |
| 605 |
|
| 606 |
cdef cn.IntervalIterator *it |
| 607 |
cdef cn.IntervalIterator *it_alloc |
| 608 |
|
| 609 |
cdef cn.IntervalMap im_buf[1024] |
| 610 |
if not self.im: # if empty |
| 611 |
return [], [] |
| 612 |
|
| 613 |
|
| 614 |
for loop_counter in range(length): |
| 615 |
|
| 616 |
# remember first pointer for dealloc |
| 617 |
ix = cn.find_overlap_start(starts[loop_counter], ends[loop_counter], self.im, self.ntop) |
| 618 |
|
| 619 |
if ix != -1: |
| 620 |
output[nfound] = indexes[loop_counter] |
| 621 |
output_other[nfound] = self.im[ix].target_id |
| 622 |
|
| 623 |
nfound += 1 |
| 624 |
|
| 625 |
return output_arr[:nfound], output_arr_other[:nfound] |
| 626 |
|
| 627 |
|
| 628 |
@cython.boundscheck(False) |
| 629 |
@cython.wraparound(False) |
| 630 |
@cython.initializedcheck(False) |
| 631 |
cpdef all_containments_both(self, const int32_t [::1] starts, const int32_t [::1] ends, const int64_t [::1] indexes): |
| 632 |
|
| 633 |
cdef int i |
| 634 |
cdef int nhit = 0 |
| 635 |
cdef int length = len(starts) |
| 636 |
cdef int loop_counter = 0 |
| 637 |
cdef int nfound = 0 |
| 638 |
cdef int start, end |
| 639 |
cdef int spent = 0 |
| 640 |
|
| 641 |
output_arr = np.zeros(length, dtype=np.int_) |
| 642 |
output_arr_other = np.zeros(length, dtype=np.int_) |
| 643 |
cdef long [::1] output |
| 644 |
cdef long [::1] output_other |
| 645 |
|
| 646 |
output = output_arr |
| 647 |
output_other = output_arr_other |
| 648 |
|
| 649 |
cdef cn.IntervalIterator *it |
| 650 |
cdef cn.IntervalIterator *it_alloc |
| 651 |
|
| 652 |
|
| 653 |
cdef cn.IntervalMap im_buf[1024] |
| 654 |
if not self.im: # if empty |
| 655 |
return [], [] |
| 656 |
|
| 657 |
|
| 658 |
it_alloc = cn.interval_iterator_alloc() |
| 659 |
it = it_alloc |
| 660 |
|
| 661 |
for loop_counter in range(length): |
| 662 |
|
| 663 |
start = starts[loop_counter] |
| 664 |
end = ends[loop_counter] |
| 665 |
spent = 0 |
| 666 |
while not spent: |
| 667 |
i = 0 |
| 668 |
cn.find_intervals(it, start, end, self.im, self.ntop, |
| 669 |
self.subheader, self.nlists, im_buf, 1024, |
| 670 |
&(nhit), &(it)) # GET NEXT BUFFER CHUNK |
| 671 |
|
| 672 |
while i < nhit: |
| 673 |
|
| 674 |
if im_buf[i].start <= start and im_buf[i].end >= end: |
| 675 |
|
| 676 |
if nfound >= length: |
| 677 |
|
| 678 |
length = nfound * 2 |
| 679 |
output_arr = np.resize(output_arr, length) |
| 680 |
output_arr_other = np.resize(output_arr_other, length) |
| 681 |
output = output_arr |
| 682 |
output_other = output_arr_other |
| 683 |
|
| 684 |
output[nfound] = indexes[loop_counter] |
| 685 |
output_other[nfound] = im_buf[i].target_id |
| 686 |
|
| 687 |
nfound += 1 |
| 688 |
i += 1 |
| 689 |
|
| 690 |
if nhit < 1024: |
| 691 |
spent = 1 |
| 692 |
|
| 693 |
cn.reset_interval_iterator(it_alloc) |
| 694 |
it = it_alloc |
| 695 |
|
| 696 |
cn.free_interval_iterator(it_alloc) |
| 697 |
|
| 698 |
return output_arr[:nfound], output_arr_other[:nfound] |
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
@cython.boundscheck(False) |
| 704 |
@cython.wraparound(False) |
| 705 |
@cython.initializedcheck(False) |
| 706 |
cpdef has_overlaps(self, const int32_t [::1] starts, const int32_t [::1] ends, const int64_t [::1] indexes): |
| 707 |
|
| 708 |
cdef int i = 0 |
| 709 |
cdef int ix = 0 |
| 710 |
cdef int length = len(starts) |
| 711 |
cdef int nfound = 0 |
| 712 |
|
| 713 |
# cdef cn.IntervalIterator *it |
| 714 |
cdef cn.IntervalMap im_buf[1024] |
| 715 |
if not self.im: # if empty |
| 716 |
return [] |
| 717 |
|
| 718 |
output_arr = np.zeros(length, dtype=np.int_) |
| 719 |
cdef long [::1] output |
| 720 |
output = output_arr |
| 721 |
|
| 722 |
for i in range(length): |
| 723 |
|
| 724 |
ix = cn.find_overlap_start(starts[i], ends[i], self.im, self.ntop) |
| 725 |
|
| 726 |
if ix != -1: |
| 727 |
output[nfound] = indexes[i] |
| 728 |
nfound += 1 |
| 729 |
|
| 730 |
i += 1 |
| 731 |
|
| 732 |
return output_arr[:nfound] |
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
cdef class NCLSIterator: |
| 737 |
|
| 738 |
cdef cn.IntervalIterator *it |
| 739 |
cdef cn.IntervalIterator *it_alloc |
| 740 |
cdef cn.IntervalMap im_buf[1024] |
| 741 |
cdef int nhit, start, end, ihit |
| 742 |
cdef NCLS32 db |
| 743 |
|
| 744 |
def __cinit__(self, int32_t start, int32_t end, NCLS32 db not None): |
| 745 |
self.it = cn.interval_iterator_alloc() |
| 746 |
self.it_alloc = self.it |
| 747 |
self.start = start |
| 748 |
self.end = end |
| 749 |
self.db = db |
| 750 |
self.nhit = 0 |
| 751 |
self.ihit = 0 |
| 752 |
|
| 753 |
|
| 754 |
def __iter__(self): |
| 755 |
return self |
| 756 |
|
| 757 |
|
| 758 |
cdef int cnext(self): # c VERSION OF ITERATOR next METHOD RETURNS INDEX |
| 759 |
cdef int i |
| 760 |
if self.ihit >= self.nhit: # TRY TO GET ONE MORE BUFFER CHUNK OF HITS |
| 761 |
if self.it == NULL: # ITERATOR IS EXHAUSTED |
| 762 |
return -1 |
| 763 |
cn.find_intervals(self.it, self.start, self.end, self.db.im, self.db.ntop, |
| 764 |
self.db.subheader, self.db.nlists, self.im_buf, 1024, |
| 765 |
&(self.nhit), &(self.it)) # GET NEXT BUFFER CHUNK |
| 766 |
self.ihit = 0 # START ITERATING FROM START OF BUFFER |
| 767 |
|
| 768 |
if self.ihit < self.nhit: # RETURN NEXT ITEM FROM BUFFER |
| 769 |
i = self.ihit |
| 770 |
self.ihit = self.ihit + 1 # ADVANCE THE BUFFER COUNTER |
| 771 |
return i |
| 772 |
else: # BUFFER WAS EMPTY, NO HITS TO ITERATE OVER... |
| 773 |
return -1 |
| 774 |
|
| 775 |
|
| 776 |
# PYTHON VERSION OF next RETURNS HIT AS A TUPLE |
| 777 |
def __next__(self): # PYREX USES THIS NON-STANDARD NAME INSTEAD OF next()!!! |
| 778 |
cdef int i |
| 779 |
i = self.cnext() |
| 780 |
if i >= 0: |
| 781 |
return (self.im_buf[i].start, self.im_buf[i].end, self.im_buf[i].target_id) |
| 782 |
else: |
| 783 |
raise StopIteration |
| 784 |
|
| 785 |
def __dealloc__(self): |
| 786 |
'remember: dealloc cannot call other methods!' |
| 787 |
cn.free_interval_iterator(self.it_alloc) |
| 788 |
|
| 789 |
|
| 790 |
def find_overlap(self, int32_t start, int32_t end): |
| 791 |
if not self.im: |
| 792 |
return [] |
| 793 |
|
| 794 |
return NCLSIterator(start, end, self) |