Class: FastqFileC

Inherits:
Object
  • Object
show all
Extended by:
FFI::Library
Defined in:
lib/scbi_fqbin/fastq_file_c.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fasta_file_name, mode = 'r', fastq_type = :sanger, qual_to_array = true, qual_to_phred = true) ⇒ FastqFileC


Initialize instance




86
87
88
89
90
91
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
128
129
130
131
132
133
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/fastq_file_c.rb', line 86

def initialize(fasta_file_name, mode='r', fastq_type = :sanger, qual_to_array=true, qual_to_phred=true)

  @fasta_file_name=fasta_file_name
  
  if mode.upcase.index('W')
    @fastq_file = File.open(fasta_file_name,'w')
  elsif mode.upcase.index('A')
    if !File.exist?(fasta_file_name)
      raise "File #{fasta_file_name} doesn't exists" 
    end
    
    @fastq_file = File.open(fasta_file_name,'a')
  else #read only
    if !File.exist?(fasta_file_name)
      raise "File #{fasta_file_name} doesn't exists" 
    end
    
    if fasta_file_name.is_a?(IO)
      @fastq_file = fasta_file_name
    else
      # @fastq_file = File.open(fasta_file_name,'r')
      @namePtr = FFIString.new
      @fastaPtr = FFIString.new
      @qualPtr = FFIString.new
      @extrasPtr = FFIString.new
      
      open_fastq
    end
  end
  
  @mode = mode
  @num_seqs = 0
  @fastq_type=fastq_type
  
  #  S - Sanger        Phred+33,  raw reads typically (0, 40)
  #  X - Solexa        Solexa+64, raw reads typically (-5, 40)
  #  I - Illumina 1.3+ Phred+64,  raw reads typically (0, 40)
  #  J - Illumina 1.5+ Phred+64,  raw reads typically (3, 40)
  # > >>> def solexa_quality_from_phred(phred_quality) :
  # > ...     return 10*log(10**(phred_quality/10.0) - 1, 10)
  # > ...
  # > >>> solexa_quality_from_phred(90)
  # > 89.999999995657035
  # > >>> solexa_quality_from_phred(50)
  # > 49.99995657033466
  # > >>> solexa_quality_from_phred(10)
  # > 9.5424250943932485
  # > >>> solexa_quality_from_phred(1)
  # > -5.8682532438011537
  # > >>> solexa_quality_from_phred(0.1)
  # > -16.32774717238372
  # > 
  # > >>> def phred_quality_from_solexa(solexa_quality) :
  # > ...     return 10*log(10**(solexa_quality/10.0) + 1, 10)
  # > ...
  # > >>> phred_quality_from_solexa(90)
  # > 90.000000004342922
  # > >>> phred_quality_from_solexa(10)
  # > 10.41392685158225
  # > >>> phred_quality_from_solexa(0)
  # > 3.0102999566398116
  # > >>> phred_quality_from_solexa(-20)
  # > 0.043213737826425784
  
  
  #sanger by default
  @to_phred = lambda{|q| q - 33}
  @from_phred = lambda{|q| (q+33).chr}
  
  if @fastq_type == :ilumina
      @to_phred = lambda{|q| q - 64}
      # @from_phred = lambda{|q| (q+64).chr}
      
  elsif @fastq_type == :solexa
     # 
     # solexa to phred quals
     
     @to_phred = lambda{|q| (10*Math.log(10**(q/10.0)+1,10)).round}
     # @from_phred = lambda{|q| (10*Math.log(10**(q/10.0)-1,10)).round.chr}
     
     #phred to solexa quals
     
  end
  
  @qual_to_array = qual_to_array
  
  @qual_to_phred = qual_to_phred
  
end

Instance Attribute Details

#num_seqsObject

Returns the value of attribute num_seqs.



40
41
42
# File 'lib/scbi_fqbin/fastq_file_c.rb', line 40

def num_seqs
  @num_seqs
end

Class Method Details

.to_fastq(seq_name, seq_fasta, seq_qual, comments = '') ⇒ Object

creates fastq otuput in sanger format



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/scbi_fqbin/fastq_file_c.rb', line 280

def self.to_fastq(seq_name,seq_fasta,seq_qual,comments='')
  
  res=[]
  
 name = ""
 
res << ("@#{seq_name} #{comments}")
res << (seq_fasta)
res << ("+#{seq_name} #{comments}")

if @qual_to_phred
    if seq_qual.is_a?(Array)
      res<<(seq_qual.map{|e| (e+33).chr}.join)
    else
      res<<(seq_qual.split(/\s+/).map{|e| (e.to_i+33).chr}.join)
    end
else
  res << seq_qual
 end
 
  return res
end

Instance Method Details

#closeObject



176
177
178
179
180
181
182
183
# File 'lib/scbi_fqbin/fastq_file_c.rb', line 176

def close
  free_string(@namePtr)
  free_string(@qualPtr)
  free_string(@fastaPtr)
  free_string(@extrasPtr)
  
  close_file(@fastq_file)
end

#eachObject


Iterate over all sequences




189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/scbi_fqbin/fastq_file_c.rb', line 189

def each
      
  rewind

 n,f,q,c=next_seq

  while (!n.nil?)
  yield(n,f,q,c)
  n,f,q,c=next_seq
  end

  rewind
  
end

#next_seqObject


Get next sequence




217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/scbi_fqbin/fastq_file_c.rb', line 217

def next_seq
  #init variables
  
  # namePtr = FFIString.new
  # fastaPtr = FFIString.new
  # qualPtr = FFIString.new
  # extrasPtr = FFIString.new
  
    
  if get_next_seq_fastq(@fastq_file,@namePtr,@fastaPtr,@qualPtr,@extrasPtr)==1
    
    seq_name=@namePtr.to_s
    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(namePtr)
    # free_string(qualPtr)
    # free_string(fastaPtr)
    # free_string(extrasPtr)
    
    return seq_name, fasta, qual, extras
    
  else

    # free_string(namePtr)
    # free_string(qualPtr)
    # free_string(fastaPtr)
    # free_string(extrasPtr)
    
    return nil
    # raise "Invalid sequence"
  end
  # res = read_fastq
  # return res
end

#open_fastqObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/scbi_fqbin/fastq_file_c.rb', line 67

def open_fastq()
   @fastq_file = FFI::MemoryPointer.new :pointer
   # puts @fastq_file.address
    open_file(@fasta_file_name,@fastq_file)
    # inspect_file_data_struct(@gzf_bin)
    # puts @fastq_file.address
    
    @fastq_file = @fastq_file.get_pointer(0)
    # puts "2"
    # puts @fastq_file.address
    # if @fastq_file.null?
    #   puts "ES NULLLLLLL"
    # end
    
end

#rewindObject

goto first position in file



205
206
207
208
209
210
211
212
# File 'lib/scbi_fqbin/fastq_file_c.rb', line 205

def rewind
   
   @num_seqs = 0;
   close_file(@fastq_file)
   open_fastq
   # @fastq_file.pos=0
  
end

#with_qual?Boolean



303
304
305
# File 'lib/scbi_fqbin/fastq_file_c.rb', line 303

def with_qual?
  true
end

#write_seq(seq_name, seq_fasta, seq_qual, comments = '') ⇒ Object

write sequence to file in sanger format



263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/scbi_fqbin/fastq_file_c.rb', line 263

def write_seq(seq_name,seq_fasta,seq_qual,comments='')
 name = ""
 
@fastq_file.puts("@#{seq_name} #{comments}")
@fastq_file.puts(seq_fasta)
@fastq_file.puts("+#{seq_name} #{comments}")

if seq_qual.is_a?(Array)
  @fastq_file.puts(seq_qual.map{|e| @from_phred.call(e)}.join)
 else
  @fastq_file.puts(seq_qual.split(/\s+/).map{|e| @from_phred.call(e.to_i)}.join)
end
  
end