# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
-require 'biopieces'
-require 'fasta'
+require 'maasha/biopieces'
+require 'maasha/fasta'
casts = []
-casts << {:long=>'name', :short=>'n', :type=>'string', :mandatory=>true, :default=>nil, :allowed=>nil, :disallowed=>nil}
-casts << {:long=>'directory', :short=>'d', :type=>'dir', :mandatory=>true, :default=>nil, :allowed=>nil, :disallowed=>nil}
-casts << {:long=>'scaffold', :short=>'s', :type=>'flag', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
-casts << {:long=>'kmer_min', :short=>'k', :type=>'uint', :mandatory=>false, :default=>25, :allowed=>nil, :disallowed=>nil}
-casts << {:long=>'kmer_max', :short=>'K', :type=>'uint', :mandatory=>false, :default=>50, :allowed=>nil, :disallowed=>nil}
-casts << {:long=>'count_min', :short=>'c', :type=>'uint', :mandatory=>false, :default=>2, :allowed=>nil, :disallowed=>nil}
-casts << {:long=>'cover', :short=>'C', :type=>'uint', :mandatory=>false, :default=>0, :allowed=>nil, :disallowed=>nil}
-casts << {:long=>'pairs_min', :short=>'p', :type=>'uint', :mandatory=>false, :default=>5, :allowed=>nil, :disallowed=>nil}
-casts << {:long=>'prefix_len', :short=>'P', :type=>'uint', :mandatory=>false, :default=>3, :allowed=>nil, :disallowed=>nil}
-casts << {:long=>'clean', :short=>'x', :type=>'flag', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
+casts << {:long=>'directory', :short=>'d', :type=>'dir', :mandatory=>true, :default=>nil, :allowed=>nil, :disallowed=>nil}
+casts << {:long=>'scaffold', :short=>'s', :type=>'flag', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
+casts << {:long=>'metagenome', :short=>'m', :type=>'flag', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
+casts << {:long=>'kmer_min', :short=>'k', :type=>'uint', :mandatory=>false, :default=>25, :allowed=>nil, :disallowed=>nil}
+casts << {:long=>'kmer_max', :short=>'K', :type=>'uint', :mandatory=>false, :default=>50, :allowed=>nil, :disallowed=>nil}
+casts << {:long=>'count_min', :short=>'c', :type=>'uint', :mandatory=>false, :default=>2, :allowed=>nil, :disallowed=>nil}
+casts << {:long=>'cover', :short=>'C', :type=>'uint', :mandatory=>false, :default=>0, :allowed=>nil, :disallowed=>nil}
+casts << {:long=>'pairs_min', :short=>'p', :type=>'uint', :mandatory=>false, :default=>5, :allowed=>nil, :disallowed=>nil}
+casts << {:long=>'prefix_len', :short=>'P', :type=>'uint', :mandatory=>false, :default=>3, :allowed=>nil, :disallowed=>nil}
+casts << {:long=>'clean', :short=>'X', :type=>'flag', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
+
+options = Biopieces.options_parse(ARGV, casts)
-bp = Biopieces.new
+Dir.mkdir(options[:directory]) unless Dir.exists?(options[:directory])
-options = bp.parse(ARGV, casts)
+file_fasta = File.join(options[:directory], "IDBA") + ".fna"
-Dir.mkdir(options[:directory]) unless Dir.exists?(options[:directory])
+count = 0
-file_fasta = [options[:directory], options[:name]].join(File::SEPARATOR) + ".fna"
+Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
+ Fasta.open(file_fasta, mode="w") do |fasta_io|
+ input.each_record do |record|
+ if record[:SEQ_NAME] and record[:SEQ]
+ seq = Seq.new_bp(record)
-Fasta.open(file_fasta, mode="w") do |fasta_io|
- bp.each_record do |record|
- fasta_io.puts record
- end
-end
+ if options[:scaffold] # we need to fix the sequence name for mate-pair IDBA
+ if seq.seq_name =~ /1$/
+ seq.seq_name = "read#{count}/1"
+ else
+ seq.seq_name = "read#{count}/2"
+
+ count += 1
+ end
+ end
+
+ fasta_io.puts seq.to_fasta
+ end
+ end
+ end
-unless File.size(file_fasta) == 0
- output = [options[:directory], options[:name]].join(File::SEPARATOR)
-
- commands = []
- commands << "nice -n 19"
- commands << "idba"
- commands << "--read #{file_fasta}"
- commands << "--output #{output}"
- commands << "--scaffold" if options[:scaffold]
- commands << "--mink #{options[:kmer_min]}"
- commands << "--maxk #{options[:kmer_max]}"
- commands << "--minCount #{options[:count_min]}"
- commands << "--cover #{options[:cover]}"
- commands << "--minPairs #{options[:pairs_min]}"
- commands << "--prefixLength #{options[:prefix_len]}"
- commands << "> /dev/null 2>&1" unless options[:verbose]
-
- command = commands.join(" ")
- system(command)
- raise "Command failed: #{command}" unless $?.success?
-
- file_contig = [options[:directory], options[:name] ].join(File::SEPARATOR) + "-contig.fa"
-
- Fasta.open(file_contig, mode="r") do |fasta_io|
- fasta_io.each do |entry|
- bp.puts entry.to_bp
+ unless File.size(file_fasta) == 0
+ prefix = File.join(options[:directory], "IDBA")
+
+ commands = []
+ commands << "nice -n 19"
+
+ if options[:metagenome]
+ commands << "metaidba"
+ else
+ commands << "idba"
end
- end
+
+ commands << "--read #{file_fasta}"
+ commands << "--output #{prefix}"
+ commands << "--scaffold" if options[:scaffold]
+ commands << "--mink #{options[:kmer_min]}"
+ commands << "--maxk #{options[:kmer_max]}"
+ commands << "--minCount #{options[:count_min]}"
+ commands << "--cover #{options[:cover]}"
+ commands << "--minPairs #{options[:pairs_min]}"
+ commands << "--prefixLength #{options[:prefix_len]}"
+ commands << "> /dev/null 2>&1" unless options[:verbose]
+
+ command = commands.join(" ")
+
+ begin
+ system(command)
+ raise "Command failed: #{command}" unless $?.success?
+ rescue
+ $stderr.puts "Command failed: #{command}"
+ end
+
+ if options[:scaffold]
+ file_contig = File.join(options[:directory], "IDBA") + "-contig-mate.fa"
+ else
+ file_contig = File.join(options[:directory], "IDBA") + "-contig.fa"
+ end
+
+ Fasta.open(file_contig, mode="r") do |fasta_io|
+ fasta_io.each do |entry|
+ output.puts entry.to_bp
+ end
+ end
+ end
end
+FileUtils.remove_entry_secure file_fasta
FileUtils.remove_entry_secure options[:directory] if options[:clean]