-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnakefile
328 lines (289 loc) · 12.5 KB
/
Snakefile
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
import glob
import pandas as pd
configfile: "config.yaml"
target_functions = {}
project_cohorts = {}
project_genome = {}
maf_colnames = {}
newline_re = re.compile(r'\s*\n\s*')
def replace_newlines(input_string):
return newline_re.sub(' ', input_string)
PROJECT_ROOT = config["PROJECT_ROOT"]
WTSI_ITERATIONS_PER_CORE = config["WTSI_ITERATIONS_PER_CORE"]
WTSI_CORES = config["WTSI_CORES"]
WTSI_NSIG_RANGE = config["WTSI_NSIG_RANGE"]
REFERENCE_SIGNATURE_PATH = config["REFERENCE_SIGNATURE_PATH"]
PROJECTS = config["PROJECT_NAMES"]
PROJECT_GENOMES = config["PROJECT_GENOMES"]
MAF_COLNAMES = config["MAF_COLNAMES"]
matlab_source_paths = '''
addpath('scripts/pipelines/wtsi-signatures');
addpath('scripts/pipelines/wtsi-signatures/custom');
addpath('scripts/pipelines/wtsi-signatures/source');
'''
def glob_to_df(glob_string, regex, column_names):
regex_obj = re.compile(regex)
glob_output = glob.iglob(glob_string)
regex_searches = (regex_obj.search(s) for s in glob_output)
regex_matches = [[r.group(i) for i in range(1, len(column_names)+1)] for r in regex_searches]
df = pd.DataFrame(
regex_matches
)
df = df.iloc[:, range(len(column_names))]
df.columns = column_names
return df
def get_snv_targets(wildcards):
df = glob_to_df(
'data/{project}/{method}/cohorts/{cohort}/snv/*.maf'.format(
method = wildcards.method,
cohort = wildcards.cohort,
project = wildcards.project
),
r'data\/' + wildcards.project + '\/' + wildcards.method + '\/cohorts\/' + wildcards.cohort + '\/snv\/(.*?).maf',
['samples']
)
return df
def get_aggregation_targets(wildcards):
''' Receives the following wildcards:
- project
- cohort
- mutation_type
- method
'''
targets_df = get_snv_targets(wildcards)
targets = 'output/' + \
wildcards.project + '/' + \
wildcards.method + '/cohorts/' + \
wildcards.cohort + '/' + \
wildcards.mutation_type + '/catalog/' + \
targets_df.samples + '.tsv'
return targets
def get_individual_aggregation_targets(wildcards):
target_loading_function = \
target_functions[wildcards.project][wildcards.mutation_type]
targets_df = target_loading_function(wildcards)
targets = 'output/' + \
wildcards.project + '/' + \
wildcards.method + '/cohorts/' + \
wildcards.cohort + '/signatures/' + \
wildcards.mutation_type + '/' + \
wildcards.signature_method + '/' + \
wildcards.summary_type + '/' + \
targets_df.samples + '.tsv'
return targets
## Create all outputs
project_outs = expand('output/{project}/genome/cohorts/{{cohort}}/signatures/snv/signature_report.html', project = PROJECTS)
#print(PROJECTS)
inputs = []
for project in PROJECTS:
print(project)
print(config["ANALYSIS_COHORTS"])
# print(expand('output/{project}/genome/cohorts/{cohort}/signatures/snv/signature_report.html', cohort = config["ANALYSIS_COHORTS"][project], project = project))
inputs.extend(expand('output/{project}/genome/cohorts/{cohort}/signatures/snv/signature_report.html', cohort = config["ANALYSIS_COHORTS"][project], project = project))
#print(inputs)
## Edit the below ALLCAPS stuff to match your data
rule all:
input:
inputs
########################################
### Aggregation of Mutation Catalogs ###
########################################
rule snv_catalogs:
input:
'data/{project}/{method}/cohorts/{cohort}/snv/{sample}.maf'
output:
'output/{project}/{method}/cohorts/{cohort}/snv/catalog/{sample}.tsv'
params:
genome = lambda wildcards: PROJECT_GENOMES[wildcards.project],
colnames = lambda wildcards: MAF_COLNAMES[wildcards.project]
shell:
'Rscript scripts/pipelines/nnls-signatures/mutation_catalog.R -t {input} -o {output} --colnames {params.colnames} --genome {params.genome}'
rule catalog_paths:
input:
get_aggregation_targets
output:
'output/{project}/{method}/cohorts/{cohort}/signatures/{mutation_type}/catalog_paths.tsv'
wildcard_constraints:
summary_type='(summary|summary_fraction)'
run:
f = open(output[0], 'w')
f.write('\n'.join(input))
f.close()
rule aggregate_snv_catalogs:
input:
'output/{project}/{method}/cohorts/{cohort}/signatures/snv/catalog_paths.tsv'
output:
'output/{project}/{method}/cohorts/{cohort}/signatures/snv/catalogs.tsv'
shell:
'Rscript scripts/pipelines/basic-functions/row_bind_tables.R -i {input} -o {output}'
########################################
### WTSI Mutation Signature Pipeline ###
########################################
rule create_wtsi_input:
input:
'output/{project}/{method}/cohorts/{cohort}/signatures/{mutation_type}/catalogs.tsv'
output:
'output/{project}/{method}/cohorts/{cohort}/signatures/{mutation_type}/wtsi/input.mat'
shell:
replace_newlines('''Rscript scripts/pipelines/wtsi-signatures/catalog_tidy_to_mat.R
-i {input}
-o {output}
-s {wildcards.project}_{wildcards.method}_{wildcards.cohort}
-c paths
''')
rule prepare_wtsi_iterations:
input:
'output/{project}/{method}/cohorts/{cohort}/signatures/{mutation_type}/wtsi/input.mat'
output:
'output/{project}/{method}/cohorts/{cohort}/signatures/{mutation_type}/wtsi/iteration_input.mat'
shell:
replace_newlines('''octave --eval "
{matlab_source}
outputIterationData('{{input}}', '{{output}}');
exit
"
'''.format(matlab_source = matlab_source_paths))
rule run_wtsi_iterations:
input:
'output/{project}/{method}/cohorts/{cohort}/signatures/{mutation_type}/wtsi/iteration_input.mat'
output:
'output/{project}/{method}/cohorts/{cohort}/signatures/{mutation_type}/wtsi/cores/output_{nsig}-signatures_core-{core}.mat'
threads: 1
shell:
replace_newlines('''octave --eval "
pkg load statistics;
{matlab_source}
runIterations(
'{{input}}',
'{{output}}',
'{iter_per_core}', '{{wildcards.nsig}}'
);
exit
"
'''.format(
matlab_source = matlab_source_paths,
iter_per_core = WTSI_ITERATIONS_PER_CORE
))
rule wtsi_iteration_paths_file:
input:
expand('output/{{project}}/{{method}}/cohorts/{{cohort}}/signatures/{{mutation_type}}/wtsi/cores/output_{{nsig}}-signatures_core-{core}.mat',
core = range(WTSI_CORES)
)
output:
'output/{project}/{method}/cohorts/{cohort}/signatures/{mutation_type}/wtsi/iteration_paths/{nsig}_core_output_paths.txt'
run:
f = open(output[0], 'w')
f.write('\n'.join(input))
f.close()
rule merge_wtsi_iterations:
input:
original = 'output/{project}/{method}/cohorts/{cohort}/signatures/{mutation_type}/wtsi/input.mat',
iter_paths = 'output/{project}/{method}/cohorts/{cohort}/signatures/{mutation_type}/wtsi/iteration_paths/{nsig}_core_output_paths.txt'
output:
'output/{project}/{method}/cohorts/{cohort}/signatures/{mutation_type}/wtsi/output/{nsig}-signatures.mat'
threads: 1
shell:
replace_newlines('''
bash
scripts/pipelines/wtsi-signatures/compiled/mergeIterations/for_redistribution_files_only/run_mergeIterations.sh
/projects/ezhao_prj/software/MCR/v901
{input.original}
{input.iter_paths}
{output}
''')
rule export_wtsi_metrics:
input:
'output/{project}/{method}/cohorts/{cohort}/signatures/{mutation_type}/wtsi/output/{nsig}-signatures.mat'
output:
'output/{project}/{method}/cohorts/{cohort}/signatures/{mutation_type}/wtsi/metrics/{nsig}-signatures.tsv'
shell:
'Rscript scripts/pipelines/wtsi-signatures/export_wtsi_metrics.R -i {input} -o {output}'
rule aggregate_wtsi_metrics:
input:
expand('output/{{project}}/{{method}}/cohorts/{{cohort}}/signatures/{{mutation_type}}/wtsi/metrics/{nsig}-signatures.tsv', nsig=WTSI_NSIG_RANGE)
params:
comma_separated = lambda wildcards, input: ','.join(input)
output:
'output/{project}/{method}/cohorts/{cohort}/signatures/{mutation_type}/wtsi/model_selection_metrics.tsv'
shell:
'Rscript scripts/pipelines/basic-functions/row_bind_tables.R -p {params.comma_separated} -o {output}'
rule project_aggregation_signatures:
input:
lambda wildcards: expand(
'output/{project}/{method}/cohorts/{{cohort}}/signatures/{mutation_type}/wtsi/combined_signatures.tsv'.format(
project = wildcards.project,
method = wildcards.method,
mutation_type = wildcards.mutation_type,
),
cohort=project_cohorts[wildcards.project],
)
params:
comma_separated = lambda wildcards, input: ','.join(input)
output:
'output/{project}/{method}/aggregated/{mutation_type}/signatures.tsv'
shell:
'Rscript scripts/pipelines/basic-functions/row_bind_tables.R -p {params.comma_separated} -o {output}'
rule project_aggregation_exposures:
input:
lambda wildcards: expand(
'output/{project}/{method}/cohorts/{{cohort}}/signatures/{mutation_type}/wtsi/combined_exposures.tsv'.format(
project = wildcards.project,
method = wildcards.method,
mutation_type = wildcards.mutation_type,
),
cohort=project_cohorts[wildcards.project],
)
params:
comma_separated = lambda wildcards, input: ','.join(input)
output:
'output/{project}/{method}/aggregated/{mutation_type}/exposures.tsv'
shell:
'Rscript scripts/pipelines/basic-functions/row_bind_tables.R -p {params.comma_separated} -o {output}'
rule project_aggregation_metrics:
input:
lambda wildcards: expand(
'output/{project}/{method}/cohorts/{{cohort}}/signatures/{mutation_type}/wtsi/model_selection_metrics.tsv'.format(
project = wildcards.project,
method = wildcards.method,
mutation_type = wildcards.mutation_type,
),
cohort=project_cohorts[wildcards.project],
)
params:
comma_separated = lambda wildcards, input: ','.join(input)
output:
'output/{project}/{method}/aggregated/{mutation_type}/model_selection_metrics.tsv'
shell:
'Rscript scripts/pipelines/basic-functions/row_bind_tables.R -p {params.comma_separated} -o {output}'
rule export_wtsi_snv_signatures_exposures:
input:
expand('output/{{project}}/{{method}}/cohorts/{{cohort}}/signatures/{{mutation_type}}/wtsi/output/{nsig}-signatures.mat', nsig=WTSI_NSIG_RANGE)
params:
comma_separated = lambda wildcards, input: ','.join(input)
output:
signatures='output/{project}/{method}/cohorts/{cohort}/signatures/{mutation_type}/wtsi/combined_signatures.tsv',
exposures='output/{project}/{method}/cohorts/{cohort}/signatures/{mutation_type}/wtsi/combined_exposures.tsv'
shell:
'Rscript scripts/pipelines/wtsi-signatures/export_wtsi_signatures_exposures.R -p {params.comma_separated} -s {output.signatures} -e {output.exposures}'
rule wtsi_snv_pipeline:
input:
signatures='output/{project}/{method}/cohorts/{cohort}/signatures/{mutation_type}/wtsi/combined_signatures.tsv',
exposures='output/{project}/{method}/cohorts/{cohort}/signatures/{mutation_type}/wtsi/combined_exposures.tsv',
metrics='output/{project}/{method}/cohorts/{cohort}/signatures/{mutation_type}/wtsi/model_selection_metrics.tsv',
output:
'output/{project}/{method}/cohorts/{cohort}/signatures/{mutation_type}/wtsi/COMPLETE'
shell:
'touch {output}'
########################
### Signature Report ###
########################
rule snv_signature_report:
input:
'output/{project}/{method}/cohorts/{cohort}/signatures/{mutation_type}/wtsi/COMPLETE'
output:
'output/{project}/{method}/cohorts/{cohort}/signatures/{mutation_type}/signature_report.html'
params:
root_dir = lambda wildcards, output: PROJECT_ROOT + '/' + '/'.join(output[0].split('/')[0:-1]),
filename = lambda wildcards, output: output[0].split('/')[-1]
shell:
'''Rscript -e "rmarkdown::render('scripts/pipelines/reports/mutation_signature.Rmd', knit_root_dir='{params.root_dir}', output_dir='{params.root_dir}', output_file='{params.filename}')"'''