]> git.donarmstrong.com Git - biopieces.git/blobdiff - bp_bin/pcr_seq
adding bzip2 support in ruby
[biopieces.git] / bp_bin / pcr_seq
index f54577373e75f7f32f24752c3a54ff31eaedafa8..6afbb343f3e497595261f4cd785a558ff0ce97b9 100755 (executable)
@@ -51,6 +51,7 @@ class Pcr
 
       command =  "scan_for_matches"
       # command << " -c"
+      command << " -o 1"
       command << " #{pat_file}"
       command << " < #{@infile}"
       command << " > #{outfile}"
@@ -68,6 +69,25 @@ end
 class Pattern
   attr_accessor :forward, :reverse
 
+  # Split a primer pattern in the form of ATCG[3,2,1] into
+  # sequence and match descriptor, reverse complement the 
+  # primer and append the match descriptor: CGAT[3,2,1].
+  def self.revcomp(pattern)
+    if pattern.match(/^(\w+)(\[.+\])?/)
+      primer     = $1
+      descriptor = $2
+    else
+      raise "Failed splitting pattern: #{pattern}"
+    end
+
+    seq      = Seq.new
+    seq.seq  = primer
+    seq.type = :dna
+    seq.reverse!.complement!
+
+    descriptor ? seq.seq + descriptor : seq.seq
+  end
+
   def initialize(forward, reverse, max_dist)
     @forward  = forward
     @reverse  = reverse
@@ -85,8 +105,8 @@ class Pattern
   def save(tmpdir)
     forward = @forward
     reverse = @reverse
-    revcomp_forward = revcomp(forward)
-    revcomp_reverse = revcomp(reverse)
+    revcomp_forward = Pattern.revcomp(forward)
+    revcomp_reverse = Pattern.revcomp(reverse)
 
     files = []
 
@@ -126,46 +146,38 @@ class Pattern
 
   # Save a pattern to file
   def save_pattern(file)
-    File.open(file, mode="w") do |ios|
+    File.open(file, "w") do |ios|
       ios.puts self
     end
   end
-
-  # Split a primer pattern in the form of ATCG[3,2,1] into
-  # sequence and match descriptor, reverse complement the 
-  # primer and append the match descriptor: CGAT[3,2,1].
-  def revcomp(pattern)
-    if pattern.match(/^(\w+)(\[.+\])?/)
-      primer     = $1
-      descriptor = $2
-    else
-      raise "Failed splitting pattern: #{pattern}"
-    end
-
-    seq      = Seq.new
-    seq.seq  = primer
-    seq.type = 'dna'
-    seq.revcomp
-
-    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"}
+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")
 
+if options[:forward_rc]
+  options[:forward] = Pattern.revcomp(options[:forward_rc])
+end
+
+if options[:reverse_rc]
+  options[:reverse] = Pattern.revcomp(options[:reverse_rc])
+end
+
+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|
+  Fasta.open(infile, "w") do |ios|
     input.each_record do |record|
       output.puts record
 
-      if record.has_key? :SEQ
+      if record[:SEQ]
         entry = Seq.new_bp(record)
         ios.puts entry.to_fasta
       end
@@ -175,7 +187,7 @@ Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
   outfiles = Pcr.new(tmpdir, infile, options).run
 
   outfiles.each do |outfile|
-    Fasta.open(outfile, mode="r") do |ios|
+    Fasta.open(outfile, "r") do |ios|
       ios.each do |entry|
         record = entry.to_bp
         record[:REC_TYPE] = "PCR"
@@ -183,8 +195,8 @@ Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
         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
+        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]