]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/assemble_seq_idba
bd8b4a819b8986ccfe6f9c968f9f65bbb264043b
[biopieces.git] / bp_bin / assemble_seq_idba
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 IDBA.
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=>'scaffold',   :short=>'s', :type=>'flag', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
37 casts << {:long=>'metagenome', :short=>'m', :type=>'flag', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
38 casts << {:long=>'kmer_min',   :short=>'k', :type=>'uint', :mandatory=>false, :default=>25,  :allowed=>nil, :disallowed=>nil}
39 casts << {:long=>'kmer_max',   :short=>'K', :type=>'uint', :mandatory=>false, :default=>50,  :allowed=>nil, :disallowed=>nil}
40 casts << {:long=>'count_min',  :short=>'c', :type=>'uint', :mandatory=>false, :default=>2,   :allowed=>nil, :disallowed=>nil}
41 casts << {:long=>'cover',      :short=>'C', :type=>'uint', :mandatory=>false, :default=>0,   :allowed=>nil, :disallowed=>nil}
42 casts << {:long=>'pairs_min',  :short=>'p', :type=>'uint', :mandatory=>false, :default=>5,   :allowed=>nil, :disallowed=>nil}
43 casts << {:long=>'prefix_len', :short=>'P', :type=>'uint', :mandatory=>false, :default=>3,   :allowed=>nil, :disallowed=>nil}
44 casts << {:long=>'clean',      :short=>'X', :type=>'flag', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
45
46 options = Biopieces.options_parse(ARGV, casts)
47
48 Dir.mkdir(options[:directory]) unless Dir.exists?(options[:directory])
49
50 file_fasta = File.join(options[:directory], "IDBA") + ".fna"
51
52 count = 0
53
54 Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
55         Fasta.open(file_fasta, mode="w") do |fasta_io|
56                 input.each_record do |record|
57       if record[:SEQ_NAME] and record[:SEQ]
58         seq = Seq.new_bp(record)
59
60         if options[:scaffold] # we need to fix the sequence name for mate-pair IDBA
61           if seq.seq_name =~ /1$/
62             seq.seq_name = "read#{count}/1"
63           else
64             seq.seq_name = "read#{count}/2"
65
66             count += 1
67           end
68         end
69
70         fasta_io.puts seq.to_fasta
71       end
72                 end
73         end
74
75         unless File.size(file_fasta) == 0
76                 prefix = File.join(options[:directory], "IDBA")
77
78                 commands = []
79                 commands << "nice -n 19"
80
81     if options[:metagenome]
82                   commands << "metaidba"
83     else
84                   commands << "idba"
85     end
86
87                 commands << "--read #{file_fasta}"
88                 commands << "--output #{prefix}"
89                 commands << "--scaffold" if options[:scaffold]
90                 commands << "--mink #{options[:kmer_min]}"
91                 commands << "--maxk #{options[:kmer_max]}"
92                 commands << "--minCount #{options[:count_min]}"
93                 commands << "--cover #{options[:cover]}"
94                 commands << "--minPairs #{options[:pairs_min]}"
95                 commands << "--prefixLength #{options[:prefix_len]}"
96                 commands << "> /dev/null 2>&1" unless options[:verbose]
97
98                 command = commands.join(" ")
99
100                 begin
101                         system(command)
102                         raise "Command failed: #{command}" unless $?.success?
103                 rescue
104                         $stderr.puts "Command failed: #{command}"
105                 end
106
107     if options[:scaffold]
108                   file_contig = File.join(options[:directory], "IDBA") + "-contig-mate.fa"
109     else
110                   file_contig = File.join(options[:directory], "IDBA") + "-contig.fa"
111     end
112
113                 Fasta.open(file_contig, mode="r") do |fasta_io|
114                         fasta_io.each do |entry|
115                                 output.puts entry.to_bp
116                         end
117                 end
118         end
119 end
120
121 FileUtils.remove_entry_secure file_fasta
122 FileUtils.remove_entry_secure options[:directory] if options[:clean]
123
124
125 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
126
127
128 __END__