def _bring_orgs_to_desired_number_in_tree(tree, mutation_rate,
                                          delta_number_of_orgs):
    """
    Adds (by fitness) or removes (uniformily) orgs from tree by delta number
    of orgs (positive adds, negative removes).
    Returns modified tree
    """
    while delta_number_of_orgs > 0:
        tree = _add_child_by_fitness_to_tree(tree, mutation_rate)
        delta_number_of_orgs -= 1
    while delta_number_of_orgs < 0:
        tree, removed_org = fitness_tree.remove_leaf_uniformly(tree)
        delta_number_of_orgs += 1
    return tree
def _bring_orgs_to_desired_number_in_tree(tree, mutation_rate,
                                          delta_number_of_orgs):
    """
    Adds (by fitness) or removes (uniformily) orgs from tree by delta number
    of orgs (positive adds, negative removes).
    Returns modified tree
    """
    while delta_number_of_orgs > 0:
        tree = _add_child_by_fitness_to_tree(tree, mutation_rate)
        delta_number_of_orgs -= 1
    while delta_number_of_orgs < 0:
        tree, removed_org = fitness_tree.remove_leaf_uniformly(tree)
        delta_number_of_orgs += 1
    return tree
def moran_death_birth(orgs, mutation_rate, desired_number_of_orgs=None):
    """
    Method to execute the replacement of organism in a death-birth
    fashion using fecundity to replace the randomly selected death organism
    """
    tree = fitness_tree.build_tree(orgs)
    if desired_number_of_orgs is None:
        desired_number_of_orgs = len(orgs)
    tree = _bring_orgs_to_desired_number_in_tree(
        tree, mutation_rate, desired_number_of_orgs - len(orgs))
    for _ in range(desired_number_of_orgs):
        tree, removed_org = fitness_tree.remove_leaf_uniformly(tree)
        tree = _add_child_by_fitness_to_tree(tree, mutation_rate)
    return fitness_tree.tree_to_list(tree)
def moran_death_birth(orgs, mutation_rate, desired_number_of_orgs=None):
    """
    Method to execute the replacement of organism in a death-birth
    fashion using fecundity to replace the randomly selected death organism
    """
    tree = fitness_tree.build_tree(orgs)
    if desired_number_of_orgs is None:
        desired_number_of_orgs = len(orgs)
    tree = _bring_orgs_to_desired_number_in_tree(
        tree, mutation_rate, desired_number_of_orgs - len(orgs))
    for _ in range(desired_number_of_orgs):
        tree, removed_org = fitness_tree.remove_leaf_uniformly(tree)
        tree = _add_child_by_fitness_to_tree(tree, mutation_rate)
    return fitness_tree.tree_to_list(tree)