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 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
32 require 'maasha/biopieces'
33 require 'maasha/fasta'
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 unless @options[:e_val]
58 @command << "uclust --ublast #{@infile} --db #{@options[:database]} --blast6out #{@outfile} --evalue #{@options[:e_val]}"
63 # Method to execute database search.
65 # uclust --query query.fasta --db db.fasta --uc results.uc --id 0.90 [--evalue E]
66 @command << "uclust --query #{@infile} --db #{@options[:database]} --uc #{@outfile} --id #{@options[:identity]}"
67 @command << "--evalue #{@options[:e_val]}" if @options.has_key? :e_val
72 # Method to execute clustering de novo.
74 # uclust --input seqs_sorted.fasta --uc results.uc --id 0.90
75 @command << "uclust --input #{@infile} --uc #{@outfile} --id #{@options[:identity]}"
80 # Method to execute clustering to database plus de novo if not matched.
82 # uclust --input seqs_sorted.fasta --lib db.fasta --uc results.uc --id 0.90
83 @command << "uclust --input #{@infile} --lib #{@options[:database]} --uc #{@outfile} --id #{@options[:identity]}"
84 @command << "--lib #{@options[:database]}" if @options.has_key? :database
89 # Method to parse a Uclust .uc file and for each line of data
90 # yield a Biopiece record.
94 File.open(@outfile, mode="r") do |ios|
95 ios.each_line do |line|
97 fields = line.chomp.split("\t")
99 record[:REC_TYPE] = "UCLUST"
100 record[:TYPE] = fields[0]
101 record[:CLUSTER] = fields[1]
102 record[:SEQ_LEN] = fields[2]
103 record[:IDENT] = fields[3]
104 record[:STRAND] = fields[4]
105 record[:Q_BEG] = fields[5]
106 record[:S_BEG] = fields[6]
107 record[:CIGAR] = fields[7]
108 record[:Q_ID] = fields[8]
109 record[:S_ID] = fields[9]
116 self # conventionally
121 # Method to execute a command using a system() call.
122 # The command is composed of bits from the @command variable.
124 @command.unshift "nice -n 19"
125 @command << "--rev" if @options[:comp]
126 @command << "> /dev/null 2>&1" unless @options[:verbose]
127 command = @command.join(" ")
129 raise "Command failed: #{command}" unless $?.success?
135 ok_methods = "ublast,usearch,uclust,usearch_uclust"
138 casts << {:long=>'no_sort', :short=>'n', :type=>'flag', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
139 casts << {:long=>'method', :short=>'m', :type=>'string', :mandatory=>true, :default=>"uclust", :allowed=>ok_methods, :disallowed=>nil}
140 casts << {:long=>'database', :short=>'d', :type=>'file!', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
141 casts << {:long=>'comp', :short=>'c', :type=>'flag', :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=>'e_val', :short=>'e', :type=>'float', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
145 options = Biopieces.options_parse(ARGV, casts)
147 tmpdir = Biopieces.mktmpdir
148 infile = File.join(tmpdir, "in.fna")
149 outfile = File.join(tmpdir, "out.uc")
151 Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
152 Fasta.open(infile, mode="w") do |fasta_io|
153 input.each_record do |record|
159 uclust = Uclust.new(infile, outfile, options)
160 uclust.sort unless options[:no_sort]
162 case options[:method].to_s
163 when "ublast" then uclust.ublast
164 when "usearch" then uclust.usearch
165 when "uclust" then uclust.uclust
166 when "usearch_uclust" then uclust.usearch_uclust
167 else raise "Unknown method: #{options[:method]}"
170 uclust.each do |record|
176 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<