Beispiel #1
0
def get_tree_iter(args, i):
    """
    Reads in trees resulting from analyses of posterior predictive datasets.
    """
    
    # Instantiates tree list
    pp_trees = dendropy.TreeList()
    file = args.basename + '_' + str(i)

    try:
        taxa = dendropy.TaxonSet()
        
        if args.nruns > 1:
            tree_file_paths = ['{file}_r{run}.t'.format(file = file, run = j) for j in range(1, args.nruns + 1)]
            tree_iter = dendropy.multi_tree_source_iter(tree_file_paths, schema = 'nexus', taxon_set = taxa, tree_offset = args.burnin)
        elif args.nruns == 1:
            tree_file_path = '{file}_r1.t'.format(file = file)
            tree_iter = dendropy.tree_source_iter(tree_file_path, schema = 'nexus', taxon_set = taxa, tree_offset = args.burnin)
    except:
        print '''
        Problem getting tree iterator for {file}
        Exiting...'''.format(file = file)
        if args.debug:
            traceback.print_exc()
        sys.exit(1)
        
    ## Currently some error with the DataSet.unify_taxa() method [1.7.10]
    # pp_trees.unify_taxa()    # Just to make sure all trees share the same TaxonSet
    
    # Returns a dataset object containing separate treelists for each pp dataset
    return(tree_iter)
def tree_iter(infile,format,burnin,taxonset=None):
    '''Returns an iterator over a multi-tree file.'''
    return tree_source_iter(
    stream=open(infile, 'rU'),
    schema=format,
    taxon_set=taxonset,
    tree_offset=burnin)
def print_distances(tree_list,mle,uniq_flag=False):
	mle_tree, taxa = get_mle_tree(mle)
	distances = []
	uniq_trees = dendropy.TreeList()
	count = 1
	for t in tree_source_iter(stream=open(tree_list, 'rU'),schema='nexus',taxon_set=taxa):
		dist = treecalc.symmetric_difference(mle_tree, t)
		print "Distance between MLE tree and tree %i: %i" % (count,dist)
		distances.append(dist)
		count +=1
		if uniq_flag and dist > 0:
			uniq_trees.append(t)
	print("Mean symmetric distance between MLE and tree list: %d" \
		% float(sum(distances)/len(distances)))
	return uniq_trees, len(uniq_trees)
def print_distances(tree_list, mle, uniq_flag=False):
    mle_tree, taxa = get_mle_tree(mle)
    distances = []
    uniq_trees = dendropy.TreeList()
    count = 1
    for t in tree_source_iter(stream=open(tree_list, 'rU'),
                              schema='nexus',
                              taxon_set=taxa):
        dist = treecalc.symmetric_difference(mle_tree, t)
        print "Distance between MLE tree and tree %i: %i" % (count, dist)
        distances.append(dist)
        count += 1
        if uniq_flag and dist > 0:
            uniq_trees.append(t)
    print("Mean symmetric distance between MLE and tree list: %d" \
     % float(sum(distances)/len(distances)))
    return uniq_trees, len(uniq_trees)
Beispiel #5
0
#! /usr/bin/env python

import dendropy
from dendropy import tree_source_iter
from dendropy import treecalc

distances = []
taxa = dendropy.TaxonSet()
mle_tree = dendropy.Tree.get_from_path('pythonidae.mle.nex', 'nexus', taxon_set=taxa)
for mcmc_tree in tree_source_iter(
        stream=open('pythonidae.mcmc.nex', 'rU'),
        schema='nexus',
        taxon_set=taxa,
        tree_offset=200):
    distances.append(treecalc.symmetric_difference(mle_tree, mcmc_tree))
print("Mean symmetric distance between MLE and MCMC trees: %d"
        % float(sum(distances)/len(distances)))
#!/usr/bin/python                                                                          import dendropy
from dendropy import tree_source_iter 

best_tree = dendropy.Tree.get_from_path("best_tree.tre", schema="nexus")

output_f = open('treedist_output', 'w')
counter = 1

for tree in tree_source_iter(
        stream=open('all_trees', 'rU'),
        schema='nexus',
        taxon_set=best_tree.taxon_set):

    distance = best_tree.symmetric_difference(tree)
    output_f.write("1 " + str(counter) + " " + str(distance) + "\n")
    counter += 1

output_f.close()
def tree_iter(infile, format, burnin, taxonset=None):
    '''Returns an iterator over a multi-tree file.'''
    return tree_source_iter(stream=open(infile, 'rU'),
                            schema=format,
                            taxon_set=taxonset,
                            tree_offset=burnin)
Beispiel #8
0
 def testSummaryTreeIter(self):
     stream = pathmap.tree_source_stream('pythonidae.beast.summary.tre')
     for tree in dendropy.tree_source_iter(stream, 'beast-summary-tree'):
         self.verify_node_info(tree)