Class: AsciiChem::Cml::Translator

Inherits:
Object
  • Object
show all
Defined in:
lib/asciichem/cml/translator.rb

Overview

Thin adapter between AsciiChem::Model and Chemicalml::Cml wire classes. chemicalml 0.2.1+ merged the canonical Model into the wire classes — Schema3::Atom, Schema3::Molecule, etc. handle both serialization and semantics. This class:

  1. Builds a Chemicalml::Cml::Document from AsciiChem::Model::Formula
  2. Serializes to XML (optionally enriching with aci: extensions)
  3. Parses CML XML back into AsciiChem::Model::Formula

The aci: extension namespace (oxidation state, Lewis markers, ring closures, groups, electron config, math, text, metadata) is handled by post-processing the XML via Extensions and GroupExtensions.

Class Method Summary collapse

Class Method Details

.from_asciichem(formula) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/asciichem/cml/translator.rb', line 21

def from_asciichem(formula)
  ensure_schema_registered!
  translation = AsciiChem::ModelAdapter.to_canonical_with_mapping(formula)
  xml = translation.document.to_xml
  xml = inject_atom_extensions(xml, translation.atom_mapping)
  inject_molecule_extensions(xml, formula, translation)
end

.to_asciichem(xml) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/asciichem/cml/translator.rb', line 29

def to_asciichem(xml)
  ensure_schema_registered!
  top_level = Extensions.extract_top_level(xml)
  atom_extensions = Extensions.extract(xml)
  group_extensions = GroupExtensions.extract(xml)
  wire_doc = Chemicalml::Cml::Document.from_xml(xml)
  formula = AsciiChem::ModelAdapter.from_canonical(wire_doc)
  Extensions.restore(formula, wire_doc, atom_extensions)
  GroupExtensions.restore(formula, wire_doc, group_extensions)
  Extensions.restore_top_level(formula, top_level)
  formula
end