class Bio::Sequence::Format::Formatter::Fastq_sanger

INTERNAL USE ONLY, YOU SHOULD NOT USE THIS CLASS.

FASTQ format output class for Bio::Sequence.

The default FASTQ format is fastq-sanger.

Public Class Methods

new() click to toggle source

INTERNAL USE ONLY, YOU SHOULD NOT CALL THIS METHOD.

Creates a new Fasta format generater object from the sequence.


Arguments:

  • sequence: Bio::Sequence object

  • (optional) :repeat_title => (true or false) if true, repeating title in the “+” line; if not true, “+” only (default false)

  • (optional) :width => width: (Fixnum) width to wrap sequence and quality lines; nil to prevent wrapping (default nil)

  • (optional) :title => title: (String) completely replaces title line with the title (default nil)

  • (optional) :default_score => score: (Integer) default score for bases that have no valid quality scores or error probabilities; false or nil means the lowest score, true means the highest score (default nil)

   # File lib/bio/db/fastq/format_fastq.rb
31 def initialize; end

Public Instance Methods

output() click to toggle source

INTERNAL USE ONLY, YOU SHOULD NOT CALL THIS METHOD.

Output the FASTQ format string of the sequence.

Currently, this method is used in Bio::Sequence#output like so,

s = Bio::Sequence.new('atgc')
puts s.output(:fastq_sanger)

Returns

String object

   # File lib/bio/db/fastq/format_fastq.rb
43 def output
44   title = @options[:title]
45   width = @options.has_key?(:width) ? @options[:width] : nil
46   seq = @sequence.seq.to_s
47   entry_id = @sequence.entry_id || 
48     "#{@sequence.primary_accession}.#{@sequence.sequence_version}"
49   definition = @sequence.definition
50   unless title then
51     title = definition.to_s
52     unless title[0, entry_id.length] == entry_id and
53         /\s/ =~ title[entry_id.length, 1].to_s then
54       title = "#{entry_id} #{title}"
55     end
56   end
57   title2 = @options[:repeat_title] ? title : ''
58   qstr = fastq_quality_string(seq, @options[:default_score])
59 
60   "@#{title}\n" +
61     if width then
62       seq.gsub(Regexp.new(".{1,#{width}}"), "\\0\n")
63     else
64       seq + "\n"
65     end +
66     "+#{title2}\n" +
67     if width then
68       qstr.gsub(Regexp.new(".{1,#{width}}"), "\\0\n")
69     else
70       qstr + "\n"
71     end
72 end

Private Instance Methods

fastq_format_data() click to toggle source
   # File lib/bio/db/fastq/format_fastq.rb
75 def fastq_format_data
76   Bio::Fastq::FormatData::FASTQ_SANGER.instance
77 end
fastq_quality_scores(seq) click to toggle source
    # File lib/bio/db/fastq/format_fastq.rb
 94 def fastq_quality_scores(seq)
 95   return [] if seq.length <= 0
 96   fmt = fastq_format_data
 97   # checks quality_scores
 98   qsc = @sequence.quality_scores
 99   qsc_type = @sequence.quality_score_type
100   if qsc and qsc_type and
101       qsc_type == fmt.quality_score_type and
102       qsc.size >= seq.length then
103     return qsc
104   end
105   
106   # checks error_probabilities
107   ep = @sequence.error_probabilities
108   if ep and ep.size >= seq.length then
109     return fmt.p2q(ep[0, seq.length])
110   end
111 
112   # If quality score type of the sequence is nil, regarded as :phred.
113   qsc_type ||= :phred
114 
115   # checks if scores can be converted
116   if qsc and qsc.size >= seq.length then
117     case [ qsc_type, fmt.quality_score_type ]
118     when [ :phred, :solexa ]
119       return fmt.convert_scores_from_phred_to_solexa(qsc[0, seq.length])
120     when [ :solexa, :phred ]
121       return fmt.convert_scores_from_solexa_to_phred(qsc[0, seq.length])
122     end
123   end
124 
125   # checks quality scores type
126   case qsc_type
127   when :phred, :solexa
128     #does nothing
129   else
130     qsc_type = nil
131     qsc = nil
132   end
133 
134   # collects piece of information
135   qsc_cov = qsc ? qsc.size.quo(seq.length) : 0
136   ep_cov = ep ? ep.size.quo(seq.length) : 0
137   if qsc_cov > ep_cov then
138     case [ qsc_type, fmt.quality_score_type ]
139     when [ :phred, :phred ], [ :solexa, :solexa ]
140       return qsc
141     when [ :phred, :solexa ]
142       return fmt.convert_scores_from_phred_to_solexa(qsc)
143     when [ :solexa, :phred ]
144       return fmt.convert_scores_from_solexa_to_phred(qsc)
145     end
146   elsif ep_cov > qsc_cov then
147     return fmt.p2q(ep)
148   end
149 
150   # if no information, returns empty array
151   return []
152 end
fastq_quality_string(seq, default_score) click to toggle source
   # File lib/bio/db/fastq/format_fastq.rb
79 def fastq_quality_string(seq, default_score)
80   sc = fastq_quality_scores(seq)
81   if sc.size < seq.length then
82     if default_score == true then
83       # when true, the highest score
84       default_score = fastq_format_data.score_range.end
85     else
86       # when false or nil, the lowest score
87       default_score ||= fastq_format_data.score_range.begin
88     end
89     sc = sc + ([ default_score ] * (seq.length - sc.size))
90   end
91   fastq_format_data.scores2str(sc)
92 end