Example #1
0
    def plot(self, hardcopy=None):

        if hardcopy:
            R.png(hardcopy, width=1024, height=768, type="cairo")

        R.require('qvalue')

        # build a qobj
        R.assign("pval", self.mPValues)
        R.assign("pi0", self.mPi0)
        R.assign("qval", self.mQValues)
        R.assign("lambda", self.mLambda)
        R("""qobj <-list( pi0=pi0, qvalues=qval, pvalues=pval, lambda=lambda)"""
          )
        R(""" class(qobj) <- "qvalue" """)

        R("""qplot(qobj)""")

        if hardcopy:
            R.dev_off()
Example #2
0
def main():

    parser = E.OptionParser( version = "%prog version: $Id: rates2rates.py 2781 2009-09-10 11:33:14Z andreas $", usage = globals()["__doc__"])

    parser.add_option( "--output-filename-pattern", dest="output_filename_pattern", type="string",
                      help="pattern for additional output files [%default]."  )

    parser.add_option( "--input-filename-neutral", dest="input_filename_neutral", type="string",
                      help="a tab-separated file with rates and G+C content in neutrally evolving regions [%default]."  )

    parser.set_defaults(
        input_filename_neutral = None,
        output_filename_pattern = "%s",
        normalize = True,
        hardcopy = None,
        )

    (options, args) = E.Start( parser, add_csv_options = True )

    if not options.input_filename_neutral:
        raise ValueError( "please supply a file with neutral rates." )

    lines = options.stdin.readlines()
    if len(lines) == 0:
        raise IOError ( "no input" )

    from rpy import r as R
    import rpy

    R.png( options.output_filename_pattern % "fit" + ".png", width=1024, height=768, type="cairo")
    matrix, headers = readRates( open( options.input_filename_neutral, "r" ) )
    R.assign("matrix", matrix)
    R.assign("headers", headers)
    nref = R( """length( matrix[,1] )""" )

    dat = R("""dat <- data.frame(x = matrix[,2], y = matrix[,3])""")
    mod = R("""mod <- lm( y ~ x, dat)""")

    R("""plot( matrix[,2], matrix[,3], cex=%s, col="blue", pch="o", xlab="%s", ylab="%s" %s)""" % (0.3, headers[1], headers[2], "") )
    R("""new <- data.frame(x = seq( min(matrix[,2]), max(matrix[,2]), (max(matrix[,2]) - min(matrix[,2])) / 100))""")
    R("""predict(mod, new, se.fit = TRUE)""")
    R("""pred.w.plim <- predict(mod, new, interval="prediction")""")
    R("""pred.w.clim <- predict(mod, new, interval="confidence")""")
    R("""matpoints(new$x,cbind(pred.w.clim, pred.w.plim[,-1]), lty=c(1,2,2,3,3), type="l")""")
    R.mtext(
        "y = %f * x + %f, r=%6.4f, n=%i" % (mod["coefficients"]["x"], 
                                            mod["coefficients"]["(Intercept)"], 
                                            R("""cor( dat )[2]"""), 
                                            nref ),
        3,
        cex = 1.0)

    R("""mean_rate <- mean( matrix[,3] )""")

    data_matrix, data_headers = readRates( lines )
    R.assign("data_matrix", data_matrix)
    R.assign("data_headers", data_headers)
    ndata = R( """length( data_matrix[,1] )""" )
    
    R("""points( data_matrix[,2], data_matrix[,3], cex=%s, col="red", pch="o" %s)""" % (0.3, "") )
    R("""topred <- data.frame( x = data_matrix[,2] )""")
    R("""corrected_rates <- predict( mod, topred, se.fit = TRUE )""")
    uncorrected = R("""uncorrected <- data_matrix[,3] / mean_rate """) 
    corrected = R("""corrected <- as.vector(data_matrix[,3] / corrected_rates$fit)""")
    R.dev_off()
    
    R.png( options.output_filename_pattern % "correction" + ".png", width=1024, height=768, type="cairo")
    R("""plot( uncorrected, corrected, cex=%s, col="blue", pch="o", xlab="uncorrected rate", ylab="corrected rate" %s)""" % (0.3, "") )
    R.dev_off()

    E.Stop()
Example #3
0
def plot(outfile, data, out_format='png'):
    w = int(round(len(data) / 4.0))

    if out_format == 'png':
        r.png(outfile, width=w * 100, height=1000, res=72)
    elif out_format == 'pdf':
        r.pdf(outfile, width=w, height=10)
    else:
        raise Exception('Unrecognised format: ' + str(out_format))

    print("total: " + str(len(data)))

    series = []
    points = {'translate': [], 'preprocessing': []}

    for dat in data:
        points['translate'].append(float(dat['translate']))
        points['preprocessing'].append(float(dat['preprocessing']))

    xlabels = []
    for k, v in data[0].iteritems():
        if k not in ["problem", 'translate', 'preprocessing']:
            series.append(k)
            points[k] = []

    index = 0
    for dat in data:
        for k in series:
            if dat[k] != 'no-plan':
                points[k].append(float(dat[k]) + \
                                 points['translate'][index] + \
                                 points['preprocessing'][index])
            else:
                points[k].append(-1000)
        xlabels.append(dat['problem'])
        index += 1

    max_value = max(iter([max(iter(points[k])) for k in series]))
    yrange = (0, max_value)
    legend_labels = []

    x = [i for i in range(1, len(points['translate']) + 1)]
    y = [-1000 for i in x]
    r.par(mar=(7, 5, 4, 2))
    r.plot(x,
           y,
           main='',
           xlab="",
           ylab='',
           xaxt='n',
           yaxt='n',
           pch=0,
           ylim=yrange,
           mgp=(5, 1, 0))
    r.mtext("Problem", side=1, line=5)
    r.mtext("CPU Time (s)", side=2, line=3)

    pch_start = 1
    pch_index = pch_start
    # plotting "translate"
    #r.plot(x, points['translate'], main='',
    #       xlab='', ylab='Time (s)',
    #       xaxt='n', yaxt='n',
    #       pch=0, ylim=yrange)
    #legend_labels.append('translate')
    r.lines(x, points['translate'], lty=1)

    # preprocessing -- Removed since it's insignificant
    #r.points(x, points['preprocessing'], pch=pch_index)
    #pch_index =+ 1

    # planner output
    for k in series:
        if k != 'translate' and k != 'preporcessing':
            r.points(x, points[k], pch=pch_index)
            pch_index += 1
            legend_labels.append("FD+" + k.upper())

    # put x-axis labels
    for i in range(0, len(xlabels)):
        r.axis(side=1, at=i + 1, labels=xlabels[i], las=2)

    # put y-axis labels
    base, step = get_y_step(max_value)
    print("base: " + str(base) + " -- step: " + str(step))
    y = base
    for i in range(0, step):
        r.axis(side=2, at=y, labels=str(y), las=2)
        y += base

    # legend
    r.legend(1,
             max_value,
             legend_labels,
             pch=[i for i in range(pch_start, pch_index)])

    r.dev_off()