Skip to content

Commit

Permalink
An unexpected consequence is some miss-use of the partition molecules…
Browse files Browse the repository at this point in the history
… function in the structure diagram generator. This

is a bit of a missue but reasonable - we now need check if the stereochemistry has not be split between two molecules.
We should also do the same of Sgroups (issue openned).
  • Loading branch information
johnmay authored and egonw committed Feb 3, 2022
1 parent 7fb6341 commit ade938a
Showing 1 changed file with 47 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.openscience.cdk.interfaces.IAtomContainerSet;
import org.openscience.cdk.interfaces.IBond;
import org.openscience.cdk.interfaces.IChemObject;
import org.openscience.cdk.interfaces.IChemObjectBuilder;
import org.openscience.cdk.interfaces.IDoubleBondStereochemistry;
import org.openscience.cdk.interfaces.ILonePair;
import org.openscience.cdk.interfaces.ISingleElectron;
Expand Down Expand Up @@ -87,6 +88,47 @@ public static IAtomContainerSet partitionIntoMolecules(IAtomContainer container)
return partitionIntoMolecules(container, cc.components());
}

private static IAtomContainer getComponent(Map<IAtom, IAtomContainer> cmap,
IChemObject cobj) {
if (cobj instanceof IAtom)
return cmap.get((IAtom) cobj);
else if (cobj instanceof IBond) {
IAtomContainer begMol = cmap.get(((IBond) cobj).getBegin());
IAtomContainer endMol = cmap.get(((IBond) cobj).getEnd());
return begMol == endMol ? begMol : null;
}
return null;
}

/**
* Given a component mapping atom -> molecule, provide the atom container
* to add the stereochemistry to. If the stereo is split across two
* molecules (components) null is returned.
*
* @param cmap component map
* @param se stereo element
* @return the molecule (or null if inconsistent)
*/
private static IAtomContainer getComponent(Map<IAtom, IAtomContainer> cmap,
IStereoElement<?, ?> se) {
IAtomContainer mol = getComponent(cmap, se.getFocus());
for (IChemObject cobj : se.getCarriers()) {
IAtomContainer tmp = getComponent(cmap, cobj);
if (tmp != mol)
return null; // inconsistent
}
return mol;
}

/**
* Split a molecule based on the provided component array. Note this function
* can also be used to split a single molecule, breaking bonds and distributing
* stereochemistry as needed.
*
* @param container the container
* @param components the components
* @return the partitioned set
*/
public static IAtomContainerSet partitionIntoMolecules(IAtomContainer container, int[] components) {

int maxComponentIndex = 0;
Expand All @@ -99,7 +141,6 @@ public static IAtomContainerSet partitionIntoMolecules(IAtomContainer container,
Map<IAtom, IAtom> componentAtomMap = new HashMap<>(2 * container.getAtomCount());
Map<IBond, IBond> componentBondMap = new HashMap<>(2 * container.getBondCount());


for (int i = 1; i < containers.length; i++)
containers[i] = container.getBuilder().newInstance(IAtomContainer.class);

Expand Down Expand Up @@ -130,21 +171,15 @@ public static IAtomContainerSet partitionIntoMolecules(IAtomContainer container,
for (ILonePair lonePair : container.lonePairs())
componentsMap.get(lonePair.getAtom()).addLonePair(lonePair);

for (IStereoElement stereo : container.stereoElements()) {
IChemObject focus = stereo.getFocus();
if (focus instanceof IAtom) {
if (componentsMap.containsKey(focus))
componentsMap.get(focus).addStereoElement(stereo);
} else if (focus instanceof IBond) {
if (componentsMap.containsKey(((IBond) focus).getBegin()))
componentsMap.get(((IBond) focus).getBegin()).addStereoElement(stereo);
} else {
throw new IllegalStateException("New stereo element not using an atom/bond for focus?");
}
for (IStereoElement<?,?>stereo : container.stereoElements()) {
IAtomContainer mol = getComponent(componentsMap, stereo);
if (mol != null)
mol.addStereoElement(stereo);
}
//add SGroups
List<Sgroup> sgroups = container.getProperty(CDKConstants.CTAB_SGROUPS);

// FIXME: we should do a consistent check as well!
if(sgroups !=null){
Map<Sgroup, Sgroup> old2NewSgroupMap = new HashMap<>();
List<Sgroup>[] newSgroups = new List[containers.length];
Expand Down

0 comments on commit ade938a

Please sign in to comment.