]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/usearch_seq
upgraded usearch_seq
[biopieces.git] / bp_bin / usearch_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 # Usearch sequences in the stream against a specified database.
28
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
30
31 require 'maasha/biopieces'
32 require 'maasha/fasta'
33
34 class Usearch
35   include Enumerable
36
37   def initialize(infile, outfile, options)
38     @infile  = infile
39     @outfile = outfile
40     @options = options
41     @command = []
42   end
43
44   # Method to execute database search.
45   def usearch
46     @command << "usearch --query #{@infile} --db #{@options[:database]} --userout #{@outfile}"
47     @command << "--userfields target+tloz+thiz+query+bits+strand"
48     @command << "--id #{@options[:identity]}"  if @options.has_key? :identity
49     @command << "--evalue #{@options[:e_val]}" if @options.has_key? :e_val
50
51                 execute
52   end
53
54         # Method to parse a Useach .uc file and for each line of data
55         # yield a Biopiece record.
56   def each
57     record = {}
58
59     File.open(@outfile, mode="r") do |ios|
60       ios.gets   # skip comment line
61       ios.each_line do |line|
62         fields = line.chomp.split("\t")
63
64         record[:REC_TYPE] = "USEARCH"
65         record[:S_ID]     = fields[0]
66         record[:S_BEG]    = fields[1].to_i
67         record[:S_END]    = fields[2].to_i
68         record[:Q_ID]     = fields[3]
69         record[:SCORE]    = fields[4].to_f
70         record[:STRAND]   = fields[5]
71
72         yield record
73       end
74     end
75
76     self # conventionally
77   end
78
79   private
80
81         # Method to execute a command using a system() call.
82         # The command is composed of bits from the @command variable.
83         def execute
84                 @command.unshift "nice -n 19"
85     @command << "--rev" if @options[:comp]
86                 @command << "> /dev/null 2>&1" unless @options[:verbose]
87                 command = @command.join(" ")
88     system(command)
89     raise "Command failed: #{command}" unless $?.success?
90
91                 @command = []
92         end
93 end
94
95 casts = []
96 casts << {:long=>'database', :short=>'d', :type=>'file!', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
97 casts << {:long=>'comp',     :short=>'c', :type=>'flag',  :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
98 casts << {:long=>'identity', :short=>'i', :type=>'float', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
99 casts << {:long=>'e_val',    :short=>'e', :type=>'float', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
100
101 options = Biopieces.options_parse(ARGV, casts)
102
103 raise ArgumentError, "--identity or --e_val must be specified" unless options[:identity] or options[:e_val]
104
105 tmpdir  = Biopieces.mktmpdir
106 infile  = File.join(tmpdir, "in.fna")
107 outfile = File.join(tmpdir, "out.uc")
108
109 Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
110   Fasta.open(infile, mode="w") do |fasta_io|
111     input.each_record do |record|
112       output.puts record
113
114       if record.has_key? :SEQ_NAME and record.has_key? :SEQ
115         fasta_io.puts Seq.new_bp(record).to_fasta
116       end
117     end
118   end
119
120   uc = Usearch.new(infile, outfile, options)
121
122   uc.usearch
123
124   uc.each do |record|
125     output.puts record
126   end
127 end
128
129
130 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
131
132
133 __END__