]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/uclust_seq
rewriting uclust_seq
[biopieces.git] / bp_bin / uclust_seq
1 #!/usr/bin/env ruby
2
3 # Copyright (C) 2007-2011 Martin A. Hansen.
4
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 # http://www.gnu.org/copyleft/gpl.html
20
21 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
22
23 # This program is part of the Biopieces framework (www.biopieces.org).
24
25 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
26
27 # Run Uclust on sequences in the stream.
28
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
30
31 require 'maasha/biopieces'
32 require 'maasha/fasta'
33
34 SORT_LIMIT = 2_000_000_000   # use mergesort for files biggern than 2Gb.
35
36 class Uclust
37   include Enumerable
38
39   def initialize(infile, outfile, options)
40     @infile  = infile
41     @outfile = outfile
42     @options = options
43     @command = []
44   end
45
46   # Method that calls Usearch sorting for sorting a FASTA file
47   # according to decending sequence length.
48   def sort
49     # usearch -sort seqs.fasta -output seqs.sorted.fasta
50     if File.size(@infile) < SORT_LIMIT
51       @command << "usearch --sort #{@infile} --output #{@infile}.sort"
52     else
53       @command << "usearch --mergesort #{@infile} --output #{@infile}.sort"
54     end
55
56                 execute
57
58     File.rename "#{@infile}.sort", @infile
59   end
60
61         # Method to execute clustering de novo.
62   def cluster
63     @command << "usearch --cluster #{@infile} --uc #{@outfile} --id #{@options[:identity]}"
64
65                 execute
66   end
67
68         # Method to parse a Uclust .uc file and for each line of data
69         # yield a Biopiece record.
70   def each
71     record = {}
72
73     File.open(@outfile, mode="r") do |ios|
74       ios.each_line do |line|
75         if line !~ /^#/
76           fields = line.chomp.split("\t")
77
78           next if fields[0] == 'C'
79
80           record[:TYPE]     = fields[0]
81           record[:CLUSTER]  = fields[1].to_i
82           record[:IDENT]    = fields[3].to_f
83           record[:Q_ID]     = fields[8]
84
85           yield record
86         end
87       end
88     end
89
90     self # conventionally
91   end
92
93   private
94
95         # Method to execute a command using a system() call.
96         # The command is composed of bits from the @command variable.
97         def execute
98                 @command.unshift "nice -n 19"
99     @command << "--rev" if @options[:comp]
100                 @command << "> /dev/null 2>&1" unless @options[:verbose]
101                 command = @command.join(" ")
102     system(command)
103     raise "Command failed: #{command}" unless $?.success?
104
105                 @command = []
106         end
107 end
108
109 casts = []
110 casts << {:long=>'no_sort',  :short=>'n', :type=>'flag',   :mandatory=>false, :default=>nil,      :allowed=>nil,        :disallowed=>nil}
111 casts << {:long=>'comp',     :short=>'c', :type=>'flag',   :mandatory=>false, :default=>nil,      :allowed=>nil,        :disallowed=>nil}
112 casts << {:long=>'identity', :short=>'i', :type=>'float',  :mandatory=>true,  :default=>0.9,      :allowed=>nil,        :disallowed=>nil}
113
114 options = Biopieces.options_parse(ARGV, casts)
115
116 tmpdir       = Biopieces.mktmpdir
117 file_records = File.join(tmpdir, "data.stream")
118 file_fasta   = File.join(tmpdir, "in.fna")
119 file_uclust  = File.join(tmpdir, "out.uc")
120
121 Biopieces.open(options[:stream_in], file_records) do |input, output|
122   Fasta.open(file_fasta, mode="w") do |fasta_io|
123     input.each_record do |record|
124       output.puts record
125
126       if record.has_key? :SEQ_NAME and record.has_key? :SEQ
127         fasta_io.puts Seq.new_bp(record).to_fasta
128       end
129     end
130   end
131 end
132
133 uc = Uclust.new(file_fasta, file_uclust, options)
134 uc.sort unless options[:no_sort]
135 uc.cluster
136
137 hash = {}
138
139 uc.each do |record|
140   hash[record[:Q_ID].to_sym] = record.dup
141 end
142
143 Biopieces.open(file_records, options[:stream_out]) do |input, output|
144   input.each_record do |record|
145     if record.has_key? :SEQ_NAME and record.has_key? :SEQ
146       if hash.has_key? record[:SEQ_NAME].to_sym
147         uc = hash[record[:SEQ_NAME].to_sym]
148         record[:CLUSTER] = uc[:CLUSTER].to_i
149         record[:IDENT]   = uc[:IDENT].to_i
150         record[:IDENT] = '*' if uc[:TYPE] == 'S'
151       end
152     end
153
154     output.puts record
155   end
156 end
157
158
159 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
160
161
162 __END__