]> git.donarmstrong.com Git - biopieces.git/commitdiff
added debranch function to classify_taxonomy
authormartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Thu, 25 Oct 2012 11:42:00 +0000 (11:42 +0000)
committermartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Thu, 25 Oct 2012 11:42:00 +0000 (11:42 +0000)
git-svn-id: http://biopieces.googlecode.com/svn/trunk@1972 74ccb610-7750-0410-82ae-013aeee3265d

bp_bin/classify_taxonomy

index a402e93c2a319a3226ce26c0aa6ea7fe5f12e098..64303fb6b582432ae052b2ceebb36684c3443f84 100755 (executable)
@@ -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