-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
117 lines (91 loc) · 3.34 KB
/
example.py
File metadata and controls
117 lines (91 loc) · 3.34 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
def main(dataset: str):
import numpy as np
import matplotlib.pyplot as plt
from switchtfi import (
fit_model,
calculate_weights,
compute_corrected_pvalues,
remove_insignificant_edges,
rank_tfs,
plot_regulon
)
from switchtfi.data import (
erythrocytes, preendocrine_beta, preendocrine_alpha,
erythrocytes_grn, preendocrine_beta_grn, preendocrine_alpha_grn,
)
# Load the preprocessed scRNA-seq data and GRN (previously inferred with the Scenic method)
if dataset == 'ery':
data = erythrocytes()
grn = erythrocytes_grn()
prog_off_annotations_key = 'prog_off'
elif dataset == 'beta':
data = preendocrine_beta()
grn = preendocrine_beta_grn()
prog_off_annotations_key = 'clusters'
else:
data = preendocrine_alpha()
grn = preendocrine_alpha_grn()
prog_off_annotations_key = 'clusters'
# ### Perform SwitchTFI analyses ### #
# - Compute weights and empirical corrected p-values for each edge in the input GRN
# - Prune the input GRN: remove edge if p-value > FWER-threshold ==> transition GRN
# - Rank transcription factors according to centrality in transition GRN
# Run fit function => transition GRN and ranked TFs
np.random.seed(42)
transition_grn, ranked_tfs = fit_model(
adata=data,
grn=grn,
layer_key='magic_imputed',
n_permutations=1000,
clustering_obs_key=prog_off_annotations_key,
)
print(f'# ### Transition GRN:\n{transition_grn}')
print(f'# ### Transition driver TFs (ranked by PageRank):\n{ranked_tfs}')
# ### The steps of SwitchTFI's analyses can be performed individually as well
# Note: Now the weighted outdegree is used for ranking the TFs in the transition GRN
np.random.seed(42)
grn_weighted = calculate_weights(
adata=data,
grn=grn,
layer_key='magic_imputed',
clustering_obs_key=prog_off_annotations_key
)
grn_weighted_with_pvals = compute_corrected_pvalues(
adata=data,
grn=grn_weighted,
n_permutations=1000,
clustering_obs_key=prog_off_annotations_key,
)
transition_grn = remove_insignificant_edges(grn=grn_weighted_with_pvals, alpha=0.05, inplace=False)
ranked_tfs = rank_tfs(
grn=transition_grn,
reverse=False,
centrality_measure='out_degree',
weight_key='score'
)
# Subset the GRN to the relevant columns
transition_grn = transition_grn[['TF', 'target', 'weight', 'pvals_wy']]
print(f'# ### Transition GRN:\n{transition_grn}')
print(f'# ### Transition driver TFs (ranked by weighted outdegree):\n{ranked_tfs}')
# Visualize the regulon of the top ranked TF
top_tf = ranked_tfs.loc[0, 'gene']
plot_regulon(
grn=transition_grn,
tf=top_tf,
sort_by='score',
top_k=10
)
plt.show()
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='Run SwitchTFI analysis on selected tissue.')
parser.add_argument(
'-d', '--dataset',
type=str,
choices=['ery', 'beta', 'alpha'],
default='ery',
help='Dataset to run the analysis on: "ery", "beta", or "alpha"'
)
args = parser.parse_args()
main(dataset=args.dataset)
print('done')