Skip to content

Commit

Permalink
We will be mocking this IChemModel and so no builder will be avaliabl…
Browse files Browse the repository at this point in the history
…e - rewrite to avoid creating IAtomContainerSets (and a temp ChemModel).
  • Loading branch information
johnmay authored and egonw committed Dec 24, 2021
1 parent fe925c8 commit 0a99998
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -261,18 +261,24 @@ public static IReaction getRelevantReaction(IChemModel chemModel, IAtom atom) {
return reaction;
}

/** Local helper to add an IAtomContainerSet to a list */
private static void addAll(List<IAtomContainer> acList, IAtomContainerSet acSet) {
for (IAtomContainer ac : acSet.atomContainers())
acList.add(ac);
}

/**
* Returns all the AtomContainer's of a ChemModel.
*/
public static List<IAtomContainer> getAllAtomContainers(IChemModel chemModel) {
IAtomContainerSet moleculeSet = chemModel.getBuilder().newInstance(IAtomContainerSet.class);
List<IAtomContainer> res = new ArrayList<>();
if (chemModel.getMoleculeSet() != null) {
moleculeSet.add(chemModel.getMoleculeSet());
addAll(res, chemModel.getMoleculeSet());
}
if (chemModel.getReactionSet() != null) {
moleculeSet.add(ReactionSetManipulator.getAllMolecules(chemModel.getReactionSet()));
addAll(res, ReactionSetManipulator.getAllMolecules(chemModel.getReactionSet()));
}
return MoleculeSetManipulator.getAllAtomContainers(moleculeSet);
return res;
}

/**
Expand Down
13 changes: 8 additions & 5 deletions storage/ctab/src/main/java/org/openscience/cdk/io/SDFWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.openscience.cdk.tools.ILoggingTool;
import org.openscience.cdk.tools.LoggingToolFactory;
import org.openscience.cdk.tools.manipulator.ChemFileManipulator;
import org.openscience.cdk.tools.manipulator.ChemModelManipulator;

/**
* Writes MDL SD files ({@cdk.cite DAL92}). A MDL SD file contains one or more molecules,
Expand Down Expand Up @@ -219,11 +220,7 @@ public void write(IChemObject object) throws CDKException {
writeChemFile((IChemFile) object);
return;
} else if (object instanceof IChemModel) {
IChemFile file = object.getBuilder().newInstance(IChemFile.class);
IChemSequence sequence = object.getBuilder().newInstance(IChemSequence.class);
sequence.addChemModel((IChemModel) object);
file.addChemSequence(sequence);
writeChemFile((IChemFile) file);
writeChemModel((IChemModel) object);
return;
} else if (object instanceof IAtomContainer) {
writeMolecule((IAtomContainer) object);
Expand Down Expand Up @@ -255,6 +252,12 @@ private void writeChemFile(IChemFile file) throws Exception {
}
}

private void writeChemModel(IChemModel model) throws Exception {
for (IAtomContainer container : ChemModelManipulator.getAllAtomContainers(model)) {
writeMolecule(container);
}
}

private static String replaceInvalidHeaderChars(String headerKey) {
return headerKey.replaceAll("[-<>.=% ]", "_");
}
Expand Down

0 comments on commit 0a99998

Please sign in to comment.