-
Notifications
You must be signed in to change notification settings - Fork 16
/
compute_score.py
276 lines (246 loc) · 9.85 KB
/
compute_score.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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import scanpy as sc
from anndata import read_h5ad
import pandas as pd
import numpy as np
import scipy as sp
import os
import time
import argparse
from statsmodels.stats.multitest import multipletests
import scdrs
"""
# Fixit
# Todo
- Implement scdrs.method.proprocess to incorporate sparse_reg_out and sparse_compute_stats
- "gene_weight" argument needs to be tested
- Check the situation where df_cov does not explicitly contain "const" but contains a linear combinition of const
# Finished
- Implement a memory efficient version
- Add --n_ctrl (default value 500)
- Add --cov_file option to regress out covariates stored in COV_FILE before feeding into the score function
- Add --ctrl_match_opt='mean_var': use mean- and var- matched control genes
- Change name from scTRS to scdrs (072721)
- Fixed: Warning for compute_score: Trying to set attribute `.X` of view, copying. (did: v_norm_score = v_raw_score.copy())
"""
VERSION = "0.0.1"
VERSION = "beta"
def convert_species_name(species):
if species in ["Mouse", "mouse", "Mus_musculus", "mus_musculus", "mmusculus"]:
return "mmusculus"
if species in ["Human", "human", "Homo_sapiens", "homo_sapiens", "hsapiens"]:
return "hsapiens"
raise ValueError("# compute_score: species name %s not supported" % species)
def main(args):
sys_start_time = time.time()
MASTHEAD = "******************************************************************************\n"
MASTHEAD += "* Single-cell disease relevance score (scDRS)\n"
MASTHEAD += "* Version %s\n" % VERSION
MASTHEAD += "* Martin Jinye Zhang and Kangcheng Hou\n"
MASTHEAD += "* HSPH / Broad Institute / UCLA\n"
MASTHEAD += "* MIT License\n"
MASTHEAD += "******************************************************************************\n"
###########################################################################################
###### Parse Options ######
###########################################################################################
H5AD_FILE = args.h5ad_file
H5AD_SPECIES = args.h5ad_species
COV_FILE = args.cov_file
GS_FILE = args.gs_file
GS_SPECIES = args.gs_species
CTRL_MATCH_OPT = args.ctrl_match_opt
WEIGHT_OPT = args.weight_opt
FLAG_FILTER = args.flag_filter == "True"
FLAG_RAW_COUNT = args.flag_raw_count == "True"
N_CTRL = int(args.n_ctrl)
FLAG_RETURN_CTRL_RAW_SCORE = args.flag_return_ctrl_raw_score == "True"
FLAG_RETURN_CTRL_NORM_SCORE = args.flag_return_ctrl_norm_score == "True"
FLAG_SPARSE = args.flag_sparse == "True"
OUT_FOLDER = args.out_folder
if H5AD_SPECIES != GS_SPECIES:
H5AD_SPECIES = convert_species_name(H5AD_SPECIES)
GS_SPECIES = convert_species_name(GS_SPECIES)
header = MASTHEAD
header += "Call: ./compute_score.py \\\n"
header += "--h5ad_file %s\\\n" % H5AD_FILE
header += "--h5ad_species %s\\\n" % H5AD_SPECIES
header += "--cov_file %s\\\n" % COV_FILE
header += "--gs_file %s\\\n" % GS_FILE
header += "--gs_species %s\\\n" % GS_SPECIES
header += "--ctrl_match_opt %s\\\n" % CTRL_MATCH_OPT
header += "--weight_opt %s\\\n" % WEIGHT_OPT
header += "--flag_filter %s\\\n" % FLAG_FILTER
header += "--flag_raw_count %s\\\n" % FLAG_RAW_COUNT
header += "--n_ctrl %d\\\n" % N_CTRL
header += "--flag_return_ctrl_raw_score %s\\\n" % FLAG_RETURN_CTRL_RAW_SCORE
header += "--flag_return_ctrl_norm_score %s\\\n" % FLAG_RETURN_CTRL_NORM_SCORE
header += "--out_folder %s\n" % OUT_FOLDER
print(header)
# Check options
if H5AD_SPECIES != GS_SPECIES:
if H5AD_SPECIES not in ["mmusculus", "hsapiens"]:
raise ValueError(
"--h5ad_species needs to be one of [mmusculus, hsapiens] "
"unless --h5ad_species==--gs_species"
)
if GS_SPECIES not in ["mmusculus", "hsapiens"]:
raise ValueError(
"--gs_species needs to be one of [mmusculus, hsapiens] "
"unless --h5ad_species==--gs_species"
)
if CTRL_MATCH_OPT not in ["mean", "mean_var"]:
raise ValueError("--ctrl_match_opt needs to be one of [mean, mean_var]")
if WEIGHT_OPT not in ["uniform", "vs", "inv_std", "od"]:
raise ValueError("--weight_opt needs to be one of [uniform, vs, inv_std, od]")
###########################################################################################
###### Load data ######
###########################################################################################
print("Load data:")
# Load .h5ad file
adata = read_h5ad(H5AD_FILE)
if FLAG_FILTER:
sc.pp.filter_cells(adata, min_genes=250)
sc.pp.filter_genes(adata, min_cells=50)
if FLAG_RAW_COUNT:
sc.pp.normalize_per_cell(adata, counts_per_cell_after=1e4)
sc.pp.log1p(adata)
print(
"--h5ad_file loaded: n_cell=%d, n_gene=%d (sys_time=%0.1fs)"
% (adata.shape[0], adata.shape[1], time.time() - sys_start_time)
)
# adata = adata[0:500,:].copy()
# Load .cov file and regress out covariates
if COV_FILE is not None:
df_cov = pd.read_csv(COV_FILE, sep="\t", index_col=0)
else:
df_cov = None
# Load .gs file, convert species if needed and merge with adata.var_names
dict_gs = scdrs.util.load_gs(
GS_FILE,
src_species=GS_SPECIES,
dst_species=H5AD_SPECIES,
to_intersect=adata.var_names,
)
print(
"--gs_file loaded: n_geneset=%d (sys_time=%0.1fs)"
% (len(dict_gs), time.time() - sys_start_time)
)
###########################################################################################
###### Computation ######
###########################################################################################
# Preprocess
scdrs.preprocess(adata, cov=df_cov, n_mean_bin=20, n_var_bin=20, copy=False)
# Compute score
print("Compute score:")
for trait in dict_gs:
gene_list, gene_weights = dict_gs[trait]
if len(gene_list) < 10:
print(
"trait=%s: skipped due to small size (n_gene=%d, sys_time=%0.1fs)"
% (trait, len(gene_list), time.time() - sys_start_time)
)
continue
df_res = scdrs.score_cell(
adata,
gene_list,
gene_weight=gene_weights,
ctrl_match_key=CTRL_MATCH_OPT,
n_ctrl=N_CTRL,
weight_opt=WEIGHT_OPT,
return_ctrl_raw_score=FLAG_RETURN_CTRL_RAW_SCORE,
return_ctrl_norm_score=FLAG_RETURN_CTRL_NORM_SCORE,
verbose=False,
)
df_res.iloc[:, 0:6].to_csv(
os.path.join(OUT_FOLDER, "%s.score.gz" % trait),
sep="\t",
index=True,
compression="gzip",
)
if FLAG_RETURN_CTRL_RAW_SCORE | FLAG_RETURN_CTRL_NORM_SCORE:
df_res.to_csv(
os.path.join(OUT_FOLDER, "%s.full_score.gz" % trait),
sep="\t",
index=True,
compression="gzip",
)
v_fdr = multipletests(df_res["pval"].values, method="fdr_bh")[1]
n_rej_01 = (v_fdr < 0.1).sum()
n_rej_02 = (v_fdr < 0.2).sum()
print(
"Gene set %s (n_gene=%d): %d/%d FDR<0.1 cells, %d/%d FDR<0.2 cells (sys_time=%0.1fs)"
% (
trait,
len(gene_list),
n_rej_01,
df_res.shape[0],
n_rej_02,
df_res.shape[0],
time.time() - sys_start_time,
)
)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="compute score")
parser.add_argument("--h5ad_file", type=str, required=True)
parser.add_argument(
"--h5ad_species", type=str, required=True, help="one of [hsapiens, mmusculus]"
)
parser.add_argument("--cov_file", type=str, required=False, default=None)
parser.add_argument("--gs_file", type=str, required=True)
parser.add_argument(
"--gs_species", type=str, required=True, help="one of [hsapiens, mmusculus]"
)
parser.add_argument(
"--ctrl_match_opt", type=str, required=False, default="mean_var"
)
parser.add_argument("--weight_opt", type=str, required=False, default="vs")
parser.add_argument(
"--flag_sparse",
type=str,
required=False,
default="False",
help="If to use a sparse implementation, which leverages the sparsity of the data."
"The appropriate usage place would be a highly sparse count matrix in adata, and one need to correct for covarates",
)
parser.add_argument(
"--flag_filter",
type=str,
required=False,
default="True",
help="If to apply cell and gene filters to the h5ad_file data",
)
parser.add_argument(
"--flag_raw_count",
type=str,
required=False,
default="True",
help="If True, apply size factor normalization and log1p transformation",
)
parser.add_argument(
"--n_ctrl",
type=int,
required=False,
default=1000,
help="Number of control genes",
)
parser.add_argument(
"--flag_return_ctrl_raw_score",
type=str,
required=False,
default="False",
help="If True, return raw control scores",
)
parser.add_argument(
"--flag_return_ctrl_norm_score",
type=str,
required=False,
default="False",
help="If True, return normalized control scores",
)
parser.add_argument(
"--out_folder",
type=str,
required=True,
help="Save file at out_folder/trait.score.gz",
)
args = parser.parse_args()
main(args)