]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/assemble_seq_velvet
adding kmer to contigs
[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, kmer_step, type)
42     commands = []
43     commands << "velveth"
44     commands << @directory + File::SEPARATOR
45     commands << "#{kmer_min},#{kmer_max + kmer_step},#{kmer_step}"   # FIXME - ugly fix for velvet bug
46     commands << "-create_binary"
47     commands << "-#{type}"
48     commands << "-fasta"
49     commands << @sequence_file
50
51     execute(commands)
52   end
53
54   def run_velvetg(ins_length, paired)
55     dirs = Dir.entries(@directory).select { |d| d.match(/^_/) }
56
57     dirs.each do |dir|
58       commands = []
59       commands << "velvetg"
60       commands << File.join(@directory, dir)
61       commands << "-cov_cutoff auto"
62       commands << "-exp_cov auto"
63       commands << "-ins_length #{ins_length}" if paired
64       commands << "-clean yes"
65       commands << "-min_contig_lgth 200"
66
67       execute(commands)
68     end
69   end
70
71   def pick_best_assembly
72     list = []
73
74     Dir.glob("#{@directory}/_*/contigs.fa").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 @verbose
86
87     command = commands.join(" ")
88
89     begin
90       $stderr.puts command if @verbose
91       system(command)
92       raise "Command failed: #{command}" unless $?.success?
93     rescue
94       $stderr.puts "Command failed: #{command}"
95     end
96   end
97
98   def fasta_n50(file)
99     total   = 0
100     lengths = []
101     count   = 0
102     n50     = 0
103
104     Fasta.open(file, mode="r") do |fasta_io|
105       fasta_io.each do |entry|
106         total   += entry.length
107         lengths << entry.length
108       end
109     end
110
111     lengths.sort.reverse.each do |length|
112       count += length
113
114       if count >= total * 0.50
115         n50 = length
116         break
117       end
118     end
119
120     n50
121   end
122 end
123
124 types = 'short,shortPaired,long,longPaired'
125
126 casts = []
127 casts << {:long=>'directory',  :short=>'d', :type=>'dir',    :mandatory=>true,  :default=>nil,     :allowed=>nil,   :disallowed=>nil}
128 casts << {:long=>'type',       :short=>'t', :type=>'string', :mandatory=>true,  :default=>'short', :allowed=>types, :disallowed=>nil}
129 casts << {:long=>'kmer_min',   :short=>'k', :type=>'uint',   :mandatory=>true,  :default=>19,      :allowed=>nil,   :disallowed=>nil}
130 casts << {:long=>'kmer_max',   :short=>'K', :type=>'uint',   :mandatory=>true,  :default=>31,      :allowed=>nil,   :disallowed=>nil}
131 casts << {:long=>'kmer_step',  :short=>'s', :type=>'uint',   :mandatory=>true,  :default=>2,       :allowed=>nil,   :disallowed=>nil}
132 casts << {:long=>'ins_length', :short=>'i', :type=>'uint',   :mandatory=>false, :default=>nil,     :allowed=>nil,   :disallowed=>nil}
133 casts << {:long=>'clean',      :short=>'X', :type=>'flag',   :mandatory=>false, :default=>nil,     :allowed=>nil,   :disallowed=>nil}
134
135 options = Biopieces.options_parse(ARGV, casts)
136
137 raise ArgumentError, "kmer_min #{options[:kmer_min]} must be uneven." if options[:kmer_min].even?
138 raise ArgumentError, "kmer_max #{options[:kmer_max]} must be uneven." if options[:kmer_max].even?
139 raise ArgumentError, "kmer_step #{options[:kmer_step]} must be even." unless options[:kmer_step].even?
140 raise ArgumentError, "kmer_min >= kmer_max: #{options[:kmer_min]} >= #{options[:kmer_max]}" unless options[:kmer_max] > options[:kmer_min]
141
142 Dir.mkdir(options[:directory]) unless Dir.exists?(options[:directory])
143
144 file_fasta = File.join(options[:directory], "sequence_in.fna")
145
146 Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
147   Fasta.open(file_fasta, mode="w") do |fasta_io|
148     input.each_record do |record|
149       if record[:SEQ_NAME] and record[:SEQ]
150         seq = Seq.new_bp(record)
151         fasta_io.puts seq.to_fasta
152       end
153     end
154         end
155
156         unless File.size(file_fasta) == 0
157                 velvet = Velvet.new(options[:directory], file_fasta, options[:verbose])
158                 velvet.run_velveth(options[:kmer_min], options[:kmer_max], options[:kmer_step], options[:type])
159                 velvet.run_velvetg(options[:ins_length], options[:type].match("Paired"))
160                 file_contigs = velvet.pick_best_assembly
161
162     kmer = file_contigs.match(/_\d+/)
163
164                 Fasta.open(file_contigs, mode="r") do |fasta_io|
165                   fasta_io.each do |entry|
166         entry.seq_name << "_kmer#{kmer}"
167                           output.puts entry.to_bp
168             end
169                 end
170         end
171 end
172
173 FileUtils.remove_entry_secure file_fasta
174 FileUtils.remove_entry_secure options[:directory] if options[:clean]
175
176 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
177
178
179 __END__