]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/uchime_seq
adding bzip2 support in ruby
[biopieces.git] / bp_bin / uchime_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 require 'maasha/usearch'
34
35 casts = []
36 casts << {:long=>'database', :short=>'d', :type=>'file!', :mandatory=>true, :default=>nil, :allowed=>nil, :disallowed=>nil}
37
38 options = Biopieces.options_parse(ARGV, casts)
39
40 tmpdir  = Biopieces.mktmpdir
41 infile  = File.join(tmpdir, "in.fna")
42 tmpfile = File.join(tmpdir, "tmp.stream")
43 outfile = File.join(tmpdir, "out.uc")
44
45 Biopieces.open(options[:stream_in], tmpfile) do |input, output|
46   Fasta.open(infile, "w") do |fasta_io|
47     input.each_record do |record|
48       output.puts record
49
50       if record[:SEQ_NAME] and record[:SEQ]
51         fasta_io.puts Seq.new_bp(record).to_fasta
52       end
53     end
54   end
55 end
56
57 us = Usearch.new(infile, outfile, options)
58
59 us.uchime
60
61 lookup = {}
62
63 File.open(outfile, 'r') do |ios|
64   while ! ios.eof?
65     line   = ios.readline.chomp
66     fields = line.split("\t")
67
68     q_id = fields[1]
69     chim = fields[-1]
70
71     if chim == 'Y'
72       lookup[q_id] = 'YES'
73     else
74       lookup[q_id] = 'NO'
75     end
76   end
77 end
78
79 Biopieces.open(tmpfile, options[:stream_out]) do |input, output|
80   input.each_record do |record|
81     if record[:SEQ_NAME] and record[:SEQ]
82       record[:CHIMERIC] = lookup[record[:SEQ_NAME]];
83     end
84     
85     output.puts record
86   end
87 end
88
89 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
90
91
92 __END__