Class: HTS::Faidx

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

Defined Under Namespace

Classes: OpenError

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.



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

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)
  @native = Native::FaidxHandle.open(@file_name, @format == :fastq ? 1 : 0, auto_build)
rescue Errno::ENOENT
  raise Errno::ENOENT, "Failed to open #{@file_name}"
end

Instance Attribute Details

#file_nameObject (readonly)

Returns the value of attribute file_name.



9
10
11
# File 'lib/hts/faidx.rb', line 9

def file_name
  @file_name
end

#formatObject (readonly)

Returns the value of attribute format.



9
10
11
# File 'lib/hts/faidx.rb', line 9

def format
  @format
end

Class Method Details

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

Raises:



23
24
25
26
27
# File 'lib/hts/faidx.rb', line 23

def self.build_index(file_name, fai_path = nil, gzi_path = nil)
  return if Native::FaidxHandle.build(file_name, fai_path, gzi_path).zero?

  raise HTS::Error, "Failed to build faidx index for #{file_name}"
end

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



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/hts/faidx.rb', line 11

def self.open(file_name, format: :auto, auto_build: true)
  file = new(file_name, format:, auto_build:)
  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



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

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

#closeObject



39
# File 'lib/hts/faidx.rb', line 39

def close = @native&.close

#closed?Boolean

Returns:

  • (Boolean)


40
# File 'lib/hts/faidx.rb', line 40

def closed? = @native.nil? || @native.closed?

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

Raises:



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/hts/faidx.rb', line 75

def fetch_qual(name, start = nil, stop = nil)
  native
  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?

    start = 0
    stop = len - 1
  else
    validate_range!(name, start, stop)
  end
  fetch_result(native.fetch_qual(name, start, stop), "quality", name, start, stop)
end

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



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/hts/faidx.rb', line 61

def fetch_seq(name, start = nil, stop = nil)
  name = name.to_s
  if start.nil? && stop.nil?
    len = seq_len(name)
    return "" if len.zero?

    start = 0
    stop = len - 1
  else
    validate_range!(name, start, stop)
  end
  fetch_result(native.fetch_seq(name, start, stop), "sequence", name, start, stop)
end

#has_seq?(key) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


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

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

  native.has_seq?(key.to_s)
end

#namesObject



43
# File 'lib/hts/faidx.rb', line 43

def names = native.names

#seq_len(chrom) ⇒ Object

Raises:

  • (ArgumentError)


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

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

  chrom = chrom.to_s
  result = native.seq_len(chrom)
  raise ArgumentError, "Sequence not found: #{chrom}" if result == -1

  result
end

#sizeObject Also known as: length



41
# File 'lib/hts/faidx.rb', line 41

def size = native.size