Skip to content

Commit 0a99998

Browse files
johnmayegonw
authored andcommitted
We will be mocking this IChemModel and so no builder will be avaliable - rewrite to avoid creating IAtomContainerSets (and a temp ChemModel).
1 parent fe925c8 commit 0a99998

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

base/standard/src/main/java/org/openscience/cdk/tools/manipulator/ChemModelManipulator.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,18 +261,24 @@ public static IReaction getRelevantReaction(IChemModel chemModel, IAtom atom) {
261261
return reaction;
262262
}
263263

264+
/** Local helper to add an IAtomContainerSet to a list */
265+
private static void addAll(List<IAtomContainer> acList, IAtomContainerSet acSet) {
266+
for (IAtomContainer ac : acSet.atomContainers())
267+
acList.add(ac);
268+
}
269+
264270
/**
265271
* Returns all the AtomContainer's of a ChemModel.
266272
*/
267273
public static List<IAtomContainer> getAllAtomContainers(IChemModel chemModel) {
268-
IAtomContainerSet moleculeSet = chemModel.getBuilder().newInstance(IAtomContainerSet.class);
274+
List<IAtomContainer> res = new ArrayList<>();
269275
if (chemModel.getMoleculeSet() != null) {
270-
moleculeSet.add(chemModel.getMoleculeSet());
276+
addAll(res, chemModel.getMoleculeSet());
271277
}
272278
if (chemModel.getReactionSet() != null) {
273-
moleculeSet.add(ReactionSetManipulator.getAllMolecules(chemModel.getReactionSet()));
279+
addAll(res, ReactionSetManipulator.getAllMolecules(chemModel.getReactionSet()));
274280
}
275-
return MoleculeSetManipulator.getAllAtomContainers(moleculeSet);
281+
return res;
276282
}
277283

278284
/**

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import org.openscience.cdk.tools.ILoggingTool;
5454
import org.openscience.cdk.tools.LoggingToolFactory;
5555
import org.openscience.cdk.tools.manipulator.ChemFileManipulator;
56+
import org.openscience.cdk.tools.manipulator.ChemModelManipulator;
5657

5758
/**
5859
* Writes MDL SD files ({@cdk.cite DAL92}). A MDL SD file contains one or more molecules,
@@ -219,11 +220,7 @@ public void write(IChemObject object) throws CDKException {
219220
writeChemFile((IChemFile) object);
220221
return;
221222
} else if (object instanceof IChemModel) {
222-
IChemFile file = object.getBuilder().newInstance(IChemFile.class);
223-
IChemSequence sequence = object.getBuilder().newInstance(IChemSequence.class);
224-
sequence.addChemModel((IChemModel) object);
225-
file.addChemSequence(sequence);
226-
writeChemFile((IChemFile) file);
223+
writeChemModel((IChemModel) object);
227224
return;
228225
} else if (object instanceof IAtomContainer) {
229226
writeMolecule((IAtomContainer) object);
@@ -255,6 +252,12 @@ private void writeChemFile(IChemFile file) throws Exception {
255252
}
256253
}
257254

255+
private void writeChemModel(IChemModel model) throws Exception {
256+
for (IAtomContainer container : ChemModelManipulator.getAllAtomContainers(model)) {
257+
writeMolecule(container);
258+
}
259+
}
260+
258261
private static String replaceInvalidHeaderChars(String headerKey) {
259262
return headerKey.replaceAll("[-<>.=% ]", "_");
260263
}

0 commit comments

Comments
 (0)