Skip to content

Commit c09dbb0

Browse files
committed
Ensure consistent behaviour when non-matching atom-maps are provided.
1 parent cd1d403 commit c09dbb0

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

app/depict/src/main/java/org/openscience/cdk/depict/DepictionGenerator.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,10 @@
5656
import java.util.Collection;
5757
import java.util.Collections;
5858
import java.util.HashMap;
59+
import java.util.HashSet;
5960
import java.util.List;
6061
import java.util.Map;
62+
import java.util.Set;
6163
import java.util.TreeMap;
6264

6365
/**
@@ -513,6 +515,18 @@ public Depiction depict(IReaction rxn) throws CDKException {
513515
private Map<IChemObject, Color> makeHighlightAtomMap(List<IAtomContainer> reactants,
514516
List<IAtomContainer> products) {
515517

518+
// only highlight atom-maps that appear in both the reactant+product
519+
Set<Integer> reactantMapIdxs = new HashSet<>();
520+
Set<Integer> productMapIdxs = new HashSet<>();
521+
for (IAtomContainer mol : reactants) {
522+
for (IAtom atom : mol.atoms())
523+
reactantMapIdxs.add(accessAtomMap(atom));
524+
}
525+
for (IAtomContainer mol : products) {
526+
for (IAtom atom : mol.atoms())
527+
productMapIdxs.add(accessAtomMap(atom));
528+
}
529+
516530
Map<IChemObject, Color> colorMap = new HashMap<>();
517531
Map<Integer, Color> mapToColor = new HashMap<>();
518532
Map<Integer, IAtom> amap = new TreeMap<>();
@@ -521,7 +535,7 @@ private Map<IChemObject, Color> makeHighlightAtomMap(List<IAtomContainer> reacta
521535
int prevPalletIdx = colorIdx;
522536
for (IAtom atom : mol.atoms()) {
523537
int mapidx = accessAtomMap(atom);
524-
if (mapidx > 0) {
538+
if (mapidx > 0 && productMapIdxs.contains(mapidx)) {
525539
if (prevPalletIdx == colorIdx) {
526540
colorIdx++; // select next color
527541
if (colorIdx >= atomMapColors.length)
@@ -548,7 +562,7 @@ private Map<IChemObject, Color> makeHighlightAtomMap(List<IAtomContainer> reacta
548562
for (IAtomContainer mol : products) {
549563
for (IAtom atom : mol.atoms()) {
550564
int mapidx = accessAtomMap(atom);
551-
if (mapidx > 0) {
565+
if (mapidx > 0 && reactantMapIdxs.contains(mapidx)) {
552566
colorMap.put(atom, mapToColor.get(mapidx));
553567
}
554568
}

0 commit comments

Comments
 (0)