Class: BatchLoad::Import::TaxonNames::CastorInterpreter

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

Instance Attribute Summary (collapse)

Attributes inherited from BatchLoad::Import

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

Instance Method Summary (collapse)

Methods inherited from BatchLoad::Import

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

Constructor Details

- (CastorInterpreter) initialize(nomenclature_code: nil, parent_taxon_name_id: nil, also_create_otu: false, **args)

Returns a new instance of CastorInterpreter

Parameters:

  • args (Hash)


20
21
22
23
24
25
26
# File 'lib/batch_load/import/taxon_names/castor_interpreter.rb', line 20

def initialize(nomenclature_code: nil, parent_taxon_name_id: nil, also_create_otu: false, **args)
  @nomenclature_code = nomenclature_code
  @parent_taxon_name_id = parent_taxon_name_id
  @also_create_otu = also_create_otu

  super(args)
end

Instance Attribute Details

- (Object) also_create_otu

Whether to create an OTU as well



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

def also_create_otu
  @also_create_otu
end

- (Object) nomenclature_code

The code (Rank Class) that new names will use



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

def nomenclature_code
  @nomenclature_code
end

- (String) parent_taxon_name

Returns:

  • (String)


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

def parent_taxon_name
  @parent_taxon_name
end

- (Integer) parent_taxon_name_id

Returns:

  • (Integer)


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

def parent_taxon_name_id
  @parent_taxon_name_id
end

- (Object) project_id

Required to handle some defaults



17
18
19
# File 'lib/batch_load/import/taxon_names/castor_interpreter.rb', line 17

def project_id
  @project_id
end

Instance Method Details

- (Hash) author_info(author_string) (private)

Parameters:

  • author_string (String)

Returns:

  • (Hash)


207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/batch_load/import/taxon_names/castor_interpreter.rb', line 207

def author_info(author_string)
  seperator_query = ' '
  separator_index = author_string.index(seperator_query)

  last_name = author_string
  first_name = ''

  if !separator_index.nil?
    separator_index += seperator_query.length
    split_author_info = author_string.split(seperator_query)
    last_name = split_author_info[0]
    first_name = split_author_info[1]
  end

  { last_name: last_name, first_name: first_name, suffix: 'suffix' }
end

- (Boolean) build

Returns:

  • (Boolean)


162
163
164
165
166
167
# File 'lib/batch_load/import/taxon_names/castor_interpreter.rb', line 162

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

- (Integer) build_taxon_names

rubocop:disable Metrics/MethodLength

Returns:

  • (Integer)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/batch_load/import/taxon_names/castor_interpreter.rb', line 40

def build_taxon_names
  @total_data_lines = 0
  i = 0
  taxon_names = {}

  csv.each do |row|
    i += 1

    parse_result = BatchLoad::RowParse.new
    parse_result.objects[:original_taxon_name] = []
    parse_result.objects[:taxon_name] = []
    parse_result.objects[:taxon_name_relationship] = []
    parse_result.objects[:otu] = []

    @processed_rows[i] = parse_result

    begin
      next if row['rank'] == 'complex'
      next if row['rank'] == 'species group'
      next if row['rank'] == 'series'
      next if row['rank'] == 'variety'
      next if row['taxon_name'] == 'unidentified'

      protonym_attributes = {
        name: row['taxon_name'],
        year_of_publication: year_of_publication(row['author_year']),
        rank_class: Ranks.lookup(@nomenclature_code.to_sym, row['rank']),
        by: @user,
        also_create_otu: false,
        project: @project,
        verbatim_author: verbatim_author(row['author_year']),
        taxon_name_authors_attributes: taxon_name_authors_attributes(verbatim_author(row['author_year']))
      }

      if row['original_name']
        original_protonym_attributes = {
          verbatim_name: row['original_name'],
          name: row['original_name'].split(' ')[-1],
          year_of_publication: year_of_publication(row['author_year']),
          rank_class: Ranks.lookup(@nomenclature_code.to_sym, row['original_rank']),
          parent: parent_taxon_name,
          by: @user,
          also_create_otu: false,
          project: @project,
          verbatim_author: verbatim_author(row['author_year']),
          taxon_name_authors_attributes: taxon_name_authors_attributes(verbatim_author(row['author_year']))
        }

        original_protonym = Protonym.new(original_protonym_attributes)

        if row['original_rank'] == 'genus'
          protonym_attributes[:original_genus] = original_protonym
        elsif row['original_rank'] == 'subgenus'
          protonym_attributes[:original_subgenus] = original_protonym
        elsif row['original_rank'] == 'species'
          protonym_attributes[:original_species] = original_protonym
        elsif row['original_rank'] == 'subspecies'
          protonym_attributes[:original_subspecies] = original_protonym
        end

        parse_result.objects[:original_taxon_name].push original_protonym
      end

      p = Protonym.new(protonym_attributes)
      taxon_name_id = row['id']
      parent_taxon_name_id = row['parent_id']
      taxon_names[taxon_name_id] = p

      if taxon_names[parent_taxon_name_id].nil?
        p.parent = parent_taxon_name
      else
        p.parent = taxon_names[parent_taxon_name_id]
      end

      # TaxonNameRelationship
      related_name_id = row['related_name_id']

      if !taxon_names[related_name_id].nil?
        related_name_nomen_class = nil

        begin
          related_name_nomen_class = row['related_name_nomen_class'].constantize

          if related_name_nomen_class.ancestors.include?(TaxonNameRelationship)
            p.name = row['taxon_name'].split(' ')[-1]
            p.verbatim_name = row['taxon_name']
            taxon_name_relationship = related_name_nomen_class.new(subject_taxon_name: p, object_taxon_name: taxon_names[related_name_id])
            parse_result.objects[:taxon_name_relationship].push taxon_name_relationship
          end
        rescue NameError
        end
      end

      # TaxonNameClassification
      name_nomen_classification = row['name_nomen_classification']
      p.taxon_name_classifications.new(type: name_nomen_classification) if TaxonName::EXCEPTED_FORM_TAXON_NAME_CLASSIFICATIONS.include?(name_nomen_classification)

      taxon_concept_identifier_castor_text = row['guid']

      if taxon_concept_identifier_castor_text.present?
        taxon_concept_identifier_castor = {
          type: 'Identifier::Global::Uri',
          identifier: taxon_concept_identifier_castor_text
        }

        taxon_concept_identifiers = []
        taxon_concept_identifiers.push(taxon_concept_identifier_castor)

        otu = Otu.new(name: row['taxon_concept_name'], taxon_name: p, identifiers_attributes: taxon_concept_identifiers)
        parse_result.objects[:otu].push(otu)
      end

      parse_result.objects[:taxon_name].push p
      @total_data_lines += 1 if p.present?
    end
  end

  @total_lines = i
end

- (Array) taxon_name_authors_attributes(author_info) (private)

Parameters:

  • author_info (String)

Returns:

  • (Array)


191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/batch_load/import/taxon_names/castor_interpreter.rb', line 191

def taxon_name_authors_attributes(author_info)
  return [] if author_info.blank?
  multiple_author_query = 'and'
  multiple_author_index = author_info.index(multiple_author_query)
  split_author_info = multiple_author_index.nil? ? [author_info] : author_info.split(multiple_author_query)
  author_infos = []

  split_author_info.each do |author_str|
    author_infos.push(author_info(author_str)) if author_str != 'NA' && author_str != 'unpublished'
  end

  author_infos
end

- (String?) verbatim_author(author_year) (private)

Parameters:

  • author_year (String)

Returns:

  • (String, nil)


182
183
184
185
186
187
# File 'lib/batch_load/import/taxon_names/castor_interpreter.rb', line 182

def verbatim_author(author_year)
  return nil if author_year.blank?
  author_end_index = author_year.rindex(' ')
  author_end_index ||= author_year.length
  author_year[0...author_end_index]
end

- (String?) year_of_publication(author_year) (private)

Parameters:

  • author_year (String)

Returns:

  • (String, nil)


173
174
175
176
177
178
# File 'lib/batch_load/import/taxon_names/castor_interpreter.rb', line 173

def year_of_publication(author_year)
  return nil if author_year.blank?
  split_author_year = author_year.split(' ')
  year = split_author_year[split_author_year.length - 1]
  year =~ /\A\d+\z/ ? year : ''
end