htslib 0.2.3 → 0.2.6

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.
data/lib/hts/hts.rb CHANGED
@@ -31,8 +31,8 @@ module HTS
31
31
  end
32
32
  end
33
33
 
34
- def initialize(*args)
35
- # do nothing
34
+ def initialize(*_args)
35
+ raise TypeError, "Can't make instance of HTS abstract class"
36
36
  end
37
37
 
38
38
  def struct
@@ -69,6 +69,11 @@ module HTS
69
69
  @hts_file.nil? || @hts_file.null?
70
70
  end
71
71
 
72
+ def fai=(fai)
73
+ check_closed
74
+ LibHTS.hts_set_fai_filename(@hts_file, fai) > 0 || raise
75
+ end
76
+
72
77
  def set_threads(n = nil)
73
78
  if n.nil?
74
79
  require "etc"
@@ -107,14 +112,12 @@ module HTS
107
112
  end
108
113
 
109
114
  def rewind
110
- if @start_position
111
- r = seek(@start_position)
112
- raise "Failed to rewind: #{r}" if r < 0
115
+ raise "Cannot rewind: no start position" unless @start_position
113
116
 
114
- tell
115
- else
116
- raise "Cannot rewind: no start position"
117
- end
117
+ r = seek(@start_position)
118
+ raise "Failed to rewind: #{r}" if r < 0
119
+
120
+ tell
118
121
  end
119
122
 
120
123
  private
@@ -158,7 +158,7 @@ module HTS
158
158
  :specific, :pointer
159
159
  end
160
160
 
161
- class HtsIdx < FFI::Struct
161
+ class HtsIdx < FFI::Struct # FIXME: ManagedStruct
162
162
  layout \
163
163
  :fmt, :int,
164
164
  :min_shift, :int,
@@ -188,6 +188,10 @@ module HTS
188
188
  :n_mapped, :uint64,
189
189
  :n_unmapped, :uint64
190
190
  )
191
+
192
+ def self.release(ptr)
193
+ LibHTS.hts_idx_destroy(ptr) unless ptr.null?
194
+ end
191
195
  end
192
196
 
193
197
  class HtsReglist < FFI::Struct
@@ -201,7 +205,7 @@ module HTS
201
205
  end
202
206
 
203
207
  # HtsFile
204
- class SamHdr < FFI::Struct
208
+ class SamHdr < FFI::ManagedStruct
205
209
  layout \
206
210
  :n_targets, :int32,
207
211
  :ignore_sam_err, :int32,
@@ -213,6 +217,10 @@ module HTS
213
217
  :sdict, :pointer,
214
218
  :hrecs, :pointer,
215
219
  :ref_count, :uint32
220
+
221
+ def self.release(ptr)
222
+ LibHTS.sam_hdr_destroy(ptr) unless ptr.null?
223
+ end
216
224
  end
217
225
 
218
226
  BamHdr = SamHdr
@@ -247,10 +255,14 @@ module HTS
247
255
 
248
256
  SamFile = HtsFile
249
257
 
250
- class HtsTpool < FFI::Struct
258
+ class HtsTpool < FFI::ManagedStruct
251
259
  layout \
252
260
  :pool, :pointer,
253
261
  :qsize, :int
262
+
263
+ def self.release(ptr)
264
+ LibHTS.hts_tpool_destroy(ptr) unless ptr.null?
265
+ end
254
266
  end
255
267
 
256
268
  class HtsOpt < FFI::Struct
@@ -265,7 +277,7 @@ module HTS
265
277
  :next, HtsOpt.ptr
266
278
  end
267
279
 
268
- class HtsItr < FFI::BitStruct
280
+ class HtsItr < FFI::BitStruct # FIXME: ManagedBitStruct
269
281
  layout \
270
282
  :_flags, :uint32, # bit_fields
271
283
  :tid, :int,
@@ -300,6 +312,10 @@ module HTS
300
312
  :nocoor, 1,
301
313
  :multi, 1,
302
314
  :dummy, 27
315
+
316
+ def self.release(ptr)
317
+ LibHTS.hts_itr_destroy(ptr) unless ptr.null?
318
+ end
303
319
  end
304
320
 
305
321
  class Bam1Core < FFI::Struct
@@ -342,14 +358,14 @@ module HTS
342
358
  class BamPileupCd < FFI::Union
343
359
  layout \
344
360
  :p, :pointer,
345
- :i, :int64_t,
361
+ :i, :int64,
346
362
  :f, :double
347
363
  end
348
364
 
349
365
  class BamPileup1 < FFI::BitStruct
350
366
  layout \
351
367
  :b, Bam1.ptr,
352
- :qpos, :int32_t,
368
+ :qpos, :int32,
353
369
  :indel, :int,
354
370
  :level, :int,
355
371
  :_flags, :uint32, # bit_fields
@@ -375,24 +391,32 @@ module HTS
375
391
  :line_skip, :int32
376
392
  end
377
393
 
378
- class Tbx < FFI::Struct
394
+ class Tbx < FFI::ManagedStruct
379
395
  layout \
380
- :conf, TbxConf.ptr,
396
+ :conf, TbxConf,
381
397
  :idx, HtsIdx.ptr,
382
398
  :dict, :pointer
399
+
400
+ def self.release(ptr)
401
+ LibHTS.tbx_destroy(ptr) unless ptr.null?
402
+ end
383
403
  end
384
404
 
385
405
  # faidx
386
406
 
387
407
  FaiFormatOptions = enum(:FAI_NONE, :FAI_FASTA, :FAI_FASTQ)
388
408
 
389
- class Faidx < FFI::Struct
390
- layout :bgzf, BGZF,
409
+ class Faidx < FFI::Struct # FIXME: ManagedStruct
410
+ layout :bgzf, BGZF.ptr,
391
411
  :n, :int,
392
412
  :m, :int,
393
413
  :name, :pointer,
394
414
  :hash, :pointer,
395
415
  :format, FaiFormatOptions
416
+
417
+ def self.release(ptr)
418
+ LibHTS.fai_destroy(ptr) unless ptr.null?
419
+ end
396
420
  end
397
421
 
398
422
  # bcf
@@ -404,7 +428,7 @@ module HTS
404
428
  end
405
429
 
406
430
  # Complete textual representation of a header line
407
- class BcfHrec < FFI::Struct
431
+ class BcfHrec < FFI::ManagedStruct
408
432
  layout \
409
433
  :type, :int,
410
434
  :key, :string,
@@ -412,6 +436,10 @@ module HTS
412
436
  :nkeys, :int,
413
437
  :keys, :pointer,
414
438
  :vals, :pointer
439
+
440
+ def self.release(ptr)
441
+ LibHTS.bcf_hrec_destroy(ptr) unless ptr.null?
442
+ end
415
443
  end
416
444
 
417
445
  class BcfInfo < FFI::BitStruct
@@ -435,7 +463,7 @@ module HTS
435
463
 
436
464
  class BcfIdinfo < FFI::Struct
437
465
  layout \
438
- :info, [:uint64_t, 3],
466
+ :info, [:uint64, 3],
439
467
  :hrec, [BcfHrec.ptr, 3],
440
468
  :id, :int
441
469
  end
@@ -446,7 +474,7 @@ module HTS
446
474
  :val, BcfIdinfo.ptr
447
475
  end
448
476
 
449
- class BcfHdr < FFI::Struct
477
+ class BcfHdr < FFI::ManagedStruct
450
478
  layout \
451
479
  :n, [:int, 3],
452
480
  :id, [:pointer, 3], # BcfIdpair.ptr
@@ -461,6 +489,10 @@ module HTS
461
489
  :keep_samples, :pointer,
462
490
  :mem, KString,
463
491
  :m, [:int, 3]
492
+
493
+ def self.release(ptr)
494
+ LibHTS.bcf_hdr_destroy(ptr) unless ptr.null?
495
+ end
464
496
  end
465
497
 
466
498
  class BcfFmt < FFI::BitStruct
@@ -504,7 +536,7 @@ module HTS
504
536
  layout \
505
537
  :pos, :hts_pos_t,
506
538
  :rlen, :hts_pos_t,
507
- :rid, :int32_t,
539
+ :rid, :int32,
508
540
  :qual, :float,
509
541
  :_n_info_allele, :uint32,
510
542
  :_n_fmt_sample, :uint32,
@@ -352,7 +352,7 @@ module HTS
352
352
  # As int32_decoded/encode, but from/to blocks instead of cram_fd
353
353
  attach_function \
354
354
  :int32_put_blk,
355
- %i[cram_block int32_t],
355
+ %i[cram_block int32],
356
356
  :int
357
357
 
358
358
  # Returns the refs_t structure used by a cram file handle.
@@ -242,13 +242,13 @@ module HTS
242
242
  [Bam1,
243
243
  :size_t,
244
244
  :string,
245
- :uint16_t,
246
- :int32_t,
245
+ :uint16,
246
+ :int32,
247
247
  :hts_pos_t,
248
- :uint8_t,
248
+ :uint8,
249
249
  :size_t,
250
250
  :string,
251
- :int32_t,
251
+ :int32,
252
252
  :hts_pos_t,
253
253
  :hts_pos_t,
254
254
  :size_t,
@@ -68,3 +68,5 @@ module HTS
68
68
  :void
69
69
  end
70
70
  end
71
+
72
+ require_relative "tbx_funcs"
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module HTS
2
4
  module LibHTS
3
5
  class << self
@@ -6,7 +8,7 @@ module HTS
6
8
  end
7
9
 
8
10
  def tbx_itr_queryi(tbx, tid, beg, end_)
9
- hts_itr_query(tbx[:idx], tid, beg, end_, tbx_readrec)
11
+ hts_itr_query(tbx[:idx], tid, beg, end_, @@tbx_readrec)
10
12
  end
11
13
 
12
14
  def tbx_itr_querys(tbx, s)
@@ -469,7 +469,7 @@ module HTS
469
469
  attach_function \
470
470
  :bcf_fmt_sized_array,
471
471
  [KString, :pointer],
472
- :uint8_t
472
+ :uint8
473
473
 
474
474
  # Encode a variable-length char array in BCF format
475
475
  attach_function \
@@ -294,15 +294,23 @@ module HTS
294
294
  end
295
295
 
296
296
  # Typed value I/O
297
- def bcf_int8_vector_end = -127 # INT8_MIN + 1
298
- def bcf_int16_vector_end = -32_767 # INT16_MIN + 1
299
- def bcf_int32_vector_end = -2_147_483_647 # INT32_MIN + 1
300
- def bcf_int64_vector_end = -9_223_372_036_854_775_807 # INT64_MIN + 1
297
+ # INT8_MIN + 1
298
+ def bcf_int8_vector_end = -127
299
+ # INT16_MIN + 1
300
+ def bcf_int16_vector_end = -32_767
301
+ # INT32_MIN + 1
302
+ def bcf_int32_vector_end = -2_147_483_647
303
+ # INT64_MIN + 1
304
+ def bcf_int64_vector_end = -9_223_372_036_854_775_807
301
305
  def bcf_str_vector_end = 0
302
- def bcf_int8_missing = -128 # INT8_MIN
303
- def bcf_int16_missing = (-32_767 - 1) # INT16_MIN
304
- def bcf_int32_missing = (-2_147_483_647 - 1) # INT32_MIN
305
- def bcf_int64_missing = (-9_223_372_036_854_775_807 - 1) # INT64_MIN
306
+ # INT8_MIN
307
+ def bcf_int8_missing = -128
308
+ # INT16_MIN
309
+ def bcf_int16_missing = (-32_767 - 1)
310
+ # INT32_MIN
311
+ def bcf_int32_missing = (-2_147_483_647 - 1)
312
+ # INT64_MIN
313
+ def bcf_int64_missing = (-9_223_372_036_854_775_807 - 1)
306
314
  def bcf_str_missing = 0x07
307
315
 
308
316
  BCF_MAX_BT_INT8 = 0x7f # INT8_MAX */
data/lib/hts/tbx.rb CHANGED
@@ -45,8 +45,6 @@ module HTS
45
45
 
46
46
  # build_index(index) if build_index
47
47
  @idx = load_index(index)
48
-
49
- super # do nothing
50
48
  end
51
49
 
52
50
  def build_index
@@ -61,6 +59,10 @@ module HTS
61
59
  end
62
60
  end
63
61
 
62
+ def index_loaded?
63
+ !@idx.null?
64
+ end
65
+
64
66
  def tid(name)
65
67
  LibHTS.tbx_name2id(@idx, name)
66
68
  end
@@ -68,10 +70,53 @@ module HTS
68
70
  def seqnames
69
71
  nseq = FFI::MemoryPointer.new(:int)
70
72
  LibHTS.tbx_seqnames(@idx, nseq).then do |pts|
71
- pts.read_array_of_pointer(nseq.read_int).map do |pt|
72
- pt.read_string
73
- end
73
+ pts.read_array_of_pointer(nseq.read_int).map(&:read_string)
74
+ end
75
+ end
76
+
77
+ def query(region, start = nil, end_ = nil, &block)
78
+ check_closed
79
+ raise "Index file is required to call the query method." unless index_loaded?
80
+
81
+ if start && end_
82
+ queryi(tid(region), start, end_, &block)
83
+ else
84
+ querys(region, &block)
85
+ end
86
+ end
87
+
88
+ private
89
+
90
+ def queryi(id, start, end_)
91
+ return to_enum(__method__, id, start, end_) unless block_given?
92
+
93
+ qiter = LibHTS.tbx_itr_queryi(@idx, id, start, end_)
94
+
95
+ raise "Failed to query region: #{id}:#{start}-#{end_}" if qiter.null?
96
+
97
+ r = LibHTS::KString.new
98
+ begin
99
+ yield r[:s] while LibHTS.tbx_itr_next(@hts_file, @idx, qiter, r) > 0
100
+ ensure
101
+ LibHTS.hts_itr_destroy(qiter)
102
+ end
103
+ self
104
+ end
105
+
106
+ def querys(region)
107
+ return to_enum(__method__, region) unless block_given?
108
+
109
+ qiter = LibHTS.tbx_itr_querys(@idx, region)
110
+
111
+ raise "Failed to query region: #{region}" if qiter.null?
112
+
113
+ r = LibHTS::KString.new
114
+ begin
115
+ yield r[:s].split("\t") while LibHTS.tbx_itr_next(@hts_file, @idx, qiter, r) > 0
116
+ ensure
117
+ LibHTS.hts_itr_destroy(qiter)
74
118
  end
119
+ self
75
120
  end
76
121
  end
77
122
  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.2.3"
4
+ VERSION = "0.2.6"
5
5
  end
data/lib/htslib.rb CHANGED
@@ -52,6 +52,7 @@ module HTS
52
52
  # before calling the LibHTS module.
53
53
  autoload :LibHTS, "hts/libhts"
54
54
 
55
+ autoload :Hts, "hts/hts"
55
56
  autoload :Bam, "hts/bam"
56
57
  autoload :Bcf, "hts/bcf"
57
58
  autoload :Tbx, "hts/tbx"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: htslib
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - kojix2
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-09-03 00:00:00.000000000 Z
11
+ date: 2023-01-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ffi
@@ -131,11 +131,13 @@ extra_rdoc_files: []
131
131
  files:
132
132
  - LICENSE.txt
133
133
  - README.md
134
+ - TUTORIAL.md
134
135
  - lib/hts/bam.rb
135
136
  - lib/hts/bam/auxi.rb
136
137
  - lib/hts/bam/cigar.rb
137
138
  - lib/hts/bam/flag.rb
138
139
  - lib/hts/bam/header.rb
140
+ - lib/hts/bam/header_record.rb
139
141
  - lib/hts/bam/record.rb
140
142
  - lib/hts/bcf.rb
141
143
  - lib/hts/bcf/format.rb
@@ -144,6 +146,7 @@ files:
144
146
  - lib/hts/bcf/info.rb
145
147
  - lib/hts/bcf/record.rb
146
148
  - lib/hts/faidx.rb
149
+ - lib/hts/faidx/sequence.rb
147
150
  - lib/hts/ffi_ext/README.md
148
151
  - lib/hts/ffi_ext/pointer.rb
149
152
  - lib/hts/ffi_ext/struct.rb
@@ -186,7 +189,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
186
189
  - !ruby/object:Gem::Version
187
190
  version: '0'
188
191
  requirements: []
189
- rubygems_version: 3.3.7
192
+ rubygems_version: 3.4.1
190
193
  signing_key:
191
194
  specification_version: 4
192
195
  summary: HTSlib bindings for Ruby