Class: Iroki::Tree

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

Class Method Summary collapse

Class Method Details

.change_names(tree) ⇒ Hash

TODO:

not a good name as it doesn’t actually change the names in the tree

Note:

The newick standard changes unquoted underscores to spaces

Note:

The reason we have to bother with this is because when the bioruby parser calls the __to_newick method, it does some annoying things to the output and changes the names. Making the names like this lets us easily gsub the names to what they should be after the name map.

Returns iroki_name (string) => quoted_orig_name (string).

Parameters:

Returns:

  • (Hash)

    iroki_name (string) => quoted_orig_name (string)



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/iroki/tree.rb', line 21

def self.change_names tree
  idx = -1
  realname = {}
  tree.each_node do |node|
    if node.name && !node.name.empty?
      idx += 1

      realname[iroki_name(idx)] = %Q{#{node.name}}

      node.name = iroki_name(idx)
    end
  end

  realname
end

.combine_hashes(h1, h2, nil_val = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/iroki/tree.rb', line 37

def self.combine_hashes h1, h2, nil_val=nil
  h_new = {}
  h1.each do |h1_key, h1_val|
    if h2.has_key? h1_val
      h_new[h1_key] = h2[h1_val]
    else
      h_new[h1_key] = nil_val
    end
  end

  h_new
end

.gsub_iroki_newick_string(tre_str, iroki_to_name, name_map = nil) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/iroki/tree.rb', line 56

def self.gsub_iroki_newick_string tre_str,
                                  iroki_to_name,
                                  name_map=nil
  if name_map
    vals = self.quoted_vals name_map
    name_map_quoted = Hash[name_map.keys.zip(vals)]

    iroki_to_new_name =
      self.combine_hashes iroki_to_name, name_map_quoted
  else
    vals = self.quoted_vals iroki_to_name

    iroki_to_new_name = Hash[iroki_to_name.keys.zip(vals)]
  end

  tre_str.gsub(/iroki[0-9]+iroki/, iroki_to_new_name)
end

.iroki_name(idx) ⇒ Object



3
4
5
# File 'lib/iroki/tree.rb', line 3

def self.iroki_name idx
  "iroki#{idx}iroki"
end

.iroki_to_color(iroki_to_name, color_map, name_map, nil_val = nil) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/iroki/tree.rb', line 74

def self.iroki_to_color iroki_to_name,
                        color_map,
                        name_map,
                        nil_val=nil
  if name_map
    old_names = name_map.keys
    new_names = name_map.values

    color_map_is_for_old_names =
      color_map.keys.all? { |key| old_names.include? key }

    color_map_is_for_new_names =
      color_map.keys.all? { |key| new_names.include? key }

    if color_map_is_for_old_names
      iroki_to_color =
        self.combine_hashes iroki_to_name, color_map, nil_val
    elsif color_map_is_for_new_names
      iroki_to_new_name =
        self.combine_hashes iroki_to_name, name_map

      iroki_to_color =
        self.combine_hashes iroki_to_new_name, color_map, nil_val
    else # some old, some new
      abort_if true,
               "The color map has both old and new names in " +
               "the first column."
    end

    iroki_to_color
  else # no name map
    self.combine_hashes iroki_to_name, color_map, nil_val
  end
end

.quoted_vals(hash) ⇒ Object



50
51
52
53
54
# File 'lib/iroki/tree.rb', line 50

def self.quoted_vals hash
  # p [:a, hash.values]
  # p [:b, hash.values.map(&:single_quote)]
  hash.values.map(&:single_quote)
end