]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/remove_primers
refactored backtrack methods to use options hash in method invocations
[biopieces.git] / bp_bin / remove_primers
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 primers from sequences in the stream.
28
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
30
31
32 require 'pp'
33 require 'maasha/biopieces'
34 require 'maasha/seq'
35 require 'maasha/seq/backtrack'
36
37 include BackTrack
38
39 casts = []
40 casts << {:long=>'forward',    :short=>'f', :type=>'string', :mandatory=>true,  :default=>nil, :allowed=>nil, :disallowed=>nil}
41 casts << {:long=>'reverse',    :short=>'r', :type=>'string', :mandatory=>true,  :default=>nil, :allowed=>nil, :disallowed=>nil}
42 casts << {:long=>'mismatches', :short=>'m', :type=>'uint',   :mandatory=>false, :default=>2,   :allowed=>nil, :disallowed=>nil}
43 casts << {:long=>'insertions', :short=>'i', :type=>'uint',   :mandatory=>false, :default=>1,   :allowed=>nil, :disallowed=>nil}
44 casts << {:long=>'deletions',  :short=>'d', :type=>'uint',   :mandatory=>false, :default=>1,   :allowed=>nil, :disallowed=>nil}
45
46 options = Biopieces.options_parse(ARGV, casts)
47
48 Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
49   input.each do |record|
50     if record[:SEQ]
51       forward = false
52       reverse = false
53       seq = Seq.new_bp(record)
54
55       seq.patscan(options[:forward].to_s,
56                   max_mismatches: options[:mismatches],
57                   max_insertions: options[:insertions],
58                   max_deletions:  options[:deletions]) do |match|
59         record[:FORWARD_POS] = match.pos
60         record[:FORWARD_LEN] = match.length
61         pos = match.pos + match.length
62         len = seq.length - pos
63         seq.subseq!(pos, len) if len > 0
64         forward = true
65         break
66       end
67
68       seq.patscan(options[:reverse].to_s,
69                   max_mismatches: options[:mismatches],
70                   max_insertions: options[:insertions],
71                   max_deletions:  options[:deletions]) do |match|
72         record[:REVERSE_POS] = match.pos
73         record[:REVERSE_LEN] = match.length
74         pos = 0
75         len = match.pos
76
77         if len == 0
78           seq.seq  = ""
79           seq.qual = "" if seq.qual
80         else
81           seq.subseq!(pos, len)
82         end 
83
84         reverse = true
85         break
86       end
87
88       if forward or reverse
89         record.merge!(seq.to_bp)
90       end
91
92       output.puts record
93     else
94       output.puts record
95     end
96   end
97 end
98
99 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
100
101 __END__