]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/assemble_seq_velvet
rewrote assemble_seq_velvet
[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
45     commands << "#{kmer_min},#{kmer_max},#{kmer_step}"
46     commands << "-#{type}"
47     commands << "-fasta"
48     commands << @sequence_file
49
50     execute(commands)
51   end
52
53   def run_velvetg(cov_cutoff, exp_cov, paired)
54     commands = []
55     commands << "velvetg"
56     commands << @directory
57     commands << "-cov_cutoff #{cov_cutoff}"
58     commands << "-exp_cov #{exp_cov}"
59     commands << "-ins_length #{ins_length}" if paired
60     commands << "-clean yes"
61
62     execute(commands)
63   end
64
65   private
66
67   def execute(commands)
68     commands.push "> /dev/null 2>&1" unless @verbose
69
70     command = commands.join(" ")
71
72     begin
73       system(command)
74       raise "Command failed: #{command}" unless $?.success?
75     rescue
76       $stderr.puts "Command failed: #{command}"
77     end
78   end
79 end
80
81 types = 'short,shortPaired,long,longPaired'
82
83 casts = []
84 casts << {:long=>'directory',  :short=>'d', :type=>'dir',    :mandatory=>true,  :default=>nil,     :allowed=>nil,   :disallowed=>nil}
85 casts << {:long=>'type',       :short=>'t', :type=>'string', :mandatory=>true,  :default=>'short', :allowed=>types, :disallowed=>nil}
86 casts << {:long=>'kmer_min',   :short=>'k', :type=>'uint',   :mandatory=>true,  :default=>19,      :allowed=>nil,   :disallowed=>nil}
87 casts << {:long=>'kmer_max',   :short=>'K', :type=>'uint',   :mandatory=>true,  :default=>31,      :allowed=>nil,   :disallowed=>nil}
88 casts << {:long=>'kmer_step',  :short=>'s', :type=>'uint',   :mandatory=>true,  :default=>2,       :allowed=>nil,   :disallowed=>nil}
89 casts << {:long=>'cov_cutoff', :short=>'c', :type=>'float',  :mandatory=>true,  :default=>'auto',  :allowed=>nil,   :disallowed=>nil}
90 casts << {:long=>'exp_cov',    :short=>'e', :type=>'float',  :mandatory=>true,  :default=>'auto',  :allowed=>nil,   :disallowed=>nil}
91 casts << {:long=>'ins_length', :short=>'i', :type=>'uint',   :mandatory=>false, :default=>nil,     :allowed=>nil,   :disallowed=>nil}
92 casts << {:long=>'clean',      :short=>'X', :type=>'flag',   :mandatory=>false, :default=>nil,     :allowed=>nil,   :disallowed=>nil}
93
94 options = Biopieces.options_parse(ARGV, casts)
95
96 raise ArgumentError, "kmer_min #{options[:kmer_min]} must be uneven." if options[:kmer_min].even?
97 raise ArgumentError, "kmer_max #{options[:kmer_max]} must be uneven." if options[:kmer_max].even?
98 raise ArgumentError, "kmer_step #{options[:kmer_step]} must be even." unless options[:kmer_step].even?
99 raise ArgumentError, "kmer_min >= kmer_max: #{options[:kmer_min]} >= #{options[:kmer_max]}" unless options[:kmer_max] > options[:kmer_min]
100
101 Dir.mkdir(options[:directory]) unless Dir.exists?(options[:directory])
102
103 file_fasta = File.join(options[:directory], "sequence_in.fna")
104
105 Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
106   Fasta.open(file_fasta, mode="w") do |fasta_io|
107     input.each_record do |record|
108       if record[:SEQ_NAME] and record[:SEQ]
109         seq = Seq.new_bp(record)
110         fasta_io.puts seq.to_fasta
111       end
112     end
113         end
114
115         unless File.size(file_fasta) == 0
116                 velvet = Velvet.new(options[:directory], file_fasta, options[:verbose])
117                 velvet.run_velveth(options[:kmer_min], options[:kmer_max], options[:kmer_step], options[:type])
118                 velvet.run_velvetg(options[:cov_cutoff], options[:exp_cov], options[:type].match("Paired"))
119                 file_contigs = File.join(options[:directory], "contigs.fa")
120
121                 Fasta.open(file_contigs, mode="r") do |fasta_io|
122                         fasta_io.each do |entry|
123                                 output.puts entry.to_bp
124                         end
125                 end
126         end
127 end
128
129 FileUtils.remove_entry_secure file_fasta
130 FileUtils.remove_entry_secure options[:directory] if options[:clean]
131
132
133 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
134
135
136 __END__