3 # Copyright (C) 2007-2011 Martin A. Hansen.
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.
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.
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.
19 # http://www.gnu.org/copyleft/gpl.html
21 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
23 # This program is part of the Biopieces framework (www.biopieces.org).
25 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
27 # Run Uclust on sequences in the stream.
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
31 require 'maasha/biopieces'
32 require 'maasha/fasta'
34 SORT_LIMIT = 2_000_000_000 # use mergesort for files biggern than 2Gb.
39 def initialize(infile, outfile, options)
46 # Method that calls Usearch sorting for sorting a FASTA file
47 # according to decending sequence length.
49 # usearch -sort seqs.fasta -output seqs.sorted.fasta
50 if File.size(@infile) < SORT_LIMIT
51 @command << "usearch --sort #{@infile} --output #{@infile}.sort"
53 @command << "usearch --mergesort #{@infile} --output #{@infile}.sort"
58 File.rename "#{@infile}.sort", @infile
61 # Method to execute clustering de novo.
63 @command << "usearch --cluster #{@infile} --uc #{@outfile} --id #{@options[:identity]}"
68 # Method to parse a Uclust .uc file and for each line of data
69 # yield a Biopiece record.
73 File.open(@outfile, mode="r") do |ios|
74 ios.each_line do |line|
76 fields = line.chomp.split("\t")
78 next if fields[0] == 'C'
80 record[:TYPE] = fields[0]
81 record[:CLUSTER] = fields[1].to_i
82 record[:IDENT] = fields[3].to_f
83 record[:Q_ID] = fields[8]
95 # Method to execute a command using a system() call.
96 # The command is composed of bits from the @command variable.
98 @command.unshift "nice -n 19"
99 @command << "--rev" if @options[:comp]
100 @command << "> /dev/null 2>&1" unless @options[:verbose]
101 command = @command.join(" ")
103 raise "Command failed: #{command}" unless $?.success?
110 casts << {:long=>'no_sort', :short=>'n', :type=>'flag', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
111 casts << {:long=>'comp', :short=>'c', :type=>'flag', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
112 casts << {:long=>'identity', :short=>'i', :type=>'float', :mandatory=>true, :default=>0.9, :allowed=>nil, :disallowed=>nil}
114 options = Biopieces.options_parse(ARGV, casts)
116 tmpdir = Biopieces.mktmpdir
117 file_records = File.join(tmpdir, "data.stream")
118 file_fasta = File.join(tmpdir, "in.fna")
119 file_uclust = File.join(tmpdir, "out.uc")
121 Biopieces.open(options[:stream_in], file_records) do |input, output|
122 Fasta.open(file_fasta, mode="w") do |fasta_io|
123 input.each_record do |record|
126 if record.has_key? :SEQ_NAME and record.has_key? :SEQ
127 fasta_io.puts Seq.new_bp(record).to_fasta
133 uc = Uclust.new(file_fasta, file_uclust, options)
134 uc.sort unless options[:no_sort]
140 hash[record[:Q_ID].to_sym] = record.dup
143 Biopieces.open(file_records, options[:stream_out]) do |input, output|
144 input.each_record do |record|
145 if record.has_key? :SEQ_NAME and record.has_key? :SEQ
146 if hash.has_key? record[:SEQ_NAME].to_sym
147 uc = hash[record[:SEQ_NAME].to_sym]
148 record[:CLUSTER] = uc[:CLUSTER].to_i
149 record[:IDENT] = uc[:IDENT].to_i
150 record[:IDENT] = '*' if uc[:TYPE] == 'S'
159 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<