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 execute database search.
70 # usearch --query query.fasta --db db.fasta --uc results.uc --id 0.90 [--evalue E]
71 @command << "usearch --query #{@infile} --db #{@options[:database]} --uc #{@outfile} --id #{@options[:identity]}"
72 @command << "--evalue #{@options[:e_val]}" if @options.has_key? :e_val
77 # Method to execute clustering to database plus de novo if not matched.
79 # usearch --cluster seqs_sorted.fasta --db db.fasta --uc results.uc --id 0.90
80 @command << "usearch --cluster #{@infile} --db #{@options[:database]} --uc #{@outfile} --id #{@options[:identity]}"
85 # Method to parse a Uclust .uc file and for each line of data
86 # yield a Biopiece record.
90 File.open(@outfile, mode="r") do |ios|
91 ios.each_line do |line|
93 fields = line.chomp.split("\t")
95 record[:REC_TYPE] = "UCLUST"
96 record[:TYPE] = fields[0]
97 record[:CLUSTER] = fields[1].to_i
98 record[:SEQ_LEN] = fields[2].to_i
99 record[:IDENT] = fields[3].to_f
100 record[:STRAND] = fields[4]
101 record[:Q_BEG] = fields[5].to_i
102 record[:S_BEG] = fields[6].to_i
103 record[:S_END] = fields[6].to_i + fields[2].to_i
104 record[:CIGAR] = fields[7]
105 record[:Q_ID] = fields[8]
106 record[:S_ID] = fields[9]
113 self # conventionally
118 # Method to execute a command using a system() call.
119 # The command is composed of bits from the @command variable.
121 @command.unshift "nice -n 19"
122 @command << "--rev" if @options[:comp]
123 @command << "> /dev/null 2>&1" unless @options[:verbose]
124 command = @command.join(" ")
126 raise "Command failed: #{command}" unless $?.success?
132 ok_methods = "uclust,usearch,usearch_uclust"
135 casts << {:long=>'no_sort', :short=>'n', :type=>'flag', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
136 casts << {:long=>'method', :short=>'m', :type=>'string', :mandatory=>true, :default=>"uclust", :allowed=>ok_methods, :disallowed=>nil}
137 casts << {:long=>'database', :short=>'d', :type=>'file!', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
138 casts << {:long=>'comp', :short=>'c', :type=>'flag', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
139 casts << {:long=>'identity', :short=>'i', :type=>'float', :mandatory=>true, :default=>0.9, :allowed=>nil, :disallowed=>nil}
140 casts << {:long=>'e_val', :short=>'e', :type=>'float', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
142 options = Biopieces.options_parse(ARGV, casts)
144 # At high identities, around 96% and above, compressed indexes are often more sensitive, faster
145 # and use less RAM. Compressed indexes are disabled by default, so I generally recommend that
146 # you specify the --slots and --w options when clustering at high identities.
148 tmpdir = Biopieces.mktmpdir
149 infile = File.join(tmpdir, "in.fna")
150 outfile = File.join(tmpdir, "out.uc")
152 Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
153 Fasta.open(infile, mode="w") do |fasta_io|
154 input.each_record do |record|
157 if record.has_key? :SEQ_NAME and record.has_key? :SEQ
158 fasta_io.puts Seq.new_bp(record).to_fasta
163 uclust = Uclust.new(infile, outfile, options)
164 uclust.sort unless options[:no_sort] or options[:method] == "usearch"
166 case options[:method].to_s
167 when "uclust" then uclust.cluster
168 when "usearch" then uclust.usearch
169 when "usearch_uclust" then uclust.usearch_uclust
172 uclust.each do |record|
178 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<