Exemple #1
0
def draw_events(canvas,
                tree,
                coords,
                events,
                losses,
                lossColor=(0, 0, 1),
                dupColor=(1, 0, 0),
                size=4):

    # draw duplications
    for node in tree:
        x, y = coords[node]
        if events[node] == "dup":
            canvas.rect(x - size / 2.0,
                        y - size / 2.0,
                        size,
                        size,
                        fillColor=dupColor,
                        strokeColor=(0, 0, 0, 0))

    # draw losses
    losses_per_branch = util.hist_dict([node for node, schild in losses])

    for node, nlosses in losses_per_branch.iteritems():
        if node.parent == None:
            continue

        x1 = coords[node.parent][0]
        x2, y1 = coords[node]
        step = (x2 - x1) / float(nlosses + 1)

        for x in util.frange(x1 + step, x2 - (step / 2.0), step):
            canvas.line(x, y1 - size, x, y1 + size, color=lossColor)
def make_color_legend(filename, colormap, start, end, step, width=100, height=10, display=False):
    from rasmus import util

    if filename is None:
        filename = util.tempfile(".", "colormap", ".svg")
        temp = True
    else:
        temp = False

    s = svg.Svg(util.open_stream(filename, "w"))
    s.beginSvg(width, height)

    xscale = float(width) / (end + step - start)

    for i in util.frange(start, end + step, step):
        color = colormap.get(i)
        s.rect((i - start) * xscale, 0, step * xscale, height, color, color)

    s.endSvg()
    s.close()

    # display
    if display:
        os.system("display %s" % filename)

    # clean up temp files
    if temp:
        os.remove(filename)
Exemple #3
0
def make_color_legend(filename,
                      colormap,
                      start,
                      end,
                      step,
                      width=100,
                      height=10,
                      display=False):
    from rasmus import util

    if filename is None:
        filename = util.tempfile(".", "colormap", ".svg")
        temp = True
    else:
        temp = False

    s = svg.Svg(util.open_stream(filename, "w"))
    s.beginSvg(width, height)

    xscale = float(width) / (end + step - start)

    for i in util.frange(start, end + step, step):
        color = colormap.get(i)
        s.rect((i - start) * xscale, 0, step * xscale, height, color, color)

    s.endSvg()
    s.close()

    # display
    if display:
        os.system("display %s" % filename)

    # clean up temp files
    if temp:
        os.remove(filename)
Exemple #4
0
    def test_plot_prob_bounded_coal(self):
        n = 1000
        k = 4
        t = 800
        alltimes = []

        # sample times
        for i in xrange(5000):
            while True:
                times = [0]
                for j in xrange(k, 1, -1):
                    times.append(times[-1] + coal.sample_coal(j, n))
                    if times[-1] >= t:
                        break
                if times[-1] < t:
                    break
            alltimes.append(times)

        p = Gnuplot()
        for i in range(1, 2):
            x, y = distrib([q[i] - q[i-1] for q in alltimes], width=20)
            p.plot(x, y, style="lines", xmax=500)

        x = list(frange(0, 500, 10))
        #for i in range(1, 2): #k):
        y2 = [coal.prob_bounded_coal(j, k, n, t) for j in x]
        p.plot(x, y2, style="lines", xmax=500)

        fequals(y, y2, rel=.05, eabs=.01)
def draw_events(canvas, tree, coords, events, losses,
               lossColor=(0, 0, 1),
               dupColor=(1, 0, 0),
               size=4):

    # draw duplications
    for node in tree:
        x, y = coords[node]
        if events[node] == "dup":
            canvas.rect(x - size/2.0, y - size/2.0,
                        size, size,  fillColor=dupColor, strokeColor=(0,0,0,0))

    # draw losses
    losses_per_branch = util.hist_dict([node for node, schild in losses])

    for node, nlosses in losses_per_branch.iteritems():
        if node.parent == None:
            continue

        x1 = coords[node.parent][0]
        x2, y1 = coords[node]
        step = (x2 - x1) / float(nlosses + 1)

        for x in util.frange(x1 + step, x2-(step/2.0), step):
            canvas.line(x, y1 - size, x, y1 + size, color=lossColor)
Exemple #6
0
    def draw_events(self):

        # draw duplications
        dups = [color(*self.dup_color)]        
        for node in self.tree:
            if self.events[node] == "dup":
                dups.append(
                    zoom_clamp(
                        shapes.box(node.x - .5, node.y - .5,
                                   node.x + .5, node.y + .5),
                        link=True, link_type="smaller",
                        maxx=8, minx=1,
                        maxy=8, miny=1,
                        origin=(node.x, node.y),
                        prezoom=(self.xscale, 1.0)))
        
        # draw losses
        losses_per_branch = util.hist_dict([node for node, schild in self.losses])
        
        losses = [color(*self.loss_color)]
        for node, nlosses in losses_per_branch.iteritems():
            if node.parent == None:
                continue
                
            x1 = node.parent.x        
            x2 = node.x
            step = (x2 - x1) / float(nlosses + 1)
            
            for x in util.frange(x1 + step, x2-(step/2.0), step):
                losses.append(lines(x, node.y - .2, x, node.y + .2))
        
        return group(group(*dups), group(*losses))
Exemple #7
0
    def test_prob_coal_cond_counts(self):

        # match against a simpler slower implementation
        a = 5
        b = 3
        t = 2000
        n = 1000

        for x in frange(0, 2000, 10):
            y = coal.prob_coal_cond_counts(x, a, b, t, n)
            y2 = coal.prob_coal_cond_counts_simple(x, a, b, t, n)
            self.assertAlmostEqual(y, y2)
Exemple #8
0
    def test_cdf_mrca2(self):
        n = 1000
        k = 6
        step = 10
        x = list(frange(0, 5000, step))
        y = [coal.prob_mrca(i, k, n) * step for i in x]
        y2 = cumsum(y)
        y3 = [coal.cdf_mrca(t, k, n) for t in x]
        y4 = [coal.prob_coal_counts(k, 1, t, n) for t in x]

        fequals(y2, y3, eabs=.01)
        fequals(y3, y4)
Exemple #9
0
    def test_cdf_coal_bounded(self):
        n = 1000
        k = 4
        t = 500
        step = .1
        x = list(frange(0, 500, step))
        y = [coal.prob_bounded_coal(i, k, n, t) * step for i in x]
        y2 = cumsum(y)
        y3 = [coal.cdf_bounded_coal(i, k, n, t) for i in x]

        p = plot(x, y2, style="lines")
        p.plot(x, y3, style="lines")
        p.plot([0, 500], [1, 1], style="lines")

        fequals(y2, y3, eabs=.01)
Exemple #10
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
Exemple #11
0
def make_color_legend(filename, colormap, start, end, step, 
                    width=100, height=10):
    from rasmus import util
    s = svg.Svg(util.open_stream(filename, "w"))    
    s.beginSvg(width, height)
    
    xscale =  float(width) / (end + step - start)
    
    for i in util.frange(start, end + step, step):
        color = colormap.get(i)
        s.rect((i-start) * xscale, 
               0, 
               step*xscale, height, 
               color, color)
    
    s.endSvg()
Exemple #12
0
def make_color_legend(filename, colormap, start, end, step, 
                    width=100, height=10):
    from rasmus import util
    s = svg.Svg(util.open_stream(filename, "w"))    
    s.beginSvg(width, height)
    
    xscale =  float(width) / (end + step - start)
    
    for i in util.frange(start, end + step, step):
        color = colormap.get(i)
        s.rect((i-start) * xscale, 
               0, 
               step*xscale, height, 
               color, color)
    
    s.endSvg()
Exemple #13
0
    def test_prob_coal_cond_counts_simple(self):

        # when we condition on b=1, it is the same as the bounded coal
        # PDF.
        # prob_coal_cond_counts is actually a more general version of
        # prob_bounded_coal

        outdir = 'test/tmp/test_coal/Coal_test_prob_coal_counys_simple/'
        make_clean_dir(outdir)

        a = 5
        b = 1
        t = 2000
        n = 1000
        p = Gnuplot()
        p.enableOutput(False)

        for x in frange(0, 2000, 10):
            y = coal.prob_coal_cond_counts_simple(x, a, b, t, n)
            y2 = coal.prob_bounded_coal(x, a, n, t)
            self.assertAlmostEqual(y, y2)
Exemple #14
0
    def test_cdf_mrca(self):

        outdir = 'test/tmp/test_coal/Coal_test_cdf_mrca/'
        make_clean_dir(outdir)

        n = 1000
        k = 6
        step = 10
        x = list(frange(0, 5000, step))
        y = [coal.prob_mrca(i, k, n) * step for i in x]
        y2 = cumsum(y)
        y3 = [coal.cdf_mrca(t, k, n) for t in x]

        p = Gnuplot()
        p.enableOutput(False)
        p.plot(x, y2, style="lines")
        p.plot(x, y3, style="lines")
        p.enableOutput(True)
        p.save(outdir + 'plot.png')

        eq_sample_pdf(x, lambda t: coal.cdf_mrca(t, k, n), 40)