def testInvalidRxn(self): """IsValid() should flag up invalid reaction data""" mol = pybel.readstring("smi", "CC>>O").OBMol facade = ob.OBReactionFacade(mol) self.assertTrue(facade.IsValid()) mol.SetIsReaction(False) self.assertFalse(facade.IsValid()) mol.SetIsReaction() self.assertTrue(facade.IsValid()) atom = mol.GetAtom(1) facade.SetRole(atom, 4) self.assertFalse(facade.IsValid()) # invalid role facade.SetRole(atom, ob.REACTANT) self.assertTrue(facade.IsValid()) data = atom.GetData("rxncomp") ob.toPairInteger(data).SetValue(-1) self.assertFalse(facade.IsValid()) # invalid rxn component id atom.DeleteData(data) self.assertFalse(atom.HasData("rxncomp")) self.assertFalse(facade.IsValid()) # data missing newdata = ob.OBPairData() newdata.SetAttribute("rxncomp") newdata.SetValue("1") atom.CloneData(newdata) self.assertTrue(atom.HasData("rxncomp")) self.assertFalse(facade.IsValid()) # wrong type of data # Connected component should not belong to two different # rxn components or two different reaction roles mol = pybel.readstring("smi", "CC>>O").OBMol facade = ob.OBReactionFacade(mol) self.assertTrue(facade.IsValid()) atom = mol.GetAtom(1) facade.SetComponentId(atom, 99) self.assertFalse(facade.IsValid()) facade.SetComponentId(atom, 1) self.assertTrue(facade.IsValid()) facade.SetRole(atom, ob.AGENT) self.assertFalse(facade.IsValid())
def testFacade(self): parts = "CNO" mol = pybel.readstring("smi", ">".join(parts)).OBMol facade = ob.OBReactionFacade(mol) # basic test - copy out each component comps = [ob.REACTANT, ob.AGENT, ob.PRODUCT] for part, comp in zip(parts, comps): nmol = ob.OBMol() facade.GetComponent(nmol, comp, 0) nsmi = pybel.Molecule(nmol).write("smi").rstrip() self.assertEqual(nsmi, part) nmol = ob.OBMol() facade.GetComponent(nmol, ob.NO_REACTIONROLE, 0) self.assertEqual(0, nmol.NumAtoms()) # Reassign role facade.ReassignComponent(ob.AGENT, 0, ob.NO_REACTIONROLE) nsmi = pybel.Molecule(mol).write("smi").rstrip() self.assertEqual("C>>O", nsmi) # ...and back again facade.ReassignComponent(ob.NO_REACTIONROLE, 0, ob.AGENT) # Add a new reactant molb = pybel.readstring("smi", "S").OBMol facade.AddComponent(molb, ob.REACTANT) nsmi = pybel.Molecule(mol).write("smi").rstrip() self.assertEqual("C.S>N>O", nsmi) # ...and copy it back out nmol = ob.OBMol() facade.GetComponent(nmol, ob.REACTANT, 1) nsmi = pybel.Molecule(nmol).write("smi").rstrip() self.assertEqual("S", nsmi) # ...and also copy the O facade.GetComponent(nmol, ob.PRODUCT, 0) nmol.SetIsReaction() nsmi = pybel.Molecule(nmol).write("smi").rstrip() self.assertEqual("S>>O", nsmi)