def setBonds(self, bonds): """Set covalent bonds between atoms. *bonds* must be a list or an array of pairs of indices. All bonds must be set at once. Bonding information can be used to make atom selections, e.g. ``"bonded to index 1"``. See :mod:`~.select` module documentation for details. Also, a data array with number of bonds will be generated and stored with label *numbonds*. This can be used in atom selections, e.g. ``'numbonds 0'`` can be used to select ions in a system.""" if isinstance(bonds, list): bonds = np.array(bonds, int) if bonds.ndim != 2: raise ValueError('bonds.ndim must be 2') if bonds.shape[1] != 2: raise ValueError('bonds.shape must be (n_bonds, 2)') if bonds.min() < 0: raise ValueError('negative atom indices are not valid') n_atoms = self._n_atoms if bonds.max() >= n_atoms: raise ValueError('atom indices are out of range') bonds.sort(1) bonds = bonds[bonds[:,1].argsort(),] bonds = bonds[bonds[:,0].argsort(),] self._bmap, self._data['numbonds'] = evalBonds(bonds, n_atoms) self._bonds = bonds self._data['fragindices'] = None self._fragments = None
def saveAtoms(atoms, filename=None, **kwargs): """Save *atoms* in ProDy internal format. All atomic classes are accepted as *atoms* argument. This function saves user set atomic data as well. Note that title of the AtomGroup instance is used as the filename when *atoms* is not an AtomGroup. To avoid overwriting an existing file with the same name, specify a *filename*.""" if not isinstance(atoms, Atomic): raise TypeError('atoms must be Atomic instance, not {0:s}' .format(type(atoms))) if isinstance(atoms, AtomGroup): ag = atoms title = ag.getTitle() SKIP = SAVE_SKIP_ATOMGROUP else: ag = atoms.getAtomGroup() title = str(atoms) SKIP = SAVE_SKIP_POINTER if filename is None: filename = ag.getTitle().replace(' ', '_') filename += '.ag.npz' attr_dict = {'title': title} attr_dict['n_atoms'] = atoms.numAtoms() attr_dict['n_csets'] = atoms.numCoordsets() attr_dict['cslabels'] = atoms.getCSLabels() coords = atoms._getCoordsets() if coords is not None: attr_dict['coordinates'] = coords bonds = ag._bonds bmap = ag._bmap if bonds is not None and bmap is not None: if isinstance(atoms, AtomGroup): attr_dict['bonds'] = bonds attr_dict['bmap'] = bmap attr_dict['numbonds'] = ag._data['numbonds'] frags = ag._data['fragindices'] if frags is not None: attr_dict['fragindices'] = frags else: bonds = trimBonds(bonds, atoms._getIndices()) attr_dict['bonds'] = bonds attr_dict['bmap'], attr_dict['numbonds'] = \ evalBonds(bonds, len(atoms)) for key, data in ag._data.iteritems(): if key in SKIP: continue if data is not None: attr_dict[key] = data ostream = openFile(filename, 'wb', **kwargs) savez(ostream, **attr_dict) ostream.close() return filename
def setBonds(self, bonds): """Set covalent bonds between atoms. *bonds* must be a list or an array of pairs of indices. All bonds must be set at once. An array with number of bonds will be generated and stored as *numbonds*. This can be used in atom selections, e.g. ``ag.select('numbonds 0')`` can be used to select ions in a system.""" if isinstance(bonds, list): bonds = np.array(bonds, int) if bonds.ndim != 2: raise ValueError('bonds.ndim must be 2') if bonds.shape[1] != 2: raise ValueError('bonds.shape must be (n_bonds, 2)') if bonds.min() < 0: raise ValueError('negative atom indices are not valid') n_atoms = self._n_atoms if bonds.max() >= n_atoms: raise ValueError('atom indices are out of range') bonds.sort(1) bonds = bonds[bonds[:,1].argsort(),] bonds = bonds[bonds[:,0].argsort(),] self._bmap, self._data['numbonds'] = evalBonds(bonds, n_atoms) self._bonds = bonds