Class: FastqFile

Inherits:
Object
  • Object
show all
Defined in:
lib/scbi_fastq/fastq_file.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) ⇒ FastqFile


Initialize instance




24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
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
# File 'lib/scbi_fastq/fastq_file.rb', line 24

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

   
  if mode.upcase.index('W.GZ')
    @fastq_file = Zlib::GzipWriter.open(fasta_file_name)
  elsif 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
      if ScbiZcatFile.gz_file?(fasta_file_name)
        #@fastq_file = Zlib::GzipReader.open(fasta_file_name)
        #@fastq_file = MultiGzReader.new(fasta_file_name)
        @fastq_file = ScbiZcatFile.new(fasta_file_name)
        # puts "GZIP file detected"
      else
        @fastq_file = File.open(fasta_file_name,'r')
        # puts "NORMAL file detected"
      end
      
    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.



19
20
21
# File 'lib/scbi_fastq/fastq_file.rb', line 19

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



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/scbi_fastq/fastq_file.rb', line 176

def self.to_fastq(seq_name,seq_fasta,seq_qual,comments='')

  res=[]

  name = ""

  res << ("@#{seq_name} #{comments}")
  res << (seq_fasta)
  res << ("+")
  #res << ("+#{seq_name} #{comments}")

  if !seq_qual.empty?
    # 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
  else # no qual provided, use a default value
    q='D'*seq_fasta.length;
    res << q
  end

  return res
end

Instance Method Details

#closeObject



118
119
120
# File 'lib/scbi_fastq/fastq_file.rb', line 118

def close
  @fastq_file.close
end

#eachObject


Iterate over all sequences




126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/scbi_fastq/fastq_file.rb', line 126

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




151
152
153
154
155
# File 'lib/scbi_fastq/fastq_file.rb', line 151

def next_seq
  #init variables
  res = read_fastq
  return res
end

#rewindObject

goto first position in file



142
143
144
145
146
# File 'lib/scbi_fastq/fastq_file.rb', line 142

def rewind
  @num_seqs = 0 ;
  # @fastq_file.pos=0
  @fastq_file.rewind
end

#with_qual?Boolean

Returns:

  • (Boolean)


205
206
207
# File 'lib/scbi_fastq/fastq_file.rb', line 205

def with_qual?
  true
end

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

write sequence to file in sanger format



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/scbi_fastq/fastq_file.rb', line 158

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("+")
  #@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