Purpose: This script melds together your node labels from Diversitree with the tip-based data you've save in a csv. This allows you to appropriately colorize the tree in FigTree. If you don't use this script, the tree will have colors that extend to the parent node of the tips, leaving the tips black.


In [1]:
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 [2]:
taxa = dendropy.TaxonSet()
mle = dendropy.Tree.get_from_path('../2598364_0', 'newick', taxon_set=taxa, preserve_underscores=True,extract_comment_metadata=True)

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


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

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


In [4]:
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 [5]:
taxa = []
putative_c = []
putative_r = []
for index, node in enumerate(mle.preorder_node_iter()):
    if len(node.child_nodes()) == 0:
        if float(node.label) == 1:
           node.annotations.add_new(name = '!color', value = '#ff0000')
        if float(node.label) == 0:
           node.annotations.add_new(name = '!color', value = '#0000FF')
    elif all([.95 < float(node.label) < 1]): 
         node.annotations.add_new(name = '!color', value = '#0000FF')
         for child in node.child_nodes():
                if any([0.0 < float(child.label) < 0.1 or float(child.label) == 1]):
                    node.annotations.add_new(name = '!color', value = '#ff0000')
                    putative_c.append(child.label)
                    print child.taxon
                else: 
                    node.annotations.add_new(name = '!color', value = '#0000FF')
    elif all([.05 > float(node.label) > 0]): 
         node.annotations.add_new(name = '!color', value = '#ff0000')
         for child in node.child_nodes():
                if any([0.95 < float(child.label) < 1 or float(child.label) == 0]):
                    node.annotations.add_new(name = '!color', value = '#0000FF')
                    putative_r.append(child.label)
                    print 'reverse: %s' % child.taxon
                else: 
                    node.annotations.add_new(name = '!color', value = '#ff0000')

 #   else:
  #      node.annotations.add_new(name = '!color', value = '#0000FF')
print 'reverse %s' % len(putative_r)


None
Rhacodactylus_trachyrhynchus
None
reverse: None
reverse: Azemiops_feae
reverse: Deinagkistrodon_acutus
reverse: Calloselasma_rhodostoma
reverse: Trimeresurus_borneensis
reverse: None
Psammodynastes_pulverulentus
Pseudaspis_cana
Liopholidophis_sexlineatus
Hemachatus_haemachatus
None
None
None
None
Sinonatrix_annularis
reverse: Calabaria_reinhardtii
reverse: Eryx_jayakari
reverse: Anomochilus_leonardi
Chamaeleo_affinis
None
None
None
reverse: Sceloporus_lundelli
Liolaemus_nigroviridis
reverse: Liolaemus_chiliensis
None
Liolaemus_espinozai
reverse: Liolaemus_calchaqui
reverse: Diploglossus_bilobatus
Anguis_fragilis
None
Monopeltis_capensis
Trogonophis_wiegmanni
reverse: None
None
reverse: None
None
None
Lipinia_noctua
reverse: Ablepharus_pannonicus
reverse: Oligosoma_suteri
None
reverse 17

In [6]:
mle.write_to_path('colors', '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.

In [ ]: