def runTTestPandas(counts_table,
                   design_table,
                   outfile,
                   outfile_prefix,
                   fdr,
                   ref_group=None):
    '''apply a ttest on the data.

    For the T-test it is best to use FPKM values as
    this method does not perform any library normalization.
    Alternatively, perform normalisation on counts table using Counts.py
    '''
    stats = importr('stats')

    (groups, pairs, conds, factors, has_replicates,
     has_pairs) = checkTagGroupsPandas(design_table, ref_group)

    df_dict = makeEmptyDataFrameDict()

    for combination in itertools.combinations(groups, 2):
        # as each combination may have different numbers of samples in control
        # and treatment, calculations have to be performed on a per
        # combination basis

        control, treatment = combination
        n_rows = counts_table.shape[0]
        df_dict["control_name"].extend((control,)*n_rows)
        df_dict["treatment_name"].extend((treatment,)*n_rows)
        df_dict["test_id"].extend(counts_table.index.tolist())

        # subset counts table for each combination
        c_keep = [x == control for x in conds]
        control_counts = counts_table.iloc[:, c_keep]
        t_keep = [x == treatment for x in conds]
        treatment_counts = counts_table.iloc[:, t_keep]

        c_mean = control_counts.mean(axis=1)
        df_dict["control_mean"].extend(c_mean)
        df_dict["control_std"].extend(control_counts.std(axis=1))

        t_mean = treatment_counts.mean(axis=1)
        df_dict["treatment_mean"].extend(t_mean)
        df_dict["treatment_std"].extend(treatment_counts.std(axis=1))

        t, prob = ttest_ind(control_counts, treatment_counts, axis=1)
        df_dict["p_value"].extend(prob)
        # what about zero values?!
        df_dict["fold"].extend(t_mean / c_mean)

    df_dict["p_value_adj"].extend(
        list(stats.p_adjust(FloatVector(df_dict["p_value"]), method='BH')))

    df_dict["significant"].extend(
        [int(x < fdr) for x in df_dict["p_value_adj"]])

    df_dict["l2fold"].extend(list(numpy.log2(df_dict["fold"])))
    # note: the transformed log2 fold change is not transformed!
    df_dict["transformed_l2fold"].extend(list(numpy.log2(df_dict["fold"])))

    # set all status values to "OK"
    df_dict["status"].extend(("OK",)*n_rows)

    results = pandas.DataFrame(df_dict)
    results.set_index("test_id", inplace=True)
    results.to_csv(outfile, sep="\t", header=True, index=True)
dataset/ETHPy150Open CGATOxford/cgat/CGAT/Expression.py/runTTestPandas/original