예제 #1
0
def from_data(formula_sublayer,
              main_sublayer_dct=None,
              charge_sublayer_dct=None,
              stereo_sublayer_dct=None,
              isotope_sublayer_dct=None):
    """ calculate an inchi string from layers
    """
    main_dct = dict_.empty_if_none(main_sublayer_dct)
    char_dct = dict_.empty_if_none(charge_sublayer_dct)
    ste_dct = dict_.empty_if_none(stereo_sublayer_dct)
    iso_dct = dict_.empty_if_none(isotope_sublayer_dct)

    fml_slyr = formula_sublayer
    main_slyrs = [
        pfx + slyr for pfx, slyr in zip(
            MAIN_PFXS, dict_.values_by_key(main_dct, MAIN_PFXS)) if slyr
    ]
    char_slyrs = [
        pfx + slyr for pfx, slyr in zip(
            CHAR_PFXS, dict_.values_by_key(char_dct, CHAR_PFXS)) if slyr
    ]
    ste_slyrs = [
        pfx + slyr
        for pfx, slyr in zip(STE_PFXS, dict_.values_by_key(ste_dct, STE_PFXS))
        if slyr
    ]
    iso_slyrs = [
        pfx + slyr
        for pfx, slyr in zip(ISO_PFXS, dict_.values_by_key(iso_dct, ISO_PFXS))
        if slyr
    ]

    ich = '/'.join(['InChI=1', fml_slyr] + main_slyrs + char_slyrs +
                   ste_slyrs + iso_slyrs)
    return ich
예제 #2
0
def bonds_from_data(bond_keys, bond_orders=None, bond_stereo_parities=None):
    """ construct bond dictionary graph from data

    format:
        bnd_dct := {bnd_key: (bnd_ord, bnd_ste_par), ...}
        [where bnd_key := frozenset({atm1_key, atm2_key})]

    :param bond_keys: bond keys
    :type bond_keys: set
    :param bond_orders: bond orders, by bond key
    :type bond_orders: dict
    :param bond_stereo_parities: bond stereo parities, by bond key
    :type bond_stereo_parities: dict
    """
    keys = sorted(bond_keys)
    assert all(len(key) == 2 for key in keys)
    ords = dict_.values_by_key(dict_.empty_if_none(bond_orders),
                               keys,
                               fill_val=1)
    pars = dict_.values_by_key(dict_.empty_if_none(bond_stereo_parities),
                               keys,
                               fill_val=None)

    nbnds = len(keys)

    ords = [1] * nbnds if ords is None else list(ords)
    pars = [None] * nbnds if pars is None else list(pars)

    assert len(ords) == nbnds
    assert len(pars) == nbnds

    keys = list(map(frozenset, keys))
    ords = list(map(int, ords))

    assert all(par in (None, False, True) for par in pars)
    pars = [bool(par) if par is not None else par for par in pars]

    bnd_dct = dict(zip(keys, zip(ords, pars)))
    return bnd_dct
예제 #3
0
def atoms_from_data(atom_symbols,
                    atom_implicit_hydrogen_valences=None,
                    atom_stereo_parities=None):
    """ construct atom dictionary from data

    format:
        atm_dct := {atm_key: (atm_sym, atm_imp_hyd_vlc, atm_ste_par), ...}

    :param atom_symbols: atomic symbols, by atom key
    :type atom_symbols: dict
    :param atom_implicit_hydrogen_valences: the number of implicit hydrogens
        associated with each atom, by atom key
    :type atom_implicit_hydrogen_valences: dict
    :param atom_stereo_parities: atom stereo parities, by atom key
    :type atom_stereo_parities: dict
    """
    keys = sorted(atom_symbols.keys())
    syms = dict_.values_by_key(atom_symbols, keys)
    vlcs = dict_.values_by_key(
        dict_.empty_if_none(atom_implicit_hydrogen_valences), keys, fill_val=0)
    pars = dict_.values_by_key(dict_.empty_if_none(atom_stereo_parities),
                               keys,
                               fill_val=None)

    natms = len(syms)
    vlcs = [0] * natms if vlcs is None else list(vlcs)
    pars = [None] * natms if pars is None else list(pars)

    assert len(vlcs) == natms
    assert len(pars) == natms

    syms = list(map(pt.to_E, syms))
    vlcs = list(map(int, vlcs))

    assert all(par in (None, False, True) for par in pars)
    pars = [bool(par) if par is not None else par for par in pars]

    atm_dct = dict(zip(keys, zip(syms, vlcs, pars)))
    return atm_dct