]> git.donarmstrong.com Git - biopieces.git/blobdiff - bp_bin/remove_primers
removed debug message
[biopieces.git] / bp_bin / remove_primers
index a68fb89ce732b4bbf0e53b3ab8637d6aca6d18f8..6544afce3087b02dd53f83f8202e7f7c647c4aa5 100755 (executable)
@@ -32,6 +32,9 @@
 require 'pp'
 require 'maasha/biopieces'
 require 'maasha/seq'
+require 'maasha/seq/backtrack'
+
+include BackTrack
 
 casts = []
 casts << {:long=>'forward',    :short=>'f', :type=>'string', :mandatory=>true,  :default=>nil, :allowed=>nil, :disallowed=>nil}
@@ -44,26 +47,46 @@ options = Biopieces.options_parse(ARGV, casts)
 
 Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
   input.each do |record|
-    if record.has_key? :SEQ
-      seq = Seq.new_bp(record)
-      pos = 0
-
-      if forward = seq.patscan(options[:forward].to_s, pos, options[:mismatches], options[:insertions], options[:deletions])
-        record[:FORWARD_POS] = forward.last.pos
-        record[:FORWARD_LEN] = forward.last.length
-        pos = forward.last.pos + forward.last.length
-        seq.subseq!(pos)
+    if record[:SEQ]
+      forward = false
+      reverse = false
+      entry = Seq.new_bp(record)
+
+      entry.patscan(options[:forward].to_s,
+                  max_mismatches: options[:mismatches],
+                  max_insertions: options[:insertions],
+                  max_deletions:  options[:deletions]) do |match|
+        record[:FORWARD_POS] = match.pos
+        record[:FORWARD_LEN] = match.length
+        pos = match.pos + match.length
+        len = entry.length - pos
+        entry = entry[pos ... pos + len] if len > 0
+        forward = true
+        break
       end
 
-      if reverse = seq.patscan(options[:reverse].to_s, pos, options[:mismatches], options[:insertions], options[:deletions])
-        record[:REVERSE_POS] = reverse.last.pos
-        record[:REVERSE_LEN] = reverse.last.length
-        pos = reverse.last.pos
-        seq.subseq!(pos)
+      entry.patscan(options[:reverse].to_s,
+                  max_mismatches: options[:mismatches],
+                  max_insertions: options[:insertions],
+                  max_deletions:  options[:deletions]) do |match|
+        record[:REVERSE_POS] = match.pos
+        record[:REVERSE_LEN] = match.length
+        pos = 0
+        len = match.pos
+
+        if len == 0
+          entry.seq  = ""
+          entry.qual = "" if entry.qual
+        else
+          entry = entry[pos ... pos + len]
+        end 
+
+        reverse = true
+        break
       end
 
-      if pos > 0
-        record.merge!(seq.to_bp)
+      if forward or reverse
+        record.merge!(entry.to_bp)
       end
 
       output.puts record