import json


def cluster_from_string(cluster_string):
    """
    If a cluster gets converted to a string (e.g., when the cluster is written
    to a text file), this function allows you to recover the cluster as a normal
    Python object (a list of pairs of integers).

    Parameters
    ----------
    cluster_string : str
        The string representation of the cluster.

    Returns
    -------
    list of list of int
        The inner lists are pairs of integers specifying the row and column
        indices of the pixels in the cluster.

    Examples
    --------
    >>> from hic3defdr.util.clusters import cluster_from_string
    >>> cluster = [(4, 5), (3, 4), (3, 5), (3, 6)]
    >>> cluster_string = str(cluster)
    >>> cluster_string
    '[(4, 5), (3, 4), (3, 5), (3, 6)]'
    >>> cluster_from_string(cluster_string)
    [[4, 5], [3, 4], [3, 5], [3, 6]]
    """
    return json.loads(
        cluster_string.replace('(', '[')
                      .replace('{', '[')
                      .replace(')', ']')
                      .replace('}', ']')
    )