]> git.donarmstrong.com Git - biopieces.git/blob - code_ruby/lib/maasha/usearch.rb
cleanup of usearch.rb
[biopieces.git] / code_ruby / lib / maasha / usearch.rb
1 # Copyright (C) 2011 Martin A. Hansen.
2
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.
7
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.
12
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.
16
17 # http://www.gnu.org/copyleft/gpl.html
18
19 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
20
21 # This software is part of the Biopieces framework (www.biopieces.org).
22
23 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
24
25 require 'maasha/fasta'
26
27 # Error class for all exceptions to do with Usearch.
28 class UsearchError < StandardError; end
29
30 SORT_LIMIT = 2_000_000_000   # use mergesort for files biggern than 2Gb.
31
32 class Usearch
33   include Enumerable
34
35   def initialize(infile, outfile, options)
36     @infile  = infile
37     @outfile = outfile
38     @options = options
39     @command = []
40   end
41
42   # Method that calls Usearch sorting for sorting a FASTA file
43   # according to decending sequence length.
44   def sort_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"
48     else
49       @command << "usearch --mergesort #{@infile} --output #{@infile}.sort"
50     end
51
52                 execute
53
54     File.rename "#{@infile}.sort", @infile
55   end
56
57   # Method that calls Usearch sorting for sorting a FASTA file
58   # according to cluster size.
59   def sort_size
60     @command << "usearch --sortsize #{@infile} --output #{@infile}.sort"
61
62                 execute
63
64     File.rename "#{@infile}.sort", @infile
65   end
66
67         # Method to execute clustering de novo.
68   def cluster
69     @command << "usearch --cluster #{@infile} --uc #{@outfile} --id #{@options[:identity]}"
70
71                 execute
72   end
73
74   # Method to execute database search.
75   def usearch
76     @command << "usearch --query #{@infile} --db #{@options[:database]} --userout #{@outfile}"
77     @command << "--userfields target+tloz+thiz+query+bits+strand"
78     @command << "--id #{@options[:identity]}"  if @options.has_key? :identity
79     @command << "--evalue #{@options[:e_val]}" if @options.has_key? :e_val
80     @command << "--rev"
81
82                 execute
83   end
84
85         # Method to parse a Uclust .uc file and for each line of data
86         # yield a Biopiece record.
87   def each_cluster
88     record = {}
89
90     File.open(@outfile, mode="r") do |ios|
91       ios.each_line do |line|
92         if line !~ /^#/
93           fields = line.chomp.split("\t")
94
95           next if fields[0] == 'C'
96
97           record[:TYPE]     = fields[0]
98           record[:CLUSTER]  = fields[1].to_i
99           record[:IDENT]    = fields[3].to_f
100           record[:Q_ID]     = fields[8]
101
102           yield record
103         end
104       end
105     end
106
107     self # conventionally
108   end
109
110         # Method to parse a Useach user defined tabular file and for each line of data
111         # yield a Biopiece record.
112   def each_hit
113     record = {}
114
115     File.open(@outfile, mode="r") do |ios|
116       ios.gets   # skip comment line
117       ios.each_line do |line|
118         fields = line.chomp.split("\t")
119
120         record[:REC_TYPE] = "USEARCH"
121         record[:S_ID]     = fields[0]
122         record[:S_BEG]    = fields[1].to_i
123         record[:S_END]    = fields[2].to_i
124         record[:Q_ID]     = fields[3]
125         record[:SCORE]    = fields[4].to_f
126         record[:STRAND]   = fields[5]
127
128         yield record
129       end
130     end
131
132     self # conventionally
133   end
134
135   private
136
137         # Method to execute a command using a system() call.
138         # The command is composed of bits from the @command variable.
139         def execute
140                 @command.unshift "nice -n 19"
141     @command << "--rev" if @options[:comp]
142                 @command << "> /dev/null 2>&1" unless @options[:verbose]
143                 command = @command.join(" ")
144     system(command)
145     raise "Command failed: #{command}" unless $?.success?
146
147                 @command = []
148         end
149 end
150
151 __END__
152