Class: Dphil::CharacterMatrix

Inherits:
Object
  • Object
show all
Includes:
LDOutput
Defined in:
lib/dphil/character_matrix.rb

Overview

A matrix of character states across taxa.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LDOutput

#as_jsonld, #to_jsonld

Constructor Details

#initialize(table) ⇒ CharacterMatrix

Instantiate a new CharacterMatrix

Parameters:

  • table (Array<Array<String>>)

    collation table (headers in first row)



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

#charactersHash<Integer, Character> (readonly)

Returns characters by character ID.

Returns:

  • (Hash<Integer, Character>)

    characters by character ID



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

def characters
  @characters
end

#statsHash (readonly)

Returns the character statistics for the matrix.

Returns:

  • (Hash)

    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_idsHash<String, Integer> (readonly)

Returns taxa IDs by names.

Returns:

  • (Hash<String, Integer>)

    taxa IDs by names



44
45
46
# File 'lib/dphil/character_matrix.rb', line 44

def taxa_ids
  @taxa_ids
end

#taxa_namesHash<Integer, String> (readonly)

Returns taxa names by ID.

Returns:

  • (Hash<Integer, String>)

    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

Parameters:

  • infile (#read)

    the file/IO object to read

  • transpose (Boolean) (defaults to: false)

    transpose the table 90° (headers in first column)

Returns:



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(options = nil)
  to_h.as_json(options)
end

#get_character(char_id) ⇒ Character?

Get character by ID

Parameters:

  • char_id (Integer)

    a character ID

Returns:

  • (Character, nil)

    the associated Character, or Nil if not found.



76
77
78
# File 'lib/dphil/character_matrix.rb', line 76

def get_character(char_id)
  characters[char_id.to_i]
end

#to_hObject



80
81
82
83
84
85
# File 'lib/dphil/character_matrix.rb', line 80

def to_h
  {
    taxa_names: taxa_names,
    characters: characters,
  }
end