#! /usr/bin/env python import dendropy from dendropy import treesim sp_tree_str = """\ [&R] (A:10,(B:6,(C:4,(D:2,E:2):2):2):4) """ sp_tree = dendropy.Tree.get_from_string(sp_tree_str, "newick") gene_to_species_map = dendropy.TaxonSetMapping.create_contained_taxon_mapping( containing_taxon_set=sp_tree.taxon_set, num_contained=3) gene_tree = treesim.contained_coalescent( containing_tree=sp_tree, gene_to_containing_taxon_map=gene_to_species_map) print(gene_tree.as_string('newick')) print(gene_tree.as_ascii_plot())
#! /usr/bin/env python import dendropy from dendropy import treesim sp_tree_str = """\ [&R] (A:10,(B:6,(C:4,(D:2,E:2):2):2):4) """ sp_tree = dendropy.Tree.get_from_string(sp_tree_str, "newick") gene_to_species_map = dendropy.TaxonSetMapping.create_contained_taxon_mapping( containing_taxon_set=sp_tree.taxon_set, num_contained=3 ) gene_tree = treesim.contained_coalescent(containing_tree=sp_tree, gene_to_containing_taxon_map=gene_to_species_map) print(gene_tree.as_string("newick")) print(gene_tree.as_ascii_plot())
"returns labels from taxon x" return x.label ##comand line parsing parser = optparse.OptionParser() parser.add_option("-n", "--pop-size", type="float", help="effective population size, N_e", metavar="NUMBER",default=1e4) parser.add_option("-N", "--number-trees", type="int", help="number of trees to generate for each species tree", metavar="NUMBER",default=100) parser.add_option("-o", "--output-file", help="output coalescent trees to FILE", metavar="FILE") parser.add_option("-s", "--species-file", help="read species trees from FILE", metavar="FILE", default="species.nex") (options,args) = parser.parse_args() if options.output_file==None: genehandle = sys.stdout else: genehandle = open(options.output_file,"w") species = dendropy.TreeList.get_from_path(options.species_file,"nexus",as_rooted=True) taxon_map = dendropy.TaxonSetMapping.create_contained_taxon_mapping(species.taxon_set, 1,contained_taxon_label_func=mylabel) tlist = dendropy.TreeList() for tree in species: for i in range(0,options.number_trees): gene_tree = treesim.contained_coalescent(tree,taxon_map,default_pop_size=options.pop_size) tlist.append(gene_tree) ##gene_tree.write_to_stream(genehandle,"newick",suppress_rooting=True) tlist.write_to_stream(genehandle,"newick",suppress_rooting=True) genehandle.close()
genes_to_species = dendropy.TaxonSetMapping.create_contained_taxon_mapping( containing_taxon_set=containing_taxa, num_contained=8) # convert to containing tree stepwise_tree = reconcile.ContainingTree(stepwise_tree, embedded_taxon_set=genes_to_species.domain_taxon_set, embedded_to_containing_taxon_map=genes_to_species) frag_tree = reconcile.ContainingTree(frag_tree, embedded_taxon_set=genes_to_species.domain_taxon_set, embedded_to_containing_taxon_map=genes_to_species) # for each rep for rep in range(num_reps): gene_tree1 = treesim.contained_coalescent(containing_tree=stepwise_tree, gene_to_containing_taxon_map=genes_to_species, default_pop_size=40000) stepwise_tree.embed_tree(gene_tree1) gene_tree2 = treesim.contained_coalescent(containing_tree=frag_tree, gene_to_containing_taxon_map=genes_to_species, default_pop_size=40000) frag_tree.embed_tree(gene_tree2) # write results # returns dictionary with embedded trees as keys # and number of deep coalescences as values stepwise_deep_coals = stepwise_tree.deep_coalescences() stepwise_out = open("stepwise.txt", "w") for tree in stepwise_deep_coals: stepwise_out.write("%d\n" % stepwise_deep_coals[tree])
containing_taxon_set=containing_taxa, num_contained=8) # convert to containing tree stepwise_tree = reconcile.ContainingTree( stepwise_tree, contained_taxon_set=genes_to_species.domain_taxon_set, contained_to_containing_taxon_map=genes_to_species) frag_tree = reconcile.ContainingTree( frag_tree, contained_taxon_set=genes_to_species.domain_taxon_set, contained_to_containing_taxon_map=genes_to_species) # for each rep for rep in range(num_reps): gene_tree1 = treesim.contained_coalescent( containing_tree=stepwise_tree, gene_to_containing_taxon_map=genes_to_species, default_pop_size=40000) stepwise_tree.embed_tree(gene_tree1) gene_tree2 = treesim.contained_coalescent( containing_tree=frag_tree, gene_to_containing_taxon_map=genes_to_species, default_pop_size=40000) frag_tree.embed_tree(gene_tree2) # write results # returns dictionary with contained trees as keys # and number of deep coalescences as values stepwise_deep_coals = stepwise_tree.deep_coalescences() stepwise_out = open("stepwise.txt", "w") for tree in stepwise_deep_coals: