forked from daviddaiweizhang/istar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenrich.py
133 lines (105 loc) · 3.66 KB
/
enrich.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import sys
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# from matplotlib.colors import LogNorm
import seaborn as sns
from utils import load_pickle, read_lines, save_tsv
def get_data(prefix0, prefix1):
labels0 = load_pickle(prefix0+'labels.pickle')
labels1 = load_pickle(prefix1+'labels.pickle')
labels1_names = read_lines(prefix1+'label-names.txt')
return labels0, labels1, labels1_names
def get_probs(labels0, labels1):
nlabs0 = labels0.max() + 1
nlabs1 = labels1.max() + 1
results = np.full((nlabs0, nlabs1), np.nan)
for l0 in range(nlabs0):
for l1 in range(nlabs1):
m0 = labels0 == l0
m1 = labels1 == l1
results[l0, l1] = (m0 * m1).sum()
results /= np.nansum(results)
return results
def plot_probs(df, filename, cmap='tab10'):
font = {'size': 15}
plt.rc('font', **font)
cmap = plt.get_cmap(cmap)
color = [cmap(i) for i in range(df.shape[1])]
df = df / np.nansum(df, 1, keepdims=True)
df.plot(kind='bar', stacked=True, color=color)
plt.xlabel('Cluster')
plt.ylabel('Cell type proportion')
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.savefig(filename, dpi=300, bbox_inches='tight')
plt.close()
print(filename)
def plot_results(df, filename, cmap='tab10'):
font = {'size': 15}
plt.rc('font', **font)
cmap = plt.get_cmap(cmap)
color = [cmap(i) for i in range(df.shape[1])]
df.plot(kind='bar', stacked=True, color=color)
plt.xlabel('Cluster')
plt.ylabel('Cell type proportion')
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.savefig(filename, dpi=300, bbox_inches='tight')
plt.close()
print(filename)
def probs_to_oddsratios(df):
x = df.to_numpy()
oddsratios = np.full_like(x, np.nan)
for i in range(x.shape[0]):
for j in range(x.shape[1]):
not_i = [k for k in range(x.shape[0]) if k != i]
not_j = [k for k in range(x.shape[1]) if k != j]
a = x[i, j] * x[not_i][:, not_j].sum()
b = x[i, not_j].sum() * x[not_i, j].sum()
oddsratios[i, j] = a / b
oddsratios = pd.DataFrame(oddsratios)
oddsratios.columns = df.columns
oddsratios.index = df.index
return oddsratios
def plot_enrichment(df, filename):
sns.heatmap(
df, cmap='magma',
annot=True, fmt='.1f', annot_kws={'fontsize': 12},
square=True, linewidth=0.5)
# # set x-ticks on top
# ax.set(xlabel='', ylabel='')
# ax.xaxis.tick_top()
plt.savefig(filename, dpi=300, bbox_inches='tight')
plt.close()
print(filename)
def process_oddsratios(df):
df = df.drop(columns='Unclassified')
x = df.to_numpy()
threshold = 2**0.05 # avoid OR == 0.0 after rounding
x[x < threshold] = np.nan
x = np.log2(x)
df[:] = x
df = df.T
return df
def main():
prefix0 = sys.argv[1] # e.g. 'data/her2st/H123/clusters-gene/'
prefix1 = sys.argv[2] # e.g. 'data/her2st/H123/markers/celltype/'
labels0, labels1, labels1_names = get_data(prefix0, prefix1)
probs = get_probs(labels0, labels1)
probs = pd.DataFrame(probs)
probs.columns = labels1_names
plot_probs(
probs,
cmap='tab10',
filename=prefix0+'proportions.png')
plot_probs(
probs,
cmap='Set3',
filename=prefix0+'proportions-altcmap.png')
oddsratios = probs_to_oddsratios(probs)
oddsratios = process_oddsratios(oddsratios)
save_tsv(
oddsratios, prefix0+'enrichment.csv',
sep=',', na_rep='NA')
plot_enrichment(oddsratios, prefix0+'enrichment.png')
if __name__ == '__main__':
main()