]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/assemble_seq_velvet
added assemble_seq_velvet draft biopiece
[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, "Velvet_#{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}/Velvet_*").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
83         execute(commands)
84       end
85     end
86   end
87
88   def pick_best_assembly
89     list = []
90
91     Dir.glob("#{@directory}/Velvet_*/Cov_cutoff_*/contigs.fa").each do |file|
92       n50 = fasta_n50(file)
93       list << [file, n50]
94     end
95
96     list.sort_by { |e| e.last }.last.first
97   end
98
99   private
100
101   def execute(commands)
102     commands.unshift "nice -n 19"
103     commands.push "> /dev/null 2>&1" unless @verbose
104
105     command = commands.join(" ")
106
107     begin
108       system(command)
109       raise "Command failed: #{command}" unless $?.success?
110     rescue
111       $stderr.puts "Command failed: #{command}"
112     end
113   end
114
115   def fasta_n50(file)
116     total   = 0
117     lengths = []
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     n50(total, lengths)
127   end
128
129   def n50(total, lengths)
130     count = 0
131     n50   = 0
132
133     lengths.sort.reverse.each do |length|
134       count += length
135
136       if count >= total * 0.50
137         n50 = length
138         break
139       end
140     end
141
142     n50
143   end
144 end
145
146 types       = 'short,shortPaired,long,longPaired'
147 cov_cutoffs = "2,4,8,16"
148
149 casts = []
150 casts << {:long=>'directory',  :short=>'d', :type=>'dir',    :mandatory=>true,  :default=>nil,         :allowed=>nil,   :disallowed=>nil}
151 casts << {:long=>'type',       :short=>'t', :type=>'string', :mandatory=>true,  :default=>'short',     :allowed=>types, :disallowed=>nil}
152 casts << {:long=>'kmer_min',   :short=>'k', :type=>'uint',   :mandatory=>true,  :default=>19,          :allowed=>nil,   :disallowed=>nil}
153 casts << {:long=>'kmer_max',   :short=>'K', :type=>'uint',   :mandatory=>true,  :default=>31,          :allowed=>nil,   :disallowed=>nil}
154 casts << {:long=>'cov_cutoff', :short=>'c', :type=>'list',   :mandatory=>true,  :default=>cov_cutoffs, :allowed=>nil,   :disallowed=>nil}
155 casts << {:long=>'exp_cov',    :short=>'e', :type=>'float',  :mandatory=>true,  :default=>'auto',      :allowed=>nil,   :disallowed=>nil}
156 casts << {:long=>'clean',      :short=>'x', :type=>'flag',   :mandatory=>false, :default=>nil,         :allowed=>nil,   :disallowed=>nil}
157
158 bp = Biopieces.new
159
160 options = bp.parse(ARGV, casts)
161
162 raise ArgumentError, "kmer_min #{options[:kmer_min]} must be uneven." if options[:kmer_min].even?
163 raise ArgumentError, "kmer_max #{options[:kmer_max]} must be uneven." if options[:kmer_max].even?
164 raise ArgumentError, "kmer_min >= kmer_max: #{options[:kmer_min]} >= #{options[:kmer_max]}" unless options[:kmer_max] > options[:kmer_min]
165
166 Dir.mkdir(options[:directory]) unless Dir.exists?(options[:directory])
167
168 file_fasta = [options[:directory], "sequence_in.fna"].join(File::SEPARATOR)
169
170 Fasta.open(file_fasta, mode="w") do |fasta_io|
171   bp.each_record do |record|
172     fasta_io.puts record
173   end
174 end
175
176 unless File.size(file_fasta) == 0
177   velvet = Velvet.new(options[:directory], file_fasta, options[:verbose])
178   velvet.run_velveth(options[:kmer_min], options[:kmer_max], options[:type])
179   velvet.run_velvetg(options[:cov_cutoff], options[:exp_cov])
180   file_contigs = velvet.pick_best_assembly
181
182   Fasta.open(file_contigs, mode="r") do |fasta_io|
183     fasta_io.each do |entry|
184       bp.puts entry.to_bp
185     end
186   end
187 end
188
189 FileUtils.remove_entry_secure options[:directory] if options[:clean]
190
191
192 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
193
194
195 __END__