Module: ObservationMatricesHelper

Defined in:
app/helpers/observation_matrices_helper.rb

Constant Summary collapse

LABEL_REPLACEMENT =
{
  '10' => 'A',
  '11' => 'B',
  '12' => 'C',
  '13' => 'D',
  '14' => 'E',
  '15' => 'F',
  '16' => 'G',
  '17' => 'H',
  '18' => 'I',
  '19' => 'J',
  '20' => 'K',
  '21' => 'L',
  '22' => 'M',
  '23' => 'N',
  '24' => 'O',
  '25' => 'P',
  '26' => 'R',
  '27' => 'S',
  '28' => 'T',
  '29' => 'U',
  '30' => 'V',
  '31' => 'W',
  '32' => 'X',
  '33' => 'Y',
  '34' => 'Z',
}.freeze

Instance Method Summary collapse

Instance Method Details

#descriptor_list(observation_matrix) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'app/helpers/observation_matrices_helper.rb', line 136

def descriptor_list(observation_matrix)
  rows = []
  observation_matrix.descriptors.each do |d|
    l = d.name + ': '
    if d.qualitative?
      l << d.character_states.order(:position).collect{|cs| "#{cs.label}: " + cs.name }.join('; ')
    elsif d.presence_absence?
      l << '0: absent; 1: present'
    end
    rows.push l
  end
  rows.join("\n")
end

#keywords_on_addable_column_itemsObject



53
54
55
# File 'app/helpers/observation_matrices_helper.rb', line 53

def keywords_on_addable_column_items
  Keyword.joins(:tags).where(project_id: sessions_current_project_id).where(tags: {tag_object_type: 'Descriptor'}).distinct.all
end

#keywords_on_addable_row_itemsObject



49
50
51
# File 'app/helpers/observation_matrices_helper.rb', line 49

def keywords_on_addable_row_items
  Keyword.joins(:tags).where(project_id: sessions_current_project_id).where(tags: {tag_object_type: ['Otu', 'CollectionObject']}).distinct.all
end

#max_row_name_width(observation_matrix) ⇒ Object

TODO: This is only used in TNT exports, expand to allow for other export formats



61
62
63
64
65
66
67
68
69
# File 'app/helpers/observation_matrices_helper.rb', line 61

def max_row_name_width(observation_matrix)
  max = 0

  observation_matrix.observation_matrix_rows.load.each do |r|
    s = observation_matrix_row_label_tnt(r).length
    max = s if max < s
  end
  max + 1
end

#nexus_symbol_list(descriptors) ⇒ String

Returns the list of symbols actually used as character state labels, like 0 1 2 3 4 5 6 7 8 9 A.

Returns:

  • (String)

    the list of symbols actually used as character state labels, like 0 1 2 3 4 5 6 7 8 9 A



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'app/helpers/observation_matrices_helper.rb', line 153

def nexus_symbol_list(descriptors)
  max = 1
  descriptors.each do |d|
    case d.type
    when 'Descriptor::Qualitative'
      # Assumes non-gap character state labels are sequential starting at 0.
      size = d.has_gap_state? ?
        d.character_states.size - 1 : d.character_states.size
      max = size if size > max
    when 'Descriptor::PresenceAbsence'
      max = 2 if max < 2
    end
  end

  (0..max - 1).to_a
    .map{|i| i > 9 ? LABEL_REPLACEMENT[i.to_s] : i.to_s}.compact.join(' ')
end

#observation_export_value(observation) ⇒ String

Returns the value shown in the cell.

Returns:

  • (String)

    the value shown in the cell



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/helpers/observation_matrices_helper.rb', line 106

def observation_export_value(observation)
  case observation.type
  when 'Observation::Qualitative'
    LABEL_REPLACEMENT[observation.character_state.label].nil? ? observation.character_state.label : LABEL_REPLACEMENT[observation.character_state.label]
  when 'Observation::PresenceAbsence'
    case observation.presence
    when true
      '1'
    when false
      '0'
    when nil
      '?'
    else
      'INTERNAL ERROR'
    end
  when 'Observation::Continuous'
    observation.converted_value.to_s
  when 'Observation::Sample'
    if observation.sample_max && observation.sample_max && observation.sample_max.to_f != observation.sample_min.to_f
      ('%g' % observation.sample_min).to_s + '-' + ('%g' % observation.sample_max).to_s
    elsif observation.sample_min
      ('%g' % observation.sample_min).to_s
    else
      '?'
    end
  else
    '-' # ? not sure
  end
end

#observation_matrices_search_formObject



40
41
42
# File 'app/helpers/observation_matrices_helper.rb', line 40

def observation_matrices_search_form
  render('/observation_matrices/quick_search_form')
end

#observation_matrix_label(observation_matrix) ⇒ Object



35
36
37
38
# File 'app/helpers/observation_matrices_helper.rb', line 35

def observation_matrix_label(observation_matrix)
  return nil if observation_matrix.nil?
  observation_matrix.name
end


44
45
46
47
# File 'app/helpers/observation_matrices_helper.rb', line 44

def observation_matrix_link(observation_matrix)
  return nil if observation_matrix.nil?
  link_to(observation_matrix_tag(observation_matrix).html_safe, observation_matrix)
end

#observation_matrix_tag(observation_matrix) ⇒ Object



30
31
32
33
# File 'app/helpers/observation_matrices_helper.rb', line 30

def observation_matrix_tag(observation_matrix)
  return nil if observation_matrix.nil?
  observation_matrix.name
end

#observations_cell_label(observations_hash, descriptor, hash_index, style = :tnt) ⇒ String

Returns the fully formatted cell, handles polymorphisms show states in tnt or nexus format for a ‘cell’ (e.g. [ab]) Mx.print_codings in mx.

Returns:

  • (String)

    the fully formatted cell, handles polymorphisms show states in tnt or nexus format for a ‘cell’ (e.g. [ab]) Mx.print_codings in mx



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
# File 'app/helpers/observation_matrices_helper.rb', line 75

def observations_cell_label(observations_hash, descriptor, hash_index, style = :tnt)

  case observations_hash[descriptor.id][hash_index].size
  when 0
    '?'
  when 1
    o = observations_hash[descriptor.id][hash_index][0]
    s = observation_export_value(o)
    s = LABEL_REPLACEMENT[s].nil? ? s : LABEL_REPLACEMENT[s]

    if s.length > 1 && (style == :nexus || style == :tnt) && o.type == 'Observation::Qualitative'
      "#{s} [WARNING STATE '#{s}' is TOO LARGE FOR PAUP (0-9, A-Z only).]"
    else
      s
    end
  else
    str = observations_hash[descriptor.id][hash_index].collect{|o| observation_export_value(o) }.sort

    case style
    when :csv
      str.join('|')
    when :nexus
      "{#{str.join("")}}"
    else
      "[#{str.join("")}]"
    end
  end
end