htslib 0.0.6 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,292 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HTS
4
+ module LibHTS
5
+ #
6
+ # attach_function \
7
+ # :cram_fd_get_header,
8
+ # [CramFd],
9
+ # SamHdr.by_ref
10
+ #
11
+ # attach_function \
12
+ # :cram_fd_set_header,
13
+ # [CramFd, SamHdr.by_ref],
14
+ # :void
15
+ #
16
+ # attach_function \
17
+ # :cram_fd_get_version,
18
+ # [CramFd],
19
+ # :int
20
+ #
21
+ # attach_function \
22
+ # :cram_fd_set_version,
23
+ # [CramFd, :int],
24
+ # :void
25
+ #
26
+ # attach_function \
27
+ # :cram_major_vers,
28
+ # [CramFd],
29
+ # :int
30
+ #
31
+ # attach_function \
32
+ # :cram_minor_vers,
33
+ # [CramFd],
34
+ # :int
35
+ #
36
+ # attach_function \
37
+ # :cram_fd_get_fp,
38
+ # [CramFd],
39
+ # HFile.by_ref
40
+ #
41
+ # attach_function \
42
+ # :cram_fd_set_fp,
43
+ # [CramFd, HFile],
44
+ # :void
45
+ #
46
+ # attach_function \
47
+ # :cram_container_get_length,
48
+ # [CramContainer],
49
+ # :int32
50
+ #
51
+ # attach_function \
52
+ # :cram_container_set_length,
53
+ # [CramContainer, :int32],
54
+ # :void
55
+ #
56
+ # attach_function \
57
+ # :cram_container_get_num_blocks,
58
+ # [CramContainer],
59
+ # :int32
60
+ #
61
+ # attach_function \
62
+ # :cram_container_set_num_blocks,
63
+ # [CramContainer, :int32],
64
+ # :void
65
+ #
66
+ # attach_function \
67
+ # :cram_container_get_landmarks,
68
+ # [CramContainer, :int32],
69
+ # :pointer
70
+ #
71
+ # attach_function \
72
+ # :cram_container_set_landmarks,
73
+ # [CramContainer, :Int32, :pointer],
74
+ # :void
75
+ #
76
+ # attach_function \
77
+ # :cram_container_is_empty,
78
+ # [CramFd],
79
+ # :int
80
+ #
81
+ # attach_function \
82
+ # :cram_block_get_content_id,
83
+ # [CramBlock],
84
+ # :int32
85
+ #
86
+ # attach_function \
87
+ # :cram_block_get_comp_size,
88
+ # [CramBlock],
89
+ # :int32
90
+ #
91
+ # attach_function \
92
+ # :cram_block_get_uncomp_size,
93
+ # [CramBlock],
94
+ # :int32
95
+ #
96
+ # attach_function \
97
+ # :cram_block_get_crc32,
98
+ # [CramBlock],
99
+ # :int32
100
+ #
101
+ # attach_function \
102
+ # :cram_block_get_data,
103
+ # [CramBlock],
104
+ # :pointer
105
+ #
106
+ # attach_function \
107
+ # :cram_block_get_content_type,
108
+ # [CramBlock],
109
+ # CramContentType # ?
110
+ #
111
+ # attach_function \
112
+ # :cram_block_set_content_id,
113
+ # [CramBlock, :int32],
114
+ # :void
115
+ #
116
+ # attach_function \
117
+ # :cram_block_set_comp_size,
118
+ # [CramBlock, :int32],
119
+ # :void
120
+ #
121
+ # attach_function \
122
+ # :cram_block_set_uncomp_size,
123
+ # [CramBlock, :int32],
124
+ # :void
125
+ #
126
+ # attach_function \
127
+ # :cram_block_set_crc32,
128
+ # [CramBlock, :int32],
129
+ # :void
130
+ #
131
+ # attach_function \
132
+ # :cram_block_set_data,
133
+ # [CramBlock, :pointer],
134
+ # :void
135
+ #
136
+ # attach_function \
137
+ # :cram_block_append,
138
+ # [CramBlock, :pointer, :int],
139
+ # :int
140
+ #
141
+ # attach_function \
142
+ # :cram_block_update_size,
143
+ # [CramBlock],
144
+ # :void
145
+ #
146
+ # attach_function \
147
+ # :cram_block_get_offset,
148
+ # [CramBlock],
149
+ # :size_t
150
+ #
151
+ # attach_function \
152
+ # :cram_block_set_offset,
153
+ # [CramBlock, :size_t],
154
+ # :void
155
+ #
156
+ # attach_function \
157
+ # :cram_block_size,
158
+ # [CramBlock],
159
+ # :uint32
160
+ #
161
+ # attach_function \
162
+ # :cram_transcode_rg,
163
+ # [CramFd, CramFd, CramContainer, :int, :pointer, :pointer],
164
+ # :int
165
+ #
166
+ # attach_function \
167
+ # :cram_copy_slice,
168
+ # [CramFd, CramFd, :int32],
169
+ # :int
170
+ #
171
+ # attach_function \
172
+ # :cram_new_block,
173
+ # [CramContentType, :int],
174
+ # CramBlock
175
+ #
176
+ # attach_function \
177
+ # :cram_read_block,
178
+ # [CramFd],
179
+ # CramBlock
180
+ #
181
+ # attach_function \
182
+ # :cram_write_block,
183
+ # [CramFd, CramBlock],
184
+ # :int
185
+ #
186
+ # attach_function \
187
+ # :cram_free_block,
188
+ # [CramBlock],
189
+ # :void
190
+ #
191
+ # attach_function \
192
+ # :cram_uncompress_block,
193
+ # [CramBlock],
194
+ # :int
195
+ #
196
+ # attach_function \
197
+ # :cram_compress_block,
198
+ # [CramFd, CramBlock, CramMetrics, :int, :int],
199
+ # :int
200
+ #
201
+ # attach_function \
202
+ # :cram_compress_block2,
203
+ # [CramFd, CramSlice, CramBlock, CramMetrics, :int, :int],
204
+ # :int
205
+ #
206
+ # attach_function \
207
+ # :cram_new_container,
208
+ # [:int, :int],
209
+ # CramContainer
210
+ #
211
+ # attach_function \
212
+ # :cram_free_container,
213
+ # [CramContainer],
214
+ # :void
215
+ #
216
+ # attach_function \
217
+ # :cram_read_container,
218
+ # [CramFd],
219
+ # CramContainer
220
+ #
221
+ # attach_function \
222
+ # :cram_write_container,
223
+ # [CramFd, CramContainer],
224
+ # :int
225
+ #
226
+ # attach_function \
227
+ # :cram_store_container,
228
+ # [CramFd, CramContainer, :string, :pointer],
229
+ # :int
230
+ #
231
+ # attach_function \
232
+ # :cram_container_size,
233
+ # [CramContainer],
234
+ # :int
235
+ #
236
+ # attach_function \
237
+ # :cram_open,
238
+ # [:string, :string],
239
+ # CramFd
240
+ #
241
+ # attach_function \
242
+ # :cram_dopen,
243
+ # [:pointer, :string, :string],
244
+ # CramFd
245
+ #
246
+ # attach_function \
247
+ # :cram_close,
248
+ # [CramFd],
249
+ # :int
250
+ #
251
+
252
+ attach_function \
253
+ :cram_seek,
254
+ %i[pointer off_t int], :int # FIXME: pointer should be CramFd
255
+
256
+ #
257
+ # attach_function \
258
+ # :cram_flush,
259
+ # [CramFd],
260
+ # :int
261
+ #
262
+ # attach_function \
263
+ # :cram_eof,
264
+ # [CramFd],
265
+ # :int
266
+ #
267
+ # attach_function \
268
+ # :cram_set_option,
269
+ # [CramFd, HtsFmtOption, ...], # vararg!
270
+ # :int
271
+ #
272
+ # attach_function \
273
+ # :cram_set_voption,
274
+ # [CramFd, HtsFmtOption, VaList],
275
+ # :int
276
+ #
277
+ # attach_function \
278
+ # :cram_set_header,
279
+ # [CramFd, SamHdr.by_ref],
280
+ # :int
281
+ #
282
+ # attach_function \
283
+ # :cram_check_eof = :cram_check_EOF,
284
+ # [CramFd], :int
285
+ #
286
+ # attach_function \
287
+ # :cram_get_refs,
288
+ # [HtsFile],
289
+ # RefsT # what is RefsT
290
+ #
291
+ end
292
+ end
@@ -6,13 +6,13 @@ module HTS
6
6
  attach_function \
7
7
  :hopen,
8
8
  %i[string string varargs],
9
- :HFILE
9
+ HFILE.by_ref
10
10
 
11
11
  # Associate a stream with an existing open file descriptor
12
12
  attach_function \
13
13
  :hdopen,
14
14
  %i[int string],
15
- :HFILE
15
+ HFILE.by_ref
16
16
 
17
17
  # Report whether the file name or URL denotes remote storage
18
18
  attach_function \
@@ -29,55 +29,63 @@ module HTS
29
29
  # Flush (for output streams) and close the stream
30
30
  attach_function \
31
31
  :hclose,
32
- [:HFILE],
32
+ [HFILE],
33
33
  :int
34
34
 
35
35
  # Close the stream, without flushing or propagating errors
36
36
  attach_function \
37
37
  :hclose_abruptly,
38
- [:HFILE],
38
+ [HFILE],
39
39
  :void
40
40
 
41
41
  # Reposition the read/write stream offset
42
42
  attach_function \
43
43
  :hseek,
44
- %i[HFILE off_t int],
44
+ [HFILE, :off_t, :int],
45
45
  :off_t
46
46
 
47
+ # Report the current stream offset
48
+ def self.htell(fp)
49
+ # TODO: This is a hack. Is this OK?
50
+ bg = FFI::Pointer.new(:int, fp.pointer.address + fp.offset_of(:begin)).read_int
51
+ bf = FFI::Pointer.new(:int, fp.pointer.address + fp.offset_of(:buffer)).read_int
52
+ fp[:offset] + (bg - bf)
53
+ end
54
+
47
55
  # Read from the stream until the delimiter, up to a maximum length
48
56
  attach_function \
49
57
  :hgetdelim,
50
- %i[string size_t int HFILE],
58
+ [:string, :size_t, :int, HFILE],
51
59
  :ssize_t
52
60
 
53
61
  # Read a line from the stream, up to a maximum length
54
62
  attach_function \
55
63
  :hgets,
56
- %i[string int HFILE],
64
+ [:string, :int, HFILE],
57
65
  :string
58
66
 
59
67
  # Peek at characters to be read without removing them from buffers
60
68
  attach_function \
61
69
  :hpeek,
62
- %i[HFILE pointer size_t],
70
+ [HFILE, :pointer, :size_t],
63
71
  :ssize_t
64
72
 
65
73
  # For writing streams, flush buffered output to the underlying stream
66
74
  attach_function \
67
75
  :hflush,
68
- [:HFILE],
76
+ [HFILE],
69
77
  :int
70
78
 
71
79
  # For hfile_mem: get the internal buffer and it's size from a hfile
72
80
  attach_function \
73
81
  :hfile_mem_get_buffer,
74
- %i[HFILE pointer],
82
+ [HFILE, :pointer],
75
83
  :string
76
84
 
77
85
  # For hfile_mem: get the internal buffer and it's size from a hfile.
78
86
  attach_function \
79
87
  :hfile_mem_steal_buffer,
80
- %i[HFILE pointer],
88
+ [HFILE, :pointer],
81
89
  :string
82
90
  end
83
91
  end
@@ -61,7 +61,7 @@ module HTS
61
61
  # Determine format by peeking at the start of a file
62
62
  attach_function \
63
63
  :hts_detect_format,
64
- [:HFILE, HtsFormat],
64
+ [HFILE, HtsFormat],
65
65
  :int
66
66
 
67
67
  # Get a human-readable description of the file format
@@ -86,7 +86,7 @@ module HTS
86
86
  # Open an existing stream as a SAM/BAM/CRAM/VCF/BCF/etc file
87
87
  attach_function \
88
88
  :hts_hopen,
89
- %i[HFILE string string],
89
+ [HFILE, :string, :string],
90
90
  HtsFile.by_ref
91
91
 
92
92
  # For output streams, flush any buffered data
@@ -145,7 +145,7 @@ module HTS
145
145
  # Create extra threads to aid compress/decompression for this file
146
146
  attach_function \
147
147
  :hts_set_thread_pool,
148
- [HtsFile, HtsThreadPool],
148
+ [HtsFile, HtsTpool],
149
149
  :int
150
150
 
151
151
  # Adds a cache of decompressed blocks, potentially speeding up seeks.
@@ -345,7 +345,8 @@ module HTS
345
345
  # Get the next read from a SAM/BAM/CRAM iterator
346
346
  def self.sam_itr_next(htsfp, itr, r)
347
347
  # FIXME: check if htsfp is compressed BGZF
348
- hts_log_error("Null iterator") if itr.null?
348
+ raise("Null iterator") if itr.null?
349
+
349
350
  # FIXME: check multi
350
351
  hts_itr_next(htsfp[:fp][:bgzf], itr, r, htsfp)
351
352
  end
@@ -0,0 +1,139 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HTS
4
+ module LibHTS
5
+ attach_function \
6
+ :hts_tpool_init,
7
+ [:int],
8
+ HtsTpool.by_ref
9
+
10
+ attach_function \
11
+ :hts_tpool_size,
12
+ [HtsTpool],
13
+ :int
14
+
15
+ # FIXME: struct
16
+ HtsTpoolProcess = :pointer
17
+ HtsTpoolResult = :pointer
18
+
19
+ attach_function \
20
+ :hts_tpool_dispatch,
21
+ [HtsTpool, HtsTpoolProcess, :pointer, :pointer],
22
+ :int
23
+
24
+ attach_function \
25
+ :hts_tpool_dispatch2,
26
+ [HtsTpool, HtsTpoolProcess, :pointer, :pointer, :int],
27
+ :int
28
+
29
+ attach_function \
30
+ :hts_tpool_dispatch3,
31
+ [HtsTpool, HtsTpoolProcess, :pointer, :pointer, :pointer, :pointer, :int],
32
+ :int
33
+
34
+ attach_function \
35
+ :hts_tpool_wake_dispatch,
36
+ [HtsTpoolProcess],
37
+ :void
38
+
39
+ attach_function \
40
+ :hts_tpool_process_flush,
41
+ [HtsTpoolProcess],
42
+ :int
43
+
44
+ attach_function \
45
+ :hts_tpool_process_reset,
46
+ [HtsTpoolProcess, :int],
47
+ :int
48
+
49
+ attach_function \
50
+ :hts_tpool_process_qsize,
51
+ [HtsTpoolProcess],
52
+ :int
53
+
54
+ attach_function \
55
+ :hts_tpool_destroy,
56
+ [HtsTpool],
57
+ :void
58
+
59
+ attach_function \
60
+ :hts_tpool_kill,
61
+ [HtsTpool],
62
+ :void
63
+
64
+ attach_function \
65
+ :hts_tpool_next_result,
66
+ [HtsTpoolProcess],
67
+ HtsTpoolResult # .by_ref
68
+
69
+ attach_function \
70
+ :hts_tpool_next_result_wait,
71
+ [HtsTpoolProcess],
72
+ HtsTpoolResult # .by_ref
73
+
74
+ attach_function \
75
+ :hts_tpool_delete_result,
76
+ [HtsTpoolResult, :int],
77
+ :void
78
+
79
+ attach_function \
80
+ :hts_tpool_result_data,
81
+ [HtsTpoolResult],
82
+ :pointer
83
+
84
+ attach_function \
85
+ :hts_tpool_process_init,
86
+ [HtsTpool, :int, :int],
87
+ HtsTpoolProcess # .by_ref
88
+
89
+ attach_function \
90
+ :hts_tpool_process_destroy,
91
+ [HtsTpoolProcess],
92
+ :void
93
+
94
+ attach_function \
95
+ :hts_tpool_process_empty,
96
+ [HtsTpoolProcess],
97
+ :int
98
+
99
+ attach_function \
100
+ :hts_tpool_process_len,
101
+ [HtsTpoolProcess],
102
+ :int
103
+
104
+ attach_function \
105
+ :hts_tpool_process_sz,
106
+ [HtsTpoolProcess],
107
+ :int
108
+
109
+ attach_function \
110
+ :hts_tpool_process_shutdown,
111
+ [HtsTpoolProcess],
112
+ :void
113
+
114
+ attach_function \
115
+ :hts_tpool_process_is_shutdown,
116
+ [HtsTpoolProcess],
117
+ :int
118
+
119
+ attach_function \
120
+ :hts_tpool_process_attach,
121
+ [HtsTpool, HtsTpoolProcess],
122
+ :void
123
+
124
+ attach_function \
125
+ :hts_tpool_process_detach,
126
+ [HtsTpool, HtsTpoolProcess],
127
+ :void
128
+
129
+ attach_function \
130
+ :hts_tpool_process_ref_incr,
131
+ [HtsTpoolProcess],
132
+ :void
133
+
134
+ attach_function \
135
+ :hts_tpool_process_ref_decr,
136
+ [HtsTpoolProcess],
137
+ :void
138
+ end
139
+ end
@@ -221,6 +221,20 @@ module HTS
221
221
  LibHTS::BcfIdpair.size * int_id # offset
222
222
  )[:val][:info][type] >> 12
223
223
  end
224
+
225
+ def bcf_hdr_id2type(hdr, type, int_id)
226
+ LibHTS::BcfIdpair.new(
227
+ hdr[:id][LibHTS::BCF_DT_ID].to_ptr +
228
+ LibHTS::BcfIdpair.size * int_id # offset
229
+ )[:val][:info][type] >> 4 & 0xf
230
+ end
231
+
232
+ def bcf_hdr_id2coltype(hdr, type, int_id)
233
+ LibHTS::BcfIdpair.new(
234
+ hdr[:id][LibHTS::BCF_DT_ID].to_ptr +
235
+ LibHTS::BcfIdpair.size * int_id # offset
236
+ )[:val][:info][type] & 0xf
237
+ end
224
238
  end
225
239
  end
226
240
  end
data/lib/hts/libhts.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "ffi_ext/struct"
4
+ require_relative "ffi_ext/pointer"
4
5
 
5
6
  module HTS
6
7
  module LibHTS
@@ -12,6 +13,9 @@ module HTS
12
13
  raise LoadError, "#{e}\nCould not find #{HTS.lib_path}"
13
14
  end
14
15
 
16
+ # @!macro attach_function
17
+ # @!method $1(${2--2})
18
+ # @return [${-1}] the return value of $0
15
19
  def self.attach_function(*)
16
20
  super
17
21
  rescue FFI::NotFoundError => e
@@ -23,11 +27,13 @@ end
23
27
  require_relative "libhts/constants"
24
28
 
25
29
  # This is alphabetical order.
30
+ require_relative "libhts/kfunc"
26
31
  require_relative "libhts/bgzf"
27
- require_relative "libhts/faidx"
28
32
  require_relative "libhts/hfile"
29
33
  require_relative "libhts/hts"
30
34
  require_relative "libhts/sam"
31
- require_relative "libhts/kfunc"
32
- require_relative "libhts/tbx"
35
+ require_relative "libhts/cram"
33
36
  require_relative "libhts/vcf"
37
+ require_relative "libhts/tbx"
38
+ require_relative "libhts/faidx"
39
+ require_relative "libhts/thread_pool"
data/lib/hts/tbx.rb ADDED
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../htslib"
4
+
5
+ require_relative "hts"
6
+
7
+ module HTS
8
+ class Tbx < Hts
9
+ include Enumerable
10
+
11
+ attr_reader :file_name
12
+
13
+ def self.open(*args, **kw)
14
+ file = new(*args, **kw) # do not yield
15
+ return file unless block_given?
16
+
17
+ begin
18
+ yield file
19
+ ensure
20
+ file.close
21
+ end
22
+ file
23
+ end
24
+
25
+ def initialize(file_name, threads: nil)
26
+ if block_given?
27
+ message = "HTS::Tbx.new() dose not take block; Please use HTS::Tbx.open() instead"
28
+ raise message
29
+ end
30
+
31
+ @file_name = file_name
32
+
33
+ # NOTE: Do not check for the existence of local files, since file_names may be remote URIs.
34
+
35
+ @mode = "r"
36
+ @hts_file = LibHTS.hts_open(@file_name, @mode)
37
+
38
+ raise Errno::ENOENT, "Failed to open #{@file_name}" if @hts_file.null?
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
44
+ end
45
+ end
46
+ 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.0.6"
4
+ VERSION = "0.1.0"
5
5
  end
data/lib/htslib.rb CHANGED
@@ -37,9 +37,9 @@ module HTS
37
37
  # You can change the path of the shared library with `HTS.lib_path=`
38
38
  # before calling the LibHTS module.
39
39
  autoload :LibHTS, "hts/libhts"
40
- end
41
40
 
42
- require_relative "hts/bam"
43
- require_relative "hts/faidx"
44
- require_relative "hts/tabix"
45
- require_relative "hts/bcf"
41
+ autoload :Bam, "hts/bam"
42
+ autoload :Bcf, "hts/bcf"
43
+ autoload :Tbx, "hts/tbx"
44
+ autoload :Faidx, "hts/faidx"
45
+ end