asciichem 0.3.1 → 0.3.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f025579767518b2551359a0817e2019c19bec299f0a7c77c16264dd98342df25
4
- data.tar.gz: c092bfffa44620c0bb2ef20abdba849732eaaaa01ae2f0039bf8c22a4d57baad
3
+ metadata.gz: dc89810e56d2b42835eddf9bdc0609e4520a4deccbc9451d6fab66ffc2f441ae
4
+ data.tar.gz: 39e94b4f58d2e61c44cbc377bea8fb8eb60bd8f5f0123053126fc634b94a8b5f
5
5
  SHA512:
6
- metadata.gz: 63957c098bb2df1785ce770d6e375c1f54c771e847f01f7de5b03cfd88a4321cf9c6abd6d93c5a5b6f43b1239887f1da80ec48d46d62351071ab25632bfd766f
7
- data.tar.gz: 82f5b7dda4a1f9461e5fe68dfc8566328ab2a4541101b33605582620be59651b61f358a7053e3361b5022587d3dc83be4ad1039615673076a492167025321fa0
6
+ metadata.gz: 7f765d66cad35e53f0e069bd1244cbc4bf1f4ba9ca029318a88c6276b28c91ca06f68e50d721a4f61fbbf494c496fbccf36f52f41d13b814c50501b86b8d23cb
7
+ data.tar.gz: 7762771fb9322427440533bbac79991ffa203f220456c30c8d9cbe5be5b9129928ce7905684090a07172d0d9273127c30d23183d20d5e0e31a376d04e36fbbac
@@ -23,6 +23,7 @@ module AsciiChem
23
23
  translation = AsciiChem::ModelAdapter.to_canonical_with_mapping(formula)
24
24
  xml = translation.document.to_xml
25
25
  xml = inject_atom_extensions(xml, translation.atom_mapping)
26
+ xml = inject_reaction_conditions(xml, formula)
26
27
  inject_molecule_extensions(xml, formula, translation)
27
28
  end
28
29
 
@@ -31,10 +32,12 @@ module AsciiChem
31
32
  top_level = Extensions.extract_top_level(xml)
32
33
  atom_extensions = Extensions.extract(xml)
33
34
  group_extensions = GroupExtensions.extract(xml)
35
+ reaction_conditions = extract_reaction_conditions(xml)
34
36
  wire_doc = Chemicalml::Cml::Document.from_xml(xml)
35
37
  formula = AsciiChem::ModelAdapter.from_canonical(wire_doc)
36
38
  Extensions.restore(formula, wire_doc, atom_extensions)
37
39
  GroupExtensions.restore(formula, wire_doc, group_extensions)
40
+ restore_reaction_conditions(formula, reaction_conditions)
38
41
  Extensions.restore_top_level(formula, top_level)
39
42
  formula
40
43
  end
@@ -57,6 +60,61 @@ module AsciiChem
57
60
  Extensions.inject(xml, extensions)
58
61
  end
59
62
 
63
+ # Inject reaction conditions via aci: attributes. Each Reaction
64
+ # in the formula with conditions produces aci:conditionsAbove
65
+ # and aci:conditionsBelow attributes on its <reaction> element.
66
+ def inject_reaction_conditions(xml, formula)
67
+ require 'nokogiri'
68
+ doc = Nokogiri::XML(xml)
69
+ root = doc.root
70
+ reactions = formula.nodes.select { |n| n.is_a?(AsciiChem::Model::Reaction) }
71
+ return xml if reactions.empty?
72
+
73
+ root.add_namespace(Extensions::PREFIX, Extensions::NAMESPACE) unless root.namespaces.value?(Extensions::NAMESPACE)
74
+ reactions.each_with_index do |reaction, idx|
75
+ next unless reaction.conditions
76
+
77
+ reaction_el = root.at_xpath("//cml:reaction[@id='r#{idx + 1}']", cml: Extensions::CML_NS)
78
+ next unless reaction_el
79
+
80
+ reaction_el["#{Extensions::PREFIX}:conditionsAbove"] = reaction.conditions.above if reaction.conditions.above
81
+ reaction_el["#{Extensions::PREFIX}:conditionsBelow"] = reaction.conditions.below if reaction.conditions.below
82
+ end
83
+ doc.to_xml
84
+ end
85
+
86
+ def extract_reaction_conditions(xml)
87
+ require 'nokogiri'
88
+ doc = Nokogiri::XML(xml)
89
+ result = {}
90
+ doc.xpath("//cml:reaction", cml: Extensions::CML_NS).each do |el|
91
+ id = el['id']
92
+ next unless id
93
+
94
+ above = el["#{Extensions::PREFIX}:conditionsAbove"]
95
+ below = el["#{Extensions::PREFIX}:conditionsBelow"]
96
+ result[id] = { above: above, below: below } if above || below
97
+ end
98
+ result
99
+ end
100
+
101
+ def restore_reaction_conditions(formula, conditions)
102
+ return formula if conditions.empty?
103
+
104
+ formula.nodes.each_with_index do |node, idx|
105
+ next unless node.is_a?(AsciiChem::Model::Reaction)
106
+
107
+ data = conditions["r#{idx + 1}"]
108
+ next unless data
109
+
110
+ node.conditions = AsciiChem::Model::Reaction::Conditions.new(
111
+ above: data[:above],
112
+ below: data[:below]
113
+ )
114
+ end
115
+ formula
116
+ end
117
+
60
118
  def inject_molecule_extensions(xml, formula, translation)
61
119
  xml = inject_groups(xml, formula, translation)
62
120
  inject_top_level(xml, formula)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AsciiChem
4
- VERSION = "0.3.1"
4
+ VERSION = "0.3.2"
5
5
  end
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: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.