File: curated_test_tree_list.py

package info (click to toggle)
python-dendropy 4.2.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 68,392 kB
  • ctags: 3,947
  • sloc: python: 41,840; xml: 1,400; makefile: 15
file content (118 lines) | stat: -rw-r--r-- 3,859 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# !/usr/bin/env python

##############################################################################
##  DendroPy Phylogenetic Computing Library.
##
##  Copyright 2010-2015 Jeet Sukumaran and Mark T. Holder.
##  All rights reserved.
##
##  See "LICENSE.rst" for terms and conditions of usage.
##
##  If you use this work or any portion thereof in published work,
##  please cite it as:
##
##     Sukumaran, J. and M. T. Holder. 2010. DendroPy: a Python library
##     for phylogenetic computing. Bioinformatics 26: 1569-1571.
##
##############################################################################

"""
Generates lists of trees and dendropy.TreeList for tests.
"""

import dendropy

DEFAULT_NUM_TREES = 5
_TREE_COUNTER = 0

def get_tree(taxon_namespace=None,
        label=None,
        suppress_internal_node_taxa=False,
        suppress_leaf_node_taxa=False):
    global _TREE_COUNTER
    _TREE_COUNTER += 1
    if taxon_namespace is None:
        taxon_namespace = dendropy.TaxonNamespace()
    if label is None:
        label = "Tree{}".format(_TREE_COUNTER)
    t1 = dendropy.Tree(label=label,
            taxon_namespace=taxon_namespace)
    t1.seed_node.label = "i0"
    c1 = t1.seed_node.new_child(label="i1")
    c2 = t1.seed_node.new_child(label="i2")
    c1.new_child(label="t1")
    c1.new_child(label="t2")
    c2.new_child(label="t3")
    c2.new_child(label="t4")
    tax_labels = set()
    for nd in t1:
        is_leaf = nd.is_leaf()
        if is_leaf and not suppress_leaf_node_taxa:
            tax1 = t1.taxon_namespace.require_taxon(nd.label)
            nd.taxon = tax1
            tax_labels.add(nd.label)
        elif (not is_leaf) and not suppress_internal_node_taxa:
            tax1 = t1.taxon_namespace.require_taxon(nd.label)
            nd.taxon = tax1
            tax_labels.add(nd.label)
    t1.tax_labels = tax_labels
    try:
        t1.taxon_namespace.tax_labels.update(tax_labels)
    except AttributeError:
        t1.taxon_namespace.tax_labels = set(tax_labels)
    return t1

def get_trees(
        num_trees,
        taxon_namespace=None,
        label=None,
        suppress_internal_node_taxa=False,
        suppress_leaf_node_taxa=False):
    trees = []
    for idx in range(num_trees):
        t1 = get_tree(
                taxon_namespace=taxon_namespace,
                label=label,
                suppress_internal_node_taxa=suppress_internal_node_taxa,
                suppress_leaf_node_taxa=suppress_leaf_node_taxa)
        trees.append(t1)
    return trees

def get_tree_list(
        num_trees,
        taxon_namespace=None,
        label=None,
        suppress_internal_node_taxa=False,
        suppress_leaf_node_taxa=False):
    if taxon_namespace is None:
        taxon_namespace = dendropy.TaxonNamespace()
    tlist1 = dendropy.TreeList(label="1",
            taxon_namespace=taxon_namespace)
    for idx in range(num_trees):
        t1 = get_tree(
                taxon_namespace=taxon_namespace,
                label=label,
                suppress_internal_node_taxa=suppress_internal_node_taxa,
                suppress_leaf_node_taxa=suppress_leaf_node_taxa)
        assert t1.taxon_namespace is tlist1.taxon_namespace
        tlist1.append(t1)
    return tlist1

def get_tree_list_and_list_of_trees(
        num_trees,
        tree_list_taxon_namespace=None,
        list_of_trees_taxon_namespace=None):
    tlist = get_tree_list(
            num_trees=0,
            taxon_namespace=tree_list_taxon_namespace,
            label=None,
            suppress_internal_node_taxa=False,
            suppress_leaf_node_taxa=False)
    trees = get_trees(
            num_trees=num_trees,
            taxon_namespace=list_of_trees_taxon_namespace,
            label=None,
            suppress_internal_node_taxa=False,
            suppress_leaf_node_taxa=False)
    return tlist, trees