asciichem 1.2.12.0 → 1.2.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +16 -7
- data/lib/asciichem/cml/translator.rb +13 -1
- data/lib/asciichem/layout.rb +11 -0
- data/lib/asciichem/model_adapter/from_canonical.rb +28 -7
- data/lib/asciichem/model_adapter/to_canonical.rb +11 -0
- data/lib/asciichem/model_adapter.rb +2 -2
- data/lib/asciichem/smiles/parser.rb +15 -7
- data/lib/asciichem/structure/graph.rb +18 -2
- data/lib/asciichem/structure/linearizer.rb +92 -15
- data/lib/asciichem/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 3ab1b163c6fda6302138ddbd9497f3474a7936d3491e1dc1cfa09d8d93c4a61a
|
|
4
|
+
data.tar.gz: 87418bf57633a9e2987557c3d8b5e23b543ea0cc46c48fb87a5db3d9152cb3c3
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c11f892d9d03e907ab42a5be864fa2a4c6219f22db8b8fe51d04821eec9c4802317b58a488d7a38c766f623ebb62060151c18fb8d802d2062ddadb1aa3e01d26
|
|
7
|
+
data.tar.gz: 84501bec07efce7eafbef546ca6c4e7bb69496e131ff3abe579d7ae86009a2cf533773b0d873bbc82854569152a9bc8719a9c1b787f4c5f5a332558becab4882
|
data/CHANGELOG.md
CHANGED
|
@@ -3,15 +3,15 @@
|
|
|
3
3
|
All notable changes to AsciiChem are documented here.
|
|
4
4
|
This project follows [Semantic Versioning](https://semver.org/).
|
|
5
5
|
|
|
6
|
-
## [1.2.
|
|
6
|
+
## [1.2.13.0] - 2026-11-20
|
|
7
7
|
|
|
8
8
|
### Fixed
|
|
9
9
|
- **The stereo parity loop closes** (asciichem-ruby#200, the #188
|
|
10
|
-
branch projection): the SMILES reader marks every edge's
|
|
11
|
-
the parse site and the linearizer partitions by the mark —
|
|
12
|
-
subtrees emit as parenthesised groups at their root (the
|
|
13
|
-
ester wires correctly), atoms keep creation order, and
|
|
14
|
-
producers (molfile, CML) ride the flat rule. With the
|
|
10
|
+
branch projection, PR #208): the SMILES reader marks every edge's
|
|
11
|
+
shape at the parse site and the linearizer partitions by the mark —
|
|
12
|
+
branch subtrees emit as parenthesised groups at their root (the
|
|
13
|
+
aspirin ester wires correctly), atoms keep creation order, and
|
|
14
|
+
shape-less producers (molfile, CML) ride the flat rule. With the
|
|
15
15
|
branch-grouped model the writer's parity inversion becomes the
|
|
16
16
|
identity, so the verbatim TH1/TH2 store closes
|
|
17
17
|
parse → write → parse with no compensation pass. The CML restore
|
|
@@ -20,7 +20,16 @@ This project follows [Semantic Versioning](https://semver.org/).
|
|
|
20
20
|
checkpoint failures closed: the store contract, alanine/cyclic
|
|
21
21
|
round-trip stability, enantiomer distinctness, the molfile parity
|
|
22
22
|
field, the group attachment bond, the aspirin/para-quinone
|
|
23
|
-
perception rows, and the PubChem resolver chirality.
|
|
23
|
+
perception rows, and the PubChem resolver chirality. (The 1.2.12.0
|
|
24
|
+
release carried this entry but shipped BEFORE the fix's merge —
|
|
25
|
+
that gem is a version ride only; this release carries the code.)
|
|
26
|
+
|
|
27
|
+
## [1.2.12.0] - 2026-11-19
|
|
28
|
+
|
|
29
|
+
### Notes
|
|
30
|
+
- Version ride released ahead of its described fix (#208 merged
|
|
31
|
+
after this gem shipped); the substance of the entry lives in
|
|
32
|
+
1.2.13.0.
|
|
24
33
|
|
|
25
34
|
## [1.2.11.0] - 2026-11-19
|
|
26
35
|
|
|
@@ -51,13 +51,25 @@ module AsciiChem
|
|
|
51
51
|
ensure_schema_registered!
|
|
52
52
|
extracted = extract_all(xml)
|
|
53
53
|
wire_doc = Chemicalml::Cml::Document.from_xml(extracted[:cleaned_xml])
|
|
54
|
-
formula = AsciiChem::ModelAdapter.from_canonical(
|
|
54
|
+
formula = AsciiChem::ModelAdapter.from_canonical(
|
|
55
|
+
wire_doc,
|
|
56
|
+
ring_closures_by_atom: ring_closures_from(extracted)
|
|
57
|
+
)
|
|
55
58
|
restore_all(formula, wire_doc, extracted)
|
|
56
59
|
formula
|
|
57
60
|
end
|
|
58
61
|
|
|
59
62
|
private
|
|
60
63
|
|
|
64
|
+
# The atom ids whose aci:ringClosures carry a digit string —
|
|
65
|
+
# the rebuilder's ring-bond transport signal.
|
|
66
|
+
def ring_closures_from(extracted)
|
|
67
|
+
extracted[:atom_extensions].each_with_object({}) do |(atom_id, entry), memo|
|
|
68
|
+
digits = entry[:ring_closures]
|
|
69
|
+
memo[atom_id] = digits if digits
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
61
73
|
def extract_all(xml)
|
|
62
74
|
opaque_list, cleaned_xml = OpaqueExtensions.extract(xml)
|
|
63
75
|
{
|
data/lib/asciichem/layout.rb
CHANGED
|
@@ -207,7 +207,18 @@ module AsciiChem
|
|
|
207
207
|
when AsciiChem::Model::Bond
|
|
208
208
|
@pending_bond = node
|
|
209
209
|
when AsciiChem::Model::Group, AsciiChem::Model::Molecule
|
|
210
|
+
# A group whose FIRST node is a bond is a BRANCH group: the
|
|
211
|
+
# leading bond attaches the branch to the atom before the
|
|
212
|
+
# group, and the continuation resumes from that root —
|
|
213
|
+
# `C(-O)=O` reads C-O and C=O, never O=O.
|
|
214
|
+
branch_root = nil
|
|
215
|
+
if node.is_a?(AsciiChem::Model::Group) && @last_atom_id
|
|
216
|
+
first = node.nodes.first
|
|
217
|
+
first = first.nodes.first while first.is_a?(AsciiChem::Model::Molecule)
|
|
218
|
+
branch_root = @last_atom_id if first.is_a?(AsciiChem::Model::Bond)
|
|
219
|
+
end
|
|
210
220
|
walk_nodes(node.nodes)
|
|
221
|
+
@last_atom_id = branch_root if branch_root
|
|
211
222
|
end
|
|
212
223
|
end
|
|
213
224
|
end
|
|
@@ -29,8 +29,17 @@ module AsciiChem
|
|
|
29
29
|
# math) round-trip when they ride in the canonical Atom's
|
|
30
30
|
# lone_pairs / radical_electrons fields.
|
|
31
31
|
class FromCanonical
|
|
32
|
-
|
|
32
|
+
# `ring_closures_by_atom` maps canonical atom id → digit string
|
|
33
|
+
# (from the aci:ringClosures extension). The rebuilder uses it
|
|
34
|
+
# to tell digit-paired ring bonds from positional continuation
|
|
35
|
+
# bonds — a bond between atoms sharing a digit is ring
|
|
36
|
+
# transport; every other wire bond renders as a token, even
|
|
37
|
+
# when its endpoints are non-adjacent in the atom array (the
|
|
38
|
+
# branch-projection shape puts continuation bonds across group
|
|
39
|
+
# gaps).
|
|
40
|
+
def initialize(document, ring_closures_by_atom: {})
|
|
33
41
|
@document = document
|
|
42
|
+
@ring_closures_by_atom = ring_closures_by_atom
|
|
34
43
|
end
|
|
35
44
|
|
|
36
45
|
def build
|
|
@@ -167,7 +176,8 @@ module AsciiChem
|
|
|
167
176
|
end
|
|
168
177
|
|
|
169
178
|
def molecule_from_canonical(molecule)
|
|
170
|
-
builder = MoleculeRebuilder.new(molecule
|
|
179
|
+
builder = MoleculeRebuilder.new(molecule,
|
|
180
|
+
ring_closures_by_atom: @ring_closures_by_atom)
|
|
171
181
|
AsciiChem::Model::Molecule.new(
|
|
172
182
|
nodes: builder.nodes,
|
|
173
183
|
coefficient: molecule.count,
|
|
@@ -307,8 +317,9 @@ module AsciiChem
|
|
|
307
317
|
class MoleculeRebuilder
|
|
308
318
|
attr_reader :nodes
|
|
309
319
|
|
|
310
|
-
def initialize(molecule)
|
|
320
|
+
def initialize(molecule, ring_closures_by_atom: {})
|
|
311
321
|
@molecule = molecule
|
|
322
|
+
@ring_closures_by_atom = ring_closures_by_atom
|
|
312
323
|
@position_by_atom_id = {}
|
|
313
324
|
wire_atoms(molecule).each_with_index do |atom, idx|
|
|
314
325
|
@position_by_atom_id[atom.id] = idx
|
|
@@ -402,10 +413,20 @@ module AsciiChem
|
|
|
402
413
|
# adjacent — its endpoints span a gap. Such bonds are
|
|
403
414
|
# represented by ring_closures digits, not positional markers.
|
|
404
415
|
def ring_bond?(bond)
|
|
405
|
-
|
|
406
|
-
return false
|
|
407
|
-
|
|
408
|
-
|
|
416
|
+
ids = bond.atom_refs2.to_s.split
|
|
417
|
+
return false unless ids.length == 2
|
|
418
|
+
|
|
419
|
+
# Digit pairing is the ring-transport signal (aci:ringClosures
|
|
420
|
+
# on both endpoints, restored onto the atoms later). Every
|
|
421
|
+
# other bond renders positionally — including continuation
|
|
422
|
+
# bonds whose endpoints sit across a group gap in the
|
|
423
|
+
# branch-projection shape. An extension-less wire's closing
|
|
424
|
+
# bonds land positionally too; the linear bind consumes the
|
|
425
|
+
# redundant token, so the behaviour matches the old
|
|
426
|
+
# gap-heuristic without its false positives.
|
|
427
|
+
first = @ring_closures_by_atom[ids[0]].to_s.chars
|
|
428
|
+
second = @ring_closures_by_atom[ids[1]].to_s.chars
|
|
429
|
+
!(first & second).empty?
|
|
409
430
|
end
|
|
410
431
|
|
|
411
432
|
def first_position(bond)
|
|
@@ -506,6 +506,16 @@ module AsciiChem
|
|
|
506
506
|
end
|
|
507
507
|
|
|
508
508
|
def enter_group(group, multiplier)
|
|
509
|
+
# A group whose FIRST node is a bond is a BRANCH group: the
|
|
510
|
+
# leading bond attaches the branch to the atom before the
|
|
511
|
+
# group, and the continuation resumes from that root —
|
|
512
|
+
# `C(-O)=O` reads C-O and C=O, never O=O.
|
|
513
|
+
branch_root = nil
|
|
514
|
+
if @last_atom_id
|
|
515
|
+
first = group.nodes.first
|
|
516
|
+
first = first.nodes.first while first.is_a?(AsciiChem::Model::Molecule)
|
|
517
|
+
branch_root = @last_atom_id if first.is_a?(AsciiChem::Model::Bond)
|
|
518
|
+
end
|
|
509
519
|
record = GroupRecord.new(
|
|
510
520
|
id: @ids.next(:group),
|
|
511
521
|
multiplicity: group.multiplicity,
|
|
@@ -516,6 +526,7 @@ module AsciiChem
|
|
|
516
526
|
@group_stack.push(record)
|
|
517
527
|
walk_nodes(group.nodes, multiplier: combine(multiplier, group.multiplicity))
|
|
518
528
|
@group_stack.pop
|
|
529
|
+
@last_atom_id = branch_root if branch_root
|
|
519
530
|
end
|
|
520
531
|
|
|
521
532
|
def emit_atom(atom, multiplier)
|
|
@@ -52,8 +52,8 @@ module AsciiChem
|
|
|
52
52
|
)
|
|
53
53
|
end
|
|
54
54
|
|
|
55
|
-
def self.from_canonical(document)
|
|
56
|
-
FromCanonical.new(document).build
|
|
55
|
+
def self.from_canonical(document, ring_closures_by_atom: {})
|
|
56
|
+
FromCanonical.new(document, ring_closures_by_atom: ring_closures_by_atom).build
|
|
57
57
|
end
|
|
58
58
|
end
|
|
59
59
|
end
|
|
@@ -40,6 +40,7 @@ module AsciiChem
|
|
|
40
40
|
@parent = nil
|
|
41
41
|
@pending_kind = nil
|
|
42
42
|
@pending_direction = nil
|
|
43
|
+
@pending_shape = nil
|
|
43
44
|
|
|
44
45
|
chain
|
|
45
46
|
|
|
@@ -50,12 +51,13 @@ module AsciiChem
|
|
|
50
51
|
|
|
51
52
|
edges = []
|
|
52
53
|
@adjacency.each_with_index do |neighbors, index|
|
|
53
|
-
neighbors.each do |to, kind|
|
|
54
|
+
neighbors.each do |to, (kind, ring, shape)|
|
|
54
55
|
next unless to > index
|
|
55
56
|
|
|
56
57
|
edges << Structure::Graph::Edge.new(
|
|
57
58
|
from: index, to: to, kind: kind,
|
|
58
|
-
direction: @edge_directions[[index, to]]
|
|
59
|
+
direction: @edge_directions[[index, to]],
|
|
60
|
+
ring: ring, shape: shape
|
|
59
61
|
)
|
|
60
62
|
end
|
|
61
63
|
end
|
|
@@ -99,11 +101,16 @@ module AsciiChem
|
|
|
99
101
|
saved_direction = @pending_direction
|
|
100
102
|
@pending_kind = kind
|
|
101
103
|
@pending_direction = direction
|
|
104
|
+
# The branch's FIRST edge is branch-shaped; the tree's inner
|
|
105
|
+
# chain stays chain-shaped (the linearizer groups only the
|
|
106
|
+
# branch edge's subtree).
|
|
107
|
+
@pending_shape = :branch
|
|
102
108
|
chain
|
|
103
109
|
expect(')')
|
|
104
110
|
@parent = saved_parent
|
|
105
111
|
@pending_kind = saved_pending
|
|
106
112
|
@pending_direction = saved_direction
|
|
113
|
+
@pending_shape = nil
|
|
107
114
|
end
|
|
108
115
|
|
|
109
116
|
def ringbond(kind, direction = nil)
|
|
@@ -122,7 +129,7 @@ module AsciiChem
|
|
|
122
129
|
raise ParseError,
|
|
123
130
|
"bonded ring closures (#{kind}) are not representable in the model's ring-closure form"
|
|
124
131
|
end
|
|
125
|
-
add_edge(opener, @parent, kind)
|
|
132
|
+
add_edge(opener, @parent, kind, ring: true)
|
|
126
133
|
else
|
|
127
134
|
@open_rings[digit] = @parent
|
|
128
135
|
end
|
|
@@ -134,18 +141,19 @@ module AsciiChem
|
|
|
134
141
|
@atoms << atom
|
|
135
142
|
@adjacency[index] = {}
|
|
136
143
|
if @parent
|
|
137
|
-
add_edge(@parent, index, @pending_kind)
|
|
144
|
+
add_edge(@parent, index, @pending_kind, shape: @pending_shape || :chain)
|
|
138
145
|
@edge_directions[[@parent, index].minmax] = @pending_direction if @pending_direction
|
|
139
146
|
end
|
|
140
147
|
@parent = index
|
|
141
148
|
@pending_kind = nil
|
|
142
149
|
@pending_direction = nil
|
|
150
|
+
@pending_shape = nil
|
|
143
151
|
end
|
|
144
152
|
|
|
145
|
-
def add_edge(from, to, explicit)
|
|
153
|
+
def add_edge(from, to, explicit, ring: false, shape: :chain)
|
|
146
154
|
kind = explicit || default_kind(from, to)
|
|
147
|
-
@adjacency[from][to] = kind
|
|
148
|
-
@adjacency[to][from] = kind
|
|
155
|
+
@adjacency[from][to] = [kind, ring, shape]
|
|
156
|
+
@adjacency[to][from] = [kind, ring, shape]
|
|
149
157
|
end
|
|
150
158
|
|
|
151
159
|
def default_kind(from, to)
|
|
@@ -8,8 +8,11 @@ module AsciiChem
|
|
|
8
8
|
module Graph
|
|
9
9
|
# `bond` is the source Model::Bond token (nil for ring-closure
|
|
10
10
|
# edges — digits carry no kind), so projections can map an
|
|
11
|
-
# assignment back to the node that renders it.
|
|
12
|
-
|
|
11
|
+
# assignment back to the node that renders it. `shape` is the
|
|
12
|
+
# SMILES reader's structural mark (:chain/:branch/:ring) — nil
|
|
13
|
+
# for edges derived from an already-linearized model.
|
|
14
|
+
Edge = Struct.new(:from, :to, :kind, :direction, :bond, :ring, :shape,
|
|
15
|
+
keyword_init: true)
|
|
13
16
|
|
|
14
17
|
class << self
|
|
15
18
|
# Returns [atoms, edges] where atoms is the atom array in walk
|
|
@@ -77,7 +80,20 @@ module AsciiChem
|
|
|
77
80
|
when AsciiChem::Model::Bond
|
|
78
81
|
pending = node
|
|
79
82
|
when AsciiChem::Model::Group, AsciiChem::Model::Molecule
|
|
83
|
+
# A group whose FIRST node is a bond is a BRANCH group:
|
|
84
|
+
# the leading bond attaches the branch to the atom
|
|
85
|
+
# before the group (the branch root), not to whatever
|
|
86
|
+
# atom the flat stream last visited — `C(-O)=O` reads
|
|
87
|
+
# C-O and C=O, never O=O. After the group the
|
|
88
|
+
# continuation resumes from the root.
|
|
89
|
+
branch_root = nil
|
|
90
|
+
if node.is_a?(AsciiChem::Model::Group) && last
|
|
91
|
+
first = node.nodes.first
|
|
92
|
+
first = first.nodes.first while first.is_a?(AsciiChem::Model::Molecule)
|
|
93
|
+
branch_root = last if first.is_a?(AsciiChem::Model::Bond)
|
|
94
|
+
end
|
|
80
95
|
walk.call(node.nodes)
|
|
96
|
+
last = branch_root if branch_root
|
|
81
97
|
end
|
|
82
98
|
end
|
|
83
99
|
end
|
|
@@ -4,11 +4,16 @@ module AsciiChem
|
|
|
4
4
|
module Structure
|
|
5
5
|
# Adjacency → linear model nodes. Atoms are emitted in the
|
|
6
6
|
# caller's creation order (a DFS order for parsers that walk
|
|
7
|
-
# depth-first).
|
|
8
|
-
#
|
|
9
|
-
# closure
|
|
10
|
-
#
|
|
11
|
-
#
|
|
7
|
+
# depth-first). The parser marks each edge's shape, and the
|
|
8
|
+
# partition rides it: a consecutive chain bond becomes a `Bond`
|
|
9
|
+
# token; a RING-closure edge becomes a pair of matching ring
|
|
10
|
+
# digits (exactly the mechanism AsciiChem's own ring syntax uses);
|
|
11
|
+
# every other non-consecutive edge roots a BRANCH — the side-tree
|
|
12
|
+
# hanging off it emits as a parenthesised group at the parent,
|
|
13
|
+
# with the branch-root binding in the structure walkers attaching
|
|
14
|
+
# the leading bond back to the parent (so the flat stream resumes
|
|
15
|
+
# from the root, and `C(=O)O` texts read as carbonyls instead of
|
|
16
|
+
# the digit soup that mis-wired aspirin's ester oxygen).
|
|
12
17
|
#
|
|
13
18
|
# Ring digits are allocated 1..9 and reused only once the
|
|
14
19
|
# previous interval (first endpoint .. second endpoint, in atom
|
|
@@ -24,24 +29,21 @@ module AsciiChem
|
|
|
24
29
|
@close_at = Array.new(MAX_DIGIT, -1)
|
|
25
30
|
@digits_for = Array.new(@atoms.length) { "" }
|
|
26
31
|
@token_before = {}
|
|
32
|
+
@branch_at = Hash.new { |hash, key| hash[key] = [] }
|
|
33
|
+
@in_branch = {}
|
|
34
|
+
@children = Hash.new { |hash, key| hash[key] = [] }
|
|
35
|
+
@branch_edges = []
|
|
27
36
|
end
|
|
28
37
|
|
|
29
38
|
# Returns the node array (Atoms and Bonds interleaved) and
|
|
30
39
|
# mutates the atoms' `ring_closures` with the allocated digits.
|
|
31
40
|
def nodes
|
|
32
|
-
|
|
33
|
-
first, last = [edge.from, edge.to].minmax
|
|
34
|
-
if last - first == 1
|
|
35
|
-
@token_before[last] = edge
|
|
36
|
-
else
|
|
37
|
-
digit = allocate_digit(first, last)
|
|
38
|
-
@digits_for[first] += digit
|
|
39
|
-
@digits_for[last] += digit
|
|
40
|
-
end
|
|
41
|
-
end
|
|
41
|
+
partition_edges
|
|
42
42
|
|
|
43
43
|
result = []
|
|
44
44
|
@atoms.each_with_index do |atom, index|
|
|
45
|
+
next if @in_branch[index]
|
|
46
|
+
|
|
45
47
|
edge = @token_before[index]
|
|
46
48
|
if index.positive? && edge
|
|
47
49
|
result << AsciiChem::Model::Bond.new(kind: edge.kind || :single,
|
|
@@ -49,12 +51,87 @@ module AsciiChem
|
|
|
49
51
|
end
|
|
50
52
|
atom.ring_closures = @digits_for[index] unless @digits_for[index].empty?
|
|
51
53
|
result << atom
|
|
54
|
+
@branch_at[index].each { |branch| result << branch_group(branch) }
|
|
52
55
|
end
|
|
53
56
|
result
|
|
54
57
|
end
|
|
55
58
|
|
|
56
59
|
private
|
|
57
60
|
|
|
61
|
+
# Partition the edges by the parser's own shape marks — tokens
|
|
62
|
+
# and digits first (so the chain map is complete), then the
|
|
63
|
+
# branch trees. A branch edge inside another branch's tree emits
|
|
64
|
+
# as a NESTED group (the tree emission walks it through
|
|
65
|
+
# @children); only top-level branches render at the flat stream.
|
|
66
|
+
# Producers without shape marks (molfile, CML) ride the flat
|
|
67
|
+
# rule: consecutive indices are chain tokens, every other
|
|
68
|
+
# non-ring edge a digit pair — the pre-branch-projection shape
|
|
69
|
+
# those producers were recorded against.
|
|
70
|
+
def partition_edges
|
|
71
|
+
branch_edges = []
|
|
72
|
+
@edges.each do |edge|
|
|
73
|
+
if edge.ring
|
|
74
|
+
first, last = [edge.from, edge.to].minmax
|
|
75
|
+
digit = allocate_digit(first, last)
|
|
76
|
+
@digits_for[first] += digit
|
|
77
|
+
@digits_for[last] += digit
|
|
78
|
+
elsif edge.shape == :branch
|
|
79
|
+
branch_edges << edge
|
|
80
|
+
elsif edge.shape == :chain || (edge.shape.nil? && (edge.to - edge.from).abs == 1)
|
|
81
|
+
@token_before[edge.to] = edge
|
|
82
|
+
@children[edge.from] << edge
|
|
83
|
+
else
|
|
84
|
+
first, last = [edge.from, edge.to].minmax
|
|
85
|
+
digit = allocate_digit(first, last)
|
|
86
|
+
@digits_for[first] += digit
|
|
87
|
+
@digits_for[last] += digit
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
branch_edges.each do |edge|
|
|
91
|
+
collect_branch(edge.to)
|
|
92
|
+
@children[edge.from] << edge
|
|
93
|
+
end
|
|
94
|
+
@branch_edges = branch_edges
|
|
95
|
+
branch_edges.each do |edge|
|
|
96
|
+
@branch_at[edge.from] << edge unless @in_branch[edge.from]
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# Marks the branch tree's atoms (everything reachable from
|
|
101
|
+
# `start` through chain bonds) for group emission.
|
|
102
|
+
def collect_branch(start)
|
|
103
|
+
@in_branch[start] = true
|
|
104
|
+
@children[start].each { |edge| collect_branch(edge.to) }
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# The branch's group: the leading bond, then the tree's atoms in
|
|
108
|
+
# walk order with their chain tokens.
|
|
109
|
+
def branch_group(edge)
|
|
110
|
+
nodes = [AsciiChem::Model::Bond.new(kind: edge.kind || :single,
|
|
111
|
+
direction: edge.direction)]
|
|
112
|
+
emit_tree(edge.to, nodes)
|
|
113
|
+
AsciiChem::Model::Group.new(bracket: :paren, nodes: nodes)
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def emit_tree(atom_index, nodes)
|
|
117
|
+
atom = @atoms[atom_index]
|
|
118
|
+
atom.ring_closures = @digits_for[atom_index] unless @digits_for[atom_index].empty?
|
|
119
|
+
nodes << atom
|
|
120
|
+
# Branch groups sit directly after their parent atom (the
|
|
121
|
+
# walkers bind a group's leading bond to the atom before it —
|
|
122
|
+
# emitting a continuation first would steal the binding).
|
|
123
|
+
@children[atom_index].each do |edge|
|
|
124
|
+
nodes << branch_group(edge) if @branch_edges.include?(edge)
|
|
125
|
+
end
|
|
126
|
+
@children[atom_index].each do |edge|
|
|
127
|
+
next if @branch_edges.include?(edge)
|
|
128
|
+
|
|
129
|
+
nodes << AsciiChem::Model::Bond.new(kind: edge.kind || :single,
|
|
130
|
+
direction: edge.direction)
|
|
131
|
+
emit_tree(edge.to, nodes)
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
|
|
58
135
|
def allocate_digit(first, last)
|
|
59
136
|
digit_index = (0...MAX_DIGIT).find { |d| @close_at[d] <= first }
|
|
60
137
|
unless digit_index
|
data/lib/asciichem/version.rb
CHANGED