def set_atom_implicit_hydrogen_valences(gra, atm_imp_hyd_vlc_dct): """ set atom implicit hydrogen valences """ atm_dct = mdict.set_by_key_by_position(atoms(gra), atm_imp_hyd_vlc_dct, ATM_IMP_HYD_VLC_POS) bnd_dct = bonds(gra) return _create.from_atoms_and_bonds(atm_dct, bnd_dct)
def add_atoms(gra, symb_dct, imp_hyd_vlc_dct=None, ste_par_dct=None): """ add atoms to this molecular graph, setting their keys """ atm_keys = atom_keys(gra) atm_symb_dct = atom_symbols(gra) atm_imp_hyd_vlc_dct = atom_implicit_hydrogen_valences(gra) atm_ste_par_dct = atom_stereo_parities(gra) keys = set(symb_dct.keys()) imp_hyd_vlc_dct = {} if imp_hyd_vlc_dct is None else imp_hyd_vlc_dct ste_par_dct = {} if ste_par_dct is None else ste_par_dct assert not keys & atm_keys assert set(imp_hyd_vlc_dct.keys()) <= keys assert set(ste_par_dct.keys()) <= keys atm_symb_dct.update(symb_dct) atm_imp_hyd_vlc_dct.update(imp_hyd_vlc_dct) atm_ste_par_dct.update(ste_par_dct) atm_dct = _create.atoms_from_data( atom_symbols=atm_symb_dct, atom_implicit_hydrogen_valences=atm_imp_hyd_vlc_dct, atom_stereo_parities=atm_ste_par_dct) bnd_dct = bonds(gra) gra = _create.from_atoms_and_bonds(atoms=atm_dct, bonds=bnd_dct) return gra
def add_bonds(xgr, keys, ord_dct=None, ste_par_dct=None): """ add bonds to this molecular graph """ bnd_keys = set(bond_keys(xgr)) bnd_ord_dct = bond_orders(xgr) bnd_ste_par_dct = bond_stereo_parities(xgr) keys = set(map(frozenset, keys)) ord_dct = {} if ord_dct is None else ord_dct ste_par_dct = {} if ste_par_dct is None else ste_par_dct assert not keys & bnd_keys assert set(ord_dct.keys()) <= keys assert set(ste_par_dct.keys()) <= keys bnd_keys.update(keys) bnd_ord_dct.update(ord_dct) bnd_ste_par_dct.update(ste_par_dct) atm_dct = atoms(xgr) bnd_dct = _create.bonds_from_data(bond_keys=bnd_keys, bond_orders=bnd_ord_dct, bond_stereo_parities=bnd_ste_par_dct) xgr = _create.from_atoms_and_bonds(atoms=atm_dct, bonds=bnd_dct) return xgr
def add_bonds(gra, keys, ord_dct=None, ste_par_dct=None, check=True): """ add bonds to this molecular graph """ bnd_keys = set(bond_keys(gra)) bnd_ord_dct = bond_orders(gra) bnd_ste_par_dct = bond_stereo_parities(gra) keys = set(map(frozenset, keys)) ord_dct = {} if ord_dct is None else ord_dct ste_par_dct = {} if ste_par_dct is None else ste_par_dct ord_dct = dict_.transform_keys(ord_dct, frozenset) ste_par_dct = dict_.transform_keys(ste_par_dct, frozenset) if check: assert not keys & bnd_keys, ( '{} and {} have a non-empty intersection'.format(keys, bnd_keys)) assert set(ord_dct.keys()) <= keys assert set(ste_par_dct.keys()) <= keys bnd_keys.update(keys) bnd_ord_dct.update(ord_dct) bnd_ste_par_dct.update(ste_par_dct) atm_dct = atoms(gra) bnd_dct = _create.bonds_from_data(bond_keys=bnd_keys, bond_orders=bnd_ord_dct, bond_stereo_parities=bnd_ste_par_dct) gra = _create.from_atoms_and_bonds(atoms=atm_dct, bonds=bnd_dct) return gra
def subgraph(xgr, atm_keys): """ the subgraph induced by a subset of the atoms """ atm_keys = set(atm_keys) assert atm_keys <= atom_keys(xgr) bnd_keys = set(filter(lambda x: x <= atm_keys, bond_keys(xgr))) atm_dct = dict_.by_key(atoms(xgr), atm_keys) bnd_dct = dict_.by_key(bonds(xgr), bnd_keys) return _create.from_atoms_and_bonds(atm_dct, bnd_dct)
def bond_induced_subgraph(gra, bnd_keys): """ the subgraph induced by a subset of the bonds """ atm_keys = set(itertools.chain(*bnd_keys)) bnd_keys = set(bnd_keys) assert atm_keys <= atom_keys(gra) atm_dct = dict_.by_key(atoms(gra), atm_keys) bnd_dct = dict_.by_key(bonds(gra), bnd_keys) return _create.from_atoms_and_bonds(atm_dct, bnd_dct)
def remove_bonds(xgr, bnd_keys): """ remove bonds from the molecular graph """ all_bnd_keys = bond_keys(xgr) bnd_keys = set(bnd_keys) assert bnd_keys <= all_bnd_keys bnd_keys = all_bnd_keys - bnd_keys atm_dct = atoms(xgr) bnd_dct = dict_.by_key(bonds(xgr), bnd_keys) return _create.from_atoms_and_bonds(atm_dct, bnd_dct)
def bond_induced_subgraph(xgr, bnd_keys, saddle=False): """ the subgraph induced by a subset of the bonds """ atm_keys = set(itertools.chain(*bnd_keys)) bnd_keys = set(bnd_keys) assert atm_keys <= atom_keys(xgr) if not saddle: assert bnd_keys <= bond_keys(xgr) atm_dct = dict_.by_key(atoms(xgr), atm_keys) bnd_dct = dict_.by_key(bonds(xgr), bnd_keys) return _create.from_atoms_and_bonds(atm_dct, bnd_dct)
def union(xgr1, xgr2): """ a union of two graphs """ assert not atom_keys(xgr1) & atom_keys(xgr2) atm_dct = {} atm_dct.update(atoms(xgr1)) atm_dct.update(atoms(xgr2)) bnd_dct = {} bnd_dct.update(bonds(xgr1)) bnd_dct.update(bonds(xgr2)) return _create.from_atoms_and_bonds(atm_dct, bnd_dct)
def bond_induced_subgraph(gra, bnd_keys, stereo=False): """ the subgraph induced by a subset of the bonds """ atm_keys = set(itertools.chain(*bnd_keys)) bnd_keys = set(bnd_keys) assert atm_keys <= atom_keys(gra) atm_dct = dict_.by_key(atoms(gra), atm_keys) bnd_dct = dict_.by_key(bonds(gra), bnd_keys) sub = _create.from_atoms_and_bonds(atm_dct, bnd_dct) if not stereo: sub = without_stereo_parities(sub) return sub
def remove_bonds(gra, bnd_keys, check=True): """ remove bonds from the molecular graph """ all_bnd_keys = bond_keys(gra) bnd_keys = set(map(frozenset, bnd_keys)) if check: assert bnd_keys <= all_bnd_keys bnd_keys = all_bnd_keys - bnd_keys atm_dct = atoms(gra) bnd_dct = dict_.by_key(bonds(gra), bnd_keys) return _create.from_atoms_and_bonds(atm_dct, bnd_dct)
def subgraph(gra, atm_keys, stereo=False): """ the subgraph induced by a subset of the atoms :param gra: the graph :param atm_keys: the atom keys to be included in the subgraph :param stereo: whether or not to include stereo in the subgraph :returns: the subgraph """ atm_keys = set(atm_keys) assert atm_keys <= atom_keys(gra) bnd_keys = set(filter(lambda x: x <= atm_keys, bond_keys(gra))) atm_dct = dict_.by_key(atoms(gra), atm_keys) bnd_dct = dict_.by_key(bonds(gra), bnd_keys) sub = _create.from_atoms_and_bonds(atm_dct, bnd_dct) if not stereo: sub = without_stereo_parities(sub) return sub
def relabel(xgr, atm_key_dct): """ relabel the graph with new atom keys """ orig_atm_keys = atom_keys(xgr) assert set(atm_key_dct.keys()) <= orig_atm_keys new_atm_key_dct = dict(zip(orig_atm_keys, orig_atm_keys)) new_atm_key_dct.update(atm_key_dct) _relabel_atom_key = new_atm_key_dct.__getitem__ def _relabel_bond_key(bnd_key): return frozenset(map(_relabel_atom_key, bnd_key)) atm_dct = dict_.transform_keys(atoms(xgr), _relabel_atom_key) bnd_dct = dict_.transform_keys(bonds(xgr), _relabel_bond_key) return _create.from_atoms_and_bonds(atm_dct, bnd_dct)
def from_yaml_dictionary(yaml_gra_dct, one_indexed=True): """ read the graph from a yaml dictionary """ atm_dct = yaml_gra_dct['atoms'] bnd_dct = yaml_gra_dct['bonds'] atm_dct = dict_.transform_values( atm_dct, lambda x: tuple(map(x.__getitem__, ATM_PROP_NAMES))) bnd_dct = dict_.transform_keys(bnd_dct, lambda x: frozenset(map(int, x.split('-')))) bnd_dct = dict_.transform_values( bnd_dct, lambda x: tuple(map(x.__getitem__, BND_PROP_NAMES))) gra = _create.from_atoms_and_bonds(atm_dct, bnd_dct) if one_indexed: # revert one-indexing if the input is one-indexed atm_key_dct = {atm_key: atm_key - 1 for atm_key in atom_keys(gra)} gra = relabel(gra, atm_key_dct) return gra
def from_string(gra_str): """ read the graph from a string """ yaml_gra_dct = yaml.load(gra_str, Loader=yaml.FullLoader) atm_dct = yaml_gra_dct['atoms'] bnd_dct = yaml_gra_dct['bonds'] atm_dct = dict_.transform_values( atm_dct, lambda x: tuple(map(x.__getitem__, ATM_PROP_NAMES))) bnd_dct = dict_.transform_keys( bnd_dct, lambda x: frozenset(map(int, x.split('-')))) bnd_dct = dict_.transform_values( bnd_dct, lambda x: tuple(map(x.__getitem__, BND_PROP_NAMES))) gra = _create.from_atoms_and_bonds(atm_dct, bnd_dct) # shift back to zero-indexing when we read it in atm_key_dct = {atm_key: atm_key-1 for atm_key in atom_keys(gra)} gra = relabel(gra, atm_key_dct) return gra
def set_bond_stereo_parities(sgr, bnd_par_dct): """ set bond parities """ bnd_dct = mdict.set_by_key_by_position(bonds(sgr), bnd_par_dct, BND_STE_PAR_POS) return _create.from_atoms_and_bonds(atoms(sgr), bnd_dct)
def set_atom_stereo_parities(sgr, atm_par_dct): """ set atom parities """ atm_dct = mdict.set_by_key_by_position(atoms(sgr), atm_par_dct, ATM_STE_PAR_POS) return _create.from_atoms_and_bonds(atm_dct, bonds(sgr))
def set_bond_orders(rgr, bnd_ord_dct): """ set bond orders """ bnd_dct = mdict.set_by_key_by_position(bonds(rgr), bnd_ord_dct, BND_ORD_POS) return _create.from_atoms_and_bonds(atoms(rgr), bnd_dct)