Class: TreeClusters::Attrs

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

Overview

A Hash table for genome/leaf/taxa attributes

Instance Method Summary collapse

Instance Method Details

#add(leaf, attr, val) ⇒ Object



251
252
253
254
255
256
257
# File 'lib/tree_clusters.rb', line 251

def add leaf, attr, val
  if self.has_key? leaf
    self[leaf][attr] = val
  else
    self[leaf] = { attr => val }
  end
end

#attrs(leaves, attr) ⇒ AttrArray<Set>

Note:

If a genome is in the leaves array, but is not in the hash table, NO error will be raised. Rather that genome will be skipped. This is for cases in which not all genomes have attributes.

Returns the an AttrArray of Sets for the given genomes and attribute.

Parameters:

  • leaves (Array<String>)

    names of the leaves for which you need attributes

  • attr (Symbol)

    the attribute you are interested in eg, :genes

Returns:

  • (AttrArray<Set>)

    an AttrArray of Sets of attributes

Raises:

  • (AbortIf::Exit)

    if they leaf is present but doesn’t have the requested attr



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/tree_clusters.rb', line 235

def attrs leaves, attr
  ary = leaves.map do |leaf|

    if self.has_key? leaf
      abort_unless self[leaf].has_key?(attr),
                   "Missing attr #{attr.inspect} for leaf '#{leaf}'"

      self[leaf][attr]
    else
      nil
    end
  end.compact

  TreeClusters::AttrArray.new ary
end