]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/read_fastq
fixed seq qual length check
[biopieces.git] / bp_bin / read_fastq
1 #!/usr/bin/env ruby
2
3 # Copyright (C) 2007-2013 Martin A. Hansen.
4
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 # http://www.gnu.org/copyleft/gpl.html
20
21 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
22
23 # This program is part of the Biopieces framework (www.biopieces.org).
24
25 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
26
27 # Read FASTQ entries from one or more files.
28
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
30
31 require 'maasha/biopieces'
32 require 'maasha/fastq'
33
34 MAX_TEST = 1_000
35
36 allowed_enc = 'auto,base_33,base_64'
37
38 casts = []
39 casts << {:long=>'data_in',  :short=>'i', :type=>'files!', :mandatory=>false, :default=>nil,    :allowed=>nil,         :disallowed=>nil}
40 casts << {:long=>'data_in2', :short=>'j', :type=>'files!', :mandatory=>false, :default=>nil,    :allowed=>nil,         :disallowed=>nil}
41 casts << {:long=>'num',      :short=>'n', :type=>'uint',   :mandatory=>false, :default=>nil,    :allowed=>nil,         :disallowed=>'0'}
42 casts << {:long=>'encoding', :short=>'e', :type=>'string', :mandatory=>false, :default=>'auto', :allowed=>allowed_enc, :disallowed=>nil}
43
44 options = Biopieces.options_parse(ARGV, casts)
45
46 encoding = options[:encoding].to_sym
47
48 num  = 0
49 last = false
50
51 Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
52   unless options[:data_in] and options[:data_in].first == '-'
53     input.each_record do |record|
54       output.puts record
55     end
56   end
57
58   if options[:data_in] and options[:data_in2]
59     raise "data_in files differ" if options[:data_in].size != options[:data_in2].size
60
61     (0 ... options[:data_in].size).each do |i|
62       file1 = options[:data_in][i]
63       file2 = options[:data_in2][i]
64
65       io1 = Fastq.open(file1, 'r')
66       io2 = Fastq.open(file2, 'r')
67
68       while entry1 = io1.get_entry and entry2 = io2.get_entry
69         if encoding == :auto
70           if entry1.qual_base33? or entry2.qual_base33?
71             encoding = :base_33
72           elsif entry1.qual_base64? or entry2.qual_base64?
73             encoding = :base_64
74           else
75             raise SeqError, "Could not auto-detect quality score encoding"
76           end
77         end
78
79         entry1.qual_convert!(encoding, :base_33)
80         entry2.qual_convert!(encoding, :base_33)
81         entry1.qual_coerce!(:base_33)
82         entry2.qual_coerce!(:base_33)
83
84         if num < MAX_TEST
85           raise SeqError, "Quality score outside valid range" unless entry1.qual_valid?(:base_33)
86           raise SeqError, "Quality score outside valid range" unless entry2.qual_valid?(:base_33)
87         end
88
89         output.puts entry1.to_bp
90         output.puts entry2.to_bp
91
92         num += 2
93
94         if options[:num] and num >= options[:num]
95           last = true
96           break
97         end
98       end
99
100       io1.close
101       io2.close
102
103       break if last
104     end
105   elsif options[:data_in]
106     options[:data_in].each do |file|
107       Fastq.open(file, 'r') do |fastq|
108         fastq.each do |entry|
109           if encoding == :auto
110             if entry.qual_base33?
111               encoding = :base_33
112             elsif entry.qual_base64?
113               encoding = :base_64
114             else
115               raise SeqError, "Could not auto-detect quality score encoding"
116             end
117           end
118
119           entry.qual_convert!(encoding, :base_33)
120           entry.qual_coerce!(:base_33)
121
122           if num < MAX_TEST
123             raise SeqError, "Quality score outside valid range" unless entry.qual_valid?(:base_33)
124           end
125
126           output.puts entry.to_bp
127           num += 1
128
129           if options[:num] == num
130             last = true
131             break
132           end
133         end
134       end
135
136       break if last
137     end
138   end
139 end
140
141
142 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
143
144
145 __END__