def testSubgraphIsomorphismManyLabels(self): molecule = Molecule() # specific case (species) molecule.fromAdjacencyList(""" 1 *1 C 1 {2,S} {3,S} 2 C 0 {1,S} {3,S} 3 C 0 {1,S} {2,S} """) pattern = MoleculePattern() # general case (functional group) pattern.fromAdjacencyList(""" 1 *1 C 1 {2,S}, {3,S} 2 R 0 {1,S} 3 R 0 {1,S} """) labeled1 = molecule.getLabeledAtoms() labeled2 = pattern.getLabeledAtoms() initialMap = {} for label,atom1 in labeled1.iteritems(): initialMap[atom1] = labeled2[label] self.assertTrue(molecule.isSubgraphIsomorphic(pattern, initialMap)) match, mapping = molecule.findSubgraphIsomorphisms(pattern, initialMap) self.assertTrue(match) self.assertTrue(len(mapping) == 1) for map in mapping: self.assertTrue(len(map) == min(len(molecule.atoms), len(pattern.atoms))) for key, value in map.iteritems(): self.assertTrue(key in molecule.atoms) self.assertTrue(value in pattern.atoms)
def __init__(self, chemgraph): chemgraph = chemgraph.strip() self.name = chemgraph.splitlines()[0] self.node_name = None if not chemgraph.splitlines()[1].strip().startswith('1'): #logging.debug("I think this is a compound structure: %s"%chemgraph) self.compound_structure=True self.chemgraph = chemgraph return else: self.compound_structure=False s = MoleculePattern() s.fromAdjacencyList(chemgraph.strip(), withLabel=True) self._structure = s
def testSubgraphIsomorphismAgain(self): molecule = Molecule() molecule.fromAdjacencyList(""" 1 * C 0 {2,D} {7,S} {8,S} 2 C 0 {1,D} {3,S} {9,S} 3 C 0 {2,S} {4,D} {10,S} 4 C 0 {3,D} {5,S} {11,S} 5 C 0 {4,S} {6,S} {12,S} {13,S} 6 C 0 {5,S} {14,S} {15,S} {16,S} 7 H 0 {1,S} 8 H 0 {1,S} 9 H 0 {2,S} 10 H 0 {3,S} 11 H 0 {4,S} 12 H 0 {5,S} 13 H 0 {5,S} 14 H 0 {6,S} 15 H 0 {6,S} 16 H 0 {6,S} """) pattern = MoleculePattern() pattern.fromAdjacencyList(""" 1 * C 0 {2,D} {3,S} {4,S} 2 C 0 {1,D} 3 H 0 {1,S} 4 H 0 {1,S} """) molecule.makeHydrogensExplicit() labeled1 = molecule.getLabeledAtoms().values()[0] labeled2 = pattern.getLabeledAtoms().values()[0] initialMap = {labeled1: labeled2} self.assertTrue(molecule.isSubgraphIsomorphic(pattern, initialMap)) initialMap = {labeled1: labeled2} match, mapping = molecule.findSubgraphIsomorphisms(pattern, initialMap) self.assertTrue(match) 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(pattern.atoms))) for key, value in map.iteritems(): self.assertTrue(key in molecule.atoms) self.assertTrue(value in pattern.atoms)