]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/denoise_seq
baa62ad7eb6102e724f7ad7b02d842ceb9d2d2d3
[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=>'cluster_ident', :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=>1,    :allowed=>nil, :disallowed=>"0"}
42 casts << {:long=>'sequence_min',  :short=>'s', :type=>'uint',  :mandatory=>true,  :default=>1,    :allowed=>nil, :disallowed=>"0"}
43 casts << {:long=>'residue_min',   :short=>'r', :type=>'float', :mandatory=>true,  :default=>0.3,  :allowed=>nil, :disallowed=>nil}
44 casts << {:long=>'gap_max',       :short=>'g', :type=>'float', :mandatory=>true,  :default=>0.4,  :allowed=>nil, :disallowed=>nil}
45 casts << {:long=>'quality_min',   :short=>'q', :type=>'uint',  :mandatory=>true,  :default=>10,   :allowed=>nil, :disallowed=>nil}
46 casts << {:long=>'quality_mean',  :short=>'Q', :type=>'uint',  :mandatory=>true,  :default=>15,   :allowed=>nil, :disallowed=>nil}
47
48 options          = Biopieces.options_parse(ARGV, casts)
49 tmpdir           = Biopieces.mktmpdir
50 fastq_file       = File.join(tmpdir, "test.fq")
51 fasta_file       = File.join(tmpdir, "test.fna")
52 fasta_file_align = File.join(tmpdir, "test.aln.fna")
53
54 options[:identity] = options[:cluster_ident]
55
56 def alignment_to_fastq(entries, index)
57   entries.each do |entry|
58     cluster, ident, name = entry.seq_name.split('|')
59
60     entry.qual = index.get(name).qual # disk based lookup
61
62     entry.seq.scan(/-+/) do |m|
63       entry.qual = entry.qual[0 ... $`.length] + ('@' * m.length) + entry.qual[$`.length .. -1]
64     end
65   end
66 end
67
68 index     = FastqIndex.new
69 seq_count = 0
70
71 Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
72   Fasta.open(fasta_file, "w") do |fasta_io|
73     Fastq.open(fastq_file, "w") do |fastq_io|
74       input.each_record do |record|
75         if record[:SEQ] and record[:SCORES]
76           entry = Seq.new_bp(record)
77           entry.seq_name = seq_count.to_s
78
79           fasta_io.puts entry.to_fasta
80           fastq_io.puts entry.to_fastq
81
82           index.add(entry)
83
84           seq_count += 1
85         else
86           output.puts record
87         end
88       end
89     end
90   end
91
92   fastq_io  = File.open(fastq_file, "r")
93   index.ios = fastq_io
94
95   tot_clusters = 0
96   tot_entries  = 0
97
98   uc = Usearch.new(fasta_file, fasta_file_align, options)
99   uc.sort_length
100   uc.cluster
101   uc.ustar
102
103   uc.each_alignment do |align|
104     if align.members >= options[:cluster_min]
105       align.options = options
106
107       alignment_to_fastq(align.entries, index)
108
109       cons          = align.consensus
110       cons.seq_name = "#{tot_clusters}_#{align.members}"
111       cons.indels_remove
112
113       new_record = cons.to_bp
114       new_record[:CLUSTER]       = tot_clusters
115       new_record[:CLUSTER_COUNT] = align.members
116
117       puts align if options[:verbose]
118
119       output.puts new_record
120
121       tot_clusters += 1
122     end
123   end
124 end
125
126 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
127
128
129 __END__