-
Notifications
You must be signed in to change notification settings - Fork 27
/
balances.py
164 lines (136 loc) · 4.78 KB
/
balances.py
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
"""
Balances (:mod:`gneiss.balances`)
=================================
.. currentmodule:: gneiss.balances
This module contains modules for calculating balances and creating ETE
objects to visualize these balances on a tree.
Functions
---------
.. autosummary::
:toctree: generated/
balance_basis
"""
# ----------------------------------------------------------------------------
# Copyright (c) 2016--, gneiss development team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
# ----------------------------------------------------------------------------
from __future__ import division
import numpy as np
from skbio.stats.composition import clr_inv
from collections import OrderedDict
def _balance_basis(tree_node):
""" Helper method for calculating balance basis
"""
counts, n_tips = _count_matrix(tree_node)
counts = OrderedDict([(x, counts[x])
for x in counts.keys() if not x.is_tip()])
nds = counts.keys()
r = np.array([counts[n]['r'] for n in nds])
s = np.array([counts[n]['l'] for n in nds])
k = np.array([counts[n]['k'] for n in nds])
t = np.array([counts[n]['t'] for n in nds])
a = np.sqrt(s / (r*(r+s)))
b = -1*np.sqrt(r / (s*(r+s)))
basis = np.zeros((n_tips-1, n_tips))
for i in range(len(nds)):
basis[i, :] = np.array([0]*k[i] + [a[i]]*r[i] + [b[i]]*s[i] + [0]*t[i])
# Make sure that the basis is in level order
basis = basis[:, ::-1]
nds = list(nds)
return basis, nds
def balance_basis(tree_node):
"""
Determines the basis based on bifurcating tree.
This is commonly referred to as sequential binary partition [1]_.
Given a binary tree relating a list of features, this module can
be used to calculate an orthonormal basis, which is used to
calculate the ilr transform.
Parameters
----------
treenode : skbio.TreeNode
Input bifurcating tree. Must be strictly bifurcating
(i.e. every internal node needs to have exactly 2 children).
Returns
-------
basis : np.array
Returns a set of orthonormal bases in the Aitchison simplex
corresponding to the tree. The order of the
basis is index by the level order of the internal nodes.
nodes : list, skbio.TreeNode
List of tree nodes indicating the ordering in the basis.
Raises
------
ValueError
The tree doesn't contain two branches.
Examples
--------
>>> from gneiss.balances import balance_basis
>>> from skbio import TreeNode
>>> tree = u"((b,c)a, d)root;"
>>> t = TreeNode.read([tree])
>>> basis, nodes = balance_basis(t)
>>> basis
array([[ 0.18507216, 0.18507216, 0.62985567],
[ 0.14002925, 0.57597535, 0.28399541]])
Notes
-----
The tree must be strictly bifurcating, meaning that
every internal node has exactly 2 children.
See Also
--------
skbio.stats.composition.ilr
References
----------
.. [1] J.J. Egozcue and V. Pawlowsky-Glahn "Exploring Compositional Data
with the CoDa-Dendrogram" (2011)
"""
basis, nodes = _balance_basis(tree_node)
basis = clr_inv(basis)
return basis, nodes
def _count_matrix(treenode):
n_tips = 0
nodes = list(treenode.levelorder(include_self=True))
# fill in the Ordered dictionary. Note that the
# elements of this Ordered dictionary are
# dictionaries.
counts = OrderedDict()
columns = ['k', 'r', 'l', 't', 'tips']
for n in nodes:
if n not in counts:
counts[n] = {}
for c in columns:
counts[n][c] = 0
# fill in r and l. This is done in reverse level order.
for n in nodes[::-1]:
if n.is_tip():
counts[n]['tips'] = 1
n_tips += 1
elif len(n.children) == 2:
lchild = n.children[0]
rchild = n.children[1]
counts[n]['r'] = counts[rchild]['tips']
counts[n]['l'] = counts[lchild]['tips']
counts[n]['tips'] = counts[n]['r'] + counts[n]['l']
else:
raise ValueError("Not a strictly bifurcating tree!")
# fill in k and t
for n in nodes:
if n.parent is None:
counts[n]['k'] = 0
counts[n]['t'] = 0
continue
elif n.is_tip():
continue
# left or right child
# left = 0, right = 1
child_idx = 'l' if n.parent.children[0] != n else 'r'
if child_idx == 'l':
counts[n]['t'] = counts[n.parent]['t'] + counts[n.parent]['l']
counts[n]['k'] = counts[n.parent]['k']
else:
counts[n]['k'] = counts[n.parent]['k'] + counts[n.parent]['r']
counts[n]['t'] = counts[n.parent]['t']
return counts, n_tips