def from_data(fml_str, main_lyr_dct=None, char_lyr_dct=None, ste_lyr_dct=None, iso_lyr_dct=None): """ Build a ChI string from each of the various layers. :param fml_str: formula string, in Hill-sort order :type fml_str: str :param main_lyr_dct: main layers, specifying connectivity and implicit hydrogens, by key ('c' and 'h') :type main_lyr_dct: dict[str: str] :param char_lyr_dct: charge layers, by key ('q' and 'p') :type char_lyr_dct: dict[str: str] :param ste_lyr_dct: stero layers, by key ('b', 't', 'm', and 's') :type ste_lyr_dct: dict[str: str] :param iso_lyr_dct: isotope layers, by key ('i', 'h', 'b', 't', 'm', and 's') :type iso_lyr_dct: dict[str: str] :rtype: str """ main_dct = dict_.empty_if_none(main_lyr_dct) char_dct = dict_.empty_if_none(char_lyr_dct) ste_dct = dict_.empty_if_none(ste_lyr_dct) iso_dct = dict_.empty_if_none(iso_lyr_dct) main_lyrs = [ pfx + lyr for pfx, lyr in zip(MAIN_PFXS, dict_.values_by_key(main_dct, MAIN_PFXS)) if lyr ] char_lyrs = [ pfx + lyr for pfx, lyr in zip(CHAR_PFXS, dict_.values_by_key(char_dct, CHAR_PFXS)) if lyr ] ste_lyrs = [ pfx + lyr for pfx, lyr in zip(STE_PFXS, dict_.values_by_key(ste_dct, STE_PFXS)) if lyr ] iso_lyrs = [ pfx + lyr for pfx, lyr in zip(ISO_PFXS, dict_.values_by_key(iso_dct, ISO_PFXS)) if lyr ] chi = '/'.join(['AMChI=1', fml_str] + main_lyrs + char_lyrs + ste_lyrs + iso_lyrs) return chi
def from_data(fml_slyr, main_lyr_dct=None, char_lyr_dct=None, ste_lyr_dct=None, iso_lyr_dct=None): """ Build an InChI string from each of the various layers. :param fml_slyr: sublayer of InChI string containing molecular formula :type fml_slyr: str :param main_lyr_dct: information for connectivity layer of InChI :type main_lyr_dct: dict[str: str] :param char_lyr_dct: information for charge layer of InChI :type char_lyr_dct: dict[str: str] :param ste_lyr_dct: information for stereochemistry layer of InChI :type ste_lyr_dct: dict[str: str] :param iso_lyr_dct: information for isotope layer of InChI :type iso_lyr_dct: dict[str: str] :rtype: str """ main_dct = dict_.empty_if_none(main_lyr_dct) char_dct = dict_.empty_if_none(char_lyr_dct) ste_dct = dict_.empty_if_none(ste_lyr_dct) iso_dct = dict_.empty_if_none(iso_lyr_dct) 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
def atoms_from_data(atm_symb_dct, atm_imp_hyd_vlc_dct=None, atm_ste_par_dct=None): """ Construct an atom dictionary from constituent data. format: atm_dct := {atm_key: (atm_sym, atm_imp_hyd_vlc, atm_ste_par), ...} :param atm_symb_dct: atomic symbols, by atom key :type atm_symb_dct: dict :param atm_imp_hyd_vlc_dct: the no. of implicit hydrogens associated with each atom, by atom key :type atm_imp_hyd_vlc_dct: dict :param atm_ste_par_dct: atom stereo parities, by atom key :type atm_ste_par_dct: dict """ keys = sorted(atm_symb_dct.keys()) symbs = dict_.values_by_key(atm_symb_dct, keys) vlcs = dict_.values_by_key(dict_.empty_if_none(atm_imp_hyd_vlc_dct), keys, fill_val=0) pars = dict_.values_by_key(dict_.empty_if_none(atm_ste_par_dct), keys, fill_val=None) natms = len(symbs) 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 symbs = list(map(ptab.to_symbol, symbs)) 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(symbs, vlcs, pars))) return atm_dct
def bonds_from_data(bnd_keys, bnd_ord_dct=None, bnd_ste_par_dct=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 bnd_keys: bond keys :type bnd_keys: set :param bnd_ord_dct: bond orders, by bond key :type bnd_ord_dct: dict :param bnd_ste_par_dct: bond stereo parities, by bond key :type bnd_ste_par_dct: dict :rtype: dict[frozenset({int}): tuple(str, str))] """ keys = sorted(bnd_keys) assert all(len(key) == 2 for key in keys) ords = dict_.values_by_key(dict_.empty_if_none(bnd_ord_dct), keys, fill_val=1) pars = dict_.values_by_key(dict_.empty_if_none(bnd_ste_par_dct), 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 = [int(o) if round(o) == o else float(round(o, 1)) for o in 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