Skip to content

Commit 1aa9a95

Browse files
committed
added a user-provided custom LD reference option
1 parent 9e53fe0 commit 1aa9a95

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

slalom.py

+33-7
Original file line numberDiff line numberDiff line change
@@ -166,22 +166,36 @@ def main(args):
166166
df["lead_variant"].iloc[lead_idx_snp] = True
167167

168168
# annotate LD
169-
for pop in gnomad_pops["GRCh37"]:
170-
ht = hl.read_table(gnomad_ld_variant_indices[reference_genome].format(pop=pop))
169+
r2_label = "r2" if not args.export_r else "r"
170+
if args.ld_reference == "gnomad":
171+
ld_matrices = [
172+
f"gs://gcp-public-data--gnomad/release/2.1.1/ld/gnomad.genomes.r2.1.1.{pop}.common.ld.bm"
173+
for pop in gnomad_pops["GRCh37"]
174+
]
175+
ld_variant_indices = [
176+
gnomad_ld_variant_indices[reference_genome].format(pop=pop) for pop in gnomad_pops["GRCh37"]
177+
]
178+
ld_labels = [f"gnomad_lead_{r2_label}_{pop}" for pop in gnomad_pops["GRCh37"]]
179+
else:
180+
ld_matrices = [args.custom_ld_path]
181+
ld_variant_indices = [args.custom_ld_variant_index_path]
182+
ld_labels = [f"{args.custom_ld_label}_lead_{r2_label}"]
183+
184+
for ld_bm_path, ld_ht_path, col in zip(ld_matrices, ld_variant_indices, ld_labels):
185+
ht = hl.read_table(ld_ht_path)
171186
ht = ht_snp.join(ht, "inner")
172187
ht = ht.checkpoint(new_temp_file())
173188

174189
lead_idx = ht.filter(hl.variant_str(ht.locus, ht.alleles) == args.lead_variant).head(1).idx.collect()
175190

176191
if len(lead_idx) == 0:
177-
col = f"gnomad_lead_r2_{pop}" if not args.export_r else f"gnomad_lead_r_{pop}"
178192
df[col] = np.nan
179193
continue
180194

181195
idx = ht.idx.collect()
182196
idx2 = sorted(list(set(idx)))
183197

184-
bm = BlockMatrix.read(f"gs://gcp-public-data--gnomad/release/2.1.1/ld/gnomad.genomes.r2.1.1.{pop}.common.ld.bm")
198+
bm = BlockMatrix.read(ld_bm_path)
185199
bm = bm.filter(idx2, idx2)
186200
if not np.all(np.diff(idx) > 0):
187201
order = np.argsort(idx)
@@ -199,9 +213,6 @@ def main(args):
199213
r2 = bm.to_numpy()[0]
200214
if not args.export_r:
201215
r2 = r2 ** 2
202-
col = f"gnomad_lead_r2_{pop}"
203-
else:
204-
col = f"gnomad_lead_r_{pop}"
205216

206217
df[col] = np.nan
207218
df[col].iloc[idx_snp] = r2
@@ -224,6 +235,8 @@ def main(args):
224235
n_samples = np.array(n_samples).T
225236
ld = np.array(ld).T
226237
df["r"] = np.nansum(n_samples * ld, axis=1) / np.nansum(n_samples * ~np.isnan(ld), axis=1)
238+
elif args.ld_reference == "custom":
239+
df["r"] = df[ld_labels[0]]
227240
else:
228241
df["r"] = df["gnomad_lead_r_nfe"]
229242

@@ -294,6 +307,12 @@ def main(args):
294307
parser.add_argument("--align-alleles", action="store_true", help="Whether to align alleles with gnomAD")
295308
parser.add_argument("--annotate-consequence", action="store_true", help="Whether to annotate VEP consequences")
296309
parser.add_argument("--annotate-gnomad-freq", action="store_true", help="Whether to annotate gnomAD frequencies")
310+
parser.add_argument(
311+
"--ld-reference", type=str, default="gnomad", choices=["gnomad", "custom"], help="Choice of LD reference"
312+
)
313+
parser.add_argument("--custom-ld-path", type=str, help="Path of user-provided LD BlockMatrix")
314+
parser.add_argument("--custom-ld-variant-index-path", type=str, help="Path of user-provided LD variant index table")
315+
parser.add_argument("--custom-ld-label", type=str, help="Label of user-provided LD")
297316
parser.add_argument("--export-r", action="store_true", help="Export signed r values instead of r2")
298317
parser.add_argument("--weighted-average-r", type=str, nargs="+", action=ParseKwargs, help="")
299318
parser.add_argument("--dentist-s", action="store_true", help="Annotate DENTIST-S statistics")
@@ -323,4 +342,11 @@ def main(args):
323342
if args.out_summary is None:
324343
args.out_summary = f"{os.path.splitext(args.out)[0]}.summary.txt"
325344

345+
if args.ld_reference == "custom" and (
346+
(args.custom_ld_path is None) or (args.custom_ld_variant_index_path is None) or (args.custom_ld_label is None)
347+
):
348+
raise argparse.ArgumentError(
349+
"All of --custom-ld-path, --custom-ld-variant-index-path, and --custom-ld-label should be provided"
350+
)
351+
326352
main(args)

0 commit comments

Comments
 (0)