def ts_bond_keys(gra): """ get keys for all order-0 bonds in the graph """ bnd_ord_dct = bond_orders(gra) ts_bnd_keys = tuple(bnd_key for bnd_key, bnd_ord in bnd_ord_dct.items() if bnd_ord == 0) return ts_bnd_keys
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 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 without_bond_orders(gra): """ resonance graph with maximum spin (i.e. no pi bonds) """ bnd_keys = list(bond_keys(gra)) # don't set dummy bonds to one! bnd_ord_dct = bond_orders(gra) bnd_vals = [ 1 if v != 0 else 0 for v in map(bnd_ord_dct.__getitem__, bnd_keys) ] bnd_ord_dct = dict(zip(bnd_keys, bnd_vals)) return set_bond_orders(gra, bnd_ord_dct)
def atom_bond_valences(gra, bond_order=True): """ bond count (bond valence), by atom """ atm_keys = list(atom_keys(gra)) gra = explicit(gra) if not bond_order: gra = without_bond_orders(gra) atm_nbhs = dict_.values_by_key(atom_neighborhoods(gra), atm_keys) atm_bnd_vlcs = [sum(bond_orders(nbh).values()) for nbh in atm_nbhs] atm_bnd_vlc_dct = dict_.transform_values(dict(zip(atm_keys, atm_bnd_vlcs)), int) return atm_bnd_vlc_dct
def from_graph(gra): """ networkx graph object from a molecular graph """ nxg = networkx.Graph() nxg.add_nodes_from(atom_keys(gra)) nxg.add_edges_from(bond_keys(gra)) networkx.set_node_attributes(nxg, atom_symbols(gra), 'symbol') networkx.set_node_attributes(nxg, atom_implicit_hydrogen_valences(gra), 'implicit_hydrogen_valence') networkx.set_node_attributes(nxg, atom_stereo_parities(gra), 'stereo_parity') networkx.set_edge_attributes(nxg, bond_orders(gra), 'order') networkx.set_edge_attributes(nxg, bond_stereo_parities(gra), 'stereo_parity') return nxg