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?, #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 (readonly)

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

#altArray

Get alt array



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

define_getter :alt

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



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

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

  if index_name
    warn "Create index for #{@file_name} to #{index_name}"
  else
    warn "Create index for #{@file_name}"
  end
  r = LibHTS.bcf_index_build3(@file_name, index_name, min_shift, @nthreads)
  raise "Failed to build index for #{@file_name}" if r < 0

  self
end

#chromArray

Get chrom array



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

define_getter :chrom

#closeObject



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

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

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



122
123
124
125
126
127
128
# File 'lib/hts/bcf.rb', line 122

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

#each_altObject

Get alt iterator



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

define_iterator :alt

#each_chromObject

Get chrom iterator



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

define_iterator :chrom

#each_endposObject

Get endpos iterator



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

define_iterator :endpos

#each_filterObject

Get filter iterator



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

define_iterator :filter

#each_format(key) ⇒ Object



213
214
215
216
217
218
219
220
# File 'lib/hts/bcf.rb', line 213

def each_format(key)
  check_closed
  return to_enum(__method__, key) unless block

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

#each_idObject

Get id iterator



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

define_iterator :id

#each_info(key) ⇒ Object



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

def each_info(key)
  check_closed
  return to_enum(__method__, key) unless block

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

#each_posObject

Get pos iterator



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

define_iterator :pos

#each_qualObject

Get qual iterator



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

define_iterator :qual

#each_refObject

Get ref iterator



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

define_iterator :ref

#endposArray

Get endpos array



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

define_getter :endpos

#filterArray

Get filter array



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

define_getter :filter

#format(key = nil) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/hts/bcf.rb', line 178

def format(key = nil)
  check_closed
  position = tell
  if key
    ary = map { |r| r.format(key) }
  else
    raise NotImplementedError
    # ary = each_copy.map { |r| r.format }
    # ary = map { |r| r.format.clone }
  end
  seek(position)
  ary
end

#idArray

Get id array



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

define_getter :id

#index_loaded?Boolean



81
82
83
84
85
# File 'lib/hts/bcf.rb', line 81

def index_loaded?
  check_closed

  !@idx.null?
end

#info(key = nil) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/hts/bcf.rb', line 164

def info(key = nil)
  check_closed
  position = tell
  if key
    ary = map { |r| r.info(key) }
  else
    raise NotImplementedError
    # ary = each_copy.map { |r| r.info }
    # ary = map { |r| r.info.clone }
  end
  seek(position)
  ary
end

#load_index(index_name = nil) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/hts/bcf.rb', line 71

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

Close the current file.



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

def nsamples
  check_closed

  header.nsamples
end

#posArray

Get pos array



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

define_getter :pos

#qualArray

Get qual array



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

define_getter :qual

#queryObject



130
131
132
# File 'lib/hts/bcf.rb', line 130

def query(...)
  querys(...) # Fixme
end

#querys(region, copy: false, &block) ⇒ Object

def queryi end



137
138
139
140
141
142
143
# File 'lib/hts/bcf.rb', line 137

def querys(region, copy: false, &block)
  if copy
    querys_copy(region, &block)
  else
    querys_reuse(region, &block)
  end
end

#refArray

Get ref array



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

define_getter :ref

#samplesObject



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

def samples
  check_closed

  header.samples
end

#write(var) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/hts/bcf.rb', line 100

def write(var)
  check_closed

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

#write_header(header) ⇒ Object



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

def write_header(header)
  check_closed

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