-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathklib.cr
354 lines (337 loc) · 8.09 KB
/
klib.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
module Klib
def getopt(argv : Array(String), ostr : String, longopts)
return if argv.size == 0
pos, cur = 0, 0
while cur < argv.size
lopt, opt, arg = "", '?', ""
while cur < argv.size
cur_len = argv[cur].size
if argv[cur][0] == '-' && cur_len > 1
cur = argv.size if cur_len == 2 && argv[cur][1] == '-'
break
else
cur += 1
end
end
break if cur == argv.size
a, a_len = argv[cur], argv[cur].size
if a[0] == '-' && a[1] == '-' # a long option
pos = -1
c, k, tmp = 0, -1, ""
pos_eq = a.index('=', 2) || -1
if pos_eq > 0
o = a[2 ... pos_eq]
arg = a[(pos_eq + 1 ..)]
else
o = a[(2 ..)]
end
longopts.each_index do |i|
y = longopts[i]
y = y[0 ... y.size - 1] if y[y.size - 1] == '=' # slow
if o.size <= y.size && o == y[0 ... o.size] # prefix match
k, tmp = i, y
c += 1 # c is the number of matches
if o == y # exact match
c = 1
break
end
end
end
if c == 1 # find a unique match
lopt = tmp
arg = argv.delete_at(cur + 1) if pos_eq < 0 && longopts[k][longopts[k].size-1] == '=' && cur + 1 < argv.size
end
else # a short optoin
pos = 1 if pos == 0
opt = a[pos]
pos += 1
k = ostr.index(opt) || -1
if k < 0
opt = '?'
elsif k + 1 < ostr.size && ostr[k+1] == ':' # requiring an argument
if pos >= a.size
arg = argv.delete_at(cur + 1) if cur + 1 < argv.size
else
arg = a[(pos ..)]
end
pos = -1
end
end
if pos < 0 || pos >= argv[cur].size
argv.delete_at(cur)
pos = 0
end
if lopt != ""
yield "--#{lopt}", arg
else
yield "-#{opt}", arg
end
end
end
class ByteString
getter size, cap, ptr
def initialize(sz = 0)
@ptr = Pointer(UInt8).new(0)
@size, @cap = 0, 0
self.resize(sz) if sz > 0
end
@[AlwaysInline]
def unsafe_fetch(i : Int32)
@ptr[i]
end
@[AlwaysInline]
def unsafe_set(i : Int32, v : UInt8)
@ptr[i] = v
end
@[AlwaysInline]
def addr(i : Int)
@ptr + i
end
@[AlwaysInline]
private def roundup32(x : Int32) : Int32
x -= 1
x |= x >> 1
x |= x >> 2
x |= x >> 4
x |= x >> 8
x |= x >> 16
return x + 1
end
def resize(sz : Int32)
@size = sz
return if @size <= @cap
@cap = roundup32(@size)
@ptr = @ptr.realloc(@cap)
end
def push(c : UInt8)
resize(@size + 1)
@ptr[@size - 1] = c
end
def append(len : Int32, ptr : Pointer(UInt8))
return if len <= 0
old_size = @size
resize(@size + len)
(@ptr + old_size).copy_from(ptr, len)
end
def unsafe_find_chr(c : Int32, st : Int, en : Int)
p = LibC.memchr(@ptr + st, c, en - st)
return p == Pointer(Void).null ? en : (p.address - @ptr.address).to_i32
end
def unsafe_find(c : Int32, st : Int, en : Int)
r = en
if c == -1
r = unsafe_find_chr(0xa, st, en)
elsif c >= 0
r = unsafe_find_chr(c, st, en)
elsif c == -2
(st ... en).each do |i|
x = unsafe_fetch(i)
if x == 0x9_u8 || x == 0x20_u8 || x == 0xa_u8 # TODO: deal with '\r'
r = i
break
end
end
end
return r
end
def to_slice
Slice.new(@ptr, @size)
end
def to_s
String.new(Slice.new(@ptr, @size))
end
end # class ByteString
abstract class BufferedReader
@buf = ByteString.new(0x10000)
@st, @en, @eof = 0, 0, false
abstract def unbuffered_read(slice : Bytes)
def read_bytes(buf : ByteString, rest : Int) : Int32
return 0 if @eof && @st >= @en
while rest > @en - @st
buf.append(@en - @st, @buf.addr(@st))
rest -= @en - @st
@st, @en = 0, unbuffered_read(@buf.to_slice)
@eof = true if @en < @buf.size
return -2 if @en < 0
return buf.size if @en == 0
end
buf.append(rest, @buf.addr(@st))
@st += rest
return buf.size
end
def read_byte : Int32
return -1 if @eof && @st >= @en
if @st >= @en
@st, @en = 0, unbuffered_read(@buf.to_slice)
@eof = true if @en < @buf.size
return -1 if @en == 0
return -2 if @en < 0
end
c = @buf.unsafe_fetch(@st)
@st += 1
return c.to_i
end
def eof
@eof && @st >= @en ? true : false
end
def read_until(buf : ByteString, delim = -1, offset = 0, keep = false) : Int32
gotany = false
buf.resize(offset)
while true
if @st >= @en
break if @eof
@st, @en = 0, unbuffered_read(@buf.to_slice)
@eof = true if @en < @buf.size
return -2 if @en < 0
break if @en == 0
end
gotany = true
r = @buf.unsafe_find(delim, @st, @en)
if r < @en && keep
buf.append(r - @st + 1, @buf.addr(@st))
else
buf.append(r - @st, @buf.addr(@st))
end
@st = r + 1
break if r < @en
end
return -1 if !gotany && self.eof
buf.resize(buf.size - 1) if delim == -1 && buf.size > 1 && buf.unsafe_fetch(buf.size - 1) == 0xd_u8
return buf.size
end
end # class BufferedReader
@[Link("z")]
lib KlibZ
fun gzopen(fn : LibC::Char*, mode : LibC::Char*) : Void*
fun gzclose(fp : Void*) : LibC::Int
fun gzread(fp : Void*, buf : Void*, len : LibC::UInt) : LibC::Int
end
class GzipReader < BufferedReader
def initialize(fn)
@fp = KlibZ.gzopen(fn, "r")
raise "GzipReader: failed to open the file" if @fp == Pointer(Void).null
@closed = false
end
def finalize
self.close
end
def close
return if @closed
@closed = true
KlibZ.gzclose(@fp) >= 0 || raise "GzipReader: failed to close the file"
end
def unbuffered_read(buf : Bytes)
return 0 if @closed
ret = KlibZ.gzread(@fp, buf, buf.size.to_u32)
raise "GzipReader: failed to read data" if ret < 0
return ret
end
end
class FastxReader(F)
getter name, seq, qual, comment
def initialize(@fp : F)
@last_char = 0
@seq = ByteString.new
@qual = ByteString.new
@name = ByteString.new
@comment = ByteString.new
@tmp = ByteString.new
end
def read
if @last_char == 0
while (c = @fp.read_byte) >= 0 && c != 62 && c != 64
end
return c if c < 0
@last_char = c
end
@seq.resize(0)
@qual.resize(0)
@comment.resize(0)
r = @fp.read_until(@name, -2, 0, true)
return r if r < 0
@fp.read_until(@comment, -1) if @name.unsafe_fetch(@name.size - 1) != 0xa_u8
@name.resize(@name.size - 1)
while (c = @fp.read_byte) >= 0 && c != 62 && c != 64 && c != 43
next if c == 0xd
@seq.push(c.to_u8)
@fp.read_until(@seq, -1, @seq.size)
end
@last_char = c if c == 62 || c == 64
return @seq.size if c != 43
r = @fp.read_until(@tmp)
return -2 if r < 0
while (r = @fp.read_until(@qual, -1, @qual.size)) >= 0 && @qual.size < @seq.size
end
return -3 if r == -2
@last_char = 0
return -2 if @seq.size != @qual.size
return @seq.size
end
end
module IITree(SType, DType)
struct Interval(SType, DType)
property st, en, max, data
def initialize(@st : SType, @en : SType, @max : SType, @data : DType)
end
end
def self.index(a : Array(Interval(SType, DType)))
a.sort_by!{|x| x.st}
last, last_i, i = 0, 1, 0
while i < a.size
last, last_i = a[i].en, i
a[i] = Interval.new(a[i].st, a[i].en, a[i].en, a[i].data)
i += 2
end
k = 1
while 1<<k <= a.size
i0, step = (1<<k) - 1, 1<<(k+1)
i = i0
while i < a.size
x = 1 << (k - 1)
max = a[i].en > a[i-x].max ? a[i].en : a[i-x].max
e = i + x < a.size ? a[i+x].max : last
max = e if max < e
a[i] = Interval.new(a[i].st, a[i].en, max, a[i].data)
i += step
end
last_i = (last_i>>k&1) != 0 ? last_i - (1<<(k-1)) : last_i + (1<<(k-1))
if last_i < a.size
last = last > a[last_i].max ? last : a[last_i].max
end
k += 1
end
end
def self.overlap(a : Array(Interval(SType, DType)), st : SType, en : SType)
h = 0
while 1<<h <= a.size
h += 1
end
h -= 1
stack, n = StaticArray(Tuple(Int32, Int32, Int32), 64).new({0,0,0}), 0
stack[n], n = { (1<<h)-1, h, 0 }, n + 1
while n > 0
n -= 1
x, h, w = stack[n]
if h <= 3
i0 = x >> h << h
i1 = i0 + (1<<(h+1)) - 1
i1 = a.size if i1 >= a.size
i = i0
while i < i1 && a.unsafe_fetch(i).st < en
yield a.unsafe_fetch(i) if st < a.unsafe_fetch(i).en
i += 1
end
elsif w == 0
stack[n], n = { x, h, 1 }, n + 1
y = x - (1<<(h-1))
if y >= a.size || a.unsafe_fetch(y).max > st
stack[n], n = { y, h - 1, 0 }, n + 1
end
elsif x < a.size && a.unsafe_fetch(x).st < en
yield a.unsafe_fetch(x) if st < a.unsafe_fetch(x).en
stack[n], n = { x + (1<<(h-1)), h - 1, 0 }, n + 1
end
end
end
end # module IITree
end # module Klib