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 # Use BWA to map sequences in the stream against a specified index.
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
31 require 'maasha/biopieces'
33 require 'maasha/fasta'
34 require 'maasha/fastq'
38 casts << {:long=>'index_name', :short=>'i', :type=>'string', :mandatory=>true, :default=>nil, :allowed=>nil, :disallowed=>nil}
39 casts << {:long=>'mismatches', :short=>'m', :type=>'uint', :mandatory=>false, :default=>0, :allowed=>"0,1,2,3", :disallowed=>nil}
40 casts << {:long=>'max_hits', :short=>'h', :type=>'uint', :mandatory=>false, :default=>30, :allowed=>nil, :disallowed=>'0'}
41 casts << {:long=>'cpus', :short=>'c', :type=>'uint', :mandatory=>false, :default=>1, :allowed=>nil, :disallowed=>'0'}
43 options = Biopieces.options_parse(ARGV, casts)
45 tmp_dir = Biopieces.mktmpdir
46 tmp_fq = File.join(tmp_dir, "bwa.fq")
47 tmp_sai = File.join(tmp_dir, "bwa.sai")
48 tmp_sam = File.join(tmp_dir, "bwa.sam")
50 # Class containing methods for executing BWA.
52 def initialize(fq_file, sai_file, sam_file, options)
60 # Method to run bwa aln.
63 @command << "-t #{@options[:cpus]}"
64 @command << "-R #{@options[:max_hits]}"
65 @command << "-f #{@sai_file}"
66 @command << @options[:index_name]
72 # Method to run bwa samse.
74 @command << "bwa samse"
75 @command << "-f #{@sam_file}"
76 @command << @options[:index_name]
85 # Method to execute a command using a system() call.
86 # The command is composed of bits from the @command variable.
88 @command.unshift "nice -n 19"
89 @command << "> /dev/null 2>&1" unless @options[:verbose]
91 command = @command.join(" ")
92 $stderr.puts "Running command: #{command}" if @options[:verbose]
94 raise "Command failed: #{command}" unless $?.success?
100 Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
101 Fastq.open(tmp_fq, 'w') do |io_fq|
102 input.each_record do |record|
105 if record[:SEQ_NAME] and record[:SEQ] and record[:SCORES]
106 entry = Seq.new_bp(record)
108 io_fq.puts entry.to_fastq
113 bwa = BWA.new(tmp_fq, tmp_sai, tmp_sam, options)
117 Sam.open(tmp_sam, 'r') do |io_sam|
118 io_sam.each do |entry|
119 output.puts Sam.to_bp(entry) unless entry[:RNAME] == '*'