Module: AsciiChem::ModelAdapter

Defined in:
lib/asciichem/model_adapter.rb,
lib/asciichem/model_adapter/to_canonical.rb,
lib/asciichem/model_adapter/from_canonical.rb

Overview

Adapter between AsciiChem::Model (the chemistry-semantic tree the text parser produces) and Chemicalml::Model (the format-agnostic canonical hub every chemistry interchange format speaks).

The adapter is bidirectional and pure — no I/O, no wire format concerns. Composed with Chemicalml::Cml::Translator it forms the AsciiChem <-> CML pipeline:

AsciiChem::Model <-> ModelAdapter <-> Chemicalml::Model
                                           ^
                                           |
                                  Chemicalml::Cml::Translator
                                           |
                                           v
                                  Chemicalml::Cml::* (wire)

Adding a new field to either model means updating this adapter's mapping rules; the parsers, formatters, and wire classes stay independent.

Defined Under Namespace

Classes: FromCanonical, ToCanonical, Translation

Class Method Summary collapse

Class Method Details

.from_canonical(document) ⇒ Object



52
53
54
# File 'lib/asciichem/model_adapter.rb', line 52

def self.from_canonical(document)
  FromCanonical.new(document).build
end

.to_canonical(formula) ⇒ Object



34
35
36
# File 'lib/asciichem/model_adapter.rb', line 34

def self.to_canonical(formula)
  ToCanonical.new(formula).build
end

.to_canonical_with_mapping(formula) ⇒ Object

Returns the canonical document plus the per-atom mapping and per-molecule group structure. Use this when you need to know which canonical atom corresponds to which AsciiChem::Model::Atom or which atoms were originally grouped (e.g. for extension data).



42
43
44
45
46
47
48
49
50
# File 'lib/asciichem/model_adapter.rb', line 42

def self.to_canonical_with_mapping(formula)
  builder = ToCanonical.new(formula)
  document = builder.build
  Translation.new(
    document: document,
    atom_mapping: builder.atom_mapping,
    groups: builder.groups
  )
end