3 # Copyright (C) 2007-2012 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'
34 require 'maasha/seq/backtrack'
36 def percent2real(length, percent)
37 (length * percent * 0.01).round
43 casts << {:long=>'forward', :short=>'f', :type=>'string', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
44 casts << {:long=>'forward_rc', :short=>'F', :type=>'string', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
45 casts << {:long=>'reverse', :short=>'r', :type=>'string', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
46 casts << {:long=>'reverse_rc', :short=>'R', :type=>'string', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
47 casts << {:long=>'len_forward', :short=>'l', :type=>'uint', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>'0'}
48 casts << {:long=>'len_reverse', :short=>'L', :type=>'uint', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>'0'}
49 casts << {:long=>'mismatches', :short=>'m', :type=>'uint', :mandatory=>false, :default=>10, :allowed=>nil, :disallowed=>nil}
50 casts << {:long=>'insertions', :short=>'i', :type=>'uint', :mandatory=>false, :default=>5, :allowed=>nil, :disallowed=>nil}
51 casts << {:long=>'deletions', :short=>'d', :type=>'uint', :mandatory=>false, :default=>5, :allowed=>nil, :disallowed=>nil}
53 options = Biopieces.options_parse(ARGV, casts)
55 if options[:forward_rc]
56 options[:forward] = Seq.new(seq: options[:forward_rc], type: :dna).reverse.complement.seq
59 if options[:reverse_rc]
60 options[:reverse] = Seq.new(seq: options[:reverse_rc], type: :dna).reverse.complement.seq
63 raise ArgumentError, "no adaptor specified" unless options[:forward] or options[:reverse]
66 options[:len_forward] = options[:forward].length unless options[:len_forward]
68 if options[:len_forward] > options[:forward].length
69 raise ArgumentError, "len_forward > forward adaptor (#{options[:len_forward]} > #{options[:forward].length})"
72 fmis = percent2real(options[:forward].length, options[:mismatches])
73 fins = percent2real(options[:forward].length, options[:insertions])
74 fdel = percent2real(options[:forward].length, options[:deletions])
78 options[:len_reverse] = options[:reverse].length unless options[:len_reverse]
80 if options[:len_reverse] > options[:reverse].length
81 raise ArgumentError, "len_reverse > reverse adaptor (#{options[:len_reverse]} > #{options[:reverse].length})"
84 rmis = percent2real(options[:reverse].length, options[:mismatches])
85 rins = percent2real(options[:reverse].length, options[:insertions])
86 rdel = percent2real(options[:reverse].length, options[:deletions])
89 Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
90 input.each do |record|
92 entry = Seq.new_bp(record)
94 if options[:forward] and record[:SEQ].length >= options[:forward].length
95 if m = entry.patmatch(options[:forward], max_mismatches: fmis, max_insertions: fins, max_deletions: fdel)
96 record[:ADAPTOR_POS_LEFT] = m.pos
97 record[:ADAPTOR_LEN_LEFT] = m.length
98 record[:ADAPTOR_PAT_LEFT] = m.match
99 elsif options[:len_forward] < options[:forward].length
100 len = options[:forward].length - 1
101 pat = options[:forward]
103 while len >= options[:len_forward]
104 fmis = percent2real(len, options[:mismatches])
105 fins = percent2real(len, options[:insertions])
106 fdel = percent2real(len, options[:deletions])
108 pat = pat[1 ... pat.length]
110 if m = entry.patmatch(pat, start: 0, stop: len, max_mismatches: fmis, max_insertions: fins, max_deletions: fdel)
111 record[:ADAPTOR_POS_LEFT] = m.pos
112 record[:ADAPTOR_LEN_LEFT] = m.length
113 record[:ADAPTOR_PAT_LEFT] = m.match
123 if options[:reverse] and record[:SEQ].length >= options[:reverse].length
124 if m = entry.patmatch(options[:reverse], max_mismatches: rmis, max_insertions: rins, max_deletions: rdel)
125 record[:ADAPTOR_POS_RIGHT] = m.pos
126 record[:ADAPTOR_LEN_RIGHT] = m.length
127 record[:ADAPTOR_PAT_RIGHT] = m.match
128 elsif options[:len_reverse] < options[:reverse].length
129 len = options[:reverse].length - 1
130 pat = options[:reverse]
132 while len >= options[:len_reverse]
133 rmis = percent2real(len, options[:mismatches])
134 rins = percent2real(len, options[:insertions])
135 rdel = percent2real(len, options[:deletions])
137 pat = pat[0 ... pat.length - 1]
139 if m = entry.patmatch(pat, start: entry.length - len, max_mismatches: rmis, max_insertions: rins, max_deletions: rdel)
140 record[:ADAPTOR_POS_RIGHT] = m.pos
141 record[:ADAPTOR_LEN_RIGHT] = m.length
142 record[:ADAPTOR_PAT_RIGHT] = m.match
157 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<