<jupyter_start><jupyter_text>Prepare TRRUST v2<jupyter_code>genes = anndata.read_h5ad(
    "s01_preprocessing/rna.h5ad", backed="r"
).var.query("dcq_highly_variable").index.to_numpy()
trrust = pd.read_table(
    "../../data/database/TRRUST-v2/trrust_rawdata.human.tsv",
    header=None, names=["tf", "target", "direction", "pmid"]
).query("direction != 'Repression'")
trrust.head()
tfs = np.loadtxt("s04_infer_gene_tf/tfs.txt", dtype=str)
tfs_used = np.intersect1d(tfs, trrust["tf"])
trrust = nx.from_pandas_edgelist(trrust, source="tf", target="target", create_using=nx.DiGraph)
trrust.number_of_edges()
trrust_mat = biadjacency_matrix(trrust, tfs_used, genes)
trrust_flag = trrust_mat.toarray().ravel()<jupyter_output><empty_output><jupyter_text>Fisher's test<jupyter_code>pvals = {}<jupyter_output><empty_output><jupyter_text>Distance<jupyter_code>dist_merged = nx.read_graphml("s04_infer_gene_tf/dist_merged.graphml.gz")
dist_mat = biadjacency_matrix(dist_merged, tfs_used, genes)
dist_flag = dist_mat.toarray().ravel()
dist_crosstab = pd.crosstab(
    pd.Series(dist_flag, name="dist"),
    pd.Series(trrust_flag, name="trrust")
).iloc[::-1, ::-1]
dist_crosstab
pvals["dist"] = scipy.stats.fisher_exact(dist_crosstab)[1]
pvals["dist"]<jupyter_output><empty_output><jupyter_text>pcHi-C<jupyter_code>pchic_merged = nx.read_graphml("s04_infer_gene_tf/pchic_merged.graphml.gz")
pchic_mat = biadjacency_matrix(pchic_merged, tfs_used, genes)
pchic_flag = pchic_mat.toarray().ravel()
pchic_crosstab = pd.crosstab(
    pd.Series(pchic_flag, name="pchic"),
    pd.Series(trrust_flag, name="trrust")
).iloc[::-1, ::-1]
pchic_crosstab
pvals["pchic"] = scipy.stats.fisher_exact(pchic_crosstab)[1]
pvals["pchic"]<jupyter_output><empty_output><jupyter_text>eQTL<jupyter_code>eqtl_merged = nx.read_graphml("s04_infer_gene_tf/eqtl_merged.graphml.gz")
eqtl_mat = biadjacency_matrix(eqtl_merged, tfs_used, genes)
eqtl_flag = eqtl_mat.toarray().ravel()
eqtl_crosstab = pd.crosstab(
    pd.Series(eqtl_flag, name="eqtl"),
    pd.Series(trrust_flag, name="trrust")
).iloc[::-1, ::-1]
eqtl_crosstab
pvals["eqtl"] = scipy.stats.fisher_exact(eqtl_crosstab)[1]
pvals["eqtl"]<jupyter_output><empty_output><jupyter_text>Correlation<jupyter_code>corr_merged = nx.read_graphml("s04_infer_gene_tf/corr_merged.graphml.gz")
corr_mat = biadjacency_matrix(corr_merged, tfs_used, genes)
corr_flag = corr_mat.toarray().ravel()
corr_crosstab = pd.crosstab(
    pd.Series(corr_flag, name="corr"),
    pd.Series(trrust_flag, name="trrust")
).iloc[::-1, ::-1]
corr_crosstab
pvals["corr"] = scipy.stats.fisher_exact(corr_crosstab)[1]
pvals["corr"]<jupyter_output><empty_output><jupyter_text>GLUE<jupyter_code>glue_merged = nx.read_graphml("s04_infer_gene_tf/glue_merged.graphml.gz")
glue_mat = biadjacency_matrix(glue_merged, tfs_used, genes)
glue_flag = glue_mat.toarray().ravel()
glue_crosstab = pd.crosstab(
    pd.Series(glue_flag, name="glue"),
    pd.Series(trrust_flag, name="trrust")
).iloc[::-1, ::-1]
glue_crosstab
tf_idx, gene_idx = np.divmod(
    np.where(np.logical_and(glue_flag, trrust_flag))[0], 6000
)
pd.DataFrame({
    "TF": tfs_used[tf_idx],
    "gene": genes[gene_idx]
})
pvals["glue"] = scipy.stats.fisher_exact(glue_crosstab)[1]
pvals["glue"]<jupyter_output><empty_output><jupyter_text>Plot comparison<jupyter_code>df = pd.DataFrame.from_dict(pvals, orient="index", columns=["pval"])
df.index.name = "conn"
df = df.reset_index()
df["conn"] = df["conn"].replace({
    "dist": "Distance", "pchic": "pcHi-C", "eqtl": "eQTL", "corr": "Correlation",
    "glue": "GLUE"
})
df["nlog10_pval"] = -np.log10(df["pval"])
ax = sns.barplot(x="conn", y="nlog10_pval", data=df, saturation=1.0)
ax.set_xlabel("Cis-regulatory region")
ax.set_ylabel("-log10 $P$-value")
for item in ax.get_xticklabels():
    item.set_rotation(67.5)
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
ax.get_figure().savefig(f"{PATH}/trrust_pval.pdf")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17526555386949924, 'fraction_numerical': 0.013151239251390997, 'mean_word_length': 8.071100917431192, 'pattern_counts': {'":': 7, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '285696', 'license': 'MIT', 'n_tokens_mistral': 1803, 'n_tokens_neox': 1703, 'n_words': 183, 'path': 'experiments/RegInf/s05_evaluate_gene_tf.ipynb', 'repo_name': 'gao-lab/GLUE', 'size': '3954'}