Class: HTS::Faidx
- Inherits:
-
Object
- Object
- HTS::Faidx
- Includes:
- Enumerable
- Defined in:
- lib/hts/faidx.rb,
lib/hts/faidx/sequence.rb
Defined Under Namespace
Classes: Sequence
Instance Attribute Summary collapse
-
#file_name ⇒ Object
readonly
Returns the value of attribute file_name.
Class Method Summary collapse
Instance Method Summary collapse
-
#[](name) ⇒ Sequence
Get a Sequence object by name or index.
- #close ⇒ Object
- #closed? ⇒ Boolean
-
#each {|Sequence| ... } ⇒ Enumerator
Iterate over each sequence in the index.
- #fetch_qual(name, start = nil, stop = nil) ⇒ Object (also: #qual)
- #fetch_seq(name, start = nil, stop = nil) ⇒ Object (also: #seq)
- #file_format ⇒ Object
-
#has_key?(key) ⇒ Boolean
Check if a sequence exists in the index.
-
#initialize(file_name) ⇒ Faidx
constructor
A new instance of Faidx.
-
#length ⇒ Integer
(also: #size)
the number of sequences in the index.
-
#names ⇒ Array<String>
(also: #keys)
Return the list of sequence names in the index.
-
#seq_len(chrom) ⇒ Integer
Return the length of the requested chromosome.
- #struct ⇒ Object
Constructor Details
#initialize(file_name) ⇒ Faidx
Returns a new instance of Faidx.
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_name ⇒ Object (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.
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 |
#close ⇒ Object
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
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.
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
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
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_format ⇒ Object
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.
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 |
#length ⇒ Integer Also known as: size
the number of sequences in the index.
70 71 72 73 |
# File 'lib/hts/faidx.rb', line 70 def length check_closed LibHTS.faidx_nseq(@fai) end |
#names ⇒ Array<String> Also known as: keys
Return the list of sequence names in the index.
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.
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 |
#struct ⇒ Object
38 39 40 |
# File 'lib/hts/faidx.rb', line 38 def struct @fai end |