3 # Copyright (C) 2007-2013 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 # Run bowtie2 to map sequences in the stream.
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
31 require 'maasha/biopieces'
32 require 'maasha/fasta'
36 casts << {:long=>'index_name', :short=>'i', :type=>'string', :mandatory=>true, :default=>nil, :allowed=>nil, :disallowed=>nil}
37 casts << {:long=>'type', :short=>'t', :type=>'string', :mandatory=>false, :default=>"single", :allowed=>"single,paired", :disallowed=>nil}
38 casts << {:long=>'cpus', :short=>'c', :type=>'uint', :mandatory=>false, :default=>1, :allowed=>nil, :disallowed=>"0"}
40 options = Biopieces.options_parse(ARGV, casts)
42 tmp_dir = Biopieces.mktmpdir
43 in_file = File.join(tmp_dir, "in.fna")
44 in1_file = File.join(tmp_dir, "in1.fna")
45 in2_file = File.join(tmp_dir, "in2.fna")
46 out_file = File.join(tmp_dir, "out.sam")
48 Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
49 if options[:type] == "single"
50 Fasta.open(in_file, 'w') do |fasta_io|
51 input.each_record do |record|
54 if record[:SEQ_NAME] and record[:SEQ]
55 fasta_io.puts Seq.new_bp(record).to_fasta
60 Fasta.open(in1_file, 'w') do |fasta1_io|
61 Fasta.open(in2_file, 'w') do |fasta2_io|
62 input.each_record do |record|
65 if record[:SEQ_NAME] and record[:SEQ]
66 case record[:SEQ_NAME]
67 when /\/1$/ then fasta1_io.puts Seq.new_bp(record).to_fasta
68 when / 1:/ then fasta1_io.puts Seq.new_bp(record).to_fasta
69 when /\/2$/ then fasta2_io.puts Seq.new_bp(record).to_fasta
70 when / 2:/ then fasta2_io.puts Seq.new_bp(record).to_fasta
72 raise "Error: could not identify paired read in: #{record[:SEQ_NAME]}"
83 cmd << "--quiet" unless options[:verbose]
84 cmd << "--threads #{options[:cpus]}"
85 cmd << "-x #{options[:index_name]}"
87 if options[:type] == "single"
88 cmd << "-U #{in_file}"
90 cmd << "-1 #{in1_file}"
91 cmd << "-2 #{in2_file}"
94 cmd << "-S #{out_file}"
98 $stderr.puts c if options[:verbose]
102 raise "Error: executing #{c}" unless $?.success?
104 Sam.open(out_file, 'r') do |sam_io|
105 sam_io.each do |entry|
106 output.puts Sam.to_bp(entry)
112 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<