Class: HTS::Faidx

Inherits:
Object
  • Object
show all
Includes:
Enumerable
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:

  • (ArgumentError)


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

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

  @file_name = file_name.freeze
  @fai = case File.extname(@file_name)
         when ".fq", ".fastq"
           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.



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

def file_name
  @file_name
end

Class Method Details

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



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

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) ⇒ Sequence

Get a Sequence object by name or index.

Parameters:

  • name (String, Symbol, Integer)

    sequence name or index

Returns:

Raises:

  • (ArgumentError)

    if the sequence does not exist



104
105
106
107
108
# File 'lib/hts/faidx.rb', line 104

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

#closeObject



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

def close
  return if closed?

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

#closed?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/hts/faidx.rb', line 49

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

#each {|Sequence| ... } ⇒ Enumerator

Iterate over each sequence in the index.

Yields:

Returns:

  • (Enumerator)

    if no block given



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

def each
  return to_enum(__method__) unless block_given?

  check_closed
  names.each { |name| yield self[name] }
end

#fetch_qual(name) ⇒ String #fetch_qual(name, start, stop) ⇒ String Also known as: qual

Overloads:

  • #fetch_qual(name) ⇒ String

    Fetch the quality string.

    Parameters:

    • name (String, Symbol)

      sequence name

    Returns:

    • (String)

      the quality string

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

    Fetch the quality string.

    Parameters:

    • name (String, Symbol)

      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 quality string



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/hts/faidx.rb', line 167

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

  if start.nil? && stop.nil?
    result = LibHTS.fai_fetchqual64(@fai, name, rlen)
  else
    validate_range!(name, start, stop)
    result = LibHTS.faidx_fetch_qual64(@fai, name, start, stop, rlen)
  end

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

  result
end

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

Overloads:

  • #fetch_seq(name) ⇒ String

    Fetch the sequence as a String.

    Parameters:

    • name (String, Symbol)

      chr1:0-10

    Returns:

    • (String)

      the sequence

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

    Fetch the sequence as a String.

    Parameters:

    • name (String, Symbol)

      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



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/hts/faidx.rb', line 135

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

  if start.nil? && stop.nil?
    result = LibHTS.fai_fetch64(@fai, name, rlen)
  else
    validate_range!(name, start, stop)
    result = LibHTS.faidx_fetch_seq64(@fai, name, start, stop, rlen)
  end

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

  result
end

#file_formatObject



53
54
55
56
# File 'lib/hts/faidx.rb', line 53

def file_format
  check_closed
  @fai[:format]
end

#has_key?(key) ⇒ Boolean

Check if a sequence exists in the index.

Parameters:

  • key (String, Symbol)

    sequence name

Returns:

  • (Boolean)

    true if the sequence exists

Raises:

  • (ArgumentError)


88
89
90
91
92
93
94
95
96
97
98
# File 'lib/hts/faidx.rb', line 88

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

#lengthInteger Also known as: size

the number of sequences in the index.

Returns:

  • (Integer)

    the number of sequences



70
71
72
73
# File 'lib/hts/faidx.rb', line 70

def length
  check_closed
  LibHTS.faidx_nseq(@fai)
end

#namesArray<String> Also known as: keys

Return the list of sequence names in the index.

Returns:

  • (Array<String>)

    sequence names



78
79
80
81
# File 'lib/hts/faidx.rb', line 78

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

#seq_len(chrom) ⇒ Integer

Return the length of the requested chromosome.

Parameters:

  • chrom (String, Symbol)

    chromosome name

Returns:

  • (Integer)

    sequence length

Raises:

  • (ArgumentError)

    if the sequence does not exist



114
115
116
117
118
119
120
121
122
123
# File 'lib/hts/faidx.rb', line 114

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_len(@fai, chrom)
  raise ArgumentError, "Sequence not found: #{chrom}" if result == -1

  result
end

#structObject



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

def struct
  @fai
end