htslib 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,429 +2,237 @@
2
2
 
3
3
  module HTS
4
4
  module LibHTS
5
- attach_function \
6
- :bcf_hdr_init,
7
- [:string],
8
- BcfHdr.by_ref
9
-
10
- attach_function \
11
- :bcf_hdr_destroy,
12
- [BcfHdr],
13
- :void
14
-
15
- attach_function \
16
- :bcf_init,
17
- [],
18
- Bcf1.by_ref
19
-
20
- attach_function \
21
- :bcf_destroy,
22
- [Bcf1],
23
- :void
24
-
25
- attach_function \
26
- :bcf_empty,
27
- [Bcf1],
28
- :void
29
-
30
- attach_function \
31
- :bcf_clear,
32
- [Bcf1],
33
- :void
34
-
35
- attach_function \
36
- :bcf_hdr_read,
37
- [HtsFile],
38
- BcfHdr.by_ref
39
-
40
- attach_function \
41
- :bcf_hdr_set_samples,
42
- [BcfHdr, :string, :int],
43
- :int
44
-
45
- attach_function \
46
- :bcf_subset_format,
47
- [BcfHdr, Bcf1],
48
- :int
49
-
50
- attach_function \
51
- :bcf_hdr_write,
52
- [HtsFile, BcfHdr],
53
- :int
54
-
55
- attach_function \
56
- :vcf_parse,
57
- [KString, BcfHdr, Bcf1],
58
- :int
59
-
60
- attach_function \
61
- :vcf_open_mode,
62
- %i[string string string],
63
- :int
64
-
65
- attach_function \
66
- :vcf_format,
67
- [BcfHdr, Bcf1, KString],
68
- :int
69
-
70
- attach_function \
71
- :bcf_read,
72
- [HtsFile, BcfHdr, Bcf1],
73
- :int
74
-
75
- attach_function \
76
- :bcf_unpack,
77
- [Bcf1, :int],
78
- :int
79
-
80
- attach_function \
81
- :bcf_dup,
82
- [Bcf1],
83
- Bcf1.by_ref
84
-
85
- attach_function \
86
- :bcf_copy,
87
- [Bcf1, Bcf1],
88
- Bcf1.by_ref
89
-
90
- attach_function \
91
- :bcf_write,
92
- [HtsFile, BcfHdr, Bcf1],
93
- :int
94
-
95
- attach_function \
96
- :vcf_hdr_read,
97
- [HtsFile],
98
- BcfHdr.by_ref
99
-
100
- attach_function \
101
- :vcf_hdr_write,
102
- [HtsFile, BcfHdr],
103
- :int
104
-
105
- attach_function \
106
- :vcf_read,
107
- [HtsFile, BcfHdr, Bcf1],
108
- :int
109
-
110
- attach_function \
111
- :vcf_write,
112
- [HtsFile, BcfHdr, Bcf1],
113
- :int
114
-
115
- attach_function \
116
- :bcf_readrec,
117
- [BGZF, :pointer, :pointer, :pointer, :hts_pos_t, :hts_pos_t],
118
- :int
119
-
120
- attach_function \
121
- :vcf_write_line,
122
- [HtsFile, KString],
123
- :int
124
-
125
- attach_function \
126
- :bcf_hdr_dup,
127
- [BcfHdr],
128
- BcfHdr.by_ref
129
-
130
- attach_function \
131
- :bcf_hdr_merge,
132
- [BcfHdr, BcfHdr],
133
- BcfHdr.by_ref
134
-
135
- attach_function \
136
- :bcf_hdr_add_sample,
137
- [BcfHdr, :string],
138
- :int
139
-
140
- attach_function \
141
- :bcf_hdr_set,
142
- [BcfHdr, :string],
143
- :int
144
-
145
- attach_function \
146
- :bcf_hdr_format,
147
- [BcfHdr, :int, KString],
148
- :int
149
-
150
- attach_function \
151
- :bcf_hdr_fmt_text,
152
- [BcfHdr, :int, :pointer],
153
- :string
154
-
155
- attach_function \
156
- :bcf_hdr_append,
157
- [BcfHdr, :string],
158
- :int
159
-
160
- attach_function \
161
- :bcf_hdr_printf,
162
- [BcfHdr, :string, :varargs],
163
- :int
164
-
165
- attach_function \
166
- :bcf_hdr_get_version,
167
- [BcfHdr],
168
- :string
169
-
170
- attach_function \
171
- :bcf_hdr_set_version,
172
- [BcfHdr, :string],
173
- :int
174
-
175
- attach_function \
176
- :bcf_hdr_remove,
177
- [BcfHdr, :int, :string],
178
- :void
179
-
180
- attach_function \
181
- :bcf_hdr_subset,
182
- [BcfHdr, :int, :pointer, :pointer],
183
- BcfHdr.by_ref
184
-
185
- attach_function \
186
- :bcf_hdr_seqnames,
187
- [BcfHdr, :pointer],
188
- :pointer
189
-
190
- attach_function \
191
- :bcf_hdr_parse,
192
- [BcfHdr, :string],
193
- :int
194
-
195
- attach_function \
196
- :bcf_hdr_sync,
197
- [BcfHdr],
198
- :int
199
-
200
- attach_function \
201
- :bcf_hdr_parse_line,
202
- [BcfHdr, :string, :pointer],
203
- BcfHrec.by_ref
204
-
205
- attach_function \
206
- :bcf_hrec_format,
207
- [BcfHrec, KString],
208
- :int
209
-
210
- attach_function \
211
- :bcf_hdr_add_hrec,
212
- [BcfHdr, BcfHrec],
213
- :int
214
-
215
- attach_function \
216
- :bcf_hdr_get_hrec,
217
- [BcfHdr, :int, :string, :string, :string],
218
- BcfHrec.by_ref
219
-
220
- attach_function \
221
- :bcf_hrec_dup,
222
- [BcfHrec],
223
- BcfHrec.by_ref
224
-
225
- attach_function \
226
- :bcf_hrec_add_key,
227
- [BcfHrec, :string, :size_t],
228
- :int
229
-
230
- attach_function \
231
- :bcf_hrec_set_val,
232
- [BcfHrec, :int, :string, :size_t, :int],
233
- :int
234
-
235
- attach_function \
236
- :bcf_hrec_find_key,
237
- [BcfHrec, :string],
238
- :int
239
-
240
- attach_function \
241
- :hrec_add_idx,
242
- [BcfHrec, :int],
243
- :int
244
-
245
- attach_function \
246
- :bcf_hrec_destroy,
247
- [BcfHrec],
248
- :void
249
-
250
- attach_function \
251
- :bcf_subset,
252
- [BcfHdr, Bcf1, :int, :pointer],
253
- :int
254
-
255
- attach_function \
256
- :bcf_translate,
257
- [BcfHdr, BcfHdr, Bcf1],
258
- :int
259
-
260
- attach_function \
261
- :bcf_get_variant_types,
262
- [Bcf1],
263
- :int
264
-
265
- attach_function \
266
- :bcf_get_variant_type,
267
- [Bcf1, :int],
268
- :int
269
-
270
- attach_function \
271
- :bcf_is_snp,
272
- [Bcf1],
273
- :int
274
-
275
- attach_function \
276
- :bcf_update_filter,
277
- [BcfHdr, Bcf1, :pointer, :int],
278
- :int
279
-
280
- attach_function \
281
- :bcf_add_filter,
282
- [BcfHdr, Bcf1, :int],
283
- :int
284
-
285
- attach_function \
286
- :bcf_remove_filter,
287
- [BcfHdr, Bcf1, :int, :int],
288
- :int
289
-
290
- attach_function \
291
- :bcf_has_filter,
292
- [BcfHdr, Bcf1, :string],
293
- :int
294
-
295
- attach_function \
296
- :bcf_update_alleles,
297
- [BcfHdr, Bcf1, :pointer, :int],
298
- :int
299
-
300
- attach_function \
301
- :bcf_update_alleles_str,
302
- [BcfHdr, Bcf1, :string],
303
- :int
304
-
305
- attach_function \
306
- :bcf_update_id,
307
- [BcfHdr, Bcf1, :string],
308
- :int
309
-
310
- attach_function \
311
- :bcf_add_id,
312
- [BcfHdr, Bcf1, :string],
313
- :int
314
-
315
- attach_function \
316
- :bcf_update_info,
317
- [BcfHdr, Bcf1, :string, :pointer, :int, :int],
318
- :int
319
-
320
- attach_function \
321
- :bcf_update_format_string,
322
- [BcfHdr, Bcf1, :string, :pointer, :int],
323
- :int
324
-
325
- attach_function \
326
- :bcf_update_format,
327
- [BcfHdr, Bcf1, :string, :pointer, :int, :int],
328
- :int
329
-
330
- attach_function \
331
- :bcf_get_fmt,
332
- [BcfHdr, Bcf1, :string],
333
- BcfFmt.by_ref
334
-
335
- attach_function \
336
- :bcf_get_info,
337
- [BcfHdr, Bcf1, :string],
338
- BcfInfo.by_ref
339
-
340
- attach_function \
341
- :bcf_get_fmt_id,
342
- [Bcf1, :int],
343
- BcfFmt.by_ref
344
-
345
- attach_function \
346
- :bcf_get_info_id,
347
- [Bcf1, :int],
348
- BcfInfo.by_ref
349
-
350
- attach_function \
351
- :bcf_get_info_values,
352
- [BcfHdr, Bcf1, :string, :pointer, :pointer, :int],
353
- :int
354
-
355
- attach_function \
356
- :bcf_get_format_string,
357
- [BcfHdr, Bcf1, :string, :pointer, :pointer],
358
- :int
359
-
360
- attach_function \
361
- :bcf_get_format_values,
362
- [BcfHdr, Bcf1, :string, :pointer, :pointer, :int],
363
- :int
364
-
365
- attach_function \
366
- :bcf_hdr_id2int,
367
- [BcfHdr, :int, :string],
368
- :int
369
-
370
- attach_function \
371
- :bcf_fmt_array,
372
- [KString, :int, :int, :pointer],
373
- :int
374
-
375
- attach_function \
376
- :bcf_fmt_sized_array,
377
- [KString, :pointer],
378
- :uint8_t
379
-
380
- attach_function \
381
- :bcf_enc_vchar,
382
- [KString, :int, :string],
383
- :int
384
-
385
- attach_function \
386
- :bcf_enc_vint,
387
- [KString, :int, :pointer, :int],
388
- :int
389
-
390
- attach_function \
391
- :bcf_enc_vfloat,
392
- [KString, :int, :pointer],
393
- :int
394
-
395
- attach_function \
396
- :bcf_index_load2,
397
- %i[string string],
398
- HtsIdx.by_ref
399
-
400
- attach_function \
401
- :bcf_index_load3,
402
- %i[string string int],
403
- HtsIdx.by_ref
404
-
405
- attach_function \
406
- :bcf_index_build,
407
- %i[string int],
408
- :int
409
-
410
- attach_function \
411
- :bcf_index_build2,
412
- %i[string string int],
413
- :int
414
-
415
- attach_function \
416
- :bcf_index_build3,
417
- %i[string string int int],
418
- :int
419
-
420
- attach_function \
421
- :bcf_idx_init,
422
- [HtsFile, BcfHdr, :int, :string],
423
- :int
424
-
425
- attach_function \
426
- :bcf_idx_save,
427
- [HtsFile],
428
- :int
5
+ # constants
6
+ BCF_HL_FLT = 0 # header line
7
+ BCF_HL_INFO = 1
8
+ BCF_HL_FMT = 2
9
+ BCF_HL_CTG = 3
10
+ BCF_HL_STR = 4 # structured header line TAG=<A=..,B=..>
11
+ BCF_HL_GEN = 5 # generic header line
12
+
13
+ BCF_HT_FLAG = 0 # header type
14
+ BCF_HT_INT = 1
15
+ BCF_HT_REAL = 2
16
+ BCF_HT_STR = 3
17
+ BCF_HT_LONG = (BCF_HT_INT | 0x100) # BCF_HT_INT, but for int64_t values; VCF only!
18
+
19
+ BCF_VL_FIXED = 0 # variable length
20
+ BCF_VL_VAR = 1
21
+ BCF_VL_A = 2
22
+ BCF_VL_G = 3
23
+ BCF_VL_R = 4
24
+
25
+ BCF_DT_ID = 0 # dictionary type
26
+ BCF_DT_CTG = 1
27
+ BCF_DT_SAMPLE = 2
28
+
29
+ BCF_BT_NULL = 0
30
+ BCF_BT_INT8 = 1
31
+ BCF_BT_INT16 = 2
32
+ BCF_BT_INT32 = 3
33
+ BCF_BT_INT64 = 4 # Unofficial, for internal use only.
34
+ BCF_BT_FLOAT = 5
35
+ BCF_BT_CHAR = 7
36
+
37
+ VCF_REF = 0
38
+ VCF_SNP = 1
39
+ VCF_MNP = 2
40
+ VCF_INDEL = 4
41
+ VCF_OTHER = 8
42
+ VCF_BND = 16 # breakend
43
+ VCF_OVERLAP = 32 # overlapping deletion, ALT=*
44
+
45
+ BCF1_DIRTY_ID = 1
46
+ BCF1_DIRTY_ALS = 2
47
+ BCF1_DIRTY_FLT = 4
48
+ BCF1_DIRTY_INF = 8
49
+
50
+ BCF_ERR_CTG_UNDEF = 1
51
+ BCF_ERR_TAG_UNDEF = 2
52
+ BCF_ERR_NCOLS = 4
53
+ BCF_ERR_LIMITS = 8
54
+ BCF_ERR_CHAR = 16
55
+ BCF_ERR_CTG_INVALID = 32
56
+ BCF_ERR_TAG_INVALID = 64
57
+
58
+ # macros
59
+ class << self
60
+ alias bcf_init1 bcf_init
61
+ alias bcf_read1 bcf_read
62
+ alias vcf_read1 vcf_read
63
+ alias bcf_write1 bcf_write
64
+ alias vcf_write1 vcf_write
65
+ alias bcf_destroy1 bcf_destroy
66
+ alias bcf_empty1 bcf_empty
67
+ alias vcf_parse1 vcf_parse
68
+ alias bcf_clear1 bcf_clear
69
+ alias vcf_format1 vcf_format
70
+
71
+ alias bcf_open hts_open
72
+ alias vcf_open hts_open
73
+ if respond_to?(:hts_flush)
74
+ alias bcf_flush hts_flush
75
+ alias vcf_flush hts_flush
76
+ end
77
+ alias bcf_close hts_close
78
+ alias vcf_close hts_close
79
+ end
80
+
81
+ BCF_UN_STR = 1 # up to ALT inclusive
82
+ BCF_UN_FLT = 2 # up to FILTER
83
+ BCF_UN_INFO = 4 # up to INFO
84
+ BCF_UN_SHR = (BCF_UN_STR | BCF_UN_FLT | BCF_UN_INFO) # all shared information
85
+ BCF_UN_FMT = 8 # unpack format and each sample
86
+ BCF_UN_IND = BCF_UN_FMT # a synonym of BCF_UN_FMT
87
+ BCF_UN_ALL = (BCF_UN_SHR | BCF_UN_FMT) # everything
88
+
89
+ class << self
90
+ def bcf_hdr_nsamples(hdr)
91
+ hdr[:n][BCF_DT_SAMPLE]
92
+ end
93
+
94
+ def bcf_update_info_int32(hdr, line, key, values, n)
95
+ bcf_update_info(hdr, line, key, values, n, BCF_HT_INT)
96
+ end
97
+
98
+ def bcf_update_info_float(hdr, line, key, values, n)
99
+ bcf_update_info(hdr, line, key, values, n, BCF_HT_REAL)
100
+ end
101
+
102
+ def bcf_update_info_flag(hdr, line, key, string, n)
103
+ bcf_update_info(hdr, line, key, string, n, BCF_HT_FLAG)
104
+ end
105
+
106
+ def bcf_update_info_string(hdr, line, key, string)
107
+ bcf_update_info(hdr, line, key, string, 1, BCF_HT_STR)
108
+ end
109
+
110
+ def bcf_update_format_int32(hdr, line, key, values, n)
111
+ bcf_update_format(hdr, line, key, values, n,
112
+ BCF_HT_INT)
113
+ end
114
+
115
+ def bcf_update_format_float(hdr, line, key, values, n)
116
+ bcf_update_format(hdr, line, key, values, n,
117
+ BCF_HT_REAL)
118
+ end
119
+
120
+ def bcf_update_format_char(hdr, line, key, values, n)
121
+ bcf_update_format(hdr, line, key, values, n,
122
+ BCF_HT_STR)
123
+ end
124
+
125
+ def bcf_update_genotypes(hdr, line, gts, n)
126
+ bcf_update_format(hdr, line, "GT", gts, n, BCF_HT_INT)
127
+ end
128
+
129
+ def bcf_gt_phased(idx)
130
+ ((idx + 1) << 1 | 1)
131
+ end
132
+
133
+ def bcf_gt_unphased(idx)
134
+ ((idx + 1) << 1)
135
+ end
136
+
137
+ def bcf_gt_missing
138
+ 0
139
+ end
140
+
141
+ def bcf_gt_is_missing(val)
142
+ ((val) >> 1 ? 0 : 1)
143
+ end
144
+
145
+ def bcf_gt_is_phased(idx)
146
+ ((idx) & 1)
147
+ end
148
+
149
+ def bcf_gt_allele(val)
150
+ (((val) >> 1) - 1)
151
+ end
152
+
153
+ def bcf_alleles2gt(a, b)
154
+ ((a) > (b) ? (a * (a + 1) / 2 + b) : (b * (b + 1) / 2 + a))
155
+ end
156
+
157
+ def bcf_get_info_int32(hdr, line, tag, dst, ndst)
158
+ bcf_get_info_values(hdr, line, tag, dst, ndst, BCF_HT_INT)
159
+ end
160
+
161
+ def bcf_get_info_float(hdr, line, tag, dst, ndst)
162
+ bcf_get_info_values(hdr, line, tag, dst, ndst, BCF_HT_REAL)
163
+ end
164
+
165
+ def bcf_get_info_string(hdr, line, tag, dst, ndst)
166
+ bcf_get_info_values(hdr, line, tag, dst, ndst, BCF_HT_STR)
167
+ end
168
+
169
+ def bcf_get_info_flag(hdr, line, tag, dst, ndst)
170
+ bcf_get_info_values(hdr, line, tag, dst, ndst, BCF_HT_FLAG)
171
+ end
172
+
173
+ def bcf_get_format_int32(hdr, line, tag, dst, ndst)
174
+ bcf_get_format_values(hdr, line, tag, dst, ndst, BCF_HT_INT)
175
+ end
176
+
177
+ def bcf_get_format_float(hdr, line, tag, dst, ndst)
178
+ bcf_get_format_values(hdr, line, tag, dst, ndst, BCF_HT_REAL)
179
+ end
180
+
181
+ def bcf_get_format_char(hdr, line, tag, dst, ndst)
182
+ bcf_get_format_values(hdr, line, tag, dst, ndst, BCF_HT_STR)
183
+ end
184
+
185
+ def bcf_get_genotypes(hdr, line, dst, ndst)
186
+ bcf_get_format_values(hdr, line, "GT", dst, ndst, BCF_HT_INT)
187
+ end
188
+
189
+ def bcf_hdr_int2id(hdr, type, int_id)
190
+ LibHTS::BcfIdpair.new(
191
+ hdr[:id][type].to_ptr +
192
+ LibHTS::BcfIdpair.size * int_id # offsets
193
+ )[:key]
194
+ end
195
+
196
+ def bcf_hdr_name2id(hdr, id)
197
+ bcf_hdr_id2int(hdr, BCF_DT_CTG, id)
198
+ end
199
+
200
+ def bcf_hdr_id2name(hdr, rid)
201
+ return nil if hdr.null? || rid < 0 || rid >= hdr[:n][LibHTS::BCF_DT_CTG]
202
+
203
+ LibHTS::BcfIdpair.new(
204
+ hdr[:id][LibHTS::BCF_DT_CTG].to_ptr +
205
+ LibHTS::BcfIdpair.size * rid # offset
206
+ )[:key]
207
+ end
208
+
209
+ def bcf_hdr_id2length(hdr, type, int_id)
210
+ LibHTS::BcfIdpair.new(
211
+ hdr[:id][LibHTS::BCF_DT_ID].to_ptr +
212
+ LibHTS::BcfIdpair.size * int_id # offset
213
+ )[:val][:info][type] >> 8 & 0xf
214
+ end
215
+
216
+ def bcf_hdr_id2number(hdr, type, int_id)
217
+ LibHTS::BcfIdpair.new(
218
+ hdr[:id][LibHTS::BCF_DT_ID].to_ptr +
219
+ LibHTS::BcfIdpair.size * int_id # offset
220
+ )[:val][:info][type] >> 12
221
+ end
222
+
223
+ def bcf_hdr_id2type(hdr, type, int_id)
224
+ LibHTS::BcfIdpair.new(
225
+ hdr[:id][LibHTS::BCF_DT_ID].to_ptr +
226
+ LibHTS::BcfIdpair.size * int_id # offset
227
+ )[:val][:info][type] >> 4 & 0xf
228
+ end
229
+
230
+ def bcf_hdr_id2coltype(hdr, type, int_id)
231
+ LibHTS::BcfIdpair.new(
232
+ hdr[:id][LibHTS::BCF_DT_ID].to_ptr +
233
+ LibHTS::BcfIdpair.size * int_id # offset
234
+ )[:val][:info][type] & 0xf
235
+ end
236
+ end
429
237
  end
430
238
  end
data/lib/hts/libhts.rb CHANGED
@@ -4,6 +4,7 @@ require_relative "ffi_ext/struct"
4
4
  require_relative "ffi_ext/pointer"
5
5
 
6
6
  module HTS
7
+ # A Module for working with native C HTSlib structures and functions.
7
8
  module LibHTS
8
9
  extend FFI::Library
9
10
 
data/lib/hts/tbx.rb CHANGED
@@ -37,10 +37,9 @@ module HTS
37
37
 
38
38
  raise Errno::ENOENT, "Failed to open #{@file_name}" if @hts_file.null?
39
39
 
40
- if threads&.> 0
41
- r = LibHTS.hts_set_threads(@hts_file, threads)
42
- raise "Failed to set number of threads: #{threads}" if r < 0
43
- end
40
+ set_threads(threads) if threads
41
+
42
+ super # do nothing
44
43
  end
45
44
  end
46
45
  end
data/lib/hts/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HTS
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end