def outputTagSummary(filename_tags,
                     outfile,
                     output_filename_pattern,
                     filename_design=None):
    '''output summary values for a count table.'''

    E.info("loading tag data from %s" % filename_tags)

    if filename_design is not None:
        # load all tag data
        loadTagData(filename_tags, filename_design)

        # filter
        nobservations, nsamples = filterTagData()

    else:
        # read complete table
        R('''countsTable = read.delim('%(filename_tags)s',
        header = TRUE,
        row.names = 1,
        stringsAsFactors = TRUE,
        comment.char = '#')''' % locals())

        nobservations, nsamples = tuple(R('''dim(countsTable)'''))
        E.info("read data: %i observations for %i samples" %
               (nobservations, nsamples))
        # remove samples without data
        R('''max_counts = apply(countsTable,2,max)''')

        filter_min_counts_per_sample = 1

        empty_samples = tuple(
            R('''max_counts < %i''' % filter_min_counts_per_sample))
        sample_names = R('''colnames(countsTable)''')
        nempty_samples = sum(empty_samples)

        if nempty_samples:
            E.warn("%i empty samples are being removed: %s" %
                   (nempty_samples,
                    ",".join([sample_names[x]
                              for x, y in enumerate(empty_samples) if y])))
            R('''countsTable <- countsTable[, max_counts >= %i]''' %
              filter_min_counts_per_sample)
            nobservations, nsamples = tuple(R('''dim(countsTable)'''))

        R('''groups = factor(colnames( countsTable ))''')
        E.debug("sample names: %s" % R('''colnames(countsTable)'''))

    nrows, ncolumns = tuple(R('''dim(countsTable)'''))

    outfile.write("metric\tvalue\tpercent\n")
    outfile.write("number of observations\t%i\t100\n" % nobservations)
    outfile.write("number of samples\t%i\t100\n" % nsamples)

    # Count windows with no data
    R('''max_counts = apply(countsTable,1,max)''')

    # output distribution of maximum number of counts per window
    outfilename = output_filename_pattern + "max_counts.tsv.gz"
    E.info(outfilename % "outputting maximum counts per window to %s")
    R('''write.table(table(max_counts),
    file=gzfile('%(outfilename)s'),
    sep="\t",
    row.names=FALSE,
    quote=FALSE)''' %
      locals())

    # removing empty rows
    E.info("removing rows with no counts in any sample")
    R('''countsTable = countsTable[max_counts>0,]''')

    for x in range(0, 20):
        nempty = tuple(R('''sum(max_counts <= %i)''' % x))[0]
        outfile.write("max per row<=%i\t%i\t%f\n" %
                      (x, nempty, 100.0 * nempty / nrows))

    E.info("removed %i empty rows" % tuple(R('''sum(max_counts == 0)''')))
    observations, samples = tuple(R('''dim(countsTable)'''))
    E.info("trimmed data: %i observations for %i samples" %
           (observations, samples))

    # build correlation
    R('''correlations = cor(countsTable)''')
    outfilename = output_filename_pattern + "correlation.tsv"
    E.info("outputting sample correlations table to %s" % outfilename)
    R('''write.table(correlations, file='%(outfilename)s',
    sep="\t",
    row.names=TRUE,
    col.names=NA,
    quote=FALSE)''' % locals())

    # output scatter plots
    outfilename = output_filename_pattern + "scatter.png"
    E.info("outputting scatter plots to %s" % outfilename)
    R.png(outfilename, width=960, height=960)
    plotPairs()
    R['dev.off']()

    # output heatmap based on correlations
    outfilename = output_filename_pattern + "heatmap.svg"
    E.info("outputting correlation heatmap to %s" % outfilename)
    R.svg(outfilename)
    plotCorrelationHeatmap(method="correlation")
    R['dev.off']()

    # output PCA
    outfilename = output_filename_pattern + "pca.svg"
    E.info("outputting PCA plot to %s" % outfilename)
    R.svg(outfilename)
    plotPCA(groups=False)
    R['dev.off']()

    # output an MDS plot
    R('''suppressMessages(library('limma'))''')
    outfilename = output_filename_pattern + "mds.svg"
    E.info("outputting mds plot to %s" % outfilename)
    R.svg(outfilename)
    try:
        R('''plotMDS(countsTable)''')
    except rpy2.rinterface.RRuntimeError, msg:
        E.warn("can not plot mds: %s" % msg)
    R['dev.off']()
dataset/ETHPy150Open CGATOxford/cgat/CGAT/Expression.py/outputTagSummary/swapped operands of `%`