def testSimple1(self):

        t = dendropy.Tree.get_from_string("((((a:1, b:1):1, c:2):1, d:3, e:3):2, (f:4, g:4):1)", "newick")
        i1 = coalescent.node_waiting_time_pairs(t)
        assert [x[1] for x in i1] == [1.0, 1.0, 1.0, 1.0, 1.0]
        i2 = coalescent.extract_coalescent_frames(t)
        assert i2 == {7: 1.0, 6:1.0, 5:1.0, 3:1.0, 2:1.0}
        check = coalescent.log_probability_of_coalescent_tree(t, 10)
예제 #2
0
    def testSimple1(self):

        t = dendropy.Tree.get_from_string(
            "((((a:1, b:1):1, c:2):1, d:3, e:3):2, (f:4, g:4):1)", "newick")
        i1 = coalescent.node_waiting_time_pairs(t)
        assert [x[1] for x in i1] == [1.0, 1.0, 1.0, 1.0, 1.0]
        i2 = coalescent.extract_coalescent_frames(t)
        assert i2 == {7: 1.0, 6: 1.0, 5: 1.0, 3: 1.0, 2: 1.0}
        check = coalescent.log_probability_of_coalescent_tree(t, 10)
예제 #3
0
def main():
    """
    Main CLI handler.
    """

    parser = argparse.ArgumentParser()
    parser.add_argument("-f",
                        "--format",
                        dest="schema",
                        type=str,
                        default="nexus",
                        choices=["nexus", "newick"],
                        help="input data format (default='%(default)s')")
    parser.add_argument("-l",
                        "--label",
                        type=str,
                        default="[TESTING]",
                        help="label for test")
    parser.add_argument(
        "-r",
        "--regime",
        dest="regime",
        type=str,
        default="mean",
        choices=[
            "mean-coalescent", "converse-coalescent", "uniform-coalescent"
        ],
        help=
        "expected regime under which the coalescent intervals were generated (default='%(default)s')"
    )
    parser.add_argument("-p",
                        "--precision",
                        type=float,
                        default=0.01,
                        help="numerical precision (default=%(default)s)")
    args = parser.parse_args()

    fails = 0
    trees = dendropy.TreeList.get_from_stream(sys.stdin, schema=args.schema)
    for tidx, tree in enumerate(trees):

        tree_fails = 0

        # check structure
        if not tree._debug_tree_is_valid():
            sys.stderr.write("{} Tree {}: structure is invalid".format(
                args.label, tidx + 1))
            tree_fails += 1

        # ensure parent ages > child ages
        tree.calc_node_ages(check_prec=args.precision)
        for nd in tree.postorder_node_iter():
            if nd.parent_node is not None:
                if nd.age > nd.parent_node.age:
                    sys.stderr.write(
                        "{} Tree {}: Node '{}': age ({}) is greater than parent age ({})"
                        .format(args.label, tidx + 1, nd.label, nd.age,
                                nd.parent_node.age))
                    tree_fails += 1

        # check waiting times
        num_tips = len(tree.leaf_nodes())
        wf = coalescent.extract_coalescent_frames(
            tree, check_ultrametricity_prec=args.precision)
        num_lineages = sorted(wf.keys())
        for n in num_lineages:
            wt = wf[n]
            if args.regime == "mean-coalescent":
                exp_wt = coalescent.expected_tmrca(n)
            elif args.regime == "converse-coalescent":
                exp_wt = coalescent.expected_tmrca(num_tips - n + 2)
            elif args.regime == "uniform-coalescent":
                exp_wt = 1.0
            else:
                raise RuntimeError("Unsupported regime: '{}'".format(
                    args.regime))
            if abs(exp_wt - wt) > args.precision:
                sys.stderr.write(
                    "{} Tree {}: Waiting time for coalescence event with {} lineages: expecting {} but found {}\n"
                    .format(args.label, tidx + 1, n, exp_wt, wt))
                tree_fails += 1
        if tree_fails > 0:
            sys.stderr.write("{} Tree {} failed: {}\n".format(
                args.label, tidx + 1, tree.compose_newick()))
        fails += tree_fails
    if fails > 0:
        sys.exit(1)
    else:
        sys.exit(0)
def main():
    """
    Main CLI handler.
    """

    parser = argparse.ArgumentParser()
    parser.add_argument("-f", "--format",
            dest="schema",
            type=str,
            default="nexus",
            choices=["nexus", "newick"],
            help="input data format (default='%(default)s')")
    parser.add_argument("-l", "--label",
            type=str,
            default="[TESTING]",
            help="label for test")
    parser.add_argument("-r", "--regime",
            dest="regime",
            type=str,
            default="mean",
            choices=["mean-coalescent", "converse-coalescent", "uniform-coalescent"],
            help="expected regime under which the coalescent intervals were generated (default='%(default)s')")
    parser.add_argument("-p", "--precision",
            type=float,
            default=0.01,
            help="numerical precision (default=%(default)s)")
    args = parser.parse_args()

    fails = 0
    trees = dendropy.TreeList.get_from_stream(sys.stdin, schema=args.schema)
    for tidx, tree in enumerate(trees):

        tree_fails = 0

        # check structure
        if not tree._debug_tree_is_valid():
            sys.stderr.write("{} Tree {}: structure is invalid".format(args.label, tidx+1))
            tree_fails += 1

        # ensure parent ages > child ages
        tree.calc_node_ages(check_prec=args.precision)
        for nd in tree.postorder_node_iter():
            if nd.parent_node is not None:
                if nd.age > nd.parent_node.age:
                    sys.stderr.write("{} Tree {}: Node '{}': age ({}) is greater than parent age ({})".format(
                        args.label, tidx+1, nd.label, nd.age, nd.parent_node.age))
                    tree_fails += 1

        # check waiting times
        num_tips = len(tree.leaf_nodes())
        wf = coalescent.extract_coalescent_frames(tree, check_ultrametricity_prec=args.precision)
        num_lineages = sorted(wf.keys())
        for n in num_lineages:
            wt = wf[n]
            if args.regime == "mean-coalescent":
                exp_wt = coalescent.expected_tmrca(n)
            elif args.regime == "converse-coalescent":
                exp_wt = coalescent.expected_tmrca(num_tips - n + 2)
            elif args.regime == "uniform-coalescent":
                exp_wt = 1.0
            else:
                raise RuntimeError("Unsupported regime: '{}'".format(args.regime))
            if abs(exp_wt - wt) > args.precision:
                sys.stderr.write("{} Tree {}: Waiting time for coalescence event with {} lineages: expecting {} but found {}\n".format(
                        args.label, tidx+1, n, exp_wt, wt))
                tree_fails += 1
        if tree_fails > 0:
            sys.stderr.write("{} Tree {} failed: {}\n".format(args.label, tidx+1, tree.compose_newick()))
        fails += tree_fails
    if fails > 0:
        sys.exit(1)
    else:
        sys.exit(0)