This notebook counts the number of evolutions and reversions in a binary trait on a large phylogenetic tree.
Import the dependencies - Dendropy and Pandas
In [1]:
import dendropy
import pandas as pd
Read data and tree.
In [2]:
data = pd.read_csv('../Data/PyronParityData.csv', index_col=0, header=False)
In [12]:
taxa = dendropy.TaxonSet()
mle = dendropy.Tree.get_from_path('../TotalOpt/annotatedTO_0param_2598364.dated', 'newick', taxon_set=taxa, preserve_underscores=True)
Iterate over the tips of the trees and annotate with data (in this case, whether the tip is viviparous or oviparous). T
In [13]:
for idx, nd in enumerate(mle.postorder_node_iter()):
if nd.label is None:
lookup = '{}'.format(nd.taxon)
nd.label = int(data.ix[lookup])
else:
pass
The counting loop. If we have a tip that has data, append to either the oviparous or viviparous list, as apporpriate. If the node label is annotated as having over a 50% probability of being oviparous, add to the oviparous list. If the node is likely to be viviparous, add to the viviparous list.
In [14]:
putative_c = []
putative_co = []
total = []
childs = []
for index, node in enumerate(mle.postorder_node_iter()):
total.append(index)
if node.parent_node is None:
pass
elif .5 < float(node.label) < 1 or float(node.label) == 0: #Is likely oviparous
if float(node.parent_node.label) < .05 : #List of nodes that demonstrate change away from oviparity.
if node.taxon is not None :
putative_co.append([node.parent_node.label, node.taxon])
else:
putative_co.append(node.parent_node.label)
for nd in node.child_nodes():
# print nd.taxon
pass
elif 0 < float(node.label) < .95 or float(node.label) == 1:
if float(node.parent_node.label) > .05:
putative_c.append([node.parent_node.label,node.taxon])
print len(putative_c), 'changes to viviparity'
print len(putative_co), 'reversions to oviparity'
Copyright (c) <2014> <April Wright, wright.aprilm@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
In [ ]: