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)
42 # Method that calls Usearch sorting for sorting a FASTA file
43 # according to decending sequence length.
45 # usearch -sort seqs.fasta -output seqs.sorted.fasta
46 if File.size(@infile) < SORT_LIMIT
47 @command << "usearch --sort #{@infile} --output #{@infile}.sort"
49 @command << "usearch --mergesort #{@infile} --output #{@infile}.sort"
54 File.rename "#{@infile}.sort", @infile
57 # Method that calls Usearch sorting for sorting a FASTA file
58 # according to cluster size.
60 @command << "usearch --sortsize #{@infile} --output #{@infile}.sort"
64 File.rename "#{@infile}.sort", @infile
67 # Method to execute clustering de novo.
69 @command << "usearch --cluster #{@infile} --uc #{@outfile} --id #{@options[:identity]}"
70 @command << "--rev" if @options[:comp]
75 # Method to execute database search.
77 @command << "usearch --query #{@infile} --db #{@options[:database]} --userout #{@outfile}"
78 @command << "--userfields target+tloz+thiz+query+bits+strand"
79 @command << "--id #{@options[:identity]}" if @options.has_key? :identity
80 @command << "--evalue #{@options[:e_val]}" if @options.has_key? :e_val
86 # Method to parse a Uclust .uc file and for each line of data
87 # yield a Biopiece record.
91 File.open(@outfile, mode="r") do |ios|
92 ios.each_line do |line|
94 fields = line.chomp.split("\t")
96 next if fields[0] == 'C'
98 record[:TYPE] = fields[0]
99 record[:CLUSTER] = fields[1].to_i
100 record[:IDENT] = fields[3].to_f
101 record[:Q_ID] = fields[8]
108 self # conventionally
111 # Method to parse a Useach user defined tabular file and for each line of data
112 # yield a Biopiece record.
116 File.open(@outfile, mode="r") do |ios|
117 ios.gets # skip comment line
118 ios.each_line do |line|
119 fields = line.chomp.split("\t")
121 record[:REC_TYPE] = "USEARCH"
122 record[:S_ID] = fields[0]
123 record[:S_BEG] = fields[1].to_i
124 record[:S_END] = fields[2].to_i
125 record[:Q_ID] = fields[3]
126 record[:SCORE] = fields[4].to_f
127 record[:STRAND] = fields[5]
133 self # conventionally
138 # Method to execute a command using a system() call.
139 # The command is composed of bits from the @command variable.
141 @command.unshift "nice -n 19"
142 @command << "> /dev/null 2>&1" unless @options[:verbose]
143 command = @command.join(" ")
145 raise "Command failed: #{command}" unless $?.success?