import copy


def extract_subtree(tree, tips, df, color=False):
    """ Extract a subtree defined by tip names"""
    subtree_mrca = tree.common_ancestor(tips)
    subtree_clade = copy.deepcopy(subtree_mrca)

    # Search for the nodes, finding tips to prune
    for c in subtree_mrca.find_clades(order="postorder"):
        cur_term = [t.name for t in subtree_clade.get_terminals()]
        if c.name not in tips:
            if c.name in cur_term:
                subtree_clade.collapse(target=c.name)

    # Color branches
    if color:
        for c in subtree_clade.find_clades():
            c.color = color

    return subtree_clade