From: martinahansen Date: Thu, 25 Oct 2012 11:42:00 +0000 (+0000) Subject: added debranch function to classify_taxonomy X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=93b9073bcec08eccc48231c187266b8ec716c678;p=biopieces.git added debranch function to classify_taxonomy git-svn-id: http://biopieces.googlecode.com/svn/trunk@1972 74ccb610-7750-0410-82ae-013aeee3265d --- diff --git a/bp_bin/classify_taxonomy b/bp_bin/classify_taxonomy index a402e93..64303fb 100755 --- a/bp_bin/classify_taxonomy +++ b/bp_bin/classify_taxonomy @@ -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