]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/assemble_pairs
rewrite of FASTQ internals
[biopieces.git] / bp_bin / assemble_pairs
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 # Assemble ordered overlapping pair-end sequences in the stream.
28
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
30
31 require 'maasha/biopieces'
32 require 'maasha/fastq'
33 require 'pp'
34
35 casts = []
36 casts << {:long=>'overlap', :short=>'o', :type=>'uint', :mandatory=>false, :default=>1, :allowed=>nil, :disallowed=>"0"}
37
38 options = Biopieces.options_parse(ARGV, casts)
39
40 tmpdir   = Biopieces.mktmpdir
41 file_in1 = File.join(tmpdir, "in1.fq")
42 file_in2 = File.join(tmpdir, "in2.fq")
43 file_out = File.join(tmpdir, "out.fq")
44
45 io_in1 = Fastq.open(file_in1, 'w')
46 io_in2 = Fastq.open(file_in2, 'w')
47
48 name1  = ""
49 name2  = ""
50 entry1 = ""
51 entry2 = ""
52
53 Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
54   input.each_record do |record|
55     if record[:SEQ_NAME] and record[:SEQ] and record[:SCORES]
56       entry = Seq.new_bp(record)
57
58       case entry.seq_name
59       when /^(.+)\/(\d)$/   # Illumina 1.5
60         name = $1
61         pair = $2.to_i
62         type = 15
63       when /^(.+) (\d):/    # Illumina 1.8
64         name = $1
65         pair = $2.to_i
66         type = 18
67       else
68         $stderr.puts "WARNING: Unmatched sequence name: #{entry.seq_name}"
69       end
70
71       if pair == 1
72         name1  = name.dup
73         entry1 = entry.dup
74       else
75         name2  = name.dup
76         entry2 = entry.dup
77       end
78
79       if name1 != nil and name1 == name2
80         io_in1.puts entry1.to_fastq
81         io_in2.puts entry2.to_fastq
82       end
83     end
84   end
85
86   io_in1.close
87   io_in2.close
88
89   cmd = "pandaseq"
90   cmd << " -F"
91   cmd << " -B"
92   cmd << " -N"
93   cmd << " -o #{options[:overlap]}"
94   cmd << " -f #{file_in1}"
95   cmd << " -r #{file_in2}"
96   cmd << " > #{file_out}"
97   cmd << " 2> /dev/null" unless options[:verbose]
98
99   $stderr.puts cmd if options[:verbose]
100
101   system(cmd)
102
103   raise RunTimeError "Error: command failed: #{cmd}" unless $?.success?
104
105   Fastq.open(file_out) do |ios|
106     ios.each do |entry|
107       output.puts entry.to_bp
108     end
109   end
110 end
111
112
113 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
114
115
116 __END__