Repository: rdkit/rdkit — pull request #5684 (cpp)
Title: Fix R Goups in matching core are poorly positioned

<!--
Thanks for contributing a pull request! 
-->
#### Reference Issue
<!-- Example: Fixes #1234 -->

Fixes #5631 

#### What does this implement/fix? Explain your changes.

Issue #5631 has 2 structures.  When the first is decomposed the position of R2 on the core is poor.  This is fixed by recalculating the dummy position on core output if the  R group is hydrogen only.
When the second structure is decomposed the poor position of R2 is retained.  This is because it matches the heavy atom co-ordinates in the input structure.  While I don't like the position it doesn't make sense to override the user's input (the position would not look so bad if the explicit hydrogens in the input structure were retained).

![image](https://user-images.githubusercontent.com/24233741/197587972-6392b0c6-786c-4580-b095-4de746ac04e2.png)

#### Any other comments?

The core for the second structure is missing the stereo chemistry information (see #5613)


Resolved issues:
title: RDKit R Goups in matching core are poorly positioned
body: **Describe the bug**

When decomposing structures with coordinates the generated coordinates of a core R group may be poor.

**To Reproduce**

```python
# %%

from rdkit import Chem
from rdkit.Chem import AllChem
from rdkit.Chem.rdRGroupDecomposition import RGroupDecomposition, RGroupDecompositionParameters, \
    RGroupMatching, RGroupScore, RGroupLabels, RGroupCoreAlignment, RGroupLabelling
from rdkit.Chem.Draw import IPythonConsole

IPythonConsole.ipython_useSVG = True

from rdkit.Chem import PandasTools
from rdkit.Chem import Draw
from IPython.display import HTML

import pandas as pd

# %%

def do_decomp(options, core, mols):
  decomp = RGroupDecomposition([core], options)
  display_mols = []
  for mol in mols:
    if decomp.Add(mol) > -1:
      display_mols.append(mol)
    else:
      print("Failed to add mol")
  decomp.Process()
  groups = decomp.GetRGroupsAsColumns()
  df = PandasTools.RGroupDecompositionToFrame(groups, display_mols, include_core=True)
  PandasTools.ChangeMoleculeRendering(df)
  return HTML(df.to_html())
 
# %%
query_block = """
     RDKit          2D

 12 12  0  0  0  0  0  0  0  0999 V2000
   -3.4154    3.2137    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -2.5903    3.2137    0.0000 A   0  0  0  0  0  0  0  0  0  0  0  0
   -2.1778    3.9282    0.0000 A   0  0  0  0  0  0  0  0  0  0  0  0
   -2.5903    4.6427    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -3.4154    4.6427    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -3.8278    3.9282    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -4.6528    3.9282    0.0000 A   0  0  0  0  0  0  0  0  0  0  0  0
   -3.8278    2.4993    0.0000 R#  0  0  0  0  0  0  0  0  0  0  0  0
   -1.3528    3.9282    0.0000 R#  0  0  0  0  0  0  0  0  0  0  0  0
   -5.0654    4.6427    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -4.6528    5.3572    0.0000 O   0  0  0  0  0  0  0  0  0  0  0  0
   -5.8904    4.6427    0.0000 R#  0  0  0  0  0  0  0  0  0  0  0  0
  5  6  8  0
  4  5  8  0
  3  4  8  0
  2  3  8  0
  1  2  8  0
  1  6  8  0
  6  7  1  0
  1  8  1  0
  3  9  1  0
  7 10  1  0
 10 11  2  0
 10 12  1  0
M  ISO  3   8   2   9   3  12   1
M  RGP  3   8   2   9   3  12   1
M  END
"""
core = Chem.MolFromMolBlock(query_block)
core

# %%
mol_block = """
  -OEChem-02051811442D

 25 27  0     0  0  0  0  0  0999 V2000
   -0.8675    0.4975    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    0.0000    0.0000    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    0.8675    0.4975    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    0.8675    1.5027    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -0.0000    2.0104    0.0000 N   0  0  0  0  0  0  0  0  0  0  0  0
   -0.8675    1.5027    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    1.7350    2.0001    0.0000 N   0  0  0  0  0  0  0  0  0  0  0  0
    2.6070    1.5001    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    3.4790    2.0002    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    3.4790    3.0002    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    2.6159    3.5053    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    1.7439    3.0052    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    4.3465    3.4976    0.0000 N   0  0  0  0  0  0  0  0  0  0  0  0
    4.3495    4.4976    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    3.4849    5.0002    0.0000 O   0  0  0  0  0  0  0  0  0  0  0  0
    5.2170    4.9951    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    6.1281    4.5828    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    6.8007    5.3248    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    6.3049    6.1952    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    5.3214    5.9901    0.0000 N   0  0  0  0  0  0  0  0  0  0  0  0
    6.7160    7.1067    0.0000 Cl  0  0  0  0  0  0  0  0  0  0  0  0
    7.7947    5.2157    0.0000 Cl  0  0  0  0  0  0  0  0  0  0  0  0
    1.7328   -0.0038    0.0000 N   0  3  0  0  0  0  0  0  0  0  0  0
    1.7313   -1.0038    0.0000 O   0  0  0  0  0  0  0  0  0  0  0  0
    2.5995    0.4950    0.0000 O   0  5  0  0  0  0  0  0  0  0  0  0
  1  6  2  0  0  0  0
  1  2  1  0  0  0  0
  2  3  2  0  0  0  0
  3  4  1  0  0  0  0
  4  5  2  0  0  0  0
  5  6  1  0  0  0  0
  4  7  1  0  0  0  0
  7 12  1  0  0  0  0
  7  8  1  0  0  0  0
  8  9  1  0  0  0  0
  9 10  1  0  0  0  0
 10 11  1  0  0  0  0
 11 12  1  0  0  0  0
 10 13  1  0  0  0  0
 13 14  1  0  0  0  0
 14 15  2  0  0  0  0
 14 16  1  0  0  0  0
 16 20  1  0  0  0  0
 16 17  2  0  0  0  0
 17 18  1  0  0  0  0
 18 19  2  0  0  0  0
 19 20  1  0  0  0  0
 19 21  1  0  0  0  0
 18 22  1  0  0  0  0
  3 23  1  0  0  0  0
 23 24  2  0  0  0  0
 23 25  1  0  0  0  0
M  CHG  2  23   1  25  -1
M  END
$$$$
"""
mol = Chem.MolFromMolBlock(mol_block)
mol

# %%
mol_block2 = """
  -OEChem-02051811442D

 45 49  0     1  0  0  0  0  0999 V2000
   -2.9472   -3.8597    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -2.8765   -2.8622    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -3.6440   -2.2211    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -3.2684   -1.2927    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -2.2691   -1.3606    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -2.0260   -2.3354    0.0000 N   0  0  0  0  0  0  0  0  0  0  0  0
   -1.6270   -0.5940    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -1.9700    0.3454    0.0000 O   0  0  0  0  0  0  0  0  0  0  0  0
   -0.6420   -0.7667    0.0000 N   0  0  0  0  0  0  0  0  0  0  0  0
    0.0000    0.0000    0.0000 C   0  0  2  0  0  0  0  0  0  0  0  0
    1.1236   -1.3417    0.0000 H   0  0  0  0  0  0  0  0  0  0  0  0
   -0.8675    0.4975    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -0.8675    1.5027    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    0.0000    2.0104    0.0000 N   0  0  3  0  0  0  0  0  0  0  0  0
    0.8675    1.5027    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    0.8675    0.4975    0.0000 C   0  0  2  0  0  0  0  0  0  0  0  0
    1.2077   -0.4429    0.0000 H   0  0  0  0  0  0  0  0  0  0  0  0
    1.8525    0.6702    0.0000 O   0  0  0  0  0  0  0  0  0  0  0  0
    2.1954    1.6096    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    0.0000    3.0104    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -0.8098    3.5999    0.0000 N   0  0  0  0  0  0  0  0  0  0  0  0
   -0.5017    4.5529    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    0.4983    4.5516    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    0.8122    3.6017    0.0000 S   0  0  0  0  0  0  0  0  0  0  0  0
    1.0846    5.3617    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    2.0793    5.2591    0.0000 O   0  0  0  0  0  0  0  0  0  0  0  0
    0.6762    6.2745    0.0000 O   0  0  0  0  0  0  0  0  0  0  0  0
   -1.0895    5.3618    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -2.0889    5.2537    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -2.6768    6.0627    0.0000 N   0  0  0  0  0  0  0  0  0  0  0  0
   -2.2755    6.9786    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -1.2762    7.0868    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -0.6781    6.2789    0.0000 N   0  0  0  0  0  0  0  0  0  0  0  0
   -2.8669    7.7850    0.0000 O   0  0  0  0  0  0  0  0  0  0  0  0
   -3.8610    7.6760    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -4.4524    8.4824    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -5.4465    8.3734    0.0000 N   0  0  0  0  0  0  0  0  0  0  0  0
   -6.0457    9.1803    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -7.0449    9.0708    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -7.4449    8.1542    0.0000 N   0  0  0  0  0  0  0  0  0  0  0  0
   -6.8558    7.3461    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -5.8566    7.4557    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -8.4389    8.0452    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -3.7990   -0.4450    0.0000 Cl  0  0  0  0  0  0  0  0  0  0  0  0
   -4.6141   -2.4639    0.0000 Cl  0  0  0  0  0  0  0  0  0  0  0  0
  1  2  1  0  0  0  0
  2  6  1  0  0  0  0
  2  3  2  0  0  0  0
  3  4  1  0  0  0  0
  4  5  2  0  0  0  0
  5  6  1  0  0  0  0
  5  7  1  0  0  0  0
  7  8  2  0  0  0  0
  7  9  1  0  0  0  0
  9 10  1  0  0  0  0
 10 11  1  1  0  0  0
 10 16  1  0  0  0  0
 10 12  1  0  0  0  0
 12 13  1  0  0  0  0
 13 14  1  0  0  0  0
 14 15  1  0  0  0  0
 15 16  1  0  0  0  0
 16 17  1  1  0  0  0
 16 18  1  0  0  0  0
 18 19  1  0  0  0  0
 14 20  1  0  0  0  0
 20 24  1  0  0  0  0
 20 21  2  0  0  0  0
 21 22  1  0  0  0  0
 22 23  2  0  0  0  0
 23 24  1  0  0  0  0
 23 25  1  0  0  0  0
 25 26  2  0  0  0  0
 25 27  1  0  0  0  0
 22 28  1  0  0  0  0
 28 33  2  0  0  0  0
 28 29  1  0  0  0  0
 29 30  2  0  0  0  0
 30 31  1  0  0  0  0
 31 32  2  0  0  0  0
 32 33  1  0  0  0  0
 31 34  1  0  0  0  0
 34 35  1  0  0  0  0
 35 36  1  0  0  0  0
 36 37  1  0  0  0  0
 37 42  1  0  0  0  0
 37 38  1  0  0  0  0
 38 39  1  0  0  0  0
 39 40  1  0  0  0  0
 40 41  1  0  0  0  0
 41 42  1  0  0  0  0
 40 43  1  0  0  0  0
  4 44  1  0  0  0  0
  3 45  1  0  0  0  0
M  END
$$$$
"""
mol2 = Chem.MolFromMolBlock(mol_block2)
mol2

# %%
options = RGroupDecompositionParameters()
options.matchingStrategy = RGroupMatching.GreedyChunks
options.matchingStrategy = RGroupMatching.Exhaustive
options.onlyMatchAtRGroups = False
options.scoreMethod = RGroupScore.FingerprintVariance
options.allowMultipleRGroupsOnUnlabelled = True
options.removeHydrogensPostMatch = True
options.allowMultipleRGroupsOnUnlabelled = False
options.removeAllHydrogenRGroups = False
do_decomp(options, core, [mol, mol2])


```

**Expected behavior**

I expect to see the core R2 for both structures in a better geometry.

**Screenshots**
 
But I see this:

![image](https://user-images.githubusercontent.com/24233741/197569558-6f31014e-19ec-4371-8c2c-e55ba7987c05.png)

**Configuration (please complete the following information):**
 - RDKit version:  master as o 10/9/22
 - OS: [e.g. Ubuntu 20.04] Ubuntu 20.04
 - Python version (if relevant):
 - Are you using conda?
 - If you are using conda, which channel did you install the rdkit from?
 - If you are not using conda: how did you install the RDKit?

**Additional context**

I think what is happening is that the R group is added while there are explicit hydrogens on the core.  I will look at it once [PR 5618](https://github.com/rdkit/rdkit/pull/5618) is out of the way.  

Edited and added an additional example n 10/24/22.  In the first example R2 is positioned as if there are 2 explicit hydrogens on the core, while in the second example R2 uses the co-ordinates of the chiral methoxy in the input structure.  Both look rubbish in the output core, though there may be an argument for retaining the R2 group position of the second structure as that is specified in the input structure.

This behavior is not present in the 2022_03 release.  I think it comes from changes in `RDKit::replaceCore` where atom positions are now set from matching atoms in the parent molecule rather than from `MolOps::setTerminalAtomCoords`.



Hints:

Fix patch:
diff --git a/Code/GraphMol/RGroupDecomposition/RGroupDecomp.cpp b/Code/GraphMol/RGroupDecomposition/RGroupDecomp.cpp
index 8ad35951683..b8a2c72cec9 100644
--- a/Code/GraphMol/RGroupDecomposition/RGroupDecomp.cpp
+++ b/Code/GraphMol/RGroupDecomposition/RGroupDecomp.cpp
@@ -456,6 +456,7 @@ RWMOL_SPTR RGroupDecomposition::outputCoreMolecule(
     }
     auto label = data->getRlabel(atom);
     auto missingRGroup = atom->hasProp(MISSING_RGROUP);
+    bool atomRemoved = false;
     Atom *nbrAtom = nullptr;
     for (const auto &nbri :
          boost::make_iterator_range(coreWithMatches->getAtomNeighbors(atom))) {
@@ -473,6 +474,7 @@ RWMOL_SPTR RGroupDecomposition::outputCoreMolecule(
       } else if (!isUserDefinedLabel ||
                  data->params.removeAllHydrogenRGroupsAndLabels) {
         coreWithMatches->removeAtom(atomIdx);
+        atomRemoved = true;
         // if we remove an unused label from an aromatic atom,
         // we need to check whether we need to adjust its explicit
         // H count, or it will fail to kekulize
@@ -485,6 +487,13 @@ RWMOL_SPTR RGroupDecomposition::outputCoreMolecule(
         }
       }
       nbrAtom->updatePropertyCache(false);
+      if (!atomRemoved && coreWithMatches->getNumConformers() > 0) {
+        auto iter = match.rgroups.find(label);
+        if (iter != match.rgroups.end() && iter->second->is_hydrogen) {
+          MolOps::setTerminalAtomCoords(*coreWithMatches, atomIdx,
+                                        nbrAtom->getIdx());
+        }
+      }
     }
   }
   return coreWithMatches;
diff --git a/Code/GraphMol/RGroupDecomposition/RGroupDecompData.h b/Code/GraphMol/RGroupDecomposition/RGroupDecompData.h
index b96017fcd25..3130576592f 100644
--- a/Code/GraphMol/RGroupDecomposition/RGroupDecompData.h
+++ b/Code/GraphMol/RGroupDecomposition/RGroupDecompData.h
@@ -71,7 +71,7 @@ struct RGroupDecompData {
   }
 
   void addCore(const ROMol &inputCore) {
-    if (params.allowMultipleRGroupsOnUnlabelled) {
+    if (params.allowMultipleRGroupsOnUnlabelled && !params.onlyMatchAtRGroups) {
       RWMol core(inputCore);
       params.addDummyAtomsToUnlabelledCoreAtoms(core);
       cores[cores.size()] = RCore(core);


Test patch:
diff --git a/Code/GraphMol/RGroupDecomposition/testRGroupDecomp.cpp b/Code/GraphMol/RGroupDecomposition/testRGroupDecomp.cpp
index a97ad1799a9..391d470530f 100644
--- a/Code/GraphMol/RGroupDecomposition/testRGroupDecomp.cpp
+++ b/Code/GraphMol/RGroupDecomposition/testRGroupDecomp.cpp
@@ -2887,7 +2887,7 @@ M  END
 void testGithub5569() {
   BOOST_LOG(rdInfoLog)
       << "********************************************************\n";
-  BOOST_LOG(rdInfoLog) << "Test that Github5569 is fixed" << std::endl;
+  BOOST_LOG(rdInfoLog) << "Test that Github5269 is fixed" << std::endl;
   auto core = R"CTAB(
 ChemDraw09152209202D
 
@@ -3196,6 +3196,253 @@ void testMultipleGroupsToUnlabelledCoreAtom() {
   }
 }
 
+void testGitHub5631() {
+  BOOST_LOG(rdInfoLog)
+      << "********************************************************\n";
+  BOOST_LOG(rdInfoLog)
+      << "Test that Github563 (proper placement of core R groups) is fixed"
+      << std::endl;
+  auto core = R"CTAB(
+     RDKit          2D
+
+ 12 12  0  0  0  0  0  0  0  0999 V2000
+   -3.4154    3.2137    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
+   -2.5903    3.2137    0.0000 A   0  0  0  0  0  0  0  0  0  0  0  0
+   -2.1778    3.9282    0.0000 A   0  0  0  0  0  0  0  0  0  0  0  0
+   -2.5903    4.6427    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
+   -3.4154    4.6427    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
+   -3.8278    3.9282    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
+   -4.6528    3.9282    0.0000 A   0  0  0  0  0  0  0  0  0  0  0  0
+   -3.8278    2.4993    0.0000 R#  0  0  0  0  0  0  0  0  0  0  0  0
+   -1.3528    3.9282    0.0000 R#  0  0  0  0  0  0  0  0  0  0  0  0
+   -5.0654    4.6427    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
+   -4.6528    5.3572    0.0000 O   0  0  0  0  0  0  0  0  0  0  0  0
+   -5.8904    4.6427    0.0000 R#  0  0  0  0  0  0  0  0  0  0  0  0
+  5  6  8  0
+  4  5  8  0
+  3  4  8  0
+  2  3  8  0
+  1  2  8  0
+  1  6  8  0
+  6  7  1  0
+  1  8  1  0
+  3  9  1  0
+  7 10  1  0
+ 10 11  2  0
+ 10 12  1  0
+M  ISO  3   8   2   9   3  12   1
+M  RGP  3   8   2   9   3  12   1
+M  END
+)CTAB"_ctab;
+
+  auto mol1 = R"CTAB(
+  -OEChem-02051811442D
+
+ 25 27  0     0  0  0  0  0  0999 V2000
+   -0.8675    0.4975    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
+    0.0000    0.0000    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
+    0.8675    0.4975    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
+    0.8675    1.5027    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
+   -0.0000    2.0104    0.0000 N   0  0  0  0  0  0  0  0  0  0  0  0
+   -0.8675    1.5027    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
+    1.7350    2.0001    0.0000 N   0  0  0  0  0  0  0  0  0  0  0  0
+    2.6070    1.5001    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
+    3.4790    2.0002    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
+    3.4790    3.0002    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
+    2.6159    3.5053    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
+    1.7439    3.0052    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
+    4.3465    3.4976    0.0000 N   0  0  0  0  0  0  0  0  0  0  0  0
+    4.3495    4.4976    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
+    3.4849    5.0002    0.0000 O   0  0  0  0  0  0  0  0  0  0  0  0
+    5.2170    4.9951    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
+    6.1281    4.5828    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
+    6.8007    5.3248    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
+    6.3049    6.1952    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
+    5.3214    5.9901    0.0000 N   0  0  0  0  0  0  0  0  0  0  0  0
+    6.7160    7.1067    0.0000 Cl  0  0  0  0  0  0  0  0  0  0  0  0
+    7.7947    5.2157    0.0000 Cl  0  0  0  0  0  0  0  0  0  0  0  0
+    1.7328   -0.0038    0.0000 N   0  3  0  0  0  0  0  0  0  0  0  0
+    1.7313   -1.0038    0.0000 O   0  0  0  0  0  0  0  0  0  0  0  0
+    2.5995    0.4950    0.0000 O   0  5  0  0  0  0  0  0  0  0  0  0
+  1  6  2  0  0  0  0
+  1  2  1  0  0  0  0
+  2  3  2  0  0  0  0
+  3  4  1  0  0  0  0
+  4  5  2  0  0  0  0
+  5  6  1  0  0  0  0
+  4  7  1  0  0  0  0
+  7 12  1  0  0  0  0
+  7  8  1  0  0  0  0
+  8  9  1  0  0  0  0
+  9 10  1  0  0  0  0
+ 10 11  1  0  0  0  0
+ 11 12  1  0  0  0  0
+ 10 13  1  0  0  0  0
+ 13 14  1  0  0  0  0
+ 14 15  2  0  0  0  0
+ 14 16  1  0  0  0  0
+ 16 20  1  0  0  0  0
+ 16 17  2  0  0  0  0
+ 17 18  1  0  0  0  0
+ 18 19  2  0  0  0  0
+ 19 20  1  0  0  0  0
+ 19 21  1  0  0  0  0
+ 18 22  1  0  0  0  0
+  3 23  1  0  0  0  0
+ 23 24  2  0  0  0  0
+ 23 25  1  0  0  0  0
+M  CHG  2  23   1  25  -1
+M  END
+$$$$
+)CTAB"_ctab;
+
+  auto mol2 = R"CTAB(
+  -OEChem-02051811442D
+
+ 45 49  0     1  0  0  0  0  0999 V2000
+   -2.9472   -3.8597    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
+   -2.8765   -2.8622    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
+   -3.6440   -2.2211    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
+   -3.2684   -1.2927    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
+   -2.2691   -1.3606    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
+   -2.0260   -2.3354    0.0000 N   0  0  0  0  0  0  0  0  0  0  0  0
+   -1.6270   -0.5940    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
+   -1.9700    0.3454    0.0000 O   0  0  0  0  0  0  0  0  0  0  0  0
+   -0.6420   -0.7667    0.0000 N   0  0  0  0  0  0  0  0  0  0  0  0
+    0.0000    0.0000    0.0000 C   0  0  2  0  0  0  0  0  0  0  0  0
+    1.1236   -1.3417    0.0000 H   0  0  0  0  0  0  0  0  0  0  0  0
+   -0.8675    0.4975    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
+   -0.8675    1.5027    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
+    0.0000    2.0104    0.0000 N   0  0  3  0  0  0  0  0  0  0  0  0
+    0.8675    1.5027    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
+    0.8675    0.4975    0.0000 C   0  0  2  0  0  0  0  0  0  0  0  0
+    1.2077   -0.4429    0.0000 H   0  0  0  0  0  0  0  0  0  0  0  0
+    1.8525    0.6702    0.0000 O   0  0  0  0  0  0  0  0  0  0  0  0
+    2.1954    1.6096    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
+    0.0000    3.0104    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
+   -0.8098    3.5999    0.0000 N   0  0  0  0  0  0  0  0  0  0  0  0
+   -0.5017    4.5529    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
+    0.4983    4.5516    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
+    0.8122    3.6017    0.0000 S   0  0  0  0  0  0  0  0  0  0  0  0
+    1.0846    5.3617    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
+    2.0793    5.2591    0.0000 O   0  0  0  0  0  0  0  0  0  0  0  0
+    0.6762    6.2745    0.0000 O   0  0  0  0  0  0  0  0  0  0  0  0
+   -1.0895    5.3618    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
+   -2.0889    5.2537    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
+   -2.6768    6.0627    0.0000 N   0  0  0  0  0  0  0  0  0  0  0  0
+   -2.2755    6.9786    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
+   -1.2762    7.0868    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
+   -0.6781    6.2789    0.0000 N   0  0  0  0  0  0  0  0  0  0  0  0
+   -2.8669    7.7850    0.0000 O   0  0  0  0  0  0  0  0  0  0  0  0
+   -3.8610    7.6760    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
+   -4.4524    8.4824    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
+   -5.4465    8.3734    0.0000 N   0  0  0  0  0  0  0  0  0  0  0  0
+   -6.0457    9.1803    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
+   -7.0449    9.0708    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
+   -7.4449    8.1542    0.0000 N   0  0  0  0  0  0  0  0  0  0  0  0
+   -6.8558    7.3461    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
+   -5.8566    7.4557    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
+   -8.4389    8.0452    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
+   -3.7990   -0.4450    0.0000 Cl  0  0  0  0  0  0  0  0  0  0  0  0
+   -4.6141   -2.4639    0.0000 Cl  0  0  0  0  0  0  0  0  0  0  0  0
+  1  2  1  0  0  0  0
+  2  6  1  0  0  0  0
+  2  3  2  0  0  0  0
+  3  4  1  0  0  0  0
+  4  5  2  0  0  0  0
+  5  6  1  0  0  0  0
+  5  7  1  0  0  0  0
+  7  8  2  0  0  0  0
+  7  9  1  0  0  0  0
+  9 10  1  0  0  0  0
+ 10 11  1  1  0  0  0
+ 10 16  1  0  0  0  0
+ 10 12  1  0  0  0  0
+ 12 13  1  0  0  0  0
+ 13 14  1  0  0  0  0
+ 14 15  1  0  0  0  0
+ 15 16  1  0  0  0  0
+ 16 17  1  1  0  0  0
+ 16 18  1  0  0  0  0
+ 18 19  1  0  0  0  0
+ 14 20  1  0  0  0  0
+ 20 24  1  0  0  0  0
+ 20 21  2  0  0  0  0
+ 21 22  1  0  0  0  0
+ 22 23  2  0  0  0  0
+ 23 24  1  0  0  0  0
+ 23 25  1  0  0  0  0
+ 25 26  2  0  0  0  0
+ 25 27  1  0  0  0  0
+ 22 28  1  0  0  0  0
+ 28 33  2  0  0  0  0
+ 28 29  1  0  0  0  0
+ 29 30  2  0  0  0  0
+ 30 31  1  0  0  0  0
+ 31 32  2  0  0  0  0
+ 32 33  1  0  0  0  0
+ 31 34  1  0  0  0  0
+ 34 35  1  0  0  0  0
+ 35 36  1  0  0  0  0
+ 36 37  1  0  0  0  0
+ 37 42  1  0  0  0  0
+ 37 38  1  0  0  0  0
+ 38 39  1  0  0  0  0
+ 39 40  1  0  0  0  0
+ 40 41  1  0  0  0  0
+ 41 42  1  0  0  0  0
+ 40 43  1  0  0  0  0
+  4 44  1  0  0  0  0
+  3 45  1  0  0  0  0
+M  END
+)CTAB"_ctab;
+
+  RGroupDecompositionParameters params;
+  params.matchingStrategy = GreedyChunks;
+  params.allowMultipleRGroupsOnUnlabelled = false;
+  params.onlyMatchAtRGroups = true;
+  RGroupDecomposition decomp(*core, params);
+  auto result = decomp.add(*mol1);
+  TEST_ASSERT(result == 0);
+  result = decomp.add(*mol2);
+  TEST_ASSERT(result == 1);
+  decomp.process();
+  RGroupRows rows = decomp.getRGroupsAsRows();
+  TEST_ASSERT(rows.size() == 2) {
+    auto coreRgd = rows[0]["Core"];
+    auto match = std::find_if(
+        coreRgd->atoms().begin(), coreRgd->atoms().end(), [](Atom *a) {
+          return a->getAtomicNum() == 0 && a->getAtomMapNum() == 2;
+        });
+    TEST_ASSERT(match != coreRgd->atoms().end());
+    auto dummy = *match;
+    auto neighbor = *coreRgd->atomNeighbors(dummy).begin();
+    auto &conf = coreRgd->getConformer();
+    auto &dummyPoint = conf.getAtomPos(dummy->getIdx());
+    auto &neighborPoint = conf.getAtomPos(neighbor->getIdx());
+    auto length = (dummyPoint - neighborPoint).length();
+    TEST_ASSERT(abs(length - 1.0) < 0.005);
+    // R2 dummy should be directly above neighbor
+    TEST_ASSERT(abs(dummyPoint.x - neighborPoint.x) < 0.05);
+  }
+
+  {
+    auto coreRgd = rows[1]["Core"];
+    auto match = std::find_if(
+        coreRgd->atoms().begin(), coreRgd->atoms().end(), [](const Atom *a) {
+          return a->getAtomicNum() == 0 && a->getAtomMapNum() == 2;
+        });
+    TEST_ASSERT(match != coreRgd->atoms().end());
+    auto dummy = *match;
+    auto &conf = coreRgd->getConformer();
+    auto &dummyPoint = conf.getAtomPos(dummy->getIdx());
+    // R2 dummy should be over input chiral oxygen, which is first oxygen of degree 2 in input mol block
+    auto &inputPoint = mol2->getConformer(0).getAtomPos(15);
+    TEST_ASSERT(abs(dummyPoint.x - inputPoint.x) < 0.05);
+    TEST_ASSERT(abs(dummyPoint.y - inputPoint.y) < 0.05);
+  }
+}
+
 int main() {
   RDLog::InitLogs();
   boost::logging::disable_logs("rdApp.debug");
@@ -3253,6 +3500,7 @@ int main() {
   testGithub4505();
   testMultipleGroupsToUnlabelledCoreAtomGithub5573();
   testMultipleGroupsToUnlabelledCoreAtom();
+  testGitHub5631();
   BOOST_LOG(rdInfoLog)
       << "********************************************************\n";
   return 0;