Method: Bio::Tree#remove_nonsense_nodes

Defined in:
lib/bio/tree.rb

#remove_nonsense_nodesObject

Removes all nodes that are not branches nor leaves. That is, removes nodes connected with exactly two edges. For each removed node, two adjacent edges are merged and a new edge are created. Returns removed nodes. Note that orphan nodes are still kept unchanged.



852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
# File 'lib/bio/tree.rb', line 852

def remove_nonsense_nodes
  _clear_cache
  hash = {}
  self.each_node do |node|
    hash[node] = true if @pathway.graph[node].size == 2
  end
  hash.each_key do |node|
    adjs = @pathway.graph[node].keys
    edges = @pathway.graph[node].values
    new_edge = get_edge_merged(edges[0], edges[1])
    @pathway.graph[adjs[0]].delete(node)
    @pathway.graph[adjs[1]].delete(node)
    @pathway.graph.delete(node)
    @pathway.append(Bio::Relation.new(adjs[0], adjs[1], new_edge))
  end
  #@pathway.to_relations
  @pathway.relations.reject! do |rel|
    hash[rel.node[0]] or hash[rel.node[1]]
  end
  return hash.keys
end