def test__backbone_keys(): """ test graph.backbone_keys """ assert graph.backbone_keys(CH2FH2H_CGR_EXP) == frozenset({0, 1, 2, 3})
def hydrogen_migrations(rct_gras, viable_only=True): """ find all possible hydrogen migration reactions for these reactants :param rct_gras: graphs for the reactants, without stereo and without overlapping keys :param viable_only: Filter out reactions with non-viable products? :type viable_only: bool :returns: a list of Reaction objects :rtype: tuple[Reaction] Hydrogen migrations are enumerated looping over unsaturated sites, adding hydrogens to them, and looping over non-equivalent heavy atoms and removing hydrgens from them. """ assert_is_valid_reagent_graph_list(rct_gras) rxns = [] if len(rct_gras) == 1: rct_gra, = rct_gras # Identify unsaturated sites rct_add_key = max(atom_keys(rct_gra)) + 1 rct_rad_keys = unsaturated_atom_keys(rct_gra) rct_hyd_keys = atom_keys(rct_gra, sym='H') for rct_rad_key in rct_rad_keys: # Add a hydrogen to the radical/unsaturated site rct_h_gra = add_bonded_atom(rct_gra, 'H', rct_rad_key, bnd_atm_key=rct_add_key) # Identify donor sites rct_don_keys = backbone_keys(rct_h_gra) - {rct_rad_key} for rct_don_key in rct_don_keys: rct_hyd_key = atom_neighbor_atom_key(rct_gra, rct_don_key, symbs_first=['H'], symbs_last=[]) if rct_hyd_key in rct_hyd_keys: prd_gra = remove_atoms(rct_h_gra, {rct_hyd_key}) prd_gra = relabel(prd_gra, {rct_add_key: rct_hyd_key}) forw_tsg = ts.graph(rct_gra, frm_bnd_keys=[(rct_rad_key, rct_hyd_key)], brk_bnd_keys=[(rct_don_key, rct_hyd_key)]) back_tsg = ts.graph(prd_gra, frm_bnd_keys=[(rct_don_key, rct_hyd_key)], brk_bnd_keys=[(rct_rad_key, rct_hyd_key)]) rxns.append( Reaction( rxn_cls=par.ReactionClass.Typ.HYDROGEN_MIGRATION, forw_tsg=forw_tsg, back_tsg=back_tsg, rcts_keys=[atom_keys(rct_gra)], prds_keys=[atom_keys(prd_gra)], )) if viable_only: rxns = filter_viable_reactions(rxns) return ts_unique(rxns)