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, file_fasta, file_pattern, file_patscan)
42 @file_fasta = file_fasta
43 @file_pattern = file_pattern
44 @file_patscan = file_patscan
46 pat = Pattern.new(@options)
47 pat.write(@file_pattern)
52 commands << "nice -n 19"
53 commands << "scan_for_matches"
54 commands << @file_pattern
55 commands << "< #{@file_fasta}"
56 commands << "> #{@file_patscan}"
57 command = commands.join(" ")
59 raise PatScanError, "Command failed: #{command}" unless $?.success?
65 Fasta.open(@file_patscan, mode='r') do |ios|
66 ios.each_entry do |entry|
67 if entry.seq_name =~ /^(\d+):\[(\d+),(\d+)\]$/
71 matches[name] = [start, stop - start + 1] unless matches.has_key? name
73 raise "Failed to parse sequence name: #{entry.seq_name}"
82 # Error class for Pattern errors.
83 class PatternError < StandardError; end;
86 def initialize(options)
89 @patterns << pattern_internal
90 @patterns += patterns_end if @options[:trim_end]
96 while @patterns.size > 1
97 new_patterns = @patterns[0 ... -2]
98 new_patterns << "( #{@patterns[-2 .. -1].join(' | ')} )"
100 @patterns = new_patterns
107 File.open(file, mode='w') do |ios|
115 pattern = @options[:adaptor]
116 mis = mis_count(pattern)
117 ins = ins_count(pattern)
118 del = del_count(pattern)
120 "#{pattern}[#{mis},#{ins},#{del}]"
125 adaptor = @options[:adaptor]
127 raise PatternError, "trim_end_min > adaptor length: #{@options[:trim_end_min]} > #{adaptor.length - 1}" if @options[:trim_end_min] > adaptor.length - 1
129 (adaptor.length - 1).downto(@options[:trim_end_min]) do |i|
130 pattern = adaptor[0 ... i]
131 mis = mis_count(pattern)
132 ins = ins_count(pattern)
133 del = del_count(pattern)
134 patterns << "#{pattern}[#{mis},#{ins},#{del}] $"
140 def mis_count(pattern)
141 (pattern.length * @options[:mismatches] * 0.01).round
144 def ins_count(pattern)
145 (pattern.length * @options[:insertions] * 0.01).round
148 def del_count(pattern)
149 (pattern.length * @options[:deletions] * 0.01).round
154 casts << {:long=>'adaptor', :short=>'a', :type=>'string', :mandatory=>true, :default=>nil, :allowed=>nil, :disallowed=>nil}
155 casts << {:long=>'trim_end', :short=>'t', :type=>'flag', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
156 casts << {:long=>'trim_end_min', :short=>'l', :type=>'uint', :mandatory=>false, :default=>10, :allowed=>nil, :disallowed=>'0'}
157 casts << {:long=>'mismatches', :short=>'m', :type=>'uint', :mandatory=>false, :default=>10, :allowed=>nil, :disallowed=>nil}
158 casts << {:long=>'insertions', :short=>'i', :type=>'uint', :mandatory=>false, :default=>5, :allowed=>nil, :disallowed=>nil}
159 casts << {:long=>'deletions', :short=>'d', :type=>'uint', :mandatory=>false, :default=>5, :allowed=>nil, :disallowed=>nil}
161 options = Biopieces.options_parse(ARGV, casts)
163 tmpdir = Biopieces.mktmpdir
164 file_fasta = File.join(tmpdir, "data.fna")
165 file_records = File.join(tmpdir, "data.stream")
166 file_pattern = File.join(tmpdir, "pattern.txt")
167 file_patscan = File.join(tmpdir, "patscan.fna")
171 Biopieces.open(options[:stream_in], file_records) do |input, output|
172 Fasta.open(file_fasta, mode='w') do |out_fa|
173 input.each do |record|
176 if record.has_key? :SEQ
177 record[:SEQ_NAME] = count
186 patscan = PatScan.new(options, file_fasta, file_pattern, file_patscan)
188 matches = patscan.parse_results
192 Biopieces.open(file_records, options[:stream_out]) do |input, output|
193 input.each_record do |record|
194 if record.has_key? :SEQ
195 if matches.has_key? count
196 record[:ADAPTOR_POS] = matches[count].first
197 record[:ADAPTOR_LEN] = matches[count].last
208 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<