def standard_keys_for_sequence(gras): """ assigns non-overlapping keys to a sequence of graphs (returns a series of key maps for each) """ atm_key_dcts = [] shift = 0 for gra in gras: natms = atom_count(gra, dummy=True, with_implicit=False) atm_key_dct = { atm_key: idx + shift for idx, atm_key in enumerate(sorted(atom_keys(gra))) } atm_key_dcts.append(atm_key_dct) shift += natms gras = [ relabel(gra, atm_key_dct) for gra, atm_key_dct in zip(gras, atm_key_dcts) ] return gras, atm_key_dcts
def insert_bonded_atom(gra, sym, atm_key, bnd_atm_key=None, imp_hyd_vlc=None, atm_ste_par=None, bnd_ord=None, bnd_ste_par=None): """ insert a single atom with a bond to an atom already in the graph Keys will be standardized upon insertion """ keys = sorted(atom_keys(gra)) bnd_atm_key_ = max(keys) + 1 gra = add_bonded_atom(gra, sym, atm_key, bnd_atm_key=bnd_atm_key_, imp_hyd_vlc=imp_hyd_vlc, atm_ste_par=atm_ste_par, bnd_ord=bnd_ord, bnd_ste_par=bnd_ste_par) if bnd_atm_key != bnd_atm_key_: assert bnd_atm_key in keys idx = keys.index(bnd_atm_key) key_dct = {} key_dct.update({k: k for k in keys[:idx]}) key_dct[bnd_atm_key_] = bnd_atm_key key_dct.update({k: k + 1 for k in keys[idx:]}) gra = relabel(gra, key_dct) return gra
def _shift_remove_dummy_atom(gra, dummy_key): keys = sorted(automol.graph.atom_keys(gra)) idx = keys.index(dummy_key) key_dct = {} key_dct.update({k: k for k in keys[:idx]}) key_dct.update({k: k - 1 for k in keys[(idx + 1):]}) gra = remove_atoms(gra, [dummy_key]) gra = relabel(gra, key_dct) return gra
def relabel_for_zmatrix(gra, zma_keys, dummy_key_dct): """ relabel a geometry graph to line up with a z-matrix Inserts dummy atoms and sorts/relabels in z-matrix order. Graph keys should correspond to the geometry used for conversion. :param gra: the graph :param zma_keys: graph keys in the order they appear in the z-matrix :param dummy_key_dct: dummy keys introduced on z-matrix conversion, by atom they are attached to """ gra = add_dummy_atoms(gra, dummy_key_dct) key_dct = dict(map(reversed, enumerate(zma_keys))) gra = relabel(gra, key_dct) return gra
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 standard_keys(gra): """ replace the current atom keys with standard indices, counting from zero """ atm_key_dct = dict(map(reversed, enumerate(sorted(atom_keys(gra))))) return relabel(gra, atm_key_dct)
def transform_keys(gra, atm_key_func): """ transform atom keys with a function """ atm_keys = atom_keys(gra) atm_key_dct = dict(zip(atm_keys, map(atm_key_func, atm_keys))) return relabel(gra, atm_key_dct)