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 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
38 def initialize(infile, outfile)
43 # Method that calls Uclusts sorting for sorting a FASTA file
44 # according to decending sequence length.
46 command = "nice -n 19"
47 command << " uclust --sort #{@infile} --output #{@infile}.sort"
48 command << " > /dev/null 2>&1" unless options[:verbose]
50 raise "Failed sorting file: #{@infile}" unless $?.success?
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 = "nice -n 19"
59 command << " uclust --ublast #{@infile} --db #{options[:database]} --uc #{@outfile} --evalue #{options[:e_val]}"
60 command << " > /dev/null 2>&1" unless options[:verbose]
62 raise "Command failed: #{command}" unless $?.success?
66 # uclust --query query.fasta --db db.fasta --uc results.uc --id 0.90 [--evalue E]
67 command = "nice -n 19"
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
70 command << " > /dev/null 2>&1" unless options[:verbose]
72 raise "Command failed: #{command}" unless $?.success?
76 # uclust --input seqs_sorted.fasta --uc results.uc --id 0.90
77 command = "nice -n 19"
78 command << " uclust --input #{@infile} --uc #{@outfile} --id #{options[:identity]}"
79 command << " > /dev/null 2>&1" unless options[:verbose]
81 raise "Command failed: #{command}" unless $?.success?
84 def usearch_uclust(options)
85 # uclust --input seqs_sorted.fasta --lib db.fasta --uc results.uc --id 0.90
86 command = "nice -n 19"
87 command << " uclust --input #{@infile} --lib #{options[:database]} --uc #{@outfile} --id #{options[:identity]}"
88 command << " --lib #{options[:database]}" if options.has_key? :database
89 command << " > /dev/null 2>&1" unless options[:verbose]
91 raise "Command failed: #{command}" unless $?.success?
97 File.open(@outfile, mode="r") do |ios|
98 ios.each_line do |line|
100 fields = line.split("\t")
102 record[:REC_TYPE] = "UCLUST"
103 record[:TYPE] = fields[0]
104 record[:CLUSTER] = fields[1]
105 record[:SEQ_LEN] = fields[2]
106 record[:IDENT] = fields[3]
107 record[:STRAND] = fields[4]
108 record[:Q_BEG] = fields[5]
109 record[:S_BEG] = fields[6]
110 record[:CIGAR] = fields[7]
111 record[:Q_ID] = fields[8]
112 record[:S_ID] = fields[9]
119 self # conventionally
123 ok_methods = "ublast,usearch,uclust,usearch_uclust"
126 casts << {:long=>'no_sort', :short=>'n', :type=>'flag', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
127 casts << {:long=>'method', :short=>'m', :type=>'string', :mandatory=>true, :default=>"uclust", :allowed=>ok_methods, :disallowed=>nil}
128 casts << {:long=>'database', :short=>'d', :type=>'file!', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
129 casts << {:long=>'identity', :short=>'i', :type=>'float', :mandatory=>true, :default=>0.9, :allowed=>nil, :disallowed=>nil}
130 casts << {:long=>'e_val', :short=>'e', :type=>'float', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
134 options = bp.parse(ARGV, casts)
137 infile = "#{tmpdir}/in.fna"
138 outfile = "#{tmpdir}/out.uc"
140 Fasta.open(infile, mode="w") do |fasta_io|
141 bp.each_record do |record|
147 uclust = Uclust.new(infile, outfile)
148 uclust.sort(options) unless options[:no_sort]
150 case options[:method].to_s
151 when "ublast" then uclust.ublast(options)
152 when "usearch" then uclust.usearch(options)
153 when "uclust" then uclust.uclust(options)
154 when "usearch_uclust" then uclust.usearch_uclust(options)
155 else raise "Unknown method: #{options[:method]}"
158 uclust.each do |record|
163 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<