def cns_dist_restraint(resid_i, atom_i, resid_j, atom_j,
                       dist, lower, upper, weight=None,
                       comment=None):
    """
    Create a CNS distance restraint string

    Parameters
    ----------
    resid_i : int
        Index of first residue
    atom_i : str
        Name of selected atom in first residue
    resid_j : int
        Index of second residue
    atom_j : str
        Name of selected atom in second residue
    dist : float
        Restrain distance between residues to this value
    lower : float
        Lower bound delta on distance
    upper : float
        Upper bound delta on distance
    weight : float, optional (default: None)
        Weight for distance restraint
    comment : str, optional (default: None)
        Print comment at the end of restraint line

    Returns
    -------
    r : str
        Distance restraint
    """
    if weight is not None:
        weight_str = "weight {} ".format(weight)
    else:
        weight_str = ""

    if comment is not None:
        comment_str = "! {}".format(comment)
    else:
        comment_str = ""

    r = (
        "assign (resid {} and name {}) (resid {} and name {})  "
        "{} {} {} {}{}".format(
            resid_i, atom_i, resid_j, atom_j, dist, lower, upper,
            weight_str, comment_str
        )
    )

    return r