Class: FbinFile
- Inherits:
-
Object
- Object
- FbinFile
- Extended by:
- FFI::Library
- Defined in:
- lib/scbi_fqbin/fbin_file.rb
Constant Summary collapse
- CREATE_NEW_FILE =
1- APPEND_TO_FILE =
2
Instance Method Summary collapse
-
#close ⇒ Object
Closes file.
-
#count ⇒ Object
Returns the number of sequences in file.
-
#each ⇒ Object
Iterates over all sequences in file.
- #get_next_seq(gzf_bin) ⇒ Object
-
#initialize(file_path, mode = 'r', qual_to_phred = true, qual_to_array = true, discretize_qual = 0, flatten_qual = 0, create_index = 1) ⇒ FbinFile
constructor
Initializes the file.
-
#next_sequence ⇒ Object
Returns the next sequence in file, or nil if no more ara available.
-
#read_sequence(seq_name) ⇒ Object
Access a sequence by its name using the indexed read.
-
#write_sequence(seq_name, fasta, qual, extras) ⇒ Object
Writes a sequence to file.
Constructor Details
#initialize(file_path, mode = 'r', qual_to_phred = true, qual_to_array = true, discretize_qual = 0, flatten_qual = 0, create_index = 1) ⇒ FbinFile
Initializes the file
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/scbi_fqbin/fbin_file.rb', line 58 def initialize(file_path, mode = 'r', qual_to_phred=true, qual_to_array=true, discretize_qual=0, flatten_qual=0, create_index=1) @file_path = file_path @open_mode=mode @qual_to_phred = qual_to_phred @qual_to_array = qual_to_array @to_phred = lambda{|q| q - 33} if @open_mode.index('r') @gzf_bin = FFI::MemoryPointer.new :pointer initialize_sequential_reads(@gzf_bin,@file_path) # inspect_file_data_struct(@gzf_bin) @gzf_bin = @gzf_bin.get_pointer(0) elsif @open_mode.index('w') @write_struct = FFI::MemoryPointer.new :pointer initialize_writes(@write_struct,@file_path, CREATE_NEW_FILE,discretize_qual, flatten_qual, create_index) @write_struct = @write_struct.get_pointer(0) # inspect_file_data_struct(@write_struct) elsif @open_mode.index('a') @write_struct = FFI::MemoryPointer.new :pointer initialize_writes(@write_struct,@file_path, APPEND_TO_FILE,discretize_qual, flatten_qual, create_index) @write_struct = @write_struct.get_pointer(0) # inspect_file_data_struct(@write_struct) else raise "Invalid aperture mode #{mode}. Use r/w" end end |
Instance Method Details
#close ⇒ Object
Closes file
202 203 204 205 206 207 208 209 210 211 |
# File 'lib/scbi_fqbin/fbin_file.rb', line 202 def close if @open_mode.index('r') close_sequential_reads(@gzf_bin) else # CREATE_NEW_FILE or APPEND_TO_FILE # inspect_write_struct(@write_struct) close_writes(@write_struct) end end |
#count ⇒ Object
Returns the number of sequences in file. Iterating over it
214 215 216 217 218 219 220 221 |
# File 'lib/scbi_fqbin/fbin_file.rb', line 214 def count res=0 each do #|n,f,q,e| # puts n res+=1 end return res end |
#each ⇒ Object
Iterates over all sequences in file
177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/scbi_fqbin/fbin_file.rb', line 177 def each gzf_bin = FFI::MemoryPointer.new :pointer initialize_sequential_reads(gzf_bin,@file_path) gzf_bin = gzf_bin.get_pointer(0) while seq=get_next_seq(gzf_bin) yield seq end close_sequential_reads(gzf_bin) end |
#get_next_seq(gzf_bin) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/scbi_fqbin/fbin_file.rb', line 134 def get_next_seq(gzf_bin) seq_namePtr = FFIString.new fastaPtr = FFIString.new qualPtr = FFIString.new extrasPtr = FFIString.new if ((r=read_data_sequential(gzf_bin, seq_namePtr, fastaPtr, qualPtr, extrasPtr))==0) qual=qualPtr.to_s if @qual_to_phred qual=qual.each_char.map{|e| (@to_phred.call(e.ord))} if !@qual_to_array qual=qual.join(' ') end end seq_name=seq_namePtr.to_s fasta=fastaPtr.to_s extras=extrasPtr.to_s # seq_namePtr.free # fastaPtr.free # qualPtr.free # extrasPtr.free free_string(seq_namePtr) free_string(fastaPtr) free_string(qualPtr) free_string(extrasPtr) return seq_name, fasta, qual, extras else free_string(seq_namePtr) free_string(fastaPtr) free_string(qualPtr) free_string(extrasPtr) return nil end end |
#next_sequence ⇒ Object
Returns the next sequence in file, or nil if no more ara available
130 131 132 |
# File 'lib/scbi_fqbin/fbin_file.rb', line 130 def next_sequence return get_next_seq(@gzf_bin) end |
#read_sequence(seq_name) ⇒ Object
Access a sequence by its name using the indexed read
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/scbi_fqbin/fbin_file.rb', line 92 def read_sequence(seq_name) fastaPtr = FFIString.new qualPtr = FFIString.new extrasPtr = FFIString.new if read_seq(@file_path,seq_name,fastaPtr,qualPtr,extrasPtr)==0 qual=qualPtr.to_s if @qual_to_phred qual=qual.each_char.map{|e| (@to_phred.call(e.ord))} if !@qual_to_array qual=qual.join(' ') end end fasta=fastaPtr.to_s extras = extrasPtr.to_s free_string(qualPtr) free_string(fastaPtr) free_string(extrasPtr) return seq_name, fasta, qual, extras else free_string(qualPtr) free_string(fastaPtr) free_string(extrasPtr) return nil # raise "Invalid sequence" end end |
#write_sequence(seq_name, fasta, qual, extras) ⇒ Object
Writes a sequence to file
191 192 193 194 195 196 197 198 |
# File 'lib/scbi_fqbin/fbin_file.rb', line 191 def write_sequence(seq_name,fasta,qual,extras) if qual.is_a?(Array) qual=qual.join(' ') end write_seq(@write_struct,seq_name,fasta,qual,extras) end |