Class: NewickString

Inherits:
Object
  • Object
show all
Defined in:
lib/rnewick.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ NewickString

Returns a new instance of NewickString.



12
13
14
# File 'lib/rnewick.rb', line 12

def initialize(str)
  @str=String.new(str)
end

Instance Attribute Details

#strObject (readonly)

Returns the value of attribute str.



11
12
13
# File 'lib/rnewick.rb', line 11

def str
  @str
end

Instance Method Details

#branch_lengthsObject

TODO find the multifurcations and show their support (guess it is 100 cause bifurcations were lower than 50) maybe try to resolve them on the fly?



22
23
24
# File 'lib/rnewick.rb', line 22

def branch_lengths
  @str.gsub("0.0;","").scan(/:([0-9]+\.[0-9]+)/).map{|n| n[0].to_f}
end

#cleanObject



51
52
53
54
55
# File 'lib/rnewick.rb', line 51

def clean
  newstr = @str.gsub('(,)',"").gsub('(,(','((').gsub('),)','))')
  newstr = newstr.gsub('(,)',"").gsub('(,(','((').gsub('),)','))')
  newstr.gsub(',)',")").gsub('(,','(').gsub("e-06","")
end

#multifurcationsObject



18
19
20
21
# File 'lib/rnewick.rb', line 18

def multifurcations
  # TODO find the multifurcations and show their support (guess it is 100 cause bifurcations were lower than 50)
  # maybe try to resolve them on the fly?
end

#numtaxaObject



48
49
50
# File 'lib/rnewick.rb', line 48

def numtaxa
  @str.count(",").to_i + 1
end

#rawtopologyObject



42
43
44
# File 'lib/rnewick.rb', line 42

def rawtopology
  @str.gsub(/[0-9]+\.[0-9]+/,'').gsub(':','')
end

#str_with_numeric_branch_lengthsObject



32
33
34
35
36
37
38
# File 'lib/rnewick.rb', line 32

def str_with_numeric_branch_lengths
  newt = @str.gsub(/:([0-9]+\.[0-9]+)e-([0-9]+)/) do |bl|
    newval = bl.gsub(":","").to_f  
    ":" + "%.9f" % newval 
  end
  newt
end

#str_with_scaled_branch_lengths(factor) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/rnewick.rb', line 25

def str_with_scaled_branch_lengths(factor)
  newt = @str.gsub(/:([0-9]+\.[0-9]+)/) do |bl|
    newval = bl.gsub(":","").to_f * factor
    ":" + "%.9f" % newval 
  end
  newt
end

#support_valuesObject



15
16
17
# File 'lib/rnewick.rb', line 15

def support_values
  @str.gsub("0.0;","").scan(/\[([0-9]+)\]/).map{|n| n[0].to_i}
end

#taxanamesObject



45
46
47
# File 'lib/rnewick.rb', line 45

def taxanames
  self.rawtopology.gsub("(", " ").gsub(")", " ").gsub(",", " ").gsub(";", " ").strip.split
end

#treelengthObject



39
40
41
# File 'lib/rnewick.rb', line 39

def treelength
  self.branch_lengths.inject(0){|acc,i| acc+i}
end