Skip to content

Commit 3f04747

Browse files
committed
Correctly support SUP Sgroup in V3000
1 parent dedcf33 commit 3f04747

File tree

3 files changed

+53
-13
lines changed

3 files changed

+53
-13
lines changed

storage/ctab/src/main/java/org/openscience/cdk/io/MDLV3000Reader.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -548,19 +548,28 @@ public void readSGroup(IAtomContainer readData) throws CDKException {
548548
options = parseOptions(exhaustStringTokenizer(tokenizer));
549549
}
550550

551+
Sgroup sgroup = new Sgroup();
551552
// now interpret line
552553
if (type.startsWith("SUP")) {
554+
sgroup.setType(SgroupType.CtabAbbreviation);
553555
Iterator<String> keys = options.keySet().iterator();
554-
int atomID = -1;
555556
String label = "";
556557
while (keys.hasNext()) {
557558
String key = keys.next();
558559
String value = options.get(key);
559560
try {
560561
if (key.equals("ATOMS")) {
561562
StringTokenizer atomsTokenizer = new StringTokenizer(value);
562-
Integer.parseInt(atomsTokenizer.nextToken()); // should be 1, int atomCount =
563-
atomID = Integer.parseInt(atomsTokenizer.nextToken());
563+
int nExpected = Integer.parseInt(atomsTokenizer.nextToken());
564+
while (atomsTokenizer.hasMoreTokens()) {
565+
sgroup.addAtom(readData.getAtom(Integer.parseInt(atomsTokenizer.nextToken())-1));
566+
}
567+
} else if (key.equals("XBONDS")) {
568+
StringTokenizer xbonds = new StringTokenizer(value);
569+
int nExpected = Integer.parseInt(xbonds.nextToken());
570+
while (xbonds.hasMoreTokens()) {
571+
sgroup.addBond(readData.getBond(Integer.parseInt(xbonds.nextToken())-1));
572+
}
564573
} else if (key.equals("LABEL")) {
565574
label = value;
566575
} else {
@@ -573,18 +582,16 @@ public void readSGroup(IAtomContainer readData) throws CDKException {
573582
logger.debug(exception);
574583
throw new CDKException(error, exception);
575584
}
576-
if (atomID != -1 && label.length() > 0) {
577-
IAtom original = readData.getAtom(atomID - 1);
578-
IAtom replacement = original;
579-
if (!(original instanceof IPseudoAtom)) {
580-
replacement = readData.getBuilder().newInstance(IPseudoAtom.class,
581-
original);
582-
}
583-
((IPseudoAtom) replacement).setLabel(label);
584-
if (!replacement.equals(original))
585-
AtomContainerManipulator.replaceAtomByAtom(readData, original, replacement);
585+
if (!sgroup.getAtoms().isEmpty() && label.length() > 0) {
586+
sgroup.setSubscript(label);
586587
}
587588
}
589+
List<Sgroup> sgroups = readData.getProperty(CDKConstants.CTAB_SGROUPS);
590+
if (sgroups == null)
591+
sgroups = new ArrayList<>();
592+
sgroups.add(sgroup);
593+
readData.setProperty(CDKConstants.CTAB_SGROUPS,
594+
sgroups);
588595
} else {
589596
logger.warn("Skipping unrecognized SGROUP type: " + type);
590597
}

storage/ctab/src/test/java/org/openscience/cdk/io/MDLV2000ReaderTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
import java.io.IOException;
7070
import java.io.InputStream;
7171
import java.io.StringReader;
72+
import java.io.StringWriter;
7273
import java.nio.charset.StandardCharsets;
7374
import java.util.Iterator;
7475
import java.util.List;
@@ -1864,4 +1865,16 @@ public void testBadAtomCoordinateFormat() throws Exception {
18641865
assertThat(expr, is(expected));
18651866
}
18661867
}
1868+
1869+
@Test public void sgroupsAbbrRoundTrip() throws IOException, CDKException {
1870+
StringWriter sw = new StringWriter();
1871+
try (InputStream in = getClass().getResourceAsStream("sgroup-sup.mol3");
1872+
MDLV3000Reader mdlr = new MDLV3000Reader(in);
1873+
MDLV2000Writer mdlw = new MDLV2000Writer(sw)) {
1874+
IAtomContainer mol = SilentChemObjectBuilder.getInstance().newAtomContainer();
1875+
mol = mdlr.read(mol);
1876+
mdlw.write(mol);
1877+
}
1878+
assertThat(sw.toString(), containsString("M SAL 1 2 2 3"));
1879+
}
18671880
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Acetonitrile
2+
3+
4+
0 0 0 0 0 999 V3000
5+
M V30 BEGIN CTAB
6+
M V30 COUNTS 3 2 1 0 0
7+
M V30 BEGIN ATOM
8+
M V30 1 C 64.4346 -22.1251 0 0
9+
M V30 2 C 65.9747 -22.1251 0 0
10+
M V30 3 N 67.5145 -22.1251 0 0
11+
M V30 END ATOM
12+
M V30 BEGIN BOND
13+
M V30 1 1 1 2
14+
M V30 2 3 2 3
15+
M V30 END BOND
16+
M V30 BEGIN SGROUP
17+
M V30 1 SUP 0 ATOMS=(2 2 3) SAP=(3 2 1 1) XBONDS=(1 1) LABEL=CN
18+
M V30 END SGROUP
19+
M V30 END CTAB
20+
M END

0 commit comments

Comments
 (0)