def loop_orientation(selection, state=STATE, visualize=1, quiet=1, *, _self=cmd): ''' DESCRIPTION Get the center and approximate direction of a peptide. Works for any secondary structure. Averages direction of N(i)->C(i) pseudo bonds. USAGE loop_orientation selection [, visualize ] SEE ALSO helix_orientation ''' state, visualize, quiet = int(state), int(visualize), int(quiet) coords = dict() _self.iterate_state(state, '(%s) and name N+C' % (selection), 'coords.setdefault(chain + resi, {})[name] = x,y,z', space=locals()) vec = cpv.get_null() center = cpv.get_null() count = 0 for x in coords.values(): if 'C' in x and 'N' in x: vec = cpv.add(vec, cpv.sub(x['C'], x['N'])) for coord in x.values(): center = cpv.add(center, coord) count += 1 if count == 0: raise CmdException('count == 0') vec = cpv.normalize(vec) center = cpv.scale(center, 1. / count) _common_orientation(selection, center, vec, visualize, 2.0 * len(coords), quiet, _self=_self) return center, vec
def _vec_sum(vec_list): # this is the same as # return numpy.array(vec_list).sum(0).tolist() vec = cpv.get_null() for x in vec_list: vec = cpv.add(vec, x) return vec
def loop_orientation(selection, visualize=1, quiet=0): ''' DESCRIPTION Get the center and approximate direction of a peptide. Works for any secondary structure. Averages direction of N(i)->C(i) pseudo bonds. USAGE loop_orientation selection [, visualize] SEE ALSO helix_orientation ''' visualize, quiet = int(visualize), int(quiet) stored.x = dict() cmd.iterate_state(STATE, '(%s) and name N+C' % (selection), 'stored.x.setdefault(chain + resi, dict())[name] = x,y,z') vec = cpv.get_null() count = 0 for x in stored.x.itervalues(): if 'C' in x and 'N' in x: vec = cpv.add(vec, cpv.sub(x['C'], x['N'])) count += 1 if count == 0: print 'warning: count == 0' raise CmdException vec = cpv.normalize(vec) return _common_orientation(selection, vec, visualize, quiet)
def loop_orientation(selection, visualize=1, quiet=0): ''' DESCRIPTION Get the center and approximate direction of a peptide. Works for any secondary structure. Averages direction of N(i)->C(i) pseudo bonds. USAGE loop_orientation selection [, visualize] SEE ALSO helix_orientation ''' visualize, quiet = int(visualize), int(quiet) stored.x = dict() cmd.iterate_state( STATE, '(%s) and name N+C' % (selection), 'stored.x.setdefault(chain + resi, dict())[name] = x,y,z') vec = cpv.get_null() count = 0 for x in stored.x.itervalues(): if 'C' in x and 'N' in x: vec = cpv.add(vec, cpv.sub(x['C'], x['N'])) count += 1 if count == 0: print 'warning: count == 0' raise CmdException vec = cpv.normalize(vec) return _common_orientation(selection, vec, visualize, quiet)
def loop_orientation(selection, state=STATE, visualize=1, quiet=1): ''' DESCRIPTION Get the center and approximate direction of a peptide. Works for any secondary structure. Averages direction of N(i)->C(i) pseudo bonds. USAGE loop_orientation selection [, visualize ] SEE ALSO helix_orientation ''' state, visualize, quiet = int(state), int(visualize), int(quiet) coords = dict() cmd.iterate_state(state, '(%s) and name N+C' % (selection), 'coords.setdefault(chain + resi, {})[name] = x,y,z', space=locals()) vec = cpv.get_null() center = cpv.get_null() count = 0 for x in coords.itervalues(): if 'C' in x and 'N' in x: vec = cpv.add(vec, cpv.sub(x['C'], x['N'])) for coord in x.itervalues(): center = cpv.add(center, coord) count += 1 if count == 0: print 'warning: count == 0' raise CmdException vec = cpv.normalize(vec) center = cpv.scale(center, 1./count) _common_orientation(selection, center, vec, visualize, 2.0*len(coords), quiet) return center, vec
def centerofmass(selection='(all)', state=CURRENT_STATE, quiet=1, *, _self=cmd): ''' DESCRIPTION Calculates the center of mass. Considers atom mass and occupancy. ARGUMENTS selection = string: atom selection {default: all} state = integer: object state, -1 for current state, 0 for all states {default: -1} NOTES If occupancy is 0.0 for an atom, set it to 1.0 for the calculation (assume it was loaded from a file without occupancy information). SEE ALSO get_extent ''' from chempy import cpv state, quiet = int(state), int(quiet) if state < 0: state = _self.get_selection_state(selection) if state == 0: states = list(range(1, _self.count_states(selection) + 1)) else: states = [state] com = cpv.get_null() totmass = 0.0 for state in states: model = _self.get_model(selection, state) for a in model.atom: m = a.get_mass() * (a.q or 1.0) com = cpv.add(com, cpv.scale(a.coord, m)) totmass += m if not totmass: raise pymol.CmdException('mass is zero') com = cpv.scale(com, 1. / totmass) if not quiet: print(' Center of Mass: [%8.3f,%8.3f,%8.3f]' % tuple(com)) return com
def centerofmass(selection='(all)', state=-1, quiet=1, _self=cmd): ''' DESCRIPTION Calculates the center of mass. Considers atom mass and occupancy. ARGUMENTS selection = string: atom selection {default: all} state = integer: object state, -1 for current state, 0 for all states {default: -1} NOTES If occupancy is 0.0 for an atom, set it to 1.0 for the calculation (assume it was loaded from a file without occupancy information). SEE ALSO get_extent ''' from chempy import cpv state, quiet = int(state), int(quiet) if state < 0: states = [get_selection_state(selection)] elif state == 0: states = range(1, _self.count_states(selection)+1) else: states = [state] com = cpv.get_null() totmass = 0.0 for state in states: model = _self.get_model(selection, state) for a in model.atom: m = a.get_mass() * (a.q or 1.0) com = cpv.add(com, cpv.scale(a.coord, m)) totmass += m if not totmass: raise pymol.CmdException('mass is zero') com = cpv.scale(com, 1./totmass) if not quiet: print ' Center of Mass: [%8.3f,%8.3f,%8.3f]' % tuple(com) return com
def centerofmass(selection='(all)', state=-1, quiet=1, *, _self=cmd): ''' DESCRIPTION Calculates the center of mass. Considers atom mass and occupancy. ARGUMENTS selection = string: atom selection {default: all} state = integer: object state, -1 for current state, 0 for all states {default: -1} EXAMPLE from psico.querying import * x = centerofmass('chain A') r = gyradius('chain A') cmd.pseudoatom('com', pos=x, vdw=r) SEE ALSO gyradius ''' from chempy import cpv state, quiet = int(state), int(quiet) if state < 0: states = [_self.get_state()] elif state == 0: states = list(range(1, _self.count_states(selection) + 1)) else: states = [state] com = cpv.get_null() totmass = 0.0 for state in states: model = _self.get_model(selection, state) for a in model.atom: if a.q == 0.0: continue m = a.get_mass() * a.q com = cpv.add(com, cpv.scale(a.coord, m)) totmass += m com = cpv.scale(com, 1. / totmass) if not quiet: print(' Center of Mass: [%8.3f,%8.3f,%8.3f]' % tuple(com)) return com
def centerofmass(selection='(all)', state=-1, quiet=1): ''' DESCRIPTION Calculates the center of mass. Considers atom mass and occupancy. ARGUMENTS selection = string: atom selection {default: all} state = integer: object state, -1 for current state, 0 for all states {default: -1} EXAMPLE from psico.querying import * x = centerofmass('chain A') r = gyradius('chain A') cmd.pseudoatom('com', pos=x, vdw=r) SEE ALSO gyradius ''' from chempy import cpv state, quiet = int(state), int(quiet) if state < 0: states = [cmd.get_state()] elif state == 0: states = list(range(1, cmd.count_states(selection)+1)) else: states = [state] com = cpv.get_null() totmass = 0.0 for state in states: model = cmd.get_model(selection, state) for a in model.atom: if a.q == 0.0: continue m = a.get_mass() * a.q com = cpv.add(com, cpv.scale(a.coord, m)) totmass += m com = cpv.scale(com, 1./totmass) if not quiet: print(' Center of Mass: [%8.3f,%8.3f,%8.3f]' % tuple(com)) return com
def centroid(selection='all', center=0, quiet=1): model = cmd.get_model(selection) nAtom = len(model.atom) centroid = cpv.get_null() for a in model.atom: centroid = cpv.add(centroid, a.coord) centroid = cpv.scale(centroid, 1. / nAtom) if not int(quiet): print ' centroid: [%8.3f,%8.3f,%8.3f]' % tuple(centroid) if int(move): cmd.alter_state(1, selection, "(x,y,z)=sub((x,y,z), centroid)", space={'centroid': centroid, 'sub': cpv.sub}) return centroid
def COM(selection='all', center=0, quiet=1): model = cmd.get_model(selection) nAtom = len(model.atom) COM = cpv.get_null() for a in model.atom: COM = cpv.add(COM, a.coord) COM = cpv.scale(COM, 1./nAtom) if not int(quiet): print ' COM: [%8.3f,%8.3f,%8.3f]' % tuple(COM) if int(center): cmd.alter_state(1, selection, "(x,y,z)=sub((x,y,z), COM)", space={'COM': COM, 'sub': cpv.sub}) return COM
def COM(selection='all', center=0, quiet=1): model = cmd.get_model(selection) nAtom = len(model.atom) COM = cpv.get_null() for a in model.atom: COM = cpv.add(COM, a.coord) COM = cpv.scale(COM, 1. / nAtom) if not int(quiet): print ' COM: [%8.3f,%8.3f,%8.3f]' % tuple(COM) if int(center): cmd.alter_state(1, selection, "(x,y,z)=sub((x,y,z), COM)", space={'COM': COM, 'sub': cpv.sub}) return COM
def find_center_of_mass(selection='(all)', state=-1): """ Find center of mass of the selection and return the value USAGE find_center_of_mass selection find_center_of_mass selection, state=state ARGUMENTS selection a selection-expression state a state index if positive int, 0 to all, or -1 to current """ state = utils.int_to_state(state) model = cmd.get_model(selection, state=state) com = cpv.get_null() # iterate all atoms and add vectors of center of mass of each atoms for atom in model.atom: com = cpv.add(com, atom.coord) com = cpv.scale(com, 1.0 / len(model.atom)) return com
def centroid(selection='all', center=0, quiet=1): model = cmd.get_model(selection) nAtom = len(model.atom) centroid = cpv.get_null() for a in model.atom: centroid = cpv.add(centroid, a.coord) centroid = cpv.scale(centroid, 1. / nAtom) if not int(quiet): print(' centroid: [%8.3f,%8.3f,%8.3f]' % tuple(centroid)) if int(center): cmd.alter_state(1, selection, "(x,y,z)=sub((x,y,z), centroid)", space={ 'centroid': centroid, 'sub': cpv.sub }) return centroid
def sidechaincenters(object='scc', selection='all', method='bahar1996', name='PS1', *, _self=cmd): ''' DESCRIPTION Creates an object with sidechain representing pseudoatoms for each residue in selection. Two methods are available: (1) Sidechain interaction centers as defined by Bahar and Jernigan 1996 http://www.ncbi.nlm.nih.gov/pubmed/9080182 (2) Sidechain centroids, the pseudoatom is the centroid of all atoms except hydrogens and backbone atoms (N, C and O). NOTE With method "bahar1996", if a residue has all relevant sidechain center atoms missing (for example a MET without SD), it will be missing in the created pseudoatom object. With method "centroid", if you want to exclude C-alpha atoms from sidechains, modify the selection like in this example: sidechaincenters newobject, all and (not name CA or resn GLY), method=2 USAGE sidechaincenters object [, selection [, method ]] ARGUMENTS object = string: name of object to create selection = string: atoms to consider {default: (all)} method = string: bahar1996 or centroid {default: bahar1996} name = string: atom name of pseudoatoms {default: PS1} SEE ALSO pseudoatom ''' from chempy import Atom, cpv, models atmap = dict() if method in ['bahar1996', '1', 1]: modelAll = _self.get_model('(%s) and resn %s' % (selection, '+'.join(sidechaincenteratoms))) for at in modelAll.atom: if at.name in sidechaincenteratoms[at.resn]: atmap.setdefault((at.segi, at.chain, at.resn, at.resi), []).append(at) elif method in ['centroid', '2', 2]: modelAll = _self.get_model('(%s) and polymer and not (hydro or name C+N+O)' % selection) for at in modelAll.atom: atmap.setdefault((at.segi, at.chain, at.resn, at.resi), []).append(at) else: raise CmdException('unknown method: {}'.format(method)) model = models.Indexed() for centeratoms in atmap.values(): center = cpv.get_null() for at in centeratoms: center = cpv.add(center, at.coord) center = cpv.scale(center, 1./len(centeratoms)) atom = Atom() atom.coord = center atom.index = model.nAtom + 1 atom.name = name for key in [ 'segi', 'chain', 'resi_number', 'resi', 'resn', 'hetatm', 'ss', 'b', ]: setattr(atom, key, getattr(at, key)) model.add_atom(atom) model.update_index() if object in _self.get_object_list(): _self.delete(object) _self.load_model(model, object) return model
def sidechaincenters(object='scc', selection='all', method='bahar1996', name='PS1'): ''' DESCRIPTION Creates an object with sidechain representing pseudoatoms for each residue in selection. Two methods are available: (1) Sidechain interaction centers as defined by Bahar and Jernigan 1996 http://www.ncbi.nlm.nih.gov/pubmed/9080182 (2) Sidechain centroids, the pseudoatom is the centroid of all atoms except hydrogens and backbone atoms (N, C and O). NOTE With method "bahar1996", if a residue has all relevant sidechain center atoms missing (for example a MET without SD), it will be missing in the created pseudoatom object. With method "centroid", if you want to exclude C-alpha atoms from sidechains, modify the selection like in this example: sidechaincenters newobject, all and (not name CA or resn GLY), method=2 USAGE sidechaincenters object [, selection [, method ]] ARGUMENTS object = string: name of object to create selection = string: atoms to consider {default: (all)} method = string: bahar1996 or centroid {default: bahar1996} name = string: atom name of pseudoatoms {default: PS1} SEE ALSO pseudoatom ''' from chempy import Atom, cpv, models atmap = dict() if method in ['bahar1996', '1', 1]: modelAll = cmd.get_model('(%s) and resn %s' % (selection, '+'.join(sidechaincenteratoms))) for at in modelAll.atom: if at.name in sidechaincenteratoms[at.resn]: atmap.setdefault((at.segi, at.chain, at.resn, at.resi), []).append(at) elif method in ['centroid', '2', 2]: modelAll = cmd.get_model('(%s) and polymer and not (hydro or name C+N+O)' % selection) for at in modelAll.atom: atmap.setdefault((at.segi, at.chain, at.resn, at.resi), []).append(at) else: print('Error: unknown method:', method) raise CmdException model = models.Indexed() for centeratoms in atmap.values(): center = cpv.get_null() for at in centeratoms: center = cpv.add(center, at.coord) center = cpv.scale(center, 1./len(centeratoms)) atom = Atom() atom.coord = center atom.index = model.nAtom + 1 atom.name = name for key in ['resn','chain','resi','resi_number','hetatm','ss','segi']: atom.__dict__[key] = at.__dict__[key] model.add_atom(atom) model.update_index() if object in cmd.get_object_list(): cmd.delete(object) cmd.load_model(model, object) return model