class Bio::Tree

require “/home-user/sswang/program/ruby-2.5.0/lib/ruby/gems/2.5.0/gems/bio-2.0.0/lib/bio/tree.rb”

Constants

DELIMITER

Attributes

index2tip[RW]
tip2index[RW]

Public Instance Methods

allSubtreeRepresentatives() click to toggle source
# File lib/bio/nwk.rb, line 64
def allSubtreeRepresentatives
  arr = Array.new
  nodes.each do |node|
    arr << tips(node)
  end
  return(arr)
end
allTips() click to toggle source
# File lib/bio/nwk.rb, line 72
def allTips
  a = Array.new
  nodes.each do |node|
    a << node if node.isTip?(self)
  end
  return(a)
end
cleanName!() click to toggle source
# File lib/bio/nwk.rb, line 20
def cleanName!
  allTips.each do |tip|
    tip.name.gsub!(' ', '_')
  end
end
cleanNewick() click to toggle source
# File lib/bio/nwk.rb, line 91
def cleanNewick
  output = output_newick
  output.gsub!(/[\n\s]/, '')
  output.gsub!(/[']/, '')
  return(output)
end
getAlldistances() click to toggle source
# File lib/bio/nwk.rb, line 123
def getAlldistances()
  distances = Array.new
  each_edge do |node0, node1, edge|
    next if node0 == root or node1 == root
    distances << edge.distance
  end
  return(distances)
end
internal_nodes() click to toggle source
# File lib/bio/nwk.rb, line 54
def internal_nodes
  a = Array.new
  nodes.each do |node|
    if not node.isTip?(self)
      a << node
    end
  end
  return(a)
end
newickIndexToTip() click to toggle source
# File lib/bio/nwk.rb, line 115
def newickIndexToTip()
  tipToIndex if @index2tip.empty?
  allTips.each do |tip|
    index = tip.name
    tip.name = @index2tip[index]
  end
end
newickTipToIndex() click to toggle source
# File lib/bio/nwk.rb, line 107
def newickTipToIndex()
  tipToIndex
  allTips.each do |tip|
    index = @tip2index[tip.name.gsub(' ', '_')]
    tip.name = (index).to_s
  end
end
normalizeBranchLength!() click to toggle source
# File lib/bio/nwk.rb, line 132
def normalizeBranchLength!()
  min, max = getAlldistances().minmax
  each_edge do |node0, node1, edge|
    edge.distance = (edge.distance-min+1e-10).to_f/(max-min)
  end
end
normalizeBranchLengthGainAndLoss!() click to toggle source
# File lib/bio/nwk.rb, line 139
def normalizeBranchLengthGainAndLoss!()
  min, max = getAlldistances().select{|i|i>=0}.minmax
  each_edge do |node0, node1, edge|
    edge.distance = (edge.distance-min+1e-10).to_f/(max-min) + 1 if edge.distance >= 0
  end

  min, max = getAlldistances().select{|i|i<0}.minmax
  each_edge do |node0, node1, edge|
    edge.distance = (edge.distance-min+1e-10).to_f/(max-min) if edge.distance < 0
  end
end
outputNexus(isTranslate=false, isTip2Index=false) click to toggle source
# File lib/bio/nwk.rb, line 80
def outputNexus(isTranslate=false, isTip2Index=false)
  puts "#NEXUS"
  puts
  puts "BEGIN TREES;"
  translate if isTranslate
  tipToIndex and newickTipToIndex if isTip2Index
  puts ["\t"+'TREE TREE1', cleanNewick()].join('= ')
  puts ['ENDBLOCK', DELIMITER].join('')
  newickIndexToTip if isTip2Index
end
sister(node) click to toggle source
# File lib/bio/nwk.rb, line 50
def sister(node)
  return(sisters(node)[0])
end
sisters(node) click to toggle source
# File lib/bio/nwk.rb, line 42
def sisters(node)
  if node == root
    return([])
  else
    return(children(parent(node)).select{|i|i!=node})
  end
end
tipToIndex() click to toggle source
# File lib/bio/nwk.rb, line 98
def tipToIndex
  @tip2index = Hash.new
  @index2tip = Hash.new
  allTips.each_with_index do |tip, index|
    @tip2index[tip.name.gsub(' ','_')] = (index+1).to_s
    @index2tip[(index+1).to_s] = tip.name
  end
end
tips(node) click to toggle source
# File lib/bio/nwk.rb, line 10
def tips(node)
  rv = Array.new
  if node.isTip?(self)
    rv = [node]
  else
    rv = descendents(node).select{|n|children(n).empty?}
  end
  return(rv)
end
twoTaxaNode(node) click to toggle source
# File lib/bio/nwk.rb, line 26
def twoTaxaNode(node)
  if node.isTip?(self)
    return(node)
  else
    return children(node).map{|child|tips(child).sort_by{|i|i.name}.shift}.sort_by{|i|i.name}
  end
end
twoTaxaNodeName(node) click to toggle source
# File lib/bio/nwk.rb, line 34
def twoTaxaNodeName(node)
  if node.isTip?(self)
    return(node.name)
  else
    return children(node).map{|child|tips(child).sort_by{|i|i.name}.shift}.sort_by{|i|i.name}.map{|i|i.name}
  end
end

Private Instance Methods

translate() click to toggle source
# File lib/bio/nwk.rb, line 152
def translate
  #  TRANSLATE
  #    1      Hamster,
  puts "\tTRANSLATE"
  allTips.each_with_index do |tip, index|
    puts ["\t\t"+(index+1).to_s, tip.name.gsub(' ','_')+','].join("\t")
  end
  puts ["\t\t"+DELIMITER].join("\t")
end