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
- #closed? ⇒ Boolean
- #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 37 |
# 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 = 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.
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 |
#close ⇒ Object
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
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
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_format ⇒ Object
54 55 56 |
# File 'lib/hts/faidx.rb', line 54 def file_format @fai[:format] end |
#has_key?(key) ⇒ Boolean
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 |
#length ⇒ Object 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 |
#names ⇒ Object 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.
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 |
#struct ⇒ Object
39 40 41 |
# File 'lib/hts/faidx.rb', line 39 def struct @fai end |