How to create a Molecule from SelectionMolecule?

ChemAxon 25dcd765a3

28-08-2013 08:52:57

I have an original molecule and I cretate a SelectionMolecule from one part of it.


I want to use that selected part as a new molecule and export it.


Here is the code:


   public static void main(String[] args) throws IOException {
// my original molecule
Molecule originalMolecule = MolImporter.importMol("CCCCC(OCC(=O)O)CCCCCC");

// add all atoms around atom 4 in depth 2 into selection
MolAtom a4 = originalMolecule.getAtom(4);
int depth = 2;
SelectionMolecule selection = new SelectionMolecule();
selection.add(a4);
addToSelection(a4, selection, depth);

// finally I fuse my selection into a new molecule so I can export it
Molecule molFromSelection = new Molecule();
molFromSelection.fuse(selection);
System.out.println(MolExporter.exportToFormat(molFromSelection, "smiles"));
}

/**
* Add all atoms around the given atom until given depth to selection.
*
* @param a atom to start from
* @param selectionMol
* @param depth
*/
private static void addToSelection(MolAtom a, SelectionMolecule selectionMol,
int depth) {
if (depth == 0)
return;
for (int i = 0; i < a.getBondCount(); i++) {
MolBond b = a.getBond(i);
if (!selectionMol.contains(b)) {
MolAtom atom = a.getLigand(i);
selectionMol.add(atom);
selectionMol.add(b);
addToSelection(atom, selectionMol, depth-1);
}
}
}