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)
44 @command << "--rev" if @options[:comp]
47 # Method that calls Uclusts sorting for sorting a FASTA file
48 # according to decending sequence length.
50 @command << "uclust --sort #{@infile} --output #{@infile}.sort"
54 File.rename "#{@infile}.sort", @infile
58 # uclust --ublast query_seqs.fasta --db database.fasta --blast6out filename --evalue E
59 @options[:e_val] = 10 unless @options[:e_val]
60 @command << "uclust --ublast #{@infile} --db #{@options[:database]} --blast6out #{@outfile} --evalue #{@options[:e_val]}"
65 # Method to execute database search.
67 # uclust --query query.fasta --db db.fasta --uc results.uc --id 0.90 [--evalue E]
68 @command << "uclust --query #{@infile} --db #{@options[:database]} --uc #{@outfile} --id #{@options[:identity]}"
69 @command << "--evalue #{@options[:e_val]}" if @options.has_key? :e_val
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]}"
82 # Method to execute clustering to database plus de novo if not matched.
84 # uclust --input seqs_sorted.fasta --lib db.fasta --uc results.uc --id 0.90
85 @command << "uclust --input #{@infile} --lib #{@options[:database]} --uc #{@outfile} --id #{@options[:identity]}"
86 @command << "--lib #{@options[:database]}" if @options.has_key? :database
91 # Method to parse a Uclust .uc file and for each line of data
92 # yield a Biopiece record.
96 File.open(@outfile, mode="r") do |ios|
97 ios.each_line do |line|
99 fields = line.chomp.split("\t")
101 record[:REC_TYPE] = "UCLUST"
102 record[:TYPE] = fields[0]
103 record[:CLUSTER] = fields[1]
104 record[:SEQ_LEN] = fields[2]
105 record[:IDENT] = fields[3]
106 record[:STRAND] = fields[4]
107 record[:Q_BEG] = fields[5]
108 record[:S_BEG] = fields[6]
109 record[:CIGAR] = fields[7]
110 record[:Q_ID] = fields[8]
111 record[:S_ID] = fields[9]
118 self # conventionally
123 # Method to execute a command using a system() call.
124 # The command is composed of bits from the @command variable.
126 @command.unshift "nice -n 19"
127 @command << "> /dev/null 2>&1" unless @options[:verbose]
128 command = @command.join(" ")
130 raise "Command failed: #{command}" unless $?.success?
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=>'comp', :short=>'c', :type=>'flag', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
143 casts << {:long=>'identity', :short=>'i', :type=>'float', :mandatory=>true, :default=>0.9, :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 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<