Debian Codegraph @

ncls/src/fncls.pyx

raw
1 cimport cython
2 from libc.stdint cimport int64_t
3 from libc.stdlib cimport malloc
4
5 cimport ncls.src.cfncls as cn
6 cimport ncls.src.cncls as cn
7
8 import numpy as np
9
10
11 cdef inline int int_max(int a, int b): return a if a >= b else b
12 cdef inline int int_min(int a, int b): return a if a <= b else b
13 # import ctypes as c
14
15 try:
16 dummy = profile
17 except:
18 profile = lambda x: x
19
20
21 cdef class FNCLS:
22
23 cdef cn.SublistHeader *subheader
24 cdef cn.IntervalMap *im
25 cdef int n, ntop
26 cdef int nlists
27
28 # build NCLS from array of starts, ends, values
29 @cython.boundscheck(False)
30 @cython.wraparound(False)
31 @cython.initializedcheck(False)
32 def __cinit__(self, const double [::1] starts=None, const double [::1] ends=None, const int64_t [::1] ids=None):
33
34 if None in (starts, ends, ids):
35 return
36
37 if len(starts) == 0 or len(ends) == 0 or len(ids) == 0:
38 return
39
40 cdef int i
41 cdef length = len(starts)
42 self.close() # DUMP OUR EXISTING MEMORY
43 self.n = len(starts)
44 self.im = cn.interval_map_alloc(self.n)
45 if self.im == NULL:
46 raise MemoryError('unable to allocate IntervalMap[%d]' % self.n)
47 i = 0
48 for i in range(length):
49 self.im[i].start = starts[i]
50 self.im[i].end = ends[i]
51 self.im[i].target_id = <long> ids[i]
52 self.im[i].sublist = -1
53
54 self.subheader = cn.build_nested_list(self.im, self.n, &(self.ntop), &(self.nlists))
55
56 @cython.boundscheck(False)
57 @cython.wraparound(False)
58 @cython.initializedcheck(False)
59 cpdef all_overlaps_both(self, const double [::1] starts, const double [::1] ends, const int64_t [::1] indexes):
60
61 cdef int i = 0
62 cdef int nhit = 0
63 cdef int length = len(starts)
64 cdef int loop_counter = 0
65 cdef int nfound = 0
66
67 output_arr = np.zeros(length, dtype=np.double)
68 output_arr_other = np.zeros(length, dtype=np.double)
69 cdef double [::1] output
70 cdef double [::1] output_other
71
72 output = output_arr
73 output_other = output_arr_other
74
75 cdef cn.IntervalIterator *it
76 cdef cn.IntervalIterator *it_alloc
77
78 cdef cn.IntervalMap im_buf[1024]
79 if not self.im: # if empty
80 return [], []
81
82 it_alloc = cn.interval_iterator_alloc()
83 it = it_alloc
84 for loop_counter in range(length):
85
86 # print("---search---: {} {}".format(starts[loop_counter], ends[loop_counter]))
87 while it:
88 i = 0
89 cn.find_intervals(it, starts[loop_counter], ends[loop_counter], self.im, self.ntop,
90 self.subheader, self.nlists, im_buf, 1024,
91 &(nhit), &(it)) # GET NEXT BUFFER CHUNK
92
93 # print("nhit", nhit)
94 if nfound + nhit >= length:
95
96 length = (length + nhit) * 2
97 output_arr = np.resize(output_arr, length)
98 output_arr_other = np.resize(output_arr_other, length)
99 output = output_arr
100 output_other = output_arr_other
101
102 while i < nhit:
103 # print(" i", i)
104
105 # print("length", length)
106 # print("nfound", nfound)
107 # print("loop_counter", loop_counter)
108 output[nfound] = indexes[loop_counter]
109 output_other[nfound] = im_buf[i].target_id
110
111 # print(" hit {} {}".format(im_buf[i].start, im_buf[i].end))
112 # print(" output[nfound]", output[nfound])
113 # print(" output_other[nfound]", output_other[nfound])
114
115 nfound += 1
116 i += 1
117
118 cn.reset_interval_iterator(it_alloc)
119 it = it_alloc
120
121 cn.free_interval_iterator(it_alloc)
122 # end = time()
123
124 # print("ncls time:", end - start)
125
126 return output_arr[:nfound], output_arr_other[:nfound]
127
128
129 def __len__(self):
130 return self.n
131
132 def __str__(self):
133
134 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)]
135 return "NCLS64\n------\n" + "\n".join(str(c) for c in contents)
136
137 def __dealloc__(self):
138 'remember: dealloc cannot call other methods!'
139 if self.subheader:
140 cn.free(self.subheader)
141 if self.im:
142 cn.free(self.im)
143
144 def close(self):
145 if self.subheader:
146 cn.free(self.subheader)
147 if self.im:
148 cn.free(self.im)
149 self.subheader = NULL
150 self.im = NULL
151
152 return None