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
30 SORT_LIMIT = 2_000_000_000 # use mergesort for files biggern than 2Gb.
35 def initialize(infile, outfile, options)
41 raise UsearchError, %{Empty input file -> "#{@infile}"} if File.size(@infile) == 0
44 # Method that calls Usearch sorting for sorting a FASTA file
45 # according to decending sequence length.
47 # usearch -sort seqs.fasta -output seqs.sorted.fasta
48 if File.size(@infile) < SORT_LIMIT
49 @command << "usearch --sort #{@infile} --output #{@infile}.sort"
51 @command << "usearch --mergesort #{@infile} --output #{@infile}.sort"
56 File.rename "#{@infile}.sort", @infile
59 # Method that calls Usearch sorting for sorting a FASTA file
60 # according to cluster size.
62 @command << "usearch --sortsize #{@infile} --output #{@infile}.sort"
66 File.rename "#{@infile}.sort", @infile
69 # Method to execute clustering de novo.
71 @command << "usearch --cluster #{@infile} --uc #{@outfile} --id #{@options[:identity]}"
72 @command << "--rev" if @options[:comp]
77 # Method to execute database search.
79 @command << "usearch --query #{@infile} --db #{@options[:database]} --userout #{@outfile}"
80 @command << "--userfields target+tloz+thiz+query+bits+strand"
81 @command << "--id #{@options[:identity]}" if @options.has_key? :identity
82 @command << "--evalue #{@options[:e_val]}" if @options.has_key? :e_val
88 # Method to execute uchime chimera detection.
90 @command << "usearch --uchime #{@infile} --db #{@options[:database]} --uchimeout #{@outfile}"
95 # Method to execute ustar alignment.
97 command = %Q{grep "^[SH]" #{@outfile} > #{@outfile}.sub}
99 raise "Command failed: #{command}" unless $?.success?
101 File.rename "#{@outfile}.sub", @outfile
103 @command << "usearch --uc2fastax #{@outfile} --input #{@infile} --output #{@infile}.sub"
107 @command << "usearch --staralign #{@infile}.sub --output #{@outfile}"
111 File.delete "#{@infile}.sub"
114 # Method to parse a Uclust .uc file and for each line of data
115 # yield a Biopiece record.
119 File.open(@outfile, "r") do |ios|
120 ios.each_line do |line|
122 fields = line.chomp.split("\t")
124 next if fields[0] == 'C'
126 record[:TYPE] = fields[0]
127 record[:CLUSTER] = fields[1].to_i
128 record[:IDENT] = fields[3].to_f
129 record[:Q_ID] = fields[8]
136 self # conventionally
139 # Method to parse a Useach user defined tabular file and for each line of data
140 # yield a Biopiece record.
144 File.open(@outfile, "r") do |ios|
145 ios.gets # skip comment line
146 ios.each_line do |line|
147 fields = line.chomp.split("\t")
149 record[:REC_TYPE] = "USEARCH"
150 record[:S_ID] = fields[0]
151 record[:S_BEG] = fields[1].to_i
152 record[:S_END] = fields[2].to_i
153 record[:Q_ID] = fields[3]
154 record[:SCORE] = fields[4].to_f
155 record[:STRAND] = fields[5]
161 self # conventionally
164 # Method to parse a FASTA file with Ustar alignments and for each alignment
165 # yield an Align object.
170 Fasta.open(@outfile, "r") do |ios|
172 entry.seq.tr!('.', '-') # Replace . with - in Ustar alignments.
173 cluster, identity, name = entry.seq_name.split('|')
174 cluster = cluster.to_i
176 if cluster == old_cluster
179 fix_alignment(entries)
181 yield Align.new(entries)
183 old_cluster = cluster
189 yield Align.new(entries) unless entries.empty?
195 # Method that fixed Ustar bug resulting in alignments with uneven
197 def fix_alignment(entries)
199 min, max = entries.minmax { |a, b| a.length <=> b.length }
201 if min.length != max.length
202 entries.each do |entry|
203 entry.seq << '-' * (max.length - entry.length)
209 # Method to execute a command using a system() call.
210 # The command is composed of bits from the @command variable.
212 @command.unshift "nice -n 19"
213 @command << "--quiet" unless @options[:verbose]
214 command = @command.join(" ")
215 $stderr.puts "Running command: #{command}" if @options[:verbose]
217 raise "Command failed: #{command}" unless $?.success?