Class: HTS::Faidx

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

Defined Under Namespace

Classes: Sequence

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_name) ⇒ Faidx

Returns a new instance of Faidx.

Raises:

  • (Errno::ENOENT)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/hts/faidx.rb', line 22

def initialize(file_name)
  if block_given?
    message = "HTS::Faidx.new() dose not take block; Please use HTS::Faidx.open() instead"
    raise message
  end

  @file_name = file_name
  @fai = if [".fq", ".fastq"].include? File.extname(@file_name)
           LibHTS.fai_load_format(@file_name, 2)
         else
           LibHTS.fai_load(@file_name)
         end

  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.



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

def file_name
  @file_name
end

Class Method Details

.open(*args, **kw) ⇒ Object



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

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

#[](name) ⇒ Object



74
75
76
77
# File 'lib/hts/faidx.rb', line 74

def [](name)
  name = LibHTS.faidx_iseq(@fai, name) if name.is_a?(Integer)
  Sequence.new(self, name)
end

#closeObject



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

def close
  LibHTS.fai_destroy(@fai)
end

#fetch_qual(name, start = nil, stop = nil) ⇒ Object Also known as: qual



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/hts/faidx.rb', line 123

def fetch_qual(name, start = nil, stop = nil)
  name = name.to_s
  rlen = FFI::MemoryPointer.new(:int)

  if start.nil? && stop.nil?
    result = LibHTS.fai_fetchqual(@fai, name, rlen)
  else
    start < 0    && raise(ArgumentError, "Expect start to be >= 0")
    stop  < 0    && raise(ArgumentError, "Expect stop to be >= 0")
    start > stop && raise(ArgumentError, "Expect start to be <= stop")
    stop >= seq_len(name) && raise(ArgumentError, "Expect stop to be < seq_len")

    result = LibHTS.faidx_fetch_qual(@fai, name, start, stop, rlen)
  end

  case rlen.read_int
  when -2 then raise "Invalid chromosome name: #{name}"
  when -1 then raise "Error fetching sequence: #{name}:#{start}-#{stop}"
  end

  result
end

#seq(name) ⇒ Object #seq(name, start, stop) ⇒ String Also known as: seq

Overloads:

  • #seq(name) ⇒ Object

    Fetch the sequence as a String.

    Parameters:

    • name (String)

      chr1:0-10

  • #seq(name, start, stop) ⇒ String

    Fetch the sequence as a String.

    Parameters:

    • name (String)

      the name of the chromosome

    • start (Integer)

      the start position of the sequence (0-based)

    • stop (Integer)

      the end position of the sequence (0-based)

    Returns:

    • (String)

      the sequence



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/hts/faidx.rb', line 98

def fetch_seq(name, start = nil, stop = nil)
  name = name.to_s
  rlen = FFI::MemoryPointer.new(:int)

  if start.nil? && stop.nil?
    result = LibHTS.fai_fetch(@fai, name, rlen)
  else
    start < 0    && raise(ArgumentError, "Expect start to be >= 0")
    stop  < 0    && raise(ArgumentError, "Expect stop to be >= 0")
    start > stop && raise(ArgumentError, "Expect start to be <= stop")
    stop >= seq_len(name) && raise(ArgumentError, "Expect stop to be < seq_len")

    result = LibHTS.faidx_fetch_seq(@fai, name, start, stop, rlen)
  end

  case rlen.read_int
  when -2 then raise "Invalid chromosome name: #{name}"
  when -1 then raise "Error fetching sequence: #{name}:#{start}-#{stop}"
  end

  result
end

#file_formatObject



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

def file_format
  @fai[:format]
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


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

def has_key?(key)
  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
  end
end

#lengthObject Also known as: size

the number of sequences in the index.



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

def length
  LibHTS.faidx_nseq(@fai)
end

#namesObject Also known as: keys

return the length of the requested chromosome.



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

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

#seq_len(chrom) ⇒ Object

return the length of the requested chromosome.

Raises:

  • (ArgumentError)


80
81
82
83
84
85
86
# File 'lib/hts/faidx.rb', line 80

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 = LibHTS.faidx_seq_len(@fai, chrom)
  result == -1 ? nil : result
end

#structObject



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

def struct
  @fai
end