def test_read_table_no_header():
    ped = Pedigree.from_table(
        "data/test/simple-no-header.tsv",
        header=False,
        check_2_parents=False,
        attrs=["sex"],
    )
    assert list(ped.graph.nodes) == list(range(1, 8 + 1))
Beispiel #2
0
def test_left_climber_single_source():
    ped = Pedigree.from_balsac_table('data/test/simple.tsv')

    path_taken = [8]
    climber = Climber(ped, [8])
    for node, parents in climber:
            # chose left parent
            chosen_parent = min(parents)
            climber.queue(chosen_parent)
            path_taken.append(chosen_parent)

    assert path_taken == [8, 5, 1]
def test_sex(pedigree):
    ped_df = get_test_pedigree_table(pedigree)
    ped = get_test_pedigree(pedigree)
    inferred_sex = Pedigree.infer_sex(
        ped_df.individual.values, ped_df.father.values, ped_df.mother.values
    )

    expected_sex = ped.get_node_attributes("sex")
    # we can't infer the sex of probands
    mask = dict(zip(ped.probands(), repeat(-1)))
    expected_sex.update(mask)
    assert integer_dict(inferred_sex) == expected_sex
def test_depth_ordering_with_shuffle(pedigree):
    np.random.seed(100)
    label = count(1)
    ped = get_test_pedigree(pedigree)

    # shuffle labels
    orig_labels = list(ped.nodes)
    random_labels = np.random.choice(orig_labels, ped.n_individuals, replace=False)
    shuffled = Pedigree(
        nx.relabel_nodes(ped.graph, dict(zip(orig_labels, random_labels)))
    )

    # infer and order by depth
    depth = shuffled.infer_depth()
    ordered_nodes = sorted(shuffled.nodes, key=lambda n: depth[n])
    ordered_labels = [next(label) for n in ordered_nodes]
    mapping = dict(zip(ordered_nodes, ordered_labels))

    # check oredring is correct
    for parent, child in shuffled.trace_edges():
        assert mapping[parent] < mapping[child]
Beispiel #5
0
def test_right_climber():
    ped = Pedigree.from_balsac_table('data/test/simple.tsv')

    path_taken = [7, 8]
    climber = Climber(ped, [7, 8])
    for node, parents in climber:
        if parents:
            # chose right parent
            chosen_parent = max(parents)
            climber.queue(chosen_parent)
            path_taken.append(chosen_parent)

    assert path_taken == [7, 8, 6, 4, 2]
def test_to_table_with_inferred_sex():
    ped_df = get_test_pedigree_table("simple")
    ped = get_test_pedigree("simple")

    inferred_sex = Pedigree.infer_sex(ped_df.individual, ped_df.father, ped_df.mother)
    nx.set_node_attributes(ped.graph, dict(zip(ped_df.individual, inferred_sex)), "sex")
    tbl = ped.to_table()
    tbl.reset_index(inplace=True)

    assert np.all(ped_df.individual == tbl.individual)
    assert np.all(ped_df.father == tbl.father)
    assert np.all(ped_df.mother == tbl.mother)
    assert np.all([1, 2, 2, 1, 2, 1, -1, -1] == tbl.sex)
# Example showing the class hierarchy

from genealogy_aligner import Pedigree
import matplotlib.pyplot as plt
from .util import get_basename

founders = 10
generations = 4
avg_children = 2
avg_out_of_family = 2
G = Pedigree.simulate_from_founders(founders, generations, avg_children, avg_out_of_family)
    
T = G.sample_path()
C = T.to_coalescent_tree()

fig, ax = plt.subplots(ncols=3, figsize=(18, 6))

G.draw(ax=ax[0])
ax[0].set_title('Genealogy')
T.draw(ax=ax[1])
ax[1].set_title('Coalescent traversal')
C.draw(ax=ax[2])
ax[2].set_title('Coalescent tree')

fig.savefig(f'fig/{get_basename(__file__)}.svg', dpi=300)
fig.savefig(f'fig/{get_basename(__file__)}.png', dpi=300)


# t = nx.DiGraph()
# t.add_nodes_from(
#     [
#         #(7, {"time": 0}),
#         (8, {"time": 0}),
#         (9, {"time": 0}),
#         (10, {"time": 0}),
#         (4, {"time": 1}),
#         (5, {"time": 1}),
#         (1, {"time": 2}),
#     ]
# )
# t.add_edges_from([[5, 10], [5, 9], [1, 4], [4, 8], [1, 5]])
# T = Traversal(t)

P = Pedigree.simulate_from_founders(6, 3, 2, 5)
probands = P.probands()
T = P.sample_path(probands)
coal = T.to_coalescent_tree()

C = copy.deepcopy(coal)
nx.set_node_attributes(C.graph, False, 'inferred')
nx.set_edge_attributes(C.graph, False, 'inferred')

index = dict(zip(P.nodes, [P.nodes.index(n) for n in P.nodes]))
prob_idx = [P.nodes.index(n)
            for n in probands]  # make sure it's the same in the tree

dist = distances(C)
tbl = P.to_table()
K = kinship_matrix(tbl.individual, tbl.mother, tbl.father, tbl.time)
from genealogy_aligner import Pedigree

print("Simulating from a random pedigree...")
ped = Pedigree.simulate_from_founders(10, 10, 2, 1)
probands = ped.probands()
sim = ped.generate_msprime_simulations(model_after=None)

print("Simulating from geneaJi pedigree...")
gen_ji = Pedigree.from_table('../data/geneaJi.tsv', header=True, check_2_parents=False)
print(gen_ji.attributes)
gji_sim = gen_ji.generate_msprime_simulations(model_after=None)

print("Simulating from a sample pedigree (kinship2)...")
sample_ped = Pedigree.from_table('../data/kinship2_sample1_ped.tsv', header=True)
print(sample_ped.attributes)
sp_sim = sample_ped.generate_msprime_simulations(model_after=None)

print("Simulating from BALSAC pedigree...")
balsac = Pedigree.from_table('../data/balsac.tsv', header=True)
bprobands = balsac.probands()
bsim = balsac.generate_msprime_simulations(model_after=None)

print("Done!")
def test_read_balsac_table_whitespace():
    ped = Pedigree.from_balsac_table("data/test/simple.whitespace", sep=r"\s+")
    assert list(ped.graph.nodes) == list(range(1, 8 + 1))
def test_read_balsac_table_csv():
    ped = Pedigree.from_balsac_table("data/test/simple.csv", sep=",")
    assert list(ped.graph.nodes) == list(range(1, 8 + 1))
def get_test_pedigree(name):
    return Pedigree.from_balsac_table("data/test/" + name + ".tsv")