Class: DatasetRecord::DarwinCore::Occurrence
- Inherits:
-
DatasetRecord::DarwinCore
- Object
- ActiveRecord::Base
- ApplicationRecord
- DatasetRecord
- DatasetRecord::DarwinCore
- DatasetRecord::DarwinCore::Occurrence
- Defined in:
- app/models/dataset_record/darwin_core/occurrence.rb
Overview
TODO: There are numerous very long methods here, we really need to break out logical chunks so that we can
a) better atomize and test the expecatations
b) interpret and document the behaviour of the importer
See app/javascript/vue/tasks/dwca_import/components/settings/Occurrences/OccurrenceSettings.vue for UI defined parameters
Defined Under Namespace
Classes: ImportProtonym
Constant Summary collapse
- DWC_CLASSIFICATION_TERMS =
genus, subgenus, specificEpithet and infraspecificEpithet are extracted from scientificName
%w{kingdom phylum class order superfamily family subfamily tribe subtribe}.freeze
- PARSE_DETAILS_KEYS =
%i(uninomial genus species infraspecies).freeze
- ACCEPTED_ATTRIBUTES =
{ CollectionObject: %I( buffered_collecting_event buffered_determinations buffered_other_labels total ).to_set.freeze, CollectingEvent: %I( document_label print_label verbatim_label field_notes formation group lithology max_ma maximum_elevation member min_ma minimum_elevation elevation_precision start_date_day start_date_month start_date_year end_date_day end_date_month end_date_year time_end_hour time_end_minute time_end_second time_start_hour time_start_minute time_start_second verbatim_collectors verbatim_date verbatim_datum verbatim_elevation verbatim_geolocation_uncertainty verbatim_habitat verbatim_latitude verbatim_locality verbatim_longitude verbatim_method verbatim_field_number ).to_set.freeze }.freeze
Instance Attribute Summary
Attributes inherited from DatasetRecord
Instance Method Summary collapse
- #append_data_attribute(attributes, attribute) ⇒ Object private
- #append_dwc_attribute(attributes, predicate, value) ⇒ Object private
- #append_dwc_attributes(dwc_attributes, target) ⇒ Object private
- #append_tag_attribute(tags, tag) ⇒ Object private
-
#delete_namespace_prefix!(identifier_str, namespace) ⇒ Object
private
Remove the namespace short name and delimiter from start of string.
- #extract_event_identifier_params ⇒ Object
- #extract_field_number_identifier_params ⇒ Object
-
#get_correct_spelling(protonym) ⇒ Object
Gets the correct spelling for a protonym, or returns the protonym if not a misspelling.
- #get_integer_field_value(field_name) ⇒ Object private
- #import(dwc_data_attributes = {}) ⇒ Object
- #parse_biocuration_group_field(group) ⇒ Object private
- #parse_biocuration_group_fields ⇒ Object private
-
#parse_event_class ⇒ Object
private
rubocop:disable Metrics/MethodLength.
-
#parse_identification_class(taxon_protonym) ⇒ Object
private
rubocop:enable Metric/MethodLength.
-
#parse_iso_date(field_name) ⇒ Array<OpenStruct>
private
Parse an iso date string from the specified column name.
-
#parse_location_class ⇒ Object
private
rubocop:enable Metrics/MethodLength.
-
#parse_occurrence_class ⇒ Object
private
rubocop:enable Metrics/MethodLength.
-
#parse_organizations_and_people(field_name, search_alt_name = false) ⇒ Array<Organization, Person::Unvetted>?
private
Search for an Organization by name or alternate name in the given field.
-
#parse_people(field_name) ⇒ Array<Person::Unvetted>?
private
Parse for names in a given field and find or create one or more Person::Unvetted (scoped to the import dataset).
-
#parse_record_level_class ⇒ Object
private
rubocop:disable Metrics/MethodLength.
-
#parse_taxon_class ⇒ Object
private
rubocop:disable Metric/MethodLength.
- #parse_tw_collecting_event_attributes ⇒ Object private
- #parse_tw_collecting_event_data_attributes ⇒ Object private
- #parse_tw_collection_object_attributes ⇒ Object private
-
#parse_tw_collection_object_data_attributes ⇒ Object
private
rubocop:disable Metric/MethodLength.
- #parse_typestatus(type_status, taxon_protonym) ⇒ Hash{Symbol=>String, TaxonName}? private
- #term_value_changed(name, value) ⇒ Object private
Methods inherited from DatasetRecord::DarwinCore
#data_field_changed, #get_field_mapping, #get_field_value, #get_fields_mapping, #get_tw_biocuration_groups, #get_tw_data_attribute_fields_for, #get_tw_fields_for, #get_tw_tag_fields_for, #normalize_value!
Methods inherited from DatasetRecord
#create_fields, #data_field_changed, #data_fields, #dataset_record_fields, #destroy_fields, #field_db_attributes, #fields_db_attributes, #frozen_fields?, #get_data_field, #initialize_data_fields, #set_data_field, #update_fields
Methods included from Shared::IsData
#errors_excepting, #full_error_messages_excepting, #identical, #is_community?, #is_destroyable?, #is_editable?, #is_in_use?, #is_in_users_projects?, #metamorphosize, #similar
Methods included from Housekeeping
#has_polymorphic_relationship?
Methods inherited from ApplicationRecord
Instance Method Details
#append_data_attribute(attributes, attribute) ⇒ Object (private)
1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 |
# File 'app/models/dataset_record/darwin_core/occurrence.rb', line 1589 def append_data_attribute(attributes, attribute) predicate = Predicate.find_by(uri: attribute[:selector], project: self.project) predicate ||= Predicate.where(project:).find_by( Predicate.arel_table[:name].matches(ApplicationRecord.sanitize_sql_like(attribute[:selector])) ) value = get_field_value(attribute[:field]) if value raise DarwinCore::InvalidData.new({ attribute[:field] => ["Predicate with #{attribute[:selector]} URI or name not found"] }) unless predicate attributes << { type: 'InternalAttribute', predicate:, value:, annotator_batch_mode: true } end end |
#append_dwc_attribute(attributes, predicate, value) ⇒ Object (private)
1680 1681 1682 1683 1684 1685 1686 1687 |
# File 'app/models/dataset_record/darwin_core/occurrence.rb', line 1680 def append_dwc_attribute(attributes, predicate, value) attributes << { type: 'InternalAttribute', predicate:, value:, annotator_batch_mode: true } if value end |
#append_dwc_attributes(dwc_attributes, target) ⇒ Object (private)
1689 1690 1691 1692 1693 |
# File 'app/models/dataset_record/darwin_core/occurrence.rb', line 1689 def append_dwc_attributes(dwc_attributes, target) dwc_attributes.each do |field, predicate| append_dwc_attribute(target[:data_attributes_attributes], predicate, get_field_value(field)) end end |
#append_tag_attribute(tags, tag) ⇒ Object (private)
1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 |
# File 'app/models/dataset_record/darwin_core/occurrence.rb', line 1566 def append_tag_attribute(, tag) value = get_field_value(tag[:field]) return unless value keyword = Keyword.find_by(uri: tag[:selector], project: self.project) keyword ||= Keyword.where(project:).find_by( Keyword.arel_table[:name].matches(ApplicationRecord.sanitize_sql_like(tag[:selector])) ) if value raise DarwinCore::InvalidData.new({ tag[:field] => ["Tag with #{tag[:selector]} URI or name not found"] }) unless keyword if value.downcase == 'true' || value == '1' .append({keyword:, annotator_batch_mode: true}) return end unless value.downcase == 'false' || value == '0' raise DarwinCore::InvalidData.new({ tag[:field] => ['Tag value must be "true" or "1" to apply, or blank, "false", or "0", to not apply'] }) end end end |
#delete_namespace_prefix!(identifier_str, namespace) ⇒ Object (private)
Remove the namespace short name and delimiter from start of string.
If the namespace has a verbatim_short_name, that is removed instead of the short_name. The delimiter is only removed if the short_name was found in the identifier.
624 625 626 |
# File 'app/models/dataset_record/darwin_core/occurrence.rb', line 624 def delete_namespace_prefix!(identifier_str, namespace) identifier_str&.delete_prefix!(namespace.verbatim_short_name || namespace.short_name)&.delete_prefix!(namespace.delimiter || '') if namespace end |
#extract_event_identifier_params ⇒ Object
483 484 485 |
# File 'app/models/dataset_record/darwin_core/occurrence.rb', line 483 def extract_event_identifier_params() # TODO: Extract logic here for shorter main loop end |
#extract_field_number_identifier_params ⇒ Object
487 488 489 |
# File 'app/models/dataset_record/darwin_core/occurrence.rb', line 487 def extract_field_number_identifier_params() # TODO: Extract logic here for shorter main loop end |
#get_correct_spelling(protonym) ⇒ Object
Gets the correct spelling for a protonym, or returns the protonym if not a misspelling
1120 1121 1122 1123 1124 1125 1126 1127 |
# File 'app/models/dataset_record/darwin_core/occurrence.rb', line 1120 def get_correct_spelling(protonym) if protonym.is_protonym? && protonym.has_misspelling_relationship? return TaxonNameRelationship.where_subject_is_taxon_name(protonym) .with_type_array(TAXON_NAME_RELATIONSHIP_NAMES_MISSPELLING_ONLY) .first&.object_taxon_name end protonym end |
#get_integer_field_value(field_name) ⇒ Object (private)
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 |
# File 'app/models/dataset_record/darwin_core/occurrence.rb', line 518 def get_integer_field_value(field_name) value = get_field_value(field_name) if value.present? begin raise unless /^\s*(?<integer>[+-]?\d+)\s*$/ =~ value value = integer.to_i rescue raise DarwinCore::InvalidData.new({ field_name => ["'#{value}' is not a valid integer value"] }) end else value = nil end value end |
#import(dwc_data_attributes = {}) ⇒ Object
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 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 342 343 344 345 346 347 348 349 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 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 437 438 439 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 476 477 478 479 480 |
# File 'app/models/dataset_record/darwin_core/occurrence.rb', line 161 def import(dwc_data_attributes = {}) super begin DatasetRecord.transaction(requires_new: true) do self..delete('error_data') names, origins = parse_taxon_class strategy = self.import_dataset.restrict_to_existing_nomenclature? ? ImportProtonym.match_existing : ImportProtonym.create_if_not_exists innermost_otu = nil innermost_protonym = names.inject(project.root_taxon_name) do |parent, name| otu_attributes = name.delete(:otu_attributes) unless name[:rank_class] || otu_attributes.present? name[:rank_class] = parent.predicted_child_rank(name[:name])&.to_s name.delete(:rank_class) unless name[:rank_class] && /::FamilyGroup::/ =~ name[:rank_class] end strategy.execute(origins, parent, name).tap do |protonym| innermost_otu = Otu.find_or_create_by!({taxon_name: protonym}.merge!(otu_attributes)) if otu_attributes end end attributes = parse_record_level_class record_level_biocuration_classifications = attributes.dig(:specimen, :biocuration_classifications) attributes.deep_merge!(parse_occurrence_class) attributes.deep_merge!(parse_event_class) attributes.deep_merge!(parse_location_class) attributes.deep_merge!(parse_identification_class(innermost_protonym)) attributes.deep_merge!(parse_tw_collection_object_data_attributes) attributes.deep_merge!(parse_tw_collecting_event_data_attributes) attributes.deep_merge!(parse_tw_collection_object_attributes) attributes.deep_merge!(parse_tw_collecting_event_attributes) append_dwc_attributes(dwc_data_attributes['CollectionObject'], attributes[:specimen]) append_dwc_attributes(dwc_data_attributes['CollectingEvent'], attributes[:collecting_event]) Utilities::Hashes::set_unless_nil(attributes[:specimen], :biocuration_classifications, (parse_biocuration_group_fields.dig(:specimen, :biocuration_classifications) || []) + (record_level_biocuration_classifications || []) + (attributes.dig(:specimen, :biocuration_classifications) || []) ) specimen = Specimen.create!({ no_dwc_occurrence: true }.merge!(attributes[:specimen])) if attributes[:type_material] && (innermost_otu&.name).nil? type_material = TypeMaterial.new( { protonym: innermost_protonym, collection_object: specimen, }.merge!(attributes[:type_material])) # protoynm can be overwritten in type_materials hash if OC did not match scientific name / innermost_protonym if self.import_dataset.require_type_material_success? # raise error if validations fail and it cannot be imported type_material.save! else # Best effort only, import will proceed even if creating the type material fails type_material.save end end if record_number = get_field_value(:recordNumber) record_number_namespace = get_field_value('TW:Namespace:recordNumber') identifier_attributes = { identifier: record_number, project_id: Current.project_id } record_number_namespace = Namespace.find_by(Namespace.arel_table[:short_name].matches(record_number_namespace)) # Case insensitive match raise DarwinCore::InvalidData.new({ 'TW:Namespace:recordNumber' => ['Namespace not found'] }) unless record_number_namespace identifier_attributes[:namespace] = record_number_namespace identifier = Identifier::Local::RecordNumber .create_with(identifier_object: specimen, annotator_batch_mode: true) .find_or_create_by!(identifier_attributes) unless identifier.identifier_object == specimen raise DarwinCore::InvalidData.new({ 'recordNumber' => ['Is already in use'] }) end end if attributes.dig(:catalog_number, :identifier) namespace = attributes.dig(:catalog_number, :namespace) delete_namespace_prefix!(attributes.dig(:catalog_number, :identifier), namespace) identifier = Identifier::Local::CatalogNumber .create_with(identifier_object: specimen, annotator_batch_mode: true) .find_or_create_by!(attributes[:catalog_number]) # if desired, ensure that cached CO identifier will match verbatim catalogNumber # this ensures that DwC exported records will have identical catalogNumbers as when they were imported if self.import_dataset.require_catalog_number_match_verbatim? && identifier.cached != get_field_value(:catalogNumber) = "Computed catalog number #{identifier.cached} will not match verbatim #{get_field_value(:catalogNumber)}. "\ 'Verify the mapped namespace and namespace delimiter are correct.' raise DarwinCore::InvalidData.new({'catalogNumber' => []}) end object = identifier.identifier_object unless object == specimen unless record_number || self.import_dataset.containerize_dup_cat_no? raise DarwinCore::InvalidData.new({ 'catalogNumber' => ['Is already in use'] }) end if object.is_a?(Container) object.add_container_items([specimen]) else identifier.update!( identifier_object: Container::Virtual.containerize([object, specimen]) ) end end end Identifier::Local::Import::Dwc.create!( namespace: import_dataset.get_core_record_identifier_namespace, identifier_object: specimen, identifier: get_field_value(:occurrenceID), annotator_batch_mode: true ) unless get_field_value(:occurrenceID).nil? || import_dataset.get_core_record_identifier_namespace.nil? specimen.taxon_determinations.create!({ otu: innermost_otu || innermost_protonym.otus.find_by(name: nil) || innermost_protonym.otus.first # TODO: Might require select-and-confirm functionality }.merge(attributes[:taxon_determination])) # There are 3 possible CE identifiers, each needs individual mapping # eventID -> Identifier::Local::Event (with TW:Namespace:eventID) # fieldNumber -> Identifier::Local::FieldNumber (with TW:Namespace:fieldNumber) # TW::CollectingEvent::verbatim_field_number # event_id, field_number = get_field_value(:eventID), get_field_value(:fieldNumber) collecting_event_identifiers = [] if event_id.present? event_id_namespace = get_field_value('TW:Namespace:eventID') # TODO: Shouldn't this be local?! identifier_type = Identifier::Global.descendants.detect { |c| c.name.downcase == event_id_namespace.downcase } if event_id_namespace identifier_attributes = { identifier: event_id, identifier_object_type: 'CollectingEvent', project_id: Current.project_id } if identifier_type.nil? identifier_type = Identifier::Local::Event # Note: This was TripCode. This is a much better fit now, as EventID is a digital accession value. using_default_event_id = false if event_id_namespace.nil? event_id_namespace = import_dataset.get_event_id_namespace using_default_event_id = true else event_id_namespace = Namespace.find_by(Namespace.arel_table[:short_name].matches(event_id_namespace)) # Case insensitive match raise DarwinCore::InvalidData.new({ 'TW:Namespace:eventID' => ['Namespace not found'] }) unless event_id_namespace end identifier_attributes[:namespace] = event_id_namespace delete_namespace_prefix!(event_id, event_id_namespace) if !using_default_event_id && self.import_dataset.require_tripcode_match_verbatim? if (cached_identifier = Identifier::Local.build_cached_prefix(event_id_namespace) + event_id) != get_field_value(:eventID) = "Computed Event #{cached_identifier} will not match verbatim #{get_field_value(:eventID)}. "\ 'Verify the namespace delimiter is correct.' # TODO include link to namespace? raise DarwinCore::InvalidData.new({'eventID' => []}) end end end event_id_identifier = identifier_type.find_by(identifier_attributes) collecting_event = event_id_identifier&.identifier_object collecting_event_identifiers << {type: identifier_type, attributes: identifier_attributes} end if field_number.present? field_number_namespace = get_field_value('TW:Namespace:fieldNumber') identifier_attributes = { identifier: field_number, identifier_object_type: 'CollectingEvent', project_id: Current.project_id } field_number_namespace = Namespace.find_by(Namespace.arel_table[:short_name].matches(field_number_namespace)) # Case insensitive match raise DarwinCore::InvalidData.new({ 'TW:Namespace:fieldNumber' => ['Namespace not found'] }) unless field_number_namespace identifier_attributes[:namespace] = field_number_namespace field_number_identifier = Identifier::Local::FieldNumber.find_by(identifier_attributes) collecting_event ||= field_number_identifier&.identifier_object collecting_event_identifiers << {type: Identifier::Local::FieldNumber, attributes: identifier_attributes} end # TODO: If all attributes are equal assume it is the same event and share it with other specimens? (eventID is an alternate method to detect duplicates) if collecting_event if field_number_identifier && event_id_identifier && field_number_identifier.identifier_object != event_id_identifier.identifier_object raise DarwinCore::InvalidData.new({ 'eventID/fieldNumber' => ['eventId and fieldNumber refer to different collecting events'] }) elsif (field_number_identifier && event_id) || (event_id_identifier && field_number) raise DarwinCore::InvalidData.new({ 'eventID/fieldNumber' => ['does not match previous definition of collecting event'] }) end # if collecting_event.identifiers.where(type: Identifer::Local::FieldNumber) # if tags have been specified to be added, update the collecting event if attributes[:collecting_event][:tags_attributes] # get list of preexisting tags, exclude them from update = collecting_event..pluck(:keyword_id).to_set = attributes[:collecting_event][:tags_attributes].reject { |t| .member?(t[:keyword].id) } # add tags if there were any new ones unless .empty? collecting_event..build() collecting_event.save! end end specimen.update!(collecting_event:) else collecting_event = CollectingEvent.create!({ collection_objects: [specimen], no_dwc_occurrence: true, no_cached: true }.merge!(attributes[:collecting_event])) collecting_event_identifiers.each do |identifier| identifier[:type].create!({ identifier_object: collecting_event, annotator_batch_mode: true }.merge!(identifier[:attributes])) end has_shape = self.import_dataset..dig('import_settings', 'require_geographic_area_has_shape') data_origin = self.import_dataset..dig('import_settings', 'geographic_area_data_origin') disable_recursive_search = self.import_dataset..dig('import_settings', 'require_geographic_area_exact_match') require_ga_found = self.import_dataset..dig('import_settings', 'require_geographic_area_exists') should_check_ga_exists = false location_hash = {} # if requiring geographic area to exist, use hash of inputs for error message if collecting_event.verbatim_latitude && collecting_event.verbatim_longitude Georeference::VerbatimData.create!({ collecting_event:, error_radius: get_field_value('coordinateUncertaintyInMeters'), no_cached: true }.merge(attributes[:georeference])) end county = get_field_value(:county) state_province = get_field_value(:stateProvince) country = get_field_value(:country) country_code = get_field_value(:countryCode) if country.blank? && country_code.present? if country_code.size == 2 country = GeographicArea.find_by(iso_3166_a2: country_code, data_origin: 'country_names_and_code_elements').name elsif country_code.size == 3 # there are no GAs with alpha3 presently country = GeographicArea.find_by(iso_3166_a3: country_code, data_origin: 'country_names_and_code_elements').name end end location_levels = [county, state_province, country].compact if require_ga_found && location_levels.size > 0 location_hash = {county:, state_province:, country:, country_code:} should_check_ga_exists = true end # try to find geographic areas until no location levels are left geographic_areas = [] if disable_recursive_search geographic_areas = GeographicArea.with_name_and_parent_names(location_levels).with_data_origin(data_origin).has_shape(has_shape) else while location_levels.size > 0 and geographic_areas.size == 0 geographic_areas = GeographicArea.with_name_and_parent_names(location_levels).with_data_origin(data_origin).has_shape(has_shape) location_levels = location_levels.drop(1) end end if should_check_ga_exists && geographic_areas.size == 0 levels = location_hash.to_a.filter{|_,v| !v.nil?}.map { |k,v| "#{k}:#{v}"} = "GeographicArea with location levels #{levels.join(", ")} not found." raise DarwinCore::InvalidData.new({'country, stateProvince, county' => []}) end collecting_event.geographic_area_id = geographic_areas[0].id if geographic_areas.size > 0 collecting_event.save! end DwcOccurrenceUpsertJob.perform_later(specimen) self.['imported_objects'] = { collection_object: { id: specimen.id } } self.status = 'Imported' end rescue DarwinCore::InvalidData => invalid self.status = 'Errored' self.['error_data'] = { messages: invalid.error_data } rescue ActiveRecord::RecordInvalid => invalid self.status = 'Errored' self.['error_data'] = { messages: invalid.record.errors. } rescue StandardError => e raise if Rails.env.development? self.status = 'Failed' self.['error_data'] = { exception: { message: e., backtrace: e.backtrace } } ensure save! end self end |
#parse_biocuration_group_field(group) ⇒ Object (private)
1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 |
# File 'app/models/dataset_record/darwin_core/occurrence.rb', line 1617 def parse_biocuration_group_field(group) biocuration_group = BiocurationGroup.find_by(uri: group[:selector], project: self.project) biocuration_group ||= BiocurationGroup.where(project:).find_by( BiocurationGroup.arel_table[:name].matches(ApplicationRecord.sanitize_sql_like(group[:selector])) ) value = get_field_value(group[:field]) if value raise DarwinCore::InvalidData.new({ group[:field] => ["Biocuration group with '#{group[:selector]}' URI or name not found"] }) unless biocuration_group biocuration_class = BiocurationClass.where(project:).joins(:tags).merge( Tag.where(keyword: biocuration_group) ).find_by(uri: value) biocuration_class ||= BiocurationClass.where(project:).joins(:tags).merge( Tag.where(keyword: biocuration_group) ).find_by( BiocurationClass.arel_table[:name].matches(ApplicationRecord.sanitize_sql_like(value)) ) raise DarwinCore::InvalidData.new({ group[:field] => ["Biocuration class with '#{value}' URI or name not found"] }) unless biocuration_class BiocurationClassification.new(biocuration_class:) end end |
#parse_biocuration_group_fields ⇒ Object (private)
1607 1608 1609 1610 1611 1612 1613 1614 1615 |
# File 'app/models/dataset_record/darwin_core/occurrence.rb', line 1607 def parse_biocuration_group_fields { specimen: { biocuration_classifications: get_tw_biocuration_groups .map { |g| parse_biocuration_group_field(g) } .reject(&:nil?) } } end |
#parse_event_class ⇒ Object (private)
rubocop:disable Metrics/MethodLength
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 |
# File 'app/models/dataset_record/darwin_core/occurrence.rb', line 857 def parse_event_class collecting_event = { } # eventID: [Mapped in import method] # parentEventID: [Not mapped] # fieldNumber: verbatim_field_number & Identifier::Local::FieldNumber start_date, end_date = parse_iso_date(:eventDate) year = get_integer_field_value(:year) month = get_integer_field_value(:month) day = get_integer_field_value(:day) startDayOfYear = get_integer_field_value(:startDayOfYear) raise DarwinCore::InvalidData.new({ "eventDate": ['Conflicting values. Please check year, month, and day match eventDate'] }) if start_date && (year && start_date.year != year || month && start_date.month != month || day && start_date.day != day) year ||= start_date&.year month ||= start_date&.month day ||= start_date&.day if startDayOfYear raise DarwinCore::InvalidData.new({ "startDayOfYear": ['Missing year value'] }) if year.nil? begin ordinal = Date.ordinal(year, startDayOfYear) rescue Date::Error raise DarwinCore::InvalidData.new({ "startDayOfYear": ['Out of range. Please also check year field'] }) end if month && ordinal.month != month || day && ordinal.day != day raise DarwinCore::InvalidData.new({ "startDayOfYear": ['Month and/or day of the event date do not match'] }) end month ||= ordinal.month day ||= ordinal.day end # eventDate | (year+month+day) | (year+startDayOfYear): start_date_* Utilities::Hashes::set_unless_nil(collecting_event, :start_date_year, year) Utilities::Hashes::set_unless_nil(collecting_event, :start_date_month, month) Utilities::Hashes::set_unless_nil(collecting_event, :start_date_day, day) # eventTime: time_start_* %r{^ (?<start_hour>\d+)(:(?<start_minute>\d+))?(:(?<start_second>\d+))? (/(?<end_hour>\d+))?(:(?<end_minute>\d+))?(:(?<end_second>\d+))? $}x =~ get_field_value(:eventTime) Utilities::Hashes::set_unless_nil(collecting_event, :time_start_hour, start_hour) Utilities::Hashes::set_unless_nil(collecting_event, :time_start_minute, start_minute) Utilities::Hashes::set_unless_nil(collecting_event, :time_start_second, start_second) Utilities::Hashes::set_unless_nil(collecting_event, :time_end_hour, end_hour) Utilities::Hashes::set_unless_nil(collecting_event, :time_end_minute, end_minute) Utilities::Hashes::set_unless_nil(collecting_event, :time_end_second, end_second) endDayOfYear = get_integer_field_value(:endDayOfYear) if endDayOfYear raise DarwinCore::InvalidData.new({ "endDayOfYear": ['Missing year value'] }) if year.nil? begin ordinal = Date.ordinal(year, endDayOfYear) rescue Date::Error raise DarwinCore::InvalidData.new({ "endDayOfYear": ['Out of range. Please also check year field'] }) end month = ordinal.month day = ordinal.day raise DarwinCore::InvalidData.new({ "eventDate": ['Conflicting values. Please check year and endDayOfYear match eventDate'] }) if end_date && (year && end_date.year != year || month && end_date.month != month || day && end_date.day != day) else year = end_date&.year month = end_date&.month day = end_date&.day end Utilities::Hashes::set_unless_nil(collecting_event, :end_date_year, year) Utilities::Hashes::set_unless_nil(collecting_event, :end_date_month, month) Utilities::Hashes::set_unless_nil(collecting_event, :end_date_day, day) # verbatimEventDate: verbatim_date Utilities::Hashes::set_unless_nil(collecting_event, :verbatim_date, get_field_value(:verbatimEventDate)) # habitat: verbatim_habitat Utilities::Hashes::set_unless_nil(collecting_event, :verbatim_habitat, get_field_value(:habitat)) # samplingProtocol: verbatim_method Utilities::Hashes::set_unless_nil(collecting_event, :verbatim_method, get_field_value(:samplingProtocol)) # sampleSizeValue: [Not mapped] # sampleSizeUnit: [Not mapped] # samplingEffort: [Not mapped] # fieldNotes: field_notes Utilities::Hashes::set_unless_nil(collecting_event, :field_notes, get_field_value(:fieldNotes)) # eventRemarks: [collecting event note] note = get_field_value(:eventRemarks) Utilities::Hashes::set_unless_nil(collecting_event, :notes_attributes, [{text: note, annotator_batch_mode: true}]) if note { collecting_event: } end |
#parse_identification_class(taxon_protonym) ⇒ Object (private)
rubocop:enable Metric/MethodLength
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 |
# File 'app/models/dataset_record/darwin_core/occurrence.rb', line 1255 def parse_identification_class(taxon_protonym) taxon_determination = {} type_material = nil # identificationID: [Not mapped] # identificationQualifier: [Mapped 1:1 with otu name parse_taxon_class] # typeStatus: [Type material only if scientific name matches scientificName and type term is recognized by TW vocabulary] if (type_status = get_field_value(:typeStatus)) type_material = parse_typestatus(type_status, taxon_protonym) if type_material.nil? && self.import_dataset.require_type_material_success? # generic error message, nothing more specific provided raise DarwinCore::InvalidData.new({ "typeStatus": ['Unprocessable typeStatus information'] }) end end # identifiedBy: determiners of taxon determination determiners = nil if self.import_dataset.enable_organization_determiners? determiners = parse_organizations_and_people(:identifiedBy, self.import_dataset.enable_organization_determiners_alt_name?) else determiners = parse_people(:identifiedBy) end unless determiners.nil? if determiners.first.is_a?(Person) taxon_determination[:determiners] = determiners elsif determiners.first.is_a?(Organization) taxon_determination[:determiners_organization] = determiners end end # dateIdentified: {year,month,day}_made of taxon determination start_date, end_date = parse_iso_date(:dateIdentified) raise DarwinCore::InvalidData.new({ "dateIdentified": ['Date range for taxon determination is not supported.'] }) if end_date if start_date Utilities::Hashes::set_unless_nil(taxon_determination, :year_made, start_date.year) Utilities::Hashes::set_unless_nil(taxon_determination, :month_made, start_date.month) Utilities::Hashes::set_unless_nil(taxon_determination, :day_made, start_date.day) end # identificationReferences: [Not mapped. Can they be imported as citations without breaking semantics?] # identificationVerificationStatus: [Not mapped] # identificationRemarks: Note for taxon determination note = get_field_value(:identificationRemarks) taxon_determination[:notes_attributes] = [{text: note, annotator_batch_mode: true}] if note { taxon_determination:, type_material: } end |
#parse_iso_date(field_name) ⇒ Array<OpenStruct> (private)
Parse an iso date string from the specified column name
The date may be a single date, or an interval of two dates separated by a slash. The second date may omit higher-order elements that are the same as the first date. See en.wikipedia.org/wiki/ISO_8601#Time_intervals for more information.
601 602 603 604 605 606 607 608 609 610 611 612 613 614 |
# File 'app/models/dataset_record/darwin_core/occurrence.rb', line 601 def parse_iso_date(field_name) value = get_field_value(field_name) return nil if value.nil? result = Utilities::Dates.parse_iso_date_str(value) raise DarwinCore::InvalidData.new( { "#{field_name}": ["Invalid date. Please make sure it conforms to ISO 8601 date format (yyyy-mm-ddThh:mm:ss). If expressing interval separate result with '/'. Examples: 1972-05; 1983-10-25; 2020-09-22T15:30; 2020-11-30/2020-12-04"] } ) if result.nil? result end |
#parse_location_class ⇒ Object (private)
rubocop:enable Metrics/MethodLength
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 |
# File 'app/models/dataset_record/darwin_core/occurrence.rb', line 967 def parse_location_class collecting_event = {} georeference = {} # locationID: [Not mapped] # higherGeographyID: [Not mapped] # higherGeography: [Not mapped] # continent: [Not mapped] # waterBody: [Not mapped] # islandGroup: [Not mapped] # island: [Not mapped] # country: [Not mapped] # countryCode: [Not mapped] # stateProvince: [Not mapped] # county: [Not mapped] # municipality: [Not mapped] # locality: [Not mapped] # verbatimLocality: [verbatim_locality] Utilities::Hashes::set_unless_nil(collecting_event, :verbatim_locality, get_field_value(:verbatimLocality)) # minimumElevationInMeters: [Not mapped] Utilities::Hashes::set_unless_nil(collecting_event, :minimum_elevation, get_field_value(:minimumElevationInMeters)) # maximumElevationInMeters: [Not mapped] Utilities::Hashes::set_unless_nil(collecting_event, :maximum_elevation, get_field_value(:maximumElevationInMeters)) # verbatimElevation: [Not mapped] Utilities::Hashes::set_unless_nil(collecting_event, :verbatim_elevation, get_field_value(:verbatimElevation)) # minimumDepthInMeters: [Not mapped. REVISIT] # maximumDepthInMeters: [Not mapped. REVISIT] # verbatimDepth: [Not mapped. REVISIT] # minimumDistanceAboveSurfaceInMeters: [Not mapped] # maximumDistanceAboveSurfaceInMeters: [Not mapped] # locationAccordingTo: [Not mapped. REVISIT] # locationRemarks: [Not mapped. REVISIT] # decimalLatitude: [verbatim_latitude] Utilities::Hashes::set_unless_nil(collecting_event, :verbatim_latitude, get_field_value(:decimalLatitude)) # decimalLongitude: [verbatim_longitude] Utilities::Hashes::set_unless_nil(collecting_event, :verbatim_longitude, get_field_value(:decimalLongitude)) # geodeticDatum: [verbatim_datum] Utilities::Hashes::set_unless_nil(collecting_event, :verbatim_datum, get_field_value(:geodeticDatum)) # coordinateUncertaintyInMeters: [verbatim_geolocation_uncertainty] uncertainty = get_field_value(:coordinateUncertaintyInMeters) unless uncertainty.nil? || uncertainty =~ /\A[+-]?\d+\z/ raise DarwinCore::InvalidData.new({ "coordinateUncertaintyInMeters": ['Non-integer value'] }) end Utilities::Hashes::set_unless_nil(collecting_event, :verbatim_geolocation_uncertainty, uncertainty&.send(:+, 'm')) # coordinatePrecision: [Not mapped. Fail import if claimed precision is incorrect? Round to precision?] # pointRadiusSpatialFit: [Not mapped] # verbatimCoordinates: [Not mapped] # verbatimLatitude: [Not mapped] # verbatimLongitude: [Not mapped] # verbatimCoordinateSystem: [Not mapped] # verbatimSRS: [Not mapped] # footprintWKT: [Not mapped] # footprintSRS: [Not mapped] # footprintSpatialFit: [Not mapped] # georeferencedBy: [Not mapped] if georeferenced_by = get_field_value(:georeferencedBy) predicate_base_props = {uri: 'http://rs.tdwg.org/dwc/terms/georeferencedBy', project: self.project} predicate = Predicate.find_by(predicate_base_props) predicate ||= Predicate.where(project:).find_by( Predicate.arel_table[:name].matches('georeferencedBy') ) predicate ||= Predicate.create!(predicate_base_props.merge( { name: 'georeferencedBy', definition: 'A list (concatenated and separated) of names of people, groups, or organizations who determined the georeference (spatial representation) for the Location.' }) ) georeference[:data_attributes] = [ InternalAttribute.new( type: 'InternalAttribute', predicate:, value: georeferenced_by, annotator_batch_mode: true ) ] end # georeferencedDate: [Not mapped] # georeferenceProtocol: [Not mapped] # georeferenceSources: [Not mapped. REVISIT] # georeferenceVerificationStatus: [Not mapped] # georeferenceRemarks: [georeference note] note = get_field_value(:georeferenceRemarks) georeference[:notes_attributes] = [{text: note, annotator_batch_mode: true}] if note { collecting_event:, georeference: } end |
#parse_occurrence_class ⇒ Object (private)
rubocop:enable Metrics/MethodLength
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 |
# File 'app/models/dataset_record/darwin_core/occurrence.rb', line 760 def parse_occurrence_class res = { catalog_number: {}, specimen: {}, collecting_event: {} } # occurrenceID: [Mapped in import method] # catalogNumber: [catalog_number.identifier] Utilities::Hashes::set_unless_nil(res[:catalog_number], :identifier, get_field_value(:catalogNumber)) # recordNumber: [Mapped in import method] # recordedBy: [collecting_event.collectors and collecting_event.verbatim_collectors] Utilities::Hashes::set_unless_nil(res[:collecting_event], :collectors, (parse_people(:recordedBy) rescue nil)) Utilities::Hashes::set_unless_nil(res[:collecting_event], :verbatim_collectors, get_field_value(:recordedBy)) # individualCount: [specimen.total] Utilities::Hashes::set_unless_nil(res[:specimen], :total, get_field_value(:individualCount) || 1) # organismQuantity: [Not mapped. Check relation with invidivialCount] # organismQuantityType: [Not mapped. Check relation with invidivialCount] # sex: [Find or create by name inside Sex biocuration Group] TODO: Think of duplicates (with and without URI) sex = get_field_value(:sex) if sex raise DarwinCore::InvalidData.new({ "sex": ['Only single-word controlled vocabulary supported at this time.'] }) if sex =~ /\s/ group = BiocurationGroup.find_by(project_id: Current.project_id, uri: DWC_ATTRIBUTE_URIS[:sex]) group ||= BiocurationGroup.where(project_id: Current.project_id).where('name ILIKE ?', 'sex').first group ||= BiocurationGroup.create!( name: 'Sex', definition: 'The sex of the individual(s) [CREATED FROM DWC-A IMPORT]', uri: DWC_ATTRIBUTE_URIS[:sex] ) # TODO: BiocurationGroup.biocuration_classes not returning AR relation sex_biocuration = group.biocuration_classes.detect { |c| c.name.casecmp(sex) == 0 } unless sex_biocuration sex_biocuration = BiocurationClass.create!(name: sex, definition: "#{sex} individual(s) [CREATED FROM DWC-A IMPORT]") Tag.create!(keyword: group, tag_object: sex_biocuration) else sex = sex_biocuration end Utilities::Hashes::set_unless_nil(res[:specimen], :biocuration_classifications, [BiocurationClassification.new(biocuration_class: sex_biocuration)]) end # lifeStage: [Not mapped] # reproductiveCondition: [Not mapped] # behavior: [Not mapped] # establishmentMeans: [Not mapped] # degreeOfEstablishment [Not mapped] # pathway [Not mapped] # occurrenceStatus: [Not mapped] # preparations: [Match PreparationType by name (case insensitive)] preparation_name = get_field_value(:preparations) if preparation_name preparation_type = PreparationType.find_by(PreparationType.arel_table[:name].matches(preparation_name)) raise DarwinCore::InvalidData.new({ "preparations": ["Unknown preparation \"#{preparation_name}\". If it is correct please add it to preparation types and retry."] }) unless preparation_type Utilities::Hashes::set_unless_nil(res[:specimen], :preparation_type, preparation_type) end Utilities::Hashes::delete_nil_and_empty_hash_values(res) # disposition: [Not mapped] # associatedMedia: [Not mapped] # associatedReferences: [Not mapped] # associatedSequences: [Not mapped] # associatedTaxa: [Not mapped] # otherCatalogNumbers: [Not mapped] # occurrenceRemarks: [specimen note] note = get_field_value(:occurrenceRemarks) Utilities::Hashes::set_unless_nil(res[:specimen], :notes_attributes, [{text: note, annotator_batch_mode: true}]) if note res end |
#parse_organizations_and_people(field_name, search_alt_name = false) ⇒ Array<Organization, Person::Unvetted>? (private)
Search for an Organization by name or alternate name in the given field. If no organization found, find or create a Person::Unvetted, scoped to the import_dataset
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 'app/models/dataset_record/darwin_core/occurrence.rb', line 542 def parse_organizations_and_people(field_name, search_alt_name = false) org_name = get_field_value(field_name) possible_organizations = Organization.where(name: org_name) if search_alt_name possible_organizations = possible_organizations.or(Organization.where(alternate_name: org_name)) end if possible_organizations.exists? if possible_organizations.count == 1 return [possible_organizations.first] elsif possible_organizations.count > 1 matching_orgs = possible_organizations.map do |o| str = "[id:#{o.id} #{o.name}" if o.alternate_name.present? str << " (AKA: #{o.alternate_name})" end str << ']' end.join(', ') # TODO how should the user disambiguate which organization they are referring to? raise DarwinCore::InvalidData.new({ field_name => ["Multiple organizations matched name or alternate name '#{org_name}': #{matching_orgs}"] }) end end parse_people(field_name) end |
#parse_people(field_name) ⇒ Array<Person::Unvetted>? (private)
Parse for names in a given field and find or create one or more Person::Unvetted (scoped to the import dataset).
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 |
# File 'app/models/dataset_record/darwin_core/occurrence.rb', line 571 def parse_people(field_name) #noinspection RubyMismatchedReturnType Person.transaction(requires_new: true) do DwcAgent.parse(get_field_value(field_name)).map! { |n| DwcAgent.clean(n) }.map! do |name| attributes = { last_name: [name[:particle], name[:family]].compact.join(' '), first_name: name[:given], suffix: name[:suffix], prefix: name[:title] || name[:appellation] } # self.import_dataset.derived_people.merge(Person.where(attributes)).first || # TODO: Doesn't work, fails to detect Person subclasses. Why (besides explanation in Shared::OriginRelationship)? Person.where(attributes).joins(:related_origin_relationships).merge( OriginRelationship.where(old_object: self.import_dataset) ).first || Person::Unvetted.create!(attributes.merge({ related_origin_relationships: [OriginRelationship.new(old_object: self.import_dataset, annotator_batch_mode: true)] })) end end end |
#parse_record_level_class ⇒ Object (private)
rubocop:disable Metrics/MethodLength
630 631 632 633 634 635 636 637 638 639 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 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 |
# File 'app/models/dataset_record/darwin_core/occurrence.rb', line 630 def parse_record_level_class res = { specimen: {}, catalog_number: {} } # type: [Check it is 'PhysicalObject'] type = get_field_value(:type) || 'PhysicalObject' raise DarwinCore::InvalidData.new({ 'type' => ["Only 'PhysicalObject' or empty allowed"] }) if type != 'PhysicalObject' # modified: [Not mapped] # language: [Not mapped] # license: [Not mapped. Possible with Attribution model? To which object(s)?] # rightsHolder: [Not mapped. Same questions as license but using roles] # accessRights: [Not mapped. Related to license] # bibliographicCitation: [Not mapped] # references: [Not mapped] # institutionID: [Not mapped. Review] # collectionID: [Not mapped. Review] # datasetID: [Not mapped] # institutionCode: [repository.acronym] # TODO: Use mappings like with namespaces here as well? (Although probably attempt guessing) institution_code = get_field_value(:institutionCode) if institution_code repository = nil = [] if institution_code.starts_with?('http://') || institution_code.starts_with?('https://') url_repositories = Repository.where(url: institution_code) if url_repositories.count == 1 repository = url_repositories.first elsif url_repositories.count > 1 << "Multiple repositories with url #{institution_code} found" else << "No repositories with url #{institution_code} found" end end unless repository acronym_repositories = Repository.where(acronym: institution_code) if acronym_repositories.count == 1 repository = acronym_repositories.first elsif acronym_repositories.count > 1 << "Multiple repositories with acronym #{institution_code} found." else << "No repositories with acronym #{institution_code} found." end end # Some repositories may not have acronyms, in that case search by name as well unless repository repository_results = Repository.where(Repository.arel_table['name'].matches(Repository.sanitize_sql_like(institution_code))) if repository_results.count == 1 repository = repository_results.first elsif repository_results.count > 1 << "Multiple repositories match the name #{institution_code}." else << "No repositories match the name #{institution_code}" end unless repository if .unshift("Could not disambiguate repository name '#{institution_code}'.") else .unshift("Unknown #{institution_code} repository. If valid please register it using '#{institution_code}' as acronym or name.") end raise DarwinCore::InvalidData.new({ "institutionCode": }) end end Utilities::Hashes::set_unless_nil(res[:specimen], :repository, repository) end # collectionCode: [catalog_number.namespace] # collection_code = get_field_value(:collectionCode) # Utilities::Hashes::set_unless_nil(res[:catalog_number], :namespace, Namespace.create_with({ # name: "#{institution_code}-#{collection_code} [CREATED FROM DWC-A IMPORT IN #{project.name} PROJECT]", # delimiter: '-' # }).find_or_create_by!(short_name: "#{institution_code}-#{collection_code}")) if collection_code if namespace = get_field_value('TW:Namespace:catalogNumber') namespace = Namespace.find_by(Namespace.arel_table[:short_name].matches(namespace)) # Case insensitive match raise DarwinCore::InvalidData.new({ 'TW:Namespace:catalogNumber' => ['Namespace not found'] }) unless namespace else namespace_id = self.import_dataset.get_catalog_number_namespace(institution_code, get_field_value(:collectionCode)) namespace = Namespace.find(namespace_id) if namespace_id end if namespace Utilities::Hashes::set_unless_nil(res[:catalog_number], :namespace, namespace) Utilities::Hashes::set_unless_nil(res[:catalog_number], :project, self.project) end # datasetName: [Not mapped] # ownerInstitutionCode: [Not mapped] # basisOfRecord: [Check it is 'PreservedSpecimen', 'FossilSpecimen'] basis = get_field_value(:basisOfRecord) basis = basis.downcase.camelize if basis&.include? '_' # Reformat GBIF occurrence download basis of records (e.g., PRESERVED_SPECIMEN to PreservedSpecimen) if 'FossilSpecimen'.casecmp(basis) == 0 fossil_biocuration = BiocurationClass.where(project:).find_by(uri: DWC_FOSSIL_URI) raise DarwinCore::InvalidData.new( { 'basisOfRecord' => ["Biocuration class #{DWC_FOSSIL_URI} is not present in project"] } ) if fossil_biocuration.nil? Utilities::Hashes::set_unless_nil(res[:specimen], :biocuration_classifications, [BiocurationClassification.new(biocuration_class: fossil_biocuration)]) else raise DarwinCore::InvalidData.new( { 'basisOfRecord' => ["Only 'PreservedSpecimen', 'FossilSpecimen' or blank is allowed."] } ) unless basis.nil? || 'PreservedSpecimen'.casecmp(basis) == 0 end # informationWithheld: [Not mapped] # dataGeneralizations: [Not mapped] # dynamicProperties: [Not mapped. Could be ImportAttribute?] Utilities::Hashes::delete_nil_and_empty_hash_values(res) end |
#parse_taxon_class ⇒ Object (private)
rubocop:disable Metric/MethodLength
1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 |
# File 'app/models/dataset_record/darwin_core/occurrence.rb', line 1315 def parse_taxon_class names = [] otu_names = [] origins = {} # taxonID: [Not mapped. Usually alias of core id] # scientificNameID: [Not mapped. Could be mapped with type detection into LSID identifier or global ID] # acceptedNameUsageID: [N/A for occurrences] # parentNameUsageID: [N/A for occurrences] # originalNameUsageID: [N/A for occurrences] # nameAccordingToID: [Not mapped] # namePublishedInID: [Not mapped] # taxonConceptID: [Not mapped] # acceptedNameUsage: [Not mapped. Review] # parentNameUsage: [N/A for occurrences] # originalNameUsage: [Not mapped. Review] # nameAccordingTo: [Not mapped] # namePublishedIn: [Not mapped] # namePublishedInYear: [Not mapped] # nomenclaturalCode: [Selects nomenclature code to pick ranks from] code = get_field_value(:nomenclaturalCode)&.downcase&.to_sym || import_dataset.default_nomenclatural_code unless Ranks::CODES.include?(code) raise DarwinCore::InvalidData.new( { "nomenclaturalCode": ["Unrecognized nomenclatural code #{get_field_value(:nomenclaturalCode)}"] } ) end # kingdom: [Kingdom protonym] origins[ {rank_class: Ranks.lookup(code, 'kingdom'), name: get_field_value(:kingdom)}.tap { |h| names << h }.object_id ] = :kingdom # phylum: [Phylum protonym] origins[ {rank_class: Ranks.lookup(code, 'phylum'), name: get_field_value(:phylum)}.tap { |h| names << h }.object_id ] = :phylum # class: [Class protonym] origins[ {rank_class: Ranks.lookup(code, 'class'), name: get_field_value(:class)}.tap { |h| names << h }.object_id ] = :class # order: [Order protonym] origins[ {rank_class: Ranks.lookup(code, 'order'), name: get_field_value(:order)}.tap { |h| names << h }.object_id ] = :order # superfamily: [Superfamily protonym] origins[ {rank_class: Ranks.lookup(code, 'superfamily'), name: get_field_value(:superfamily)}.tap { |h| names << h }.object_id ] = :superfamily # family: [Family protonym] origins[ {rank_class: Ranks.lookup(code, 'family'), name: get_field_value(:family)}.tap { |h| names << h }.object_id ] = :family # subfamily: [Subfamily protonym] origins[ {rank_class: Ranks.lookup(code, 'subfamily'), name: get_field_value(:subfamily)}.tap { |h| names << h }.object_id ] = :subfamily # tribe: [Tribe protonym] origins[ {rank_class: Ranks.lookup(code, 'tribe'), name: get_field_value(:tribe)}.tap { |h| names << h }.object_id ] = :tribe # subtribe: [Subtribe protonym] origins[ {rank_class: Ranks.lookup(code, 'subtribe'), name: get_field_value(:subtribe)}.tap { |h| names << h }.object_id ] = :subtribe # genus: [Not mapped, extracted from scientificName instead] # subgenus: [Not mapped, extracted from scientificName instead] # specificEpithet: [Not mapped, extracted from scientificName instead] # infraspecificEpithet: [Not mapped, extracted from scientificName instead] # scientificName: [Parsed with biodiversity and mapped into several protonyms] parse_results = Biodiversity::Parser.parse(get_field_value(:scientificName) || '') parse_details = parse_results[:details] parse_details = (parse_details&.keys - PARSE_DETAILS_KEYS).empty? ? parse_details.values.first : nil if parse_details unless (1..3).include?(parse_results[:quality]) && parse_details parse_details = parse_results[:details]&.values&.first end raise DarwinCore::InvalidData.new({ "scientificName": parse_results[:qualityWarnings] ? parse_results[:qualityWarnings].map { |q| q[:warning] } : ['Unable to parse scientific name. Please make sure it is correctly spelled.'] }) unless parse_details&.is_a?(Hash) unless parse_details[:uninomial] origins[ {rank_class: Ranks.lookup(code, 'genus'), name: parse_details[:genus]}.tap { |h| names << h }.object_id ] = :scientificName origins[ {rank_class: Ranks.lookup(code, 'subgenus'), name: parse_details[:subgenus]}.tap { |h| names << h }.object_id ] = :scientificName origins[ {rank_class: Ranks.lookup(code, 'species'), name: parse_details[:species]}.tap { |h| names << h }.object_id ] = :scientificName origins[ {rank_class: Ranks.lookup(code, 'subspecies'), name: parse_details[:infraspecies]&.map{ |d| d.dig(:value) }&.join(' ') }.tap { |h| names << h }.object_id ] = :scientificName else if parse_details[:parent] origins[ {rank_class: Ranks.lookup(code, 'genus'), name: parse_details[:parent]}.tap { |h| names << h }.object_id ] = :scientificName origins[ { rank_class: /subgen/ =~ parse_details[:rank] ? Ranks.lookup(code, 'subgenus') : nil, name: parse_details[:uninomial] }.tap { |h| names << h }.object_id ] = :scientificName elsif get_field_value(:genus) == parse_details[:uninomial] origins[ {rank_class: Ranks.lookup(code, 'genus'), name: parse_details[:uninomial]}.tap { |h| names << h }.object_id ] = :scientificName elsif names.reverse.detect { |n| n[:name] }&.dig(:name) != parse_details[:uninomial] origins[ {rank_class: nil, name: parse_details[:uninomial]}.tap { |h| names << h }.object_id ] = :scientificName end end names.reject! { |v| v[:name].nil? } # taxonRank: [Rank of innermost protonym] rank = get_field_value(:taxonRank) if rank && otu_names.empty? # TODO: Probably don't need otu_name check, rank matches the taxon name, NOT the OTU concept when identificationQualifier is used names.last[:rank_class] = Ranks.lookup(code, rank) raise DarwinCore::InvalidData.new({ "taxonRank": ["Unknown #{code.upcase} rank #{rank}"] }) unless names.last[:rank_class] end ident_qualifier = get_field_value(:identificationQualifier) otu_names << ident_qualifier unless ident_qualifier.nil? names.last&.merge!({otu_attributes: {name: otu_names.join(' ')}}) unless otu_names.empty? # higherClassification: [Several protonyms with ranks determined automatically when possible. Classification lower or at genus level is ignored and extracted from scientificName instead] higherClassification = ['|', ':', ';', ','].inject([]) do |names, separator| break names if names.size > 1 get_field_value(:higherClassification)&.split(separator) || [] end.map! do |name| normalize_value!(name) {rank_class: nil, name:} end curr = 0 names.each do |name| idx = higherClassification[curr..].index { |n| n[:name] == name[:name] } if idx higherClassification[curr+idx] = name curr += idx + 1 end end idx = higherClassification.index { |n| n[:rank_class] == Ranks.lookup(code, 'genus') } higherClassification = higherClassification.slice(0, idx) if idx curr = 0 higherClassification.each do |name| if name[:rank_class] curr = names.index(name) + 1 else names.insert(curr, name) origins[name.object_id] = :higherClassification curr += 1 end end # verbatimTaxonRank: [Not mapped] # scientificNameAuthorship: [verbatim_author of innermost protonym] begin , year = Utilities::Strings.(get_field_value('scientificNameAuthorship')) names.last&.merge!({ verbatim_author: , year_of_publication: year }) end # vernacularName: [Not mapped] # taxonomicStatus: [Not mapped. Review] # nomenclaturalStatus: [Not mapped. Review] # taxonRemarks: [Not mapped] [names, origins] end |
#parse_tw_collecting_event_attributes ⇒ Object (private)
1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 |
# File 'app/models/dataset_record/darwin_core/occurrence.rb', line 1661 def parse_tw_collecting_event_attributes attributes = {} get_tw_fields_for('CollectingEvent').each do |attribute| value = get_field_value(attribute[:field]) if value if !ACCEPTED_ATTRIBUTES[:CollectingEvent].include?(attribute[:name]) raise DarwinCore::InvalidData.new({ attribute[:field] => ["#{attribute[:name]} is not a valid CollectingEvent attribute"] }) end attributes[attribute[:name]] = value end end { collecting_event: attributes } end |
#parse_tw_collecting_event_data_attributes ⇒ Object (private)
1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 |
# File 'app/models/dataset_record/darwin_core/occurrence.rb', line 1546 def parse_tw_collecting_event_data_attributes attributes = [] = [] get_tw_data_attribute_fields_for('CollectingEvent').each do |attribute| append_data_attribute(attributes, attribute) end get_tw_tag_fields_for('CollectingEvent').each do |tag| append_tag_attribute(, tag) end { collecting_event: { data_attributes_attributes: attributes, tags_attributes: } } end |
#parse_tw_collection_object_attributes ⇒ Object (private)
1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 |
# File 'app/models/dataset_record/darwin_core/occurrence.rb', line 1642 def parse_tw_collection_object_attributes attributes = {} get_tw_fields_for('CollectionObject').each do |attribute| value = get_field_value(attribute[:field]) if value if !ACCEPTED_ATTRIBUTES[:CollectionObject].include?(attribute[:name]) raise DarwinCore::InvalidData.new({ attribute[:field] => ["#{attribute[:name]} is not a valid CollectionObject attribute"] }) end attributes[attribute[:name]] = value end end { specimen: attributes } end |
#parse_tw_collection_object_data_attributes ⇒ Object (private)
rubocop:disable Metric/MethodLength
1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 |
# File 'app/models/dataset_record/darwin_core/occurrence.rb', line 1526 def parse_tw_collection_object_data_attributes attributes = [] = [] get_tw_data_attribute_fields_for('CollectionObject').each do |attribute| append_data_attribute(attributes, attribute) end get_tw_tag_fields_for('CollectionObject').each do |tag| append_tag_attribute(, tag) end { specimen: { data_attributes_attributes: attributes, tags_attributes: } } end |
#parse_typestatus(type_status, taxon_protonym) ⇒ Hash{Symbol=>String, TaxonName}? (private)
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 |
# File 'app/models/dataset_record/darwin_core/occurrence.rb', line 1106 def parse_typestatus(type_status, taxon_protonym) type_material = nil type_status_parsed = type_status&.match(/^(?<type>\w+)$/i) || type_status&.match(/(?<type>\w+)(\s+OF\s+(?<scientificName>.*))/i) # only nil if non-alphanumeric entry, or multiple words not matching "\w+ of \w+" raise DarwinCore::InvalidData.new({ "typeStatus": ['Unprocessable typeStatus information'] }) unless type_status_parsed && type_status_parsed[:type] type_type = type_status_parsed[:type].downcase code = get_field_value(:nomenclaturalCode)&.downcase&.to_sym || import_dataset.default_nomenclatural_code unless TypeMaterial::legal_type_type(code, type_type) raise DarwinCore::InvalidData.new({ "typeStatus": ['could not extract legal type from typeStatus'] }) end # Gets the correct spelling for a protonym, or returns the protonym if not a misspelling # @param [Protonym] protonym the protonym to get correct spelling for def get_correct_spelling(protonym) if protonym.is_protonym? && protonym.has_misspelling_relationship? return TaxonNameRelationship.where_subject_is_taxon_name(protonym) .with_type_array(TAXON_NAME_RELATIONSHIP_NAMES_MISSPELLING_ONLY) .first&.object_taxon_name end protonym end scientific_name = get_field_value(:scientificName)&.gsub(/\s+/, ' ') # Run the name through the biodiversity parser to remove authorship info parse_results = Biodiversity::Parser.parse((type_status_parsed&.[](:scientificName)&.gsub(/\s+/, ' ') rescue nil) || '') , type_year = nil # Only use biodiversity parsed name if it has very high confidence if parse_results[:quality] == 1 type_scientific_name = parse_results.dig(:canonical, :simple) # Save authorship info for narrowing down potential protonyms , type_year = Utilities::Strings.(parse_results.dig(:authorship, :normalized)) end # if typeStatus is single word, assume the user wants the specimen name as the type name type_scientific_name ||= scientific_name if scientific_name && type_scientific_name.present? # list of messages to help user debug why matching failed = [] # if type_scientific_name matches the current name of the occurrence, use that if type_scientific_name.delete_prefix(scientific_name)&.match(/^\W*$/) return { type_type: } end name_pattern = "^#{type_scientific_name.split.map { |n| "#{n}(?: \\[sic\\])?" }.join(" ")}$" original_combination_protonyms = Protonym.where('cached_original_combination ~ :pat', pat: name_pattern) .where(project_id: self.project_id) if original_combination_protonyms.count == 1 oc_protonym = original_combination_protonyms.first return { type_type:, protonym: get_correct_spelling(oc_protonym) } elsif original_combination_protonyms.count > 1 potential_protonym_strings = original_combination_protonyms.map { |proto| "[id: #{proto.id} #{proto.cached_original_combination_html}]" }.join(', ') << "Multiple matches found for name #{type_scientific_name}}: #{potential_protonym_strings}" else << 'Could not find exact original combination match for typeStatus' end # See if name matches a synonym of taxon name (ie any name linked to current taxon name) synonyms = taxon_protonym.synonyms matching_synonyms = Set[] synonyms.each do |s| possible_names = [s.cached, s.cached_original_combination].compact.to_set # Try excluding subgenus possible_names += possible_names.map {|n| n.sub(/\(\w+\) /, '')} # Check for misspellings possible_names += possible_names.map { |n| n.gsub(' [sic]', '') } if possible_names.include?(type_scientific_name) if s.is_combination? matching_synonyms << s.finest_protonym else matching_synonyms << s end end end matching_synonyms = matching_synonyms.map { |s| get_correct_spelling(s) }.uniq if matching_synonyms.count == 1 return { type_type:, protonym: matching_synonyms.first } elsif matching_synonyms.count > 1 synonym_strings = matching_synonyms.map { |proto| "[id: #{proto.id} #{proto.cached_original_combination_html}]" }.join(', ') << "Multiple synonym matches found for name #{type_scientific_name}}: #{synonym_strings}" end # Try wildcard match on subgenus if not present type_name_elements = type_scientific_name.split if type_name_elements.length > 1 && type_name_elements[1].first != '(' && type_name_elements[1].last != ')' type_name_elements.map! { |s| Regexp.escape(s) } # append subgenus wildcard to genus string type_name_elements[0] << '( \(\w+\))?' name_pattern = "^#{type_name_elements.join(" ")}$" wildcard_original_protonym = Protonym.where('cached_original_combination ~ :pat', pat: name_pattern) .or(Protonym.where('cached ~ :pat', pat: name_pattern)) .where(project_id: self.project_id) if .present? = if .starts_with?('(') && .end_with?(')') .delete_prefix!('(').delete_suffix!(')') end wildcard_original_protonym = wildcard_original_protonym.where(cached_author:) end if type_year.present? wildcard_original_protonym = wildcard_original_protonym.where(year_of_publication: type_year) end if wildcard_original_protonym.count == 1 return { type_type:, protonym: get_correct_spelling(wildcard_original_protonym.first) } elsif wildcard_original_protonym.count > 1 matching_protonyms = wildcard_original_protonym.map { |p| "[id: #{p.id} #{p.}]" } .join(', ') << "Multiple names returned in wildcard search: #{matching_protonyms}" else << 'No names returned in subgenus wildcard search' end end # report errors if .unshift "Could not identify or disambiguate name #{type_scientific_name}." raise DarwinCore::InvalidData.new({ "typeStatus": }) end end type_material end |
#term_value_changed(name, value) ⇒ Object (private)
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 |
# File 'app/models/dataset_record/darwin_core/occurrence.rb', line 496 def term_value_changed(name, value) if ['institutioncode', 'collectioncode', 'catalognumber', 'basisofrecord'].include?(name.downcase) and self.status != 'Imported' ready = get_field_value('catalogNumber').blank? || get_field_value('TW:Namespaces:catalogNumber').present? ready ||= !!self.import_dataset.get_catalog_number_namespace(get_field_value('institutionCode'), get_field_value('collectionCode')) self..delete('error_data') if ready self.status = 'Ready' else self.status = 'NotReady' self.['error_data'] = { messages: { catalogNumber: ['Record cannot be imported until namespace is set, see "Settings".'] } } end self.import_dataset.add_catalog_number_namespace(get_field_value('institutionCode'), get_field_value('collectionCode')) self.import_dataset.add_catalog_number_collection_code_namespace(get_field_value('collectionCode')) self.save! end end |