Module: ObservationMatricesHelper

Defined in:
app/helpers/observation_matrices_helper.rb

Instance Method Summary collapse

Instance Method Details

#descriptor_list(observation_matrix) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/helpers/observation_matrices_helper.rb', line 107

def descriptor_list(observation_matrix)
  rows = []
  observation_matrix.descriptors.order(:position).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



26
27
28
# File 'app/helpers/observation_matrices_helper.rb', line 26

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



22
23
24
# File 'app/helpers/observation_matrices_helper.rb', line 22

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



34
35
36
37
38
39
40
41
42
# File 'app/helpers/observation_matrices_helper.rb', line 34

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

#observation_export_value(observation) ⇒ String

Returns the value shown in the cell.

Returns:

  • (String)

    the value shown in the cell



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
103
104
105
# File 'app/helpers/observation_matrices_helper.rb', line 77

def observation_export_value(observation)
  case observation.type
  when 'Observation::Qualitative'
    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



13
14
15
# File 'app/helpers/observation_matrices_helper.rb', line 13

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

#observation_matrix_label(observation_matrix) ⇒ Object



8
9
10
11
# File 'app/helpers/observation_matrices_helper.rb', line 8

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


17
18
19
20
# File 'app/helpers/observation_matrices_helper.rb', line 17

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



3
4
5
6
# File 'app/helpers/observation_matrices_helper.rb', line 3

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/helpers/observation_matrices_helper.rb', line 48

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)

    if s.length > 1 && style == :nexus && 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