| 1 |
""" |
| 2 |
Author: Curtis Huttenhower |
| 3 |
Description: Used to create tree structures to hierarchically normalize abundance tables. |
| 4 |
""" |
| 5 |
|
| 6 |
|
| •7 |
|
| 8 |
|
| •9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| •16 |
|
| 17 |
|
| 18 |
|
| •19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
__author__ = "Curtis Huttenhower" |
| 28 |
__copyright__ = "Copyright 2012" |
| 29 |
__credits__ = ["Curtis Huttenhower"] |
| 30 |
__license__ = "MIT" |
| 31 |
__maintainer__ = "Timothy Tickle" |
| 32 |
__email__ = "[email protected]" |
| 33 |
__status__ = "Development" |
| 34 |
|
| 35 |
import sys |
| 36 |
|
| 37 |
class CClade: |
| 38 |
|
| 39 |
def __init__( self ): |
| 40 |
""" |
| 41 |
Initialize CClade |
| 42 |
Dictionary to hold the children nodes from feature consensus lineages. |
| 43 |
adValues is a list of the abundance value. |
| 44 |
""" |
| 45 |
|
| 46 |
self.m_hashChildren = {} |
| 47 |
self.m_adValues = None |
| 48 |
|
| 49 |
def get( self, astrClade ): |
| 50 |
""" |
| 51 |
Recursively travel the length of a tree until you find the terminal node |
| 52 |
(where astrClade == Falseor actually []) |
| 53 |
or a dict key that matches the clade call. |
| 54 |
If at any time a clade is given that is not currently know, return a new clade |
| 55 |
which is set to the current Clade as a child. |
| 56 |
""" |
| 57 |
|
| 58 |
return self.m_hashChildren.setdefault( |
| 59 |
astrClade[0], CClade( ) ).get( astrClade[1:] ) if astrClade else self |
| 60 |
|
| 61 |
def set( self, adValues ): |
| 62 |
""" |
| 63 |
Set all the values given as a list in the same order given. |
| 64 |
""" |
| 65 |
|
| 66 |
self.m_adValues = [0] * len( adValues ) |
| 67 |
for i, d in enumerate( adValues ): |
| 68 |
if d: |
| 69 |
self.m_adValues[i] = d |
| 70 |
|
| 71 |
def impute( self ): |
| 72 |
""" |
| 73 |
This allows you to recursively impute values for clades without values given their children counts. |
| 74 |
Assumably this should be called only once and after all clade abundances have been added. |
| 75 |
If the m_adValues already exist return the stored m_adValues. (No imputation needed). |
| 76 |
Otherwise call impute for all children and take the sum of the values from all the children by column |
| 77 |
Not a sum of a list but summing a list with lists by element. |
| 78 |
""" |
| 79 |
|
| 80 |
|
| 81 |
if not self.m_adValues: |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| •87 |
for pChild in list(self.m_hashChildren.values( )): |
| 88 |
adChild = pChild.impute( ) |
| 89 |
if self.m_adValues: |
| 90 |
for i in range( len( adChild or [] ) ): |
| 91 |
if adChild[i]: |
| 92 |
self.m_adValues[i] += adChild[i] |
| 93 |
elif adChild: |
| 94 |
self.m_adValues = adChild[:] |
| 95 |
|
| 96 |
return self.m_adValues |
| 97 |
|
| 98 |
def _freeze( self, hashValues, iTarget, astrClade, iDepth, fLeaves ): |
| 99 |
""" |
| 100 |
Update the given hashValues dict with clade abundances given depth specifications |
| 101 |
Return a set of integers returning an indicator of the structure of the tree preserved in the dict/hash |
| 102 |
When the appropriate level of the tree is reached |
| 103 |
Hashvalue is updated with the clade (including lineage) and the abundance. looks like {"clade":blist(["0.0","0.1"...])} |
| 104 |
""" |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
fHit = ( not iTarget ) or ( ( fLeaves and ( iDepth == iTarget ) ) or ( ( not fLeaves ) and ( iDepth <= iTarget ) ) ) |
| 111 |
|
| 112 |
iDepth += 1 |
| 113 |
|
| 114 |
setiRet = set() |
| 115 |
|
| 116 |
if self.m_hashChildren: |
| 117 |
|
| 118 |
|
| 119 |
|
| •120 |
for strChild, pChild in list(self.m_hashChildren.items( )): |
| 121 |
setiRet |= pChild._freeze( hashValues, iTarget, astrClade + [strChild], iDepth, fLeaves ) |
| 122 |
setiRet = set( ( i + 1 ) for i in setiRet ) |
| 123 |
else: |
| 124 |
setiRet.add( 0 ) |
| 125 |
|
| 126 |
if iTarget < 0: |
| 127 |
if fLeaves: |
| 128 |
fHit = -( iTarget + 1 ) in setiRet |
| 129 |
else: |
| 130 |
fHit = -( iTarget + 1 ) <= max( setiRet ) |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
if astrClade and self.m_adValues and fHit: |
| 136 |
hashValues["|".join( astrClade )] = self.m_adValues |
| 137 |
return setiRet |
| 138 |
|
| 139 |
def freeze( self, hashValues, iTarget, fLeaves ): |
| 140 |
""" |
| 141 |
Call helper function setting the clade and depth to defaults (start positions) |
| 142 |
The important result of this method is hashValues is updated with clade and abundance information |
| 143 |
""" |
| 144 |
self._freeze( hashValues, iTarget, [], 0, fLeaves ) |
| 145 |
|
| 146 |
def _repr( self, strClade ): |
| 147 |
""" |
| 148 |
Represent tree clade for debugging. Helper function for recursive repr. |
| 149 |
""" |
| 150 |
|
| 151 |
strRet = "<" |
| 152 |
if strClade: |
| 153 |
strRet += "%s %s" % (strClade, self.m_adValues) |
| 154 |
if self.m_hashChildren: |
| 155 |
strRet += " " |
| 156 |
if self.m_hashChildren: |
| •157 |
strRet += " ".join( p._repr( s ) for (s, p) in list(self.m_hashChildren.items( )) ) |
| 158 |
|
| 159 |
return ( strRet + ">" ) |
| 160 |
|
| 161 |
def __repr__( self ): |
| 162 |
""" |
| 163 |
Represent tree clade for debugging. |
| 164 |
""" |
| 165 |
return self._repr( "" ) |
| 166 |
|
| 167 |
""" |
| 168 |
pTree = CClade( ) |
| 169 |
pTree.get( ("A", "B") ).set( [1, 2, 3] ) |
| 170 |
pTree.get( ("A", "C") ).set( [4, 5, 6] ) |
| 171 |
pTree.get( ("D", "E") ).set( [7, 8, 9] ) |
| 172 |
iTaxa = 0 |
| 173 |
if iTaxa: |
| 174 |
pTree.impute( ) |
| 175 |
hashFeatures = {} |
| 176 |
pTree.freeze( hashFeatures, iTaxa ) |
| 177 |
print( pTree ) |
| 178 |
print( hashFeatures ) |
| 179 |
sys.exit( 0 ) |
| 180 |
#""" |