| 1 |
""" |
| 2 |
Author: Curtis Huttenhower |
| 3 |
Description: Used to create tree structures to hierarchically normalize abundance tables. |
| 4 |
""" |
| 5 |
|
| 6 |
##################################################################################### |
| 7 |
#Copyright (C) <2012> |
| 8 |
# |
| 9 |
#Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 10 |
#this software and associated documentation files (the "Software"), to deal in the |
| 11 |
#Software without restriction, including without limitation the rights to use, copy, |
| 12 |
#modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, |
| 13 |
#and to permit persons to whom the Software is furnished to do so, subject to |
| 14 |
#the following conditions: |
| 15 |
# |
| 16 |
#The above copyright notice and this permission notice shall be included in all copies |
| 17 |
#or substantial portions of the Software. |
| 18 |
# |
| 19 |
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 20 |
#INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
| 21 |
#PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 22 |
#HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 23 |
#OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 24 |
#SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 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 |
#If values do not exist |
| 81 |
if not self.m_adValues: |
| 82 |
#Call impute on all children |
| 83 |
#If the parent clade has no abundance values |
| 84 |
#Then take a copy of the child's |
| 85 |
#If they now have a copy of a child's but have other children |
| 86 |
#Sum their children with thier current values |
| 87 |
for pChild in 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 |
#If values exist return |
| 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 |
#fHit is true on atleast one of the following conditions: |
| 107 |
#iTarget is not 0 indicating no changes |
| 108 |
#Leaves are indicated to be only given and the target depth for the leaves is reached. |
| 109 |
#The required depth is reached. |
| 110 |
fHit = ( not iTarget ) or ( ( fLeaves and ( iDepth == iTarget ) ) or ( ( not fLeaves ) and ( iDepth <= iTarget ) ) ) |
| 111 |
#Increment depth |
| 112 |
iDepth += 1 |
| 113 |
#Returns a set |
| 114 |
setiRet = set() |
| 115 |
#If there are children build integer set indicating structure of the tree preserved in the dict |
| 116 |
if self.m_hashChildren: |
| 117 |
#Union all the results from freeze of all children |
| 118 |
#Call freeze but append the child clade to the clade in the call. |
| 119 |
#And give an incremented depth |
| 120 |
for strChild, pChild in 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 |
#Indicate if the correct level is reached |
| 126 |
if iTarget < 0: |
| 127 |
if fLeaves: |
| 128 |
fHit = -( iTarget + 1 ) in setiRet |
| 129 |
else: |
| 130 |
fHit = -( iTarget + 1 ) <= max( setiRet ) |
| 131 |
#if astrClade is not == [] (so you are actually in a clade of the tree) |
| 132 |
#And the clade has values (should be true, if not impute should have been callded before running this method) |
| 133 |
#And we are at the correct level of the tree then |
| 134 |
#Add to the dict the clade and the abundance values |
| 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 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 |
#""" |