]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/denoise_seq
added denoise_seq
[biopieces.git] / bp_bin / denoise_seq
1 #!/usr/bin/env ruby
2
3 # Copyright (C) 2007-2012 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 # Denoises sequences with quality scores in the stream.
28
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
30
31 require 'pp'
32 require 'maasha/biopieces'
33 require 'maasha/seq'
34 require 'maasha/fastq'
35 require 'maasha/fasta'
36 require 'maasha/align'
37 require 'maasha/usearch'
38
39 casts = []
40 casts << {:long=>'identity',    :short=>'i', :type=>'float', :mandatory=>true, :default=>0.97, :allowed=>nil, :disallowed=>nil}
41 casts << {:long=>'cluster_min', :short=>'c', :type=>'uint',  :mandatory=>true, :default=>2,    :allowed=>nil, :disallowed=>"0"}
42
43 options          = Biopieces.options_parse(ARGV, casts)
44 tmpdir           = Biopieces.mktmpdir
45 fastq_file       = File.join(tmpdir, "test.fq")
46 fasta_file       = File.join(tmpdir, "test.fna")
47 fasta_file_align = File.join(tmpdir, "test.aln.fna")
48
49 def alignment_to_fastq(entries, index)
50   entries.each do |entry|
51     cluster, ident, name = entry.seq_name.split('|')
52
53     entry.qual = index.get(name).qual # disk based lookup
54
55     entry.seq.scan(/-+/) do |m|
56       entry.qual = entry.qual[0 ... $`.length] + ('@' * m.length) + entry.qual[$`.length .. -1]
57     end
58   end
59 end
60
61 index     = FastqIndex.new
62 seq_count = 0
63
64 Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
65   Fasta.open(fasta_file, "w") do |fasta_io|
66     Fastq.open(fastq_file, "w") do |fastq_io|
67       input.each_record do |record|
68         if record.has_key? :SEQ and record.has_key? :SCORES
69           entry = Seq.new_bp(record)
70           entry.seq_name = seq_count.to_s
71
72           fasta_io.puts entry.to_fasta
73           fastq_io.puts entry.to_fastq
74
75           index.add(entry)
76
77           seq_count += 1
78         else
79           output.puts record
80         end
81       end
82     end
83   end
84
85   fastq_io  = File.open(fastq_file, "r")
86   index.ios = fastq_io
87
88   tot_clusters = 0
89   tot_entries  = 0
90
91   uc = Usearch.new(fasta_file, fasta_file_align, options)
92   uc.sort_length
93   uc.cluster
94   uc.ustar
95
96   uc.each_alignment do |align|
97     if align.members >= options[:cluster_min]
98       alignment_to_fastq(align.entries, index)
99
100       cons          = align.consensus
101       cons.seq_name = "#{tot_clusters}_#{align.members}"
102       cons.indels_remove
103
104       new_record = cons.to_bp
105       new_record[:CLUSTER]       = tot_clusters
106       new_record[:CLUSTER_COUNT] = align.members
107
108       puts align if options[:verbose]
109
110       output.puts new_record
111
112       tot_clusters += 1
113     end
114   end
115 end
116
117 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
118
119
120 __END__