]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/find_adaptor
added find_adaptor biopiece
[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 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', '.')
49   adaptor_disamb
50 end
51
52 class Seq
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)
57     raise SeqError, "Edit distance percent out of range #{ed_percent}" unless (0 .. 100).include? ed_percent
58
59     if pos < 0
60       pos = self.length - pos # pos offset from the right end
61     end
62
63     match = adaptor_find_simple(adaptor_disamb, pos)          or
64             adaptor_find_complex(adaptor, pos, ed_percent)    or
65             adaptor_partial_find_complex(adaptor, ed_percent)
66
67     match
68   end
69
70   private
71
72   # Method to find an adaptor in a sequence taking into account ambiguity
73   # codes, but not considering mismatches, insertions, and deletions.
74   def adaptor_find_simple(adaptor, pos)
75     self.seq.upcase.match(adaptor, pos) do |m|
76       return Match.new($`.length, m, m.to_s.length, 0, 0, 0, m.to_s.length)
77     end
78   end
79
80   # Method to find an adaptor in a sequence taking into account ambiguity
81   # codes, mismatches, insertions, and deletions.
82   def adaptor_find_complex(adaptor, pos, ed_percent)
83     ed_max = (adaptor.length * ed_percent * 0.01).round
84
85     self.scan(adaptor, pos, ed_max).each do |match|
86       return match
87     end
88   end
89
90   # Method to find part of an adaptor at the right end of a sequence taking
91   # into account ambiguity codes, mismatches, insertions, and deletions.
92   def adaptor_partial_find_complex(adaptor, ed_percent)
93     adaptor = adaptor[0 ... -1]
94
95     pos = self.len - adaptor.length
96
97     while adaptor.length > 0
98       ed_max = (adaptor.length * ed_percent * 0.01).round
99
100       if ed_max == 0
101         self.seq.upcase.match(adaptor, pos) do |m|
102           return Match.new($`.length, m, m.to_s.length, 0, 0, 0, m.to_s.length)
103         end
104       else
105         self.scan(adaptor, pos, ed_max).each do |match|
106           return match
107         end
108       end
109
110       adaptor = adaptor[0 ... -1]
111
112       pos = self.len - adaptor.length
113     end
114   end
115 end
116
117 casts = []
118 casts << {:long=>'adaptor',       :short=>'r', :type=>'string', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
119 casts << {:long=>'edit_distance', :short=>'e', :type=>'uint',   :mandatory=>false, :default=>20,  :allowed=>nil, :disallowed=>nil}
120 casts << {:long=>'pos',           :short=>'p', :type=>'int',    :mandatory=>false, :default=>1,   :allowed=>nil, :disallowed=>"0"}
121
122 bp = Biopieces.new
123
124 options = bp.parse(ARGV, casts)
125
126 adaptor        = options[:adaptor].to_s.upcase
127 adaptor_disamb = disambiguate(adaptor)
128
129 pos  = options[:pos]
130 pos -= 1 if pos > 0  # pos was 1-based
131
132 bp.each_record do |record|
133   if record.has_key? :SEQ
134     entry = Seq.new(record[:SEQ_NAME], record[:SEQ], "dna", record[:SCORES])
135
136     if match = entry.adaptor_find(adaptor, adaptor_disamb, options[:pos], options[:edit_distance])
137       record[:ADAPTOR_POS]   = match.pos
138       record[:ADAPTOR_LEN]   = match.length
139       record[:ADAPTOR_MATCH] = match.match
140     end
141   end
142
143   bp.puts record
144 end
145
146
147 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
148
149
150 __END__