| 
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230 | # File 'app/helpers/observation_matrices/export/otu_contents_helper.rb', line 5
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 = {}
  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|
            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
  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|
      h = p.nomeclatural_history
      unless h.empty?
        st = []
        h.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] ||= {}
        content_hash['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] ||= {}
      content_hash['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] ||= {}
      content_hash['row_' + o.row_id.to_s]['Description'] = description.generated_description
          end
  end
  if options[:include_distribution] == '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|
      content_columns['Distribution'] = true
      content_hash['row_' + p.row_id.to_s] ||= {}
      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)
        protonyms.each do |p|
      content_columns['Type'] = true
      content_hash['row_' + p.row_id.to_s] ||= {}
      content_hash['row_' + p.row_id.to_s]['Type'] = content_type_material(p)
                                        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?
                    img_attr = Image.find(depiction[:image_id]).attribution
                    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] ||= {}
      content_hash['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     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 |