]> git.donarmstrong.com Git - biopieces.git/blob - code_ruby/lib/maasha/usearch.rb
added ustar and each_alignment methods to 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     @command << "--rev" if @options[:comp]
71
72                 execute
73   end
74
75   # Method to execute database search.
76   def usearch
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
81     @command << "--rev"
82
83                 execute
84   end
85
86   # Method to execute ustar alignment.
87   def ustar
88     @command << %Q{grep "^[SH]" #{@outfile} > #{@outfile}.sub}
89
90     execute
91
92     File.rename "#{@outfile}.sub", @outfile
93
94     @command << "usearch --uc2fastax #{@outfile} --input #{@infile} --output #{@infile}.sub"
95
96     execute
97
98     @command << "usearch --staralign #{@infile}.sub --output #{@outfile}"
99
100     execute
101
102     File.delete "#{@infile}.sub"
103   end
104
105         # Method to parse a Uclust .uc file and for each line of data
106         # yield a Biopiece record.
107   def each_cluster
108     record = {}
109
110     File.open(@outfile, "r") do |ios|
111       ios.each_line do |line|
112         if line !~ /^#/
113           fields = line.chomp.split("\t")
114
115           next if fields[0] == 'C'
116
117           record[:TYPE]     = fields[0]
118           record[:CLUSTER]  = fields[1].to_i
119           record[:IDENT]    = fields[3].to_f
120           record[:Q_ID]     = fields[8]
121
122           yield record
123         end
124       end
125     end
126
127     self # conventionally
128   end
129
130         # Method to parse a Useach user defined tabular file and for each line of data
131         # yield a Biopiece record.
132   def each_hit
133     record = {}
134
135     File.open(@outfile, "r") do |ios|
136       ios.gets   # skip comment line
137       ios.each_line do |line|
138         fields = line.chomp.split("\t")
139
140         record[:REC_TYPE] = "USEARCH"
141         record[:S_ID]     = fields[0]
142         record[:S_BEG]    = fields[1].to_i
143         record[:S_END]    = fields[2].to_i
144         record[:Q_ID]     = fields[3]
145         record[:SCORE]    = fields[4].to_f
146         record[:STRAND]   = fields[5]
147
148         yield record
149       end
150     end
151
152     self # conventionally
153   end
154
155   # Method to parse a FASTA file with Ustar alignments and for each alignment
156   # yield an Align object.
157   def each_alignment
158     old_cluster = 0
159     entries     = []
160
161     Fasta.open(@outfile, "r") do |ios|
162       ios.each do |entry|
163         entry.seq.tr!('.', '-')   # Replace . with - in Ustar alignments.
164         cluster, identity, name = entry.seq_name.split('|')
165         cluster = cluster.to_i
166
167         if cluster == old_cluster
168           entries << entry
169         else
170           fix_alignment(entries)
171
172           yield Align.new(entries)
173
174           old_cluster = cluster
175           entries     = []
176           entries << entry
177         end
178       end
179     end
180   end
181
182   private
183
184   # Method that fixed Ustar bug resulting in alignments with uneven 
185   # sequence length.
186   def fix_alignment(entries)
187     if entries.size > 1
188       min, max = entries.minmax { |a, b| a.length <=> b.length } 
189
190       if min.length != max.length
191         entries.each do |entry|
192           entry.seq << '-' * (max.length - entry.length)
193         end
194       end
195     end
196   end
197
198         # Method to execute a command using a system() call.
199         # The command is composed of bits from the @command variable.
200         def execute
201                 @command.unshift "nice -n 19"
202                 @command << "> /dev/null 2>&1" unless @options[:verbose]
203                 command = @command.join(" ")
204     system(command)
205     raise "Command failed: #{command}" unless $?.success?
206
207                 @command = []
208         end
209 end
210
211 __END__
212