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
37
# 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 = 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.



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



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

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

#closeObject



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

def close
  return if closed?

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

#closed?Boolean

Returns:

  • (Boolean)


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

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

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



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

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_fetchqual64(@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_qual64(@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



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

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_fetch64(@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_seq64(@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



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

def file_format
  @fai[:format]
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


71
72
73
74
75
76
77
78
79
80
# File 'lib/hts/faidx.rb', line 71

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.



59
60
61
# File 'lib/hts/faidx.rb', line 59

def length
  LibHTS.faidx_nseq(@fai)
end

#namesObject Also known as: keys

return the length of the requested chromosome.



65
66
67
# File 'lib/hts/faidx.rb', line 65

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)


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

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



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

def struct
  @fai
end