In [2]:
import dendropy
import pandas as pd

Import tree with node labels. Because we'll use a pandas dataframe to associate the tip data with the tree, we want to preserve the underscores, assuming they exist in your data file of tip states. If not, set to false.


In [1]:
taxa = dendropy.TaxonSet()
mle = dendropy.Tree.get_from_path('vec_tree', 'newick', taxon_set=taxa, preserve_underscores=True)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-4fbee3a8739d> in <module>()
----> 1 taxa = dendropy.TaxonSet()
      2 mle = dendropy.Tree.get_from_path('vec_tree', 'newick', taxon_set=taxa, preserve_underscores=True)

NameError: name 'dendropy' is not defined

Import data. CSV with one column of taxon labels, one of tip states.


In [4]:
data = pd.read_csv('PyronParityData.csv', index_col=0)

Iterate over nodes, associating them with the tip states in the pandas dataframe.


In [5]:
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

Associate value ranges with colors.


In [6]:
for nd in mle.postorder_node_iter():    
    if float(nd.label) == 0:
       nd.annotations.add_new(name = '!color', value = '#0000FF') #Dark blue, zeroes
    elif float(nd.label) > 0.00000000000001 and float(nd.label) < 0.16:
        nd.annotations.add_new(name = '!color', value = 'E50000') #Red, strongly viviparous        
    elif float(nd.label) > 0.17 and float(nd.label) < 0.41:
        nd.annotations.add_new(name = '!color', value = '##ffa500')#orangey-red, ambiguous viviparous     
    elif float(nd.label) > 0.42 and float(nd.label) < 0.67:
        nd.annotations.add_new(name = '!color', value = '#00ff00') #green, equivocal
    elif float(nd.label) > 0.66 and float(nd.label) < 0.85:
        nd.annotations.add_new(name = '!color', value = '#4DBD33') #darker green, ambiguous oviparous        
    elif float(nd.label) > .85 and float(nd.label) < 0.99999999999999:
        nd.annotations.add_new(name = '!color', value = '#0000FF') #Blue, strongly oviparous 
    elif float(nd.label) == 1.0:
         nd.annotations.add_new(name = '!color', value = '#ff0000') #Red, viviparous

In [367]:
#for index, node in enumerate(mle.postorder_node_iter()):
#   print node.label
#Change this to a print if you want to see a really long vector of node.labels. I just had this in as a check.

In [7]:
mle.write_to_path('attempt_1', 'nexus', suppress_annotations = False, annotations_as_nhx=False,
                  suppress_taxa_block=True,suppress_internal_taxon_labels=True)

You now have a FigTree-readable nexus with node and tip annotations.

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.