File size: 1,818 Bytes
a521a25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from __future__ import annotations
import gzip, pathlib, re
import numpy as np
import pandas as pd
from scipy.stats import ttest_ind
from statsmodels.stats.multitest import multipletests

def read_geo_counts(path: pathlib.Path) -> pd.DataFrame:
    df = pd.read_csv(path, compression="gzip", index_col=0)
    df.index = df.index.astype(str).str.replace(r"\.\d+$", "", regex=True)
    df = df.groupby(level=0).mean(numeric_only=True)
    return df

def hct116_de(df: pd.DataFrame) -> tuple[pd.DataFrame, list[str], list[str]]:
    acid = [c for c in df.columns if re.search(r"HCT65", c, re.I)]
    ctrl = [c for c in df.columns if re.search(r"HCT74", c, re.I)]
    if len(acid) < 2 or len(ctrl) < 2:
        raise ValueError(f"Expected HCT65/HCT74 replicates; found acid={acid}, control={ctrl}")
    x = np.log2(df[acid].astype(float) + 1)
    y = np.log2(df[ctrl].astype(float) + 1)
    stat, p = ttest_ind(x.values, y.values, axis=1, equal_var=False, nan_policy="omit")
    out = pd.DataFrame({"gene": df.index, "log2fc": x.mean(1)-y.mean(1), "statistic":stat, "pvalue":p})
    out["fdr"] = multipletests(out.pvalue.fillna(1), method="fdr_bh")[1]
    out["mean_log2_expression"] = pd.concat([x,y],axis=1).mean(1).values
    return out.sort_values("fdr"), acid, ctrl

def evidence_table(de: pd.DataFrame) -> pd.DataFrame:
    # Only actual evidence is scored. Literature/screen tiers are not injected until a parsed source table exists.
    z = de.copy()
    z["acid_crc_de"] = ((z.fdr <= .05) & (z.log2fc.abs() >= 1)).astype(int)
    z["acid_crc_direction"] = np.where(z.log2fc > 0, "up", "down")
    z["evidence_score"] = z["acid_crc_de"].astype(float)
    z["evidence_tiers"] = np.where(z.acid_crc_de.eq(1), "CRC_chronic_acidosis_DE", "")
    return z.sort_values(["evidence_score","fdr"], ascending=[False,True])