def stereo_priority_vector(gra, atm_key, atm_ngb_key): """ generates a sortable one-to-one representation of the branch extending from `atm_key` through its bonded neighbor `atm_ngb_key` """ bbn_keys = backbone_keys(gra) exp_hyd_keys = explicit_hydrogen_keys(gra) if atm_ngb_key not in bbn_keys: assert atm_ngb_key in exp_hyd_keys assert frozenset({atm_key, atm_ngb_key}) in bond_keys(gra) pri_vec = () else: gra = implicit(gra) atm_dct = atoms(gra) bnd_dct = bonds(gra) assert atm_key in bbn_keys assert frozenset({atm_key, atm_ngb_key}) in bnd_dct # here, switch to an implicit graph atm_ngb_keys_dct = atoms_neighbor_atom_keys(gra) def _priority_vector(atm1_key, atm2_key, seen_keys): # we keep a list of seen keys to cut off cycles, avoiding infinite # loops bnd_val = bnd_dct[frozenset({atm1_key, atm2_key})] atm_val = atm_dct[atm2_key] bnd_val = _replace_nones_with_negative_infinity(bnd_val) atm_val = _replace_nones_with_negative_infinity(atm_val) if atm2_key in seen_keys: ret = (bnd_val, ) else: seen_keys.update({atm1_key, atm2_key}) atm3_keys = atm_ngb_keys_dct[atm2_key] - {atm1_key} if atm3_keys: next_vals, seen_keys = zip(*[ _priority_vector(atm2_key, atm3_key, seen_keys) for atm3_key in atm3_keys ]) ret = (bnd_val, atm_val) + next_vals else: ret = (bnd_val, atm_val) return ret, seen_keys pri_vec, _ = _priority_vector(atm_key, atm_ngb_key, set()) return pri_vec
def radical_group_dct(gra): """ return a dictionary of lists of groups attached each radical """ gra = without_fractional_bonds(gra) groups = {} rads = list(sing_res_dom_radical_atom_keys(gra)) atms = atoms(gra) for rad in rads: key = atms[rad][0] if key in groups: groups[atms[rad][0]] += atom_groups(gra, rad) else: groups[atms[rad][0]] = atom_groups(gra, rad) return groups
def from_graph(gra): """ igraph object from a molecular graph """ atm_keys = sorted(atom_keys(gra)) bnd_keys = sorted(bond_keys(gra), key=sorted) atm_vals = dict_.values_by_key(atoms(gra), atm_keys) bnd_vals = dict_.values_by_key(bonds(gra), bnd_keys) atm_colors = list(itertools.starmap(_encode_vertex_attributes, atm_vals)) bnd_colors = list(itertools.starmap(_encode_edge_attributes, bnd_vals)) atm_idx_dct = dict(map(reversed, enumerate(atm_keys))) bnd_idxs = [sorted(map(atm_idx_dct.__getitem__, k)) for k in bnd_keys] igr = igraph.Graph(bnd_idxs) igr.vs['keys'] = atm_keys igr.vs['color'] = atm_colors igr.es['color'] = bnd_colors return igr