From e9c3a77be1b8dfbe2ef70fa81e5d6910860bd0f6 Mon Sep 17 00:00:00 2001 From: martinahansen Date: Wed, 22 Sep 2010 12:18:24 +0000 Subject: [PATCH] polishing on uclust_seq git-svn-id: http://biopieces.googlecode.com/svn/trunk@1101 74ccb610-7750-0410-82ae-013aeee3265d --- bp_bin/uclust_seq | 94 +++++++++++++++++++++++++++-------------------- 1 file changed, 54 insertions(+), 40 deletions(-) diff --git a/bp_bin/uclust_seq b/bp_bin/uclust_seq index 4738212..36e40ad 100755 --- a/bp_bin/uclust_seq +++ b/bp_bin/uclust_seq @@ -24,7 +24,7 @@ # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< -# +# Run Uclust on sequences in the stream. # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @@ -35,62 +35,75 @@ require 'fasta' class Uclust include Enumerable - def initialize(infile, outfile) + def initialize(infile, outfile, options) @infile = infile @outfile = outfile + @options = options + @command = [] end # Method that calls Uclusts sorting for sorting a FASTA file # according to decending sequence length. - def sort(options) - command = "nice -n 19" - command << " uclust --sort #{@infile} --output #{@infile}.sort" - command << " > /dev/null 2>&1" unless options[:verbose] - system(command) - raise "Failed sorting file: #{@infile}" unless $?.success? + def sort + @command << "uclust --sort #{@infile} --output #{@infile}.sort" + + execute File.rename "#{@infile}.sort", @infile end - def ublast(options) + def ublast # uclust --ublast query_seqs.fasta --db database.fasta --blast6out filename --evalue E - options[:e_val] = 10 if options[:e_val].is_nil? - command = "nice -n 19" - command << " uclust --ublast #{@infile} --db #{options[:database]} --uc #{@outfile} --evalue #{options[:e_val]}" - command << " > /dev/null 2>&1" unless options[:verbose] - system(command) - raise "Command failed: #{command}" unless $?.success? + @options[:e_val] = 10 if @options[:e_val].is_nil? + @command << "uclust --ublast #{@infile} --db #{@options[:database]} --uc #{@outfile} --evalue #{@options[:e_val]}" + @command << "--gapopen *I" if @options.has_key? :no_gaps + + execute end - def usearch(options) + # Method to execute database search. + def usearch # uclust --query query.fasta --db db.fasta --uc results.uc --id 0.90 [--evalue E] - command = "nice -n 19" - command << " uclust --query #{@infile} --db #{options[:database]} --uc #{@outfile} --id #{options[:identity]}" - command << " --evalue #{options[:e_val]}" if options.has_key? :e_val - command << " > /dev/null 2>&1" unless options[:verbose] - system(command) - raise "Command failed: #{command}" unless $?.success? + @command << "uclust --query #{@infile} --db #{@options[:database]} --uc #{@outfile} --id #{@options[:identity]}" + @command << "--evalue #{@options[:e_val]}" if @options.has_key? :e_val + @command << "--gapopen *I" if @options.has_key? :no_gaps + + execute end - def uclust(options) + # Method to execute clustering de novo. + def uclust # uclust --input seqs_sorted.fasta --uc results.uc --id 0.90 - command = "nice -n 19" - command << " uclust --input #{@infile} --uc #{@outfile} --id #{options[:identity]}" - command << " > /dev/null 2>&1" unless options[:verbose] - system(command) - raise "Command failed: #{command}" unless $?.success? + @command << "uclust --input #{@infile} --uc #{@outfile} --id #{@options[:identity]}" + @command << "--gapopen *I" if @options.has_key? :no_gaps + + execute end - def usearch_uclust(options) + # Method to execute clustering to database plus de novo if not matched. + def usearch_uclust # uclust --input seqs_sorted.fasta --lib db.fasta --uc results.uc --id 0.90 - command = "nice -n 19" - command << " uclust --input #{@infile} --lib #{options[:database]} --uc #{@outfile} --id #{options[:identity]}" - command << " --lib #{options[:database]}" if options.has_key? :database - command << " > /dev/null 2>&1" unless options[:verbose] + @command << "uclust --input #{@infile} --lib #{@options[:database]} --uc #{@outfile} --id #{@options[:identity]}" + @command << "--lib #{@options[:database]}" if @options.has_key? :database + @command << "--gapopen *I" if @options.has_key? :no_gaps + + execute + end + + # Method to execute a command using a system() call. + # The command is composed of bits from the @command variable. + def execute + @command.unshift "nice -n 19" + @command << "> /dev/null 2>&1" unless @options[:verbose] + command = @command.join(" ") system(command) raise "Command failed: #{command}" unless $?.success? - end + @command = [] + end + + # Method to parse a Uclust .uc file and for each line of data + # yield a Biopiece record. def each record = {} @@ -127,6 +140,7 @@ casts << {:long=>'no_sort', :short=>'n', :type=>'flag', :mandatory=>false, :d casts << {:long=>'method', :short=>'m', :type=>'string', :mandatory=>true, :default=>"uclust", :allowed=>ok_methods, :disallowed=>nil} casts << {:long=>'database', :short=>'d', :type=>'file!', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil} casts << {:long=>'identity', :short=>'i', :type=>'float', :mandatory=>true, :default=>0.9, :allowed=>nil, :disallowed=>nil} +casts << {:long=>'no_gaps', :short=>'g', :type=>'flag', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil} casts << {:long=>'e_val', :short=>'e', :type=>'float', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil} bp = Biopieces.new @@ -144,14 +158,14 @@ Fasta.open(infile, mode="w") do |fasta_io| end end -uclust = Uclust.new(infile, outfile) -uclust.sort(options) unless options[:no_sort] +uclust = Uclust.new(infile, outfile, options) +uclust.sort unless options[:no_sort] case options[:method].to_s -when "ublast" then uclust.ublast(options) -when "usearch" then uclust.usearch(options) -when "uclust" then uclust.uclust(options) -when "usearch_uclust" then uclust.usearch_uclust(options) +when "ublast" then uclust.ublast +when "usearch" then uclust.usearch +when "uclust" then uclust.uclust +when "usearch_uclust" then uclust.usearch_uclust else raise "Unknown method: #{options[:method]}" end -- 2.39.2