Class: BatchLoad::Import::DWCA

Inherits:
BatchLoad::Import show all
Defined in:
lib/batch_load/import/dwca.rb

Overview

TODO: Originally transliterated from Import::CollectingEvents: Remove this to-do after successful operation.

Instance Attribute Summary (collapse)

Attributes inherited from BatchLoad::Import

#create_attempted, #csv, #errors, #file, #file_errors, #import_level, #processed, #processed_rows, #project, #successful_rows, #total_data_lines, #total_lines, #user, #user_header_map

Instance Method Summary (collapse)

Methods inherited from BatchLoad::Import

#all_objects, #create_attempted?, #import_level_ok?, #line_strict_level_ok?, #processed?, #ready_to_create?, #sorted_processed_rows, #strict_level_ok?, #total_records_created, #user_map, #valid?, #valid_objects, #warn_level_ok?

Constructor Details

- (DWCA) initialize(dwca_namespace: nil, **args)

Returns a new instance of DWCA



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
# File 'lib/batch_load/import/dwca.rb', line 16

def initialize(dwca_namespace: nil, **args)
  @dwca_namespace    = dwca_namespace
  @root              = args.delete(:root)
  @kingdom           = args.delete(:kingdom)
  @cat_no_pred       = args.delete(:cat_no_pred)
  @geo_rem_kw        = args.delete(:geo_rem_kw)
  @repo              = args.delete(:repo)
  @namespace         = args.delete(:namespace)
  @collecting_events = {}
  @rows              = {}
  @row_objects       = {}
  @parser            = ScientificNameParser.new
  @tasks_            = {
    make_ident: %w(catalognumber occurrenceid),
    make_prsn:  %w(georeferencedby),
    make_tn:    %w(scientificname taxonrank family kingdom),
    make_td:    %w(scientificname basisofrecord individualcount organismquantity organismquantitytype recordedby),
    # make_otu:   %w(scientificname),
    make_co:    %w(individualcount organismquantity organismquantitytype),
    make_ba:    %w(associatedtaxa),
    make_notes: %w(georeferenceremarks locationremarks occurrenceremarks),
    make_tag:   %w(),
    make_gr:    %w(decimallatitude decimallongitude countrycode stateprovince county municipality locality coordinateuncertaintyinmeters georeferencedby),
    make_ce:    %w(decimallatitude decimallongitude coordinateuncertaintyinmeters eventdate locationremarks recordedby geodeticdatum countrycode stateprovince county municipality locality)
  }.freeze

  super(args)
end

Instance Attribute Details

- (Object) collecting_events

Returns the value of attribute collecting_events



5
6
7
# File 'lib/batch_load/import/dwca.rb', line 5

def collecting_events
  @collecting_events
end

- (Object) dwca_namespace

Returns the value of attribute dwca_namespace



7
8
9
# File 'lib/batch_load/import/dwca.rb', line 7

def dwca_namespace
  @dwca_namespace
end

- (Object) parser

Returns the value of attribute parser



10
11
12
# File 'lib/batch_load/import/dwca.rb', line 10

def parser
  @parser
end

- (Object) repo

Returns the value of attribute repo



8
9
10
# File 'lib/batch_load/import/dwca.rb', line 8

def repo
  @repo
end

- (Object) row_objects

Returns the value of attribute row_objects



14
15
16
# File 'lib/batch_load/import/dwca.rb', line 14

def row_objects
  @row_objects
end

- (Object) rows

Returns the value of attribute rows



13
14
15
# File 'lib/batch_load/import/dwca.rb', line 13

def rows
  @rows
end

- (Object) tasks_

Returns the value of attribute tasks_



11
12
13
# File 'lib/batch_load/import/dwca.rb', line 11

def tasks_
  @tasks_
end

Instance Method Details

- (Object) _setup (private)

only for use in a TaxonWorks rails console



360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/batch_load/import/dwca.rb', line 360

def _setup
  $project    = Project.where(name: 'BatchLoad Test').first
  $project_id = $project.id
  $user       = User.find(185)
  $user_id    = $user.id
  @root       = Protonym.find_or_create_by(name:       'Root',
                                           rank_class: 'NomenclaturalRank',
                                           parent_id:  nil,
                                           project_id: $project_id)
  @animalia   = Protonym.find_or_create_by(name:       'Animalia',
                                           parent_id:  @root.id,
                                           rank_class: Ranks.lookup(:iczn, :kingdom),
                                           project_id: $project_id)
end

- (Object) build



304
305
306
307
308
309
# File 'lib/batch_load/import/dwca.rb', line 304

def build
  if valid?
    build_dwca
    @processed = true
  end
end

- (Object) build_dwca

process each row for information:



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
231
232
233
234
235
236
237
238
239
240
241
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
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
299
300
301
302
# File 'lib/batch_load/import/dwca.rb', line 83

def build_dwca
  line_counter = 1 # accounting for headers

  tasks = triage(csv.headers, tasks_)
  csv.each do |row|
    @warns                    = []
    @errs                     = []
    @rows[line_counter]       = {}
    @rows[line_counter][:row] = row
    @row_objects              = {}
    tasks.each {|task|
      results            = send(task, row)
      @row_objects[task] = results
      warn               = results[:warn]
      err                = results[:err]
      @errs.push(results.delete(:err)) unless err.blank?
      @warns.push(results.delete(:warn)) unless warn.blank?
    }


    t_n = @row_objects[:make_tn].select {|kee, val| val != nil}.values.first
    # save the (possible new) taxon_name
    t_n.save! if t_n.new_record?
    # associate the taxon_name with the otu
    otu = Otu.find_or_create_by(taxon_name: t_n)
    otu.save! if otu.new_record?
    @row_objects[:make_otu] = otu

    t_d       = @row_objects[:make_td]
    c_o       = @row_objects[:make_co][:c_o]
    idents    = @row_objects[:make_ident]
    id_cat_no = idents[:id_cat_no]
    id_occ_id = idents[:id_occ_id]
    b_a_s     = @row_objects[:make_ba]
    notes     = @row_objects[:make_notes]
    c_o_notes = notes[:c_o]
    c_e_notes = notes[:c_e]
    g_r_notes = notes[:g_r]
    c_e       = @row_objects[:make_ce][:c_e]
    g_r       = @row_objects[:make_gr]
    ppl       = @row_objects[:make_prsn]

    unless c_e.valid?
      test_list = c_e.errors.keys
      if test_list.include?(:start_date_day) or
        test_list.include?(:start_date_month) or
        test_list.include?(:start_date_year) or
        test_list.include?(:end_date_day) or
        test_list.include?(:end_date_month) or
        test_list.include?(:end_date_year)
        @warns.push(c_e.errors.full_messages)
        @warns.push("eventDate (#{c_e.verbatim_date}) does not parse properly.")

        c_e.start_date_day   = nil
        c_e.start_date_month = nil
        c_e.start_date_year  = nil
        c_e.end_date_day     = nil
        c_e.end_date_month   = nil
        c_e.end_date_year    = nil
      end
    end

    if c_e.valid? # check c_e again
      c_e.save if c_e.new_record?
      unless c_e_notes.blank?
        apply_note = false
        c_e_notes.keys.each {|kee|
          c_e_n = c_e_notes[kee]
          if c_e.notes.present?
            c_e.notes.each {|note|
              if note.text == c_e_n.text
                # need to drop the object without warning
                @row_objects[:make_notes][:c_e].delete(:remark)
              else
                apply_note = true
              end
            }
          else
            apply_note = true
          end
          c_e_n.note_object = c_e if apply_note
        }
      end
    else
      @errs.push(c_e.errors.full_messages.flatten)
      # everything else is a fail.
      line_counter = loop_end(line_counter)
      next
    end
    # associate the collection_object with the collecting_event
    c_o.collecting_event = c_e
    # associate the namespace and catalog number with the collection_object
    if id_cat_no.blank?
      @warns.push('Missing catalog number.')
    else
      c_o.identifiers << id_cat_no
    end
    if id_occ_id.blank?
      # is a warning required?
    else
      c_o.identifiers << id_occ_id
    end
    c_o.repository = @repo
    c_o.save!
    # add the possible biological_associations, new? otu to collection_object
    unless b_a_s.blank?
      b_a_s.keys.each {|kee|
        b_a                                = b_a_s[kee]
        b_a.biological_association_subject = c_o
      }
    end
    # add notes to collection_object, if required
    unless c_o_notes.blank?
      c_o_notes.keys.each {|kee|
        c_o_notes[kee].note_object = c_o
      }
    end
    # associate the collection_object with the otu using the taxon_determiniation
    t_d.biological_collection_object = c_o
    t_d.otu                          = otu
    t_d.save!
    # associate the georeference with the collecting_event
    g_r.collecting_event = c_e
    # two different types of georeferences: verbatim, and geolocate
    if g_r.type.include?('Ver')
      # for verbatim_data, generate a new georeference using the collecting_event
      a = c_e.generate_verbatim_data_georeference
      if a.valid?
        # and replace the pre-built georeference
        g_r                    = c_e.georeferences.last
        @row_objects[:make_gr] = g_r
      else
        @warns.push(a.errors.full_messages)
        @row_objects.delete(:make_gr)
      end
    else
      # is a GeoLocate
      unless g_r.valid?
        # georeference was (0,0), will be dropped
        err_txt = 'Georeference::GeoLocate cannot be created, '
        if g_r.errors.messages[:collecting_event_id].present?
          # what to do here?
          @warns.push(err_txt + 'duplicate georeference')
        else
          @warns.push(err_txt + 'lat/long not valid.')
        end
        @row_objects.delete(:make_gr)
        unless g_r_notes.blank?
          @warns.push('Georeference::GeoLocate cannot be created, \'georeferenceRemark\' has been dropped.')
          @row_objects[:make_notes].delete(:g_r)
        end
      end
    end
    # add a georeferencer as required
    g_rer = ppl[:g_r]
    unless g_rer.blank?
      unless g_r.georeferencers.include?(g_rer)
        g_r.georeferencers << g_rer
      end
    end
    # add notes to georeference, if required
    unless g_r_notes.blank?
      apply_note = false
      g_r_notes.keys.each {|kee|
        g_r_n = g_r_notes[kee]
        if g_r.notes.present?
          g_r.notes.each {|note|
            if note.text == g_r_n.text
              # need to drop the object without warning
              @row_objects[:make_notes][:g_r].delete(:remark)
            else
              apply_note = true
            end
          }
        else
          apply_note = true
        end
        g_r_n.note_object = g_r if apply_note
      }
    end
    # either the original, or the ce-created one gets saved
    # g_r.save

    begin # make sure all objects for this row get saved
      @row_objects.keys.each {|kee|
        objects = @row_objects[kee]
        case objects.class.to_s
          when 'Array'
            objects.flatten.each {|object|
              if object.valid?
                object.save
              else
                @errs.push(object.errors.full_messages)
              end
            }
          when 'Hash'
            @errs.push(save_hash(objects))
          else # all other single object classes
            test = objects.try(:valid?)
            case test
              when true
                objects.save
              when false
                @errs.push(objects.errors.full_messages)
              when nil
                @warns.push('No georeference exists.')
            end
        end
      }
      #@warns.push("warning #{line_counter}")
      #@errs.push("error #{line_counter}") if line_counter.even?
      @warns.flatten!
      @errs.flatten!
      # ap@warns
    end
    line_counter = loop_end(line_counter)
    # break if line_counter > 30
  end
  @total_lines = line_counter - 1
end

- (Object) code_to_name(code) (private)



728
729
730
# File 'lib/batch_load/import/dwca.rb', line 728

def code_to_name(code)
  code.nil? ? nil : GeographicArea.where(iso_3166_a2: code).try(:first).try(:name)
end

- (Object) create



311
312
313
# File 'lib/batch_load/import/dwca.rb', line 311

def create

end

- (Object) dump_hash(objects) (private)



327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/batch_load/import/dwca.rb', line 327

def dump_hash(objects)
  objects.keys.each {|kee|
    object = objects[kee]
    unless object.blank?
      if object.class.to_s == 'Hash'
        dump_hash(object)
      else
        object.destroy
      end
    end
  }
end

- (Integer) loop_end(line_counter) (private)

package up the results of the process into a hash

Parameters:

  • current (Integer)

    line_counter/rows key

Returns:

  • (Integer)

    new line_counter/rows key



320
321
322
323
324
325
# File 'lib/batch_load/import/dwca.rb', line 320

def loop_end(line_counter)
  @rows[line_counter][:row_objects] = @row_objects
  @rows[line_counter][:err]         = @errs
  @rows[line_counter][:warn]        = @warns
  line_counter + 1
end

- (Object) make_ba(row) (private)



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/batch_load/import/dwca.rb', line 375

def make_ba(row)
  # for each associatedTaxa, find or create an otu, and creata a biological association for it,
  # connecting it to a collection_object
  retval = {}
  bas    = row['associatedtaxa']
  unless bas.blank?
    taxa = bas.split('|')

    taxa.each {|bio_assoc|
      unless bio_assoc.blank?
        br                = BiologicalRelationship.find_or_create_by(name:       'associated_taxa',
                                                                     project_id: @project_id)
        otu               = Otu.find_or_create_by(name:       bio_assoc,
                                                  project_id: @project_id)
        ba                = BiologicalAssociation.new(biological_relationship:             br,
                                                      biological_association_object:       otu,
                                                      biological_association_object_type:  'Otu',
                                                      biological_association_subject_type: 'CollectionObject')
        retval[bio_assoc] = ba
      end
    }
  end
  retval
end

- (Object) make_ce(row) (private)

a full set of symbolized filter names (kees, below) can be found in Utilities::Dates::REGEXP_DATES.keys



531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
# File 'lib/batch_load/import/dwca.rb', line 531

def make_ce(row)
  ret_val     = {}
  kees        = [:yyyy_mm_dd, :mm_dd_yy]
  d_s         = row['eventdate']
  v_l         = [row['municipality'], row['locality']].select {|name| name.present?}.join(':')
  date_params = {}
  unless d_s.blank?
    trials = Utilities::Dates.hunt_dates(d_s, kees)
    trials.keys.each {|kee|
      trial = trials[kee]
      unless trial.blank?
        case kee
          when kees[0]
            type = kee
          when kees[1]
            type = kee
        end
        sdd, sdm, sdy                  = trial[:start_date_day], trial[:start_date_month], trial[:start_date_year]
        edd, edm, edy                  = trial[:end_date_day], trial[:end_date_month], trial[:end_date_year]
        date_params[:start_date_day]   = sdd unless sdd.blank?
        date_params[:start_date_month] = sdm unless sdm.blank?
        date_params[:start_date_year]  = sdy unless sdy.blank?
        date_params[:end_date_day]     = edd unless edd.blank?
        date_params[:end_date_month]   = edm unless edm.blank?
        date_params[:end_date_year]    = edy unless edy.blank?
        break # bail after the first with a date
      end
    }
  end
  g_a_list = [row['county'], row['stateprovince'], code_to_name(row['countrycode'])]
  g_a_text = g_a_list.select {|name| name.present?}.join(':')
  if g_a_text.blank?
    g_a = nil
  else
    g_a_matches = GeographicArea.matching(g_a_text, true)
    g_a         = g_a_matches[g_a_text].try(:first)
  end
  hunt_params   = {project_id:                       @project_id,
                   geographic_area:                  g_a,
                   verbatim_datum:                   row['geodeticdatum'],
                   verbatim_locality:                v_l,
                   verbatim_date:                    d_s,
                   verbatim_label:                   row['locationremarks'],
                   verbatim_geolocation_uncertainty: row['coordinateuncertaintyinmeters'],
                   verbatim_latitude:                row['decimallatitude'],
                   verbatim_longitude:               row['decimallongitude'],
                   verbatim_collectors:              row['recordedby']}.merge!(date_params)
  c_e           = CollectingEvent.find_or_initialize_by(hunt_params)
  ret_val[:c_e] = c_e
  ret_val.merge!(err: [])
  ret_val.merge!(warn: [])

  ret_val
end

- (Object) make_co(row) (private)

make_co: %w(individualcount organismquantity organismquantitytype) rs.gbif.org/vocabulary/gbif/quantity_type_2015-07-10.xml



592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
# File 'lib/batch_load/import/dwca.rb', line 592

def make_co(row)
  ret_val = {}
  warn    = []
  err     = []
  ic      = row['individualcount']
  oq      = row['organismquantity']
  oqt     = row['organismquantitytype']
  combo   = false
  if ic.present? and (oqt.present? or oq.present?)
    # warn.push('Choose either individualCount or the combination of organismQuantity and organismQuantityType, not both.')
    combo = true
  end
  if (ic.to_i > 1) and combo
    # leave ic alone
    warn.push('individualCount has been used instead of the combination of organismQuantity and organismQuantityType.')
  else
    # set ic according to organismQuantityType
    case oqt
      when 'individuals'
        ic = oq.to_i
      when 'percentageOfSpecies',
        'percentageOfBiovolume',
        'percentageOfBiomass',
        'percentageCoverage',
        'dominScale',
        'braunBlanquetScale',
        'biomassAFDG',
        'biomassG',
        'biomassKg',
        'biovolumeCubicMicrons',
        'biovolumeMl'
        ic = oq
      else
        warn.push("Unrecognized organismQuantityType #{oqt}.")
        ic = oq
    end
  end
  # ic will cause the selection of 'lot' or 'specimen'
  c_o = Specimen.new(total: ic)

  ret_val[:c_o] = c_o
  ret_val.merge!(err: err)
  ret_val.merge!(warn: warn)

  ret_val
end

- (Object) make_gr(row) (private)

available data comes from Tulane geolocation action, reflected by the fact that the georefernce is a GeoLocate



443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# File 'lib/batch_load/import/dwca.rb', line 443

def make_gr(row)
  geo_by = row['georeferencedby']
  # lat, long           = (lat.length > 0) ? lat : nil, (long.length > 0) ? long : nil
  lat, long = row['decimallatitude'], row['decimallongitude']
  if lat.nil? and long.nil?
    uncert = nil
  else
    uncert = row['coordinateuncertaintyinmeters']
  end
  geo_by = '' if geo_by.nil?
  if geo_by.downcase.include?('verbatim')
    # make a verbatim reference
    # fill in what we can, the real work is done when the collecting_event is added
    g_r = Georeference::VerbatimData.new(error_radius: uncert)
  else
    # faking a Georeference::GeoLocate:
    gl_req_params = {country:   code_to_name(row['countrycode']),
                     state:     row['stateprovince'],
                     county:    row['county'],
                     Placename: row['municipality'],
                     Uncert:    uncert,
                     Latitude:  lat,
                     Longitude: long,
                     locality:  row['locality'],
                     gc:        geo_by}.stringify_keys
    req           = Georeference::GeoLocate::RequestUI.new(gl_req_params)
    #   1) new the Georeference, without a collecting_event
    g_r = Georeference::GeoLocate::GeoLocate.find_or_initialize_by(api_request: req.request_params_string,
                                                                   project_id:  @project_id)
    #   2) save the information from the row in request_hash
    if g_r.new_record?
      g_r.api_request = req.request_params_string
      #   3) build a fake iframe response in the form '52.65|-106.333333|3036|Unavailable'
      text = "#{lat}|#{long}|#{uncert}|Unavailable"
      #   4) use that fake to stimulate the parser to create the object
      g_r.iframe_response = text
    end
  end

  g_r
end

- (Object) make_ident(row) (private)



400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# File 'lib/batch_load/import/dwca.rb', line 400

def make_ident(row)
  ident  = {}
  cat_no = row['catalognumber'].split(' ')
  unless cat_no.blank?
    if @namespace.short_name != cat_no[0]
      @errs.push("Namespace (#{cat_no[0]}) does not match import namespace (#{@namespace.short_name}).")
    end
    unless cat_no[1].blank?
      id                = Identifier::Local::CatalogNumber.find_or_initialize_by(namespace:  @namespace,
                                                                                 project_id: @project_id,
                                                                                 identifier: cat_no[1])
      ident[:id_cat_no] = id
    end
  end
  occ_id = row['occurrenceid']
  unless occ_id.blank?
    id                = Identifier::Global::OccurrenceId.find_or_initialize_by(relation:   'skos:exactMatch',
                                                                               project_id: @project_id,
                                                                               identifier: occ_id)
    ident[:id_occ_id] = id
  end
  ident
end

- (Hash) make_notes(row) (private)

Returns of notes where key is object type

Returns:

  • (Hash)

    of notes where key is object type



486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
# File 'lib/batch_load/import/dwca.rb', line 486

def make_notes(row)
  ret_val = {}
  begin
    notes      = {}
    geo_remark = row['georeferenceremarks']
    unless geo_remark.blank?
      notes[:remark] = Note.new(text:       geo_remark,
                                project_id: @project_id)
    end
    ret_val[:g_r] = notes unless notes.blank?
  end

  begin # collecting event notes
    notes      = {}
    loc_remark = row['locationremarks']
    unless loc_remark.blank?
      notes[:remark] = Note.new(text:                  loc_remark,
                                note_object_attribute: :verbatim_locality,
                                project_id:            @project_id)
    end
    ret_val[:c_e] = notes unless notes.blank?
  end

  begin # collection object notes
    notes      = {}
    occ_remark = row['occurrenceremarks']
    unless occ_remark.blank?
      notes[:remark] = Note.new(text:       occ_remark,
                                project_id: @project_id)
    end
    ret_val[:c_o] = notes unless notes.blank?
  end

  ret_val
end

- (Object) make_otu(row) (private)



586
587
588
# File 'lib/batch_load/import/dwca.rb', line 586

def make_otu(row)
  Otu.new
end

- (Object) make_prsn(row) (private)

make_prsn: %w(georeferencedby),



425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'lib/batch_load/import/dwca.rb', line 425

def make_prsn(row)
  ppl = {}
  # check for person's name
  name = row['georeferencedby']
  unless name.blank?
    if name.downcase.include?('verbatim')
    else
      # make a person to be used as a 'georeferencer'
      parsed    = Person.parser(name)
      pr        = Person.find_or_create_by(last_name:  parsed[0]['family'],
                                           first_name: parsed[0]['given'])
      ppl[:g_r] = pr
    end
  end
  ppl
end

- (Object) make_tag(row) (private)



522
523
524
525
526
527
528
# File 'lib/batch_load/import/dwca.rb', line 522

def make_tag(row)
  ret_val = {}
  ret_val.merge!(err: [])
  ret_val.merge!(warn: [])

  ret_val
end

- (Object) make_td(row) (private)

make_td: %w(scientificname basisofrecord individualcount organismquantity organismquantitytype recordedby)



724
725
726
# File 'lib/batch_load/import/dwca.rb', line 724

def make_td(row)
  TaxonDetermination.new
end

- (Object) make_tn(row) (private)

make_tn: %w(scientificname taxonrank family kingdom)



640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
# File 'lib/batch_load/import/dwca.rb', line 640

def make_tn(row)
  ret_val      = {species: nil, genus: nil, tribe: nil}
  this_kingdom = row['kingdom']
  if @kingdom.try(:name) != this_kingdom
    @kingdom = Protonym.find_or_create_by(name:       this_kingdom,
                                          rank_class: Ranks.lookup(:iczn, :kingdom),
                                          project_id: @project_id)

    if @kingdom.new_record?
      @kingdom.parent = @root
      @kingdom.save!
      ret_val[:kingdom] = @kingdom
    end
  end

  this_family = row['family']
  if @family.try(:name) != this_family
    @family = Protonym.find_or_create_by(name:       this_family,
                                         rank_class: Ranks.lookup(:iczn, :family),
                                         project_id: @project_id)
    if @family.new_record?
      @family.parent = @kingdom
      @family.save!
      ret_val[:family] = @family
    end
  end
  sn  = row['scientificname']
  snp = @parser.parse(sn)

  # find or create Protonym based on exact match of row['scientificname'] and taxon_names.cached

  t_n       = Protonym.find_or_create_by(cached: snp[:scientificName][:canonical], project_id: @project_id)
  this_rank = row['taxonrank'].downcase.to_sym

  if t_n.new_record?
    case this_rank
      when :species
        begin # find or create genus
          genus_name = snp[:scientificName][:details][0][:genus][:string]
          @genus     = Protonym.find_or_create_by(name:       genus_name,
                                                  parent:     @family,
                                                  rank_class: Ranks.lookup(:iczn, :genus),
                                                  project_id: @project_id)
          if @genus.new_record?
            @genus.save!
            ret_val[:new_genus] = @genus
          end
        end
        species        = snp[:scientificName][:details][0][:species]
        t_n.parent     = @genus
        t_n.rank_class = Ranks.lookup(:iczn, :species)
        t_n.name       = species[:string]
        # t_n.cached_author_year = snp[:scientificName][:details][0][:species][:authorship]
        t_n.year_of_publication = species[:basionymAuthorTeam][:year].to_i
        author_name             = species[:basionymAuthorTeam][:authorTeam]
        if species[:authorship].include?('(')
          author_name = "(#{author_name})"
        end
        t_n.verbatim_author = author_name
        ret_val[:species]   = t_n
      when :genus
        genus           = snp[:scientificName][:details][0][:uninomial]
        t_n.parent      = @family
        t_n.rank_class  = Ranks.lookup(:iczn, :genus)
        t_n.name        = genus[:string]
        ret_val[:genus] = t_n
      when :tribe
        tribe           = snp[:scientificName][:details][0][:uninomial]
        t_n.parent      = @family
        t_n.rank_class  = Ranks.lookup(:iczn, :tribe)
        t_n.name        = tribe[:string]
        ret_val[:tribe] = t_n
      else
        raise "Unknown taxonRank #{this_rank}."
    end
    # t_n.create_otu
  else
    ret_val[this_rank] = t_n
  end

  ret_val
end

- (Object) preview_dwca



45
46
47
48
49
# File 'lib/batch_load/import/dwca.rb', line 45

def preview_dwca
  @preview_table = {}

  @preview_table
end

- (Object) save_hash(objects) (private)



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/batch_load/import/dwca.rb', line 340

def save_hash(objects)
  l_errs = []
  objects.keys.each {|kee|
    object = objects[kee]
    unless object.blank?
      if object.class.to_s == 'Hash'
        l_errs.push(save_hash(object))
      else
        if object.valid?
          object.save
        else
          l_errs.push(object.errors.full_messages)
        end
      end
    end
  }
  l_errs.flatten
end

- (Array) triage(headers, tasks) (private)

intersection of the word list from the tasks hash (per key) and the list of headers

Parameters:

  • of (Array)

    Strings which represent the TSV file headers

  • of (Hash)

    the method names (as keys) for the tasks, with lists of required headers (as values)

Returns:

  • (Array)

    of named tasks to perform, based on the presents of task's word list in the header list



747
748
749
# File 'lib/batch_load/import/dwca.rb', line 747

def triage(headers, tasks)
  tasks.select {|kee, vlu| (vlu & headers) == vlu}.keys
end