Skip to content

Commit 8977dcc

Browse files
committed
MDLV3000Reader throws NPE when query bond is present Issue #664
When an order >= 4 set the bond order to IBond.Order.UNSET to avoid the NPE in valence calculation.
1 parent de3315b commit 8977dcc

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ public void readBondBlock(IAtomContainer readData) throws CDKException {
417417
String orderString = tokenizer.nextToken();
418418
int order = Integer.parseInt(orderString);
419419
if (order >= 4) {
420+
bond.setOrder(IBond.Order.UNSET);
420421
logger.warn("Query order types are not supported (yet). File a bug if you need it");
421422
} else {
422423
bond.setOrder(BondManipulator.createBondOrder((double) order));

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import org.openscience.cdk.exception.CDKException;
3232
import org.openscience.cdk.interfaces.IAtom;
3333
import org.openscience.cdk.interfaces.IAtomContainer;
34-
import org.openscience.cdk.io.iterator.IteratingSDFReader;
34+
import org.openscience.cdk.interfaces.IBond;
3535
import org.openscience.cdk.sgroup.Sgroup;
3636
import org.openscience.cdk.sgroup.SgroupType;
3737
import org.openscience.cdk.silent.AtomContainer;
@@ -154,4 +154,20 @@ public void testPseudoAtomLabels() throws Exception {
154154
assertThat(mol.getAtomCount(), CoreMatchers.is(31));
155155
}
156156
}
157+
158+
/**
159+
* @cdk.bug https://github.com/cdk/cdk/issues/664
160+
*
161+
* MDLV3000Reader does not yet support queries. Parsed query bonds (order >= 4) should be set to IBond.Order.UNSET
162+
* to avoid NPE in valence calculation.
163+
*/
164+
@Test public void reading_query_bond_should_not_npe() throws Exception {
165+
try (MDLV3000Reader reader = new MDLV3000Reader(getClass().getResourceAsStream("v3000Query.mol"))) {
166+
IAtomContainer container = reader.read(SilentChemObjectBuilder.getInstance().newAtomContainer());
167+
for (IBond bond: container.bonds()) {
168+
assertNotNull(bond.getOrder());
169+
}
170+
assertThat(container.getBond(4).getOrder(), is(IBond.Order.UNSET));
171+
}
172+
}
157173
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
3+
4+
0 0 0 0 0 999 V3000
5+
M V30 BEGIN CTAB
6+
M V30 COUNTS 6 6 0 0 0
7+
M V30 BEGIN ATOM
8+
M V30 1 C -4.5418 1.0401 0 0
9+
M V30 2 C -5.8753 0.2701 0 0
10+
M V30 3 C -5.8753 -1.2701 0 0
11+
M V30 4 C -4.5418 -2.0401 0 0
12+
M V30 5 C -3.208 -1.2701 0 0
13+
M V30 6 C -3.208 0.2701 0 0
14+
M V30 END ATOM
15+
M V30 BEGIN BOND
16+
M V30 1 2 1 2
17+
M V30 2 1 2 3
18+
M V30 3 2 3 4
19+
M V30 4 1 4 5
20+
M V30 5 4 5 6
21+
M V30 6 1 6 1
22+
M V30 END BOND
23+
M V30 END CTAB
24+
M END

0 commit comments

Comments
 (0)