Class: BatchLoad::Import::CollectionObjects::CastorInterpreter

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

Instance Attribute Summary

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(**args)

Returns a new instance of CastorInterpreter

Parameters:

  • args (Hash)


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

def initialize(**args)
  @collection_objects = {}
  super(args)
end

Instance Method Details

- (Boolean) build

Returns:

  • (Boolean)


159
160
161
162
163
164
# File 'lib/batch_load/import/collection_objects/castor_interpreter.rb', line 159

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

- (Integer) build_collection_objects

rubocop:disable Metrics/MethodLength

Returns:

  • (Integer)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
# File 'lib/batch_load/import/collection_objects/castor_interpreter.rb', line 12

def build_collection_objects
  # GenBank           GBK
  # DRMLabVoucher     DRMLV
  # DRMDNAVoucher     DRMDNA
  namespace_genbank = Namespace.find_by(name: 'GenBank')
  namespace_drm_lab_voucher = Namespace.find_by(name: 'DRMLabVoucher')
  namespace_drm_dna_voucher = Namespace.find_by(name: 'DRMDNAVoucher')

  drm_lab_voucher_texts = {}

  @total_data_lines = 0
  i = 0

  # loop throw rows
  csv.each do |row|
    i += 1

    parse_result = BatchLoad::RowParse.new
    parse_result.objects[:collection_object] = []
    parse_result.objects[:extract] = []
    parse_result.objects[:taxon_determination] = []

    @processed_rows[i] = parse_result

    # Only accept records from DRM to keep things simple for now
    next if row['locality_database'] != 'DRM'

    begin # processing
      # Text for sample code identifiers for both extract and specimen
      sample_code_identifier_text = "#{row['sample_code_prefix']}#{row['sample_code']}"

      # Extract identifiers
      extract_identifier_genbank = {
        namespace: namespace_genbank,
        type: 'Identifier::Local::CatalogNumber',
        identifier: sample_code_identifier_text
      }

      extract_identifiers = []
      extract_identifiers.push(extract_identifier_genbank) if sample_code_identifier_text.present?

      extract_attributes = {
        quantity_value: 0,
        quantity_unit: 0,
        verbatim_anatomical_origin: row['taxon_name'],
        year_made: 2015,
        month_made: 10,
        day_made: 10,
        identifiers_attributes: extract_identifiers
      }

      extract = Extract.new(extract_attributes)

      parse_result.objects[:extract].push(extract)

      # Text for collection object identifiers
      co_identifier_castor_text = row['guid']
      co_identifier_morphbank_text = row['morphbank_specimen_id']
      co_identifier_drm_lab_voucher_text = "#{row['voucher_number_prefix']}#{row['voucher_number_string']}"

      # If the drm lab voucher identifier text has already been used, don't attach it
      # by setting it blank
      if drm_lab_voucher_texts.has_key?(co_identifier_drm_lab_voucher_text)
        co_identifier_drm_lab_voucher_text = ''

      # Create a new entry with the lab voucher text as the key
      else
        drm_lab_voucher_texts[co_identifier_drm_lab_voucher_text] = true
      end

      # Collection object identifiers
      co_identifier_castor = {
        type: 'Identifier::Global::Uri',
        identifier: co_identifier_castor_text
      }

      co_identifier_morphbank = {
        type: 'Identifier::Global::MorphbankSpecimenNumber',
        identifier: co_identifier_morphbank_text
      }

      co_identifier_drm_lab_voucher = {
        namespace: namespace_drm_lab_voucher,
        type: 'Identifier::Local::CatalogNumber',
        identifier: co_identifier_drm_lab_voucher_text
      }

      co_identifier_drm_dna = {
        namespace: namespace_drm_dna_voucher,
        type: 'Identifier::Local::CatalogNumber',
        identifier: sample_code_identifier_text
      }

      # Add collection object identifiers
      co_identifiers = []
      co_identifiers.push(co_identifier_castor)             if co_identifier_castor_text.present?
      co_identifiers.push(co_identifier_morphbank)          if co_identifier_morphbank_text.present?
      co_identifiers.push(co_identifier_drm_lab_voucher)    if co_identifier_drm_lab_voucher_text.present?
      co_identifiers.push(co_identifier_drm_dna)            if sample_code_identifier_text.present?

      # OriginRelationship between CollecitonObject and Extract
      co_origin_relationships_attributes = [{ new_object: extract }]

      # Data attributes
      co_data_attributes = []

      if row['specimen_number'].present?
        co_data_attributes.push({
          type: 'ImportAttribute',
          import_predicate: 'SpecimenNumber',
          value: row['specimen_number']
        })
      end

      # Create collection object
      co = CollectionObject.new({
        type: 'Specimen',
        total: 1,
        identifiers_attributes: co_identifiers,
        origin_relationships_attributes: co_origin_relationships_attributes,
        data_attributes_attributes: co_data_attributes
      })

      # Collecting event that this collection object corresponds to
      ce = CollectingEvent.with_identifier(row['collecting_event_guid']).take
      co.collecting_event = ce if ce.present?

      parse_result.objects[:collection_object].push(co);
      @total_data_lines += 1 if co.present?

      # Taxon determination between this object and otus this object belongs to
      otus = Otu.with_identifier(row['taxon_guid'])

      otus.each do |otu|
        taxon_determination = TaxonDetermination.new(otu: otu, biological_collection_object: co)
        parse_result.objects[:taxon_determination].push(taxon_determination)
      end

    #rescue
    end
  end

  @total_lines = i
end