Class: Chemicalml::Convention::Molecular::Constraints::AtomIdsUniqueWithinMolecule

Inherits:
Constraint::DocumentConstraint show all
Defined in:
lib/chemicalml/convention/molecular/constraints/atom_ids_unique_within_molecule.rb

Overview

Atom ids MUST be unique within the eldest containing molecule per the molecular convention.

Instance Method Summary collapse

Instance Method Details

#check(document) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/chemicalml/convention/molecular/constraints/atom_ids_unique_within_molecule.rb', line 10

def check(document)
  violations = []
  walk_nodes(document) do |node, path|
    next unless molecule?(node)

    ids = atom_ids_within(node)
    duplicates = ids.group_by { |x| x }.select { |_, v| v.length > 1 }.keys
    duplicates.each do |dup|
      violations << violation(
        path: path.empty? ? "molecule[#{node.id}]" : path.join("/"),
        message: "duplicate atom id #{dup.inspect} within molecule #{node.id.inspect}"
      )
    end
  end
  violations
end