-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrn.py
79 lines (68 loc) · 2.7 KB
/
grn.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import pandas as pd
import numpy as np
import scipy.stats as stats
from statsmodels.sandbox.stats.multicomp import multipletests
def reconstructNet(
net: pd.DataFrame,
alpha: float = 0.05,
apval: float | None = None,
method: str = "fdr_bh",
count: int | None = None,
inplace: bool = False,
) -> pd.DataFrame:
"""
Seralize and filter the TE matrix into a GRN.
@params te: the TE matrix.
@params alpha: the alpha value for false positive correction.
@params apval: the adjusted p-value threshold.
@params method: the method of false positive correction.
@params count: the number of significant edges to be returned.
"""
if not inplace:
net = net.copy()
relationship = net.to_numpy().flatten()
source = net.index.repeat(len(net)).to_list()
target = net.index.to_list() * len(net)
regNet = pd.DataFrame(dict(source=source, relationship=relationship, target=target))
regNet = regNet[regNet.source != regNet.target]
mean, std = regNet.relationship.mean(), regNet.relationship.std()
regNet["tstat"] = (regNet.relationship - mean) / std
regNet["pval"] = 1 - stats.norm.cdf(regNet.tstat.astype(float))
regNet["adj_pval"] = multipletests(regNet.pval, alpha=alpha, method=method)[1]
regNet = regNet.sort_values("adj_pval")
if apval is not None:
regNet = regNet[regNet.adj_pval < apval]
if count is not None:
regNet = regNet.head(count)
return regNet
def trimIndirect(
net: pd.DataFrame, threshold: float = 0, depth: int = 1, inplace: bool = False
) -> pd.DataFrame:
"""
Trim the indirect edges of a network.
@params net: the filtered and Seralized network.
@params threshold: the threshold of indirect edges.
@params depth: the depth of indirect edges.
"""
assert depth == 1, "Currently only supports depth=1"
if not inplace:
net = net.copy()
net = net.sort_values("relationship")
net['to_drop'] = False
for idx, interaction in net.iterrows():
ingress = net[net.source == interaction.source].drop(index=idx)
egress = net[net.target == interaction.target].drop(index=idx)
if not ingress.empty and not egress.empty:
paths = pd.merge(
ingress,
egress,
how="inner",
left_on="target",
right_on="source",
suffixes=("_i", "_e"),
)
paths['relationship_min'] = paths[['relationship_i', 'relationship_e']].min(axis=1)
if not paths[interaction.relationship < paths.relationship_min + threshold].empty:
net.loc[idx, 'to_drop'] = True
net = net[~net.to_drop]
return net.drop(columns=['to_drop'])