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