Module: ObservationMatrices::Export::OtuContentsHelper

Includes:
Lib::DistributionHelper
Defined in:
app/helpers/observation_matrices/export/otu_contents_helper.rb

Instance Method Summary collapse

Instance Method Details

#content_type_material(t) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'app/helpers/observation_matrices/export/otu_contents_helper.rb', line 217

def content_type_material(t)
  return nil if t.type != 'Protonym'

  # Species type
  if t.is_species_rank?
    types = t.get_primary_type
    if types.any?
      return types.collect{|i| type_material_catalog_label(i)}.join('. ')
    else
      return nil
    end
  else
    if type_taxon_name_relationship = t&.type_taxon_name_relationship
      str = citations_tag(type_taxon_name_relationship)
      [ ' ' + type_taxon_name_relationship_label(t.type_taxon_name_relationship),
        (type_taxon_name_relationship.citations&.load&.any? ?  ' in ' : nil),
        str
      ].compact.join #.html_safe
    else
      nil
    end
  end
end

#get_otu_contents(options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
74
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
103
104
105
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'app/helpers/observation_matrices/export/otu_contents_helper.rb', line 4

def get_otu_contents(options = {})
  opt = {otus: []}.merge!(options)
  m = opt[:observation_matrix]

  otus = Otu.select('otus.*, observation_matrix_rows.id AS row_id').
          joins('INNER JOIN observation_matrix_rows ON observation_matrix_rows.observation_object_id = otus.id').
          where("observation_matrix_rows.observation_object_type = 'Otu'").
          where('observation_matrix_rows.observation_matrix_id = (?)', m.id).
          order(:observation_object_id)
  otu_rows = {}
  otus.each do |i|
    otu_rows[i.id] = i.row_id
  end
  otu_ids = otu_rows.keys

  content_hash = {}
  content_columns = {}

  #otu_ids = m.otus.collect{|i| i.id}
  ##### CSV.generate do |csv|
  ##### csv << ['otu_id', 'topic', 'text']

    if options[:taxon_name] == 'true'
      protonyms = Protonym.select('taxon_names.*, otus.name AS otu_name, observation_matrix_rows.id AS row_id').
        joins(:otus).joins('INNER JOIN observation_matrix_rows ON observation_matrix_rows.observation_object_id = otus.id').
        where("observation_matrix_rows.observation_object_type = 'Otu'").
        where('otus.id IN (?)', otu_ids).where('observation_matrix_rows.observation_matrix_id = (?)', m.id).
        order(:observation_object_id)
      content_columns['Taxon name'] = true
      content_columns['Authority'] = true
      protonyms.each do |p|
        ##### csv << ['row_' + p.row_id.to_s, 'Taxon name', [p.otu_name, p.cached_html].compact.join(': ') ]
        content_hash['row_' + p.row_id.to_s] = {}
        content_hash['row_' + p.row_id.to_s]['Taxon name'] = [p.otu_name, p.cached_html].compact.join(': ')
        content_hash['row_' + p.row_id.to_s]['Authority'] = p.cached_author_year
      end
      ##### protonyms.each do |p|
        ##### csv << ['row_' + p.row_id.to_s, 'Authority', p.cached_author_year ]  #####
      ##### end
    end

    if options[:include_nomenclature] == 'true'
      protonyms = Protonym.select('taxon_names.*, observation_matrix_rows.id AS row_id').
        joins(:otus).joins('INNER JOIN observation_matrix_rows ON observation_matrix_rows.observation_object_id = otus.id').
        where("observation_matrix_rows.observation_object_type = 'Otu'").
        where('otus.id IN (?)', otu_ids).where('observation_matrix_rows.observation_matrix_id = (?)', m.id).
        order(:observation_object_id)
      protonyms.each do |p|
        history = p.nomeclatural_history
        unless history.empty?
          st = []
          history.each do |h|
            st.push( [h[:name], h[:author_year], h[:statuses]].join(' '))
          end
          content_columns['Nomenclature'] = true
          content_hash['row_' + p.row_id.to_s]['Nomenclature'] = st.join('<br>')
          #####csv << ['row_' + p.row_id.to_s, 'Nomenclature', st.join('<br>') ]
        end
      end
    end

    if options[:include_contents] == 'true'
      contents = Content.select('contents.*, controlled_vocabulary_terms.name, observation_matrix_rows.id AS row_id').
        joins(:topic).joins('INNER JOIN observation_matrix_rows ON observation_matrix_rows.observation_object_id = contents.otu_id').
        where("observation_matrix_rows.observation_object_type = 'Otu'").
        where('contents.otu_id IN (?)', otu_ids).where('observation_matrix_rows.observation_matrix_id = (?)', m.id).
        order(:otu_id, :topic_id)
      contents.each do |i|
        content_columns[i.name] = true
        content_hash['row_' + i.row_id.to_s][i.name] = i.text
        ##### csv << ['row_' + i.row_id.to_s, i.name, i.text]
      end
    end

    if options[:include_autogenerated_description] == 'true'
      otus = Otu.select('otus.*, observation_matrix_rows.id AS row_id').
        joins('INNER JOIN observation_matrix_rows ON observation_matrix_rows.observation_object_id = otus.id').
        where("observation_matrix_rows.observation_object_type = 'Otu'").
        where('observation_matrix_rows.observation_matrix_id = (?)', m.id).
        order(:observation_object_id)

      otus.each do |o|
        description = Tools::Description::FromObservationMatrix.new(project_id: m.project_id, observation_matrix_id: m.id, otu_id: o.id)
        content_columns['Description'] = true
        content_hash['row_' + o.row_id.to_s]['Description'] = description.generated_description
        ##### csv << ['row_' + o.row_id.to_s, 'Description', description.generated_description ]
      end
    end

    if options[:include_distribution] == 'true'
      ###### ad = AssertedDistribution.select('asserted_distributions.*, geographic_areas.name, observation_matrix_rows.id AS row_id').
      #  joins(:geographic_area).joins('INNER JOIN observation_matrix_rows ON observation_matrix_rows.observation_object_id = asserted_distributions.otu_id').
      #  where("observation_matrix_rows.observation_object_type = 'Otu'").
      #  where('asserted_distributions.otu_id IN (?)', otu_ids).where('observation_matrix_rows.observation_matrix_id = (?)', m.id).
      #  order(:otu_id, :geographic_area_id)
      #otu_ad = {}
      #ad.each do |i|
      #  otu_ad[i.row_id] = [] if otu_ad[i.row_id].nil?
      #  otu_ad[i.row_id].append(i.name)
      #end
      #otu_ad.each do |key, value|
      #  ##### csv << ['row_' + key.to_s, 'Distribution', value.join(', ') ]
      #end
      protonyms = Protonym.select('taxon_names.*, observation_matrix_rows.id AS row_id').
        joins(:otus).joins('INNER JOIN observation_matrix_rows ON observation_matrix_rows.observation_object_id = otus.id').
        where("observation_matrix_rows.observation_object_type = 'Otu'").
        where('otus.id IN (?)', otu_ids).where('observation_matrix_rows.observation_matrix_id = (?)', m.id).
        order(:observation_object_id)
      protonyms.each do |p|
        content_columns['Distribution'] = true
        content_hash['row_' + p.row_id.to_s]['Distribution'] = paper_distribution_entry(p)
      end
    end

    if options[:include_type] == 'true'
      protonyms = Protonym.select('taxon_names.*, observation_matrix_rows.id AS row_id').
        joins(:otus).joins('INNER JOIN observation_matrix_rows ON observation_matrix_rows.observation_object_id = otus.id').
        where("observation_matrix_rows.observation_object_type = 'Otu'").
        where('otus.id IN (?)', otu_ids).where('observation_matrix_rows.observation_matrix_id = (?)', m.id).
        order(:observation_object_id)
      #p_ids = {}
      protonyms.each do |p|
        content_columns['Type'] = true
        content_hash['row_' + p.row_id.to_s]['Type'] = content_type_material(p)
        #next unless p_ids[p.id].nil?
        #stype = p.type_species
        #csv << ['row_' + p.row_id.to_s, 'Type species', stype.cached_html_original_name_and_author_year ] unless stype.nil?
        #gtype = p.type_genus
        #csv << ['row_' + p.row_id.to_s, 'Type genus', gtype.cached_html_original_name_and_author_year ] unless gtype.nil?
        #p_ids[p.id] = true
      end
      # protonyms = Protonym.select('taxon_names.*, observation_matrix_rows.id AS row_id').
      #  joins(:otus).joins('INNER JOIN observation_matrix_rows ON observation_matrix_rows.observation_object_id = otus.id').
      #  where("observation_matrix_rows.observation_object_type = 'Otu'").
      #  where('otus.id IN (?)', otu_ids).where('observation_matrix_rows.observation_matrix_id = (?)', m.id).
      #  where("rank_class LIKE '%Species%'").
      #  order(:observation_object_id)
      #p_ids = {}
      #protonyms.each do |p|
      #  next unless p_ids[p.id].nil?
      #  type = p&.type_materials&.primary&.first&.collection_object&.repository&.name
      #  csv << ['row_' + p.row_id.to_s, 'Type repository', type ] unless type.nil?
      #  p_ids[p.id] = true
      #end
      #p_ids = {}
      #protonyms.each do |p|
      #  next unless p_ids[p.id].nil?
      #  type = type = p&.type_materials&.primary&.first&.type_type
      #  csv << ['row_' + p.row_id.to_s, 'Type status', type ] unless type.nil?
      #  p_ids[p.id] = true
      #end
      #p_ids = {}
      #protonyms.each do |p|
      #  next unless p_ids[p.id].nil?
      #  type = p&.type_materials&.primary&.first&.collection_object&.buffered_collecting_event
      #  csv << ['row_' + p.row_id.to_s, 'Type locality', type ] unless type.nil?
      #  p_ids[p.id] = true
      #end
    end

    if options[:include_depictions] == 'true'
      tw_url = 'https://sfg.taxonworks.org'
      im = Tools::ImageMatrix.new(
        project_id: m.project_id,
        otu_filter: otu_ids.join('|'),
        per: 1000000)
      descriptors = im.list_of_descriptors.values
      im.depiction_matrix.each do |object|
        list = ''
        object[1][:depictions].each_with_index do |depictions, index|
          depictions.each do |depiction|
            lbl = []
            cit = im.image_hash[depiction[:image_id]][:citations].collect{|i| i[:cached]}.join('')
            lbl.push(descriptors[index][:name]) unless descriptors[index][:name].blank?
            lbl.push(depiction[:caption]) unless depiction[:caption].blank?
            #lbl.push('<b>Citation:</b> ' + cit) unless cit.blank?
            img_attr = Image.find(depiction[:image_id]).attribution
            #lbl.push(attribution_tag(img_attr).gsub('&#169;', '')) unless img_attr.nil?
            lbl.push(attribution_nexml_label(img_attr)) unless img_attr.nil?
            lbl = lbl.compact.join('; ')

            if im.image_hash[depiction[:image_id]][:image_file_content_type] == 'image/tiff'
              href = im.image_hash[depiction[:image_id]][:medium_url]
            else
              href = im.image_hash[depiction[:image_id]][:original_url]
            end

            list += "<span class='tw_depiction'><br>"
            list += "#{tag.img(src: short_url(href), class: 'tw_image')}<br>"
            list += "<b>Label:</b> #{lbl}<br>" unless lbl.blank?
            list += "<b>Citation:</b> #{cit}<br>" unless cit.blank?
            list += "</span>"
          end
        end
        content_columns['Illustrations'] = true
        content_hash['row_' + otu_rows[object[1][:otu_id]].to_s]['Illustrations'] = list unless list.blank?
        ##### csv << ['row_' + otu_rows[object[1][:otu_id]].to_s, 'Illustrations', list ] unless list.blank?
      end
    end

  ::CSV.generate(col_sep: "\t") do |csv|
    row = ['otu_id'] + content_columns.keys # Headers
    csv << row
    content_hash.each do |k,v|
      row = [k]
      content_columns.keys.each do |c|
        row << v[c]
      end
      csv << row
    end
  end.html_safe
end