3 # Copyright (C) 2007-2010 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 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
38 def initialize(infile, outfile, options)
45 # Method that calls Uclusts sorting for sorting a FASTA file
46 # according to decending sequence length.
48 @command << "uclust --sort #{@infile} --output #{@infile}.sort"
52 File.rename "#{@infile}.sort", @infile
56 # uclust --ublast query_seqs.fasta --db database.fasta --blast6out filename --evalue E
57 @options[:e_val] = 10 if @options[:e_val].is_nil?
58 @command << "uclust --ublast #{@infile} --db #{@options[:database]} --uc #{@outfile} --evalue #{@options[:e_val]}"
59 @command << "--gapopen *I" if @options.has_key? :no_gaps
64 # Method to execute database search.
66 # uclust --query query.fasta --db db.fasta --uc results.uc --id 0.90 [--evalue E]
67 @command << "uclust --query #{@infile} --db #{@options[:database]} --uc #{@outfile} --id #{@options[:identity]}"
68 @command << "--evalue #{@options[:e_val]}" if @options.has_key? :e_val
69 @command << "--gapopen *I" if @options.has_key? :no_gaps
74 # Method to execute clustering de novo.
76 # uclust --input seqs_sorted.fasta --uc results.uc --id 0.90
77 @command << "uclust --input #{@infile} --uc #{@outfile} --id #{@options[:identity]}"
78 @command << "--gapopen *I" if @options.has_key? :no_gaps
83 # Method to execute clustering to database plus de novo if not matched.
85 # uclust --input seqs_sorted.fasta --lib db.fasta --uc results.uc --id 0.90
86 @command << "uclust --input #{@infile} --lib #{@options[:database]} --uc #{@outfile} --id #{@options[:identity]}"
87 @command << "--lib #{@options[:database]}" if @options.has_key? :database
88 @command << "--gapopen *I" if @options.has_key? :no_gaps
93 # Method to execute a command using a system() call.
94 # The command is composed of bits from the @command variable.
96 @command.unshift "nice -n 19"
97 @command << "> /dev/null 2>&1" unless @options[:verbose]
98 command = @command.join(" ")
100 raise "Command failed: #{command}" unless $?.success?
105 # Method to parse a Uclust .uc file and for each line of data
106 # yield a Biopiece record.
110 File.open(@outfile, mode="r") do |ios|
111 ios.each_line do |line|
113 fields = line.split("\t")
115 record[:REC_TYPE] = "UCLUST"
116 record[:TYPE] = fields[0]
117 record[:CLUSTER] = fields[1]
118 record[:SEQ_LEN] = fields[2]
119 record[:IDENT] = fields[3]
120 record[:STRAND] = fields[4]
121 record[:Q_BEG] = fields[5]
122 record[:S_BEG] = fields[6]
123 record[:CIGAR] = fields[7]
124 record[:Q_ID] = fields[8]
125 record[:S_ID] = fields[9]
132 self # conventionally
136 ok_methods = "ublast,usearch,uclust,usearch_uclust"
139 casts << {:long=>'no_sort', :short=>'n', :type=>'flag', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
140 casts << {:long=>'method', :short=>'m', :type=>'string', :mandatory=>true, :default=>"uclust", :allowed=>ok_methods, :disallowed=>nil}
141 casts << {:long=>'database', :short=>'d', :type=>'file!', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
142 casts << {:long=>'identity', :short=>'i', :type=>'float', :mandatory=>true, :default=>0.9, :allowed=>nil, :disallowed=>nil}
143 casts << {:long=>'no_gaps', :short=>'g', :type=>'flag', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
144 casts << {:long=>'e_val', :short=>'e', :type=>'float', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
148 options = bp.parse(ARGV, casts)
151 infile = "#{tmpdir}/in.fna"
152 outfile = "#{tmpdir}/out.uc"
154 Fasta.open(infile, mode="w") do |fasta_io|
155 bp.each_record do |record|
161 uclust = Uclust.new(infile, outfile, options)
162 uclust.sort unless options[:no_sort]
164 case options[:method].to_s
165 when "ublast" then uclust.ublast
166 when "usearch" then uclust.usearch
167 when "uclust" then uclust.uclust
168 when "usearch_uclust" then uclust.usearch_uclust
169 else raise "Unknown method: #{options[:method]}"
172 uclust.each do |record|
177 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<