def testLoadCoords(self): import numpy cmd.fragment('gly', 'm1') coords = cmd.get_coords('m1') coords += 5.0 cmd.load_coords(coords, 'm1') self.assertTrue(numpy.allclose(coords, cmd.get_coords('m1')))
def apply_symmetry(R, t, inchain, outchain): cmd.copy('symm', f'inpdb') coords = cmd.get_coords('symm') cmd.remove(f'symm and not chain {inchain}') coords_symm = (R.dot(coords.T)).T + t cmd.load_coords(coords_symm, 'symm') myspace = {'outchain': outchain} cmd.alter('symm', 'chain=f"{outchain}"', space=myspace)
def write_pdb(obj, coords, outfilename, seq=None, resids=None, chains=None): cmd.load_coords(coords, obj) if seq is not None: myspace = {} myspace['seq_iter'] = iter(seq) cmd.alter(obj, 'resn=f"{seq_iter.__next__()}"', space=myspace) if resids is not None: myspace = {} myspace['resid_iter'] = iter(resids) cmd.alter(obj, 'resi=f"{resid_iter.__next__()}"', space=myspace) if chains is not None: myspace = {} myspace['chain_iter'] = iter(chains) cmd.alter(obj, 'chain=f"{chain_iter.__next__()}"', space=myspace) cmd.save(outfilename, selection=obj)
def create_object_with_flipkin_coords(mpobj, which_flips='NQ'): '''Duplicate the mpobj molecule and apply the flipNQ/H group coordinates. PARAMETERS mpobj An MPObject instance which_flips Either 'NQ' or 'H' to specify which flipkin to use ''' flipkin_name = 'flipkin{}'.format(which_flips) flipkin = mpobj.kin[flipkin_name] flip_group = 'flip{}'.format(which_flips) reduced_obj = mpobj.pdb['reduce'] flipped_obj = mpobj.pdb[flipkin_name] cmd.create(flipped_obj, reduced_obj) for vl in flipkin.vectorlists(): # Skip if not coordinates if vl[0].vectorlist_name == 'x': msg = 'skipping non-coords vectorlist {}' logger.debug(msg.format(vl[0].vectorlist_name)) continue # Skip everything except the'flipNQ' (or 'flipH') group if vl[0].group[0] != flip_group: msg = 'skipping non-{} vectorlist'.format(flip_group) logger.debug(msg) continue logger.debug('begin flipping vectorlist') for v in vl: if not (v.atom[0] and v.atom[1]): msg = 'vector {} was missing atoms: {}'.format(v, v.atom) logger.warning(msg) continue macro = v.macro(1) sel = v.sel(1) logger.debug('atom to be flipped: {}\n sel: {}'.format(macro, sel)) source_coords = v.coords[1] target_sel = '{} and {}'.format(flipped_obj, sel) msg = 'flipping atom {} to {}'.format(target_sel, source_coords) logger.debug(msg) ret = cmd.load_coords([source_coords], target_sel) if ret == -1: success = 0 a = v.atom[1] if a['resn'] == 'HIS': his_h = { 'HD1': { 'old_h': 'HE2', 'old_n': 'NE2', 'new_h': 'HD1', 'new_n': 'ND1', }, 'HE2': { 'old_h': 'HD1', 'old_n': 'ND1', 'new_h': 'HE2', 'new_n': 'NE2', } } if a['name'] in his_h.keys(): # Reduce has switched which N is protonated. Let's move # the old H atom and rename it. resi_sel = '{} and chain {} and resi {}'.format( flipped_obj, a['chain'], a['resi']) h = his_h[a['name']] old_h = '{} and name {}'.format(resi_sel, h['old_h']) old_n = '{} and name {}'.format(resi_sel, h['old_n']) new_h = '{} and name {}'.format(resi_sel, h['new_h']) new_n = '{} and name {}'.format(resi_sel, h['new_n']) # Break the old bond cmd.unbond(old_h, old_n) # Rename the atom cmd.alter(old_h, 'name="{}"'.format(a['name'])) # Make the new bond cmd.bond(new_h, new_n) # Retry loading coordinates ret = cmd.load_coords([source_coords], target_sel) if not ret == -1: success = 1 if not success: msg = 'failed to load coords for {}!'.format(macro) logger.warning(msg) logger.debug('end flipping vectorlist')
if __name__ == '__main__': import argparse from pymol import cmd # argparse.ArgumentParser(prog=None, usage=None, description=None, epilog=None, parents=[], formatter_class=argparse.HelpFormatter, prefix_chars='-', fromfile_prefix_chars=None, argument_default=None, conflict_handler='error', add_help=True, allow_abbrev=True, exit_on_error=True) parser = argparse.ArgumentParser(description='') # parser.add_argument(name or flags...[, action][, nargs][, const][, default][, type][, choices][, required][, help][, metavar][, dest]) parser.add_argument('--pdb1') parser.add_argument( '--pdb2', help='PDB filename for the reference structure to fit on') parser.add_argument('--sel1', help='Selection for pdb1 (default: all)', default='all') parser.add_argument('--sel2', help='Selection for pdb2 (equal to sel1 if not given)') args = parser.parse_args() cmd.load(args.pdb1, 'pdb1') cmd.load(args.pdb2, 'pdb2') if args.sel2 is None: args.sel2 = args.sel1 coords1 = cmd.get_coords(f'pdb1 and {args.sel1}') coords2 = cmd.get_coords(f'pdb2 and {args.sel2}') R, t = rigid_body_fit(coords1, coords2) print(R) toalign = cmd.get_coords('pdb1') coords_aligned = (R.dot(toalign.T)).T + t cmd.load_coords(coords_aligned, f'pdb1') cmd.save('aligned.pdb', 'pdb1')
coords = cmd.get_coords('inpdb') center = coords.mean(axis=0) coords -= center if not args.random: angles = np.linspace(0, 2 * np.pi, args.nframes // 3) axes = [np.array([1, 0, 0]), np.array([0, 1, 0]), np.array([0, 0, 1])] else: angles = np.random.uniform(low=0, high=2 * np.pi, size=int(np.sqrt(args.nframes))) axes = [ np.random.choice([0, 1], size=3) for _ in range(int(np.sqrt(args.nframes))) ] i = 0 for vec in axes: for alpha in angles: i += 1 r = R.from_rotvec(alpha * vec).as_matrix() coords_rot = (r.dot(coords.T)).T + center if args.translate: coords_rot += np.random.uniform(low=-5, high=5) cmd.create('out', 'inpdb', 1, i) cmd.load_coords(coords_rot, 'out', state=i) outname = f'{os.path.splitext(args.pdb)[0]}' if args.nframes > 1: cmd.save_traj(f'{outname}.dcd', 'out') else: cmd.save(f'{outname}_roll.pdb', 'out')
def create_object_with_flipkin_coords(mpobj, which_flips='NQ'): '''Duplicate the mpobj molecule and apply the flipNQ/H group coordinates. PARAMETERS mpobj An MPObject instance which_flips Either 'NQ' or 'H' to specify which flipkin to use ''' flipkin_name = 'flipkin{}'.format(which_flips) flipkin = mpobj.kin[flipkin_name] flip_group = 'flip{}'.format(which_flips) reduced_obj = mpobj.pdb['reduce'] flipped_obj = mpobj.pdb[flipkin_name] cmd.create(flipped_obj, reduced_obj) for vl in flipkin.vectorlists(): # Skip if not coordinates if vl[0].vectorlist_name == 'x': msg = 'skipping non-coords vectorlist {}' logger.debug(msg.format(vl[0].vectorlist_name)) continue # Skip everything except the'flipNQ' (or 'flipH') group if vl[0].group[0] != flip_group: msg = 'skipping non-{} vectorlist'.format(flip_group) logger.debug(msg) continue logger.debug('begin flipping vectorlist') for v in vl: if not (v.atom[0] and v.atom[1]): msg = 'vector {} was missing atoms: {}'.format(v, v.atom) logger.warning(msg) continue macro = v.macro(1) sel = v.sel(1) logger.debug('atom to be flipped: {}\n sel: {}'.format( macro, sel)) source_coords = v.coords[1] target_sel = '{} and {}'.format(flipped_obj, sel) msg = 'flipping atom {} to {}'.format(target_sel, source_coords) logger.debug(msg) ret = cmd.load_coords([source_coords], target_sel) if ret == -1: success = 0 a = v.atom[1] if a['resn'] == 'HIS': his_h = { 'HD1': { 'old_h': 'HE2', 'old_n': 'NE2', 'new_h': 'HD1', 'new_n': 'ND1', }, 'HE2': { 'old_h': 'HD1', 'old_n': 'ND1', 'new_h': 'HE2', 'new_n': 'NE2', } } if a['name'] in his_h.keys(): # Reduce has switched which N is protonated. Let's move # the old H atom and rename it. resi_sel = '{} and chain {} and resi {}'.format( flipped_obj, a['chain'], a['resi']) h = his_h[a['name']] old_h = '{} and name {}'.format(resi_sel, h['old_h']) old_n = '{} and name {}'.format(resi_sel, h['old_n']) new_h = '{} and name {}'.format(resi_sel, h['new_h']) new_n = '{} and name {}'.format(resi_sel, h['new_n']) # Break the old bond cmd.unbond(old_h, old_n) # Rename the atom cmd.alter(old_h, 'name="{}"'.format(a['name'])) # Make the new bond cmd.bond(new_h, new_n) # Retry loading coordinates ret = cmd.load_coords([source_coords], target_sel) if not ret == -1: success = 1 if not success: msg = 'failed to load coords for {}!'.format(macro) logger.warning(msg) logger.debug('end flipping vectorlist')
def testLoadCoords(self): cmd.fragment("gly", "m1") coords = cmd.get_model("m1").get_coord_list() # buggy state argument cmd.load_coords(coords, "m1", state=2 + 1) self.assertEqual(2, cmd.count_states("m1"))