Пример #1
0
    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
 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")
Пример #3
0
    def optimize_model(self, gtree, stree, gene2species):
        """Optimizes the model"""
        CostModel.optimize_model(self, gtree, stree, gene2species)

        #=============================
        # read sequences
        if not self.align:
            self.parser.error("--align must be specified")
        self.align = fasta.read_fasta(self.align)

        #=============================
        # read SPIDIR parameters
        if not self.params:
            self.parser.error("--param must be specified")
        self.params = spidir.read_params(self.params)

        #=============================
        # determine background base frequency
        if self.bgfreq:
            # use supplied frequency
            vals = map(float, self.bgfreq.split(","))
            if len(vals) != 4:
                self.parser.error("invalid --bgfreq: %s" % self.bgfreq)
            self.bgfreq = vals
        else:
            # compute frequency from alignment
            self.bgfreq = alignlib.compute_bgfreq(self.align)

        #=============================
        # branch lengths
        if self.kappa >= 0:
            # use supplied kappa
            self.kappa = self.kappa
        else:
            # compute kappa from alignment
            # from spidir.find_ml_kapp_hky
            minkappa = 0.4; maxkappa = 5.0; stepkappa = 0.1
            maxlk = -util.INF
            maxk = minkappa

            for k in util.frange(minkappa, maxkappa, stepkappa):
                l = spidir.find_ml_branch_lengths_hky(gtree, self.align, self.bgfreq, k, maxiter=1,
                                                      parsinit=(k == minkappa))
                if l > maxlk:
                    maxlk = l
                    maxk = k

            self.kappa = maxk
Пример #4
0
    def optimize_model(self, gtree, stree, gene2species):
        """Optimizes the model"""
        CostModel.optimize_model(self, gtree, stree, gene2species)

        if self.dupcost < 0:
            self.parser.error("-D/--dupcost must be >= 0")
        if self.losscost < 0:
            self.parser.error("-L/--losscost must be >= 0")

        # 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")
Пример #5
0
    def optimize_model(self, gtree, stree, gene2species):
        """Optimizes the model"""
        CostModel.optimize_model(self, gtree, stree, gene2species)

        # check arguments
        if self.dupcost < 0:
            self.parser.error("-D/--dupcost must be >= 0: " + str(self.dupcost))
        if self.losscost < 0:
            self.parser.error("-L/--losscost must be >= 0: " + str(self.losscost))
        if self.coalcost < 0:
            self.parser.error("-C/--coalcost must be >= 0: " + str(self.coalcost))
        if self.output is None:
            self.parser.error("-o/--output must be specified")

        if self.dcs < 0 or self.dcs > 1:
            self.parser.error("--dcs must be in [0,1]: " + str(self.dcs))

        # copy over tree topology
        ltree = treelib.Tree(nextname=gtree.nextname)
        def walk(node):
            # copy of node
            newnode = treelib.TreeNode(node.name)

            # recurse
            for child in node.children:
                ltree.add_child(newnode, walk(child))

            return newnode
        if gtree.root:
            walk(gtree.root)
            ltree.root = ltree.nodes[gtree.root.name]
        else:
            raise Exception("input gene tree must be rooted")
        self.locustree = ltree

        # search functions
        self.search = phylo.TreeSearchMix(None)
        self.search.add_proposer(phylo.TreeSearchNni(None), 0.5)
        self.search.add_proposer(phylo.TreeSearchSpr(None), 0.5)