]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/find_adaptor
fixed bug in find_adaptor
[biopieces.git] / bp_bin / find_adaptor
1 #!/usr/bin/env ruby
2
3 # Copyright (C) 2007-2011 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 # Remove adaptors or parts thereof from sequences in the stream.
28
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
30
31
32 require 'biopieces'
33 require 'seq'
34
35 VERBOSE = true
36
37 def disambiguate(adaptor)
38   adaptor_disamb = adaptor.dup
39   adaptor_disamb.gsub!('U', 'T')
40   adaptor_disamb.gsub!('R', '[AG]')
41   adaptor_disamb.gsub!('Y', '[CT]')
42   adaptor_disamb.gsub!('S', '[GC]')
43   adaptor_disamb.gsub!('W', '[AT]')
44   adaptor_disamb.gsub!('M', '[AC]')
45   adaptor_disamb.gsub!('K', '[GT]')
46   adaptor_disamb.gsub!('V', '[ACG]')
47   adaptor_disamb.gsub!('H', '[ACT]')
48   adaptor_disamb.gsub!('D', '[AGT]')
49   adaptor_disamb.gsub!('B', '[CGT]')
50   adaptor_disamb.gsub!('N', '.')
51   adaptor_disamb
52 end
53
54 class Seq
55   # Method that finds an adaptor or part thereof in the sequence of a Seq object.
56   # Returns a Match object if the adaptor was found otherwise nil. The ed_percent
57   # indicates the maximum edit distance allowed in all possible overlaps.
58   def adaptor_find(adaptor, adaptor_disamb, pos = 0, ed_percent = 0)
59     raise SeqError, "Edit distance percent out of range #{ed_percent}" unless (0 .. 100).include? ed_percent
60
61     if pos < 0
62       pos = self.length + pos # pos offset from the right end
63     end
64
65     if pos < self.length
66       if match = adaptor_find_simple(adaptor_disamb, pos)
67         return match
68       elsif match = adaptor_find_complex(adaptor, pos, ed_percent)
69         return match
70       elsif match = adaptor_partial_find_complex(adaptor, pos, ed_percent)
71         return match
72       end
73     end
74   end
75
76   private
77
78   # Method to find an adaptor in a sequence taking into account ambiguity
79   # codes, but not considering mismatches, insertions, and deletions.
80   def adaptor_find_simple(adaptor, pos)
81     self.seq.upcase.match(adaptor, pos) do |m|
82       return Match.new($`.length, m, m.to_s.length, 0, 0, 0, m.to_s.length)
83     end
84   end
85
86   # Method to find an adaptor in a sequence taking into account ambiguity
87   # codes, mismatches, insertions, and deletions.
88   def adaptor_find_complex(adaptor, pos, ed_percent)
89     ed_max = (adaptor.length * ed_percent * 0.01).round
90
91     match = self.match(adaptor, pos, ed_max)
92
93     match
94   end
95
96   # Method to find part of an adaptor at the right end of a sequence taking
97   # into account ambiguity codes, mismatches, insertions, and deletions.
98   def adaptor_partial_find_complex(adaptor, pos, ed_percent)
99     if pos > self.length - adaptor.length
100       adaptor = adaptor[0 ... self.length - pos]
101     else
102       adaptor = adaptor[0 ... adaptor.length - 1]
103
104       pos = self.length - adaptor.length
105     end
106
107     #puts self.seq if VERBOSE
108
109     while adaptor.length > 0
110       #puts (" " * pos) + adaptor if VERBOSE
111
112       ed_max = (adaptor.length * ed_percent * 0.01).round
113
114       if ed_max == 0
115         self.seq.upcase.match(adaptor, pos) do |m|
116           return Match.new($`.length, m, m.to_s.length, 0, 0, 0, m.to_s.length)
117         end
118       else
119         self.scan(adaptor, pos, ed_max).each do |match|
120           return match
121         end
122       end
123
124       adaptor = adaptor[0 ... -1]
125
126       pos += 1
127     end
128   end
129 end
130
131 casts = []
132 casts << {:long=>'adaptor',       :short=>'r', :type=>'string', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
133 casts << {:long=>'edit_distance', :short=>'e', :type=>'uint',   :mandatory=>false, :default=>20,  :allowed=>nil, :disallowed=>nil}
134 casts << {:long=>'pos',           :short=>'p', :type=>'int',    :mandatory=>false, :default=>1,   :allowed=>nil, :disallowed=>"0"}
135
136 bp = Biopieces.new
137
138 options = bp.parse(ARGV, casts)
139
140 adaptor        = options[:adaptor].to_s.upcase
141 adaptor_disamb = disambiguate(adaptor)
142
143 pos  = options[:pos]
144 pos -= 1 if pos > 0  # pos was 1-based
145
146 bp.each_record do |record|
147   if record.has_key? :SEQ
148     entry = Seq.new(record[:SEQ_NAME], record[:SEQ], "dna", record[:SCORES])
149
150     if match = entry.adaptor_find(adaptor, adaptor_disamb, pos, options[:edit_distance])
151       record[:ADAPTOR_POS]   = match.pos
152       record[:ADAPTOR_LEN]   = match.length
153       record[:ADAPTOR_MATCH] = match.match
154     end
155   end
156
157   bp.puts record
158 end
159
160
161 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
162
163
164 __END__