def avTimeExpression(infile):
    '''
    Calculate average expression over replicates at each time point
    Requires genes as columns with 'replicates' and 'times' as additional
    columns
    '''

    # check file compression
    if infile.split(".")[-1] == "gz":
        comp = "gzip"
    else:
        comp = None

    df = pd.read_table(infile, sep="\t",
                       header=0, index_col=0,
                       compression=comp)

    # average over replicates at each time point
    # then recombine
    df_groups = df.groupby(by='times')
    data_frame = pd.DataFrame(index=df.columns,
                              columns=None)
    for names, groups in df_groups:
        _df = groups.drop(['times', 'replicates'], axis=1)
        _df = _df.apply(np.mean, axis=0)
        data_frame[names] = _df

    # check no extraneous columns remaining
    try:
        data_frame.drop(['replicates', 'times'],
                        inplace=True,
                        axis=0)
    except __HOLE__:
        pass

    return data_frame
dataset/ETHPy150Open CGATOxford/cgat/CGAT/Timeseries/__init__.py/avTimeExpression