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

Returns a new instance of Faidx.

Raises:

  • (Errno::ENOENT)


21
22
23
24
25
26
27
28
29
30
31
# File 'lib/hts/faidx.rb', line 21

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 = LibHTS.fai_load(@file_name)

  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.



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

def file_name
  @file_name
end

Class Method Details

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



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

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

#chrom_namesObject

return the length of the requested chromosome.



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

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

#chrom_size(chrom) ⇒ Object Also known as: chrom_length

return the length of the requested chromosome.

Raises:

  • (ArgumentError)


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

def chrom_size(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

#closeObject



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

def close
  LibHTS.fai_destroy(@fai)
end

#lengthObject Also known as: size

the number of sequences in the index.



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

def length
  LibHTS.faidx_nseq(@fai)
end

#fetch(name) ⇒ Object #fetch(name, start, stop) ⇒ String

Overloads:

  • #fetch(name) ⇒ Object

    Fetch the sequence as a String.

    Parameters:

    • name (String)

      chr1:0-10

  • #fetch(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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/hts/faidx.rb', line 77

def 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")

    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

#structObject



33
34
35
# File 'lib/hts/faidx.rb', line 33

def struct
  @fai
end