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 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
32 require 'maasha/biopieces'
35 def disambiguate(adaptor)
36 adaptor_disamb = adaptor.dup
37 adaptor_disamb.gsub!('U', 'T')
38 adaptor_disamb.gsub!('R', '[AG]')
39 adaptor_disamb.gsub!('Y', '[CT]')
40 adaptor_disamb.gsub!('S', '[GC]')
41 adaptor_disamb.gsub!('W', '[AT]')
42 adaptor_disamb.gsub!('M', '[AC]')
43 adaptor_disamb.gsub!('K', '[GT]')
44 adaptor_disamb.gsub!('V', '[ACG]')
45 adaptor_disamb.gsub!('H', '[ACT]')
46 adaptor_disamb.gsub!('D', '[AGT]')
47 adaptor_disamb.gsub!('B', '[CGT]')
48 adaptor_disamb.gsub!('N', '.')
53 # Method that finds an adaptor or part thereof in the sequence of a Seq object.
54 # Returns a Match object if the adaptor was found otherwise nil. The ed_percent
55 # indicates the maximum edit distance allowed in all possible overlaps.
56 def adaptor_find(adaptor, adaptor_disamb, pos = 0, ed_percent = 0, dist = 0)
57 raise SeqError, "Edit distance percent out of range #{ed_percent}" unless (0 .. 100).include? ed_percent
60 pos = self.length + pos # pos offset from the right end
63 if pos < self.length - dist + 1
64 if match = adaptor_find_simple(adaptor_disamb, pos)
66 elsif match = adaptor_find_complex(adaptor, pos, ed_percent)
68 elsif match = adaptor_partial_find_complex(adaptor, pos, ed_percent, dist)
76 # Method to find an adaptor in a sequence taking into account ambiguity
77 # codes, but not considering mismatches, insertions, and deletions.
78 def adaptor_find_simple(adaptor, pos)
79 self.seq.upcase.match(adaptor, pos) do |m|
80 return Match.new($`.length, m, m.to_s.length, 0, 0, 0, m.to_s.length)
84 # Method to find an adaptor in a sequence taking into account ambiguity
85 # codes, mismatches, insertions, and deletions.
86 def adaptor_find_complex(adaptor, pos, ed_percent)
87 ed_max = (adaptor.length * ed_percent * 0.01).round
89 match = self.match(adaptor, pos, ed_max)
94 # Method to find part of an adaptor at the right end of a sequence taking
95 # into account ambiguity codes, mismatches, insertions, and deletions.
96 def adaptor_partial_find_complex(adaptor, pos, ed_percent, dist)
97 if pos > self.length - adaptor.length
98 adaptor = adaptor[0 ... self.length - pos]
100 adaptor = adaptor[0 ... adaptor.length]
102 pos = self.length - adaptor.length
107 while adaptor.length > 0 and adaptor.length >= dist
108 # puts (" " * pos) + adaptor
110 ed_max = (adaptor.length * ed_percent * 0.01).round
113 self.seq.upcase.match(adaptor, pos) do |m|
114 return Match.new($`.length, m, m.to_s.length, 0, 0, 0, m.to_s.length)
117 self.scan(adaptor, pos, ed_max).each do |match|
122 adaptor = adaptor[0 ... -1]
130 casts << {:long=>'adaptor', :short=>'r', :type=>'string', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
131 casts << {:long=>'edit_distance', :short=>'e', :type=>'uint', :mandatory=>false, :default=>20, :allowed=>nil, :disallowed=>nil}
132 casts << {:long=>'pos', :short=>'p', :type=>'int', :mandatory=>false, :default=>1, :allowed=>nil, :disallowed=>"0"}
133 casts << {:long=>'dist', :short=>'d', :type=>'uint', :mandatory=>false, :default=>0, :allowed=>nil, :disallowed=>nil}
134 casts << {:long=>'cache', :short=>'c', :type=>'flag', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
136 options = Biopieces.options_parse(ARGV, casts)
138 adaptor = options[:adaptor].to_s.upcase
139 adaptor_disamb = disambiguate(adaptor)
142 pos -= 1 if pos > 0 # pos was 1-based
146 Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
147 input.each_record do |record|
148 if record.has_key? :SEQ
149 entry = Seq.new(record[:SEQ_NAME], record[:SEQ], "dna", record[:SCORES])
151 if cache[entry.seq.upcase.to_sym] and options[:cache]
152 match = cache[entry.seq.upcase]
154 match = entry.adaptor_find(adaptor, adaptor_disamb, pos, options[:edit_distance], options[:dist])
156 cache[entry.seq.upcase.to_sym] = match if match and options[:cache]
160 record[:ADAPTOR_POS] = match.pos
161 record[:ADAPTOR_LEN] = match.length
162 record[:ADAPTOR_MATCH] = match.match
171 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<