Class: AsciiChem::ModelAdapter::FromCanonical

Inherits:
Object
  • Object
show all
Defined in:
lib/asciichem/model_adapter/from_canonical.rb

Overview

Chemicalml::Model -> AsciiChem::Model. Walks the canonical tree and rebuilds an AsciiChem::Model::Formula. Pure transformation; no I/O.

Mapping rules:

  • Chemicalml::Cml::Document -> Formula.
  • Chemicalml::Cml::Molecule -> Molecule. Atoms become AsciiChem atoms with subscript=count. Bonds re-insert between their endpoint positions.
  • Chemicalml::Cml::Atom -> Atom (element, isotope, charge, lone pairs, radical electrons).
  • Chemicalml::Cml::Bond -> Bond (kind enum).
  • Chemicalml::Cml::Reaction -> Reaction.
  • Chemicalml::Cml::ReactionList -> ReactionCascade.

Round-trip note: the canonical model is richer than AsciiChem's (3D coordinates, metadata, etc.). Those fields are dropped on the way back. AsciiChem-specific constructs (Lewis markers, embedded math) round-trip when they ride in the canonical Atom's lone_pairs / radical_electrons fields.

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ FromCanonical

Returns a new instance of FromCanonical.



32
33
34
# File 'lib/asciichem/model_adapter/from_canonical.rb', line 32

def initialize(document)
  @document = document
end

Instance Method Details

#buildObject



36
37
38
39
40
41
# File 'lib/asciichem/model_adapter/from_canonical.rb', line 36

def build
  molecules = @document.molecules.map { |m| molecule_from_canonical(m) }
  reactions = @document.reactions.map { |r| reaction_from_canonical(r) }
  cascades = @document.reaction_lists.map { |l| reaction_list_from_canonical(l) }
  AsciiChem::Model::Formula.new(nodes: molecules + reactions + cascades)
end