def test_broken_xyz_file(): with pytest.raises(XYZfileDidNotExist): _ = xyz_file_to_atoms(filename='XXXXXX') with pytest.raises(NotXYZfile): path = os.path.join(here, 'data', 'wrong_extension_xyz_file.mol') _ = xyz_file_to_atoms(filename=path) with pytest.raises(XYZfileMalformatted): path = os.path.join(here, 'data', 'wrong_format_xyz_file.xyz') _ = xyz_file_to_atoms(filename=path) with pytest.raises(XYZfileMalformatted): path = os.path.join(here, 'data', 'no_data_xyz_file.xyz') _ = xyz_file_to_atoms(filename=path)
def test_okay_xyz_file(): atoms = xyz_file_to_atoms(os.path.join(here, 'data', 'hydrocarbon.xyz')) assert len(atoms) == 20 for atom in atoms: assert isinstance(atom, Atom) # String representation should be concise assert len(str(atoms[0])) < 50
def test_example(): benzene_file_path = os.path.join(here, 'data', 'benzene.xyz') benzene = CoreMolecule(xyz_filename=benzene_file_path, atoms_to_del=[7]) toluene = CombinedMolecule(core_mol=benzene, frag_smiles='C[*]', name='toluene') toluene.print_xyz_file() assert os.path.exists('toluene.xyz') gen_toluene_atoms = xyz_file_to_atoms('toluene.xyz') os.remove('toluene.xyz') # Toluene has 15 atoms assert len(gen_toluene_atoms) == 15
def test_cli(): join = Popen(['molfunc', xyz_path, '-a', '7', '-s', 'C[*]']) join.wait() toluene_path = os.path.join(here, 'data', 'benzene_mod.xyz') assert os.path.exists(toluene_path) toluene_atoms = xyz_file_to_atoms(filename=toluene_path) assert len(toluene_atoms) == 15 toluene = Molecule(atoms=toluene_atoms) # Geometry should be sensible.. i.e. min pairwise distance > 0.8 Å # and the maximum < 5 Å toluene_coords = toluene.get_coordinates() dist_mat = distance_matrix(toluene_coords, toluene_coords) assert 0.8 < np.min(dist_mat+np.identity(15)) < 5.0 os.remove(toluene_path)
def __init__(self, name='mol', xyz_filename=None, smiles=None, atoms=None): """ Base molecule class. Initialised in order of priority: SMILES string, xyz file, atoms ------------------------ Keyword Arguments ---------------------------- :param name: (str) :param xyz_filename: (str) .xyz filename (or filepath) from which atoms will be extracted :param smiles: (str) SMILES string defining the molecule from which a 3D structure as atoms are extracted using RDKit :param atoms: (list(molfunc.atom.Atom)) List of atoms used to initialise the molecule """ self.name = str(name) self.n_atoms = 0 self.graph = None self.atoms = None if smiles is not None: # Use RDKit to convert SMILES -> atoms self.set_atoms(atoms=smiles_to_atoms(smiles)) if xyz_filename is not None: # Initialisation with an xyz file takes precedence over SMILES self.set_atoms(atoms=xyz_file_to_atoms(xyz_filename)) if atoms is not None: self.set_atoms(atoms) if self.n_atoms != 0: # If there are atoms in the molecule set the graph and valancies self.make_graph() self.set_atomic_valancies()