]> git.donarmstrong.com Git - biopieces.git/blobdiff - bp_bin/pcr_seq
refactoring of revcomp in seq.rb
[biopieces.git] / bp_bin / pcr_seq
index ff930cca29e6201b18f273eb57fdb7d285859b3d..57eb447b3684d51f7346bb6a111c2c386937b776 100755 (executable)
 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 
 
-require 'biopieces'
-require 'fasta'
-require 'seq'
-require 'pp'
+require 'maasha/biopieces'
+require 'maasha/fasta'
+require 'maasha/seq'
 
 class Pcr
   def initialize(tmpdir, infile, options)
@@ -51,7 +50,8 @@ class Pcr
       outfile = pat_file.sub("pat", "fna")
 
       command =  "scan_for_matches"
-      command << " -c"
+      # command << " -c"
+      command << " -o 1"
       command << " #{pat_file}"
       command << " < #{@infile}"
       command << " > #{outfile}"
@@ -146,50 +146,65 @@ class Pattern
     seq      = Seq.new
     seq.seq  = primer
     seq.type = 'dna'
-    seq.revcomp
+    seq.reverse!.complement!
 
     descriptor ? seq.seq + descriptor : seq.seq
   end
 end
 
 casts = []
-casts << {:long=>'forward',  :short=>'f', :type=>'string', :mandatory=>true, :default=>nil,  :allowed=>nil, :disallowed=>nil}
-casts << {:long=>'reverse',  :short=>'r', :type=>'string', :mandatory=>true, :default=>nil,  :allowed=>nil, :disallowed=>nil}
-casts << {:long=>'max_dist', :short=>'m', :type=>'uint',   :mandatory=>true, :default=>5000, :allowed=>nil, :disallowed=>"0"}
-
-bp = Biopieces.new
-
-options = bp.parse(ARGV, casts)
-tmpdir  = bp.mktmpdir
+casts << {:long=>'forward',    :short=>'f', :type=>'string', :mandatory=>false, :default=>nil,  :allowed=>nil, :disallowed=>nil}
+casts << {:long=>'forward_rc', :short=>'F', :type=>'string', :mandatory=>false, :default=>nil,  :allowed=>nil, :disallowed=>nil}
+casts << {:long=>'reverse',    :short=>'r', :type=>'string', :mandatory=>false, :default=>nil,  :allowed=>nil, :disallowed=>nil}
+casts << {:long=>'reverse_rc', :short=>'R', :type=>'string', :mandatory=>false, :default=>nil,  :allowed=>nil, :disallowed=>nil}
+casts << {:long=>'max_dist',   :short=>'m', :type=>'uint',   :mandatory=>true, :default=>5000, :allowed=>nil, :disallowed=>"0"}
+
+options = Biopieces.options_parse(ARGV, casts)
+tmpdir  = Biopieces.mktmpdir
 infile  = File.join(tmpdir, "in.fna")
 
-Fasta.open(infile, mode="w") do |ios|
-  bp.each_record do |record|
-    bp.puts record
-    ios.puts record
-  end
+if options[:forward_rc]
+  options[:forward] = Seq.new("test", options[:forward_rc], 'dna').reverse.complement.seq
+end
+
+if options[:reverse_rc]
+  options[:reverse] = Seq.new("test", options[:reverse_rc], 'dna').reverse.complement.seq
 end
 
-outfiles = Pcr.new(tmpdir, infile, options).run
-
-outfiles.each do |outfile|
-  Fasta.open(outfile, mode="r") do |ios|
-    ios.each do |entry|
-      record = entry.to_bp
-      record[:REC_TYPE] = "PCR"
-      record[:STRAND]   = "+"
-      record[:TYPE]     = File.basename(outfile).sub(".fna", "").upcase
-      record[:SEQ_NAME].match(/(.+):\[(\d+),(\d+)\]$/)
-      record[:SEQ_NAME] = $1
-      record[:PCR_BEG]  = $2
-      record[:PCR_END]  = $3
-
-      if record[:PCR_BEG] > record[:PCR_END]
-        record[:PCR_BEG], record[:PCR_END] = record[:PCR_END], record[:PCR_BEG]
-        record[:STRAND] = "-"
+raise ArgumentError, "no adaptor specified" unless options[:forward] or options[:reverse]
+Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
+  Fasta.open(infile, mode="w") do |ios|
+    input.each_record do |record|
+      output.puts record
+
+      if record.has_key? :SEQ
+        entry = Seq.new_bp(record)
+        ios.puts entry.to_fasta
       end
+    end
+  end
 
-      bp.puts record
+  outfiles = Pcr.new(tmpdir, infile, options).run
+
+  outfiles.each do |outfile|
+    Fasta.open(outfile, mode="r") do |ios|
+      ios.each do |entry|
+        record = entry.to_bp
+        record[:REC_TYPE] = "PCR"
+        record[:STRAND]   = "+"
+        record[:TYPE]     = File.basename(outfile).sub(".fna", "").upcase
+        record[:SEQ_NAME].match(/(.+):\[(\d+),(\d+)\]$/)
+        record[:SEQ_NAME] = $1
+        record[:PCR_BEG]  = $2.to_i
+        record[:PCR_END]  = $3.to_i
+
+        if record[:PCR_BEG] > record[:PCR_END]
+          record[:PCR_BEG], record[:PCR_END] = record[:PCR_END], record[:PCR_BEG]
+          record[:STRAND] = "-"
+        end
+
+        output.puts record
+      end
     end
   end
 end