]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/assemble_seq_ray
fixed seq qual length check
[biopieces.git] / bp_bin / assemble_seq_ray
1 #!/usr/bin/env ruby
2
3 # Copyright (C) 2007-2011 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 sequences in the stream using Ray.
28
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
30
31 require 'maasha/biopieces'
32 require 'maasha/fasta'
33
34 class Ray
35   def initialize(directory, sequence_file, options)
36     @directory     = directory
37     @sequence_file = sequence_file
38     @options       = options
39   end
40
41   def run
42     kmer = @options[:kmer_min]
43
44                 if @options[:type] == "single"
45                         type = "-s"
46                 else
47                         type = "-i"
48                 end
49
50     while kmer <= @options[:kmer_max]
51       dir_ray = File.join(@directory, "Kmer_#{kmer}")
52
53       Dir.mkdir(dir_ray)
54
55       commands = []
56         commands << "nice -n 19"
57       commands << "mpirun"
58       commands << "-np #{@options[:cpus]}"
59       commands << "Ray"
60       commands << "-o " + File.join(dir_ray, "Ray")
61       commands << "-k #{kmer}"
62       commands << type
63       commands << @sequence_file
64
65       execute(commands)
66
67       kmer += 2
68     end
69   end
70
71   def pick_best_assembly
72     list = []
73
74     Dir.glob("#{@directory}/Kmer_*/*.fasta").each do |file|
75       n50 = fasta_n50(file)
76       list << [file, n50]
77     end
78
79     list.sort_by { |e| e.last }.last.first
80   end
81
82   private
83
84   def execute(commands)
85     commands.push "> /dev/null 2>&1" unless @options[:verbose]
86
87     command = commands.join(" ")
88
89     begin
90       system(command)
91       raise "Command failed: #{command}" unless $?.success?
92     rescue
93       $stderr.puts "Command failed: #{command}"
94     end
95   end
96
97   def fasta_n50(file)
98     total   = 0
99     lengths = []
100     count   = 0
101     n50     = 0
102
103     Fasta.open(file, "r") do |fasta_io|
104       fasta_io.each do |entry|
105         total   += entry.length
106         lengths << entry.length
107       end
108     end
109
110     lengths.sort.reverse.each do |length|
111       count += length
112
113       if count >= total * 0.50
114         n50 = length
115         break
116       end
117     end
118
119     n50
120   end
121 end
122
123 casts = []
124 casts << {:long=>'directory',  :short=>'d', :type=>'dir',    :mandatory=>true,  :default=>nil,      :allowed=>nil,             :disallowed=>nil}
125 casts << {:long=>'type',       :short=>'t', :type=>'string', :mandatory=>true,  :default=>'single', :allowed=>'single,paired', :disallowed=>nil}
126 casts << {:long=>'cpus',       :short=>'c', :type=>'uint',   :mandatory=>true,  :default=>1,        :allowed=>nil,             :disallowed=>'0'}
127 casts << {:long=>'kmer_min',   :short=>'k', :type=>'uint',   :mandatory=>true,  :default=>19,       :allowed=>nil,             :disallowed=>nil}
128 casts << {:long=>'kmer_max',   :short=>'K', :type=>'uint',   :mandatory=>true,  :default=>31,       :allowed=>nil,             :disallowed=>nil}
129 casts << {:long=>'clean',      :short=>'X', :type=>'flag',   :mandatory=>false, :default=>nil,      :allowed=>nil,             :disallowed=>nil}
130
131 options = Biopieces.options_parse(ARGV, casts)
132
133 raise ArgumentError, "kmer_min #{options[:kmer_min]} must be uneven." if options[:kmer_min].even?
134 raise ArgumentError, "kmer_max #{options[:kmer_max]} must be uneven." if options[:kmer_max].even?
135 raise ArgumentError, "kmer_min > kmer_max: #{options[:kmer_min]} > #{options[:kmer_max]}" if options[:kmer_min] > options[:kmer_max]
136
137 Dir.mkdir(options[:directory]) unless Dir.exists?(options[:directory])
138
139 file_fasta = File.join(options[:directory], "sequence_in.fasta")
140
141 Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
142         Fasta.open(file_fasta, "w") do |fasta_io|
143                 input.each_record do |record|
144       if record[:SEQ_NAME] and record[:SEQ]
145         seq = Seq.new_bp(record)
146
147                           fasta_io.puts seq.to_fasta
148       end
149                 end
150         end
151
152         unless File.size(file_fasta) == 0
153                 ray = Ray.new(options[:directory], file_fasta, options)
154                 ray.run
155                 file_contigs = ray.pick_best_assembly
156
157                 Fasta.open(file_contigs, "r") do |fasta_io|
158                         fasta_io.each do |entry|
159                                 output.puts entry.to_bp
160                         end
161                 end
162         end
163 end
164
165 FileUtils.remove_entry_secure file_fasta
166 FileUtils.remove_entry_secure options[:directory] if options[:clean]
167
168
169 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
170
171
172 __END__