import re


def element_set_conversion(filter_expr):
    """
    Convert element_set filter to multiple element filters
    Input:
        :str filter_expr: raw filter expression w/ element_set parameter
            Valid element_set expression:
                ',': AND operator
                '-': OR operator
                '~': NOT operator
                '(', ')': to change precedence
            Examples:
                element_set=Al;O,H
                element_set=(Mn;Fe),O
    Output:
        :str : converted filter expression
    """
    filter_expr_out = filter_expr

    for els in re.findall('element_set=[\S]*', filter_expr):
        els_out = els.replace('element_set=', '')

        for el in re.findall('[A-Z][a-z]*', els):
            els_out = els_out.replace(el, ' element='+el+' ')

        els_out = els_out.replace(',', ' AND ')
        els_out = els_out.replace('-', ' OR ')

        filter_expr_out = filter_expr_out.replace(els, '('+els_out+')')

    return filter_expr_out