Module: Export::Coldp::Files::Name

Defined in:
lib/export/coldp/files/name.rb

Overview

The names table includes

  • All name strings, even if hanging (= not attached to OTUs/Taxa)

  • It contains strings that may be invalid or valid

Future considerations

- see bottom, should we parameterize a CSV row add, is it performat?, it
would let us DRY all csv << , but it's also not dry to parameterize.
Probably OK as is.

TODO:

- [ ] refactor ref additions so that they happen at the aggregate level
- [ ] resolve the `.length` issue, what the heck is that needed for [see comment from Tom]

Constant Summary collapse

MANIFEST =
[
  :valid_higher_names,
  :valid_family_names,
  :core_names,
  :combination_names,
  :original_combination_names,
  :invalid_family_and_higher_names,
  :invalid_core_names,
  :invalid_original_combination_names,
]

Class Method Summary collapse

Class Method Details

.add_combination_names(otu, csv, project_members, reference_csv) ⇒ Object



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
521
522
523
524
525
526
527
528
529
530
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
# File 'lib/export/coldp/files/name.rb', line 493

def self.add_combination_names(otu, csv, project_members, reference_csv)
  names = combination_names(otu)
  names.length

  names.each do |row| # row is a Hash, not a ActiveRecord object

    # At this point all formatting (gender) is done
    elements = Combination.full_name_hash_from_row(row)

    # NOT POSSIBLE?! Combination can't have infraspecific in UI
    infraspecies, rank = Utilities::Nomenclature.infraspecies(elements)

    rank = elements.keys.last if rank.nil?

    # Decide whether or not to skip this Combination
    #   * Is it identical to the current placement?
    #      * Yes
    #         * Is the current placement an "inferred combination"?
    #             * Yes - include
    #             * No - skip
    #      * No - include
    #
    if (row[rank + "_cached"] == row['cached'])  # it is a dupe

      if  row[rank + '_cached_is_valid']         # it is valid
        ::Export::Coldp.skipped_combinations << row['id']
        next
      end

    # # it is not valid
    # # it *is* referencing an inferred combination
    #   if !row[rank + '_inferred_combination']
    #     ::Export::Coldp.skipped_combinations << row['id']
    #     next
    #   end
    end

    scientific_name = ::Utilities::Nomenclature.unmisspell_name(row['cached'])

    uninomial = scientific_name if rank == 'genus'

    csv << [
      row['id'],                                                          # ID
      nil,                                                                # basionymID
      scientific_name,                                                    # scientificName
      row['cached_author_year'],                                          # authorship
      rank,                                                               # rank
      uninomial,                                                          # uninomial   <- if genus group only (i.e. incomplete Combination)
      elements['genus']&.last,                                            # genus
      elements['subgenus']&.last,                                         # subgenus (no parens)
      elements['species']&.last,                                          # species
      infraspecies,                                                       # infraspecificEpithet
      row['source_id'],                                                   # publishedInID
      row['pages'],                                                       # publishedInPage
      row['cached_nomenclature_date']&.year,                              # publishedInYear
      code_field(row['reference_rank_class']),                            # code
      nil,                                                                # nomStatus (nil for Combination)
      nil,                                                                # etymology
      nil,                                                                # gender
      nil,                                                                # link (probably TW public or API)
      Export::Coldp.sanitize_remarks(row['id']),                          # remarks
      Export::Coldp.modified(row['updated_at']),                          # modified
      Export::Coldp.modified_by(row[:updated_by_id], project_members)     # modifiedBy
    ]
  end

  if reference_csv
    Source.with(names: names.where(citations: { is_original: true}))
      .joins('JOIN names n on n.source_id = sources.id')
      .find_each do |s|
        Export::Coldp::Files::Reference.add_reference_rows([s].compact, reference_csv, project_members)
      end
  end
end

.add_core_names(otu, csv, project_members, reference_csv) ⇒ Object



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/export/coldp/files/name.rb', line 597

def self.add_core_names(otu, csv, project_members, reference_csv)
  names = core_names(otu)
  names.length

  names.find_each do |t|

    origin_citation = t.origin_citation
    basionym_id = t.id # by defintion
    uninomial = t.cached if t.rank == 'genus'

    # Future- resolve in SQL perhaps, though not very expensive here
    species = align_gender(t, :species)
    infraspecies = align_gender(t, :infraspecies)

    csv << [
      t.id,                                                               # ID
      basionym_id,                                                        # basionymID
      t.cached,                                                           # scientificName  # should just be t.cached
      t.cached_author_year,                                               # authorship
      t.rank,                                                             # rank
      uninomial,                                                          # uninomial   <- if genus here
      t.genus,                                                            # genus and below - IIF species or lower # TODO: confirm this is OK now
      t.subgenus,                                                         # infragenericEpithet (subgenus)
      species,                                                            # specificEpithet
      infraspecies,                                                       # infraspecificEpithet
      origin_citation&.source_id,                                         # publishedInID
      origin_citation&.pages,                                             # publishedInPage
      t.cached_nomenclature_date&.year,                                   # publishedInYear
      code_field(t.rank_class.name),                                      # code
      ::TaxonName::NOMEN_VALID[t.rank_class.name.to_sym],                 # nomStatus # TODO: untested
      nil,                                                                # etymology
      nil,                                                                # gender
      nil,                                                                # link (probably TW public or API)
      Export::Coldp.sanitize_remarks(t.id),                               # remarks
      Export::Coldp.modified(t[:updated_at]),                             # modified
      Export::Coldp.modified_by(t[:updated_by_id], project_members)       # modifiedBy
    ]

    Export::Coldp::Files::Reference.add_reference_rows([origin_citation.source].compact, reference_csv, project_members) if reference_csv && origin_citation
  end
end

.add_invalid_core_names(otu, csv, project_members, reference_csv) ⇒ Object



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
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
# File 'lib/export/coldp/files/name.rb', line 693

def self.add_invalid_core_names(otu, csv, project_members, reference_csv)
  names = invalid_core_names(otu)

  classification_status = taxon_name_classification_status(names)
  relationship_status = taxon_name_relationship_status(names)

  names.length # !! TODO: without this the result is truncated, why!?

  names.find_each do |t|

    origin_citation = t.origin_citation

    scientific_name = t.cached_misspelling ? ::Utilities::Nomenclature.unmisspell_name(t.cached) : t.cached

    uninomial = scientific_name if t.rank == 'genus'

    nom_status = nomenclatural_status(t.id, classification_status, relationship_status)

    csv << [
      t.id,                                                               # ID
      nil,                                                                # basionymID
      scientific_name,                                                    # scientificName  # should just be t.cached
      t.cached_author_year,                                               # authorship
      t.rank,                                                             # rank
      uninomial,                                                          # uninomial   <- if genus here
      t.genus,                                                            # genus and below - IIF species or lower
      t.subgenus,                                                         # infragenericEpithet (subgenus)
      t.species,                                                          # specificEpithet
      t.infraspecies,                                                     # infraspecificEpithet
      origin_citation&.source_id,                                         # publishedInID
      origin_citation&.pages,                                             # publishedInPage
      t.cached_nomenclature_date&.year,                                   # publishedInYear
      code_field(t.rank_class.name),                                      # code
      nom_status,                                                         # nomStatus
      nil,                                                                # etymology
      nil,                                                                # gender
      nil,                                                                # link (probably TW public or API)
      Export::Coldp.sanitize_remarks(t.id),                               # remarks
      Export::Coldp.modified(t[:updated_at]),                             # modified
      Export::Coldp.modified_by(t[:updated_by_id], project_members)       # modifiedBy
    ]

    Export::Coldp::Files::Reference.add_reference_rows([origin_citation.source].compact, reference_csv, project_members) if reference_csv && origin_citation
  end
end

.add_invalid_family_and_higher_names(otu, csv, project_members, reference_csv) ⇒ Object



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
# File 'lib/export/coldp/files/name.rb', line 651

def self.add_invalid_family_and_higher_names(otu, csv, project_members, reference_csv)

  names = invalid_family_and_higher_names(otu)

  classification_status = taxon_name_classification_status(names)
  relationship_status = taxon_name_relationship_status(names)

  names.length # !! TODO: without this the result is truncated, why!?

  names.find_each do |t|
    origin_citation = t.origin_citation

    nom_status = nomenclatural_status(t.id, classification_status, relationship_status)

    csv << [
      t.id,                                                             # ID
      nil,                                                              # basionymID
      t.name,                                                           # scientificName
      t.cached_author_year,                                             # authorship
      t.rank,                                                           # rank
      t.name,                                                           # uninomial
      nil,                                                              # genus and below - IIF species or lower
      nil,                                                              # infragenericEpithet (subgenus)
      nil,                                                              # specificEpithet
      nil,                                                              # infraspecificEpithet
      origin_citation&.source_id,                                       # publishedInID
      origin_citation&.pages,                                           # publishedInPage
      t.cached_nomenclature_date&.year,                                 # publishedInYear
      code_field(t.rank_class.name),                                    # code
      nom_status,                                                       # nomStatus
      nil,                                                              # etymology
      nil,                                                              # gender
      nil,                                                              # link (probably TW public or API)
      Export::Coldp.sanitize_remarks(t.id),                             # remarks
      Export::Coldp.modified(t[:updated_at]),                           # modified
      Export::Coldp.modified_by(t[:updated_by_id], project_members)     # modifiedBy
    ]

    Export::Coldp::Files::Reference.add_reference_rows([origin_citation.source].compact, reference_csv, project_members) if reference_csv && origin_citation
  end
end

.add_invalid_original_combination_names(otu, csv, project_members, reference_csv) ⇒ Object

May have to split out misspellings from other invalid names to handle parens



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
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
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/export/coldp/files/name.rb', line 263

def self.add_invalid_original_combination_names(otu, csv, project_members, reference_csv)

  # !!
  # !! Remember, the names here are loaded with values FROM THEIR ORIGINAL COMBINATIONS
  # !!
  names = invalid_original_combination_names(otu)
  names.length

  names.find_each do |row|
    # At this point all formatting (gender) is done
    elements = Protonym.original_combination_full_name_hash_from_flat(row)

    infraspecies, rank = Utilities::Nomenclature.infraspecies(elements)
    rank = 'forma' if rank == 'form' # CoL preferred string

    # Hmm- why needed?
    rank = elements.keys.last if rank.nil? # Note that this depends on order of Hash creation

    # Generic names can also be misspelled so, just process everything, don't bother checking the flag
    scientific_name = ::Utilities::Nomenclature.unmisspell_name(row['cached_original_combination'])

    # TODO: resolve/verify needed
    uninomial = scientific_name if rank == 'genus'

    # !! Ideally we de-reify these names in the query with (cached != cached_original_combination)
    # !! SO that we know these *must* be reified
    # !! We are reifying *without* "[sic]" in the string
    id = ::Utilities::Nomenclature.reified_id(row['id'], scientific_name)

    # By definition - for invalid names, the basionym points to itself (the reified original combination)
    basionym_id = id

    # !! g-maculata
    # if id == '1093903-f91ea42436b0bbf9a5115437e67afc27'
    #   byebug
    #   foo = 1
    # end

    # A major brain-@#$@#.
    # Original combinations of misspelled names
    # CAN have parenthesis in their rendering
    # if the original genus is different
    # THAN THE GENUS OF THE PROPERLY SPELLED VERSION OF THE NAME

    # Remember, 'genus' is `original_genus`, so
    # by the fact that synonyms are under the same parent
    # as that of the valid name we are comparing the
    # placement of the properly spelled version of the name.

    author_year = row['cached_author_year']
    author_year = author_year.delete('()') if strip_parens_for_author_year?(row)

    csv << [
      id,                                                                 # ID
      basionym_id,                                                        # basionymID
      scientific_name,                                                    # scientificName
      author_year,                                                        # authorship
      rank,                                                               # rank
      uninomial,                                                          # uninomial
      elements['genus']&.last,                                            # genus
      elements['subgenus']&.last,                                         # subgenus (no parens)
      elements['species']&.last,                                          # species
      infraspecies,                                                       # infraspecificEpithet
      row['source_id'],                                                   # referenceID
      row['pages'],                                                       # publishedInPage
      row['cached_nomenclature_date']&.year,                              # publishedInYear - OK
      code_field(row['reference_rank_class']),                            # code
      nil,                                                                # status https://api.checklistbank.org/vocab/nomStatus
      nil,                                                                # etymology
      nil,                                                                # gender
      nil,                                                                # link (probably TW public or API)
      nil,                                                                # remarks (we have no way to capture this in TW)
      Export::Coldp.modified(row[:updated_at]),                           # modified
      Export::Coldp.modified_by(row[:updated_by_id], project_members)     # modifiedBy
    ]

    # !! We do not need to add a reference here because it is the same as the corresponding Protonym id
  end
end

.add_original_combination_names(otu, csv, project_members, reference_csv) ⇒ Object



350
351
352
353
354
355
356
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
402
403
# File 'lib/export/coldp/files/name.rb', line 350

def self.add_original_combination_names(otu, csv, project_members, reference_csv)
  names = original_combination_names(otu)
  names.length

  names.find_each do |row|
    # At this point all formatting (gender) is done
    elements = Protonym.original_combination_full_name_hash_from_flat(row)

    infraspecies, rank = Utilities::Nomenclature.infraspecies(elements)
    rank = 'forma' if rank == 'form' # CoL preferred string

    # Hmm- why needed?
    rank = elements.keys.last if rank.nil? # Note that this depends on order of Hash creation

    scientific_name = ::Utilities::Nomenclature.unmisspell_name(row['cached_original_combination'])

    # TODO: resolve/verify needed
    uninomial = scientific_name if rank == 'genus'

    # !! Ideally we de-reify these names in the query with (cached != cached_original_combination)
    # !! So that we know these *must* be reified
    # !! We are reifieing *without* "[sic]" in the string
    id = ::Utilities::Nomenclature.reified_id(row['id'], scientific_name)

    # By definition
    basionym_id = row['id']

    csv << [
      id,                                                                 # ID
      basionym_id,                                                        # basionymID
      scientific_name,                                                    # scientificName
      row['cached_author_year'].gsub(/[\(\)]/, ''),                       # authorship  # TODO <- stripping author/year here
      rank,                                                               # rank
      uninomial,                                                          # uninomial
      elements['genus']&.last,                                            # genus
      elements['subgenus']&.last,                                         # subgenus (no parens)
      elements['species']&.last,                                          # species
      infraspecies,                                                       # infraspecificEpithet
      row['source_id'],                                                   # referenceID
      row['pages'],                                                       # publishedInPage
      row['cached_nomenclature_date']&.year,                              # publishedInYear - OK
      code_field(row['reference_rank_class']),                            # code
      nil,                                                                # status https://api.checklistbank.org/vocab/nomStatus
      nil,                                                                # etymology
      nil,                                                                # gender
      nil,                                                                # link (probably TW public or API)
      nil,                                                                # remarks (we have no way to capture this in TW)
      Export::Coldp.modified(row[:updated_at]),                           # modified
      Export::Coldp.modified_by(row[:updated_by_id], project_members)     # modifiedBy
    ]

    # !! We do not need to add a reference here because it is the same as the corresponding Protonym id
  end
end

.add_valid_family_names(otu, csv, project_members, reference_csv) ⇒ Object



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
437
# File 'lib/export/coldp/files/name.rb', line 405

def self.add_valid_family_names(otu, csv, project_members, reference_csv)
  names = valid_family_names(otu)
  names.length

  names.find_each do |t|
    origin_citation = t.origin_citation
    csv << [
      t.id,                                                               # ID
      nil,                                                                # basionymID
      t.cached,                                                           # scientificName  # should just be t.cached
      t.cached_author_year,                                               # authorship
      t.rank,                                                             # rank
      t.cached,                                                           # uninomial
      nil,                                                                # genus and below - IIF species or lower
      nil,                                                                # infragenericEpithet (subgenus)
      nil,                                                                # specificEpithet
      nil,                                                                # infraspecificEpithet
      origin_citation&.source_id,                                         # publishedInID
      origin_citation&.pages,                                             # publishedInPage
      t.cached_nomenclature_date&.year,                                   # publishedInYear
      code_field(t.rank_class.name),                                      # code
      ::TaxonName::NOMEN_VALID[t.rank_class.name.to_sym],                 # nomStatus
      nil,                                                                # etymology
      nil,                                                                # gender
      nil,                                                                # link (probably TW public or API)
      Export::Coldp.sanitize_remarks(t.id),                               # remarks
      Export::Coldp.modified(t[:updated_at]),                             # modified
      Export::Coldp.modified_by(t[:updated_by_id], project_members)       # modifiedBy
    ]

    Export::Coldp::Files::Reference.add_reference_rows([origin_citation.source].compact, reference_csv, project_members) if reference_csv && origin_citation
  end
end

.add_valid_higher_names(otu, csv, project_members, reference_csv) ⇒ Object

TODO: could select less, don’t need ‘cached’, just name?



440
441
442
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
# File 'lib/export/coldp/files/name.rb', line 440

def self.add_valid_higher_names(otu, csv, project_members, reference_csv)
  names = valid_higher_names(otu)

  names.length
  names.find_each do |t|

    # TODO: isolate/refine
    origin_citation = t.origin_citation

    csv << [
      t.id,                                                               # ID
      nil,                                                                # basionymID
      t.cached,                                                           # scientificName  # should just be t.name?
      t.cached_author_year,                                               # authorship
      t.rank,                                                             # rank
      t.cached,                                                           # uninomial
      nil,                                                                # genus and below - IIF species or lower
      nil,                                                                # infragenericEpithet (subgenus)
      nil,                                                                # specificEpithet
      nil,                                                                # infraspecificEpithet
      origin_citation&.source_id,                                         # publishedInID
      origin_citation&.pages,                                             # publishedInPage
      t.cached_nomenclature_date&.year,                                   # publishedInYear
      code_field(t.rank_class.name),                                      # code
      ::TaxonName::NOMEN_VALID[t.rank_class.name.to_sym],                 # nomStatus
      nil,                                                                # etymology
      nil,                                                                # gender
      nil,                                                                # link (probably TW public or API)
      Export::Coldp.sanitize_remarks(t.id),                               # remarks
      Export::Coldp.modified(t[:updated_at]),                             # modified
      Export::Coldp.modified_by(t[:updated_by_id], project_members)       # modifiedBy
    ]

    Export::Coldp::Files::Reference.add_reference_rows([origin_citation.source].compact, reference_csv, project_members) if reference_csv && origin_citation
  end
end

.align_gender(core_name, rank = :species) ⇒ Object

Return, based on the gender of the genus, the element at the rank requested. Note infraspecies is a “fake” rank that is combined data here. This is in part because CoL only handles trinomials.

Parameters:

  • rank (defaults to: :species)
    • the element of the name requested



575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
# File 'lib/export/coldp/files/name.rb', line 575

def self.align_gender(core_name, rank = :species)
  if g = core_name.genus_gender # there is a name at this rank and we can work with the gender

    case core_name.rank
    when 'species'
      case rank
      when :species
        core_name.send( (g + '_name').to_sym )
      else
        nil
      end
    when 'subspecies', 'form', 'variety' # See compression in core_names, may be an issue
      case rank
      when :species
        core_name.send( "species_#{g}_name".to_sym )
      when :infraspecies
        core_name.send( (g + '_name').to_sym )
      end
    end
  end
end

.code_field(rank_class) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/export/coldp/files/name.rb', line 27

def self.code_field(rank_class)
  return 'ICZN' if rank_class =~ /Iczn/
  return 'ICNP' if rank_class =~ /Icnp/
  return 'ICVCN' if rank_class =~ /Icvcn/
  return 'ICN' if rank_class =~ /Icn/
  nil
end

.combination_names(otu) ⇒ Object

Combinations

 See also self.core_names
- Potential TODO: a-typical verbatim_names (though perhaps OK)
  - If not OK, then simply provide verbatim_name *without* genus, subgenus, species fields


482
483
484
485
486
487
488
489
490
491
# File 'lib/export/coldp/files/name.rb', line 482

def self.combination_names(otu)
  a = otu.taxon_name.self_and_descendants.unscope(:order).select(:id)

  b = Combination
    .where('taxon_names.verbatim_name is null OR taxon_names.verbatim_name = taxon_names.cached') # !! verbatim_name when present is used in cached?! if so this isn't correct!
    .flattened
    .with(project_scope: a)
    .complete
    .joins('JOIN project_scope ps on ps.id = taxon_names.cached_valid_taxon_name_id') # Combinations that point to any of "a"
end

.core_names(otu) ⇒ Object

Core names are:

- Protonyms
- Valid
- Genus or species group

They are NOT

- "inferred combinations" - We have consciously excluded inferred combinations (sensu Browse TaxonNames) from the result set.

If you wish to include an inferred combination then you must create an equivalent (subsequent) Combination.



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
# File 'lib/export/coldp/files/name.rb', line 73

def self.core_names(otu)

  # TODO: adding .that_is_valid increases names, why? Are we hitting duplicates?
  a = otu.taxon_name.self_and_descendants.unscope(:order).select(:id)

  # b sets up the query that aggregates the different ranks in one row
  b = ::Protonym.is_species_or_genus_group.with(valid_scope: a)
    .where(cached_is_valid: true)
    .joins('JOIN valid_scope on valid_scope.id = taxon_names.cached_valid_taxon_name_id')

  select = 'taxon_names.id,'
  select << ::NomenclaturalRank.rank_expansion_sql(ranks: %w{genus subgenus species}, nomenclatural_code: otu.taxon_name.nomenclatural_code)

  # We group some ranks here
  # TODO: This doesn't likely actually work in the case of subspecies + <some other rank> ?
  select << ", MAX(CASE WHEN parent.rank_class LIKE \'%::Subspecies\' OR parent.rank_class LIKE \'%::Variety\' OR parent.rank_class LIKE \'%::Form\' THEN parent.name END) AS infraspecies"

  b = b.select(select)
    .joins('INNER JOIN taxon_name_hierarchies ON taxon_names.id = taxon_name_hierarchies.descendant_id')
    .joins('LEFT JOIN taxon_names AS parent ON parent.id = taxon_name_hierarchies.ancestor_id')
    .group('taxon_names.id')

  c = ::TaxonName.with(n: b)
    .joins('JOIN n on n.id = taxon_names.id')
    .where(cached_is_valid: true)
    .eager_load(origin_citation: [:source])
    .select('taxon_names.*, n.genus, n.subgenus, n.species, n.infraspecies, n.genus_gender, n.species_masculine_name, n.species_neuter_name, n.species_feminine_name')
end

.generate(otu, project_members, reference_csv = nil) ⇒ Object



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
# File 'lib/export/coldp/files/name.rb', line 137

def self.generate(otu, project_members, reference_csv = nil)
  name_total = 0

  # We should not be setting this here !!
  project_id = otu.project_id

  # TODO: scope this to name_remarks, keep internal
  if predicate_id = Predicate.find_by(
      uri: 'https://github.com/catalogueoflife/coldp#Name.remarks',
      project_id:)&.id

    ::Export::Coldp.remarks = ::Export::Coldp.get_remarks(otu.taxon_name.self_and_descendants, predicate_id)
  end

  # genderAgreement boolean is ultimately derivable from TaxonNameClassification::Gender

  output = ::CSV.generate(col_sep: "\t") do |csv|
    csv << %w{
      ID
      basionymID
      scientificName
      authorship
      rank
      uninomial
      genus
      infragenericEpithet
      specificEpithet
      infraspecificEpithet
      referenceID
      publishedInPage
      publishedInYear
      code
      status
      etymology
      gender
      link
      remarks
      modified
      modifiedBy
    }

    MANIFEST.each do |m|
      send( ('add_' + m.to_s).to_sym, otu, csv, project_members, reference_csv)
    end

    true
  end
end

.invalid_core_names(otu) ⇒ Object



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
# File 'lib/export/coldp/files/name.rb', line 102

def self.invalid_core_names(otu)
  a = otu.taxon_name.self_and_descendants.unscope(:order).select(:id)

  b = ::Protonym
    .with(valid_scope: a)
    .joins('JOIN valid_scope on valid_scope.id = taxon_names.cached_valid_taxon_name_id')
    .is_species_or_genus_group
    .where(cached_is_valid: false)
    .where('((taxon_names.cached = taxon_names.cached_original_combination) OR (taxon_names.cached_original_combination IS NULL))')
    .and(TaxonName.where.not("taxon_names.rank_class like '%::Iczn::Family%' AND taxon_names.cached_is_valid = FALSE"))

  select = 'taxon_names.id,'
  select << ::NomenclaturalRank.rank_expansion_sql(ranks: %w{genus subgenus species}, nomenclatural_code: otu.taxon_name.nomenclatural_code)

  # We group some ranks here
  # TODO: This doesn't likely actually work in the case of subspecies + <some other rank> ?
  select << ", MAX(CASE WHEN parent.rank_class LIKE \'%::Subspecies\' OR parent.rank_class LIKE \'%::Variety\' OR parent.rank_class LIKE \'%::Form\' THEN parent.name END) AS infraspecies"

  b = b

  b = b.select(select)
    .joins('INNER JOIN taxon_name_hierarchies ON taxon_names.id = taxon_name_hierarchies.descendant_id')
    .joins('LEFT JOIN taxon_names AS parent ON parent.id = taxon_name_hierarchies.ancestor_id')
    .group('taxon_names.id')
    .eager_load(origin_citation: [:source])

  c = ::TaxonName.with(n: b)
    .joins('JOIN n on n.id = taxon_names.id')
    .where(cached_is_valid: false) # redundant
    .eager_load(origin_citation: [:source])
    .select('taxon_names.*, n.genus, n.subgenus, n.species, n.infraspecies')
end

.invalid_family_and_higher_names(otu) ⇒ Object

Invalid family/higher names



209
210
211
212
213
214
215
216
217
218
# File 'lib/export/coldp/files/name.rb', line 209

def self.invalid_family_and_higher_names(otu)
  a = otu.taxon_name.self_and_descendants
    .where(
      type: 'Protonym',
      rank_class: FAMILY_RANK_NAMES + HIGHER_RANK_NAMES,
      cached_is_valid: false)
    .unscope(:order)
    .eager_load(origin_citation: [:source]) # TODO, just source_id, and pages
    .select(:id, :name, :type, :cached_author_year, :cached_nomenclature_date, :rank_class, :cached_is_valid, :cached_valid_taxon_name_id, :updated_at, :updated_by_id) # cached has sic
end

.invalid_original_combination_names(otu) ⇒ Object

Invalid original combinations are:

- Species or genus group names
- invalid names (not valid)
- names with original combinations set
- names where cached != original_combination, i.e. it needs re-ification

These are the original combinations for invalid protonyms that need reified IDs



247
248
249
250
251
252
253
254
255
256
257
# File 'lib/export/coldp/files/name.rb', line 247

def self.invalid_original_combination_names(otu)
  a = otu.taxon_name.self_and_descendants.unscope(:order).select(:id)

  b = Protonym
    .original_combination_specified
    .original_combinations_flattened
    .with(valid_scope: a)
    .where(cached_is_valid: false)
    .where('taxon_names.cached != taxon_names.cached_original_combination') # Only reified!!
    .joins('JOIN valid_scope on valid_scope.id = taxon_names.cached_valid_taxon_name_id')
end

.nomenclatural_status(taxon_name_id, classification_status = [], relationship_status = []) ⇒ Object

Parameters:

  • classification_status (defaults to: [])

    Array

  • relationship_status (defaults to: [])

    Array



641
642
643
644
645
646
647
648
649
# File 'lib/export/coldp/files/name.rb', line 641

def self.nomenclatural_status(taxon_name_id, classification_status = [], relationship_status = [])
  # Always prefer  a classification, regardless of age
  a = classification_status.bsearch{|i| i['taxon_name_id'] >= taxon_name_id}
  return a['type'].safe_constantize::NOMEN_URI if !a.blank? && a['taxon_name_id'] == taxon_name_id # binary is first >=
  b = relationship_status.bsearch{|i| i['subject_taxon_name_id'] >= taxon_name_id}
  return nil if b.blank? || b['subject_taxon_name_id'] != taxon_name_id
  return b['type'].safe_constantize::NOMEN_URI unless b.blank?
  nil
end

.original_combination_names(otu) ⇒ Object

Valid original combinations are:

- species or genus group names
- valid names
- names with original combinations set
- names where cached != original_combination, i.e. it needs re-ification

As a test these should parse correctly in the Biodiversity wrapper as of 4/8/2025.



228
229
230
231
232
233
234
235
236
237
# File 'lib/export/coldp/files/name.rb', line 228

def self.original_combination_names(otu)
  a = core_names(otu)

  b = Protonym
    .original_combination_specified
    .original_combinations_flattened
    .with(project_scope: a)
    .where('taxon_names.cached != taxon_names.cached_original_combination') # Only reified!!
    .joins('JOIN project_scope ps on ps.id = taxon_names.id')
end

.strip_parens_for_author_year?(row) ⇒ Boolean

Returns:

  • (Boolean)


343
344
345
346
347
348
# File 'lib/export/coldp/files/name.rb', line 343

def self.strip_parens_for_author_year?(row)
  return true unless row['cached_misspelling']
  return true if row['genus'].nil?

  row['cached'] =~ /\A#{Regexp.escape(row['genus'])}\b/
end

.taxon_name_classification_status(scope) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/export/coldp/files/name.rb', line 35

def self.taxon_name_classification_status(scope)
  c = TaxonNameClassification.with(invalid_names: scope.select('taxon_names.id invalid_id'))
    .joins('JOIN invalid_names ON invalid_names.invalid_id = taxon_name_classifications.taxon_name_id')
    .joins("LEFT JOIN citations c on c.citation_object_id = taxon_name_classifications.id and c.citation_object_type = 'TaxonNameClassification'")
    .joins('LEFT JOIN sources s on s.id = c.source_id')
    .select('taxon_name_classifications.taxon_name_id, s.cached_nomenclature_date,
            MAX(taxon_name_classifications.type) AS type,
            MAX(s.cached_nomenclature_date) as date')
    .group('taxon_name_classifications.taxon_name_id, s.cached_nomenclature_date, taxon_name_classifications.type')
    .order('taxon_name_classifications.taxon_name_id, s.cached_nomenclature_date')

  ApplicationRecord.connection.execute(c.to_sql).to_a
end

.taxon_name_relationship_status(scope) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/export/coldp/files/name.rb', line 49

def self.taxon_name_relationship_status(scope)
  c = TaxonNameRelationship.with(invalid_names: scope.select('taxon_names.id invalid_id'))
    .joins('JOIN invalid_names ON invalid_names.invalid_id = taxon_name_relationships.subject_taxon_name_id')
    .joins("LEFT JOIN citations c on c.citation_object_id = taxon_name_relationships.id and c.citation_object_type = 'TaxonNameClassification'")
    .joins('LEFT JOIN sources s on s.id = c.source_id')
    .where('taxon_name_relationships.object_taxon_name_id = invalid_names.cached_valid_taxon_name_id')
    .where(taxon_name_relationships: {type: TAXON_NAME_RELATIONSHIP_NAMES_SYNONYM})
    .select('taxon_name_relationships.subject_taxon_name_id, s.cached_nomenclature_date,
            MAX(taxon_name_relationships.type) AS type,
            MAX(s.cached_nomenclature_date) as date')
    .group('taxon_name_relationships.subject_taxon_name_id, s.cached_nomenclature_date, taxon_name_relationships.type')
    .order('taxon_name_relationships.subject_taxon_name_id, s.cached_nomenclature_date')

  ApplicationRecord.connection.execute(c.to_sql).to_a
end

.valid_family_names(otu) ⇒ Object

Valid family names are:

- valid family group names


200
201
202
203
204
205
206
# File 'lib/export/coldp/files/name.rb', line 200

def self.valid_family_names(otu)
  a = otu.taxon_name.self_and_descendants
    .where(rank_class: FAMILY_RANK_NAMES, cached_is_valid: true)
    .unscope(:order)
    .eager_load(origin_citation: [:source]) # TODO, just source_id, and pages
    .select(:id, :cached, :type, :cached_author_year, :cached_nomenclature_date, :rank_class, :updated_at, :updated_by_id)
end

.valid_higher_names(otu) ⇒ Object

Higher names are:

- valid higher names

Notes

- candidate for merging with valid_family_names


190
191
192
193
194
195
196
# File 'lib/export/coldp/files/name.rb', line 190

def self.valid_higher_names(otu)
  a = otu.taxon_name.self_and_descendants
    .where(rank_class: HIGHER_RANK_NAMES, cached_is_valid: true)
    .unscope(:order)
    .eager_load(origin_citation: [:source]) # TODO, just source_id, and pages
    .select(:id, :cached, :type, :cached_author_year, :cached_nomenclature_date, :rank_class, :updated_at, :updated_by_id)
end