1 # Copyright (C) 2011 Martin A. Hansen.
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # http://www.gnu.org/copyleft/gpl.html
19 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
21 # This software is part of the Biopieces framework (www.biopieces.org).
23 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
25 require 'maasha/fasta'
27 # Error class for all exceptions to do with Usearch.
28 class UsearchError < StandardError; end
33 def initialize(infile, outfile, options)
39 raise UsearchError, %{Empty input file -> "#{@infile}"} if File.size(@infile) == 0
42 # Method that calls Usearch sorting for sorting a FASTA file
43 # according to decending sequence length.
45 # usearch -sortbylength seqs.fasta -output seqs_sorted.fasta -minseqlength 64
47 @command << "-sortbylength #{@infile}"
48 @command << "-output #{@infile}.sort"
52 File.rename "#{@infile}.sort", @infile
55 # Method that calls Usearch sorting for sorting a FASTA file
56 # according to cluster size.
58 # usearch -sortbysize seqs.fasta -output seqs_sorted.fasta -minsize 4
60 @command << "-sortbysize #{@infile}"
61 @command << "-output #{@infile}.sort"
65 File.rename "#{@infile}.sort", @infile
68 # Method to execute cluster_fast.
70 # usearch -cluster_fast query.fasta -id 0.9 -centroids nr.fasta -uc clusters.uc
72 @command << "-cluster_fast #{@infile}"
73 @command << "-id #{@options[:identity]}"
74 @command << "-uc #{@outfile}"
75 @command << "-threads #{@options[:cpus]}"
80 # Method to execute cluster_smallmem.
81 # NB sequences must be sorted with sortbylength or sortbysize.
83 # usearch -cluster_smallmem query.fasta -id 0.9 -centroids nr.fasta -uc clusters.uc
85 @command << "-cluster_smallmem #{@infile}"
86 @command << "-id #{@options[:identity]}"
87 @command << "-uc #{@outfile}"
88 @command << "-strand both" if @options[:comp]
93 # Method to execute database local search.
95 # usearch -usearch_local query.fasta -db proteins.udb -id 0.8 -alnout results.aln
96 # usearch -usearch_local query.fasta -db ESTs.fasta -id 0.9 -evalue 1e-6 -blast6out results.m8 -strand plus -maxaccepts 8 -maxrejects 256
99 @command << "-usearch_local #{@infile}"
100 @command << "-db #{@options[:database]}"
101 @command << "-blast6out #{@outfile}"
102 @command << "-strand both"
103 @command << "-id #{@options[:identity]}" if @options[:identity]
104 @command << "-evalue #{@options[:e_val]}" if @options[:e_val]
105 @command << "-maxaccepts #{@options[:maxaccepts]}"
106 @command << "-threads #{@options[:cpus]}"
111 # Method to execute database global search.
113 # usearch -usearch_global query.fasta -db proteins.udb -id 0.8 -alnout results.aln
114 # usearch -usearch_global query.fasta -db ESTs.fasta -id 0.9 -blast6out results.m8 -strand plus -maxaccepts 8 -maxrejects 256
116 @command << "usearch"
117 @command << "-usearch_global #{@infile}"
118 @command << "-db #{@options[:database]}"
119 @command << "-blast6out #{@outfile}"
120 @command << "-strand both"
121 @command << "-id #{@options[:identity]}"
122 @command << "-evalue #{@options[:e_val]}" if @options[:e_val]
123 @command << "-maxaccepts #{@options[:maxaccepts]}"
124 @command << "-threads #{@options[:cpus]}"
129 # Method to execute uchime chimera detection.
131 @command << "usearch --uchime #{@infile} --db #{@options[:database]} --uchimeout #{@outfile}"
136 # Method to execute ustar alignment.
138 command = %Q{grep "^[SH]" #{@outfile} > #{@outfile}.sub}
140 raise "Command failed: #{command}" unless $?.success?
142 File.rename "#{@outfile}.sub", @outfile
144 @command << "usearch --uc2fastax #{@outfile} --input #{@infile} --output #{@infile}.sub"
148 @command << "usearch --staralign #{@infile}.sub --output #{@outfile}"
152 File.delete "#{@infile}.sub"
155 # Method to parse a Uclust .uc file and for each line of data
156 # yield a Biopiece record.
160 File.open(@outfile, "r") do |ios|
161 ios.each_line do |line|
163 fields = line.chomp.split("\t")
165 next if fields[0] == 'C'
167 record[:TYPE] = fields[0]
168 record[:CLUSTER] = fields[1].to_i
169 record[:IDENT] = fields[3].to_f
170 record[:Q_ID] = fields[8]
177 self # conventionally
180 # Method to parse a Useach user defined tabular file and for each line of data
181 # yield a Biopiece record.
185 File.open(@outfile, "r") do |ios|
186 ios.each_line do |line|
187 fields = line.chomp.split("\t")
188 record[:REC_TYPE] = "USEARCH"
189 record[:Q_ID] = fields[0]
190 record[:S_ID] = fields[1]
191 record[:IDENT] = fields[2].to_f
192 record[:ALIGN_LEN] = fields[3].to_i
193 record[:MISMATCHES] = fields[4].to_i
194 record[:GAPS] = fields[5].to_i
195 record[:Q_BEG] = fields[6].to_i - 1
196 record[:Q_END] = fields[7].to_i - 1
197 record[:S_BEG] = fields[8].to_i - 1
198 record[:S_END] = fields[9].to_i - 1
199 record[:E_VAL] = fields[10] == '*' ? '*' : fields[10].to_f
200 record[:SCORE] = fields[11] == '*' ? '*' : fields[11].to_f
201 record[:STRAND] = record[:S_BEG].to_i < record[:S_END].to_i ? '+' : '-'
203 record[:S_BEG], record[:S_END] = record[:S_END], record[:S_BEG] if record[:STRAND] == '-'
209 self # conventionally
212 # Method to parse a FASTA file with Ustar alignments and for each alignment
213 # yield an Align object.
218 Fasta.open(@outfile, "r") do |ios|
220 entry.seq.tr!('.', '-') # Replace . with - in Ustar alignments.
221 cluster, identity, name = entry.seq_name.split('|')
222 cluster = cluster.to_i
224 if cluster == old_cluster
227 fix_alignment(entries)
229 yield Align.new(entries)
231 old_cluster = cluster
237 yield Align.new(entries) unless entries.empty?
240 self # conventionally
245 # Method that fixed Ustar bug resulting in alignments with uneven
247 def fix_alignment(entries)
249 min, max = entries.minmax { |a, b| a.length <=> b.length }
251 if min.length != max.length
252 entries.each do |entry|
253 entry.seq << '-' * (max.length - entry.length)
259 # Method to execute a command using a system() call.
260 # The command is composed of bits from the @command variable.
262 @command << "--quiet" unless @options[:verbose]
263 command = @command.join(" ")
264 $stderr.puts "Running command: #{command}" if @options[:verbose]
266 raise "Command failed: #{command}" unless $?.success?