def test_write_xyz(component, tmpdir_factory): wd = tmpdir_factory.mktemp('xyz_test') xyz_file = os.path.join(wd, f'{component.id}.xyz') ccd_writer.write_molecule(xyz_file, component) assert os.path.isfile(xyz_file) assert os.path.getsize(xyz_file) > 0 with open(xyz_file, 'r') as fp: lines = fp.read().splitlines() assert lines[1] == component.id assert int(lines[0]) == len(lines[2:])
def test_cif_write(component: Component, tmpdir, rem_hs): path = tmpdir.join(f"{component.id}.cif") to_check = must_have_categories.copy() ccd_writer.write_molecule(str(path), component, remove_hs=rem_hs) json_obj = reader.read(path) if component.id == "NA": # Na is an atom! to_check.pop(2) # remove "_chem_comp_bond" if component.id == "D3O" and rem_hs: # D3O has single heavy atom to_check.pop(2) assert json_obj assert component.id in json_obj for c in to_check: assert c in json_obj[component.id]
def test_json_write(component, tmpdir_factory): wd = tmpdir_factory.mktemp("json_test") for remove_h in True, False: suffix = "" if remove_h else "H" json_file = os.path.join(wd, f"{component.id}{suffix}.json") ccd_writer.write_molecule(json_file, component, remove_hs=remove_h) rdkit_mol = component.mol_no_h if remove_h else component.mol assert os.path.isfile(json_file) assert os.path.getsize(json_file) > 0 with open(json_file, "r") as fp: js = json.load(fp) key = list(js.keys())[0] assert key == component.id assert len(js[key]["atoms"]) == rdkit_mol.GetNumAtoms() assert len(js[key]["bonds"]) == rdkit_mol.GetNumBonds()
def test_write_sdf(component, tmpdir_factory): wd = tmpdir_factory.mktemp('sdf_test') for ideal in True, False: for remove_hs in True, False: suffix = f'{("" if remove_hs else "H")}' sdf_file = os.path.join(wd, f'{component.id}_{suffix}.sdf') conf_type = ConformerType.Ideal if ideal else ConformerType.Model ccd_writer.write_molecule(path=sdf_file, component=component, conf_type=conf_type, remove_hs=remove_hs) rdkit_mol = component.mol_no_h if remove_hs else component.mol assert os.path.isfile(sdf_file) assert os.path.getsize(sdf_file) > 0 mol = Chem.MolFromMolFile(sdf_file, sanitize=False) assert isinstance(mol, Chem.rdchem.Mol) assert mol.GetNumAtoms() == rdkit_mol.GetNumAtoms()
def __write_molecule(self, path, component, alt_names, conformer_type): """Write out deemed structure. Args: path str: Path where the molecule will be stored. component (Component): Component to be written. alt_names (bool): Whether or not molecule will be written with alternate names. conformer_type (Component): Conformer to be written. """ try: ccd_writer.write_molecule(path, component, remove_hs=False, alt_names=alt_names, conf_type=conformer_type) except Exception: self.logger.error(f'error writing {path}.') with open(path, 'w') as f: f.write('')