]> git.donarmstrong.com Git - biopieces.git/blobdiff - bp_bin/classify_taxonomy
refactoring of assemble_pairs
[biopieces.git] / bp_bin / classify_taxonomy
index 958ecb94be360c0ca9c8d363db7968b4b4057775..64303fb6b582432ae052b2ceebb36684c3443f84 100755 (executable)
@@ -49,7 +49,7 @@ class TaxNode
   def add_gg(s_id, score, size)
     node = self
 
-    s_id.scan(/ ([\w])__([^;]+)/) do
+    s_id.scan(/([\w])__([^;]+)/) do
       level = expand_level($1)
       name  = $2
 
@@ -68,8 +68,8 @@ class TaxNode
   def merge(node_new, node_old = self)
     node_new.children.each do |name, child|
       if node_old.children[name]
-        node_old.count += child.count
-        node_old.score += child.score
+        node_old.children[name].count += child.count
+        node_old.children[name].score += child.score
 
         merge(child, node_old.children[name])
       else
@@ -89,13 +89,29 @@ class TaxNode
     list
   end
 
+  # Method to recursively remove branches in taxonomic tree where the child count is less than or
+  # equal to a given minimum.
+  def debranch(min_count = nil)
+    node = self
+
+    node.children.each do |name, child|
+      node.children.delete(name) if child.count <= min_count
+    end
+
+    node.children.each_value do |child|
+      child.debranch(min_count)
+    end
+  end
+
   # Method to recursively trim a taxonomic tree so that it only contains an unbranched tree,
   # which gives the lowest common ancestor.
-  def lowest_common_ancestor(node = self)
+  def lowest_common_ancestor
+    node = self
+
     node.children = {} if node.children.size > 1
 
-    node.children.each do |name, child|
-      lowest_common_ancestor(child)
+    node.children.each_value do |child|
+      child.lowest_common_ancestor
     end
   end
 
@@ -142,8 +158,9 @@ class TaxNode
 end
 
 casts = []
-casts << {:long=>'LCA',  :short=>'l', :type=>'flag', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
-casts << {:long=>'size', :short=>'s', :type=>'flag', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
+casts << {:long=>'LCA',       :short=>'l', :type=>'flag', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
+casts << {:long=>'size',      :short=>'s', :type=>'flag', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
+casts << {:long=>'min_count', :short=>'m', :type=>'uint', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
 
 options = Biopieces.options_parse(ARGV, casts)
 
@@ -176,6 +193,7 @@ Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
 
   if options[:LCA]
     tax_hash.each_value do |tree|
+      tree.debranch(options[:min_count]) if options[:min_count]
       tree.lowest_common_ancestor
     end