def _get_chem_form(chem_form_native, ion):
    chem_form_ion = ''
    for (i, c) in enumerate(chem_form_native):
        if ((i + 1) >= len(chem_form_native)):
            if c.isdigit():
                chem_form_ion += c
            else:
                chem_form_ion += c
                chem_form_ion += '1'
        elif c.isdigit():
            chem_form_ion += c
        elif (c.isupper() and chem_form_native[(i + 1)].isdigit()):
            chem_form_ion += c
        elif (c.isupper() and chem_form_native[(i + 1)].isupper()):
            chem_form_ion += c
            chem_form_ion += '1'
        elif chem_form_native[(i + 1)].isdigit():
            chem_form_ion += c
    list_chem = [''.join(g) for (_, g) in groupby(chem_form_ion, str.isalpha)]
    chem_form_ion = dict(zip(list_chem[::2], map(int, list_chem[1::2])))
    if ('+' not in ion):
        if ('[M-H]-' in ion):
            try:
                chem_form_ion['H'] -= 1
            except KeyError:
                logging.critical('ERROR: could not subtract atom when getting the ionized form from the molecule')
        if ('[M-2H](2-)' in ion):
            try:
                chem_form_ion['H'] -= 2
            except KeyError:
                logging.critical('ERROR: could not subtract atom when getting the ionized form from the molecule')
        if ('[M-Ac-H]-' in ion):
            try:
                chem_form_ion['C'] += 2
                chem_form_ion['H'] += 3
                chem_form_ion['O'] += 2
            except KeyError:
                logging.critical('ERROR: could not subtract atom when getting the ionized form from the molecule')
    else:
        if ('[M+H]+' in ion):
            try:
                chem_form_ion['H'] += 1
            except KeyError:
                logging.critical('ERROR: could not add atom when getting the ionized form from the molecule')
        if ('[M+NH4]+' in ion):
            try:
                if chem_form_ion.has_key('N'):
                    chem_form_ion['N'] += 1
                else:
                    chem_form_ion['N'] = 1
                chem_form_ion['H'] += 4
            except KeyError:
                logging.critical('ERROR: could not add atom when getting the ionized form from the molecule')
        if ('[M+Na]+' in ion):
            try:
                if chem_form_ion.has_key('Na'):
                    chem_form_ion['Na'] += 1
                else:
                    chem_form_ion['Na'] = 1
            except KeyError:
                logging.critical('ERROR: could not add atom when getting the ionized form from the molecule')
        if ('[M+Na2-H]+' in ion):
            try:
                if chem_form_ion.has_key('Na'):
                    chem_form_ion['Na'] += 2
                else:
                    chem_form_ion['Na'] = 2
                chem_form_ion['H'] -= 1
            except KeyError:
                logging.critical('ERROR: could not add atom when getting the ionized form from the molecule')
    return ''.join([(atom + str(num_atom)) for (atom, num_atom) in sorted(chem_form_ion.items())])
def _get_chem_form(chem_form_native, ion):
    chem_form_ion = 
    for (i, c) in enumerate(chem_form_native):
        if ((i + 1) >= len(chem_form_native)):
            if c.isdigit():
                chem_form_ion += c
            else:
                chem_form_ion += c
                chem_form_ion += '1'
        elif c.isdigit():
            chem_form_ion += c
        elif (c.isupper() and chem_form_native[(i + 1)].isdigit()):
            chem_form_ion += c
        elif (c.isupper() and chem_form_native[(i + 1)].isupper()):
            chem_form_ion += c
            chem_form_ion += '1'
        elif chem_form_native[(i + 1)].isdigit():
            chem_form_ion += c
    list_chem = [.join(g) for (_, g) in groupby(chem_form_ion, str.isalpha)]
    chem_form_ion = dict(zip(list_chem[::2], map(int, list_chem[1::2])))
    if ('+' not in ion):
        if ('[M-H]-' in ion):
            try:
                chem_form_ion['H'] -= 1
            except KeyError:
                logging.critical('ERROR: could not subtract atom when getting the ionized form from the molecule')
        if ('[M-2H](2-)' in ion):
            try:
                chem_form_ion['H'] -= 2
            except KeyError:
                logging.critical('ERROR: could not subtract atom when getting the ionized form from the molecule')
        if ('[M-Ac-H]-' in ion):
            try:
                chem_form_ion['C'] += 2
                chem_form_ion['H'] += 3
                chem_form_ion['O'] += 2
            except KeyError:
                logging.critical('ERROR: could not subtract atom when getting the ionized form from the molecule')
    else:
        if ('[M+H]+' in ion):
            try:
                chem_form_ion['H'] += 1
            except KeyError:
                logging.critical('ERROR: could not add atom when getting the ionized form from the molecule')
        if ('[M+NH4]+' in ion):
            try:
                if chem_form_ion.has_key('N'):
                    chem_form_ion['N'] += 1
                else:
                    chem_form_ion['N'] = 1
                chem_form_ion['H'] += 4
            except KeyError:
                logging.critical('ERROR: could not add atom when getting the ionized form from the molecule')
        if ('[M+Na]+' in ion):
            try:
                if chem_form_ion.has_key('Na'):
                    chem_form_ion['Na'] += 1
                else:
                    chem_form_ion['Na'] = 1
            except KeyError:
                logging.critical('ERROR: could not add atom when getting the ionized form from the molecule')
        if ('[M+Na2-H]+' in ion):
            try:
                if chem_form_ion.has_key('Na'):
                    chem_form_ion['Na'] += 2
                else:
                    chem_form_ion['Na'] = 2
                chem_form_ion['H'] -= 1
            except KeyError:
                logging.critical('ERROR: could not add atom when getting the ionized form from the molecule')
    return .join([(atom + str(num_atom)) for (atom, num_atom) in sorted(chem_form_ion.items())])