def filterTagData(filter_min_counts_per_row=1,
                  filter_min_counts_per_sample=10,
                  filter_percentile_rowsums=0):
    '''filter tag data.

    * remove rows with fewer than x counts in most highly expressed sample

    * remove samples with fewer than x counts in most highly expressed row

    * remove the lowest percentile of rows in the table, sorted
       by total tags per row
    '''

    # Remove windows with no data
    R('''max_counts = apply(countsTable,1,max)''')
    R('''countsTable = countsTable[max_counts>=%i,]''' %
      filter_min_counts_per_row)
    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))

    # remove samples without data
    R('''max_counts = apply(countsTable,2,max)''')

    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)
        R('''groups <- groups[max_counts >= %i]''' %
          filter_min_counts_per_sample)
        R('''pairs <- pairs[max_counts >= %i]''' %
          filter_min_counts_per_sample)
        R('''if (!is.na(factors)) {factors <- factors[max_counts >= %i,]}''' %
          filter_min_counts_per_sample)
        observations, samples = tuple(R('''dim(countsTable)'''))

    # percentile filtering
    if filter_percentile_rowsums > 0:
        percentile = float(filter_percentile_rowsums) / 100.0
        R('''sum_counts = rowSums( countsTable )''')
        R('''take = (sum_counts > quantile(sum_counts, probs = %(percentile)f))''' %
          locals())
        discard, keep = R('''table( take )''')
        E.info("percentile filtering at level %f: keep=%i, discard=%i" %
               (filter_percentile_rowsums,
                keep, discard))
        R('''countsTable = countsTable[take,]''')

    observations, samples = tuple(R('''dim(countsTable)'''))

    return observations, samples
dataset/ETHPy150Open CGATOxford/cgat/CGAT/Expression.py/filterTagData/original