def testEquivalent(self): """ Test the GroupAtom.equivalent() method. """ for label1, atomType1 in atomTypes.iteritems(): for label2, atomType2 in atomTypes.iteritems(): atom1 = GroupAtom(atomType=[atomType1], radicalElectrons=[1], charge=[0], label='*1', lonePairs=[0]) atom2 = GroupAtom(atomType=[atomType2], radicalElectrons=[1], charge=[0], label='*1', lonePairs=[0]) if label1 == label2 or atomType2 in atomType1.generic or atomType1 in atomType2.generic: self.assertTrue(atom1.equivalent(atom2), '{0!s} is not equivalent to {1!s}'.format(atom1, atom2)) self.assertTrue(atom2.equivalent(atom1), '{0!s} is not equivalent to {1!s}'.format(atom2, atom1)) else: self.assertFalse(atom1.equivalent(atom2), '{0!s} is equivalent to {1!s}'.format(atom1, atom2)) self.assertFalse(atom2.equivalent(atom1), '{0!s} is equivalent to {1!s}'.format(atom2, atom1)) # Now see if charge and radical count are checked properly for charge in range(3): for radicals in range(2): for lonePair in range(2): atom3 = GroupAtom(atomType=[atomType1], radicalElectrons=[radicals], charge=[charge], label='*1', lonePairs=[lonePair]) if radicals == 1 and charge == 0 and lonePair == 0: self.assertTrue(atom1.equivalent(atom3), '{0!s} is not equivalent to {1!s}'.format(atom1, atom3)) self.assertTrue(atom1.equivalent(atom3), '{0!s} is not equivalent to {1!s}'.format(atom3, atom1)) else: self.assertFalse(atom1.equivalent(atom3), '{0!s} is equivalent to {1!s}'.format(atom1, atom3)) self.assertFalse(atom1.equivalent(atom3), '{0!s} is equivalent to {1!s}'.format(atom3, atom1))
def testIsSpecificCaseOf(self): """ Test the GroupAtom.isSpecificCaseOf() method. """ for label1, atomType1 in atomTypes.iteritems(): for label2, atomType2 in atomTypes.iteritems(): atom1 = GroupAtom(atomType=[atomType1], radicalElectrons=[1], charge=[0], label="*1") atom2 = GroupAtom(atomType=[atomType2], radicalElectrons=[1], charge=[0], label="*1") # And make more generic types of these two atoms atom1gen = GroupAtom(atomType=[atomType1], radicalElectrons=[0, 1], charge=[0, 1], label="*1") atom2gen = GroupAtom(atomType=[atomType2], radicalElectrons=[0, 1], charge=[0, 1], label="*1") if label1 == label2 or atomType2 in atomType1.generic: self.assertTrue( atom1.isSpecificCaseOf(atom2), "{0!s} is not a specific case of {1!s}".format(atom1, atom2) ) self.assertTrue( atom1.isSpecificCaseOf(atom2gen), "{0!s} is not a specific case of {1!s}".format(atom1, atom2gen), ) self.assertFalse( atom1gen.isSpecificCaseOf(atom2), "{0!s} is a specific case of {1!s}".format(atom1gen, atom2) ) else: self.assertFalse( atom1.isSpecificCaseOf(atom2), "{0!s} is a specific case of {1!s}".format(atom1, atom2) ) self.assertFalse( atom1.isSpecificCaseOf(atom2gen), "{0!s} is a specific case of {1!s}".format(atom1, atom2gen) ) self.assertFalse( atom1gen.isSpecificCaseOf(atom2), "{0!s} is a specific case of {1!s}".format(atom1gen, atom2) )
def testEquivalent(self): """ Test the GroupAtom.equivalent() method. """ for label1, atomType1 in atomTypes.iteritems(): for label2, atomType2 in atomTypes.iteritems(): atom1 = GroupAtom(atomType=[atomType1], radicalElectrons=[1], charge=[0], label='*1', lonePairs=[0]) atom2 = GroupAtom(atomType=[atomType2], radicalElectrons=[1], charge=[0], label='*1', lonePairs=[0]) if label1 == label2 or atomType2 in atomType1.generic or atomType1 in atomType2.generic: self.assertTrue( atom1.equivalent(atom2), '{0!s} is not equivalent to {1!s}'.format( atom1, atom2)) self.assertTrue( atom2.equivalent(atom1), '{0!s} is not equivalent to {1!s}'.format( atom2, atom1)) else: self.assertFalse( atom1.equivalent(atom2), '{0!s} is equivalent to {1!s}'.format(atom1, atom2)) self.assertFalse( atom2.equivalent(atom1), '{0!s} is equivalent to {1!s}'.format(atom2, atom1)) # Now see if charge and radical count are checked properly for charge in range(3): for radicals in range(2): for lonePair in range(2): atom3 = GroupAtom(atomType=[atomType1], radicalElectrons=[radicals], charge=[charge], label='*1', lonePairs=[lonePair]) if radicals == 1 and charge == 0 and lonePair == 0: self.assertTrue( atom1.equivalent(atom3), '{0!s} is not equivalent to {1!s}'.format( atom1, atom3)) self.assertTrue( atom1.equivalent(atom3), '{0!s} is not equivalent to {1!s}'.format( atom3, atom1)) else: self.assertFalse( atom1.equivalent(atom3), '{0!s} is equivalent to {1!s}'.format( atom1, atom3)) self.assertFalse( atom1.equivalent(atom3), '{0!s} is equivalent to {1!s}'.format( atom3, atom1))
def testApplyActionLosePair(self): """ Test the GroupAtom.applyAction() method for a LOSE_PAIR action when lonePairs is either specified or not. """ action = ['LOSE_PAIR', '*1', 1] # lonePairs specified: for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1', lonePairs=[1]) atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.decrementLonePair)) for a in atomType.decrementLonePair: self.assertTrue( a in atom.atomType, "LOSE_PAIR on {0} gave {1} not {2}".format( atomType, atom.atomType, atomType.decrementLonePair)) self.assertEqual(atom0.radicalElectrons, atom.radicalElectrons) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) self.assertEqual(atom0.lonePairs, [r + 1 for r in atom.lonePairs]) except ActionError: self.assertEqual(len(atomType.decrementLonePair), 0) #lonePairs unspecified: for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1') atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.decrementLonePair)) for a in atomType.decrementLonePair: self.assertTrue( a in atom.atomType, "LOSE_PAIR on {0} gave {1} not {2}".format( atomType, atom.atomType, atomType.decrementLonePair)) self.assertEqual(atom0.radicalElectrons, atom.radicalElectrons) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) self.assertEqual([1, 2, 3, 4], [r + 1 for r in atom.lonePairs]) except ActionError: self.assertEqual(len(atomType.decrementLonePair), 0)
def testIsSpecificCaseOf(self): """ Test the GroupAtom.isSpecificCaseOf() method. """ for label1, atomType1 in atomTypes.iteritems(): for label2, atomType2 in atomTypes.iteritems(): atom1 = GroupAtom(atomType=[atomType1], radicalElectrons=[1], charge=[0], label='*1', lonePairs=[0]) atom2 = GroupAtom(atomType=[atomType2], radicalElectrons=[1], charge=[0], label='*1', lonePairs=[0]) # And make more generic types of these two atoms atom1gen = GroupAtom(atomType=[atomType1], radicalElectrons=[0, 1], charge=[0, 1], label='*1', lonePairs=[0, 1]) atom2gen = GroupAtom(atomType=[atomType2], radicalElectrons=[0, 1], charge=[0, 1], label='*1', lonePairs=[0, 1]) if label1 == label2 or atomType2 in atomType1.generic: self.assertTrue( atom1.isSpecificCaseOf(atom2), '{0!s} is not a specific case of {1!s}'.format( atom1, atom2)) self.assertTrue( atom1.isSpecificCaseOf(atom2gen), '{0!s} is not a specific case of {1!s}'.format( atom1, atom2gen)) self.assertFalse( atom1gen.isSpecificCaseOf(atom2), '{0!s} is a specific case of {1!s}'.format( atom1gen, atom2)) else: self.assertFalse( atom1.isSpecificCaseOf(atom2), '{0!s} is a specific case of {1!s}'.format( atom1, atom2)) self.assertFalse( atom1.isSpecificCaseOf(atom2gen), '{0!s} is a specific case of {1!s}'.format( atom1, atom2gen)) self.assertFalse( atom1gen.isSpecificCaseOf(atom2), '{0!s} is a specific case of {1!s}'.format( atom1gen, atom2))
def testIsSpecificCaseOf(self): """ Test the GroupAtom.isSpecificCaseOf() method. """ for label1, atomType1 in atomTypes.iteritems(): for label2, atomType2 in atomTypes.iteritems(): atom1 = GroupAtom(atomType=[atomType1], radicalElectrons=[1], spinMultiplicity=[2], charge=[0], label='*1') atom2 = GroupAtom(atomType=[atomType2], radicalElectrons=[1], spinMultiplicity=[2], charge=[0], label='*1') if label1 == label2 or atomType2 in atomType1.generic: self.assertTrue(atom1.isSpecificCaseOf(atom2), '{0!s} is not a specific case of {1!s}'.format(atom1, atom2)) else: self.assertFalse(atom1.isSpecificCaseOf(atom2), '{0!s} is not a specific case of {1!s}'.format(atom1, atom2))
def testEquivalent(self): """ Test the GroupAtom.equivalent() method. """ for label1, atomType1 in atomTypes.iteritems(): for label2, atomType2 in atomTypes.iteritems(): atom1 = GroupAtom(atomType=[atomType1], radicalElectrons=[1], spinMultiplicity=[2], charge=[0], label='*1') atom2 = GroupAtom(atomType=[atomType2], radicalElectrons=[1], spinMultiplicity=[2], charge=[0], label='*1') if label1 == label2 or atomType2 in atomType1.generic or atomType1 in atomType2.generic: self.assertTrue(atom1.equivalent(atom2), '{0!s} is not equivalent to {1!s}'.format(atom1, atom2)) self.assertTrue(atom2.equivalent(atom1), '{0!s} is not equivalent to {1!s}'.format(atom2, atom1)) else: self.assertFalse(atom1.equivalent(atom2), '{0!s} is equivalent to {1!s}'.format(atom1, atom2)) self.assertFalse(atom2.equivalent(atom1), '{0!s} is equivalent to {1!s}'.format(atom2, atom1))
def testApplyActionLoseRadical(self): """ Test the GroupAtom.applyAction() method for a LOSE_RADICAL action. """ action = ['LOSE_RADICAL', '*1', 1] for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1') atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.decrementRadical)) for a in atomType.incrementRadical: self.assertTrue( a in atom.atomType, "LOSE_RADICAL on {0} gave {1} not {2}".format( atomType, atom.atomType, atomType.decrementRadical)) self.assertEqual(atom0.radicalElectrons, [r + 1 for r in atom.radicalElectrons]) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) except ActionError: self.assertEqual(len(atomType.decrementRadical), 0)
def testApplyActionLoseRadical(self): """ Test the GroupAtom.applyAction() method for a LOSE_RADICAL action. """ action = ['LOSE_RADICAL', '*1', 1] for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1', lonePairs=[0]) atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.decrementRadical)) for a in atomType.incrementRadical: self.assertTrue(a in atom.atomType, "LOSE_RADICAL on {0} gave {1} not {2}".format(atomType, atom.atomType, atomType.decrementRadical)) self.assertEqual(atom0.radicalElectrons, [r + 1 for r in atom.radicalElectrons]) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) self.assertEqual(atom0.lonePairs, atom.lonePairs) except ActionError: self.assertEqual(len(atomType.decrementRadical), 0) #test when radicals unspecified group = Group().fromAdjacencyList(""" 1 R ux """) #ux causes a wildcare for radicals atom1 = group.atoms[0] atom1.applyAction(action) self.assertListEqual(atom1.radicalElectrons, [0,1,2,3])
def testApplyActionLosePair(self): """ Test the GroupAtom.applyAction() method for a LOSE_PAIR action when lonePairs is either specified or not. """ action = ['LOSE_PAIR', '*1', 1] # lonePairs specified: for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1', lonePairs=[1]) atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.decrementLonePair)) for a in atomType.decrementLonePair: self.assertTrue(a in atom.atomType, "LOSE_PAIR on {0} gave {1} not {2}".format(atomType, atom.atomType, atomType.decrementLonePair)) self.assertEqual(atom0.radicalElectrons, atom.radicalElectrons) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) self.assertEqual(atom0.lonePairs, [r + 1 for r in atom.lonePairs]) except ActionError: self.assertEqual(len(atomType.decrementLonePair), 0) #lonePairs unspecified: for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1') atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.decrementLonePair)) for a in atomType.decrementLonePair: self.assertTrue(a in atom.atomType, "LOSE_PAIR on {0} gave {1} not {2}".format(atomType, atom.atomType, atomType.decrementLonePair)) self.assertEqual(atom0.radicalElectrons, atom.radicalElectrons) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) self.assertEqual([1,2,3,4], [r + 1 for r in atom.lonePairs]) except ActionError: self.assertEqual(len(atomType.decrementLonePair), 0)
def testApplyActionDecrementBond(self): """ Test the GroupAtom.applyAction() method for a CHANGE_BOND action. """ action = ['CHANGE_BOND', '*1', -1, '*2'] for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1') atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.decrementBond)) for a in atomType.decrementBond: self.assertTrue(a in atom.atomType) self.assertEqual(atom0.radicalElectrons, atom.radicalElectrons) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) except ActionError: self.assertEqual(len(atomType.decrementBond), 0)
def testApplyActionFormBond(self): """ Test the GroupAtom.applyAction() method for a FORM_BOND action. """ action = ["FORM_BOND", "*1", "S", "*2"] for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label="*1") atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.formBond)) for a in atomType.formBond: self.assertTrue(a in atom.atomType) self.assertEqual(atom0.radicalElectrons, atom.radicalElectrons) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) except ActionError: self.assertEqual(len(atomType.formBond), 0)
def testApplyActionLoseRadical(self): """ Test the GroupAtom.applyAction() method for a LOSE_RADICAL action. """ action = ['LOSE_RADICAL', '*1', 1] for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1') atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.decrementRadical)) for a in atomType.incrementRadical: self.assertTrue(a in atom.atomType, "LOSE_RADICAL on {0} gave {1} not {2}".format(atomType, atom.atomType, atomType.decrementRadical)) self.assertEqual(atom0.radicalElectrons, [r + 1 for r in atom.radicalElectrons]) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) except ActionError: self.assertEqual(len(atomType.decrementRadical), 0)
def testApplyActionFormBond(self): """ Test the GroupAtom.applyAction() method for a FORM_BOND action. """ action = ['FORM_BOND', '*1', 'S', '*2'] for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], spinMultiplicity=[2], charge=[0], label='*1') atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.formBond)) for a in atomType.formBond: self.assertTrue(a in atom.atomType) self.assertEqual(atom0.radicalElectrons, atom.radicalElectrons) self.assertEqual(atom0.spinMultiplicity, atom.spinMultiplicity) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) except ActionError: self.assertEqual(len(atomType.formBond), 0)
def testApplyActionGainRadical(self): """ Test the GroupAtom.applyAction() method for a GAIN_RADICAL action. """ action = ['GAIN_RADICAL', '*1', 1] for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], spinMultiplicity=[2], charge=[0], label='*1') atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.incrementRadical)) for a in atomType.incrementRadical: self.assertTrue(a in atom.atomType) self.assertEqual(atom0.radicalElectrons, [r - 1 for r in atom.radicalElectrons]) self.assertEqual(atom0.spinMultiplicity, [s - 1 for s in atom.spinMultiplicity]) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) except ActionError: self.assertEqual(len(atomType.incrementRadical), 0)
def testApplyActionIncrementBond(self): """ Test the GroupAtom.applyAction() method for a CHANGE_BOND action. """ action = ["CHANGE_BOND", "*1", 1, "*2"] for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], spinMultiplicity=[2], charge=[0], label="*1") atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.incrementBond)) for a in atomType.incrementBond: self.assertTrue(a in atom.atomType) self.assertEqual(atom0.radicalElectrons, atom.radicalElectrons) self.assertEqual(atom0.spinMultiplicity, atom.spinMultiplicity) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) except ActionError: self.assertEqual(len(atomType.incrementBond), 0)
def testApplyActionBreakBond(self): """ Test the GroupAtom.applyAction() method for a BREAK_BOND action. """ action = ['BREAK_BOND', '*1', 1, '*2'] for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1', lonePairs=[0]) atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.breakBond)) for a in atomType.breakBond: self.assertTrue(a in atom.atomType) self.assertEqual(atom0.radicalElectrons, atom.radicalElectrons) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) self.assertEqual(atom0.label, atom.label) self.assertEqual(atom0.lonePairs, atom.lonePairs) except ActionError: self.assertEqual(len(atomType.breakBond), 0)
def testApplyActionFormBond(self): """ Test the GroupAtom.applyAction() method for a FORM_BOND action. """ action = ['FORM_BOND', '*1', 'S', '*2'] for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1') atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.formBond)) for a in atomType.formBond: self.assertTrue(a in atom.atomType) self.assertEqual(atom0.radicalElectrons, atom.radicalElectrons) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) except ActionError: self.assertEqual(len(atomType.formBond), 0)
def testApplyActionLoseRadical(self): """ Test the GroupAtom.applyAction() method for a LOSE_RADICAL action. """ action = ['LOSE_RADICAL', '*1', 1] for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1', lonePairs=[0]) atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.decrementRadical)) for a in atomType.incrementRadical: self.assertTrue( a in atom.atomType, "LOSE_RADICAL on {0} gave {1} not {2}".format( atomType, atom.atomType, atomType.decrementRadical)) self.assertEqual(atom0.radicalElectrons, [r + 1 for r in atom.radicalElectrons]) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) self.assertEqual(atom0.lonePairs, atom.lonePairs) except ActionError: self.assertEqual(len(atomType.decrementRadical), 0) #test when radicals unspecified group = Group().fromAdjacencyList(""" 1 R ux """) #ux causes a wildcare for radicals atom1 = group.atoms[0] atom1.applyAction(action) self.assertListEqual(atom1.radicalElectrons, [0, 1, 2, 3])
def testApplyActionIncrementBond(self): """ Test the GroupAtom.applyAction() method for a CHANGE_BOND action. """ action = ['CHANGE_BOND', '*1', 1, '*2'] for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1', lonePairs=[0]) atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.incrementBond)) for a in atomType.incrementBond: self.assertTrue(a in atom.atomType) self.assertEqual(atom0.radicalElectrons, atom.radicalElectrons) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) self.assertEqual(atom0.lonePairs, atom.lonePairs) except ActionError: self.assertEqual(len(atomType.incrementBond), 0)