Module: GraphHelper

Defined in:
app/helpers/graph_helper.rb

Instance Method Summary collapse

Instance Method Details

#asserted_environment_graph(asserted_environment, graph: nil, target: nil) ⇒ Object



170
171
172
173
174
175
176
177
178
179
# File 'app/helpers/graph_helper.rb', line 170

def asserted_environment_graph(asserted_environment, graph: nil, target: nil)
  a = asserted_environment
  return nil if a.nil?

  g = initialize_graph(graph, a, target)

  g.add(a.asserted_environment_object, a)

  g
end

#biological_association_graph(biological_association, graph: nil, target: nil) ⇒ Object



274
275
276
277
278
279
280
281
282
283
284
285
# File 'app/helpers/graph_helper.rb', line 274

def biological_association_graph(biological_association, graph: nil, target: nil)
  b = biological_association
  return nil if b.nil?

  g = initialize_graph(graph, nil, target)

  g.add_node(b.biological_association_subject, citations: false)
  g.add_node(b.biological_association_object, citations: false)

  g.add_edge(b.biological_association_subject, b.biological_association_object, edge_label: b.biological_relationship.name)
  g
end

#citation_graph(citation, graph: nil, target: nil, source: false, citation_object: false, topics: true) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'app/helpers/graph_helper.rb', line 197

def citation_graph(citation, graph: nil, target: nil, source: false, citation_object: false, topics: true)
  c = citation
  return nil if c.nil?

  g = initialize_graph(graph, c, target)

  if source
    source_graph(c.source, graph: g, target: c, authors: true, editors: true)
  end

  if citation_object
    g.add_node(c.citation_object, citations: false, identifiers: true)
    g.add_edge(c, c.citation_object)
  end

  if topics
    c.topics.each do |t|
      g.add(t, c)
    end
  end
  g
end

#collecting_event_graph(collecting_event, graph: nil, target: nil, collection_objects: false, asserted_environments: true) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'app/helpers/graph_helper.rb', line 242

def collecting_event_graph(collecting_event, graph: nil, target: nil, collection_objects: false, asserted_environments: true)
  c = collecting_event
  return nil if c.nil?

  g = initialize_graph(graph, c, target)

  if collection_objects
    c.collection_objects.each do |o|
      collection_object_graph(o, graph: g, target: c, taxon_determinations: true)
    end
  end

  c.collectors.each do |p|
    g.add(p,c)
  end

  c.georeferences.each do |r|
    g.add(r,c)
    r.georeference_authors.each do |p|
      g.add(p,r)
    end
  end

  if asserted_environments
    c.asserted_environments.each do |a|
      g.add(a, c)
    end
  end

  g
end

#collection_object_graph(collection_object, graph: nil, target: nil, collecting_event: false, taxon_determinations: false, biological_associations: false, images: false) ⇒ Object

Returns Export::Graph.

Returns:

  • Export::Graph



357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# File 'app/helpers/graph_helper.rb', line 357

def collection_object_graph(collection_object, graph: nil, target: nil, collecting_event: false, taxon_determinations: false, biological_associations: false, images: false)
  c = collection_object
  return nil if c.nil?

  g = initialize_graph(graph, c, target)

  collecting_event_graph(c.collecting_event, graph: g, target: c)

  if r = c.repository
    g.add(r, c)
  end

  c.biocuration_classes.each do |b|
    g.add(b, c)
  end

  c.observations.each do |o|
    g.add(o, c)
  end

  if images
    c.images.each do |i|
      image_graph(i, graph: g, target: c)
    end
  end

  if taxon_determinations
    c.taxon_determinations.each do |d|
      taxon_determination_graph(d, graph: g, target: c, taxon_names: true)
    end
  end

  if biological_associations
    c.all_biological_associations.each do |b|
      g.add_node(b.biological_association_subject)
      g.add_node(b.biological_association_object)
      g.add_node(b.biological_relationship)

      g.add_edge(b.biological_relationship, b.biological_association_subject)
      g.add_edge(b.biological_relationship, b.biological_association_object)
    end
  end

  g
end

#depiction_graph(depiction, graph: nil, target: nil) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
# File 'app/helpers/graph_helper.rb', line 157

def depiction_graph(depiction, graph: nil, target: nil)
  d = depiction
  return nil if d.nil?

  g = initialize_graph(graph, d, target)

  o = d.depiction_object
  g.add_node(o)
  g.add_edge(d, o)

  g
end

#gazetteer_graph(gazetteer, graph: nil, target: nil, asserted_environments: true) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'app/helpers/graph_helper.rb', line 181

def gazetteer_graph(gazetteer, graph: nil, target: nil, asserted_environments: true)
  z = gazetteer
  return nil if z.nil?

  g = initialize_graph(graph, z, target)

  if asserted_environments
    z.asserted_environments.each do |a|
      g.add(a, z)
    end
  end

  g
end

#image_graph(image, graph: nil, target: nil, depictions: false, citations: false, copyright_holders: true) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'app/helpers/graph_helper.rb', line 136

def image_graph(image, graph: nil, target: nil, depictions: false, citations: false, copyright_holders: true)
  i = image
  return nil if i.nil?

  g = initialize_graph(graph, i, target)

  if depictions
    i.depictions.each do |d|
      depiction_graph(d, graph: g, target: i)
    end
  end

  if citations
    i.citations.where(project_id: sessions_current_project_id).each do |c|
      citation_graph(c, graph: g, target: i, citation_object: true, topics: true)
    end
  end

  g
end

#initialize_graph(graph, object, target) ⇒ Object (private)



440
441
442
443
444
445
446
447
448
# File 'app/helpers/graph_helper.rb', line 440

def initialize_graph(graph, object, target)
  g = graph
  if g.nil?
    g = Export::Graph.new(object:)
  else
    g.add(object, target)
  end
  g
end

#object_graph(object) ⇒ Object



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
# File 'app/helpers/graph_helper.rb', line 43

def object_graph(object)
  return nil if object.nil?

  case object.class.base_class.name
  when 'CollectionObject'
    collection_object_graph(object, collecting_event: true, taxon_determinations: true, biological_associations: true, images: true).to_json
  when 'CollectingEvent'
    collecting_event_graph(object, collection_objects: true).to_json
  when 'TaxonName'
    taxon_name_graph(object, children: true, parents: true, otus: true, synonymy: true).to_json
  when 'Otu'
    otu_graph(object, collection_objects: true, taxon_name: true, synonymy: true, biological_associations: true, asserted_distributions: true).to_json
  when 'TaxonDetermination'
    taxon_determination_graph(object, collection_object: true, taxon_names: true).to_json
  when 'Person'
    person_graph(object).to_json
  when 'Citation'
    citation_graph(object, source: true, citation_object: true, topics: true).to_json
  when 'Source'
    source_graph(object, citations: true, authors: true, editors: true).to_json
  when 'Image'
    image_graph(object, citations: true, depictions: true).to_json
  when 'BiologicalAssociation'
    biological_association_graph(object).to_json
  when 'AssertedEnvironment'
    asserted_environment_graph(object).to_json
  when 'Gazetteer'
    gazetteer_graph(object).to_json
  else
    g = Export::Graph.new( object: )
    g.to_json
  end
end

#object_graph2(object) ⇒ Object



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
# File 'app/helpers/graph_helper.rb', line 77

def object_graph2(object)
  return nil if object.nil?

  case object.class.base_class.name
  when 'CollectionObject'
    collection_object_graph(object, collecting_event: true, taxon_determinations: true, biological_associations: true, images: true).to_json
  when 'CollectingEvent'
    collecting_event_graph(object, collection_objects: true).to_json
  when 'TaxonName'
    taxon_name_graph(object, children: true, parents: true, otus: true, synonymy: true).to_json
  when 'Otu'
    otu_graph(object, collection_objects: true, taxon_name: true, synonymy: true, biological_associations: true, asserted_distributions: true).to_json
  when 'TaxonDetermination'
    taxon_determination_graph(object, collection_object: true, taxon_names: true).to_json
  when 'Person'
    person_graph(object).to_json
  when 'Citation'
    citation_graph(object, source: true, citation_object: true, topics: true).to_json
  when 'Source'
    source_graph(object, citations: true, authors: true, editors: true).to_json
  when 'Image'
    image_graph(object, citations: true, depictions: true).to_json
  else
    g = Export::Graph.new( object: )
    g.to_json
  end
end

#objects_graph(object_scope) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/helpers/graph_helper.rb', line 27

def objects_graph(object_scope)
  g = initialize_graph(nil, nil, nil)

  object_scope.each do |o|

    case o.class.base_class.name
    when 'BiologicalAssociation'
      biological_association_graph(o, graph: g)
    else
      next
    end
  end

  g
end

#otu_graph(otu, graph: nil, target: nil, collection_objects: false, taxon_name: true, synonymy: false, biological_associations: true, asserted_distributions: true, asserted_environments: true) ⇒ Object



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'app/helpers/graph_helper.rb', line 287

def otu_graph(otu, graph: nil, target: nil, collection_objects: false, taxon_name: true, synonymy: false, biological_associations: true, asserted_distributions: true, asserted_environments: true )
  o = otu
  return nil if o.nil?
  g = initialize_graph(graph, o, target)

  if taxon_name
    taxon_name_graph(o.taxon_name, graph: g, target: o, synonymy:, parents: true)
  end

  if collection_objects
    o.collection_objects.each do |co|
      collection_object_graph(co, graph: g, target: o, collecting_event: true)
    end
  end

  if biological_associations
    o.all_biological_associations.each do |b|
      g.add_node(b.biological_association_subject)
      g.add_node(b.biological_association_object)
      g.add_node(b.biological_relationship)

      g.add_edge(b.biological_relationship, b.biological_association_subject)
      g.add_edge(b.biological_relationship, b.biological_association_object)
    end
  end

  if asserted_distributions
    o.asserted_distributions.each do |a|

      g.add_node(a)
      g.add_node(a.asserted_distribution_shape)

      g.add_edge(a, a.asserted_distribution_shape)
    end
  end

  if asserted_environments
    o.asserted_environments.each do |a|
      g.add(a, o)
    end
  end

  g
end

#person_graph(person, graph: nil, target: nil) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'app/helpers/graph_helper.rb', line 220

def person_graph(person, graph: nil, target: nil)
  p = person
  return nil if p.nil?

  g = initialize_graph(graph, p, target)

  # TODO: Loop has_many
  [:authored_sources, :edited_sources, :human_sources].each do |m|
    p.send(m).each do |o|
      g.add(o, p)
    end
  end

  [ :collecting_events, :taxon_determinations, :authored_taxon_names, :georeferences, :collection_objects, :dwc_occurrences].each do |m|
    p.send(m).where(project_id: sessions_current_project_id).each do |o|
      g.add(o,p)
    end
  end

  g
end

#source_graph(source, graph: nil, target: nil, citations: false, authors: false, editors: false) ⇒ Object



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
# File 'app/helpers/graph_helper.rb', line 105

def source_graph(source, graph: nil, target: nil, citations: false, authors: false, editors: false)
  s = source
  return nil if s.nil?

  g = initialize_graph(graph, nil, nil)

  # We can't auto-cite, so .add can not be used
  g.add_node(s, citations: false, identifiers: true)
  g.add_edge(target, s)

  if authors && s.respond_to?(:authors)
    s.authors.each do |p|
      g.add(p, s)
    end
  end

  if editors && s.respond_to?(:editors)
    s.editors.each do |p|
      g.add(p, s)
    end
  end

  if citations
    s.citations.where(project_id: sessions_current_project_id).each do |c|
      citation_graph(c, graph: g, target: s, citation_object: true, topics: true)
    end
  end

  g
end

#taxon_determination_graph(taxon_determination, graph: nil, target: nil, collection_object: false, taxon_names: false) ⇒ Object



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'app/helpers/graph_helper.rb', line 332

def taxon_determination_graph(taxon_determination, graph: nil, target: nil, collection_object: false, taxon_names: false)
  td = taxon_determination
  return nil if td.nil?
  g = initialize_graph(graph, td, target)

  g.add(td.otu, td)

  if collection_object
    g.add(td.taxon_determination_object, td)
  end

  if taxon_names
    if td.otu.taxon_name
      taxon_name_graph(td.otu.taxon_name, graph: g, target: td.otu, parents: true)
    end
  end

  td.determiners.each do |p|
    g.add(p, td)
  end

  g
end

#taxon_name_graph(taxon_name, graph: nil, target: nil, synonymy: false, children: false, otus: false, parents: false) ⇒ Object



403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'app/helpers/graph_helper.rb', line 403

def taxon_name_graph(taxon_name, graph: nil, target: nil, synonymy: false, children: false, otus: false, parents: false)
  t = taxon_name
  return nil if t.nil?

  g = initialize_graph(graph, t, target)

  t.taxon_name_authors.each do |p|
    g.add(p, t)
  end

  if children
    t.children.each do |n|
      taxon_name_graph(n, target: t, graph: g)
    end
  end

  if synonymy
    t.synonyms.each do |n|
      taxon_name_graph(n, target: t, graph: g)
    end
  end

  if otus
    t.otus.each do |o|
      otu_graph(o, target: t, graph: g)
    end
  end

  if parents
    taxon_name_graph(t.parent, graph: g, target: t, parents:)
  end

  g
end

#week_in_review(weeks) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/helpers/graph_helper.rb', line 3

def week_in_review(weeks)
  h = {
    metadata: {
      weeks_ago: weeks,
    },
    data: []
  }

  %w{otus taxon_names collection_objects collecting_events biological_associations asserted_distributions type_materials images documents descriptors observations contents}.each do |i|

    g = { target: i }

    q = sessions_current_project.send(i.to_sym).where("#{i}.created_at > ?", weeks.week.ago)

    g[:data] = q.group(:created_by_id).count.collect{|k,v| [k, User.find(k).name, v]}.sort{|a,b| b.last <=> a.last}
    g[:count] = q.count
    g[:title] = "#{i.humanize} created (#{q.count})"

    h[:data].push g
  end

  h
end