Class: HTS::Faidx

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_name, format: :auto, auto_build: true) ⇒ Faidx

Returns a new instance of Faidx.

Raises:

  • (ArgumentError)


36
37
38
39
40
41
42
43
44
# File 'lib/hts/faidx.rb', line 36

def initialize(file_name, format: :auto, auto_build: true)
  raise ArgumentError, "HTS::Faidx.new() does not take block; Please use HTS::Faidx.open() instead" if block_given?

  @file_name = file_name.freeze
  @format = resolve_format(@file_name, format)
  @fai = load_handle(@file_name, @format, auto_build)

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

Instance Attribute Details

#file_nameObject (readonly)

Returns the value of attribute file_name.



15
16
17
# File 'lib/hts/faidx.rb', line 15

def file_name
  @file_name
end

#formatObject (readonly)

Returns the value of attribute format.



15
16
17
# File 'lib/hts/faidx.rb', line 15

def format
  @format
end

Class Method Details

.build_index(file_name, fai_path = nil, gzi_path = nil) ⇒ Object



29
30
31
32
33
34
# File 'lib/hts/faidx.rb', line 29

def self.build_index(file_name, fai_path = nil, gzi_path = nil)
  case LibHTS.fai_build3(file_name, fai_path, gzi_path)
  when 0
  else raise HTS::Error, "Failed to build faidx index for #{file_name}"
  end
end

.open(file_name, format: :auto, auto_build: true) ⇒ Object



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

def self.open(file_name, format: :auto, auto_build: true)
  file = new(file_name, format:, auto_build:) # do not yield
  return file unless block_given?

  begin
    yield file
  ensure
    file.close
  end
  file
end

Instance Method Details

#build_index(fai_path = nil, gzi_path = nil) ⇒ Object



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

def build_index(fai_path = nil, gzi_path = nil)
  self.class.build_index(@file_name, fai_path, gzi_path)
  self
end

#closeObject



50
51
52
53
54
55
# File 'lib/hts/faidx.rb', line 50

def close
  return if closed?

  LibHTS.fai_destroy(@fai)
  @fai = nil
end

#closed?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/hts/faidx.rb', line 57

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

#fetch_qual(name, start = nil, stop = nil) ⇒ Object

Raises:



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/hts/faidx.rb', line 112

def fetch_qual(name, start = nil, stop = nil)
  check_closed
  raise HTS::Error, "Quality is only available for FASTQ indexes" unless format == :fastq
  name = name.to_s

  if start.nil? && stop.nil?
    len = seq_len(name)
    return "" if len.zero?
    return fetch_qual(name, 0, len - 1)
  else
    validate_range!(name, start, stop)
    rlen = FFI::MemoryPointer.new(:int64)
    result = LibHTS.faidx_fetch_qual64(@fai, name, start, stop, rlen)
    return fetch_result(result, rlen.read_int64, "quality", name, start, stop)
  end
end

#fetch_seq(name, start = nil, stop = nil) ⇒ Object



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

def fetch_seq(name, start = nil, stop = nil)
  check_closed
  name = name.to_s

  if start.nil? && stop.nil?
    len = seq_len(name)
    return "" if len.zero?
    return fetch_seq(name, 0, len - 1)
  else
    validate_range!(name, start, stop)
    rlen = FFI::MemoryPointer.new(:int64)
    result = LibHTS.faidx_fetch_seq64(@fai, name, start, stop, rlen)
    return fetch_result(result, rlen.read_int64, "sequence", name, start, stop)
  end
end

#has_seq?(key) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


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

def has_seq?(key)
  check_closed
  raise ArgumentError, "Expect chrom to be String or Symbol" unless key.is_a?(String) || key.is_a?(Symbol)

  key = key.to_s
  case LibHTS.faidx_has_seq(@fai, key)
  when 1 then true
  when 0 then false
  else raise HTS::Error, "Unexpected return value from faidx_has_seq"
  end
end

#namesObject



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

def names
  check_closed
  Array.new(length) { |i| LibHTS.faidx_iseq(@fai, i) }
end

#seq_len(chrom) ⇒ Object

Raises:

  • (ArgumentError)


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

def seq_len(chrom)
  check_closed
  raise ArgumentError, "Expect chrom to be String or Symbol" unless chrom.is_a?(String) || chrom.is_a?(Symbol)

  chrom = chrom.to_s
  result = LibHTS.faidx_seq_len64(@fai, chrom)
  raise ArgumentError, "Sequence not found: #{chrom}" if result == -1

  result
end

#sizeObject Also known as: length



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

def size
  check_closed
  LibHTS.faidx_nseq(@fai)
end

#structObject



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

def struct
  @fai
end