# AUTOGENERATED FILE! PLEASE DON'T EDIT HERE. EDIT THE SOURCE NOTEBOOKS INSTEAD
from .atom import Atom, mo, _a
from functools import partial
from typing import List
import k1lib, re
__all__ = ["alkane", "alcohol", "sameEmpirical"]
# a little bit complex molecules
def _mole(name, f):                                                              # _mole
    _a[name] = f; mo.registerSubstance(name, f)                                  # _mole
_mole("H2O", lambda: mo.O(mo.H)(mo.H))                                           # _mole
_mole("CH4", lambda: mo.C(mo.H)(mo.H)(mo.H)(mo.H))                               # _mole
_mole("COOH", lambda: mo.C(mo.O, 2)(mo.O(mo.H))(mo.H))                           # _mole
_mole("NH3", lambda: mo.N(mo.H)(mo.H)(mo.H))                                     # _mole
_mole("CH3OH", lambda: mo.CH4(mo.H2O))                                           # _mole
def glucose():                                                                   # glucose
    o = mo.O                                                                     # glucose
    o.bond(mo.CH4).bond(mo.CH3OH).bond(mo.CH3OH).bond(mo.CH3OH).bond(mo.CH3OH).bond(o) # glucose
    o.next().bond(mo.CH3OH); return o                                            # glucose
_mole("glucose", glucose)                                                        # glucose
def cyclohexane():                                                               # cyclohexane
    c = mo.CH4                                                                   # cyclohexane
    return c.bond(mo.CH4).bond(mo.CH4).bond(mo.CH4).bond(mo.CH4).bond(mo.CH4).bond(c) # cyclohexane
_mole("cyclohexane", cyclohexane)                                                # cyclohexane
def benzene():                                                                   # benzene
    c = mo.CH4                                                                   # benzene
    return c.main(mo.CH4, 2).main(mo.CH4).main(mo.CH4, 2).main(mo.CH4).main(mo.CH4, 2).main(c) # benzene
_mole("benzene", benzene)                                                        # benzene
def adenine():                                                                   # adenine
    n = mo.NH3                                                                   # adenine
    c1 = n.bond(mo.CH4).bond(mo.NH3, 2).bond(mo.CH4)                             # adenine
    c2 = c1.bond(mo.CH4, 2); c2.bond(n)                                          # adenine
    c1.bond(mo.CH4)(mo.NH3).bond(mo.NH3, 2).bond(mo.CH4).bond(mo.NH3, 2).bond(c2); # adenine
    return n                                                                     # adenine
_mole("adenine", adenine)                                                        # adenine
def ribose():                                                                    # ribose
    o = mo.H2O                                                                   # ribose
    return o.bond(mo.CH4(mo.CH3OH)).main(mo.CH3OH).main(mo.CH3OH).main(mo.CH3OH).main(o) # ribose
_mole("ribose", ribose)                                                          # ribose
def adenosine(): ri = mo.ribose; ri.next(1)(mo.adenine); return ri               # adenosine
_mole("adenosine", adenosine)                                                    # adenosine
def alkane(n:int) -> Atom:                                                       # alkane
    """Creates an alkane with ``n`` carbons.
Example::

    # returns "C3H8"
    mo.alkane(3).empirical()"""                                                  # alkane
    answer = mo.CH4                                                              # alkane
    for i in range(n-1): answer = answer.bond(mo.CH4)                            # alkane
    return answer                                                                # alkane
def alcohol(n:int, loc:int=1) -> Atom:                                           # alcohol
    """Creates an alcohol with ``n`` carbons and an OH group at carbon ``loc``.
Example::

    # returns "C3H8O"
    mo.alcohol(3, 1).empirical()"""                                              # alcohol
    a = alkane(n)                                                                # alcohol
    a.next(times=loc-1)(mo.H2O)                                                  # alcohol
    return a                                                                     # alcohol
@k1lib.patch(Atom)                                                               # alcohol
def perfluoro_ize(mainA:Atom) -> Atom:                                           # perfluoro_ize
    """Replaces all C-H bonds with C-F bonds"""                                  # perfluoro_ize
    for a in mainA.atoms():                                                      # perfluoro_ize
        b = a.next()                                                             # perfluoro_ize
        if a.name == "H" and b.name == "C":                                      # perfluoro_ize
            a.removeBond(b); b(mo.F)                                             # perfluoro_ize
    return mainA                                                                 # perfluoro_ize
empC = re.compile("[A-Z][a-z]*[0-9]*")                                           # perfluoro_ize
def sameEmpirical(a:str, b:str) -> bool:                                         # sameEmpirical
    """Checks whether 2 empirical formula are the same or not.
Example::

    moparse.sameEmpirical("C2H4", "H4C2") # returns True"""                      # sameEmpirical
    return sorted(re.findall(empC, a)) == sorted(re.findall(empC, b))            # sameEmpirical
