]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/assemble_seq_velvet
90cf759b8c82c6fd947c6356a85961c2b20e2c19
[biopieces.git] / bp_bin / assemble_seq_velvet
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 Velvet.
28
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
30
31 require 'maasha/biopieces'
32 require 'maasha/fasta'
33
34 class Velvet
35   def initialize(directory, sequence_file, verbose)
36     @directory     = directory
37     @sequence_file = sequence_file
38     @verbose       = verbose
39   end
40
41   def run_velveth(kmer_min, kmer_max, type)
42     @kmer_min = kmer_min
43     @kmer_max = kmer_max
44
45     kmer = @kmer_min
46
47     while kmer <= @kmer_max
48       dir_velveth = File.join(@directory, "Kmer_#{kmer}")
49
50       Dir.mkdir(dir_velveth)
51
52       commands = []
53       commands << "velveth"
54       commands << dir_velveth
55       commands << kmer
56       commands << "-#{type}"
57       commands << "-fasta"
58       commands << @sequence_file
59
60       execute(commands)
61
62       kmer += 2
63     end
64   end
65
66   def run_velvetg(cov_cutoff, exp_cov)
67     Dir.glob("#{@directory}/Kmer_*").each do |dir_velveth|
68       files_velveth = Dir.glob("#{dir_velveth}/*")
69
70       dir_velvetg = File.join(dir_velveth, "Cov_cutoff_#{cov_cutoff}")
71
72       Dir.mkdir(dir_velvetg)
73       FileUtils.cp_r files_velveth, dir_velvetg
74       
75       commands = []
76       commands << "velvetg"
77       commands << dir_velvetg
78       commands << "-cov_cutoff #{cov_cutoff}"
79       commands << "-exp_cov #{exp_cov}"
80       commands << "-clean yes"
81
82       execute(commands)
83     end
84   end
85
86   def pick_best_assembly
87     list = []
88
89     Dir.glob("#{@directory}/Kmer_*/Cov_cutoff_*/contigs.fa").each do |file|
90       n50 = fasta_n50(file)
91       list << [file, n50]
92     end
93
94     list.sort_by { |e| e.last }.last.first
95   end
96
97   private
98
99   def execute(commands)
100     commands.unshift "nice -n 19"
101     commands.push "> /dev/null 2>&1" unless @verbose
102
103     command = commands.join(" ")
104
105     begin
106       system(command)
107       raise "Command failed: #{command}" unless $?.success?
108     rescue
109       $stderr.puts "Command failed: #{command}"
110     end
111   end
112
113   def fasta_n50(file)
114     total   = 0
115     lengths = []
116     count   = 0
117     n50     = 0
118
119     Fasta.open(file, mode="r") do |fasta_io|
120       fasta_io.each do |entry|
121         total   += entry.length
122         lengths << entry.length
123       end
124     end
125
126     lengths.sort.reverse.each do |length|
127       count += length
128
129       if count >= total * 0.50
130         n50 = length
131         break
132       end
133     end
134
135     n50
136   end
137 end
138
139 types       = 'short,shortPaired,long,longPaired'
140
141 casts = []
142 casts << {:long=>'directory',  :short=>'d', :type=>'dir',    :mandatory=>true,  :default=>nil,     :allowed=>nil,   :disallowed=>nil}
143 casts << {:long=>'type',       :short=>'t', :type=>'string', :mandatory=>true,  :default=>'short', :allowed=>types, :disallowed=>nil}
144 casts << {:long=>'kmer_min',   :short=>'k', :type=>'uint',   :mandatory=>true,  :default=>19,      :allowed=>nil,   :disallowed=>nil}
145 casts << {:long=>'kmer_max',   :short=>'K', :type=>'uint',   :mandatory=>true,  :default=>31,      :allowed=>nil,   :disallowed=>nil}
146 casts << {:long=>'cov_cutoff', :short=>'c', :type=>'float',  :mandatory=>true,  :default=>'auto',  :allowed=>nil,   :disallowed=>nil}
147 casts << {:long=>'exp_cov',    :short=>'e', :type=>'float',  :mandatory=>true,  :default=>'auto',  :allowed=>nil,   :disallowed=>nil}
148 casts << {:long=>'clean',      :short=>'X', :type=>'flag',   :mandatory=>false, :default=>nil,     :allowed=>nil,   :disallowed=>nil}
149
150 options = Biopieces.options_parse(ARGV, casts)
151
152 raise ArgumentError, "kmer_min #{options[:kmer_min]} must be uneven." if options[:kmer_min].even?
153 raise ArgumentError, "kmer_max #{options[:kmer_max]} must be uneven." if options[:kmer_max].even?
154 raise ArgumentError, "kmer_min >= kmer_max: #{options[:kmer_min]} >= #{options[:kmer_max]}" unless options[:kmer_max] > options[:kmer_min]
155
156 Dir.mkdir(options[:directory]) unless Dir.exists?(options[:directory])
157
158 file_fasta = File.join(options[:directory], "sequence_in.fna")
159
160 Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
161   Fasta.open(file_fasta, mode="w") do |fasta_io|
162     input.each_record do |record|
163       if record[:SEQ_NAME] and record[:SEQ]
164         seq = Seq.new_bp(record)
165         fasta_io.puts seq.to_fasta
166       end
167     end
168         end
169
170         unless File.size(file_fasta) == 0
171                 velvet = Velvet.new(options[:directory], file_fasta, options[:verbose])
172                 velvet.run_velveth(options[:kmer_min], options[:kmer_max], options[:type])
173                 velvet.run_velvetg(options[:cov_cutoff], options[:exp_cov])
174                 file_contigs = velvet.pick_best_assembly
175
176                 Fasta.open(file_contigs, mode="r") do |fasta_io|
177                         fasta_io.each do |entry|
178                                 output.puts entry.to_bp
179                         end
180                 end
181         end
182 end
183
184 FileUtils.remove_entry_secure file_fasta
185 FileUtils.remove_entry_secure options[:directory] if options[:clean]
186
187
188 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
189
190
191 __END__