asciichem 1.1.0.0 → 1.1.0.1
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/lib/asciichem/rinchi/native_engine.rb +178 -0
- data/lib/asciichem/rinchi/reaction.rb +17 -0
- data/lib/asciichem/rinchi/record.rb +15 -0
- data/lib/asciichem/rinchi.rb +63 -6
- data/lib/asciichem/version.rb +1 -1
- metadata +4 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 520a082b38a7c95cd7078c38882ff3b179faade3c3a7f8277d3d5bbd5d28d689
|
|
4
|
+
data.tar.gz: '0494e3f48f16fcc70b967d6a211f6b8b8335ab2eab4c33dd2706503456735015'
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: aa17d4103124b58e4818f0018804d92230cee0a786a4ecc812cd6072bf2acbec1001bb28ec1e5599d13d5e067848bff0518edbb546ee11f887997e2a21773621
|
|
7
|
+
data.tar.gz: 1047f41ab7a8e4c400496765329e6363ba316b59eaa5a436f7f6427cd9a3c036479626ed4c8ddb320e0462b8d037e2fefb006b1381e0f9c8b955d76f8164c1af
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module AsciiChem
|
|
4
|
+
module Rinchi
|
|
5
|
+
# Engine that drives the native chemomer engine's reaction-identity
|
|
6
|
+
# surface (the RInChI layer, chemomer 1.1.0+) through its C ABI —
|
|
7
|
+
# the same transport the identity derivation uses
|
|
8
|
+
# (AsciiChem::Inchi::NativeEngine): a documented external artifact,
|
|
9
|
+
# nothing vendored — build the cdylib once from the chemomer
|
|
10
|
+
# checkout (`cargo build --release`) and point CHEMOMER_LIB at it.
|
|
11
|
+
# The `ffi` gem loads lazily — the core gem stays dependency-free.
|
|
12
|
+
#
|
|
13
|
+
# AsciiChem::Rinchi.derive_from_rxnfile(rxnfile) # CHEMOMER_LIB
|
|
14
|
+
# AsciiChem::Rinchi::NativeEngine.new(lib: "/path/libchemomer.dylib")
|
|
15
|
+
#
|
|
16
|
+
# The exports this engine binds (one string return each, null on
|
|
17
|
+
# error with the message on chemomer_last_error, released with
|
|
18
|
+
# chemomer_string_free): the RXNfile edge derives the five
|
|
19
|
+
# identity lines (identifier, RAuxInfo, the three key shapes) and
|
|
20
|
+
# the RDfile edge the identifier line; decode reads a RInChI (+ its
|
|
21
|
+
# RAuxInfo companion) back into the component table — the flag
|
|
22
|
+
# line, TAB-separated `inchi<TAB>aux<TAB>key` triples, bare `<>`
|
|
23
|
+
# role separators. The forced-equilibrium reading has no C-ABI
|
|
24
|
+
# entry: an RXNfile parses directional, and only the engine's own
|
|
25
|
+
# model API (the rs gate) can derive under the forced flag.
|
|
26
|
+
class NativeEngine
|
|
27
|
+
def self.default_lib
|
|
28
|
+
ENV.fetch('CHEMOMER_LIB', nil)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
attr_reader :lib
|
|
32
|
+
|
|
33
|
+
def initialize(lib: self.class.default_lib)
|
|
34
|
+
super()
|
|
35
|
+
@lib = lib
|
|
36
|
+
unless @lib
|
|
37
|
+
raise AsciiChem::Error,
|
|
38
|
+
'chemomer native engine: set CHEMOMER_LIB to the built cdylib ' \
|
|
39
|
+
'(cargo build --release in the chemomer checkout)'
|
|
40
|
+
end
|
|
41
|
+
return if File.readable?(@lib)
|
|
42
|
+
|
|
43
|
+
raise AsciiChem::Error, "chemomer native engine: #{@lib} is not readable"
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# RXNfile → the five identity lines (engine-verbatim — the key
|
|
47
|
+
# headers included), mirroring asciichem::rinchi's RinchiRecord.
|
|
48
|
+
def derive_from_rxnfile(rxnfile)
|
|
49
|
+
Record.new(
|
|
50
|
+
rinchi: call(rxnfile, :chemomer_rinchi_from_rxn),
|
|
51
|
+
rauxinfo: call(rxnfile, :chemomer_rinchi_aux_from_rxn),
|
|
52
|
+
rinchikey: call(rxnfile, :chemomer_rinchi_short_key_from_rxn),
|
|
53
|
+
long_key: call(rxnfile, :chemomer_rinchi_long_key_from_rxn),
|
|
54
|
+
web_key: call(rxnfile, :chemomer_rinchi_web_key_from_rxn)
|
|
55
|
+
)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# RDfile → the identifier line. The C ABI exports the identifier
|
|
59
|
+
# only for this edge (the other four lines ride the RXN exports'
|
|
60
|
+
# assembly under the engine's own gate), so the remaining fields
|
|
61
|
+
# stay nil — the record carries what the transport provides.
|
|
62
|
+
def derive_from_rdfile(rdfile)
|
|
63
|
+
Record.new(rinchi: call(rdfile, :chemomer_rinchi_from_rd))
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# RInChI (+ RAuxInfo companion; pass "" when unknown) → the
|
|
67
|
+
# decoded reaction: the components carry their identities (and
|
|
68
|
+
# aux/key content) straight from the strings, the direction flag
|
|
69
|
+
# recovers the real roles, and no structures are rebuilt.
|
|
70
|
+
def decode(rinchi, rauxinfo: '')
|
|
71
|
+
table = call(rinchi, :chemomer_rinchi_decode, rauxinfo)
|
|
72
|
+
decode_component_table(table)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# The C ABI surface this engine binds — one string in, one string
|
|
76
|
+
# out (null on error, released with chemomer_string_free), decode
|
|
77
|
+
# taking the RInChI and its RAuxInfo companion. Listed once so the
|
|
78
|
+
# binding module attaches them in one walk.
|
|
79
|
+
EXPORTS = [
|
|
80
|
+
[:chemomer_rinchi_from_rxn, %i[string]],
|
|
81
|
+
[:chemomer_rinchi_aux_from_rxn, %i[string]],
|
|
82
|
+
[:chemomer_rinchi_short_key_from_rxn, %i[string]],
|
|
83
|
+
[:chemomer_rinchi_long_key_from_rxn, %i[string]],
|
|
84
|
+
[:chemomer_rinchi_web_key_from_rxn, %i[string]],
|
|
85
|
+
[:chemomer_rinchi_from_rd, %i[string]],
|
|
86
|
+
[:chemomer_rinchi_decode, %i[string string]],
|
|
87
|
+
[:chemomer_string_free, %i[pointer], :void],
|
|
88
|
+
[:chemomer_last_error, [], :pointer]
|
|
89
|
+
].freeze
|
|
90
|
+
|
|
91
|
+
# One FFI module per library path, cached: attach_function
|
|
92
|
+
# defines the symbol bindings once.
|
|
93
|
+
def self.binding_class(path)
|
|
94
|
+
@binding_classes ||= {}
|
|
95
|
+
@binding_classes[path] ||= begin
|
|
96
|
+
require 'ffi'
|
|
97
|
+
Module.new do
|
|
98
|
+
extend FFI::Library
|
|
99
|
+
|
|
100
|
+
ffi_lib path
|
|
101
|
+
EXPORTS.each { |name, args, ret = :pointer| attach_function name, args, ret }
|
|
102
|
+
|
|
103
|
+
def self.last_error
|
|
104
|
+
pointer = chemomer_last_error
|
|
105
|
+
pointer.null? ? nil : pointer.read_string
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
private
|
|
112
|
+
|
|
113
|
+
def call(input, function, second = nil)
|
|
114
|
+
binding_class = self.class.binding_class(@lib)
|
|
115
|
+
pointer = if second.nil?
|
|
116
|
+
binding_class.public_send(function, input)
|
|
117
|
+
else
|
|
118
|
+
binding_class.public_send(function, input, second)
|
|
119
|
+
end
|
|
120
|
+
# An FFI null return is a TRUTHY pointer object — `unless pointer`
|
|
121
|
+
# never fires; nil?/null? is the only correct emptiness test.
|
|
122
|
+
if pointer.nil? || pointer.null?
|
|
123
|
+
message = binding_class.last_error
|
|
124
|
+
raise AsciiChem::Error, "chemomer native engine: #{message || 'call failed'}"
|
|
125
|
+
end
|
|
126
|
+
value = pointer.read_string
|
|
127
|
+
binding_class.chemomer_string_free(pointer)
|
|
128
|
+
value
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# The component table's wire form (documented on the engine's
|
|
132
|
+
# RinchiReaction::component_table): the first line is the
|
|
133
|
+
# direction flag, the bare `<>` lines separate the three roles,
|
|
134
|
+
# and every other line is one component's TAB-separated identity
|
|
135
|
+
# triple. The strings carry no tab and no newline, so the table
|
|
136
|
+
# splits unambiguously.
|
|
137
|
+
def decode_component_table(table)
|
|
138
|
+
lines = table.split("\n")
|
|
139
|
+
directionality = decode_directionality(lines.shift)
|
|
140
|
+
roles = decode_roles(lines)
|
|
141
|
+
Reaction.new(
|
|
142
|
+
directionality: directionality,
|
|
143
|
+
reactants: roles[:reactants],
|
|
144
|
+
products: roles[:products],
|
|
145
|
+
agents: roles[:agents]
|
|
146
|
+
)
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
# The flag line: `=` speaks equilibrium; `+` (and the engine's
|
|
150
|
+
# never-emitted `-`, whose roles arrive pre-resolved) directional.
|
|
151
|
+
def decode_directionality(flag)
|
|
152
|
+
flag == '=' ? :equilibrium : :directional
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
# Walks the component lines into the three roles, the bare `<>`
|
|
156
|
+
# separators advancing the role cursor.
|
|
157
|
+
def decode_roles(lines)
|
|
158
|
+
roles = { reactants: [], products: [], agents: [] }
|
|
159
|
+
role_order = %i[reactants products agents]
|
|
160
|
+
role = 0
|
|
161
|
+
lines.each do |line|
|
|
162
|
+
if line == '<>'
|
|
163
|
+
role += 1
|
|
164
|
+
else
|
|
165
|
+
roles[role_order[role]] << decode_component(line)
|
|
166
|
+
end
|
|
167
|
+
end
|
|
168
|
+
roles
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
# One line's identity triple (InChI, AuxInfo, InChIKey).
|
|
172
|
+
def decode_component(line)
|
|
173
|
+
inchi, aux, inchikey = line.split("\t")
|
|
174
|
+
Component.new(inchi: inchi.to_s, aux: aux.to_s, inchikey: inchikey.to_s)
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module AsciiChem
|
|
4
|
+
module Rinchi
|
|
5
|
+
# One decoded component's identity: the standard InChI string, its
|
|
6
|
+
# AuxInfo companion (empty on a bare decode), and the InChIKey.
|
|
7
|
+
Component = Struct.new(:inchi, :aux, :inchikey, keyword_init: true)
|
|
8
|
+
|
|
9
|
+
# The decoded reaction — the decode direction's output: the
|
|
10
|
+
# components carry their identities straight from the identifier
|
|
11
|
+
# string (no structures are rebuilt), the direction flag
|
|
12
|
+
# (`:directional` / `:equilibrium`) recovering which group held
|
|
13
|
+
# which role.
|
|
14
|
+
Reaction = Struct.new(:directionality, :reactants, :products, :agents,
|
|
15
|
+
keyword_init: true)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module AsciiChem
|
|
4
|
+
module Rinchi
|
|
5
|
+
# The assembled reaction-identity record — the derive direction's
|
|
6
|
+
# output (mirrors asciichem::rinchi's RinchiRecord): the
|
|
7
|
+
# identifier, its RAuxInfo companion line, and the three RInChIKey
|
|
8
|
+
# shapes (the short hashed one under `rinchikey`, per the shared
|
|
9
|
+
# corpus's field naming). Every string is engine-verbatim — the
|
|
10
|
+
# key headers included; the corpus records the keys headerless and
|
|
11
|
+
# the runners strip.
|
|
12
|
+
Record = Struct.new(:rinchi, :rauxinfo, :rinchikey, :long_key, :web_key,
|
|
13
|
+
keyword_init: true)
|
|
14
|
+
end
|
|
15
|
+
end
|
data/lib/asciichem/rinchi.rb
CHANGED
|
@@ -3,16 +3,29 @@
|
|
|
3
3
|
module AsciiChem
|
|
4
4
|
# Reaction identity via RInChI (TODO.v2/13 item 4; TODO.v2/10 pattern).
|
|
5
5
|
#
|
|
6
|
-
# Like InChI, the algorithm is never reimplemented
|
|
7
|
-
#
|
|
8
|
-
# Model::Reaction → RXN → engine → RInChI keys.
|
|
6
|
+
# Like InChI, the algorithm is never reimplemented. Two layers share
|
|
7
|
+
# this module, one per transport:
|
|
9
8
|
#
|
|
10
|
-
#
|
|
11
|
-
#
|
|
12
|
-
#
|
|
9
|
+
# - **Per-molecule identities** (the drawn reaction in the model):
|
|
10
|
+
# engines wrap the IUPAC RInChI software (`rinchi-1`). The pipe is
|
|
11
|
+
# Model::Reaction → RXN → engine → RInChI keys. Engines are
|
|
12
|
+
# opt-in: set `AsciiChem::Rinchi.engine` (or pass one per call).
|
|
13
|
+
# Without an engine every derivation raises EngineMissingError
|
|
14
|
+
# with install guidance — no silent fallback.
|
|
15
|
+
# - **The reaction-identity surface** (the chemomer engine's RInChI
|
|
16
|
+
# layer, mirroring asciichem::rinchi): a recorded reaction's
|
|
17
|
+
# RXNfile/RDfile text derives the five identity lines, and a
|
|
18
|
+
# RInChI string decodes back into its components. The transport
|
|
19
|
+
# is the chemomer cdylib through its C ABI
|
|
20
|
+
# (`Rinchi::NativeEngine`, CHEMOMER_LIB) — the same opt-in
|
|
21
|
+
# posture as the identity derivation's native engine.
|
|
13
22
|
module Rinchi
|
|
14
23
|
autoload :BinaryEngine, 'asciichem/rinchi/binary_engine'
|
|
15
24
|
autoload :Identity, 'asciichem/rinchi/identity'
|
|
25
|
+
autoload :NativeEngine, 'asciichem/rinchi/native_engine'
|
|
26
|
+
autoload :Record, 'asciichem/rinchi/record'
|
|
27
|
+
autoload :Component, 'asciichem/rinchi/reaction'
|
|
28
|
+
autoload :Reaction, 'asciichem/rinchi/reaction'
|
|
16
29
|
|
|
17
30
|
INSTALL_GUIDE = 'install the IUPAC RInChI software ' \
|
|
18
31
|
'(https://github.com/IUPAC-InChI/RInChI — build ' \
|
|
@@ -21,6 +34,10 @@ module AsciiChem
|
|
|
21
34
|
'AsciiChem::Rinchi::BinaryEngine.new ' \
|
|
22
35
|
'(or pass bin: "path/to/rinchi_cmdline")'
|
|
23
36
|
|
|
37
|
+
NATIVE_INSTALL_GUIDE = 'build the chemomer engine cdylib ' \
|
|
38
|
+
'(cargo build --release in the chemomer ' \
|
|
39
|
+
'checkout) and set CHEMOMER_LIB to its path'
|
|
40
|
+
|
|
24
41
|
class << self
|
|
25
42
|
attr_accessor :engine
|
|
26
43
|
|
|
@@ -33,6 +50,46 @@ module AsciiChem
|
|
|
33
50
|
|
|
34
51
|
selected.identity(reaction)
|
|
35
52
|
end
|
|
53
|
+
|
|
54
|
+
# -- the chemomer reaction-identity surface (the recorded
|
|
55
|
+
# bytes' derive/decode directions) --------------------------
|
|
56
|
+
|
|
57
|
+
# Assembles a reaction's identity record from its V2000 RXNfile
|
|
58
|
+
# text — the five engine-verbatim lines (identifier, RAuxInfo,
|
|
59
|
+
# the three RInChIKey shapes).
|
|
60
|
+
def derive_from_rxnfile(rxnfile, engine: nil)
|
|
61
|
+
selected_engine(engine).derive_from_rxnfile(rxnfile)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Assembles the identity record from an MDL RDfile (the RXN
|
|
65
|
+
# block verbatim plus the `$DATUM $MFMT` agent scan). The C ABI
|
|
66
|
+
# exports the identifier line only for this edge; the remaining
|
|
67
|
+
# fields stay nil.
|
|
68
|
+
def derive_from_rdfile(rdfile, engine: nil)
|
|
69
|
+
selected_engine(engine).derive_from_rdfile(rdfile)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Decodes a RInChI string (plus its RAuxInfo companion when
|
|
73
|
+
# known) into the reaction it identifies — the components carry
|
|
74
|
+
# their identities straight from the string; the direction flag
|
|
75
|
+
# recovers which group held which role.
|
|
76
|
+
def decode(rinchi, rauxinfo: '', engine: nil)
|
|
77
|
+
selected_engine(engine).decode(rinchi, rauxinfo: rauxinfo)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
private
|
|
81
|
+
|
|
82
|
+
def selected_engine(engine)
|
|
83
|
+
return engine if engine
|
|
84
|
+
|
|
85
|
+
lib = NativeEngine.default_lib
|
|
86
|
+
if lib.nil?
|
|
87
|
+
raise EngineMissingError,
|
|
88
|
+
"no chemomer native engine configured — #{NATIVE_INSTALL_GUIDE}"
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
NativeEngine.new(lib: lib)
|
|
92
|
+
end
|
|
36
93
|
end
|
|
37
94
|
end
|
|
38
95
|
end
|
data/lib/asciichem/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: asciichem
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.1.0.
|
|
4
|
+
version: 1.1.0.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Ribose Inc.
|
|
@@ -283,6 +283,9 @@ files:
|
|
|
283
283
|
- lib/asciichem/rinchi.rb
|
|
284
284
|
- lib/asciichem/rinchi/binary_engine.rb
|
|
285
285
|
- lib/asciichem/rinchi/identity.rb
|
|
286
|
+
- lib/asciichem/rinchi/native_engine.rb
|
|
287
|
+
- lib/asciichem/rinchi/reaction.rb
|
|
288
|
+
- lib/asciichem/rinchi/record.rb
|
|
286
289
|
- lib/asciichem/ring_bonds.rb
|
|
287
290
|
- lib/asciichem/rxn.rb
|
|
288
291
|
- lib/asciichem/rxn/writer.rb
|