Module: ObservationMatrices::Export::OtuContentsHelper

Extended by:
OtuContentsHelper
Included in:
OtuContentsHelper
Defined in:
app/helpers/observation_matrices/export/otu_contents_helper.rb

Instance Method Summary collapse

Instance Method Details

#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
# 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]

  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.*, observation_matrix_rows.id AS row_id').
        joins(:otus).joins('INNER JOIN observation_matrix_rows ON observation_matrix_rows.otu_id = otus.id').
        where('otus.id IN (?)', otu_ids).where('observation_matrix_rows.observation_matrix_id = (?)', m.id).
        order(:otu_id)
      protonyms.each do |p|
        csv << ['row_' + p.row_id.to_s, 'Taxon name', p.cached_html_name_and_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.otu_id = otus.id').
        where('otus.id IN (?)', otu_ids).where('observation_matrix_rows.observation_matrix_id = (?)', m.id).
        order(:otu_id)
      protonyms.each do |p|
        history = p.nomeclatural_history
        unless history.empty?
          st = ''
          history.each do |h|
            st += [h[:name], h[:author_year], h[:statuses], '<br>'].join(' ')
          end
          csv << ['row_' + p.row_id.to_s, 'Nomenclature', st ]
        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.otu_id = contents.otu_id').
        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|
        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.otu_id = otus.id').
        where('observation_matrix_rows.observation_matrix_id = (?)', m.id).
        order(:otu_id)

      otus.each do |o|
        description = Catalog::DescriptionFromObservationMatrix.new(project_id: m.project_id, observation_matrix_id: m.id, otu_id: o.id)
        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.otu_id = asserted_distributions.otu_id').
        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
    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.otu_id = otus.id').
        where('otus.id IN (?)', otu_ids).where('observation_matrix_rows.observation_matrix_id = (?)', m.id).
        where("rank_class LIKE '%Family%' OR rank_class LIKE '%Genus%'").
        order(:otu_id)
      protonyms.each do |p|
        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?
      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.otu_id = otus.id').
        where('otus.id IN (?)', otu_ids).where('observation_matrix_rows.observation_matrix_id = (?)', m.id).
        where("rank_class LIKE '%Species%'").
        order(:otu_id)
      protonyms.each do |p|
        type = p&.type_materials&.primary&.first&.collection_object&.repository&.name
        csv << ['row_' + p.row_id.to_s, 'Type repository', type ] unless type.nil?
      end
    end

    if options[:include_depictions] == 'true'
      tw_url = 'https://sfg.taxonworks.org'
      im =  ImageMatrix.new(
        project_id: m.project_id,
        otu_filter: otu_ids.join('|'))
      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|
            list += "<span class='tw_depiction'><br>\n"
            image_url = tw_url + im.image_hash[depiction[:image_id]][:original_url]
            #image_url = ::ApplicationController.helpers.short_url(im.image_hash[depiction[:image_id]][:original_url])
            list += "<img class='tw_image' src='#{image_url}'><br>\n"
            list += "<b>Object:</b> #{object[1][:object].otu_name}<br>\n" unless object[1][:object].otu_name.blank?
            list += "<b>Description:</b> #{descriptors[index][:name]}<br>\n" unless descriptors[index].blank?
            list += "<b>Label:</b> #{depiction[:figure_label]}<br>\n" unless depiction[:figure_label].blank?
            list += "<b>Citation:</b> #{depiction[:source_cached]}<br>\n" unless depiction[:source_cached].blank?
            list += "</span>\n"
          end
        end
        csv << ['row_' + object[1][:row_id].to_s, 'Illustrations', list ] unless list.blank?
      end
    end

  end
end