3 # Copyright (C) 2007-2011 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 # Remove adaptors or parts thereof from sequences in the stream.
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
33 require 'maasha/biopieces'
34 require 'maasha/fasta'
36 # Error class for PatScan errors.
37 class PatScanError < StandardError; end;
40 def initialize(options, tmpdir, file_pattern, cpus)
43 @file_pattern = file_pattern
45 @files_fasta = Dir.glob(File.join(@tmpdir, "*.fna"))
47 pat = Pattern.new(@options)
48 pat.write(@file_pattern)
54 # @files_fasta.each do |file|
56 # Process.wait if ( child_count += 1 ) >= @cpus
58 # command = command_compile(file)
60 # raise PatScanError, "Command failed: #{command}" unless $?.success?
69 @files_fasta.each do |file|
70 Thread.pass while child_count >= @cpus
74 command = command_compile(file)
76 raise PatScanError, "Command failed: #{command}" unless $?.success?
84 # ch_mutex = Mutex.new
87 # @files_fasta.each do |file|
88 # Thread.pass while child_count >= @cpus
89 # ch_mutex.synchronize { child_count += 1 }
91 # threads << Thread.new do
92 # command = command_compile(file)
94 # raise PatScanError, "Command failed: #{command}" unless $?.success?
95 # ch_mutex.synchronize { child_count -= 1 }
99 # threads.each { |t| t.join }
102 def command_compile(file)
104 commands << "nice -n 19"
105 commands << "scan_for_matches"
106 commands << @file_pattern
107 commands << "< #{file}"
108 commands << "> #{file}.out"
109 command = commands.join(" ")
113 files_result = Dir.glob(File.join(@tmpdir, "*.out"))
117 files_result.each do |file|
118 Fasta.open(file, 'r') do |ios|
120 if entry.seq_name =~ /^(\d+):\[(\d+),(\d+)\]$/
124 matches[name] = [start, stop - start + 1] unless matches.has_key? name
126 raise "Failed to parse sequence name: #{entry.seq_name}"
136 # Error class for Pattern errors.
137 class PatternError < StandardError; end;
140 def initialize(options)
143 @patterns << pattern_internal
144 @patterns += patterns_end if @options[:partial]
150 while @patterns.size > 1
151 new_patterns = @patterns[0 ... -2]
152 new_patterns << "( #{@patterns[-2 .. -1].join(' | ')} )"
154 @patterns = new_patterns
161 File.open(file, 'w') do |ios|
169 pattern = @options[:adaptor]
170 mis = mis_count(pattern)
171 ins = ins_count(pattern)
172 del = del_count(pattern)
174 "#{pattern}[#{mis},#{ins},#{del}]"
179 adaptor = @options[:adaptor]
181 raise PatternError, "len > adaptor length: #{@options[:len]} > #{adaptor.length - 1}" if @options[:len] > adaptor.length - 1
183 (adaptor.length - 1).downto(@options[:len]) do |i|
184 pattern = adaptor[0 ... i]
185 mis = mis_count(pattern)
186 ins = ins_count(pattern)
187 del = del_count(pattern)
188 patterns << "#{pattern}[#{mis},#{ins},#{del}] $"
194 def mis_count(pattern)
195 (pattern.length * @options[:mismatches] * 0.01).round
198 def ins_count(pattern)
199 (pattern.length * @options[:insertions] * 0.01).round
202 def del_count(pattern)
203 (pattern.length * @options[:deletions] * 0.01).round
208 casts << {:long=>'adaptor', :short=>'a', :type=>'string', :mandatory=>true, :default=>nil, :allowed=>nil, :disallowed=>nil}
209 casts << {:long=>'partial', :short=>'p', :type=>'flag', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
210 casts << {:long=>'len', :short=>'l', :type=>'uint', :mandatory=>false, :default=>10, :allowed=>nil, :disallowed=>'0'}
211 casts << {:long=>'mismatches', :short=>'m', :type=>'uint', :mandatory=>false, :default=>10, :allowed=>nil, :disallowed=>nil}
212 casts << {:long=>'insertions', :short=>'i', :type=>'uint', :mandatory=>false, :default=>5, :allowed=>nil, :disallowed=>nil}
213 casts << {:long=>'deletions', :short=>'d', :type=>'uint', :mandatory=>false, :default=>5, :allowed=>nil, :disallowed=>nil}
214 casts << {:long=>'cpus', :short=>'c', :type=>'uint', :mandatory=>false, :default=>1, :allowed=>nil, :disallowed=>'0'}
216 BASE_PER_FILE = 10_000_000
218 options = Biopieces.options_parse(ARGV, casts)
220 #tmpdir = Biopieces.mktmpdir
221 tmpdir = "/home/maasha/Tyt"
222 file_records = File.join(tmpdir, "data.stream")
223 file_pattern = File.join(tmpdir, "pattern.txt")
229 Biopieces.open(options[:stream_in], file_records) do |input, output|
230 file_fasta = File.join(tmpdir, "#{number_file}.fna")
231 out_fa = Fasta.open(file_fasta, 'w')
233 input.each do |record|
236 if record.has_key? :SEQ
237 record[:SEQ_NAME] = number_seq
241 bases += record[:SEQ].length
243 if bases > BASE_PER_FILE
247 file_fasta = File.join(tmpdir, "#{number_file}.fna")
248 out_fa = Fasta.open(file_fasta, 'w')
253 out_fa.close if out_fa.respond_to? :close
256 patscan = PatScan.new(options, tmpdir, file_pattern, options[:cpus])
258 matches = patscan.parse_results
262 Biopieces.open(file_records, options[:stream_out]) do |input, output|
263 input.each_record do |record|
264 if record.has_key? :SEQ
265 if matches.has_key? number_seq
266 record[:ADAPTOR_POS] = matches[number_seq].first
267 record[:ADAPTOR_LEN] = matches[number_seq].last
278 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<