def testSDF(self): # get molecule with SDF annotations cid = 6830 cmd.set('load_object_props_default', '') cmd.load('CID_%d.sdf' % cid, 'm1') self.assertEqual(None, cmd.get_property('PUBCHEM_COMPOUND_CID', 'm1'), 'property loaded, but should not') cmd.delete('*') cmd.load('CID_%d.sdf' % cid, 'm1', object_props='*') self.assertEqual(cid, cmd.get_property('PUBCHEM_COMPOUND_CID', 'm1')) v_pc = cmd.get_property('PUBCHEM_MMFF94_PARTIAL_CHARGES', 'm1') lines = v_pc.rstrip().splitlines() n_pc = int(lines[0]) self.assertEqual(n_pc, len(lines) - 1) # test loading selective properties cmd.set( 'load_object_props_default', "PUBCHEM_COMPOUND_CID PUBCHEM_CONFORMER_RMSD PUBCHEM_CONFORMER_DIVERSEORDER" ) cmd.load('CID_%d.sdf' % cid, 'm2') self.assertEqual(cid, cmd.get_property('PUBCHEM_COMPOUND_CID', 'm2')) self.assertEqual( None, cmd.get_property('PUBCHEM_EFFECTIVE_ROTOR_COUNT', 'm2'))
def testMAEsaveLoadSessions(self): cmd.load(self.datafile('1d_smiles.mae'), '1d_smiles', object_props='*') allpropdata = {} objs = cmd.get_object_list() for obj in objs: props = cmd.get_property_list(obj) allpropdata[obj] = {} for prop in props: allpropdata[obj][prop] = cmd.get_property(prop, obj) with testing.mktemp('.pse') as psefilename: cmd.save(psefilename) cmd.load(psefilename) # this is to fail the test on purpose # cmd.set_property('i_m_ct_format', 3, '1d_smiles.mol_13') objs = cmd.get_object_list() for obj in objs: props = cmd.get_property_list(obj) # test to make sure there are no extra properties or not enough properties self.assertEqual(set(props), set(allpropdata[obj].keys())) # test to make sure all property values are the same for prop in props: try: self.assertTrue(allpropdata[obj][prop] == cmd.get_property(prop, obj)) except: self.fail('properties are not the same: obj=%s prop=%s' % (obj, prop))
def testStringType(self, str1, str2): cmd.fragment('ala') cmd.set_property('string_property_1', str1, 'ala') cmd.set_property('string_property_2', str2, 'ala') self.assertEqual(cmd.get_property('string_property_1', 'ala'), str1) self.assertEqual(cmd.get_property('string_property_2', 'ala'), str2) # test setting boolean type by string cmd.set_property('string_property_1', str1, 'ala', proptype=PROPERTY_STRING) cmd.set_property('string_property_2', str1, 'ala', proptype=PROPERTY_STRING) self.assertEqual(cmd.get_property('string_property_1', 'ala'), str1) self.assertEqual(cmd.get_property('string_property_2', 'ala'), str1) # overwritting
def testCopyObjectProperties(self): cmd.load(self.datafile('1molecule.mae'), 'test', object_props='*') objs = cmd.get_object_list() for obj in objs: obj_copy = '%s_copy' % obj cmd.create(obj_copy, obj, copy_properties=True) props1 = cmd.get_property_list(obj) props2 = cmd.get_property_list(obj_copy) self.assertEqual(set(props1), set(props2)) for prop in props1: prop1 = cmd.get_property(prop, obj) prop2 = cmd.get_property(prop, obj_copy) self.assertEqual(prop1, prop2)
def testSimple(self): cmd.fab('A', 'm1') cmd.fab('A', 'm2') v1 = 'foo' v2 = 'bar' v3 = 'com' # single state cmd.set_property('filename', v1, 'm1') self.assertTrue('foo' == v1) self.assertTrue(cmd.get_property('filename', 'm1') == v1) self.assertTrue(cmd.get_property('filename', 'm2') == None) # multiple objects cmd.set_property('filename', v1) self.assertTrue(cmd.get_property('filename', 'm2') == v1) # two states cmd.create('m1', 'm1', 1, 2) self.assertTrue(cmd.count_states() == 2) # set for all states cmd.set_property('filename', v1, 'm1') self.assertTrue(cmd.get_property('filename', 'm1', 2) == v1) # set for particular state cmd.set_property('filename', v2, 'm1', 2) self.assertTrue(cmd.get_property('filename', 'm1', 1) == v1) self.assertTrue(cmd.get_property('filename', 'm1', 2) == v2) # set for current state cmd.frame(2) cmd.set_property('filename', v3, 'm1', -1) self.assertTrue(cmd.get_property('filename', 'm1', 1) == v1) self.assertTrue(cmd.get_property('filename', 'm1', 2) == v3)
def testMAEchempy(self, molfilename): cmd.load(self.datafile(molfilename), 'test', object_props='*', atom_props='*') objs = cmd.get_object_list() for obj in objs: idxToVal = {} natoms = cmd.count_atoms(obj) for i in range(natoms): idxToVal[i+1] = i*10 cmd.set_atom_property('test_prop', i*10, "index %d and %s" % (i+1, obj)) model = cmd.get_model(obj) # test to make sure the properties that exist are the same prop_list= cmd.get_property_list(obj) mol_prop_list = [x[0] for x in model.molecule_properties] self.assertEqual(set(prop_list), set(mol_prop_list)) # need to test whether the values are the same for prop, val in model.molecule_properties: propval = cmd.get_property(prop, obj) self.assertEqual(propval, val) self.assertEqual(natoms, len(model.atom)) # need to test values of all atom properties, including the ones that were set above stored.prop_lookup = {} cmd.iterate(obj, "stored.prop_lookup[index-1] = properties.all") idx = 0 for at in model.atom: for prop in at.atom_properties.keys(): val = at.atom_properties[prop] self.assertEqual(val, stored.prop_lookup[idx][prop]) idx += 1
def testFloatType(self, val1, val2): cmd.fragment('ala') cmd.set_property('float_property_1', val1, 'ala') cmd.set_property('float_property_2', val2, 'ala') self.assertAlmostEqual(cmd.get_property('float_property_1', 'ala'), val1) self.assertAlmostEqual(cmd.get_property('float_property_2', 'ala'), val2) # test setting boolean type by string cmd.set_property('float_property_1', str(val1), 'ala', proptype=PROPERTY_FLOAT) cmd.set_property('float_property_2', str(val1), 'ala', proptype=PROPERTY_FLOAT) cmd.set_property('float_property_3', str(val1), 'ala', proptype=PROPERTY_FLOAT) cmd.set_property('float_property_4', str(val2), 'ala', proptype=PROPERTY_FLOAT) self.assertAlmostEqual(cmd.get_property('float_property_1', 'ala'), val1) self.assertAlmostEqual(cmd.get_property('float_property_2', 'ala'), val1) # overwritting self.assertAlmostEqual(cmd.get_property('float_property_3', 'ala'), val1) self.assertAlmostEqual(cmd.get_property('float_property_4', 'ala'), val2)
def testIntegerType(self, val1, val2): cmd.fragment('ala') cmd.set_property('int_property_1', val1, 'ala') cmd.set_property('int_property_2', val2, 'ala') self.assertEqual(cmd.get_property('int_property_1', 'ala'), val1) self.assertEqual(cmd.get_property('int_property_2', 'ala'), val2) # test setting boolean type by string cmd.set_property('int_property_1', str(val1), 'ala', proptype=PROPERTY_INT) cmd.set_property('int_property_2', str(val1), 'ala', proptype=PROPERTY_INT) cmd.set_property('int_property_3', str(val1), 'ala', proptype=PROPERTY_INT) cmd.set_property('int_property_4', str(val2), 'ala', proptype=PROPERTY_INT) self.assertEqual(cmd.get_property('int_property_1', 'ala'), val1) self.assertEqual(cmd.get_property('int_property_2', 'ala'), val1) # overwritting self.assertEqual(cmd.get_property('int_property_3', 'ala'), val1) self.assertEqual(cmd.get_property('int_property_4', 'ala'), val2)
def testBooleanType(self): cmd.fragment('ala') cmd.set_property('bool_property_1', True, 'ala') cmd.set_property('bool_property_2', False, 'ala') self.assertEqual(cmd.get_property('bool_property_1', 'ala'), True) self.assertEqual(cmd.get_property('bool_property_2', 'ala'), False) # test setting boolean type by string cmd.set_property('bool_property_1', "True", 'ala', proptype=PROPERTY_BOOLEAN) cmd.set_property('bool_property_2', "True", 'ala', proptype=PROPERTY_BOOLEAN) cmd.set_property('bool_property_3', "True", 'ala', proptype=PROPERTY_BOOLEAN) cmd.set_property('bool_property_4', "False", 'ala', proptype=PROPERTY_BOOLEAN) self.assertEqual(cmd.get_property('bool_property_1', 'ala'), True) self.assertEqual(cmd.get_property('bool_property_2', 'ala'), True) # overwritting self.assertEqual(cmd.get_property('bool_property_3', 'ala'), True) self.assertEqual(cmd.get_property('bool_property_4', 'ala'), False)
def testSDF(self): # get molecule with SDF annotations cid = 6830 cmd.load('CID_%d.sdf' % cid, 'm1') self.assertEqual(None, cmd.get_property('PUBCHEM_COMPOUND_CID', 'm1'), 'property loaded, but should not') cmd.delete('*') cmd.load('CID_%d.sdf' % cid, 'm1', object_props='*') self.assertEqual(cid, cmd.get_property('PUBCHEM_COMPOUND_CID', 'm1')) v_pc = cmd.get_property('PUBCHEM_MMFF94_PARTIAL_CHARGES', 'm1') lines = v_pc.rstrip().splitlines() n_pc = int(lines[0]) self.assertEqual(n_pc, len(lines) - 1) # test loading selective properties cmd.set('load_object_props_default', "PUBCHEM_COMPOUND_CID PUBCHEM_CONFORMER_RMSD PUBCHEM_CONFORMER_DIVERSEORDER") cmd.load('CID_%d.sdf' % cid, 'm2') self.assertEqual(cid, cmd.get_property('PUBCHEM_COMPOUND_CID', 'm2')) self.assertEqual(None, cmd.get_property('PUBCHEM_EFFECTIVE_ROTOR_COUNT', 'm2'))
def test(self): cmd.fragment('gly') # atom properties cmd.alter('index 2-4', 'p.r_custom_indexhalve = index / 2.0') cmd.alter('index 3-5', 'p.neg_index = -index') cmd.alter('elem C', 'p["spaced resn name"] = resn + " " + name') # object properties objprops = [ ('first', 'Max'), ('last', 'Mustermann'), ('full name', 'Max Mustermann'), ('quoted "name"', 'Max "Mustermann"'), ] for (key, value) in objprops: cmd.set_property(key, value) # save/load round-trip with testing.mktemp('.mae') as filename: cmd.save(filename) cmd.delete('*') cmd.set('load_object_props_default', '*') cmd.set('load_atom_props_default', '*') cmd.load(filename, 'm1') # type conversions cmd.alter('m1', 'p.neg_index = int(p.s_pymol_neg_index or 0)') # atom properties self.assertEqual(2, cmd.count_atoms('p.neg_index < -3')) self.assertEqual(2, cmd.count_atoms('p.r_custom_indexhalve > 1.0')) # changed in 2.3.1: undefined (<>) -> None resnname = set() cmd.iterate('m1', 'resnname.add(p["s_pymol_spaced resn name"] or "")', space=locals()) self.assertEqual(resnname, set(['GLY C', 'GLY CA', ''])) # object properties for (key, value) in objprops: self.assertEqual(cmd.get_property('s_pymol_' + key, 'm1'), value)
def testColorTypeCmd(self): cmd.fragment('ala') cmd.do('set_property color_prop, red, ala, proptype=5', echo=0) prop = cmd.get_property('color_prop', 'ala') self.assertColorEqual(prop, 'red')
def testColorType(self, color1, color2): # colors are saved as integers, but can be accessed/set by either strings or hex from pymol.querying import get_color_index_from_string_or_list val1 = get_color_index_from_string_or_list(color1, _self=cmd) val2 = get_color_index_from_string_or_list(color2, _self=cmd) cmd.fragment('ala') cmd.set_property('color_property_1', color1, 'ala', proptype=PROPERTY_COLOR) cmd.set_property('color_property_2', color2, 'ala', proptype=PROPERTY_COLOR) self.assertColorEqual(cmd.get_property('color_property_1', 'ala'), val1) self.assertColorEqual(cmd.get_property('color_property_2', 'ala'), val2) self.assertColorEqual(cmd.get_property('color_property_1', 'ala'), color1) self.assertColorEqual(cmd.get_property('color_property_2', 'ala'), color2) cmd.set_property('color_property_1', str(val1), 'ala', proptype=PROPERTY_COLOR) cmd.set_property('color_property_2', str(val1), 'ala', proptype=PROPERTY_COLOR) cmd.set_property('color_property_3', str(val1), 'ala', proptype=PROPERTY_COLOR) cmd.set_property('color_property_4', str(val2), 'ala', proptype=PROPERTY_COLOR) self.assertColorEqual(cmd.get_property('color_property_1', 'ala'), color1) self.assertColorEqual(cmd.get_property('color_property_1', 'ala'), val1) self.assertColorEqual(cmd.get_property('color_property_2', 'ala'), color1) # overwritting self.assertColorEqual(cmd.get_property('color_property_2', 'ala'), val1) self.assertColorEqual(cmd.get_property('color_property_3', 'ala'), color1) self.assertColorEqual(cmd.get_property('color_property_3', 'ala'), val1) self.assertColorEqual(cmd.get_property('color_property_4', 'ala'), color2) self.assertColorEqual(cmd.get_property('color_property_4', 'ala'), val2)
def testMAEloadNoProperties(self): cmd.load(self.datafile('1d_smiles.mae'), '1d_smiles') objs = cmd.get_object_list() for obj in objs: props = cmd.get_property('', obj) self.assertIsNone(props)