htslib 0.0.10 → 0.2.1

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: 2ecf4c183a1720313371e493fbf7876a08d17639ad853b1d26faeff3addb7c6e
4
- data.tar.gz: cd1ed908a5dd8be184c8d9195c6fb5fabda5487d3c35e11476d9bd6d15c7dae8
3
+ metadata.gz: d26f2d2af0e353b349235ec153e4127288ecd602362254dafd91e015f61dc0a2
4
+ data.tar.gz: 567d03329907f3f53424511f74a4c9fd6f745ef7c2b0c6c14e8dff6da957dbdb
5
5
  SHA512:
6
- metadata.gz: 4980914c7b92528055d2d0cf62fad6ca5edcde8ba1b9d426740538aee42fab53c7ef96ebe53adcaa9663368f024546c048b71760b68bd88e7cbb7647e200847c
7
- data.tar.gz: a94b5d01702cc9873fdf6d1498658cff15c44ba8c90b08cafe514a883560c6aaa90cfbd83cede8a4ae1a5095c6f99710e76a8aacb0e0851e628c607c37719bd5
6
+ metadata.gz: 4c51ee0ed3c259da9e5e7353ae92bcbc7faad622453c8875ff749e8bb0a861c6c28fa09df7dac79ca37d15a0e6f83afb200d4065028e7da63910eab8f9c6e7a4
7
+ data.tar.gz: c62b5c7e95ba91a9f465b6822170c0faa05cf9626af8c63c486ecb55edb1af7f3d841cfa8b2d5adcd9cd1ac6df75088b85ea3145ae78a645eb2b7e3390ca11bd
data/README.md CHANGED
@@ -6,15 +6,15 @@
6
6
  [![DOI](https://zenodo.org/badge/247078205.svg)](https://zenodo.org/badge/latestdoi/247078205)
7
7
  [![Docs Stable](https://img.shields.io/badge/docs-stable-blue.svg)](https://rubydoc.info/gems/htslib)
8
8
 
9
- Ruby-htslib is the [Ruby](https://www.ruby-lang.org) bindings to [HTSlib](https://github.com/samtools/htslib), a C library for high-throughput sequencing data formats. It allows you to read and write file formats commonly used in genomics, such as [SAM, BAM, VCF, and BCF](http://samtools.github.io/hts-specs/) in the Ruby language.
9
+ Ruby-htslib is the [Ruby](https://www.ruby-lang.org) bindings to [HTSlib](https://github.com/samtools/htslib), a C library for high-throughput sequencing data formats. It allows you to read and write file formats commonly used in genomics, such as [SAM, BAM, VCF, and BCF](http://samtools.github.io/hts-specs/), in the Ruby language.
10
10
 
11
11
  :apple: Feel free to fork it out if you can develop it!
12
12
 
13
- :bowtie: alpha stage.
13
+ :bowtie: Alpha stage.
14
14
 
15
15
  ## Requirements
16
16
 
17
- * [Ruby](https://github.com/ruby/ruby) 2.7 or above.
17
+ * [Ruby](https://github.com/ruby/ruby) 3.1 or above.
18
18
  * [HTSlib](https://github.com/samtools/htslib)
19
19
  * Ubuntu : `apt install libhts-dev`
20
20
  * macOS : `brew install htslib`
@@ -27,7 +27,7 @@ gem install htslib
27
27
  ```
28
28
 
29
29
  If you have installed htslib with apt on Ubuntu or homebrew on Mac, [pkg-config](https://github.com/ruby-gnome/pkg-config)
30
- will automatically detect the location of the shared library.
30
+ will automatically detect the location of the shared library. If pkg-config does not work well, set `PKG_CONFIG_PATH`.
31
31
  Alternatively, you can specify the directory of the shared library by setting the environment variable `HTSLIBDIR`.
32
32
 
33
33
  ```sh
@@ -43,21 +43,21 @@ Read SAM / BAM / CRAM - Sequence Alignment Map file
43
43
  ```ruby
44
44
  require 'htslib'
45
45
 
46
- bam = HTS::Bam.open("a.bam")
46
+ bam = HTS::Bam.open("test/fixtures/moo.bam")
47
47
 
48
48
  bam.each do |r|
49
- p name: r.qname,
50
- flag: r.flag,
51
- chr: r.chrom,
52
- pos: r.start + 1,
53
- mqual: r.mapping_quality,
54
- cigar: r.cigar.to_s,
55
- mchr: r.mate_chrom,
56
- mpos: r.mate_start + 1,
57
- isize: r.insert_size,
58
- seq: r.sequence,
59
- qual: r.base_qualities.map { |i| (i + 33).chr }.join,
60
- tagMC: r.tag("MC")
49
+ pp name: r.qname,
50
+ flag: r.flag,
51
+ chrm: r.chrom,
52
+ strt: r.pos + 1,
53
+ mapq: r.mapq,
54
+ cigr: r.cigar.to_s,
55
+ mchr: r.mate_chrom,
56
+ mpos: r.mpos + 1,
57
+ isiz: r.isize,
58
+ seqs: r.seq,
59
+ qual: r.qual.map { |i| (i + 33).chr }.join,
60
+ MC: r.aux("MC")
61
61
  end
62
62
 
63
63
  bam.close
@@ -75,17 +75,30 @@ bcf.each do |r|
75
75
  qual: r.qual.round(2),
76
76
  ref: r.ref,
77
77
  alt: r.alt,
78
- filter: r.filter
79
- # info: r.info
80
- # format: r.format
78
+ filter: r.filter,
79
+ info: r.info.to_h,
80
+ format: r.format.to_h
81
81
  end
82
82
 
83
83
  bcf.close
84
84
  ```
85
85
 
86
+ <details>
87
+ <summary><b>Faidx</b></summary>
88
+
89
+ ```ruby
90
+ fa = HTS::Faidx.open("c.fa")
91
+
92
+ fa.fetch("chr1:1-10")
93
+
94
+ fa.close
95
+ ```
96
+
97
+ </details>
98
+
86
99
  ### Low level API
87
100
 
88
- `HTS::LibHTS` provides native C functions.
101
+ `HTS::LibHTS` provides native C functions.
89
102
 
90
103
  ```ruby
91
104
  require 'htslib'
@@ -96,11 +109,11 @@ p b[:category]
96
109
  p b[:format]
97
110
  ```
98
111
 
99
- Note: htslib makes extensive use of macro functions for speed. you cannot use C macro functions in Ruby if they are not reimplemented in ruby-htslib. Only small number of C structs are implemented with FFI's ManagedStruct, which frees memory when Ruby's garbage collection fires. Other structs will need to be freed manually.
112
+ Note: htslib makes extensive use of macro functions for speed. You cannot use C macro functions in Ruby if they are not reimplemented in ruby-htslib. Only small number of C structs are implemented with FFI's ManagedStruct, which frees memory when Ruby's garbage collection fires. Other structs will need to be freed manually.
100
113
 
101
114
  ### Need more speed?
102
115
 
103
- Try Crystal. [htslib.cr](https://github.com/bio-crystal/htslib.cr) is implemented in Crystal language and provides an API compatible with ruby-htslib. Crsytal language is not as flexible as Ruby language. You can not use eval methods, and you must always be aware of the types. It is not very suitable for writing one-time scripts or experimenting with different code. However, If you have already written code in ruby-htslib, have a clear idea of the manipulations you want to do, and need to execute them many times, then by all means try to implement the command line tool using htslib.cr. The Crystal language is very fast and can perform almost as well as the Rust and C languages.
116
+ Try Crystal. [htslib.cr](https://github.com/bio-crystal/htslib.cr) is implemented in Crystal language and provides an API compatible with ruby-htslib. Crystal language is not as flexible as Ruby language. You can not use `eval` methods, and you must always be careful with the data types. Writing one-time scripts in Crystal or playing with REPL may not be as much fun. However, if you have a clear idea of what you want to do in your mind, have already written code in Ruby, and need to run them over and over, try creating a command line tool in Crystal. The Crystal language is fast, as fast as the Rust and C languages. It will give you great power to create tools.
104
117
 
105
118
  ## Documentation
106
119
 
@@ -123,9 +136,12 @@ bundle exec rake test
123
136
 
124
137
  Many macro functions are used in HTSlib. Since these macro functions cannot be called using FFI, they must be reimplemented in Ruby.
125
138
 
126
- * Actively use the advanced features of Ruby.
127
- * Remain compatibile with [htslib.cr](https://github.com/bio-crystal/htslib.cr).
128
- * The most difficult part is the return value. In the Crystal language, it is convenient for a method to return only one type. In the Ruby language, on the other hand, it is more convenient to return multiple classes. For example, in the Crystal language, it is confusing that a return value can take four types: Int32, Float32, Nil, and String. In Ruby, on the other hand, it is very common and does not cause any problems.
139
+ * Use the new version of Ruby to take full advantage of Ruby's potential.
140
+ * This is possible because we have a small number of users. What a deal!
141
+ * Remain compatible with [htslib.cr](https://github.com/bio-crystal/htslib.cr).
142
+ * The most challenging part is the return value. In the Crystal language, methods are expected to return only one type. On the other hand, in the Ruby language, methods that return multiple classes are very common. For example, in the Crystal language, the compiler gets confused if the return value is one of six types: Int32, Int64, Float32, Float64, Nil, or String. In fact Crystal can do this. But the code gets a little messy. In Ruby, this is very common and doesn't cause any problems.
143
+
144
+ In the script directory, there are several tools to help implement ruby-htslib. These tools may be forked into independent repository in the future.
129
145
 
130
146
  #### FFI Extensions
131
147
 
@@ -145,7 +161,10 @@ Ruby-htslib is a library under development, so even small improvements like typo
145
161
  * Suggest or add new features
146
162
  * [financial contributions](https://github.com/sponsors/kojix2)
147
163
 
164
+
148
165
  ```
166
+ # Ownership and Commitment Rights
167
+
149
168
  Do you need commit rights to ruby-htslib repository?
150
169
  Do you want to get admin rights and take over the project?
151
170
  If so, please feel free to contact us @kojix2.
@@ -153,7 +172,7 @@ If so, please feel free to contact us @kojix2.
153
172
 
154
173
  #### Why do you implement htslib in a language like Ruby, which is not widely used in the bioinformatics?
155
174
 
156
- One of the greatest joys of using a minor language like Ruby in bioinformatics is that there is nothing stopping you from reinventing the wheel. Reinventing the wheel can be fun. But with languages like Python and R, where many bioinformatics masters work, there is no chance left for beginners to create htslib bindings. Bioinformatics file formats, libraries and tools are very complex and I don't know how to understand them. So I wanted to implement the HTSLib binding myself to better understand how the pioneers of bioinformatics felt when establishing the file format and how they created their tools. And that effort is still going on today...
175
+ One of the greatest joys of using a minor language like Ruby in bioinformatics is that there is nothing stopping you from reinventing the wheel. Reinventing the wheel can be fun. But with languages like Python and R, where many bioinformatics masters work, there is no chance left for beginners to create htslib bindings. Bioinformatics file formats, libraries and tools are very complex and I don't know how to understand them. So I wanted to implement the HTSLib binding myself to better understand how the pioneers of bioinformatics felt when establishing the file format and how they created their tools. I hope one day we can work on bioinformatics using Ruby and Crystal languages, not to replace other languages such as Python and R, but to add new power and value to this advancing field.
157
176
 
158
177
  ## Links
159
178
 
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HTS
4
+ class Bam < Hts
5
+ # Auxiliary record data
6
+ class Aux
7
+ def initialize(record)
8
+ @record = record
9
+ end
10
+
11
+ def get(key, type = nil)
12
+ aux = LibHTS.bam_aux_get(@record.struct, key)
13
+ return nil if aux.null?
14
+
15
+ type ||= aux.read_string(1)
16
+
17
+ # A (character), B (general array),
18
+ # f (real number), H (hexadecimal array),
19
+ # i (integer), or Z (string).
20
+
21
+ case type
22
+ when "i", "I", "c", "C", "s", "S"
23
+ LibHTS.bam_aux2i(aux)
24
+ when "f", "d"
25
+ LibHTS.bam_aux2f(aux)
26
+ when "Z", "H"
27
+ LibHTS.bam_aux2Z(aux)
28
+ when "A" # char
29
+ LibHTS.bam_aux2A(aux).chr
30
+ else
31
+ raise NotImplementedError, "type: #{t}"
32
+ end
33
+ end
34
+
35
+ def [](key)
36
+ get(key)
37
+ end
38
+ end
39
+ end
40
+ end
data/lib/hts/bam/cigar.rb CHANGED
@@ -1,20 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Based on hts-python
4
- # https://github.com/quinlan-lab/hts-python
5
-
6
3
  module HTS
7
4
  class Bam < Hts
5
+ # CIGAR string
8
6
  class Cigar
9
7
  include Enumerable
10
8
 
11
9
  def initialize(pointer, n_cigar)
12
- @pointer = pointer
13
10
  @n_cigar = n_cigar
14
- end
15
-
16
- def to_ptr
17
- @pointer
11
+ # Read the pointer before the memory is changed.
12
+ # Especially when called from a block of `each` iterator.
13
+ @c = pointer.read_array_of_uint32(n_cigar)
18
14
  end
19
15
 
20
16
  def to_s
@@ -24,8 +20,7 @@ module HTS
24
20
  def each
25
21
  return to_enum(__method__) unless block_given?
26
22
 
27
- @n_cigar.times do |i|
28
- c = @pointer[i].read_uint32
23
+ @c.each do |c|
29
24
  op = LibHTS.bam_cigar_opchr(c)
30
25
  len = LibHTS.bam_cigar_oplen(c)
31
26
  yield [op, len]
data/lib/hts/bam/flag.rb CHANGED
@@ -5,6 +5,7 @@
5
5
 
6
6
  module HTS
7
7
  class Bam < Hts
8
+ # SAM flags
8
9
  class Flag
9
10
  def initialize(flag_value)
10
11
  raise TypeError unless flag_value.is_a? Integer
@@ -27,70 +28,38 @@ module HTS
27
28
  # BAM_FDUP = 1024
28
29
  # BAM_FSUPPLEMENTARY = 2048
29
30
 
30
- # TODO: Enabling bitwise operations
31
- # hts-nim
32
- # proc `and`*(f: Flag, o: uint16): uint16 {. borrow, inline .}
33
- # proc `and`*(f: Flag, o: Flag): uint16 {. borrow, inline .}
34
- # proc `or`*(f: Flag, o: uint16): uint16 {. borrow .}
35
- # proc `or`*(o: uint16, f: Flag): uint16 {. borrow .}
36
- # proc `==`*(f: Flag, o: Flag): bool {. borrow, inline .}
37
- # proc `==`*(f: Flag, o: uint16): bool {. borrow, inline .}
38
- # proc `==`*(o: uint16, f: Flag): bool {. borrow, inline .}
39
-
40
- def paired?
41
- has_flag? LibHTS::BAM_FPAIRED
42
- end
43
-
44
- def proper_pair?
45
- has_flag? LibHTS::BAM_FPROPER_PAIR
46
- end
47
-
48
- def unmapped?
49
- has_flag? LibHTS::BAM_FUNMAP
50
- end
51
-
52
- def mate_unmapped?
53
- has_flag? LibHTS::BAM_FMUNMAP
54
- end
55
-
56
- def reverse?
57
- has_flag? LibHTS::BAM_FREVERSE
58
- end
59
-
60
- def mate_reverse?
61
- has_flag? LibHTS::BAM_FMREVERSE
62
- end
63
-
64
- def read1?
65
- has_flag? LibHTS::BAM_FREAD1
66
- end
67
-
68
- def read2?
69
- has_flag? LibHTS::BAM_FREAD2
70
- end
71
-
72
- def secondary?
73
- has_flag? LibHTS::BAM_FSECONDARY
74
- end
75
-
76
- def qcfail?
77
- has_flag? LibHTS::BAM_FQCFAIL
78
- end
79
-
80
- def dup?
81
- has_flag? LibHTS::BAM_FDUP
31
+ # TODO: Enabling bitwise operations?
32
+
33
+ TABLE = { paired?: LibHTS::BAM_FPAIRED,
34
+ proper_pair?: LibHTS::BAM_FPROPER_PAIR,
35
+ unmapped?: LibHTS::BAM_FUNMAP,
36
+ mate_unmapped?: LibHTS::BAM_FMUNMAP,
37
+ reverse?: LibHTS::BAM_FREVERSE,
38
+ mate_reverse?: LibHTS::BAM_FMREVERSE,
39
+ read1?: LibHTS::BAM_FREAD1,
40
+ read2?: LibHTS::BAM_FREAD2,
41
+ secondary?: LibHTS::BAM_FSECONDARY,
42
+ qcfail?: LibHTS::BAM_FQCFAIL,
43
+ duplicate?: LibHTS::BAM_FDUP,
44
+ supplementary?: LibHTS::BAM_FSUPPLEMENTARY }.freeze
45
+
46
+ TABLE.each do |name, v|
47
+ define_method(name) do
48
+ has_flag?(v)
49
+ end
82
50
  end
83
51
 
84
- def supplementary?
85
- has_flag? LibHTS::BAM_FSUPPLEMENTARY
52
+ def has_flag?(f)
53
+ (@value & f) != 0
86
54
  end
87
55
 
88
- def has_flag?(m)
89
- (@value & m) != 0
56
+ def to_i
57
+ @value
90
58
  end
91
59
 
92
60
  def to_s
93
- "0x#{format('%x', @value)}\t#{@value}\t#{LibHTS.bam_flag2str(@value)}"
61
+ LibHTS.bam_flag2str(@value)
62
+ # "0x#{format('%x', @value)}\t#{@value}\t#{LibHTS.bam_flag2str(@value)}"
94
63
  end
95
64
  end
96
65
  end
@@ -1,10 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Based on hts-python
4
- # https://github.com/quinlan-lab/hts-python
5
-
6
3
  module HTS
7
4
  class Bam < Hts
5
+ # A class for working with alignment header.
8
6
  class Header
9
7
  def initialize(hts_file)
10
8
  @sam_hdr = LibHTS.sam_hdr_read(hts_file)
@@ -28,7 +26,7 @@ module HTS
28
26
  end
29
27
  end
30
28
 
31
- def target_lengths
29
+ def target_len
32
30
  Array.new(target_count) do |i|
33
31
  LibHTS.sam_hdr_tid2len(@sam_hdr, i)
34
32
  end
@@ -1,10 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Based on hts-python
4
- # https://github.com/quinlan-lab/hts-python
3
+ require_relative "flag"
4
+ require_relative "cigar"
5
+ require_relative "aux"
5
6
 
6
7
  module HTS
7
8
  class Bam < Hts
9
+ # A class for working with alignment records.
8
10
  class Record
9
11
  SEQ_NT16_STR = "=ACMGRSVTWYHKDBN"
10
12
 
@@ -38,26 +40,52 @@ module HTS
38
40
  @bam1[:core][:tid]
39
41
  end
40
42
 
43
+ def tid=(tid)
44
+ @bam1[:core][:tid] = tid
45
+ end
46
+
41
47
  # returns the tid of the mate or -1 if not mapped.
42
- def mate_tid
48
+ def mtid
43
49
  @bam1[:core][:mtid]
44
50
  end
45
51
 
52
+ def mtid=(mtid)
53
+ @bam1[:core][:mtid] = mtid
54
+ end
55
+
46
56
  # returns 0-based start position.
47
- def start
57
+ def pos
48
58
  @bam1[:core][:pos]
49
59
  end
50
60
 
51
- # returns end position of the read.
52
- def stop
53
- LibHTS.bam_endpos @bam1
61
+ def pos=(pos)
62
+ @bam1[:core][:pos] = pos
54
63
  end
55
64
 
56
65
  # returns 0-based mate position
57
- def mate_start
66
+ def mate_pos
58
67
  @bam1[:core][:mpos]
59
68
  end
60
- alias mate_pos mate_start
69
+
70
+ def mate_pos=(mpos)
71
+ @bam1[:core][:mpos] = mpos
72
+ end
73
+
74
+ alias mpos mate_pos
75
+ alias mpos= mate_pos=
76
+
77
+ def bin
78
+ @bam1[:core][:bin]
79
+ end
80
+
81
+ def bin=(bin)
82
+ @bam1[:core][:bin] = bin
83
+ end
84
+
85
+ # returns end position of the read.
86
+ def endpos
87
+ LibHTS.bam_endpos @bam1
88
+ end
61
89
 
62
90
  # returns the chromosome or '' if not mapped.
63
91
  def chrom
@@ -66,37 +94,43 @@ module HTS
66
94
  LibHTS.sam_hdr_tid2name(@header, tid)
67
95
  end
68
96
 
69
- # returns the chromosome or '' if not mapped.
70
- def contig
71
- chrom
72
- end
97
+ alias contig chrom
73
98
 
74
99
  # returns the chromosome of the mate or '' if not mapped.
75
100
  def mate_chrom
76
- mtid = mate_tid
77
101
  return "" if mtid == -1
78
102
 
79
103
  LibHTS.sam_hdr_tid2name(@header, mtid)
80
104
  end
81
105
 
106
+ alias mate_contig mate_chrom
107
+
108
+ # Get strand information.
82
109
  def strand
83
110
  LibHTS.bam_is_rev(@bam1) ? "-" : "+"
84
111
  end
85
112
 
86
- # def start=(v)
87
- # raise 'Not Implemented'
88
- # end
89
-
90
113
  # insert size
91
114
  def insert_size
92
115
  @bam1[:core][:isize]
93
116
  end
94
117
 
118
+ def insert_size=(isize)
119
+ @bam1[:core][:isize] = isize
120
+ end
121
+
122
+ alias isize insert_size
123
+ alias isize= insert_size=
124
+
95
125
  # mapping quality
96
- def mapping_quality
126
+ def mapq
97
127
  @bam1[:core][:qual]
98
128
  end
99
129
 
130
+ def mapq=(mapq)
131
+ @bam1[:core][:qual] = mapq
132
+ end
133
+
100
134
  # returns a `Cigar` object.
101
135
  def cigar
102
136
  Cigar.new(LibHTS.bam_get_cigar(@bam1), @bam1[:core][:n_cigar])
@@ -117,7 +151,7 @@ module HTS
117
151
  end
118
152
 
119
153
  # return the read sequence
120
- def sequence
154
+ def seq
121
155
  r = LibHTS.bam_get_seq(@bam1)
122
156
  seq = String.new
123
157
  (@bam1[:core][:l_qseq]).times do |i|
@@ -125,64 +159,77 @@ module HTS
125
159
  end
126
160
  seq
127
161
  end
162
+ alias sequence seq
163
+
164
+ def len
165
+ @bam1[:core][:l_qseq]
166
+ end
128
167
 
129
168
  # return only the base of the requested index "i" of the query sequence.
130
- def base_at(n)
169
+ def base(n)
131
170
  n += @bam1[:core][:l_qseq] if n < 0
132
- return "." if (n >= @bam1[:core][:l_qseq]) || (n < 0) # eg. base_at(-1000)
171
+ return "." if (n >= @bam1[:core][:l_qseq]) || (n < 0) # eg. base(-1000)
133
172
 
134
173
  r = LibHTS.bam_get_seq(@bam1)
135
174
  SEQ_NT16_STR[LibHTS.bam_seqi(r, n)]
136
175
  end
137
176
 
138
177
  # return the base qualities
139
- def base_qualities
178
+ def qual
140
179
  q_ptr = LibHTS.bam_get_qual(@bam1)
141
180
  q_ptr.read_array_of_uint8(@bam1[:core][:l_qseq])
142
181
  end
143
182
 
144
183
  # return only the base quality of the requested index "i" of the query sequence.
145
- def base_quality_at(n)
184
+ def base_qual(n)
146
185
  n += @bam1[:core][:l_qseq] if n < 0
147
- return 0 if (n >= @bam1[:core][:l_qseq]) || (n < 0) # eg. base_quality_at(-1000)
186
+ return 0 if (n >= @bam1[:core][:l_qseq]) || (n < 0) # eg. base_qual(-1000)
148
187
 
149
188
  q_ptr = LibHTS.bam_get_qual(@bam1)
150
189
  q_ptr.get_uint8(n)
151
190
  end
152
191
 
153
- def flag_str
154
- LibHTS.bam_flag2str(@bam1[:core][:flag])
155
- end
156
-
157
192
  # returns a `Flag` object.
158
193
  def flag
159
194
  Flag.new(@bam1[:core][:flag])
160
195
  end
161
196
 
162
- def tag(str)
163
- aux = LibHTS.bam_aux_get(@bam1, str)
164
- return nil if aux.null?
197
+ def flag=(flag)
198
+ case flag
199
+ when Integer
200
+ @bam1[:core][:flag] = flag
201
+ when Flag
202
+ @bam1[:core][:flag] = flag.value
203
+ else
204
+ raise "Invalid flag type: #{flag.class}"
205
+ end
206
+ end
207
+
208
+ # retruns the auxillary fields.
209
+ def aux(key = nil)
210
+ aux = Aux.new(self)
211
+ if key
212
+ aux.get(key)
213
+ else
214
+ aux
215
+ end
216
+ end
217
+
218
+ # TODO: add a method to get the auxillary fields as a hash.
165
219
 
166
- t = aux.read_string(1)
220
+ # TODO: add a method to set the auxillary fields.
167
221
 
168
- # A (character), B (general array),
169
- # f (real number), H (hexadecimal array),
170
- # i (integer), or Z (string).
222
+ # TODO: add a method to remove the auxillary fields.
171
223
 
172
- case t
173
- when "i", "I", "c", "C", "s", "S"
174
- LibHTS.bam_aux2i(aux)
175
- when "f", "d"
176
- LibHTS.bam_aux2f(aux)
177
- when "Z", "H"
178
- LibHTS.bam_aux2Z(aux)
179
- when "A" # char
180
- LibHTS.bam_aux2A(aux).chr
224
+ # TODO: add a method to set variable length data (qname, cigar, seq, qual).
225
+
226
+ # Calling flag is delegated to the Flag object.
227
+ Flag::TABLE.each_key do |m|
228
+ define_method(m) do
229
+ flag.send(m)
181
230
  end
182
231
  end
183
232
 
184
- # def tags; end
185
-
186
233
  def to_s
187
234
  kstr = LibHTS::KString.new
188
235
  raise "Failed to format bam record" if LibHTS.sam_format1(@header.struct, @bam1, kstr) == -1