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

#close, #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
56
# 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
  super # do nothing
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

Returns:

  • (Array)

    the alt array



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

define_getter :alt

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



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

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

Returns:

  • (Array)

    the chrom array



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

define_getter :chrom

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



117
118
119
120
121
122
123
# File 'lib/hts/bcf.rb', line 117

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

#each_altObject

Get alt iterator



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

define_iterator :alt

#each_chromObject

Get chrom iterator



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

define_iterator :chrom

#each_endposObject

Get endpos iterator



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

define_iterator :endpos

#each_filterObject

Get filter iterator



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

define_iterator :filter

#each_format(key) ⇒ Object



280
281
282
283
284
285
286
287
# File 'lib/hts/bcf.rb', line 280

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



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

define_iterator :id

#each_info(key) ⇒ Object



271
272
273
274
275
276
277
278
# File 'lib/hts/bcf.rb', line 271

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



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

define_iterator :pos

#each_qualObject

Get qual iterator



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

define_iterator :qual

#each_refObject

Get ref iterator



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

define_iterator :ref

#endposArray

Get endpos array

Returns:

  • (Array)

    the endpos array



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

define_getter :endpos

#filterArray

Get filter array

Returns:

  • (Array)

    the filter array



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

define_getter :filter

#format(key = nil) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/hts/bcf.rb', line 245

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

Returns:

  • (Array)

    the id array



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

define_getter :id

#index_loaded?Boolean

Returns:

  • (Boolean)


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

def index_loaded?
  check_closed

  !@idx.null?
end

#info(key = nil) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/hts/bcf.rb', line 231

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



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

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.



105
106
107
108
109
# File 'lib/hts/bcf.rb', line 105

def nsamples
  check_closed

  header.nsamples
end

#posArray

Get pos array

Returns:

  • (Array)

    the pos array



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

define_getter :pos

#qualArray

Get qual array

Returns:

  • (Array)

    the qual array



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

define_getter :qual

#queryObject



148
149
150
# File 'lib/hts/bcf.rb', line 148

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

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

def queryi end



155
156
157
158
159
160
161
# File 'lib/hts/bcf.rb', line 155

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

#refArray

Get ref array

Returns:

  • (Array)

    the ref array



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

define_getter :ref

#samplesObject



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

def samples
  check_closed

  header.samples
end

#write(var) ⇒ Object



96
97
98
99
100
101
# File 'lib/hts/bcf.rb', line 96

def write(var)
  check_closed

  var_dup = var.dup
  LibHTS.bcf_write(@hts_file, header, var_dup) > 0 || raise
end

#write_headerObject



88
89
90
91
92
93
94
# File 'lib/hts/bcf.rb', line 88

def write_header
  check_closed

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