def optimize_model(self, gtree, stree, gene2species):
        """Optimizes the model"""
        CostModel.optimize_model(self, gtree, stree, gene2species)
        
        # ensure gtree and stree are both rooted and binary
        if not (treelib.is_rooted(gtree) and treelib.is_binary(gtree)):
            raise Exception("gene tree must be rooted and binary")
        if not (treelib.is_rooted(stree) and treelib.is_binary(stree)):
            raise Exception("species tree must be rooted and binary")
        try:
            junk = phylo.reconcile(gtree, stree, gene2species)
        except:
            raise Exception("problem mapping gene tree to species tree")
    
        treeout = StringIO.StringIO()
        if not self.printed:
            import pprint
            treelib.draw_tree(gtree, out=treeout, minlen=5, maxlen=5)
            print "gene tree:\n"
            print(treeout.getvalue())
            
            treelib.draw_tree(self.stree, out=treeout, minlen=5, maxlen=5)
            print "spec tree:\n"
            print(treeout.getvalue())
            pprint.pprint(junk)

            self.printed = True
Exemple #2
0
def draw_raxml_tree(tr, adef):
    util.tic("Tree to string...")
    treestr = raxml.tree_to_string(tr, adef)
    util.toc()

    util.tic("Drawing tree...")
    T = treelib.parse_newick(treestr)
    T2 = treelib.unroot(T)
    treelib.draw_tree(T2, out=sys.stdout, minlen=5, maxlen=5)
    util.toc()
Exemple #3
0
def draw_raxml_tree(tr, adef):
    util.tic("Tree to string...")
    treestr = raxml.tree_to_string(tr, adef)
    util.toc()

    util.tic("Drawing tree...")
    T = treelib.parse_newick(treestr)
    T2 = treelib.unroot(T)
    treelib.draw_tree(T2, out=sys.stdout, minlen=5, maxlen=5)
    util.toc()
Exemple #4
0
def write_event_tree(stree, out=sys.stdout):
    labels = {}
    for name, node in stree.nodes.iteritems():
        labels[name] = "[%s]\nD=%d,L=%d;\nG=%d;" % \
                       (str(name),
                        node.data['dup'], node.data['loss'],
                        node.data['genes'])

    treelib.draw_tree(stree, labels=labels, minlen=15, spacing=4,
                      labelOffset=-3,
                      out=out)
Exemple #5
0
def buildTree(conf, stree, gene2species):
    params = Spidir.readParams(conf["param"])
    
    if "correcttree" in conf:
        conf["correcthash"] = phylo.hash_tree(conf["correcttree"])
    
    
    if "dist" in conf:
        for i in range(len(conf["dist"])):
            distfile = conf["dist"][i]
            
            labels, distmat = phylip.read_dist_matrix(distfile)
        
            # read in different labels if needed
            if "labels" in conf:
                labels = Spidir.readLabels(conf["labels"][i])
                conf["aln"] = fasta.read_fasta(conf["labels"][i])
            
            tree, logl = Spidir.spidir(conf, distmat, labels, stree, 
                                          gene2species, params)
            tree.write(Spidir.outTreeFile(conf))
            
            # test for correctness
            if "correcttree" in conf:
                correctTree = conf["correcttree"]
                phylo.hash_order_tree(correctTree)
                phylo.hash_order_tree(tree)
                
                thash1 = phylo.hash_tree(tree)
                thash2 = phylo.hash_tree(correctTree)
                
                print "spidir: "
                treelib.draw_tree(tree, maxlen=5, minlen=5)
                print
                
                print "correct:"
                treelib.draw_tree(correctTree, maxlen=5, minlen=5)
                print
                
                if len(tree.leaves()) > 3:
                    rferror = Spidir.robinson_foulds_error(correctTree, tree)
                else:
                    rferror = 0.0
                
                if thash1 == thash2:
                    print "CORRECT TREE FOUND"
                else:
                    print "WRONG TREE FOUND (RF: %f)" % rferror
Exemple #6
0
def debug_test2():
    stree = treelib.read_tree('examples/flies.stree') # run from ../ of this directory
    for node in stree:
        node.dist *= 1e7 # gen per myr
    popsize = 2e7
    freq = 1e0
    dr = .0012/1e7
    lr = .0006/1e7
    freqdup = freqloss = .05
    forcetime = 1e7
    
#    ltree, ex = sim_DLILS_gene_tree(stree, popsize, freq, dr, lr, freqdup, freqloss, forcetime)
    
    coal_tree, ex = sample_dlcoal_no_ifix(stree=stree, n=popsize, freq=freq, duprate=dr, lossrate=lr, freqdup=freqdup, freqloss=freqloss, forcetime=forcetime)
    
    treelib.draw_tree(coal_tree, scale=.00000005)
    def test_draw_tree(self):
        """Test tree drawing"""
        text = "((A:10,B:1):5,(C:2,D:3):5);"
        tree = treelib.parse_newick(text)
        out = StringIO()
        treelib.draw_tree(tree, scale=1, spacing=2, out=out,
                          labelOffset=-1, minlen=1)
        drawing = out.getvalue()
        expected = '''\
      /---------  A
 /----+
 |    \  B
++
 |    /-  C
 \----+
      \--  D
'''
        self.assertEqual(drawing, expected)
Exemple #8
0
    def test_draw_tree(self):
        """Test tree drawing"""
        text = "((A:10,B:1):5,(C:2,D:3):5);"
        tree = treelib.parse_newick(text)
        out = StringIO()
        treelib.draw_tree(tree,
                          scale=1,
                          spacing=2,
                          out=out,
                          labelOffset=-1,
                          minlen=1)
        drawing = out.getvalue()
        expected = '''\
      /---------  A
 /----+
 |    \  B
++
 |    /-  C
 \----+
      \--  D
'''
        self.assertEqual(drawing, expected)
Exemple #9
0
    return stree # poor nomenclature; this will be fixed in v2.1



if __name__ == "__main__":
    stree = treelib.read_tree('simple.stree')
    popsize = 1e4
    freq = 1e0
    dr = 2.1
    lr = 2.0
    freqdup = .05
    freqloss = .05
    forcetime = 1e0
    tree = sim_tree(stree, popsize, freq, dr, lr, freqdup, freqloss, forcetime)
    if tree:
        treelib.draw_tree(tree, scale=1)



### VERSION 1 CODE (for reference)
#
#def sim_branch(N=1e6, T=1e4, S=1e1, dr=.0012, lr=.0011, Fd=None, Fl=None, Fi=[1e0]):
#    """ 
#    Run a Poisson-based D/L simulation on a single branch.
#    N is population size, 
#    T is total time to run the sim, 
#    S is the number of steps in the time period T,
#    dr is the duplication rate (in events/individual/myr), 
#    lr is the loss rate (in events/individual/myr),
#    Fd is the frequency for dups,
#    Fl is the frequency for losses, 
 def draw_raxml_tree(self, *args, **kargs):
     """Draw raxml tr -- adef and tr must have been previously defined"""
     treestr = raxml.tree_to_string(self.tr, self.adef)
     tree = treelib.parse_newick(treestr)
     treelib.draw_tree(treelib.unroot(tree), *args, **kargs)
 def draw_tree(self):
     treelib.draw_tree(self.tree)
Exemple #12
0
    
    return gtree



if __name__ == "__main__":
    stree = treelib.read_tree('simple.stree')
    popsize = 1e4
    freq = 1e0
    dr = 2.1
    lr = 2.0
    freqdup = .07
    freqloss = .05
    forcetime = 1e0
    gtree = sim_tree(stree, popsize, freq, dr, lr, freqdup, freqloss, forcetime)
    treelib.draw_tree(stree, scale=1)
    print 
    for leaf in stree.leaves():
        print leaf.name, treelib.find_dist(stree, stree.root.name, leaf.name)
    print
    treelib.draw_tree(gtree, scale=1)
    print
    
    if len(gtree.nodes) > 1:
        for leaf in gtree.leaves():
            print leaf.name, treelib.find_dist(gtree,gtree.root.name,leaf.name), leaf.data['freq']
    else:
        print 'only the root remains'
    
#    if len(gtree.nodes) > 1:
#        extant_count = 0