Class: ObservationMatricesController

Inherits:
ApplicationController show all
Includes:
DataControllerConfiguration::ProjectDataControllerConfiguration
Defined in:
app/controllers/observation_matrices_controller.rb

Constant Summary

Constants included from ProjectsHelper

ProjectsHelper::CLASSIFIER, ProjectsHelper::CLASSIFIER_ANNOTATION

Instance Method Summary collapse

Methods included from DataControllerConfiguration::ProjectDataControllerConfiguration

#annotator_params

Methods included from RedirectHelper

#destroy_redirect

Methods included from RequestType

#json_request?

Methods included from LogRecent

#log_user_recent_route

Methods included from Cookies

#digest_cookie, #digested_cookie_exists?

Methods included from Whitelist

#whitelist_constantize

Methods included from ProjectsHelper

#cumulative_gb_per_year, #cumulative_projects_created_per_year, #document_cumulative_gb_per_year, #document_gb_per_year, #gb_per_year, #image_cumulative_gb_per_year, #image_gb_per_year, #invalid_object, #project_classification, #project_link, #project_matches, #project_tag, #projects_list, #projects_search_form, #sound_cumulative_gb_per_year, #sound_gb_per_year, #taxonworks_classification, #week_in_review_graphs

Methods included from Api::Intercept

#intercept_api

Methods included from TokenAuthentication

#intercept_project, #intercept_user, #intercept_user_or_project, #project_token_authenticate, #token_authenticate

Instance Method Details

#api_indexObject

GET /api/v1/observation_matrices

[View source]

240
241
242
243
244
245
246
# File 'app/controllers/observation_matrices_controller.rb', line 240

def api_index
  @observation_matrices = Queries::ObservationMatrix::Filter.new(params.merge!(api: true)).all
    .where(project_id: sessions_current_project_id)
    .page(params[:page])
    .per(params[:per])
  render '/observation_matrices/api/v1/index'
end

#api_showObject

GET /api/v1/observation_matrices/:id

[View source]

249
250
251
# File 'app/controllers/observation_matrices_controller.rb', line 249

def api_show
  render '/observation_matrices/api/v1/show'
end

#autocompleteObject

[View source]

118
119
120
# File 'app/controllers/observation_matrices_controller.rb', line 118

def autocomplete
  @observation_matrices = ObservationMatrix.where(project_id: sessions_current_project_id).where('name ilike ?', "%#{params[:term]}%")
end

#batch_addObject

.json

[View source]

97
98
99
100
101
102
103
104
# File 'app/controllers/observation_matrices_controller.rb', line 97

def batch_add
  o = ObservationMatrix.batch_add(params.merge(project_id: sessions_current_project_id))
  if o.kind_of?(Hash)
    render json: o
  else
    render json: o, status: :unprocessable_entity
  end
end

#batch_createObject

.json

[View source]

87
88
89
90
91
92
93
94
# File 'app/controllers/observation_matrices_controller.rb', line 87

def batch_create
  o = ObservationMatrix.batch_create(params.merge(project_id: sessions_current_project_id))
  if o.kind_of?(Hash)
    render json: o
  else
    render json: o, status: :unprocessable_entity
  end
end

#columnObject

[View source]

213
214
215
# File 'app/controllers/observation_matrices_controller.rb', line 213

def column
  @observation_matrix_column = ObservationMatrixColumn.where(project_id: sessions_current_project_id).find(params.require(:observation_matrix_column_id))
end

#column_labelsObject

[View source]

134
135
# File 'app/controllers/observation_matrices_controller.rb', line 134

def column_labels
end

#createObject

POST /observation_matrices POST /observation_matrices.json

[View source]

48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/controllers/observation_matrices_controller.rb', line 48

def create
  @observation_matrix = ObservationMatrix.new(observation_matrix_params)

  respond_to do |format|
    if @observation_matrix.save
      format.html { redirect_to @observation_matrix, notice: 'Matrix was successfully created.' }
      format.json { render :show, status: :created, location: @observation_matrix }
    else
      format.html { render :new }
      format.json { render json: @observation_matrix.errors, status: :unprocessable_entity }
    end
  end
end

#create_matrix_for_nexus_import(matrix_name) ⇒ Matrix? (private)

Returns:

  • (Matrix, nil)
[View source]

423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# File 'app/controllers/observation_matrices_controller.rb', line 423

def create_matrix_for_nexus_import(matrix_name)
  if matrix_name.present?
    begin
      m = ObservationMatrix.create!(name: matrix_name)
    rescue ActiveRecord::RecordInvalid
      render json: { errors: 'The provided matrix name is already in use, try another' },
        status: :unprocessable_entity
    end

    return m
  end

  i = 0
  unique = ''
  user_name = User.find(Current.user_id).name
  begin
    title = "Converted matrix created #{Time.now.utc.to_formatted_s(:long)} by #{user_name + unique}"

    m = ObservationMatrix.create!(name: title)
  rescue ActiveRecord::RecordInvalid
    if i < 60
      i = i + 1
      unique = "-#{i}"
      retry
    end
    raise
  end

  m
end

#csvObject

[View source]

185
186
187
188
189
190
191
192
193
194
# File 'app/controllers/observation_matrices_controller.rb', line 185

def csv
  respond_to do |format|
    base = '/observation_matrices/export/csv/'
    format.html { render base + 'index' }
    format.text {
      s = render_to_string(partial: base + 'csv', locals: { as_file: true }, layout: false, formats: [:html])
      send_data(s, filename: "csv_#{DateTime.now}.csv", type: 'text/plain')
    }
  end
end

#descriptor_listObject

[View source]

163
164
165
166
167
168
169
170
171
172
# File 'app/controllers/observation_matrices_controller.rb', line 163

def descriptor_list
  respond_to do |format|
    base = '/observation_matrices/export/descriptor_list/'
    format.html { render base + 'index' }
    format.text {
      s = render_to_string(partial: base + 'descriptor_list', locals: { as_file: true }, layout: false, formats: [:html])
      send_data(s, filename: "descriptor_list_#{DateTime.now}.csv", type: 'text/plain')
    }
  end
end

#destroyObject

DELETE /observation_matrices/1 DELETE /observation_matrices/1.json

[View source]

78
79
80
81
82
83
84
# File 'app/controllers/observation_matrices_controller.rb', line 78

def destroy
  @observation_matrix.destroy
  respond_to do |format|
    format.html { redirect_to observation_matrices_url, notice: 'Matrix was successfully destroyed.' }
    format.json { head :no_content }
  end
end

#document_to_nexus(doc_id) ⇒ Object (private)

[View source]

404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# File 'app/controllers/observation_matrices_controller.rb', line 404

def document_to_nexus(doc_id)
  begin
    Vendor::NexusParser.document_id_to_nexus(doc_id)
  rescue ActiveRecord::RecordNotFound, NexusParser::ParseError => e
    render json: { errors: "Nexus parse error: #{e}" },
      status: :unprocessable_entity
    return nil
  rescue ArgumentError => e
    render json: { errors: "Error reading document: #{e} - are you sure it's a nexus document?" },
      status: :unprocessable_entity
    return nil
  rescue TaxonWorks::Error => e
    render json: { errors: "Error converting nexus to TaxonWorks: #{e}" },
      status: :unprocessable_entity
    return nil
  end
end

#downloadObject

[View source]

217
218
219
# File 'app/controllers/observation_matrices_controller.rb', line 217

def download
  send_data Export::CSV.generate_csv(ObservationMatrix.where(project_id: sessions_current_project_id)), type: 'text', filename: "observation_matrices_#{DateTime.now}.tsv"
end

#download_contentsObject

[View source]

221
222
223
# File 'app/controllers/observation_matrices_controller.rb', line 221

def download_contents
  send_data Export::CSV.generate_csv(ObservationMatrix.where(project_id: sessions_current_project_id)), type: 'text', filename: "observation_matrices_#{DateTime.now}.csv"
end

#editObject

GET /observation_matrices/1/edit

[View source]

42
43
44
# File 'app/controllers/observation_matrices_controller.rb', line 42

def edit
  redirect_to new_matrix_task_path observation_matrix_id: @observation_matrix.id
end

#import_from_nexusObject

POST /observation_matrices/import_nexus.json

[View source]

269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'app/controllers/observation_matrices_controller.rb', line 269

def import_from_nexus
  return if !(nf = document_to_nexus(params[:nexus_document_id]))

  return if !nexus_dimensions_okay(nf)

  options = nexus_import_options_params
  return if !nexus_options_are_sane(options)

  return if !(m = create_matrix_for_nexus_import(options[:matrix_name]))

  Documentation.create!({
    documentation_object: m,
    document_id: params[:nexus_document_id]
  })

  # Once we've handed the matrix off to the background job it could get
  # destroyed at any time, so save what we want from it now.
  matrix_id = m.id
  matrix_name = m.name

  ImportNexusJob.perform_later(
    params[:nexus_document_id],
    m,
    options,
    sessions_current_user_id,
    sessions_current_project_id,
  )

  render json: { matrix_id:, matrix_name: }
end

#indexObject

GET /observation_matrices GET /observation_matrices.json

[View source]

12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/controllers/observation_matrices_controller.rb', line 12

def index
  respond_to do |format|
    format.html do
      @recent_objects = ObservationMatrix.recent_from_project_id(sessions_current_project_id).order(updated_at: :desc).limit(10)
      render '/shared/data/all/index'
    end
    format.json {
      @observation_matrices = ::Queries::ObservationMatrix::Filter.new(params).all
        .where(project_id: sessions_current_project_id)
        .page(params[:page])
        .per(params[:per])
    }
  end
end

#listObject

[View source]

32
33
34
# File 'app/controllers/observation_matrices_controller.rb', line 32

def list
  @observation_matrices = ObservationMatrix.with_project_id(sessions_current_project_id).page(params[:page]).per(params[:per])
end

#newObject

GET /observation_matrices/new

[View source]

37
38
39
# File 'app/controllers/observation_matrices_controller.rb', line 37

def new
  redirect_to new_matrix_task_path, notice: 'Redirecting to new task.'
end

#nexmlObject

TODO export formats can move to a concern controller

[View source]

139
140
141
142
143
144
145
146
147
148
149
# File 'app/controllers/observation_matrices_controller.rb', line 139

def nexml
  @options = nexml_params
  respond_to do |format|
    base =  '/observation_matrices/export/nexml/'
    format.html { render base + 'index' }
    format.rdf {
      s = render_to_string(partial: base + 'nexml', layout: false, formats: [:rdf])
      send_data(s, filename: "nexml_#{DateTime.now}.xml", type: 'text/plain')
    }
  end
end

#nexml_paramsObject (private)

TODO: Not all params are supported yet.

[View source]

303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'app/controllers/observation_matrices_controller.rb', line 303

def nexml_params
  { observation_matrix: @observation_matrix,
    target: '',
    include_otus: 'true',
    include_collection_objects: 'true',
    include_descriptors: 'true',
    include_otu_depictions: 'true',
    include_matrix: 'true',
    include_trees: 'false',
    rdf: false }.merge!(
      params.permit(
        :include_otus,
        :include_collection_objects,
        :include_descriptors,
        :include_matrix,
        :include_trees,
        :rdf
      ).to_h
    )
end

#nexusObject

[View source]

196
197
198
199
200
201
202
203
204
205
# File 'app/controllers/observation_matrices_controller.rb', line 196

def nexus
  respond_to do |format|
    base = '/observation_matrices/export/nexus/'
    format.html { render base + 'index' }
    format.text {
      s = render_to_string(partial: base + 'nexus', locals: { as_file: true }, layout: false, formats: [:html])
      send_data(s, filename: "nexus_#{DateTime.now}.nex", type: 'text/plain')
    }
  end
end

#nexus_dataObject

GET /observation_matrices/nexus_data.json

[View source]

254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'app/controllers/observation_matrices_controller.rb', line 254

def nexus_data
  return if !(nf = document_to_nexus(params[:nexus_document_id]))

  return if !nexus_dimensions_okay(nf)

  options = nexus_import_options_params

  @otus = helpers.preview_nexus_otus(nf,
    options[:match_otu_to_name], options[:match_otu_to_taxonomy_name])

  @descriptors = helpers.preview_nexus_descriptors(nf,
    options[:match_character_to_name])
end

#nexus_dimensions_okay(nf) ⇒ Object (private)

[View source]

454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# File 'app/controllers/observation_matrices_controller.rb', line 454

def nexus_dimensions_okay(nf)
  max_dim = 1000

  if (nf.taxa.size <= max_dim && nf.characters.size <= max_dim) &&
    (nf.taxa.size > 0 && nf.characters.size > 0)
    return true
  end

  error_message = ''
  if nf.taxa.size > max_dim || nf.characters.size > max_dim
    error_message = "Max size is #{max_dim}x#{max_dim}, this file has #{nf.taxa.size} taxa, #{nf.characters.size} characters"
  elsif nf.taxa.size == 0 && nf.characters.size == 0
    error_message = "No taxa or characters parsed - this could be because your matrix is very large and nexus_parser failed to parse it. TaxonWorks limits are #{max_dim}x#{max_dim}"
  else
    error_message = "The nexus file must include both taxa and characters, this one has #{nf.taxa.size} taxa and #{nf.characters.size} characters"
  end

  render json: { errors: error_message }, status: :unprocessable_entity

  false
end

#nexus_import_options_paramsObject (private)

[View source]

365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'app/controllers/observation_matrices_controller.rb', line 365

def nexus_import_options_params
  if params[:options].blank?
    # Rails discarded the empty options {} that's sent when all options are
    # defaults, this restores it
    return ActionController::Parameters.new({}).permit!
  end

  boolean_options = [:match_otu_to_taxonomy_name, :match_otu_to_name,
    :match_character_to_name, :cite_otus, :cite_descriptors,
    :cite_observations, :cite_matrix]

  params[:options].each { |k, v|
    if boolean_options.include? k.to_sym
      params[:options][k] = (v == 'true' || v == true) ? true : false
    end
  }

  params.require(:options)
    .permit(:matrix_name, *boolean_options,
      citation: [:source_id, :pages, :is_original]
    )
end

#nexus_options_are_sane(options) ⇒ Object (private)

[View source]

388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'app/controllers/observation_matrices_controller.rb', line 388

def nexus_options_are_sane(options)
  if !options[:citation]&.dig(:source_id) &&
    (options[:cite_otus] || options[:cite_descriptors] ||
      options[:cite_observations] || options[:cite_matrix])

    render json: {
        errors: 'Citation option(s) checked but no source selected'
      },
      status: :unprocessable_entity

    return false
  end

  true
end

#observation_matrix_paramsObject (private)

[View source]

357
358
359
360
361
362
363
# File 'app/controllers/observation_matrices_controller.rb', line 357

def observation_matrix_params
  params.require(:observation_matrix).permit(
    :name,
    :otu_id,
    :is_public
  )
end

#otu_contentObject

[View source]

151
152
153
154
155
156
157
158
159
160
161
# File 'app/controllers/observation_matrices_controller.rb', line 151

def otu_content
  @options = otu_content_params
  respond_to do |format|
    base ='/observation_matrices/export/otu_content/'
    format.html { render base + 'index' }
    format.text {
      s = render_to_string(partial: base + 'otu_content', layout: false, formats: [:html])
      send_data(s, filename: "otu_content_#{DateTime.now}.csv", type: 'text/plain')
    }
  end
end

#otu_content_paramsObject (private)

[View source]

324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'app/controllers/observation_matrices_controller.rb', line 324

def otu_content_params
  { observation_matrix: @observation_matrix,
    target: '',
    include_otus: 'true',
    include_collection_objects: 'false',
    include_contents: 'true',
    include_distribution: 'true',
    include_type: 'true',
    taxon_name: 'true',
    include_nomenclature: 'true',
    include_autogenerated_description: 'true',
    include_depictions: 'true',
    rdf: false }.merge!(
      params.permit(
        :include_otus,
        :include_collection_objects,
        :include_matrix,
        :include_contents,
        :include_distribution,
        :include_type,
        :taxon_name,
        :include_nomenclature,
        :include_autogenerated_description,
        :include_depictions,
        :rdf
      ).to_h
    )
end

#otus_used_in_matricesObject

[View source]

225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'app/controllers/observation_matrices_controller.rb', line 225

def otus_used_in_matrices
  # ObservationMatrix.with_otu_ids_array([13597, 25680])
  if params[:otu_ids].present?
    p = ObservationMatrix.with_otu_id_array(params[:otu_ids].split('|')).pluck(:id)
    if p.nil?
      render json: {otus_used_in_matrices: ''}.to_json
    else
      render json: {otus_used_in_matrices: p}.to_json
    end
  else
    render json: {otus_used_in_matrices: ''}.to_json
  end
end

#reorder_columnsObject

[View source]

114
115
116
# File 'app/controllers/observation_matrices_controller.rb', line 114

def reorder_columns
  @observation_matrix.reorder_columns(params.require(:by))
end

#reorder_rowsObject

[View source]

106
107
108
109
110
111
112
# File 'app/controllers/observation_matrices_controller.rb', line 106

def reorder_rows
  if @observation_matrix.reorder_rows(params.require(:by))
    render json: :success
  else
    render json:  :error, status: :unprocessable_entity
  end
end

#rowObject

GET /observation_matrices/row.json?observation_matrix_row_id=1 TODO: Why is this here? (used in Matrix Row Coder)

[View source]

209
210
211
# File 'app/controllers/observation_matrices_controller.rb', line 209

def row
  @observation_matrix_row = ObservationMatrixRow.where(project_id: sessions_current_project_id).find(params.require(:observation_matrix_row_id))
end

#row_labelsObject

[View source]

131
132
# File 'app/controllers/observation_matrices_controller.rb', line 131

def row_labels
end

#searchObject

TODO: deprecate

[View source]

123
124
125
126
127
128
129
# File 'app/controllers/observation_matrices_controller.rb', line 123

def search
  if params[:id].blank?
    redirect_to observation_matrices_path, alert: 'You must select an item from the list with a click or tab press before clicking show.'
  else
    redirect_to observation_matrix_path(params[:id])
  end
end

#set_observation_matrixObject (private)

[View source]

353
354
355
# File 'app/controllers/observation_matrices_controller.rb', line 353

def set_observation_matrix
  @observation_matrix = ObservationMatrix.where(project_id: sessions_current_project_id).find(params[:id])
end

#showObject

GET /observation_matrices/1 GET /observation_matrices/1.json

[View source]

29
30
# File 'app/controllers/observation_matrices_controller.rb', line 29

def show
end

#tntObject

[View source]

174
175
176
177
178
179
180
181
182
183
# File 'app/controllers/observation_matrices_controller.rb', line 174

def tnt
  respond_to do |format|
    base = '/observation_matrices/export/tnt/'
    format.html { render base + 'index' }
    format.text {
      s = render_to_string(partial: base + 'tnt', locals: { as_file: true }, layout: false, formats: [:html])
      send_data(s, filename: "tnt_#{DateTime.now}.tnt", type: 'text/plain')
    }
  end
end

#updateObject

PATCH/PUT /observation_matrices/1 PATCH/PUT /observation_matrices/1.json

[View source]

64
65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/observation_matrices_controller.rb', line 64

def update
  respond_to do |format|
    if @observation_matrix.update(observation_matrix_params)
      format.html { redirect_to @observation_matrix, notice: 'Matrix was successfully updated.' }
      format.json { render :show, status: :ok, location: @observation_matrix }
    else
      format.html { render :edit }
      format.json { render json: @observation_matrix.errors, status: :unprocessable_entity }
    end
  end
end