]> git.donarmstrong.com Git - biopieces.git/commitdiff
changed Align.muscle to file based instead of popen3
authormartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Fri, 20 Jan 2012 14:09:51 +0000 (14:09 +0000)
committermartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Fri, 20 Jan 2012 14:09:51 +0000 (14:09 +0000)
git-svn-id: http://biopieces.googlecode.com/svn/trunk@1729 74ccb610-7750-0410-82ae-013aeee3265d

code_ruby/lib/maasha/align.rb

index 0a56dbcd0eaebfac62e834e72c3d99db646d4ffc..16e0d636e2092a086ef24553d23aafa5e4ca568e 100755 (executable)
@@ -23,7 +23,7 @@
 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 
 require 'pp'
-require 'open3'
+require 'tempfile'
 require 'narray'
 require 'maasha/fasta'
 
@@ -79,35 +79,37 @@ ROW_T = 1
 ROW_C = 2
 ROW_G = 3
 
-# Adding an initialize method to the existing Fasta class.
-class Fasta
-  def initialize(io)
-    @io = io
-  end
-end
-
 # Class for aligning sequences.
 class Align
   # Class method to align sequences in a list of Seq objects and
   # return these as a new list of Seq objects.
-  def self.muscle(entries)
-    result = []
-    index  = {}
+  def self.muscle(entries, verbose = false)
+    file_in  = Tempfile.new("input")
+    file_out = Tempfile.new("output")
+    result   = []
+    index    = {}
 
-    Open3.popen3("muscle") do |stdin, stdout, stderr|
+    Fasta.open(file_in, "w") do |ios|
       entries.each do |entry|
         raise AlignError, "Duplicate sequence name: #{entry.seq_name}" if index.has_key? entry.seq_name
 
         index[entry.seq_name] = entry
 
-        stdin.puts entry.to_fasta
+        ios.puts entry.to_fasta
       end
+    end
 
-      stdin.close
+    if verbose
+      cmd = "muscle < #{file_in.path} > #{file_out.path}"
+    else
+      cmd = "muscle -quiet < #{file_in.path} > #{file_out.path}"
+    end
 
-      aligned_entries = Fasta.new(stdout)
+    system(cmd)
+    raise AlignError, "command failed: #{cmd}" unless $?.success?
 
-      aligned_entries.each do |fa_entry|
+    Fasta.open(file_out, "r") do |ios|
+      ios.each do |fa_entry|
         fq_entry = index[fa_entry.seq_name]
 
         fa_entry.seq.scan(/-+/) do |m|