htslib 0.0.5 → 0.0.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3eb6fbb228a9fa0642c55cdfa39d5189b311df191725954ae46b8dda135dc8c7
4
- data.tar.gz: c90ad39aa4919cefa56e534a706bf9659bba9c6ad69eb374491092ce16ae93f6
3
+ metadata.gz: f05bc93a621f3d5fb8d06e44c20ac4a3a95c5f6e243df2aebc97e10125fcb779
4
+ data.tar.gz: 73e747aa0999e54b3c8f01b981b91c4e293167265276a24eac85ea0fd6b85c13
5
5
  SHA512:
6
- metadata.gz: 392ea7c6acb5b86e9e1c1a29f066049c54954edfd277be30541aee433a7349f9d74df09e663ec0269f30d6b87ff47d66a4a668470d3aa536e25a2058cf963546
7
- data.tar.gz: 9cc235f8876b8fd4339593eb84a0f2cb8b605b9c82b624cd0f97691b4109a672f55ae90447bba17fb5efcb8257279ad8c2d87234bca390b1624c7aa8f9a7014e
6
+ metadata.gz: 8d0f3f8bb9f7a9c063222fd2dc6463f58bdfd784aff39757921dda1c8d417023b024ca9f6c92d7f0eecd30b8064adb1b94966cf962cf9e0e56d67303de0a903b
7
+ data.tar.gz: 8ef35b7aef04bf2f51ae0ba110d9ffc81c26d7a0c97a821aa1274c04b31ec29515b33c278391494f6a0d8efc5e2d965060ccd190ee0f92a2ec3562fb59c49169
data/README.md CHANGED
@@ -9,11 +9,12 @@
9
9
  :dna: [HTSlib](https://github.com/samtools/htslib) - for Ruby
10
10
 
11
11
  Ruby-htslib is the Ruby bindings to HTSlib, a C library for processing high throughput sequencing (HTS) data.
12
- It will provide APIs to read and write file formats such as SAM, BAM, VCF, and BCF.
12
+ It will provide APIs to read and write file formats such as [SAM, BAM, VCF, and BCF](http://samtools.github.io/hts-specs/).
13
13
 
14
14
  :apple: Feel free to fork it out if you can develop it!
15
15
 
16
16
  :bowtie: alpha stage.
17
+
17
18
  ## Requirements
18
19
 
19
20
  * [Ruby](https://github.com/ruby/ruby) 2.7 or above.
@@ -92,8 +93,8 @@ bundle exec rake htslib:build
92
93
  bundle exec rake test
93
94
  ```
94
95
 
95
- We plan to actively use the new features of Ruby. Since the number of users is small, backward compatibility is not important.
96
- On the other hand, we will consider compatibility with [Crystal](https://github.com/bio-crystal/htslib.cr) to some extent.
96
+ * Actively use the advanced features of Ruby.
97
+ * Consider compatibility with [htslib.cr](https://github.com/bio-crystal/htslib.cr) to some extent.
97
98
 
98
99
  #### FFI Extensions
99
100
 
data/lib/hts/bam/cigar.rb CHANGED
@@ -22,6 +22,8 @@ module HTS
22
22
  end
23
23
 
24
24
  def each
25
+ return to_enum(__method__) unless block_given?
26
+
25
27
  @n_cigar.times do |i|
26
28
  c = @pointer[i].read_uint32
27
29
  yield [LibHTS.bam_cigar_oplen(c),
data/lib/hts/bam/flag.rb CHANGED
@@ -85,8 +85,12 @@ module HTS
85
85
  has_flag? LibHTS::BAM_FSUPPLEMENTARY
86
86
  end
87
87
 
88
- def has_flag?(o)
89
- @value[o] != 0
88
+ def has_flag?(m)
89
+ (@value & m) != 0
90
+ end
91
+
92
+ def to_s
93
+ "0x#{format('%x', @value)}\t#{@value}\t#{LibHTS.bam_flag2str(@value)}"
90
94
  end
91
95
  end
92
96
  end
@@ -18,6 +18,10 @@ module HTS
18
18
  @sam_hdr.to_ptr
19
19
  end
20
20
 
21
+ def target_count
22
+ @sam_hdr[:n_targets]
23
+ end
24
+
21
25
  # FIXME: better name?
22
26
  def seqs
23
27
  Array.new(@sam_hdr[:n_targets]) do |i|
@@ -25,7 +29,7 @@ module HTS
25
29
  end
26
30
  end
27
31
 
28
- def text
32
+ def to_s
29
33
  LibHTS.sam_hdr_str(@sam_hdr)
30
34
  end
31
35
  end
@@ -69,7 +69,6 @@ module HTS
69
69
 
70
70
  # returns the chromosome or '' if not mapped.
71
71
  def chrom
72
- tid = @bam1[:core][:tid]
73
72
  return "" if tid == -1
74
73
 
75
74
  LibHTS.sam_hdr_tid2name(@header, tid)
@@ -77,10 +76,10 @@ module HTS
77
76
 
78
77
  # returns the chromosome of the mate or '' if not mapped.
79
78
  def mate_chrom
80
- tid = @bam1[:core][:mtid]
81
- return "" if tid == -1
79
+ mtid = mate_tid
80
+ return "" if mtid == -1
82
81
 
83
- LibHTS.sam_hdr_tid2name(@header, tid)
82
+ LibHTS.sam_hdr_tid2name(@header, mtid)
84
83
  end
85
84
 
86
85
  def strand
@@ -168,6 +167,11 @@ module HTS
168
167
  return nil if aux.null?
169
168
 
170
169
  t = aux.read_string(1)
170
+
171
+ # A (character), B (general array),
172
+ # f (real number), H (hexadecimal array),
173
+ # i (integer), or Z (string).
174
+
171
175
  case t
172
176
  when "i", "I", "c", "C", "s", "S"
173
177
  LibHTS.bam_aux2i(aux)
@@ -175,8 +179,8 @@ module HTS
175
179
  LibHTS.bam_aux2f(aux)
176
180
  when "Z", "H"
177
181
  LibHTS.bam_aux2Z(aux)
178
- when "A"
179
- LibHTS.bam_aux2A(aux)
182
+ when "A" # char
183
+ LibHTS.bam_aux2A(aux).chr
180
184
  end
181
185
  end
182
186
 
@@ -36,8 +36,9 @@ module HTS
36
36
  format_values.call(LibHTS::BCF_HT_FLAG)
37
37
  .read_int == 1
38
38
  when :string, :str
39
+ raise NotImplementedError, "String type not implemented yet."
39
40
  format_values.call(LibHTS::BCF_HT_STR)
40
- .read_pointer.read_string
41
+ .read_string
41
42
  end
42
43
  end
43
44
 
@@ -3,16 +3,23 @@
3
3
  module HTS
4
4
  class Bcf
5
5
  class Header
6
- def initialize(h)
7
- @h = h
6
+ def initialize(bcf_hdr)
7
+ @bcf_hdr = bcf_hdr
8
8
  end
9
9
 
10
10
  def struct
11
- @h
11
+ @bcf_hdr
12
12
  end
13
13
 
14
14
  def to_ptr
15
- @h.to_ptr
15
+ @bcf_hdr.to_ptr
16
+ end
17
+
18
+ def to_s
19
+ kstr = LibHTS::KString.new
20
+ raise "Failed to get header string" unless LibHTS.bcf_hdr_format(@bcf_hdr, 0, kstr)
21
+
22
+ kstr[:s]
16
23
  end
17
24
  end
18
25
  end
data/lib/hts/bcf/info.rb CHANGED
@@ -44,6 +44,7 @@ module HTS
44
44
  end
45
45
  end
46
46
 
47
+ # FIXME: naming? room for improvement.
47
48
  def fields
48
49
  n_info = @record.struct[:n_info]
49
50
  Array.new(n_info) do |i|
@@ -16,7 +16,7 @@ module HTS
16
16
  end
17
17
 
18
18
  def to_ptr
19
- @bcf.to_ptr
19
+ @bcf1.to_ptr
20
20
  end
21
21
 
22
22
  # def inspect; end
@@ -64,6 +64,8 @@ module HTS
64
64
  d[:flt].get_array_of_int(0, n_flt).map do |i|
65
65
  LibHTS.bcf_hdr_int2id(@bcf.header.struct, LibHTS::BCF_DT_ID, i)
66
66
  end
67
+ else
68
+ raise "Unexpected number of filters. n_flt: #{n_flt}"
67
69
  end
68
70
  end
69
71
 
@@ -80,13 +82,14 @@ module HTS
80
82
  LibHTS.bcf_unpack(@bcf1, LibHTS::BCF_UN_STR)
81
83
  @bcf1[:d][:allele].get_array_of_pointer(
82
84
  FFI::TYPE_POINTER.size, @bcf1[:n_allele] - 1
83
- ).map { |c| c.read_string }
85
+ ).map(&:read_string)
84
86
  end
85
87
 
86
88
  def alleles
89
+ LibHTS.bcf_unpack(@bcf1, LibHTS::BCF_UN_STR)
87
90
  @bcf1[:d][:allele].get_array_of_pointer(
88
91
  0, @bcf1[:n_allele]
89
- ).map { |c| c.read_string }
92
+ ).map(&:read_string)
90
93
  end
91
94
 
92
95
  def info
data/lib/hts/bcf.rb CHANGED
@@ -66,7 +66,7 @@ module HTS
66
66
  self
67
67
  end
68
68
 
69
- def n_samples
69
+ def sample_count
70
70
  LibHTS.bcf_hdr_nsamples(header.struct)
71
71
  end
72
72
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'ffi/bit_field'
3
+ require "ffi/bit_field"
4
4
 
5
5
  module FFI
6
6
  class Struct
@@ -89,6 +89,12 @@ module HTS
89
89
  %i[HFILE string string],
90
90
  HtsFile.by_ref
91
91
 
92
+ # For output streams, flush any buffered data
93
+ attach_function \
94
+ :hts_flush,
95
+ [HtsFile],
96
+ :int
97
+
92
98
  # Close a file handle, flushing buffered data for output streams
93
99
  attach_function \
94
100
  :hts_close,