def read_mol_xyz(mol_dict: Settings) -> Optional[Molecule]: """Read an .xyz file.""" try: mol = Molecule(mol_dict.mol, inputformat='xyz') if mol_dict.guess_bonds and not mol_dict.is_qd: mol.guess_bonds() if not mol_dict.is_core: canonicalize_mol(mol) return mol except Exception as ex: print_exception('read_mol_xyz', mol_dict.name, ex)
def set_qd(qd: Molecule, mol_dict: Settings) -> Molecule: """Update quantum dots imported by :func:`.read_mol`.""" # Create ligand (and anchor) molecules ligand = molkit.from_smiles(mol_dict.ligand_smiles) ligand_rdmol = molkit.to_rdmol(ligand) anchor = molkit.from_smiles(mol_dict.ligand_anchor) anchor_rdmol = molkit.to_rdmol(anchor) qd_rdmol = molkit.to_rdmol(qd) # Create arrays of atomic indices of the core and ligands lig_idx = 1 + np.array(qd_rdmol.GetSubstructMatches(ligand_rdmol)) core_idx = np.arange(1, len(qd))[~lig_idx] lig_idx = lig_idx.ravel().tolist() core_idx = core_idx.tolist() # Guess bonds if mol_dict.guess_bonds: qd.guess_bonds(atom_subset=[qd[i] for i in lig_idx]) # Reorder all atoms: core atoms first followed by ligands qd.atoms = [qd[i] for i in core_idx] + [qd[j] for i in lig_idx for j in i] # Construct a list with the indices of all ligand anchor atoms core_idx_max = 1 + len(core_idx) _anchor_idx = ligand_rdmol.GetSubstructMatch(anchor_rdmol)[0] start = core_idx_max + _anchor_idx stop = core_idx_max + _anchor_idx + np.product(lig_idx.shape) step = len(ligand) anchor_idx = list(range(start, stop, step)) # Update the properties of **qd** for i in anchor_idx: qd[i].properties.anchor = True qd.properties.indices = list(range(1, core_idx_max)) + anchor_idx qd.properties.job_path = [] qd.properties.name = mol_dict.name qd.properties.path = mol_dict.path qd.properties.ligand_smiles = Chem.CanonSmiles(mol_dict.ligand_smiles) qd.properties.ligand_anchor = f'{ligand[_anchor_idx].symbol}{_anchor_idx}' # Update the pdb_info of all atoms for i, at in enumerate(qd, 1): at.properties.pdb_info.SerialNumber = i if i <= core_idx_max: # A core atom at.properties.pdb_info.ResidueNumber = 1 else: # A ligand atom at.properties.pdb_info.ResidueNumber = 2 + int( (i - core_idx_max) / len(ligand))
import os from pathlib import Path import numpy as np try: import dill as pickle except ImportError: import pickle from scm.plams import Molecule, Atom, MoleculeError from scm.plams import read_all_molecules_in_xyz_file PATH = Path('.') / 'xyz' BENZENE = Molecule(PATH / 'benzene.xyz') BENZENE.guess_bonds() def test_index(): """Test :meth:`Molecule.index`.""" atom = BENZENE[1] bond = BENZENE[1, 2] atom_test = Atom(coords=[0, 0, 0], symbol='H') assert BENZENE.index(atom) == 1 assert BENZENE.index(bond) == (1, 2) try: BENZENE.index(None) # None is of invalid type except MoleculeError: pass
import warnings from pathlib import Path import numpy as np from scipy.sparse import (bsr_matrix, coo_matrix, csr_matrix, csc_matrix, dia_matrix, dok_matrix, lil_matrix) from scm.plams import Molecule, MoleculeError from assertionlib import assertion from FOX.ff.degree_of_separation import degree_of_separation, sparse_bond_matrix PATH = Path('tests', 'test_files') MOL = Molecule(PATH / 'Cd68Se55_26COO_MD_trajec.xyz') MOL.guess_bonds([at for at in MOL if at.symbol in {'C', 'H', 'O'}]) def test_degree_of_separation(): """Test :func:`degree_of_separation`.""" ref1 = np.load(PATH / 'degree_of_separation.npy') ref2 = ref1.copy() ref2[ref1 > 1] = np.inf mat1 = degree_of_separation(MOL) mat2 = degree_of_separation(MOL, bond_mat=MOL.bond_matrix()) mat3 = degree_of_separation(MOL, limit=1) np.testing.assert_array_equal(mat1, ref1) np.testing.assert_array_equal(mat2, ref1) np.testing.assert_array_equal(mat3, ref2)