3 # Copyright (C) 2007-2012 Martin A. Hansen.
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.
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.
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.
19 # http://www.gnu.org/copyleft/gpl.html
21 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
23 # This program is part of the Biopieces framework (www.biopieces.org).
25 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
27 # Order records with pair end sequence data.
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
33 require 'maasha/biopieces'
35 def files_merge(file_in1, file_in2)
36 file_out = "#{file_in1}.merge"
38 in1, out1 = Biopieces.open(file_in1, file_out)
39 in2, out2 = Biopieces.open(file_in2)
41 record1 = in1.get_entry
42 record2 = in2.get_entry
45 if record1 and record2
46 if record1[:SEQ_NAME] < record2[:SEQ_NAME]
48 record1 = in1.get_entry
51 record2 = in2.get_entry
55 record1 = in1.get_entry
58 record2 = in2.get_entry
64 File.rename(file_out, file_in1)
69 casts << {:long=>'block_size', :short=>'b', :type=>'uint', :mandatory=>false, :default=>10_000_000, :allowed=>nil, :disallowed=>'0'}
71 options = Biopieces.options_parse(ARGV, casts)
72 tmpdir = Biopieces.mktmpdir
74 Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
79 input.each do |record|
82 block_size += record.to_s.size
84 if block_size >= options[:block_size]
85 block.sort_by! { |r| r[:SEQ_NAME] }
87 tmpfile = File.join(tmpdir, "#{file_count}.stream")
89 $stderr.puts "Writing tmp file #{tmpfile}" if options[:verbose]
90 Biopieces.open(nil, tmpfile) do |tmpin, tmpout|
91 block.each { |r| tmpout.puts r }
100 block.sort_by! { |r| r[:SEQ_NAME] }
102 tmpfile = File.join(tmpdir, "#{file_count}.stream")
104 $stderr.puts "Writing tmp file #{tmpfile}" if options[:verbose]
105 Biopieces.open(nil, tmpfile) do |tmpin, tmpout|
106 block.each { |r| tmpout.puts r }
110 while files = Dir.glob(File.join(tmpdir, "*.stream")) and files.size > 1
111 0.step(files.size - 2, 2) do |i|
112 $stderr.puts "Merging files #{files[i]} #{files[i + 1]}" if options[:verbose]
113 files_merge(files[i], files[i + 1])
117 Biopieces.open(File.join(tmpdir, "0.stream"), options[:stream_out]) do |input, output|
118 record1 = input.get_entry
119 record2 = input.get_entry
121 while record1 and record2
122 case record1[:SEQ_NAME]
123 when /^(.+)\/(\d)$/ # Illumina 1.5
126 when /^(.+) (\d):/ # Illumina 1.8
130 $stderr.puts "WARNING: Unmatched sequence name: #{record1[:SEQ_NAME]}"
133 case record2[:SEQ_NAME]
134 when /^(.+)\/(\d)$/ # Illumina 1.5
137 when /^(.+) (\d):/ # Illumina 1.8
141 $stderr.puts "WARNING: Unmatched sequence name: #{record1[:SEQ_NAME]}"
144 if name1 == name2 and (pair1 - pair2).abs == 1
145 record1[:ORDER] = "paired"
146 record2[:ORDER] = "paired"
149 record1 = input.get_entry
150 record2 = input.get_entry
152 record1[:ORDER] = "orphan #{pair1}"
155 record2 = input.get_entry
160 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<