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