Class: AsciiChem::Model::Atom

Inherits:
Node
  • Object
show all
Defined in:
lib/asciichem/model/atom.rb

Overview

A chemical atom: element symbol plus optional isotope, charge, oxidation state, Lewis markers (lone pairs, radicals), and ring closure digits.

The semantic fix over AsciiMath: the prefix isotope (e.g. ^14 in ^14C) binds to THIS atom as the isotope field, not to a phantom preceding element. The transform enforces the binding.

Ring closures (SMILES-style): a digit suffix on an atom opens or closes a ring. Two atoms with the same digit become bonded. C1-C-C-C-C-C1 is cyclohexane. The ring_closures field carries the digit string (e.g. "1"); multiple digits mean multiple open/close points (e.g. "12" opens/closes rings 1 and 2).

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Node

#==, #accept, #children, #hash, short_name, #to_cml, #to_html, #to_latex, #to_mathml, #to_svg, #to_text

Constructor Details

#initialize(element:, isotope: nil, subscript: nil, superscript: nil, charge: nil, oxidation_state: nil, lone_pairs: nil, radical_electrons: nil, ring_closures: nil, x2: nil, y2: nil, z2: nil, atom_parity: nil) ⇒ Atom

Returns a new instance of Atom.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/asciichem/model/atom.rb', line 25

def initialize(element:, isotope: nil, subscript: nil,
               superscript: nil, charge: nil, oxidation_state: nil,
               lone_pairs: nil, radical_electrons: nil,
               ring_closures: nil,
               x2: nil, y2: nil, z2: nil, atom_parity: nil)
  @element = element
  @isotope = isotope
  @subscript = subscript
  @superscript = superscript
  @charge = charge
  @oxidation_state = oxidation_state
  @lone_pairs = lone_pairs
  @radical_electrons = radical_electrons
  @ring_closures = ring_closures
  @x2 = x2
  @y2 = y2
  @z2 = z2
  @atom_parity = atom_parity
end

Instance Attribute Details

#atom_parityObject

Returns the value of attribute atom_parity.



19
20
21
# File 'lib/asciichem/model/atom.rb', line 19

def atom_parity
  @atom_parity
end

#chargeObject

Returns the value of attribute charge.



19
20
21
# File 'lib/asciichem/model/atom.rb', line 19

def charge
  @charge
end

#elementObject

Returns the value of attribute element.



19
20
21
# File 'lib/asciichem/model/atom.rb', line 19

def element
  @element
end

#isotopeObject

Returns the value of attribute isotope.



19
20
21
# File 'lib/asciichem/model/atom.rb', line 19

def isotope
  @isotope
end

#lone_pairsObject

Returns the value of attribute lone_pairs.



19
20
21
# File 'lib/asciichem/model/atom.rb', line 19

def lone_pairs
  @lone_pairs
end

#oxidation_stateObject

Returns the value of attribute oxidation_state.



19
20
21
# File 'lib/asciichem/model/atom.rb', line 19

def oxidation_state
  @oxidation_state
end

#radical_electronsObject

Returns the value of attribute radical_electrons.



19
20
21
# File 'lib/asciichem/model/atom.rb', line 19

def radical_electrons
  @radical_electrons
end

#ring_closuresObject

Returns the value of attribute ring_closures.



19
20
21
# File 'lib/asciichem/model/atom.rb', line 19

def ring_closures
  @ring_closures
end

#subscriptObject

Returns the value of attribute subscript.



19
20
21
# File 'lib/asciichem/model/atom.rb', line 19

def subscript
  @subscript
end

#superscriptObject

Returns the value of attribute superscript.



19
20
21
# File 'lib/asciichem/model/atom.rb', line 19

def superscript
  @superscript
end

#x2Object

Returns the value of attribute x2.



19
20
21
# File 'lib/asciichem/model/atom.rb', line 19

def x2
  @x2
end

#y2Object

Returns the value of attribute y2.



19
20
21
# File 'lib/asciichem/model/atom.rb', line 19

def y2
  @y2
end

#z2Object

Returns the value of attribute z2.



19
20
21
# File 'lib/asciichem/model/atom.rb', line 19

def z2
  @z2
end

Instance Method Details

#diagnostic_labelObject



54
55
56
# File 'lib/asciichem/model/atom.rb', line 54

def diagnostic_label
  "Atom(#{element})"
end

#to_sObject



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/asciichem/model/atom.rb', line 58

def to_s
  parts = []
  parts << ":#{lone_pairs}" if lone_pairs
  parts << element.to_s
  parts << "_#{subscript}" if subscript
  parts << "^#{isotope}" if isotope
  parts << "^#{superscript}" if superscript
  parts << "^#{charge}" if charge
  parts << "^(#{oxidation_state})" if oxidation_state
  parts << ".#{radical_electrons}" if radical_electrons
  parts << ring_closures.to_s if ring_closures
  "Atom(#{parts.join})"
end

#value_attributesObject



45
46
47
48
49
50
51
52
# File 'lib/asciichem/model/atom.rb', line 45

def value_attributes
  { element: element, isotope: isotope, subscript: subscript,
    superscript: superscript, charge: charge,
    oxidation_state: oxidation_state,
    lone_pairs: lone_pairs, radical_electrons: radical_electrons,
    ring_closures: ring_closures,
    x2: x2, y2: y2, z2: z2, atom_parity: atom_parity }
end