Пример #1
0
def to_pdb_ccd_cif_file(path, component: Component, remove_hs=True):
    """Converts structure to the PDB CIF format. Both model and ideal
    coordinates are stored. In case ideal coordinates are missing, rdkit
    attempts to generate 3D coordinates of the conformer.

    Args:
        path (str): Path to save cif file.
        component (Component): Component to be exported.
        remove_hs (bool, optional): Defaults to True.
    """
    if not isinstance(component.ccd_cif_dict, dict):
        component.ccd_cif_dict = _to_pdb_ccd_cif_dict(component)

    cif_copy = copy.deepcopy(component.ccd_cif_dict)

    _add_sw_info_cif(cif_copy)
    _add_2d_depiction_cif(component, cif_copy)
    _add_fragments_and_scaffolds_cif(component, cif_copy)
    _add_rdkit_properties_cif(component, cif_copy)
    _add_unichem_mapping_cif(component, cif_copy)
    _add_rdkit_conformer_cif(component, cif_copy, remove_hs)

    if remove_hs:
        h_indices: List[int] = [
            i for i, x in enumerate(cif_copy['_chem_comp_atom']['type_symbol'])
            if x == "H"
        ]
        h_names: List[str] = [
            cif_copy['_chem_comp_atom']['atom_id'][i] for i in h_indices
        ]

        hb_indices = []
        for key in ('atom_id_1', 'atom_id_2'):
            indices = [
                i for i, k in enumerate(cif_copy['_chem_comp_bond'][key])
                if k in h_names
            ]
            hb_indices += indices

        hb_indices = list(set(hb_indices))

        # scrap hydrogen atoms
        for key in cif_copy['_chem_comp_atom']:
            cif_copy['_chem_comp_atom'][key] = ([
                k for i, k in enumerate(cif_copy['_chem_comp_atom'][key])
                if i not in h_indices
            ])

        # scrap bonds to hydrogen atoms
        for key in cif_copy['_chem_comp_bond']:
            cif_copy['_chem_comp_bond'][key] = ([
                k for i, k in enumerate(cif_copy['_chem_comp_bond'][key])
                if i not in hb_indices
            ])

    cfd = mmcif.CifFileWriter(path)
    cfd.write({component.id: cif_copy})
Пример #2
0
    def test_plain_cif_write(component: Component, tmpdir, rem_hs):
        path = tmpdir.join(f"{component.id}.cif")
        to_check = must_have_categories.copy()

        component.ccd_cif_dict = None
        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]