Class: Chemicalml::Cml::Translator

Inherits:
Object
  • Object
show all
Extended by:
ValueTranslations::ClassMethods
Defined in:
lib/chemicalml/cml/translator.rb,
lib/chemicalml/cml/translator/value_translations.rb

Overview

Adapter between the canonical Chemicalml::Model and the CML XML wire-format layer (Chemicalml::Cml::* lutaml-model classes).

Pure transformation. No I/O. Two public class methods, one per direction. All helpers are private.

from_canonical accepts a schema: keyword (:schema3 default, or :schema24) and uses WireClassRegistry to instantiate the right wire class for every nested element — not just the Document root.

Defined Under Namespace

Modules: ValueTranslations

Constant Summary collapse

ORDER_TO_KIND =

-- Lookup tables -----------------------------------------------

{
  "S" => :single, "D" => :double, "T" => :triple,
  "Q" => :quadruple, "A" => :aromatic, "W" => :wedge,
  "H" => :hash, "DG" => :dative, "V" => :wavy
}.freeze
TYPE_TO_ARROW =
{
  "forward" => :forward,
  "reverse" => :reverse,
  "equilibrium" => :equilibrium,
  "resonance" => :resonance
}.freeze

Class Method Summary collapse

Methods included from ValueTranslations::ClassMethods

array_from_canonical, array_to_canonical, formula_from_canonical, formula_to_canonical, label_from_canonical, label_to_canonical, matrix_from_canonical, matrix_to_canonical, metadata_from_canonical, metadata_to_canonical, parameter_from_canonical, parameter_to_canonical, property_from_canonical, property_to_canonical, scalar_from_canonical, scalar_to_canonical, value_container_from_canonical, value_container_to_canonical

Class Method Details

.atom_from_canonical(atom, schema:) ⇒ Object



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/chemicalml/cml/translator.rb', line 308

def self.atom_from_canonical(atom, schema:)
  Chemicalml::Cml::WireClassRegistry
    .for(schema, Chemicalml::Cml::Role::Atom)
    .new(
      id: atom.id,
      element_type: atom.element,
      formal_charge: atom.formal_charge,
      isotope: atom.isotope,
      count: atom.count,
      hydrogen_count: atom.hydrogen_count,
      spin_multiplicity: atom.spin_multiplicity,
      title: atom.title,
      x2: atom.x2,
      y2: atom.y2,
      x3: atom.x3,
      y3: atom.y3,
      z3: atom.z3,
      xFract: atom.x_fract,
      yFract: atom.y_fract,
      zFract: atom.z_fract,
      atom_parity: atom_parity_from_canonical(atom.atom_parity, schema: schema)
    )
end

.atom_parity_from_canonical(ap, schema:) ⇒ Object



343
344
345
346
347
348
349
350
351
352
# File 'lib/chemicalml/cml/translator.rb', line 343

def self.atom_parity_from_canonical(ap, schema:)
  return nil unless ap

  Chemicalml::Cml::WireClassRegistry
    .for(schema, Chemicalml::Cml::Role::AtomParity)
    .new(
      atom_refs4: [*ap.atom_refs4].join(" "),
      content: ap.value
    )
end

.atom_parity_to_canonical(cml_ap) ⇒ Object



175
176
177
178
179
180
181
182
# File 'lib/chemicalml/cml/translator.rb', line 175

def self.atom_parity_to_canonical(cml_ap)
  return nil unless cml_ap

  Model::AtomParity.new(
    atom_refs4: cml_ap.atom_refs4&.split(" ") || [],
    value: cml_ap.content
  )
end

.atom_to_canonical(cml_atom) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/chemicalml/cml/translator.rb', line 144

def self.atom_to_canonical(cml_atom)
  Model::Atom.new(
    id: cml_atom.id,
    element: cml_atom.element_type,
    formal_charge: cml_atom.formal_charge,
    isotope: cml_atom.isotope,
    count: cml_atom.count,
    hydrogen_count: cml_atom.hydrogen_count,
    spin_multiplicity: cml_atom.spin_multiplicity,
    title: cml_atom.title,
    x2: cml_atom.x2,
    y2: cml_atom.y2,
    x3: cml_atom.x3,
    y3: cml_atom.y3,
    z3: cml_atom.z3,
    x_fract: cml_atom.xFract,
    y_fract: cml_atom.yFract,
    z_fract: cml_atom.zFract,
    atom_parity: atom_parity_to_canonical(cml_atom.atom_parity)
  )
end

.bond_from_canonical(bond, schema:) ⇒ Object



332
333
334
335
336
337
338
339
340
341
# File 'lib/chemicalml/cml/translator.rb', line 332

def self.bond_from_canonical(bond, schema:)
  Chemicalml::Cml::WireClassRegistry
    .for(schema, Chemicalml::Cml::Role::Bond)
    .new(
      id: bond.id,
      atom_refs2: bond.atom_refs.join(" "),
      order: bond.cml_order,
      bond_stereo: bond_stereo_from_canonical(bond.bond_stereo, schema: schema)
    )
end

.bond_stereo_from_canonical(bs, schema:) ⇒ Object



354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/chemicalml/cml/translator.rb', line 354

def self.bond_stereo_from_canonical(bs, schema:)
  return nil unless bs

  Chemicalml::Cml::WireClassRegistry
    .for(schema, Chemicalml::Cml::Role::BondStereo)
    .new(
      content: bs.value,
      atom_refs2: bs.atom_refs2 && [*bs.atom_refs2].join(" "),
      atom_refs4: bs.atom_refs4 && [*bs.atom_refs4].join(" "),
      dict_ref: bs.dict_ref
    )
end

.bond_stereo_to_canonical(cml_bs) ⇒ Object



184
185
186
187
188
189
190
191
192
193
# File 'lib/chemicalml/cml/translator.rb', line 184

def self.bond_stereo_to_canonical(cml_bs)
  return nil unless cml_bs

  Model::BondStereo.new(
    value: cml_bs.content,
    atom_refs2: cml_bs.atom_refs2&.split(" "),
    atom_refs4: cml_bs.atom_refs4&.split(" "),
    dict_ref: cml_bs.dict_ref
  )
end

.bond_to_canonical(cml_bond) ⇒ Object



166
167
168
169
170
171
172
173
# File 'lib/chemicalml/cml/translator.rb', line 166

def self.bond_to_canonical(cml_bond)
  Model::Bond.new(
    id: cml_bond.id,
    atom_refs: cml_bond.atom_refs2&.split(" ") || [],
    kind: cml_order_to_kind(cml_bond.order),
    bond_stereo: bond_stereo_to_canonical(cml_bond.bond_stereo)
  )
end

.from_canonical(node, schema: :schema3) ⇒ Object

Canonical -> wire format. Polymorphic: accepts either a canonical Document or a canonical Module. schema: selects the target version (:schema3 default, or :schema24).



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/chemicalml/cml/translator.rb', line 63

def self.from_canonical(node, schema: :schema3)
  case node
  when Model::Document
    document_from_canonical(node, schema: schema)
  when Model::Module
    module_from_canonical(node, schema: schema)
  else
    raise ArgumentError,
          "from_canonical accepts a canonical Document or " \
          "Module, got #{node.class}"
  end
end

.identifier_to_canonical(cml_id) ⇒ Object



134
135
136
137
138
139
140
141
142
# File 'lib/chemicalml/cml/translator.rb', line 134

def self.identifier_to_canonical(cml_id)
  return nil unless cml_id

  Model::Identifier.new(
    value: cml_id.value,
    convention: cml_id.convention,
    dict_ref: cml_id.dict_ref
  )
end

.metadata_list_from_canonical(list, schema:) ⇒ Object



429
430
431
432
433
434
435
436
437
# File 'lib/chemicalml/cml/translator.rb', line 429

def self.(list, schema:)
  registry = Chemicalml::Cml::WireClassRegistry
  registry.for(schema, Chemicalml::Cml::Role::MetadataList).new(
    id: list.id,
    title: list.title,
    dict_ref: list.dict_ref,
    metadata: list..map { |m| (m, schema: schema) }
  )
end

.metadata_list_to_canonical(cml_list) ⇒ Object



260
261
262
263
264
265
266
267
268
269
# File 'lib/chemicalml/cml/translator.rb', line 260

def self.(cml_list)
  return Model::MetadataList.new unless cml_list

  Model::MetadataList.new(
    id: cml_list.id,
    title: cml_list.title,
    dict_ref: cml_list.dict_ref,
    metadata: cml_list..map { |m| (m) }
  )
end

.module_from_canonical(model_module, schema: :schema3) ⇒ Object

Canonical Model::Module -> wire Module. Compchem documents are rooted at <module> rather than <cml>; this method produces the corresponding wire tree (Schema 3 only — Schema 2.4 lacks <module>).



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/chemicalml/cml/translator.rb', line 90

def self.module_from_canonical(model_module, schema: :schema3)
  registry = Chemicalml::Cml::WireClassRegistry
  registry.for(schema, Chemicalml::Cml::Role::Module).new(
    id: model_module.id,
    title: model_module.title,
    dict_ref: model_module.dict_ref,
    convention: model_module.convention,
    molecules: model_module.molecules.map { |m| molecule_from_canonical(m, schema: schema) },
    modules: model_module.modules.map { |m| module_from_canonical(m, schema: schema) },
    parameter_lists: model_module.parameter_lists.map { |l| parameter_list_from_canonical(l, schema: schema) },
    property_lists: model_module.property_lists.map { |l| property_list_from_canonical(l, schema: schema) },
    metadata_lists: model_module..map { |l| (l, schema: schema) },
    lists: []
  )
end

.module_to_canonical(wire_module) ⇒ Object

Wire Module -> canonical Model::Module. Used for compchem documents where the root is <module convention="convention:compchem"> rather than <cml>.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/chemicalml/cml/translator.rb', line 45

def self.module_to_canonical(wire_module)
  Model::Module.new(
    id: wire_module.id,
    title: wire_module.title,
    dict_ref: wire_module.dict_ref,
    convention: wire_module.convention,
    molecules: (wire_module.molecules || []).map { |m| molecule_to_canonical(m) },
    modules: (wire_module.modules || []).map { |m| module_to_canonical(m) },
    parameter_lists: (wire_module.parameter_lists || []).map { |l| parameter_list_to_canonical(l) },
    property_lists: (wire_module.property_lists || []).map { |l| property_list_to_canonical(l) },
    metadata_lists: (wire_module. || []).map { |l| (l) },
    lists: []
  )
end

.molecule_from_canonical(mol, schema:) ⇒ Object

-- Canonical -> CML -------------------------------------------- Every helper takes schema: and looks up wire classes via WireClassRegistry so children are the right version.



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/chemicalml/cml/translator.rb', line 287

def self.molecule_from_canonical(mol, schema:)
  registry = Chemicalml::Cml::WireClassRegistry
  registry.for(schema, Chemicalml::Cml::Role::Molecule).new(
    id: mol.id,
    atom_array: mol.atoms.empty? ? nil : registry.for(schema, Chemicalml::Cml::Role::AtomArray).new(
      atoms: mol.atoms.map { |a| atom_from_canonical(a, schema: schema) }
    ),
    bond_array: mol.bonds.empty? ? nil : registry.for(schema, Chemicalml::Cml::Role::BondArray).new(
      bonds: mol.bonds.map { |b| bond_from_canonical(b, schema: schema) }
    ),
    names: mol.names.map { |n| registry.for(schema, Chemicalml::Cml::Role::Name).new(content: n.content, convention: n.convention, dict_ref: n.dict_ref) },
    identifiers: mol.identifiers.map { |i| registry.for(schema, Chemicalml::Cml::Role::Identifier).new(value: i.value, convention: i.convention, dict_ref: i.dict_ref) },
    formulas: mol.formulas.map { |f| formula_from_canonical(f, schema: schema) },
    properties: mol.properties.map { |p| property_from_canonical(p, schema: schema) },
    labels: mol.labels.map { |l| label_from_canonical(l, schema: schema) },
    count: mol.count,
    formal_charge: mol.formal_charge,
    title: mol.title
  )
end

.molecule_to_canonical(cml_mol) ⇒ Object

-- CML -> canonical --------------------------------------------



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/chemicalml/cml/translator.rb', line 108

def self.molecule_to_canonical(cml_mol)
  Model::Molecule.new(
    id: cml_mol.id,
    atoms: cml_mol.atom_array&.atoms&.map { |a| atom_to_canonical(a) } || [],
    bonds: cml_mol.bond_array&.bonds&.map { |b| bond_to_canonical(b) } || [],
    names: cml_mol.names.map { |n| name_to_canonical(n) },
    identifiers: cml_mol.identifiers.map { |i| identifier_to_canonical(i) },
    formulas: (cml_mol.formulas || []).map { |f| formula_to_canonical(f) },
    properties: (cml_mol.properties || []).map { |p| property_to_canonical(p) },
    labels: (cml_mol.labels || []).map { |l| label_to_canonical(l) },
    count: cml_mol.count,
    formal_charge: cml_mol.formal_charge,
    title: cml_mol.title
  )
end

.name_to_canonical(cml_name) ⇒ Object



124
125
126
127
128
129
130
131
132
# File 'lib/chemicalml/cml/translator.rb', line 124

def self.name_to_canonical(cml_name)
  return nil unless cml_name

  Model::Name.new(
    content: cml_name.content,
    convention: cml_name.convention,
    dict_ref: cml_name.dict_ref
  )
end

.parameter_list_from_canonical(list, schema:) ⇒ Object



419
420
421
422
423
424
425
426
427
# File 'lib/chemicalml/cml/translator.rb', line 419

def self.parameter_list_from_canonical(list, schema:)
  registry = Chemicalml::Cml::WireClassRegistry
  registry.for(schema, Chemicalml::Cml::Role::ParameterList).new(
    id: list.id,
    title: list.title,
    dict_ref: list.dict_ref,
    parameters: list.parameters.map { |p| parameter_from_canonical(p, schema: schema) }
  )
end

.parameter_list_to_canonical(cml_list) ⇒ Object



249
250
251
252
253
254
255
256
257
258
# File 'lib/chemicalml/cml/translator.rb', line 249

def self.parameter_list_to_canonical(cml_list)
  return Model::ParameterList.new unless cml_list

  Model::ParameterList.new(
    id: cml_list.id,
    title: cml_list.title,
    dict_ref: cml_list.dict_ref,
    parameters: cml_list.parameters.map { |p| parameter_to_canonical(p) }
  )
end

.product_list_from_canonical(list, schema:) ⇒ Object



394
395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'lib/chemicalml/cml/translator.rb', line 394

def self.product_list_from_canonical(list, schema:)
  registry = Chemicalml::Cml::WireClassRegistry
  registry.for(schema, Chemicalml::Cml::Role::ProductList).new(
    products: list.products.map do |p|
      registry.for(schema, Chemicalml::Cml::Role::Product).new(
        substance: registry.for(schema, Chemicalml::Cml::Role::Substance).new(
          molecule: molecule_from_canonical(p.substance.molecule, schema: schema),
          title: p.substance.title,
          role: p.substance.role
        )
      )
    end
  )
end

.product_list_to_canonical(cml_list) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/chemicalml/cml/translator.rb', line 222

def self.product_list_to_canonical(cml_list)
  return Model::ProductList.new unless cml_list

  Model::ProductList.new(
    products: cml_list.products.map do |p|
      Model::Product.new(
        substance: Model::Substance.new(
          molecule: molecule_to_canonical(p.substance.molecule),
          title: p.substance.title,
          role: p.substance.role
        )
      )
    end
  )
end

.property_list_from_canonical(list, schema:) ⇒ Object



409
410
411
412
413
414
415
416
417
# File 'lib/chemicalml/cml/translator.rb', line 409

def self.property_list_from_canonical(list, schema:)
  registry = Chemicalml::Cml::WireClassRegistry
  registry.for(schema, Chemicalml::Cml::Role::PropertyList).new(
    id: list.id,
    title: list.title,
    dict_ref: list.dict_ref,
    properties: list.properties.map { |p| property_from_canonical(p, schema: schema) }
  )
end

.property_list_to_canonical(cml_list) ⇒ Object



238
239
240
241
242
243
244
245
246
247
# File 'lib/chemicalml/cml/translator.rb', line 238

def self.property_list_to_canonical(cml_list)
  return Model::PropertyList.new unless cml_list

  Model::PropertyList.new(
    id: cml_list.id,
    title: cml_list.title,
    dict_ref: cml_list.dict_ref,
    properties: cml_list.properties.map { |p| property_to_canonical(p) }
  )
end

.reactant_list_from_canonical(list, schema:) ⇒ Object



379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/chemicalml/cml/translator.rb', line 379

def self.reactant_list_from_canonical(list, schema:)
  registry = Chemicalml::Cml::WireClassRegistry
  registry.for(schema, Chemicalml::Cml::Role::ReactantList).new(
    reactants: list.reactants.map do |r|
      registry.for(schema, Chemicalml::Cml::Role::Reactant).new(
        substance: registry.for(schema, Chemicalml::Cml::Role::Substance).new(
          molecule: molecule_from_canonical(r.substance.molecule, schema: schema),
          title: r.substance.title,
          role: r.substance.role
        )
      )
    end
  )
end

.reactant_list_to_canonical(cml_list) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/chemicalml/cml/translator.rb', line 206

def self.reactant_list_to_canonical(cml_list)
  return Model::ReactantList.new unless cml_list

  Model::ReactantList.new(
    reactants: cml_list.reactants.map do |r|
      Model::Reactant.new(
        substance: Model::Substance.new(
          molecule: molecule_to_canonical(r.substance.molecule),
          title: r.substance.title,
          role: r.substance.role
        )
      )
    end
  )
end

.reaction_from_canonical(rxn, schema:) ⇒ Object



367
368
369
370
371
372
373
374
375
376
377
# File 'lib/chemicalml/cml/translator.rb', line 367

def self.reaction_from_canonical(rxn, schema:)
  Chemicalml::Cml::WireClassRegistry
    .for(schema, Chemicalml::Cml::Role::Reaction)
    .new(
      id: rxn.id,
      title: rxn.title || rxn.arrow.to_s,
      type: rxn.type || rxn.arrow.to_s,
      reactant_list: reactant_list_from_canonical(rxn.reactant_list, schema: schema),
      product_list: product_list_from_canonical(rxn.product_list, schema: schema)
    )
end

.reaction_to_canonical(cml_rxn) ⇒ Object



195
196
197
198
199
200
201
202
203
204
# File 'lib/chemicalml/cml/translator.rb', line 195

def self.reaction_to_canonical(cml_rxn)
  Model::Reaction.new(
    id: cml_rxn.id,
    reactant_list: reactant_list_to_canonical(cml_rxn.reactant_list),
    product_list: product_list_to_canonical(cml_rxn.product_list),
    arrow: type_to_arrow(cml_rxn.type || cml_rxn.title),
    title: cml_rxn.title,
    type: cml_rxn.type
  )
end

.to_canonical(node) ⇒ Object

Wire format -> canonical. Polymorphic: accepts either a wire Document (<cml> root) or a wire Module (<module> root). Dispatches via Role module checks.



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/chemicalml/cml/translator.rb', line 21

def self.to_canonical(node)
  case node
  when Chemicalml::Cml::Role::Document
    document_to_canonical(node)
  when Chemicalml::Cml::Role::Module
    module_to_canonical(node)
  else
    raise ArgumentError,
          "to_canonical accepts a wire Document or Module, " \
          "got #{node.class}"
  end
end