]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/bowtie2_seq
fixed seq qual length check
[biopieces.git] / bp_bin / bowtie2_seq
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 # Run bowtie2 to map sequences in the stream.
28
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
30
31 require 'maasha/biopieces'
32 require 'maasha/fasta'
33 require 'maasha/sam'
34
35 casts = []
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"}
39
40 options = Biopieces.options_parse(ARGV, casts)
41
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")
47
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|
52         output.puts record
53
54         if record[:SEQ_NAME] and record[:SEQ]
55           fasta_io.puts Seq.new_bp(record).to_fasta
56         end
57       end
58     end
59   else
60     Fasta.open(in1_file, 'w') do |fasta1_io|
61       Fasta.open(in2_file, 'w') do |fasta2_io|
62         input.each_record do |record|
63           output.puts record
64
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
71             else
72               raise "Error: could not identify paired read in: #{record[:SEQ_NAME]}"
73             end
74           end
75         end
76       end
77     end
78   end
79
80   cmd = []
81   cmd << "bowtie2"
82   cmd << "-f"
83   cmd << "--quiet" unless options[:verbose]
84   cmd << "--threads #{options[:cpus]}"
85   cmd << "-x #{options[:index_name]}"
86
87   if options[:type] == "single"
88     cmd << "-U #{in_file}"
89   else
90     cmd << "-1 #{in1_file}"
91     cmd << "-2 #{in2_file}"
92   end
93
94   cmd << "-S #{out_file}"
95
96   c = cmd.join(" ")
97
98   $stderr.puts c if options[:verbose]
99
100   system(c)
101
102   raise "Error: executing #{c}" unless $?.success?
103
104   Sam.open(out_file, 'r') do |sam_io|
105     sam_io.each do |entry|
106       output.puts Sam.to_bp(entry)
107     end
108   end
109 end
110
111
112 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
113
114
115 __END__