@Test public void unionMolecules() throws IOException, CDKException { SmilesParser sp = new SmilesParser(DefaultChemObjectBuilder.getInstance()); IAtomContainer mol1 = sp.parseSmiles("OOC1=CC=CC=C1"); IAtomContainer mol2 = sp.parseSmiles("c1ccc(cc1)c2ccccc2"); int i = 0; for (IAtom atom1 : mol1.atoms()) { atom1.setID(String.valueOf((i++))); } int j = 0; for (IAtom atom2 : mol2.atoms()) { atom2.setID(String.valueOf((j++))); } MoleculeSanityCheck.aromatizeMolecule(mol1); MoleculeSanityCheck.aromatizeMolecule(mol2); Isomorphism isomorphism = new Isomorphism(mol1, mol2, Algorithm.DEFAULT, true, false, false); isomorphism.setChemFilters(false, false, false); int combinations = 1; List<String> acSet = new ArrayList<String>(); if (isomorphism.getFirstAtomMapping() != null) { for (AtomAtomMapping mapping : isomorphism.getAllAtomMapping()) { IAtomContainer union = new AtomContainer(); for (IAtom atom : mol1.atoms()) { union.addAtom(atom); } for (IBond bond : mol1.bonds()) { union.addBond(bond); } for (IBond bond : mol2.bonds()) { IAtom a1 = bond.getAtom(0); IAtom a2 = bond.getAtom(1); if (!mapping.getMappingsByAtoms().containsValue(a1) && !mapping.getMappingsByAtoms().containsValue(a2)) { if (!union.contains(a1)) { union.addAtom(a1); } if (!union.contains(a2)) { union.addAtom(a2); } union.addBond(bond); } else if (mapping.getMappingsByAtoms().containsValue(a1) && !mapping.getMappingsByAtoms().containsValue(a2)) { if (!union.contains(a2)) { union.addAtom(a2); } union.addBond( new Bond( a2, getKey(a1, mapping.getMappingsByAtoms()), bond.getOrder(), bond.getStereo())); } else if (!mapping.getMappingsByAtoms().containsValue(a1) && mapping.getMappingsByAtoms().containsValue(a2)) { if (!union.contains(a1)) { union.addAtom(a1); } union.addBond( new Bond( a1, getKey(a2, mapping.getMappingsByAtoms()), bond.getOrder(), bond.getStereo())); } } /*check if this combination is chemically valid*/ if (isChemicallyValid(union)) { String molSMILES = getSMILES(union).toString(); if (!acSet.contains(molSMILES)) { acSet.add(molSMILES); } } } } // for (String container : acSet) { // System.out.println("\n-------------" + " Combination " + combinations++ + // "--------------------"); // System.out.println("Query SMILES " + getSMILES(mol1).toString() + ", count " + // mol1.getAtomCount()); // System.out.println("Target SMILES " + getSMILES(mol2).toString() + ", count " + // mol2.getAtomCount()); // System.out.println("Union SMILES " + container + ", count " + // sp.parseSmiles(container).getAtomCount()); // } }