def testSubgraphIsomorphismManyLabels(self): molecule = Molecule() # specific case (species) molecule.fromAdjacencyList(""" 1 *1 C u1 p0 c0 {2,S} {3,S} {4,S} 2 C u0 p0 c0 {1,S} {3,S} {5,S} {6,S} 3 C u0 p0 c0 {1,S} {2,S} {7,S} {8,S} 4 H u0 p0 c0 {1,S} 5 H u0 p0 c0 {2,S} 6 H u0 p0 c0 {2,S} 7 H u0 p0 c0 {3,S} 8 H u0 p0 c0 {3,S} """) group = Group() # general case (functional group) group.fromAdjacencyList(""" 1 *1 C u1 p0 c0 {2,S}, {3,S} 2 R!H u0 p0 c0 {1,S} 3 R!H u0 p0 c0 {1,S} """) labeled1 = molecule.getLabeledAtoms() labeled2 = group.getLabeledAtoms() initialMap = {} for label,atom1 in labeled1.iteritems(): initialMap[atom1] = labeled2[label] self.assertTrue(molecule.isSubgraphIsomorphic(group, initialMap)) mapping = molecule.findSubgraphIsomorphisms(group, initialMap) self.assertEqual(len(mapping), 2) for map in mapping: self.assertTrue(len(map) == min(len(molecule.atoms), len(group.atoms))) for key, value in map.iteritems(): self.assertTrue(key in molecule.atoms) self.assertTrue(value in group.atoms)
def testSubgraphIsomorphismManyLabels(self): molecule = Molecule() # specific case (species) molecule.fromAdjacencyList(""" 1 *1 C u1 p0 c0 {2,S} {3,S} {4,S} 2 C u0 p0 c0 {1,S} {3,S} {5,S} {6,S} 3 C u0 p0 c0 {1,S} {2,S} {7,S} {8,S} 4 H u0 p0 c0 {1,S} 5 H u0 p0 c0 {2,S} 6 H u0 p0 c0 {2,S} 7 H u0 p0 c0 {3,S} 8 H u0 p0 c0 {3,S} """) group = Group() # general case (functional group) group.fromAdjacencyList(""" 1 *1 C u1 p0 c0 {2,S}, {3,S} 2 R!H u0 p0 c0 {1,S} 3 R!H u0 p0 c0 {1,S} """) labeled1 = molecule.getLabeledAtoms() labeled2 = group.getLabeledAtoms() initialMap = {} for label, atom1 in labeled1.iteritems(): initialMap[atom1] = labeled2[label] self.assertTrue(molecule.isSubgraphIsomorphic(group, initialMap)) mapping = molecule.findSubgraphIsomorphisms(group, initialMap) self.assertEqual(len(mapping), 2) for map in mapping: self.assertTrue( len(map) == min(len(molecule.atoms), len(group.atoms))) for key, value in map.iteritems(): self.assertTrue(key in molecule.atoms) self.assertTrue(value in group.atoms)
def methyl_in_ethane(): """ 12 possible ways to put CH3 in H3C-CH3: 2 possibilities for the carbon atom 3! possibilities to organize the 3 H-atoms. total = 2 * 3! = 12 """ adjlist_mol = """ 1 C u0 p0 c0 {2,S} {3,S} {4,S} {5,S} 2 C u0 p0 c0 {1,S} {6,S} {7,S} {8,S} 3 H u0 p0 c0 {1,S} 4 H u0 p0 c0 {1,S} 5 H u0 p0 c0 {1,S} 6 H u0 p0 c0 {2,S} 7 H u0 p0 c0 {2,S} 8 H u0 p0 c0 {2,S} """ #mol = Group().fromAdjacencyList(adjlist_mol) mol= Molecule().fromAdjacencyList(adjlist_mol) adjlist_group = """ 1 C ux p0 c0 {2,S} {3,S} {4,S} 2 H u0 p0 c0 {1,S} 3 H u0 p0 c0 {1,S} 4 H u0 p0 c0 {1,S} """ group = Group().fromAdjacencyList(adjlist_group) K= np.zeros((4,8)) R= np.zeros((8,4)) result = mol.findSubgraphIsomorphisms(group) for i, match in enumerate(result): print 'Match no: {}'.format(i) for k,v in match.iteritems(): molatno = mol.atoms.index(k) gratno = group.atoms.index(v) print 'Mol atom number: {}, Group atom number: {}'.format(molatno, gratno) K[gratno,molatno]= K[gratno,molatno]+1 R[molatno,gratno]= R[molatno,gratno]+1 K= K / (len(result)) R= R / (len(result)) print K print R return result
def test_isomorphism_R(): mol = Molecule().fromAdjacencyList(""" 1 C u0 p0 c0 """, saturateH=True) gp = Group().fromAdjacencyList(""" 1 R u0 p0 c0 """) assert_true(len(mol.findSubgraphIsomorphisms(gp)) > 0)
def test_isomorphism_mol_group_not_identical(): """ Testing multiplicities in mol and group that don't match """ mol = Molecule().fromAdjacencyList(""" 1 C u0 p0 c0 """, saturateH=True) gp = Group().fromAdjacencyList(""" multiplicity [2] 1 R u0 p0 c0 """) assert_false(mol.isSubgraphIsomorphic(gp)) assert_false(len(mol.findSubgraphIsomorphisms(gp)) > 0)
def testSubgraphIsomorphismAgain(self): molecule = Molecule() molecule.fromAdjacencyList(""" 1 * C u0 p0 c0 {2,D} {7,S} {8,S} 2 C u0 p0 c0 {1,D} {3,S} {9,S} 3 C u0 p0 c0 {2,S} {4,D} {10,S} 4 C u0 p0 c0 {3,D} {5,S} {11,S} 5 C u0 p0 c0 {4,S} {6,S} {12,S} {13,S} 6 C u0 p0 c0 {5,S} {14,S} {15,S} {16,S} 7 H u0 p0 c0 {1,S} 8 H u0 p0 c0 {1,S} 9 H u0 p0 c0 {2,S} 10 H u0 p0 c0 {3,S} 11 H u0 p0 c0 {4,S} 12 H u0 p0 c0 {5,S} 13 H u0 p0 c0 {5,S} 14 H u0 p0 c0 {6,S} 15 H u0 p0 c0 {6,S} 16 H u0 p0 c0 {6,S} """) group = Group() group.fromAdjacencyList(""" 1 * C u0 p0 c0 {2,D} {3,S} {4,S} 2 C u0 p0 c0 {1,D} 3 H u0 p0 c0 {1,S} 4 H u0 p0 c0 {1,S} """) labeled1 = molecule.getLabeledAtoms().values()[0] labeled2 = group.getLabeledAtoms().values()[0] initialMap = {labeled1: labeled2} self.assertTrue(molecule.isSubgraphIsomorphic(group, initialMap)) initialMap = {labeled1: labeled2} mapping = molecule.findSubgraphIsomorphisms(group, initialMap) self.assertTrue( len(mapping) == 2, "len(mapping) = %d, should be = 2" % (len(mapping))) for map in mapping: self.assertTrue( len(map) == min(len(molecule.atoms), len(group.atoms))) for key, value in map.iteritems(): self.assertTrue(key in molecule.atoms) self.assertTrue(value in group.atoms)
def testSubgraphIsomorphism(self): """ Check the graph isomorphism functions. """ molecule = Molecule().fromSMILES('C=CC=C[CH]C') group = Group().fromAdjacencyList(""" 1 Cd u0 p0 c0 {2,D} 2 Cd u0 p0 c0 {1,D} """) self.assertTrue(molecule.isSubgraphIsomorphic(group)) mapping = molecule.findSubgraphIsomorphisms(group) self.assertTrue(len(mapping) == 4, "len(mapping) = %d, should be = 4" % (len(mapping))) for map in mapping: self.assertTrue(len(map) == min(len(molecule.atoms), len(group.atoms))) for key, value in map.iteritems(): self.assertTrue(key in molecule.atoms) self.assertTrue(value in group.atoms)
def testSubgraphIsomorphismAgain(self): molecule = Molecule() molecule.fromAdjacencyList(""" 1 * C u0 p0 c0 {2,D} {7,S} {8,S} 2 C u0 p0 c0 {1,D} {3,S} {9,S} 3 C u0 p0 c0 {2,S} {4,D} {10,S} 4 C u0 p0 c0 {3,D} {5,S} {11,S} 5 C u0 p0 c0 {4,S} {6,S} {12,S} {13,S} 6 C u0 p0 c0 {5,S} {14,S} {15,S} {16,S} 7 H u0 p0 c0 {1,S} 8 H u0 p0 c0 {1,S} 9 H u0 p0 c0 {2,S} 10 H u0 p0 c0 {3,S} 11 H u0 p0 c0 {4,S} 12 H u0 p0 c0 {5,S} 13 H u0 p0 c0 {5,S} 14 H u0 p0 c0 {6,S} 15 H u0 p0 c0 {6,S} 16 H u0 p0 c0 {6,S} """) group = Group() group.fromAdjacencyList(""" 1 * C u0 p0 c0 {2,D} {3,S} {4,S} 2 C u0 p0 c0 {1,D} 3 H u0 p0 c0 {1,S} 4 H u0 p0 c0 {1,S} """) labeled1 = molecule.getLabeledAtoms().values()[0] labeled2 = group.getLabeledAtoms().values()[0] initialMap = {labeled1: labeled2} self.assertTrue(molecule.isSubgraphIsomorphic(group, initialMap)) initialMap = {labeled1: labeled2} mapping = molecule.findSubgraphIsomorphisms(group, initialMap) self.assertTrue(len(mapping) == 2, "len(mapping) = %d, should be = 2" % (len(mapping))) for map in mapping: self.assertTrue(len(map) == min(len(molecule.atoms), len(group.atoms))) for key, value in map.iteritems(): self.assertTrue(key in molecule.atoms) self.assertTrue(value in group.atoms)
def testSubgraphIsomorphism(self): """ Check the graph isomorphism functions. """ molecule = Molecule().fromSMILES('C=CC=C[CH]C') group = Group().fromAdjacencyList(""" 1 Cd u0 p0 c0 {2,D} 2 Cd u0 p0 c0 {1,D} """) self.assertTrue(molecule.isSubgraphIsomorphic(group)) mapping = molecule.findSubgraphIsomorphisms(group) self.assertTrue( len(mapping) == 4, "len(mapping) = %d, should be = 4" % (len(mapping))) for map in mapping: self.assertTrue( len(map) == min(len(molecule.atoms), len(group.atoms))) for key, value in map.iteritems(): self.assertTrue(key in molecule.atoms) self.assertTrue(value in group.atoms)