def consensus(splits, taxon_namespace, q=0.5): """Creates a consensus tree from a dictionary of splits w/ credibilities (default is majority-rule consensus). Splits are added to the tree in order of decreasing credibility. A split is added if: 1. Its credibility is >= q 2. It is compatible with all preceding splits The edges of the returned tree are annotated with their split's credibilities. """ # Order and select splits sorted_splits = sorted(splits.items(), key=op.itemgetter(1), reverse=True) selected_splits = map(op.itemgetter(0), it.takewhile(lambda x: x[1] >= q, sorted_splits)) # Dendropy handles dropping of incompatible splits tree = Tree.from_bipartition_encoding(selected_splits, taxon_namespace) # Annotate edges with credibilities for edge in tree.edges(): edge.annotations.add(Annotation('cred', splits[edge.bipartition])) return tree