]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/read_fastq
added pair-end power to read_fastq
[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 not io1.eof? and not io2.eof?
69         entry1 = io1.get_entry
70         entry2 = io2.get_entry
71
72         if encoding == :auto
73           if entry1.qual_base33? or entry2.qual_base33?
74             encoding = :base_33
75           elsif entry1.qual_base64? or entry2.qual_base64?
76             encoding = :base_64
77           else
78             raise SeqError, "Could not auto-detect quality score encoding"
79           end
80         end
81
82         entry1.qual_convert!(encoding, :base_33)
83         entry2.qual_convert!(encoding, :base_33)
84         entry1.qual_coerce!(:base_33)
85         entry2.qual_coerce!(:base_33)
86
87         if num < MAX_TEST
88           raise SeqError, "Quality score outside valid range" unless entry1.qual_valid?(:base_33)
89           raise SeqError, "Quality score outside valid range" unless entry2.qual_valid?(:base_33)
90         end
91
92         output.puts entry1.to_bp
93         output.puts entry2.to_bp
94
95         num += 2
96
97         if options[:num] and num >= options[:num]
98           last = true
99           break
100         end
101       end
102
103       io1.close
104       io2.close
105
106       break if last
107     end
108   elsif options[:data_in]
109     options[:data_in].each do |file|
110       Fastq.open(file, 'r') do |fastq|
111         fastq.each do |entry|
112           if encoding == :auto
113             if entry.qual_base33?
114               encoding = :base_33
115             elsif entry.qual_base64?
116               encoding = :base_64
117             else
118               raise SeqError, "Could not auto-detect quality score encoding"
119             end
120           end
121
122           entry.qual_convert!(encoding, :base_33)
123           entry.qual_coerce!(:base_33)
124
125           if num < MAX_TEST
126             raise SeqError, "Quality score outside valid range" unless entry.qual_valid?(:base_33)
127           end
128
129           output.puts entry.to_bp
130           num += 1
131
132           if options[:num] == num
133             last = true
134             break
135           end
136         end
137       end
138
139       break if last
140     end
141   end
142 end
143
144
145 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
146
147
148 __END__