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.



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

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.



7
8
9
# File 'lib/hts/faidx.rb', line 7

def file_name
  @file_name
end

#formatObject (readonly)

Returns the value of attribute format.



7
8
9
# File 'lib/hts/faidx.rb', line 7

def format
  @format
end

Class Method Details

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

Raises:



21
22
23
24
25
# File 'lib/hts/faidx.rb', line 21

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



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

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



90
91
92
93
# File 'lib/hts/faidx.rb', line 90

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

#closeObject



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

def close = @native&.close

#closed?Boolean

Returns:

  • (Boolean)


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

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

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

Raises:



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

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



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

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)


43
44
45
46
47
# File 'lib/hts/faidx.rb', line 43

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



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

def names = native.names

#seq_len(chrom) ⇒ Object

Raises:

  • (ArgumentError)


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

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



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

def size = native.size