FragmentUtils { protected static IAtomContainer makeAtomContainer(IAtom atom, List<IBond> parts, IAtom excludedAtom) { IAtomContainer partContainer = atom.getBuilder().newInstance(IAtomContainer.class); partContainer.addAtom(atom); for (IBond aBond : parts) { for (IAtom bondedAtom : aBond.atoms()) { if (!bondedAtom.equals(excludedAtom) && !partContainer.contains(bondedAtom)) partContainer.addAtom(bondedAtom); } if (!aBond.contains(excludedAtom)) partContainer.addBond(aBond); } return partContainer; }    }
@Test public void testMakeAtomContainer() { IChemObjectBuilder builder = SilentChemObjectBuilder.getInstance(); IAtom atom = builder.newInstance(IAtom.class, "C"); IAtom exclude = builder.newInstance(IAtom.class, "C"); IAtom a1 = builder.newInstance(IAtom.class, "C"); IAtom a2 = builder.newInstance(IAtom.class, "C"); IBond[] bonds = new IBond[]{builder.newInstance(IBond.class, atom, exclude), builder.newInstance(IBond.class, a1, a2), builder.newInstance(IBond.class, a1, atom), builder.newInstance(IBond.class, a2, exclude)}; IAtomContainer part = FragmentUtils.makeAtomContainer(atom, Arrays.asList(bonds), exclude); assertThat(part.getAtomCount(), is(3)); assertThat(part.getBondCount(), is(2)); Assert.assertTrue(part.contains(atom)); Assert.assertTrue(part.contains(a1)); Assert.assertTrue(part.contains(a2)); Assert.assertFalse(part.contains(exclude)); Assert.assertTrue(part.contains(bonds[1])); Assert.assertTrue(part.contains(bonds[2])); }