Class: HTS::Hts

Inherits:
Object
  • Object
show all
Defined in:
lib/hts/hts.rb

Overview

A base class for hts files.

Direct Known Subclasses

Bam, Bcf, Tbx

Instance Method Summary collapse

Constructor Details

#initialize(*_args) ⇒ Hts

Returns a new instance of Hts.

Raises:

  • (TypeError)


34
35
36
# File 'lib/hts/hts.rb', line 34

def initialize(*_args)
  raise TypeError, "Can't make instance of HTS abstract class"
end

Instance Method Details

#closeObject



61
62
63
64
65
66
# File 'lib/hts/hts.rb', line 61

def close
  return if closed?

  LibHTS.hts_close(@hts_file)
  @hts_file = nil
end

#closed?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/hts/hts.rb', line 68

def closed?
  @hts_file.nil? || @hts_file.null?
end

#file_formatObject



46
47
48
# File 'lib/hts/hts.rb', line 46

def file_format
  LibHTS.hts_get_format(@hts_file)[:format].to_s
end

#file_format_versionObject



50
51
52
53
54
55
56
57
58
59
# File 'lib/hts/hts.rb', line 50

def file_format_version
  v = LibHTS.hts_get_format(@hts_file)[:version]
  major = v[:major]
  minor = v[:minor]
  if minor == -1
    major.to_s
  else
    "#{major}.#{minor}"
  end
end

#rewindObject



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

def rewind
  if @start_position
    r = seek(@start_position)
    raise "Failed to rewind: #{r}" if r < 0

    tell
  else
    raise "Cannot rewind: no start position"
  end
end

#seek(offset) ⇒ Object



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

def seek(offset)
  if @hts_file[:is_cram] == 1
    LibHTS.cram_seek(@hts_file[:fp][:cram], offset, IO::SEEK_SET)
  elsif @hts_file[:is_bgzf] == 1
    LibHTS.bgzf_seek(@hts_file[:fp][:bgzf], offset, IO::SEEK_SET)
  else
    LibHTS.hseek(@hts_file[:fp][:hfile], offset, IO::SEEK_SET)
  end
end

#set_threads(n = nil) ⇒ Object

Raises:

  • (TypeError)


72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/hts/hts.rb', line 72

def set_threads(n = nil)
  if n.nil?
    require "etc"
    n = [Etc.nprocessors - 1, 1].max
  end
  raise TypeError unless n.is_a?(Integer)
  raise ArgumentError, "Number of threads must be positive" if n < 1

  r = LibHTS.hts_set_threads(@hts_file, n)
  raise "Failed to set number of threads: #{threads}" if r < 0

  @nthreads = n
  self
end

#structObject



38
39
40
# File 'lib/hts/hts.rb', line 38

def struct
  @hts_file
end

#tellObject



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/hts/hts.rb', line 97

def tell
  if @hts_file[:is_cram] == 1
    # LibHTS.cram_tell(@hts_file[:fp][:cram])
    # warn 'cram_tell is not implemented in c htslib'
    nil
  elsif @hts_file[:is_bgzf] == 1
    LibHTS.bgzf_tell(@hts_file[:fp][:bgzf])
  else
    LibHTS.htell(@hts_file[:fp][:hfile])
  end
end

#to_ptrObject



42
43
44
# File 'lib/hts/hts.rb', line 42

def to_ptr
  @hts_file.to_ptr
end