def toMolecule(self, formalcharges=False, ids=None): """ Return the moleculekit.molecule.Molecule Parameters ---------- formalcharges: bool If True,the formal charges are used instead of partial ones ids: list The list of conformer ids to store in the moleculekit Molecule object- If None, all are returned Default: None Returns ------- mol: moleculekit.molecule.Molecule The moleculekit Molecule object """ from moleculekit.molecule import Molecule class NoConformerError(Exception): pass if ids is None: ids = np.arange(self.numFrames) if self.numFrames == 0: raise NoConformerError( "No Conformers are found in the molecule. Generate at least one conformer." ) elif not isinstance(ids, list) and not isinstance(ids, np.ndarray): raise ValueError( 'The argument ids should be a list of confomer ids') mol = Molecule() mol.empty(self.numAtoms) mol.record[:] = 'HETATM' mol.resname[:] = self.ligname[:3] mol.resid[:] = self._resid mol.coords = self._coords[:, :, ids] mol.name[:] = self._name mol.element[:] = self._element if formalcharges: mol.charge[:] = self._formalcharge else: mol.charge[:] = self._charge mol.box = np.zeros((3, self.numFrames), dtype=np.float32) mol.viewname = self.ligname mol.bonds = self._bonds mol.bondtype = self._bondtype mol.atomtype = self._atomtype return mol
def test_mcs_atom_matching(self): from moleculekit.home import home from moleculekit.molecule import Molecule import numpy as np path = home(dataDir="test-molecule-graphalignment") mol1 = Molecule(os.path.join(path, "OIC.cif")) mol1.atomtype = mol1.element idx = np.random.permutation(np.arange(mol1.numAtoms)) mol1.reorderAtoms(idx) mol2 = Molecule("5VBL") mol2.filter("resname OIC") atm1, atm2 = mcsAtomMatching(mol1, mol2, bondCompare="order") assert len(atm1) == 11 assert np.array_equal(mol1.name[atm1], mol2.name[atm2]) atm1, atm2 = mcsAtomMatching(mol1, mol2, bondCompare="any") assert len(atm1) == 11 # Compare elements here since it can match O to OXT when ignoring bond type assert np.array_equal(mol1.element[atm1], mol2.element[atm2])
def pdb2psf_CA(pdb_name_in, psf_name_out, bonds=True, angles=True): mol = Molecule(pdb_name_in) mol.filter("name CA") n = mol.numAtoms atom_types = [] for i in range(n): atom_types.append(CA_MAP[(mol.resname[i], mol.name[i])]) if bonds: bonds = np.concatenate( ( np.arange(n - 1).reshape([n - 1, 1]), (np.arange(1, n).reshape([n - 1, 1])), ), axis=1, ) else: bonds = np.empty([0, 2], dtype=np.int32) if angles: angles = np.concatenate( ( np.arange(n - 2).reshape([n - 2, 1]), (np.arange(1, n - 1).reshape([n - 2, 1])), (np.arange(2, n).reshape([n - 2, 1])), ), axis=1, ) else: angles = np.empty([0, 3], dtype=np.int32) mol.atomtype = np.array(atom_types) mol.bonds = bonds mol.angles = angles mol.write(psf_name_out)
def pdb2psf_CACB(pdb_name_in, psf_name_out, bonds=True, angles=True): mol = Molecule(pdb_name_in) mol.filter("name CA CB") n = mol.numAtoms atom_types = [] for i in range(n): atom_types.append(CACB_MAP[(mol.resname[i], mol.name[i])]) CA_idx = [] CB_idx = [] for i, name in enumerate(mol.name): if name[:2] == "CA": CA_idx.append(i) else: CB_idx.append(i) if bonds: CA_bonds = np.concatenate( ( np.array(CA_idx)[:-1].reshape([len(CA_idx) - 1, 1]), np.array(CA_idx)[1:].reshape([len(CA_idx) - 1, 1]), ), axis=1, ) CB_bonds = np.concatenate( ( np.array(CB_idx).reshape(len(CB_idx), 1) - 1, np.array(CB_idx).reshape(len(CB_idx), 1), ), axis=1, ) bonds = np.concatenate((CA_bonds, CB_bonds)) else: bonds = np.empty([0, 2], dtype=np.int32) if angles: CA_angles = np.concatenate( ( np.array(CA_idx)[:-2].reshape([len(CA_idx) - 2, 1]), np.array(CA_idx)[1:-1].reshape([len(CA_idx) - 2, 1]), np.array(CA_idx)[2:].reshape([len(CA_idx) - 2, 1]), ), axis=1, ) CB_angles = [] cbn = 0 for i in CA_idx: if mol.resname[i] != "GLY": if i != 0: CB_angles.append( [CA_idx[CA_idx.index(i) - 1], i, CB_idx[cbn]]) if i != CA_idx[-1]: CB_angles.append( [CA_idx[CA_idx.index(i) + 1], i, CB_idx[cbn]]) cbn += 1 CB_angles = np.array(CB_angles) angles = np.concatenate((CA_angles, CB_angles)) else: angles = np.empty([0, 3], dtype=np.int32) mol.atomtype = np.array(atom_types) mol.bonds = bonds mol.angles = angles mol.write(psf_name_out)