-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathqqplot.py
More file actions
executable file
·225 lines (194 loc) · 5.68 KB
/
qqplot.py
File metadata and controls
executable file
·225 lines (194 loc) · 5.68 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env python3
import sys
import argparse
import numpy as np
import matplotlib.pyplot as plt
from time import localtime, strftime
def _plot(x, y, ax=None, color=None, ablinecolor="r", alpha=0.8, **kwargs):
"""
Parameters
----------
x, y : array-like, x is the expected, y is the observed
ax : matplotlib axis, optional
color : the dots color in the plot, optional
kwargs : key, value pairings passed to ``plt.scatter()``
Returns
-------
ax : matplotlib Axes object with the plot.
"""
# Draw the plot and return the Axes
if ax is None:
_, ax = plt.subplots(figsize=(6, 6), facecolor="w", edgecolor="k")
# Get the color from the current color cycle
if color is None:
(line,) = ax.plot(0, x.mean())
color = line.get_color()
line.remove()
ax.scatter(x, y, c=color, alpha=alpha, edgecolors="none", **kwargs)
ax.set_xlim(xmin=x.min(), xmax=1.05 * x.max())
ax.set_ylim(ymin=y.min())
if ablinecolor:
# plot the y=x line
ax.plot(
[x.min(), ax.get_xlim()[1]],
[x.min(), ax.get_xlim()[1]],
color=ablinecolor,
linestyle="-",
)
return ax
def _group_bins(cutoff, bins):
if cutoff < 0 or cutoff > 1:
raise ValueError("`cutoff` must be a float in (0, 1).")
# bin: [pval, size]
allbins = [[-1, 0] for _ in range(bins)]
# keep all the low pvals, which may be significant
sigp = []
N = 0
c = -np.log10(cutoff)
sys.stdout.write(
strftime("%a, %d %b %Y %H:%M:%S", localtime())
+ f" : start to process the input!\n"
)
try:
for line in sys.stdin:
N += 1
if N % 100000000 == 0:
sys.stdout.write(
strftime("%a, %d %b %Y %H:%M:%S", localtime())
+ f" : reaching the {N}-th line.\n"
)
p = -np.log10(float(line))
if p > c:
sigp.append(p)
else:
# find the bin where the P should go
idx = bins - int(np.floor(p / c * bins)) - 1
idx = 0 if idx == -1 else idx
allbins[idx][0] = max(allbins[idx][0], p)
allbins[idx][1] += 1
except KeyboardInterrupt:
pass
sys.stdout.write(
strftime("%a, %d %b %Y %H:%M:%S", localtime())
+ f" : reached the end! the total number of lines is {N}!\n"
)
size = len(sigp)
obs = sorted(sigp, reverse=True)
exp = [-np.log10((i + 1 - 0.5) / N) for i in np.arange(size)]
for bi in allbins:
if bi[0] != -1:
obs.append(bi[0])
exp.append(-np.log10((bi[1] + size - 0.5) / N))
size += bi[1]
else:
pass
return np.array(obs), np.array(exp)
def qq(
x=None,
y=None,
figname=None,
cutoff=1e-4,
bins=1000,
ax=None,
title=None,
color=None,
alpha=0.8,
ablinecolor="r",
dpi=300,
xlabel=None,
ylabel=None,
**kwargs,
):
if x is None:
# read data from stdin
o, e = _group_bins(cutoff, bins)
else:
if y is None:
n = len(x)
a = 0.5
o = -np.log10(sorted(x))
e = -np.log10((np.arange(n, dtype=float) + 1 - a) / (n + 1 - 2 * a))
else:
o = x
e = y
ax = _plot(e, o, ax=ax, color=color, ablinecolor=ablinecolor, alpha=alpha, **kwargs)
if xlabel is None:
xlabel = r"Expected $-log_{10}{(P)}$"
if ylabel is None:
ylabel = r"Observed $-log_{10}{(P)}$"
ax.set_title(title)
ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)
plt.savefig(figname, bbox_inches="tight", dpi=dpi)
plt.clf()
plt.close()
return ax
def main():
parser = argparse.ArgumentParser(
description="QQ plot on the fly! only read data from the pipe!"
)
parser.add_argument(
"-cutoff",
metavar="FLOAT",
nargs="?",
default=1e-4,
type=float,
help="pval bigger than this cutoff will be grouped into bins. [%(default)s]",
)
parser.add_argument(
"-bins",
metavar="INT",
nargs="?",
default=1000,
type=int,
help="the number of bins to use. [%(default)s]",
)
parser.add_argument(
"-csv",
default=False,
action="store_true",
dest="csv",
help="read csv (two columns) from stdin. [%(default)s]",
)
parser.add_argument(
"-no",
default=False,
action="store_true",
dest="no",
help="no grouping. [%(default)s]",
)
parser.add_argument("-out", metavar="FILE", help="prefix of output files")
parser.add_argument("-title", metavar="STRING", help="title of the plot")
args = parser.parse_args()
assert args.out is not None, "please specify the file of output."
f, ax = plt.subplots(figsize=(6, 6), facecolor="w", edgecolor="k")
if args.csv:
import pandas as pd
d = pd.read_csv(sys.stdin, header=0, names=["o", "e"])
qq(
x=d["o"].values,
y=d["e"].values,
figname=args.out + ".png",
title=args.title,
ax=ax,
)
elif args.no:
o = list(map(float, filter(None, (row.rstrip() for row in sys.stdin))))
qq(
x = o,
figname=args.out + ".png",
cutoff=args.cutoff,
bins=args.bins,
title=args.title,
ax=ax,
)
else:
qq(
figname=args.out + ".png",
cutoff=args.cutoff,
bins=args.bins,
title=args.title,
ax=ax,
)
if __name__ == "__main__":
main()