Repository navigation
Expand file tree
/
Copy pathSnakefile
More file actions
417 lines (375 loc) · 16.3 KB
/
Copy pathSnakefile
File metadata and controls
417 lines (375 loc) · 16.3 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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import os
import shlex
import numpy as np
import pandas as pd
import subprocess
import sys
os.chdir('pipeline_steps')
TEST_RUN = os.getenv('TEST_RUN')
DATA_DIR = os.getenv('DATA_DIR')
RESULTS_DIR = os.getenv('RESULTS_DIR')
PIPELINE_VERSION = os.getenv('PIPELINE_VERSION') or 'current'
BLOCK_LIST = os.getenv('BLOCK_LIST') or ''
ALLOW_LIST = os.getenv('ALLOW_LIST') or ''
PIPELINE_METADATA = DATA_DIR + 'pipeline_metadata/'
STUDY_DIR = DATA_DIR + 'study/'
LD_BLOCK_DATA_DIR = DATA_DIR + 'ld_blocks/'
version_results_dir = os.path.join(RESULTS_DIR.rstrip(os.sep), PIPELINE_VERSION)
STATIC_WEB_DIR = os.path.join(version_results_dir, 'static_web') + os.sep
### INPUT DATA FILES
studies_to_process_file = PIPELINE_METADATA + 'studies_to_process.tsv'
studies_to_process = pd.read_csv(
studies_to_process_file ,
sep='\t',
dtype={'cell_type': str, 'ensg': str}
)
if len(studies_to_process) == 0:
print('No studies to process, exiting.')
sys.exit()
if TEST_RUN:
ld_blocks = pd.read_csv('../tests/data/ld_blocks.tsv', sep='\t')
else:
ld_blocks = pd.read_csv('data/ld_blocks.tsv', sep='\t')
relevant_ancestries = np.isin(ld_blocks['ancestry'], studies_to_process['ancestry'].unique())
ld_blocks = ld_blocks[relevant_ancestries]
ld_blocks = [f'{ld.ancestry}/{ld.chr}/{ld.start}-{ld.stop}' for i, ld in ld_blocks.iterrows()]
extracted_studies = [s["extracted_location"] for i,s in studies_to_process.iterrows()]
extracted_study_pattern = '{study_location}extracted_snps.tsv'
standardisation_pattern = LD_BLOCK_DATA_DIR + '{ld_block}/standardisation_complete'
imputation_pattern = LD_BLOCK_DATA_DIR + '{ld_block}/imputation_complete'
finemapping_pattern = LD_BLOCK_DATA_DIR + '{ld_block}/finemapping_complete'
coloc_pattern = LD_BLOCK_DATA_DIR + '{ld_block}/coloc_complete'
clustering_pattern = LD_BLOCK_DATA_DIR + '{ld_block}/clustering_complete'
compare_rare_pattern = LD_BLOCK_DATA_DIR + '{ld_block}/compare_rare_complete'
### OUTPUT DATA FILES
studies_processed_file = RESULTS_DIR + 'latest/studies_processed.tsv.gz'
traits_processed_file = RESULTS_DIR + 'latest/traits_processed.tsv.gz'
ld_blocks_to_process = f'{PIPELINE_METADATA}updated_ld_blocks_to_colocalise.tsv'
def _under_version(*parts):
return os.path.join(version_results_dir, *parts)
coloc_pairwise_results = _under_version('coloc_pairwise_results.tsv.gz')
coloc_clustered_results = _under_version('coloc_clustered_results.tsv.gz')
rare_results = _under_version('rare_results.tsv.gz')
study_extractions = _under_version('study_extractions.tsv.gz')
new_studies_processed = _under_version('studies_processed.tsv.gz')
new_traits_processed = _under_version('traits_processed.tsv.gz')
static_web_files_ready_file = os.path.join(STATIC_WEB_DIR.rstrip(os.sep), 'static_web_files_ready')
studies_db_file = _under_version('studies.db')
associations_full_db_file = _under_version('associations_full.db')
associations_specific_db_file = _under_version('associations.db')
coloc_pairs_full_db_file = _under_version('coloc_pairs_full.db')
coloc_pairs_significant_db_file = _under_version('coloc_pairs.db')
ld_db_file = _under_version('ld.db')
gwas_upload_db_file = _under_version('gwas_upload.db')
create_dbs_done_file = _under_version('create_dbs_done')
latest_results_dir = os.path.join(RESULTS_DIR.rstrip(os.sep), 'latest')
latest_results_updated_file = _under_version('latest_results_updated')
backup_done_file = _under_version('backup_done')
sync_done_file = _under_version('sync_done')
block_list_arg = f' --block_list {shlex.quote(BLOCK_LIST)}' if BLOCK_LIST else ''
# allow list only cuts the compiled release; per-block steps keep every partner study in view
allow_list_arg = f' --allow_list {shlex.quote(ALLOW_LIST)}' if ALLOW_LIST else ''
# only the main (current) release updates latest/, which compile_results reads as its base
latest_results_targets = [latest_results_updated_file] if PIPELINE_VERSION == 'current' else []
rule all:
input: expand(extracted_study_pattern, study_location=extracted_studies),
ld_blocks_to_process,
expand(imputation_pattern, ld_block=ld_blocks),
expand(finemapping_pattern, ld_block=ld_blocks),
expand(coloc_pattern, ld_block=ld_blocks),
expand(clustering_pattern, ld_block=ld_blocks),
expand(compare_rare_pattern, ld_block=ld_blocks),
coloc_pairwise_results,
coloc_clustered_results,
rare_results,
study_extractions,
new_studies_processed,
new_traits_processed,
studies_db_file,
associations_full_db_file,
associations_specific_db_file,
coloc_pairs_full_db_file,
coloc_pairs_significant_db_file,
ld_db_file,
gwas_upload_db_file,
create_dbs_done_file,
static_web_files_ready_file,
latest_results_targets
rule extract_regions_from_studies:
params: lambda wildcards: list(filter(bool, wildcards.study_location.split("/")))[-1]
output: extracted_study_pattern
threads: 2
retries: 1
run:
study = studies_to_process[studies_to_process.study_name == str(params)]
if (len(study) != 1): raise ValueError(f'More than 1 study found for {str(params)}')
study = study.iloc[0]
if study.data_format == 'opengwas':
command = f'Rscript extract_regions_from_opengwas.R \
--extracted_study_location {study.extracted_location} \
--extracted_output_file {output}'
elif study.data_format == 'besd':
command = f'Rscript extract_regions_from_besd.R \
--extracted_study_location {study.extracted_location} \
--extracted_output_file {output}'
elif study.data_format == 'tsv' and study.variant_type != 'common':
command = f'Rscript extract_regions_from_rare_tsv.R \
--extracted_study_location {study.extracted_location} \
--extracted_output_file {output}'
elif study.data_format == 'summary_stats':
command = f'Rscript extract_regions_from_summary_stats.R \
--extracted_study_location {study.extracted_location} \
--extracted_output_file {output}'
else:
raise ValueError(f'Cant ingest unknown data format: {study.data_format}')
subprocess.run(command, shell=True)
rule organise_extracted_studies_into_ld_blocks:
input: expand(extracted_study_pattern, study_location=extracted_studies)
output: temporary(ld_blocks_to_process)
threads: 1
shell:
"""
Rscript organise_extracted_regions_into_ld_blocks.R --output_file {output}
"""
rule standardise_rule:
name: f'standardise_per_ld_block'
input: ld_blocks_to_process
output: temporary(standardisation_pattern)
retries: 1
threads: 1
params:
ld_dir=lambda wildcards, output: os.path.dirname(output[0])
run:
ld_blocks = pd.read_csv(ld_blocks_to_process, sep='\t')
skip_block = len(ld_blocks[ld_blocks.data_dir == params.ld_dir]) == 0
if skip_block:
command = f"mkdir -p $(dirname {output}) && touch {output}"
else:
ld_block = params.ld_dir.replace(LD_BLOCK_DATA_DIR, '')
command = f"Rscript standardise_studies_in_ld_block.R \
--ld_block {ld_block} \
--completed_output_file {output} \
{block_list_arg}"
subprocess.run(command, shell=True)
rule:
name: 'impute_per_ld_block'
input: standardisation_pattern
output: temporary(imputation_pattern)
retries: 1
threads: 5
params:
ld_dir=lambda wildcards, output: os.path.dirname(output[0])
run:
ld_blocks = pd.read_csv(ld_blocks_to_process, sep='\t')
skip_block = len(ld_blocks[ld_blocks.data_dir == params.ld_dir]) == 0
if skip_block:
command = f"mkdir -p $(dirname {output}) && touch {output}"
else:
ld_block = params.ld_dir.replace(LD_BLOCK_DATA_DIR, '')
command = f"Rscript impute_studies_in_ld_block.R \
--ld_block {ld_block} \
--completed_output_file {output} \
{block_list_arg}"
subprocess.run(command, shell=True)
rule finemap_rule:
name: f'finemap_per_ld_block'
input: imputation_pattern
output: temporary(finemapping_pattern)
retries: 1
threads: 3
params:
ld_dir=lambda wildcards, output: os.path.dirname(output[0])
run:
ld_blocks = pd.read_csv(ld_blocks_to_process, sep='\t')
skip_block = len(ld_blocks[ld_blocks.data_dir == params.ld_dir]) == 0
if skip_block:
command = f"mkdir -p $(dirname {output}) && touch {output}"
else:
ld_block = params.ld_dir.replace(LD_BLOCK_DATA_DIR, '')
command = f"Rscript finemap_studies_in_ld_block.R \
--ld_block {ld_block} \
--completed_output_file {output} \
{block_list_arg}"
subprocess.run(command, shell=True)
rule coloc_rule:
name: f'coloc_per_ld_block'
retries: 2
threads: 2
input:
finemap = finemapping_pattern
output: temporary(coloc_pattern)
params:
ld_dir=lambda wildcards, output: os.path.dirname(output[0])
run:
ld_blocks = pd.read_csv(ld_blocks_to_process, sep='\t')
skip_block = len(ld_blocks[ld_blocks.data_dir == params.ld_dir]) == 0
if skip_block:
command = f"mkdir -p $(dirname {output}) && touch {output}"
else:
ld_block = params.ld_dir.replace(LD_BLOCK_DATA_DIR, '')
command = f"Rscript coloc_studies_in_ld_block.R \
--ld_block {ld_block} \
--completed_output_file {output} \
{block_list_arg}"
subprocess.run(command, shell=True)
# rule calculate_global_bfdr:
# name: 'calculate_global_bfdr'
# input: expand(coloc_pattern, ld_block=ld_blocks)
# output:
# global_bfdr_tsv=global_bfdr_file,
# global_bfdr_complete=temporary(global_bfdr_complete_file)
# threads: 1
# params:
# block_list_arg=block_list_arg,
# shell:
# """
# Rscript calculate_global_bfdr.R \
# --output_file {output.global_bfdr_tsv} \
# --completed_output_file {output.global_bfdr_complete} \
# {params.block_list_arg}
# """
rule clustering_rule:
name: f'cluster_per_ld_block'
retries: 1
threads: 2
input:
coloc = coloc_pattern
output: temporary(clustering_pattern)
params:
ld_dir=lambda wildcards, output: os.path.dirname(output[0])
run:
ld_blocks = pd.read_csv(ld_blocks_to_process, sep='\t')
skip_block = len(ld_blocks[ld_blocks.data_dir == params.ld_dir]) == 0
if skip_block:
command = f"mkdir -p $(dirname {output}) && touch {output}"
else:
ld_block = params.ld_dir.replace(LD_BLOCK_DATA_DIR, '')
command = f"Rscript cluster_studies_in_ld_block.R \
--ld_block {ld_block} \
--completed_output_file {output} \
{block_list_arg}"
subprocess.run(command, shell=True)
rule compare_rare_rule:
name: f'compare_rare_per_ld_block'
retries: 2
threads: 3
input:
standardisation = standardisation_pattern
output: temporary(compare_rare_pattern)
params:
ld_dir=lambda wildcards, output: os.path.dirname(output[0])
run:
ld_blocks = pd.read_csv(ld_blocks_to_process, sep='\t')
skip_block = len(ld_blocks[ld_blocks.data_dir == params.ld_dir]) == 0
if skip_block:
command = f"mkdir -p $(dirname {output}) && touch {output}"
else:
ld_block = params.ld_dir.replace(LD_BLOCK_DATA_DIR, '')
command = f"Rscript compare_rare_studies_in_ld_block.R \
--ld_block {ld_block} \
--completed_output_file {output}"
subprocess.run(command, shell=True)
rule compile_results:
input: expand(clustering_pattern, ld_block=ld_blocks), expand(compare_rare_pattern, ld_block=ld_blocks)
threads: 1
params:
block_list_arg=block_list_arg,
allow_list_arg=allow_list_arg,
output:
coloc_pairwise_results = coloc_pairwise_results,
coloc_clustered_results = coloc_clustered_results,
rare_results = rare_results,
study_extractions = study_extractions,
new_studies_processed = new_studies_processed,
new_traits_processed = new_traits_processed
shell:
"""
mkdir -p $(dirname {output.coloc_pairwise_results})
Rscript compile_results.R \
--studies_to_process {studies_to_process_file} \
--studies_processed {studies_processed_file} \
--traits_processed {traits_processed_file} \
--new_studies_processed_file {output.new_studies_processed} \
--new_traits_processed_file {output.new_traits_processed} \
--study_extractions_file {output.study_extractions} \
--coloc_pairwise_results_file {output.coloc_pairwise_results} \
--coloc_clustered_results_file {output.coloc_clustered_results} \
--rare_results_file {output.rare_results} \
{params.block_list_arg} \
{params.allow_list_arg}
# rsync -Lavzh $RESULTS_DIR $BACKUP_DIR/results/ --exclude=".*"
"""
# rule backup_data_dir:
# input: coloc_pairwise_results, coloc_clustered_results, rare_results, study_extractions
# threads: 1
# output: temporary(backup_done_file)
# shell:
# """
# rsync -Lavzh --exclude='*cached*' $DATA_DIR/ld_blocks $BACKUP_DIR/data/
# rsync -Lavzh $DATA_DIR/ld_reference_panel_hg38 $BACKUP_DIR/data/
# rsync -Lavzh --ignore-missing-args $DATA_DIR/study $BACKUP_DIR/data/
# touch {output}
# """
rule create_results_db:
input: coloc_pairwise_results, coloc_clustered_results, rare_results, study_extractions, new_studies_processed, new_traits_processed
threads: 1
output:
studies_db_file = studies_db_file,
associations_full_db_file = associations_full_db_file,
associations_specific_db_file = associations_specific_db_file,
coloc_pairs_full_db_file = coloc_pairs_full_db_file,
coloc_pairs_significant_db_file = coloc_pairs_significant_db_file,
ld_db_file = ld_db_file,
gwas_upload_db_file = gwas_upload_db_file,
create_dbs_done_file = temporary(create_dbs_done_file)
shell:
"""
Rscript create_db_from_results.R \
--results_dir {version_results_dir} \
--studies_db_file {studies_db_file} \
--associations_full_db_file {associations_full_db_file} \
--associations_specific_db_file {associations_specific_db_file} \
--coloc_pairs_full_db_file {coloc_pairs_full_db_file} \
--coloc_pairs_significant_db_file {coloc_pairs_significant_db_file} \
--ld_db_file {ld_db_file} \
--gwas_upload_db_file {gwas_upload_db_file} \
--completed_output_file {output.create_dbs_done_file}
"""
rule create_static_web_files:
input: studies_db_file, coloc_pairs_significant_db_file, associations_full_db_file, associations_specific_db_file, create_dbs_done_file
threads: 1
params:
static_web_dir=STATIC_WEB_DIR,
results_dir=version_results_dir,
output: temporary(static_web_files_ready_file)
shell:
"""
Rscript create_static_web_files.R \
--studies_db_file {studies_db_file} \
--results_dir {params.results_dir} \
--static_web_dir {params.static_web_dir} \
--static_web_files_ready_file {output}
"""
# compile_results reads studies_processed and traits_processed from latest/, so only copy once everything else succeeded
rule update_latest_results:
input:
results = [coloc_pairwise_results, coloc_clustered_results, rare_results, study_extractions, new_studies_processed, new_traits_processed],
static_web_files_ready = static_web_files_ready_file
threads: 1
params:
latest_results_dir=latest_results_dir,
output: temporary(latest_results_updated_file)
shell:
"""
mkdir -p {params.latest_results_dir}
cp {input.results} {params.latest_results_dir}/
touch {output}
"""
onsuccess:
print('Yay! Please look here:')
print(coloc_pairwise_results)
print(coloc_clustered_results)
print(rare_results)
print(study_extractions)
onerror:
print(':(')