htslib 0.4.2 → 0.5.0

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.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +79 -49
  3. data/TUTORIAL.md +9 -20
  4. data/ext/htslib_native/extconf.rb +41 -0
  5. data/ext/htslib_native/htslib_native.h +11 -0
  6. data/ext/htslib_native/htslib_native_ext.c +48 -0
  7. data/ext/htslib_native/native_bam.c +1108 -0
  8. data/ext/htslib_native/native_bcf.c +369 -0
  9. data/ext/htslib_native/native_faidx.c +155 -0
  10. data/ext/htslib_native/native_tabix.c +246 -0
  11. data/lib/hts/bam/auxi.rb +87 -342
  12. data/lib/hts/bam/base_mod.rb +37 -73
  13. data/lib/hts/bam/cigar.rb +9 -55
  14. data/lib/hts/bam/flag.rb +19 -27
  15. data/lib/hts/bam/header.rb +29 -56
  16. data/lib/hts/bam/mpileup.rb +158 -132
  17. data/lib/hts/bam/pileup.rb +120 -145
  18. data/lib/hts/bam/record.rb +233 -270
  19. data/lib/hts/bam.rb +102 -42
  20. data/lib/hts/bcf/errors.rb +1 -0
  21. data/lib/hts/bcf/format.rb +278 -379
  22. data/lib/hts/bcf/header.rb +38 -122
  23. data/lib/hts/bcf/header_record.rb +9 -35
  24. data/lib/hts/bcf/info.rb +72 -320
  25. data/lib/hts/bcf/record.rb +61 -102
  26. data/lib/hts/bcf.rb +149 -323
  27. data/lib/hts/faidx.rb +28 -87
  28. data/lib/hts/hts.rb +6 -94
  29. data/lib/hts/native.rb +13 -0
  30. data/lib/hts/tabix.rb +93 -48
  31. data/lib/hts/version.rb +1 -1
  32. data/lib/htslib.rb +6 -52
  33. metadata +13 -64
  34. data/lib/hts/ffi_ext/README.md +0 -8
  35. data/lib/hts/ffi_ext/pointer.rb +0 -18
  36. data/lib/hts/ffi_ext/struct.rb +0 -45
  37. data/lib/hts/libhts/bgzf.rb +0 -199
  38. data/lib/hts/libhts/constants.rb +0 -658
  39. data/lib/hts/libhts/cram.rb +0 -471
  40. data/lib/hts/libhts/fai.rb +0 -146
  41. data/lib/hts/libhts/hfile.rb +0 -121
  42. data/lib/hts/libhts/hts.rb +0 -477
  43. data/lib/hts/libhts/kfunc.rb +0 -38
  44. data/lib/hts/libhts/sam.rb +0 -760
  45. data/lib/hts/libhts/sam_funcs.rb +0 -155
  46. data/lib/hts/libhts/tbx.rb +0 -94
  47. data/lib/hts/libhts/tbx_funcs.rb +0 -36
  48. data/lib/hts/libhts/thread_pool.rb +0 -139
  49. data/lib/hts/libhts/vcf.rb +0 -567
  50. data/lib/hts/libhts/vcf_funcs.rb +0 -366
  51. data/lib/hts/libhts.rb +0 -47
@@ -1,366 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module HTS
4
- module LibHTS
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
- # Get number of samples
91
- def bcf_hdr_nsamples(hdr)
92
- hdr[:n][BCF_DT_SAMPLE]
93
- end
94
-
95
- # Function for updating INFO fields
96
- def bcf_update_info_int32(hdr, line, key, values, n)
97
- bcf_update_info(hdr, line, key, values, n, BCF_HT_INT)
98
- end
99
-
100
- # Function for updating INFO fields (int64; VCF only)
101
- def bcf_update_info_int64(hdr, line, key, values, n)
102
- bcf_update_info(hdr, line, key, values, n, BCF_HT_LONG)
103
- end
104
-
105
- # Function for updating INFO fields
106
- def bcf_update_info_float(hdr, line, key, values, n)
107
- bcf_update_info(hdr, line, key, values, n, BCF_HT_REAL)
108
- end
109
-
110
- # Function for updating INFO fields
111
- def bcf_update_info_flag(hdr, line, key, string, n)
112
- bcf_update_info(hdr, line, key, string, n, BCF_HT_FLAG)
113
- end
114
-
115
- # Function for updating INFO fields
116
- def bcf_update_info_string(hdr, line, key, string)
117
- bcf_update_info(hdr, line, key, string, 1, BCF_HT_STR)
118
- end
119
-
120
- # Function for updating FORMAT fields
121
- def bcf_update_format_int32(hdr, line, key, values, n)
122
- bcf_update_format(hdr, line, key, values, n,
123
- BCF_HT_INT)
124
- end
125
-
126
- # Function for updating FORMAT fields
127
- def bcf_update_format_float(hdr, line, key, values, n)
128
- bcf_update_format(hdr, line, key, values, n,
129
- BCF_HT_REAL)
130
- end
131
-
132
- # Function for updating FORMAT fields
133
- def bcf_update_format_char(hdr, line, key, values, n)
134
- bcf_update_format(hdr, line, key, values, n,
135
- BCF_HT_STR)
136
- end
137
-
138
- # Function for updating FORMAT fields
139
- def bcf_update_genotypes(hdr, line, gts, n)
140
- bcf_update_format(hdr, line, "GT", gts, n, BCF_HT_INT)
141
- end
142
-
143
- # Macro for setting genotypes correctly
144
- def bcf_gt_phased(idx)
145
- ((idx + 1) << 1 | 1)
146
- end
147
-
148
- # Macro for setting genotypes correctly
149
- def bcf_gt_unphased(idx)
150
- ((idx + 1) << 1)
151
- end
152
-
153
- # Macro for setting genotypes correctly
154
- def bcf_gt_missing
155
- 0
156
- end
157
-
158
- # Macro for setting genotypes correctly
159
- def bcf_gt_vector_end
160
- bcf_int32_vector_end
161
- end
162
-
163
- # Macro for setting genotypes correctly
164
- def bcf_gt_is_missing(val)
165
- ((val >> 1) == 0 ? 1 : 0)
166
- end
167
-
168
- # Macro for setting genotypes correctly
169
- def bcf_gt_is_vector_end(val)
170
- val == bcf_gt_vector_end ? 1 : 0
171
- end
172
-
173
- # Macro for setting genotypes correctly
174
- def bcf_gt_is_phased(idx)
175
- (idx & 1)
176
- end
177
-
178
- # Macro for setting genotypes correctly
179
- def bcf_gt_allele(val)
180
- ((val >> 1) - 1)
181
- end
182
-
183
- # Conversion between alleles indexes to Number=G genotype index (assuming diploid, all 0-based)
184
- def bcf_alleles2gt(a, b)
185
- (a > b ? (a * (a + 1) / 2 + b) : (b * (b + 1) / 2 + a))
186
- end
187
-
188
- # Get INFO values
189
- def bcf_get_info_int32(hdr, line, tag, dst, ndst)
190
- bcf_get_info_values(hdr, line, tag, dst, ndst, BCF_HT_INT)
191
- end
192
-
193
- # Get INFO values (int64; VCF only)
194
- def bcf_get_info_int64(hdr, line, tag, dst, ndst)
195
- bcf_get_info_values(hdr, line, tag, dst, ndst, BCF_HT_LONG)
196
- end
197
-
198
- # Get INFO values
199
- def bcf_get_info_float(hdr, line, tag, dst, ndst)
200
- bcf_get_info_values(hdr, line, tag, dst, ndst, BCF_HT_REAL)
201
- end
202
-
203
- # Get INFO values
204
- def bcf_get_info_string(hdr, line, tag, dst, ndst)
205
- bcf_get_info_values(hdr, line, tag, dst, ndst, BCF_HT_STR)
206
- end
207
-
208
- # Get INFO values
209
- def bcf_get_info_flag(hdr, line, tag, dst, ndst)
210
- bcf_get_info_values(hdr, line, tag, dst, ndst, BCF_HT_FLAG)
211
- end
212
-
213
- # Get FORMAT values
214
- def bcf_get_format_int32(hdr, line, tag, dst, ndst)
215
- bcf_get_format_values(hdr, line, tag, dst, ndst, BCF_HT_INT)
216
- end
217
-
218
- # Get FORMAT values
219
- def bcf_get_format_float(hdr, line, tag, dst, ndst)
220
- bcf_get_format_values(hdr, line, tag, dst, ndst, BCF_HT_REAL)
221
- end
222
-
223
- # Get FORMAT values
224
- def bcf_get_format_char(hdr, line, tag, dst, ndst)
225
- bcf_get_format_values(hdr, line, tag, dst, ndst, BCF_HT_STR)
226
- end
227
-
228
- # Get FORMAT values
229
- def bcf_get_genotypes(hdr, line, dst, ndst)
230
- bcf_get_format_values(hdr, line, "GT", dst, ndst, BCF_HT_INT)
231
- end
232
-
233
- # Translates numeric ID into string
234
- def bcf_hdr_int2id(hdr, type, int_id)
235
- LibHTS::BcfIdpair.new(
236
- hdr[:id][type].to_ptr +
237
- LibHTS::BcfIdpair.size * int_id # offsets
238
- )[:key]
239
- end
240
-
241
- # Translates sequence names (chromosomes) into numeric ID
242
- def bcf_hdr_name2id(hdr, id)
243
- bcf_hdr_id2int(hdr, BCF_DT_CTG, id)
244
- end
245
-
246
- # Translates numeric ID to sequence name
247
- def bcf_hdr_id2name(hdr, rid)
248
- return nil if hdr.null? || rid < 0 || rid >= hdr[:n][LibHTS::BCF_DT_CTG]
249
-
250
- LibHTS::BcfIdpair.new(
251
- hdr[:id][LibHTS::BCF_DT_CTG].to_ptr +
252
- LibHTS::BcfIdpair.size * rid # offset
253
- )[:key]
254
- end
255
-
256
- # Macro for accessing bcf_idinfo_t
257
- def bcf_hdr_id2length(hdr, type, int_id)
258
- LibHTS::BcfIdpair.new(
259
- hdr[:id][LibHTS::BCF_DT_ID].to_ptr +
260
- LibHTS::BcfIdpair.size * int_id # offset
261
- )[:val][:info][type] >> 8 & 0xf
262
- end
263
-
264
- # Macro for accessing bcf_idinfo_t
265
- def bcf_hdr_id2number(hdr, type, int_id)
266
- LibHTS::BcfIdpair.new(
267
- hdr[:id][LibHTS::BCF_DT_ID].to_ptr +
268
- LibHTS::BcfIdpair.size * int_id # offset
269
- )[:val][:info][type] >> 12
270
- end
271
-
272
- # Macro for accessing bcf_idinfo_t
273
- def bcf_hdr_id2type(hdr, type, int_id)
274
- LibHTS::BcfIdpair.new(
275
- hdr[:id][LibHTS::BCF_DT_ID].to_ptr +
276
- LibHTS::BcfIdpair.size * int_id # offset
277
- )[:val][:info][type] >> 4 & 0xf
278
- end
279
-
280
- # Macro for accessing bcf_idinfo_t
281
- def bcf_hdr_id2coltype(hdr, type, int_id)
282
- LibHTS::BcfIdpair.new(
283
- hdr[:id][LibHTS::BCF_DT_ID].to_ptr +
284
- LibHTS::BcfIdpair.size * int_id # offset
285
- )[:val][:info][type] & 0xf
286
- end
287
-
288
- def bcf_hdr_idinfo_exists(hdr, type, int_id)
289
- return false if int_id.negative? || int_id >= hdr[:n][LibHTS::BCF_DT_ID]
290
-
291
- pair = LibHTS::BcfIdpair.new(
292
- hdr[:id][LibHTS::BCF_DT_ID].to_ptr +
293
- LibHTS::BcfIdpair.size * int_id # offset
294
- )
295
-
296
- !pair[:val].null? && bcf_hdr_id2coltype(hdr, type, int_id) != 0xf
297
- end
298
-
299
- # def bcf_hdr_id2hrec
300
-
301
- alias bcf_itr_destroy hts_itr_destroy
302
-
303
- def bcf_itr_queryi(idx, tid, beg, _end)
304
- hts_itr_query(idx, tid, beg, _end, @ffi_functions[:bcf_readrec])
305
- end
306
-
307
- @@bcf_hdr_name2id = proc do |hdr, id|
308
- LibHTS.bcf_hdr_name2id(hdr, id)
309
- end
310
-
311
- def bcf_itr_querys(idx, hdr, s)
312
- return bcf_itr_querys1(idx, hdr, s) if respond_to?(:bcf_itr_querys1)
313
-
314
- hts_itr_querys(idx, s, @@bcf_hdr_name2id, hdr, @ffi_functions[:hts_itr_query], @ffi_functions[:bcf_readrec])
315
- end
316
-
317
- # Ruby implementation of htslib's static inline bcf_itr_next().
318
- def bcf_itr_next(htsfp, itr, r)
319
- return -2 unless htsfp[:is_bgzf] == 1
320
-
321
- if itr[:multi] == 1
322
- hts_itr_multi_next(htsfp, itr, r)
323
- else
324
- hts_itr_next(htsfp[:fp][:bgzf], itr, r, FFI::Pointer::NULL)
325
- end
326
- end
327
-
328
- # Load a BCF index
329
- def bcf_index_load(fn)
330
- hts_idx_load(fn, HTS_FMT_CSI)
331
- end
332
-
333
- # Load a BCF index
334
- def bcf_index_seqnames(idx, hdr, nptr)
335
- hts_idx_seqnames(idx, nptr, @@bcf_hdr_id2name, hdr)
336
- end
337
-
338
- # Typed value I/O
339
- # INT8_MIN + 1
340
- def bcf_int8_vector_end = -127
341
- # INT16_MIN + 1
342
- def bcf_int16_vector_end = -32_767
343
- # INT32_MIN + 1
344
- def bcf_int32_vector_end = -2_147_483_647
345
- # INT64_MIN + 1
346
- def bcf_int64_vector_end = -9_223_372_036_854_775_807
347
- def bcf_str_vector_end = 0
348
- # INT8_MIN
349
- def bcf_int8_missing = -128
350
- # INT16_MIN
351
- def bcf_int16_missing = (-32_767 - 1)
352
- # INT32_MIN
353
- def bcf_int32_missing = (-2_147_483_647 - 1)
354
- # INT64_MIN
355
- def bcf_int64_missing = (-9_223_372_036_854_775_807 - 1)
356
- def bcf_str_missing = 0x07
357
-
358
- BCF_MAX_BT_INT8 = 0x7f # INT8_MAX */
359
- BCF_MAX_BT_INT16 = 0x7fff # INT16_MAX */
360
- BCF_MAX_BT_INT32 = 0x7fffffff # INT32_MAX */
361
- BCF_MIN_BT_INT8 = -120 # INT8_MIN + 8 */
362
- BCF_MIN_BT_INT16 = -32_760 # INT16_MIN + 8 */
363
- BCF_MIN_BT_INT32 = -2_147_483_640 # INT32_MIN + 8 */
364
- end
365
- end
366
- end
data/lib/hts/libhts.rb DELETED
@@ -1,47 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "ffi_ext/struct"
4
- require_relative "ffi_ext/pointer"
5
-
6
- module HTS
7
- # A Module for working with native C HTSlib structures and functions.
8
- module LibHTS
9
- extend FFI::Library
10
-
11
- begin
12
- ffi_lib HTS.lib_path
13
- rescue LoadError => e
14
- raise LoadError, "#{e}\nCould not find #{HTS.lib_path}"
15
- end
16
-
17
- # @!macro attach_function
18
- # @!scope class
19
- # @!method $1(${2--2})
20
- # @return [${-1}] the return value of $0
21
- def self.attach_function(*)
22
- super
23
- rescue FFI::NotFoundError => e
24
- warn e.message if $VERBOSE
25
- end
26
-
27
- def self.attach_variable(*)
28
- super
29
- rescue FFI::NotFoundError => e
30
- warn e.message if $VERBOSE
31
- end
32
- end
33
- end
34
-
35
- require_relative "libhts/constants"
36
-
37
- # This is alphabetical order.
38
- require_relative "libhts/kfunc"
39
- require_relative "libhts/bgzf"
40
- require_relative "libhts/hfile"
41
- require_relative "libhts/hts"
42
- require_relative "libhts/sam"
43
- require_relative "libhts/cram"
44
- require_relative "libhts/vcf"
45
- require_relative "libhts/tbx"
46
- require_relative "libhts/fai"
47
- require_relative "libhts/thread_pool"