3 # Copyright (C) 2007-2012 Martin A. Hansen.
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.
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.
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.
19 # http://www.gnu.org/copyleft/gpl.html
21 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
23 # This program is part of the Biopieces framework (www.biopieces.org).
25 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
27 # Denoises sequences with quality scores in the stream.
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
32 require 'maasha/biopieces'
34 require 'maasha/fastq'
35 require 'maasha/fasta'
36 require 'maasha/align'
37 require 'maasha/usearch'
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 casts << {:long=>'frequency_min', :short=>'f', :type=>'uint', :mandatory=>true, :default=>2, :allowed=>nil, :disallowed=>"0"}
43 casts << {:long=>'quality_min', :short=>'q', :type=>'uint', :mandatory=>true, :default=>20, :allowed=>nil, :disallowed=>nil}
44 casts << {:long=>'consensus_min', :short=>'C', :type=>'float', :mandatory=>true, :default=>0.2, :allowed=>nil, :disallowed=>nil}
46 options = Biopieces.options_parse(ARGV, casts)
47 tmpdir = Biopieces.mktmpdir
48 fastq_file = File.join(tmpdir, "test.fq")
49 fasta_file = File.join(tmpdir, "test.fna")
50 fasta_file_align = File.join(tmpdir, "test.aln.fna")
52 def alignment_to_fastq(entries, index)
53 entries.each do |entry|
54 cluster, ident, name = entry.seq_name.split('|')
56 entry.qual = index.get(name).qual # disk based lookup
58 entry.seq.scan(/-+/) do |m|
59 entry.qual = entry.qual[0 ... $`.length] + ('@' * m.length) + entry.qual[$`.length .. -1]
64 index = FastqIndex.new
67 Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
68 Fasta.open(fasta_file, "w") do |fasta_io|
69 Fastq.open(fastq_file, "w") do |fastq_io|
70 input.each_record do |record|
71 if record.has_key? :SEQ and record.has_key? :SCORES
72 entry = Seq.new_bp(record)
73 entry.seq_name = seq_count.to_s
75 fasta_io.puts entry.to_fasta
76 fastq_io.puts entry.to_fastq
88 fastq_io = File.open(fastq_file, "r")
94 uc = Usearch.new(fasta_file, fasta_file_align, options)
99 uc.each_alignment do |align|
100 if align.members >= options[:cluster_min]
101 align.options = options
103 alignment_to_fastq(align.entries, index)
105 cons = align.consensus
106 cons.seq_name = "#{tot_clusters}_#{align.members}"
109 new_record = cons.to_bp
110 new_record[:CLUSTER] = tot_clusters
111 new_record[:CLUSTER_COUNT] = align.members
113 puts align if options[:verbose]
115 output.puts new_record
122 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<