Class: HTS::Faidx
- Inherits:
-
Object
- Object
- HTS::Faidx
- 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) ⇒ Object
- #close ⇒ Object
- #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
-
#initialize(file_name) ⇒ Faidx
constructor
A new instance of Faidx.
-
#length ⇒ Object
(also: #size)
the number of sequences in the index.
-
#names ⇒ Object
(also: #keys)
return the length of the requested chromosome.
-
#seq_len(chrom) ⇒ Object
return the length of the requested chromosome.
- #struct ⇒ Object
Constructor Details
#initialize(file_name) ⇒ Faidx
Returns a new instance of Faidx.
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? = "HTS::Faidx.new() dose not take block; Please use HTS::Faidx.open() instead" raise 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_name ⇒ Object (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 |
#close ⇒ Object
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
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_format ⇒ Object
46 47 48 |
# File 'lib/hts/faidx.rb', line 46 def file_format @fai[:format] end |
#has_key?(key) ⇒ Boolean
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 |
#length ⇒ Object 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 |
#names ⇒ Object 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.
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 |
#struct ⇒ Object
38 39 40 |
# File 'lib/hts/faidx.rb', line 38 def struct @fai end |