Class: Dphil::CharacterMatrix
- Inherits:
-
Object
- Object
- Dphil::CharacterMatrix
- Includes:
- LDOutput
- Defined in:
- lib/dphil/character_matrix.rb
Overview
A matrix of character states across taxa.
Instance Attribute Summary collapse
-
#characters ⇒ Hash<Integer, Character>
readonly
Characters by character ID.
-
#stats ⇒ Hash
readonly
The character statistics for the matrix.
-
#taxa_ids ⇒ Hash<String, Integer>
readonly
Taxa IDs by names.
-
#taxa_names ⇒ Hash<Integer, String>
readonly
Taxa names by ID.
Class Method Summary collapse
-
.from_csv(infile, transpose: false) ⇒ CharacterMatrix
Instantiate a new CharacterMatrix from a UTF-8 CSV file.
Instance Method Summary collapse
- #as_json(options = nil) ⇒ Object
-
#get_character(char_id) ⇒ Character?
Get character by ID.
-
#initialize(table) ⇒ CharacterMatrix
constructor
Instantiate a new CharacterMatrix.
- #to_h ⇒ Object
Methods included from LDOutput
Constructor Details
#initialize(table) ⇒ CharacterMatrix
Instantiate a new CharacterMatrix
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/dphil/character_matrix.rb', line 22 def initialize(table) @taxa_names = table.to_a.first.each_with_object({}) .with_index do |(name, acc), index| acc[index + 1] = normalize_text(name) end @taxa_ids = @taxa_names.invert taxa_arr = @taxa_ids.values @characters = (1...table.length).each_with_object({}) do |char_num, acc| char_states = taxa_arr.zip(table[char_num]).to_h acc[char_num] = Dphil::Character.new(id: char_num, states: char_states) end instance_variables.each { |ivar| instance_variable_get(ivar).freeze } end |
Instance Attribute Details
#characters ⇒ Hash<Integer, Character> (readonly)
Returns characters by character ID.
48 49 50 |
# File 'lib/dphil/character_matrix.rb', line 48 def characters @characters end |
#stats ⇒ Hash (readonly)
Returns the character statistics for the matrix.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/dphil/character_matrix.rb', line 52 def stats @stats ||= begin hash = { total: characters.count, constant: 0, uninformative: 0, informative: 0, } characters.each_value do |char| if char.constant? hash[:constant] += 1 elsif char.informative? hash[:informative] += 1 else hash[:uninformative] += 1 end end hash end.freeze end |
#taxa_ids ⇒ Hash<String, Integer> (readonly)
Returns taxa IDs by names.
44 45 46 |
# File 'lib/dphil/character_matrix.rb', line 44 def taxa_ids @taxa_ids end |
#taxa_names ⇒ Hash<Integer, String> (readonly)
Returns taxa names by ID.
40 41 42 |
# File 'lib/dphil/character_matrix.rb', line 40 def taxa_names @taxa_names end |
Class Method Details
.from_csv(infile, transpose: false) ⇒ CharacterMatrix
Instantiate a new CharacterMatrix from a UTF-8 CSV file
14 15 16 17 18 |
# File 'lib/dphil/character_matrix.rb', line 14 def self.from_csv(infile, transpose: false) csv = CSV.read(infile, "r:bom|utf-8") csv = csv.transpose if transpose new(csv) end |
Instance Method Details
#as_json(options = nil) ⇒ Object
87 88 89 |
# File 'lib/dphil/character_matrix.rb', line 87 def as_json( = nil) to_h.as_json() end |
#get_character(char_id) ⇒ Character?
Get character by ID
76 77 78 |
# File 'lib/dphil/character_matrix.rb', line 76 def get_character(char_id) characters[char_id.to_i] end |
#to_h ⇒ Object
80 81 82 83 84 85 |
# File 'lib/dphil/character_matrix.rb', line 80 def to_h { taxa_names: taxa_names, characters: characters, } end |