Sha256: 84ac73757f5cc9751a5572774d4e734d65deabd3cfcc3baf50c0cf136fe930f5

Contents?: true

Size: 1.25 KB

Versions: 5

Compression:

Stored size: 1.25 KB

Contents

module TreeClusters
  # A Hash table for genome/leaf/taxa attributes
  class Attrs < Hash

    # Returns the an AttrArray of Sets for the given genomes and
    # attribute.
    #
    # @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.
    #
    # @param leaves [Array<String>] names of the leaves for which you
    #   need attributes
    # @param attr [Symbol] the attribute you are interested in eg,
    #   :genes
    #
    # @return [AttrArray<Set>] an AttrArray of Sets of
    #   attributes
    #
    # @raise [AbortIf::Exit] if they leaf is present but doesn't have
    #   the requested attr
    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

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
tree_clusters-0.8.3 lib/tree_clusters/attrs.rb
tree_clusters-0.8.2 lib/tree_clusters/attrs.rb
tree_clusters-0.8.1 lib/tree_clusters/attrs.rb
tree_clusters-0.8.0 lib/tree_clusters/attrs.rb
tree_clusters-0.7.0 lib/tree_clusters/attrs.rb