Class: HTS::Bcf

Inherits:
Hts
  • Object
show all
Includes:
Enumerable
Defined in:
lib/hts/bcf.rb,
lib/hts/bcf/info.rb,
lib/hts/bcf/format.rb,
lib/hts/bcf/header.rb,
lib/hts/bcf/record.rb,
lib/hts/bcf/header_record.rb

Overview

A class for working with VCF, BCF files.

Defined Under Namespace

Classes: Format, Header, HeaderRecord, Info, Record

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Hts

#closed?, #fai=, #file_format, #file_format_version, #rewind, #seek, #set_threads, #struct, #tell, #to_ptr

Constructor Details

#initialize(file_name, mode = "r", index: nil, threads: nil, build_index: false) ⇒ Bcf

Returns a new instance of Bcf.

Raises:

  • (Errno::ENOENT)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/hts/bcf.rb', line 30

def initialize(file_name, mode = "r", index: nil, threads: nil,
               build_index: false)
  if block_given?
    message = "HTS::Bcf.new() dose not take block; Please use HTS::Bcf.open() instead"
    raise message
  end

  # NOTE: Do not check for the existence of local files, since file_names may be remote URIs.

  @file_name  = file_name
  @index_name = index
  @mode       = mode
  @nthreads   = threads
  @hts_file   = LibHTS.hts_open(@file_name, mode)

  raise Errno::ENOENT, "Failed to open #{@file_name}" if @hts_file.null?

  set_threads(threads) if threads

  return if @mode[0] == "w"

  @header = Bcf::Header.new(@hts_file)
  build_index(index) if build_index
  @idx = load_index(index)
  @start_position = tell
end

Instance Attribute Details

#file_nameObject (readonly)

Returns the value of attribute file_name.



16
17
18
# File 'lib/hts/bcf.rb', line 16

def file_name
  @file_name
end

#headerObject

Returns the value of attribute header.



16
17
18
# File 'lib/hts/bcf.rb', line 16

def header
  @header
end

#index_nameObject (readonly)

Returns the value of attribute index_name.



16
17
18
# File 'lib/hts/bcf.rb', line 16

def index_name
  @index_name
end

#modeObject (readonly)

Returns the value of attribute mode.



16
17
18
# File 'lib/hts/bcf.rb', line 16

def mode
  @mode
end

#nthreadsObject (readonly)

Returns the value of attribute nthreads.



16
17
18
# File 'lib/hts/bcf.rb', line 16

def nthreads
  @nthreads
end

Class Method Details

.open(*args, **kw) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/hts/bcf.rb', line 18

def self.open(*args, **kw)
  file = new(*args, **kw) # do not yield
  return file unless block_given?

  begin
    yield file
  ensure
    file.close
  end
  file
end

Instance Method Details

#<<(var) ⇒ Object



117
118
119
# File 'lib/hts/bcf.rb', line 117

def <<(var)
  write(var)
end

#altArray

Get alt array

Returns:

  • (Array)

    the alt array



142
# File 'lib/hts/bcf.rb', line 142

define_getter :alt

#build_index(index_name = nil, min_shift: 14, threads: 2) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/hts/bcf.rb', line 57

def build_index(index_name = nil, min_shift: 14, threads: 2)
  check_closed

  if index_name
    warn "Create index for #{@file_name} to #{index_name}"
  else
    warn "Create index for #{@file_name}"
  end
  case LibHTS.bcf_index_build3(@file_name, index_name, min_shift, (@nthreads || threads))
  when 0 # successful
  when -1 then raise "indexing failed"
  when -2 then raise "opening #{@file_name} failed"
  when -3 then raise "format not indexable"
  when -4 then raise "failed to create and/or save the index"
  else raise "unknown error"
  end
  self # for method chaining
end

#chromArray

Get chrom array

Returns:

  • (Array)

    the chrom array



137
# File 'lib/hts/bcf.rb', line 137

define_getter :chrom

#closeObject



92
93
94
95
96
# File 'lib/hts/bcf.rb', line 92

def close
  LibHTS.hts_idx_destroy(@idx) unless @idx&.null?
  @idx = nil
  super
end

#each(copy: false, &block) ⇒ Object



204
205
206
207
208
209
210
# File 'lib/hts/bcf.rb', line 204

def each(copy: false, &block)
  if copy
    each_record_copy(&block)
  else
    each_record_reuse(&block)
  end
end

#each_altObject

Get alt iterator



182
# File 'lib/hts/bcf.rb', line 182

define_iterator :alt

#each_chromObject

Get chrom iterator



177
# File 'lib/hts/bcf.rb', line 177

define_iterator :chrom

#each_endposObject

Get endpos iterator



179
# File 'lib/hts/bcf.rb', line 179

define_iterator :endpos

#each_filterObject

Get filter iterator



184
# File 'lib/hts/bcf.rb', line 184

define_iterator :filter

#each_format(key) ⇒ Object



195
196
197
198
199
200
201
202
# File 'lib/hts/bcf.rb', line 195

def each_format(key)
  check_closed
  return to_enum(__method__, key) unless block_given?

  each do |r|
    yield r.format(key)
  end
end

#each_idObject

Get id iterator



180
# File 'lib/hts/bcf.rb', line 180

define_iterator :id

#each_info(key) ⇒ Object



186
187
188
189
190
191
192
193
# File 'lib/hts/bcf.rb', line 186

def each_info(key)
  check_closed
  return to_enum(__method__, key) unless block_given?

  each do |r|
    yield r.info(key)
  end
end

#each_posObject

Get pos iterator



178
# File 'lib/hts/bcf.rb', line 178

define_iterator :pos

#each_qualObject

Get qual iterator



183
# File 'lib/hts/bcf.rb', line 183

define_iterator :qual

#each_refObject

Get ref iterator



181
# File 'lib/hts/bcf.rb', line 181

define_iterator :ref

#endposArray

Get endpos array

Returns:

  • (Array)

    the endpos array



139
# File 'lib/hts/bcf.rb', line 139

define_getter :endpos

#filterArray

Get filter array

Returns:

  • (Array)

    the filter array



144
# File 'lib/hts/bcf.rb', line 144

define_getter :filter

#format(key = nil) ⇒ Object

Raises:

  • (NotImplementedError)


160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/hts/bcf.rb', line 160

def format(key = nil)
  check_closed
  position = tell
  raise NotImplementedError unless key

  ary = map { |r| r.format(key) }

  # ary = each_copy.map { |r| r.format }
  # ary = map { |r| r.format.clone }

  seek(position)
  ary
end

#idArray

Get id array

Returns:

  • (Array)

    the id array



140
# File 'lib/hts/bcf.rb', line 140

define_getter :id

#index_loaded?Boolean

Returns:

  • (Boolean)


86
87
88
89
90
# File 'lib/hts/bcf.rb', line 86

def index_loaded?
  check_closed

  !@idx.null?
end

#info(key = nil) ⇒ Object

Raises:

  • (NotImplementedError)


146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/hts/bcf.rb', line 146

def info(key = nil)
  check_closed
  position = tell
  raise NotImplementedError unless key

  ary = map { |r| r.info(key) }

  # ary = each_copy.map { |r| r.info }
  # ary = map { |r| r.info.clone }

  seek(position)
  ary
end

#load_index(index_name = nil) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/hts/bcf.rb', line 76

def load_index(index_name = nil)
  check_closed

  if index_name
    LibHTS.bcf_index_load2(@file_name, index_name)
  else
    LibHTS.bcf_index_load3(@file_name, nil, 2)
  end
end

#nsamplesObject



121
122
123
124
125
# File 'lib/hts/bcf.rb', line 121

def nsamples
  check_closed

  header.nsamples
end

#posArray

Get pos array

Returns:

  • (Array)

    the pos array



138
# File 'lib/hts/bcf.rb', line 138

define_getter :pos

#qualArray

Get qual array

Returns:

  • (Array)

    the qual array



143
# File 'lib/hts/bcf.rb', line 143

define_getter :qual

#query(region, beg = nil, end_ = nil, copy: false, &block) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/hts/bcf.rb', line 212

def query(region, beg = nil, end_ = nil, copy: false, &block)
  check_closed

  raise "query is only available for BCF files" unless file_format == "bcf"
  raise "Index file is required to call the query method." unless index_loaded?

  if beg && end_
    tid = header.name2id(region)
    queryi(tid, beg, end_, copy:, &block)
  elsif beg.nil? && end_.nil?
    querys(region, copy:, &block)
  else
    raise ArgumentError, "beg and end must be specified together"
  end
end

#refArray

Get ref array

Returns:

  • (Array)

    the ref array



141
# File 'lib/hts/bcf.rb', line 141

define_getter :ref

#samplesObject



127
128
129
130
131
# File 'lib/hts/bcf.rb', line 127

def samples
  check_closed

  header.samples
end

#write(record) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/hts/bcf.rb', line 109

def write(record)
  check_closed

  # record = record.dup
  r = LibHTS.bcf_write(@hts_file, header, record)
  raise "Failed to write record" if r < 0
end

#write_header(header) ⇒ Object



98
99
100
101
102
103
# File 'lib/hts/bcf.rb', line 98

def write_header(header)
  check_closed

  @header = header.clone
  LibHTS.bcf_hdr_write(@hts_file, header)
end