def rank_genes_or_pathways(col_to_rank, DE_summary_stats, is_template):
    """
    Returns the input dataframe (`DE_summary_stats`) that has been modified such that
    genes are ranked by the selected statistic, `col_to_rank` (if the input is the
    template experiment) or the median of the selected statistic
    (if the input is the simulated experiments).
    The ordering of the ranking depends on the statistic selected.

    Arguments
    ---------
    col_to_rank: str
        DE statistic to use to rank genes
    DE_summary_stats: df
        dataframe containing gene ranking for either template or simulated experiments
    is_template: bool
        if the DE_summary_stats df is for the template experiment or simulated experiments

    """

    # If ranking by p-value or adjusted p-value then high rank = low value
    if col_to_rank in ["P.Value", "adj.P.Val", "pvalue", "padj", "FDR", "p.adjust"]:
        if is_template:
            DE_summary_stats["ranking"] = DE_summary_stats[col_to_rank].rank(
                ascending=False
            )
            DE_summary_stats = DE_summary_stats.sort_values(
                by=col_to_rank, ascending=True
            )
        else:
            DE_summary_stats["ranking"] = DE_summary_stats[
                (col_to_rank, "median")
            ].rank(ascending=False)
            DE_summary_stats = DE_summary_stats.sort_values(
                by=(col_to_rank, "median"), ascending=True
            )

    # If ranking by logFC then high rank = high abs(value)
    elif col_to_rank in ["logFC", "t", "log2FoldChange", "ES"]:
        if is_template:
            DE_summary_stats["ranking"] = DE_summary_stats[col_to_rank].rank(
                ascending=True
            )
            DE_summary_stats = DE_summary_stats.sort_values(
                by=col_to_rank, ascending=False
            )
        else:
            DE_summary_stats["ranking"] = DE_summary_stats[
                (col_to_rank, "median")
            ].rank(ascending=True)
            DE_summary_stats = DE_summary_stats.sort_values(
                by=(col_to_rank, "median"), ascending=False
            )

    # If ranking by Z-score then high rank = high value
    else:
        if is_template:
            DE_summary_stats["ranking"] = DE_summary_stats[col_to_rank].rank(
                ascending=True
            )
            DE_summary_stats = DE_summary_stats.sort_values(
                by=col_to_rank, ascending=False
            )
        else:
            DE_summary_stats["ranking"] = DE_summary_stats[
                (col_to_rank, "median")
            ].rank(ascending=True)
            DE_summary_stats = DE_summary_stats.sort_values(
                by=(col_to_rank, "median"), ascending=False
            )

    return DE_summary_stats