]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/assemble_seq_idba
e600c0392143a9d8fe4df10ac72df00aa0f8f3aa
[biopieces.git] / bp_bin / assemble_seq_idba
1 #!/usr/bin/env ruby
2
3 # Copyright (C) 2007-2012 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 IDBA-UD.
28
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
30
31 require 'maasha/biopieces'
32 require 'maasha/fasta'
33
34 casts = []
35 casts << {:long=>'directory',  :short=>'d', :type=>'dir',  :mandatory=>true,  :default=>nil, :allowed=>nil, :disallowed=>nil}
36 casts << {:long=>'kmer_min',   :short=>'k', :type=>'uint', :mandatory=>false, :default=>20,  :allowed=>nil, :disallowed=>nil}
37 casts << {:long=>'kmer_max',   :short=>'K', :type=>'uint', :mandatory=>false, :default=>100, :allowed=>nil, :disallowed=>nil}
38 casts << {:long=>'count_min',  :short=>'c', :type=>'uint', :mandatory=>false, :default=>2,   :allowed=>nil, :disallowed=>nil}
39 casts << {:long=>'pairs_min',  :short=>'p', :type=>'uint', :mandatory=>false, :default=>3,   :allowed=>nil, :disallowed=>nil}
40 casts << {:long=>'prefix_len', :short=>'P', :type=>'uint', :mandatory=>false, :default=>3,   :allowed=>nil, :disallowed=>nil}
41 casts << {:long=>'cpus',       :short=>'C', :type=>'uint', :mandatory=>false, :default=>0,   :allowed=>nil, :disallowed=>nil}
42 casts << {:long=>'clean',      :short=>'X', :type=>'flag', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
43
44 options = Biopieces.options_parse(ARGV, casts)
45
46 Dir.mkdir(options[:directory]) unless Dir.exists?(options[:directory])
47
48 file_fasta = File.join(options[:directory], "IDBA-UD") + ".fna"
49
50 count = 0
51
52 Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
53         Fasta.open(file_fasta, "w") do |fasta_io|
54                 input.each_record do |record|
55       if record[:SEQ_NAME] and record[:SEQ]
56         seq = Seq.new_bp(record)
57
58         fasta_io.puts seq.to_fasta
59       end
60                 end
61         end
62
63         unless File.size(file_fasta) == 0
64                 prefix = File.join(options[:directory], "IDBA-UD")
65
66                 commands = []
67                 commands << "nice -n 19"
68                 commands << "idba_ud"
69                 commands << "--read #{file_fasta}"
70                 commands << "--out #{prefix}"
71                 commands << "--mink #{options[:kmer_min]}"
72                 commands << "--maxk #{options[:kmer_max]}"
73                 commands << "--min_count #{options[:count_min]}"
74                 commands << "--min_pairs #{options[:pairs_min]}"
75                 commands << "--prefix #{options[:prefix_len]}"
76                 commands << "--num_threads #{options[:cpus]}"
77                 commands << "> /dev/null 2>&1" unless options[:verbose]
78
79                 command = commands.join(" ")
80
81           system(command)
82           raise "Command failed: #{command}" unless $?.success?
83
84     file_contig = File.join(options[:directory], "IDBA-UD", "contig.fa")
85
86                 Fasta.open(file_contig, mode="r") do |fasta_io|
87                         fasta_io.each do |entry|
88                                 output.puts entry.to_bp
89                         end
90                 end
91         end
92 end
93
94 FileUtils.remove_entry_secure file_fasta
95 FileUtils.remove_entry_secure options[:directory] if options[:clean]
96
97
98 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
99
100
101 __END__