import re


def parse_chains(chains):
    """Parses a given chains string returned from the MySQL DB
    """
    ## Turns "A:100:1:aa" -> "A", "100", "1", "aa"
    ## I.e.: chain_id = A, num_residues = 100,
    ##       selected = 1/True, type = aa/amino acids
    ## NOTE:
    ##   - aa = amino acid
    ##   - na = nucleic acid
    ##   - ot = other
    chid     = re.sub(r'([A-Za-z0-9]):([0-9]{1,}):([01]):([naot]{2});?', '\\1;', chains).rstrip(";")
    length   = re.sub(r'([A-Za-z0-9]):([0-9]{1,}):([01]):([naot]{2});?', '\\2;', chains).rstrip(";")
    selected = re.sub(r'([A-Za-z0-9]):([0-9]{1,}):([01]):([naot]{2});?', '\\3;', chains).rstrip(";")
    type     = re.sub(r'([A-Za-z0-9]):([0-9]{1,}):([01]):([naot]{2});?', '\\4;', chains).rstrip(";")

    return chid, length, selected, type