Esempio n. 1
0
def test_emit_internal():
    """
    Calculate emission probabilities for internal branches
    """

    k = 10
    n = 1e4
    rho = 1.5e-8 * 20
    mu = 2.5e-8 * 20
    length = int(10e3) / 20
    times = argweaver.get_time_points(ntimes=20, maxtime=200000)

    arg = argweaver.sample_arg_dsmc(k,
                                    2 * n,
                                    rho,
                                    start=0,
                                    end=length,
                                    times=times)

    muts = argweaver.sample_arg_mutations(arg, mu, times)
    seqs = argweaver.make_alignment(arg, muts)

    trees, names = argweaverc.arg2ctrees(arg, times)
    seqs2, nseqs, seqlen = argweaverc.seqs2cseqs(seqs, names)

    assert argweaverc.argweaver_assert_emit_internal(trees, len(times), times,
                                                     mu, seqs2, nseqs, seqlen)
Esempio n. 2
0
def test_trans_switch():
    """
    Calculate transition probabilities for switch matrix

    Only calculate a single matrix
    """

    k = 12
    n = 1e4
    rho = 1.5e-8 * 20
    length = 1000
    times = argweaver.get_time_points(ntimes=20, maxtime=200000)
    popsizes = [n] * len(times)

    recombs = []

    while len(recombs) == 0:
        arg = argweaver.sample_arg_dsmc(k,
                                        2 * n,
                                        rho,
                                        start=0,
                                        end=length,
                                        times=times)
        recombs = [x.pos for x in arg if x.event == "recomb"]

    pos = recombs[0]
    tree = arg.get_marginal_tree(pos - .5)
    rpos, r, c = arglib.iter_arg_sprs(arg, start=pos - .5).next()
    spr = (r, c)

    assert argweaverc.assert_transition_switch_probs(tree, spr, times,
                                                     popsizes, rho)
Esempio n. 3
0
def test_emit():
    """
    Calculate emission probabilities
    """

    k = 10
    n = 1e4
    rho = 1.5e-8 * 20
    mu = 2.5e-8 * 20
    length = int(1e3) / 20
    times = argweaver.get_time_points(ntimes=20, maxtime=200000)

    arg = argweaver.sample_arg_dsmc(k,
                                    2 * n,
                                    rho,
                                    start=0,
                                    end=length,
                                    times=times)

    muts = argweaver.sample_arg_mutations(arg, mu, times)
    seqs = argweaver.make_alignment(arg, muts)

    new_name = "n%d" % (k - 1)
    arg = argweaver.remove_arg_thread(arg, new_name)

    trees, names = argweaverc.arg2ctrees(arg, times)
    seqs2, nseqs, seqlen = argweaverc.seqs2cseqs(seqs, names + [new_name])

    assert argweaverc.argweaver_assert_emit(trees, len(times), times, mu,
                                            seqs2, nseqs, seqlen)
Esempio n. 4
0
def test_node_numbering():
    """
    Test node numbering across ARG.

    A node should keep the same numbering until it is broken by
    recombination. The new recoal node should take the index of the broken
    node.
    """

    k = 10
    n = 1e4
    rho = 1.5e-8 * 20
    length = 10000
    times = argweaver.get_time_points(ntimes=20, maxtime=200000)

    arg = argweaver.sample_arg_dsmc(k, 2*n, rho, start=0, end=length,
                                    times=times)

    (ptrees, ages, sprs, blocks), all_nodes = (
        argweaverc.get_treeset(arg, times))

    # check nodes list
    nnodes = len(all_nodes[0])
    last_nodes = None
    for i, nodes in enumerate(all_nodes):
        if last_nodes:
            recombj = sprs[i][0]
            brokenj = ptrees[i][recombj]
            for j in range(nnodes):
                if j != brokenj:
                    nose.tools.assert_equal(last_nodes[j], nodes[j])

        last_nodes = nodes
Esempio n. 5
0
def test_emit():
    """
    Calculate emission probabilities
    """

    k = 10
    n = 1e4
    rho = 1.5e-8 * 20
    mu = 2.5e-8 * 20
    length = int(1e3) / 20
    times = argweaver.get_time_points(ntimes=20, maxtime=200000)

    arg = argweaver.sample_arg_dsmc(k, 2*n, rho, start=0, end=length,
                                    times=times)

    muts = argweaver.sample_arg_mutations(arg, mu, times)
    seqs = argweaver.make_alignment(arg, muts)

    new_name = "n%d" % (k-1)
    arg = argweaver.remove_arg_thread(arg, new_name)

    trees, names = argweaverc.arg2ctrees(arg, times)
    seqs2, nseqs, seqlen = argweaverc.seqs2cseqs(seqs, names + [new_name])

    assert argweaverc.argweaver_assert_emit(trees, len(times), times, mu,
                                            seqs2, nseqs, seqlen)
Esempio n. 6
0
def test_node_numbering():
    """
    Test node numbering across ARG.

    A node should keep the same numbering until it is broken by
    recombination. The new recoal node should take the index of the broken
    node.
    """

    k = 10
    n = 1e4
    rho = 1.5e-8 * 20
    length = 10000
    times = argweaver.get_time_points(ntimes=20, maxtime=200000)

    arg = argweaver.sample_arg_dsmc(k, 2*n, rho, start=0, end=length,
                                    times=times)

    (ptrees, ages, sprs, blocks), all_nodes = (
        argweaverc.get_treeset(arg, times))

    # check nodes list
    nnodes = len(all_nodes[0])
    last_nodes = None
    for i, nodes in enumerate(all_nodes):
        if last_nodes:
            recombj = sprs[i][0]
            brokenj = ptrees[i][recombj]
            for j in range(nnodes):
                if j != brokenj:
                    nose.tools.assert_equal(last_nodes[j], nodes[j])

        last_nodes = nodes
Esempio n. 7
0
def test_trans_switch():
    """
    Calculate transition probabilities for switch matrix

    Only calculate a single matrix
    """

    k = 12
    n = 1e4
    rho = 1.5e-8 * 20
    length = 1000
    times = argweaver.get_time_points(ntimes=20, maxtime=200000)
    popsizes = [n] * len(times)

    recombs = []

    while len(recombs) == 0:
        arg = argweaver.sample_arg_dsmc(k, 2*n, rho, start=0, end=length,
                                        times=times)
        recombs = [x.pos for x in arg if x.event == "recomb"]

    pos = recombs[0]
    tree = arg.get_marginal_tree(pos-.5)
    rpos, r, c = arglib.iter_arg_sprs(arg, start=pos-.5).next()
    spr = (r, c)

    assert argweaverc.assert_transition_switch_probs(
        tree, spr, times, popsizes, rho)
Esempio n. 8
0
def test_trans_switch():
    """
    Calculate transition probabilities for switch matrix

    Only calculate a single matrix
    """
    create_data = False
    if create_data:
        make_clean_dir('test/data/test_trans_switch')

    # model parameters
    k = 12
    n = 1e4
    rho = 1.5e-8 * 20
    length = 1000
    times = argweaver.get_time_points(ntimes=20, maxtime=200000)
    popsizes = [n] * len(times)
    ntests = 100

    # generate test data
    if create_data:
        for i in range(ntests):
            # Sample ARG with at least one recombination.
            while True:
                arg = argweaver.sample_arg_dsmc(k,
                                                2 * n,
                                                rho,
                                                start=0,
                                                end=length,
                                                times=times)
                if any(x.event == "recomb" for x in arg):
                    break
            arg.write('test/data/test_trans_switch/%d.arg' % i)

    for i in range(ntests):
        print('arg', i)
        arg = arglib.read_arg('test/data/test_trans_switch/%d.arg' % i)
        argweaver.discretize_arg(arg, times)
        recombs = [x.pos for x in arg if x.event == "recomb"]
        pos = recombs[0]
        tree = arg.get_marginal_tree(pos - .5)
        rpos, r, c = next(arglib.iter_arg_sprs(arg, start=pos - .5))
        spr = (r, c)

        if not argweaverc.assert_transition_switch_probs(
                tree, spr, times, popsizes, rho):
            tree2 = tree.get_tree()
            treelib.remove_single_children(tree2)
            treelib.draw_tree_names(tree2, maxlen=5, minlen=5)
            assert False
Esempio n. 9
0
def test_trans_switch():
    """
    Calculate transition probabilities for switch matrix

    Only calculate a single matrix
    """
    create_data = False
    if create_data:
        make_clean_dir('test/data/test_trans_switch')

    # model parameters
    k = 12
    n = 1e4
    rho = 1.5e-8 * 20
    length = 1000
    times = argweaver.get_time_points(ntimes=20, maxtime=200000)
    popsizes = [n] * len(times)
    ntests = 100

    # generate test data
    if create_data:
        for i in range(ntests):
            # Sample ARG with at least one recombination.
            while True:
                arg = argweaver.sample_arg_dsmc(
                    k, 2*n, rho, start=0, end=length, times=times)
                if any(x.event == "recomb" for x in arg):
                    break
            arg.write('test/data/test_trans_switch/%d.arg' % i)

    for i in range(ntests):
        print 'arg', i
        arg = arglib.read_arg('test/data/test_trans_switch/%d.arg' % i)
        argweaver.discretize_arg(arg, times)
        recombs = [x.pos for x in arg if x.event == "recomb"]
        pos = recombs[0]
        tree = arg.get_marginal_tree(pos-.5)
        rpos, r, c = arglib.iter_arg_sprs(arg, start=pos-.5).next()
        spr = (r, c)

        if not argweaverc.assert_transition_switch_probs(
                tree, spr, times, popsizes, rho):
            tree2 = tree.get_tree()
            treelib.remove_single_children(tree2)
            treelib.draw_tree_names(tree2, maxlen=5, minlen=5)
            assert False
Esempio n. 10
0
def test_arg_convert():
    """
    Test conversion for python to C args
    """

    k = 10
    n = 1e4
    rho = 1.5e-8 * 20
    length = 10000
    times = argweaver.get_time_points(ntimes=20, maxtime=200000)

    arg = argweaver.sample_arg_dsmc(k, 2 * n, rho, start=0, end=length, times=times)

    # convert to C++ and back
    trees, names = argweaverc.arg2ctrees(arg, times)
    arg2 = argweaverc.ctrees2arg(trees, names, times)

    arg_equal(arg, arg2)
Esempio n. 11
0
def test_arg_joint():
    """
    Compute joint probability of an ARG
    """

    k = 2
    n = 1e4
    rho = 1.5e-8 * 20
    mu = 2.5e-8 * 20
    length = 10000
    times = argweaver.get_time_points(ntimes=20, maxtime=200000)

    arg = argweaver.sample_arg_dsmc(k, 2*n, rho, start=0, end=length,
                                    times=times)
    muts = argweaver.sample_arg_mutations(arg, mu, times=times)
    seqs = arglib.make_alignment(arg, muts)

    lk = argweaver.calc_joint_prob(arg, seqs, mu=mu, rho=rho, times=times)
    print lk
Esempio n. 12
0
def test_arg_convert():
    """
    Test conversion for python to C args
    """

    k = 10
    n = 1e4
    rho = 1.5e-8 * 20
    length = 10000
    times = argweaver.get_time_points(ntimes=20, maxtime=200000)

    arg = argweaver.sample_arg_dsmc(k, 2*n, rho, start=0, end=length,
                                    times=times)

    # convert to C++ and back
    trees, names = argweaverc.arg2ctrees(arg, times)
    arg2 = argweaverc.ctrees2arg(trees, names, times)

    arg_equal(arg, arg2)
Esempio n. 13
0
def test_trans_switch_internal():
    """
    Calculate transition probabilities for switch matrix and internal branches

    Only calculate a single matrix
    """

    k = 10
    n = 1e4
    rho = 1.5e-8 * 20
    length = int(100e3) / 20
    times = argweaver.get_time_points(ntimes=20, maxtime=200000)
    popsizes = [n] * len(times)

    arg = argweaver.sample_arg_dsmc(k, 2*n, rho, start=0, end=length,
                                    times=times)
    trees, names = argweaverc.arg2ctrees(arg, times)

    assert argweaverc.assert_transition_probs_switch_internal(
        trees, times, popsizes, rho)
Esempio n. 14
0
def test_emit_internal():
    """
    Calculate emission probabilities for internal branches
    """

    k = 10
    n = 1e4
    rho = 1.5e-8 * 20
    mu = 2.5e-8 * 20
    length = int(10e3) / 20
    times = argweaver.get_time_points(ntimes=20, maxtime=200000)

    arg = argweaver.sample_arg_dsmc(k, 2*n, rho, start=0, end=length,
                                    times=times)

    muts = argweaver.sample_arg_mutations(arg, mu, times)
    seqs = argweaver.make_alignment(arg, muts)

    trees, names = argweaverc.arg2ctrees(arg, times)
    seqs2, nseqs, seqlen = argweaverc.seqs2cseqs(seqs, names)

    assert argweaverc.argweaver_assert_emit_internal(
        trees, len(times), times, mu, seqs2, nseqs, seqlen)