-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snakefile
433 lines (394 loc) · 15.2 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
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
import os
from scripts.util import *
####### SELECT CONFIG FILE HERE #######
configfile: "config/config_NNNlib2b_20211216.yaml"
#######################################
# --- Define Global Variables --- #
datadir = config['datadir']
expdir = os.path.normpath(datadir + '/../') + '/'
sequencingResult = config["sequencingResult"]
normalizedSeries = datadir + "series_normalized/" + config["imagingExperiment"] + "_normalized.pkl"
fittedVariant = datadir + "fitted_variant/" + config["imagingExperiment"] + ".CPvariant.gz"
# hardcoded tile numbers
TILES = ['tile%03d'%i for i in range(1,19)]
TILES_NO_ZERO_PAD = ['tile%d'%i for i in range(1,19)]
# == Decide which outputs to require base on progress of the experiment ==
assert config["processingType"] in ['pre-array', 'post-array']
if config["processingType"] == "pre-array":
fluor_files = []
requested_output = ["%s_STATS.csv" % sequencingResult.strip('.CPseq'),
expand(expdir + "fig/fiducial/{tile}_Bottom_fiducial.png", tile=TILES)]
elif config["processingType"] == "post-array":
fluor_files = get_fluor_names_from_mapfile(config["mapfile"], config["tifdir"], config["fluordir"])
if config["fitting"] == "NNN":
requested_output = fittedVariant
else:
requested_output = config["seriesfile"]
container: "library://kyx/array_analysis/mamba:latest"
#wildcard_constraints:
# --- Define Required Output --- #
rule all:
input:
requested_output
# --- Rules --- #
## unzip_fastq: in case the fastq files were not unzipped
rule unzip_fastq:
input:
r1 = config['fastq']['read1'] + ".gz",
r2 = config['fastq']['read2'] + ".gz"
output:
r1 = config['fastq']['read1'],
r2 = config['fastq']['read2']
threads:
1
params:
cluster_time = "02:00:00"
shell:
"gunzip {input.r1} {input.r2}"
## run_FLASH: align paired ends with FLASH
rule run_FLASH:
input:
r1 = config['fastq']['read1'],
r2 = config['fastq']['read2']
output:
datadir + "FLASH_output/out.extendedFrags.fastq"
params:
outdir = datadir + "FLASH_output"
threads:
1
shell:
"""
cd {params[outdir]}
{config[FLASHdir]}/FLASH-1.2.11/flash {input.r1} {input.r2}
"""
rule convert_FLASH_to_CPseq:
input:
datadir + "FLASH_output/out.extendedFrags.fastq"
output:
datadir + "paired_reads/ConsensusPairedReads.CPseq"
params:
cluster_memory = "16G"
threads:
1
conda:
"envs/align.yml"
shell:
"python3 scripts/convertFLASH_OutputToCPseq.py {input} {output}"
rule align_consensus_read_to_library:
input:
reads = datadir + "paired_reads/ConsensusPairedReads.CPseq",
reference = config["referenceLibrary"],
scoring_matrix = os.path.join(os.getcwd(), "data/reference/NUC.4.4") # need this to check existence of the matrix file
output:
seq_result = sequencingResult,
cluster_annot = sequencingResult.replace('.CPseq', '.CPannot')
threads:
6
params:
fiveprimeRegion = config["refSeqContext"]["fiveprime"],
threeprimeRegion = config["refSeqContext"]["threeprime"],
variant_col = config['variantCol'],
cluster_memory = "90G",
cluster_time = "24:00:00"
conda:
"envs/align.yml"
shell:
"""
python3 scripts/matchConsensusReadsToLibrary.py --beam {config[alignBeam]} {input.reads} --library {input.reference} -o {output.seq_result} --clusterAnnotation {output.cluster_annot} --scoringMatrix {input.scoring_matrix} --fiveprimeRegion {params.fiveprimeRegion} --threeprimeRegion {params.threeprimeRegion}
"""
rule get_stats:
input:
sequencingResult
output:
"%s_STATS.csv" % sequencingResult.strip('.CPseq')
threads:
1
conda:
"envs/align.yml"
shell:
"python3 scripts/get_stats.py {input} {output}"
rule merge_fastqs_to_CPseq:
input:
r1 = config['fastq']['read1'],
r2 = config['fastq']['read2']
output:
datadir + "sequence/ALL.CPseq"
params:
cluster_memory = "10G",
cluster_time = "10:00:00"
threads:
2
conda:
"envs/ame.yml"
shell:
"""
python scripts/array_tools/CPscripts/mergeFastqReadsToCPseq.py -r1 {input.r1} -r2 {input.r2} -o {output}
"""
rule split_CPseq:
input:
datadir + "sequence/ALL.CPseq"
output:
expand(datadir + "tiles/ALL_{tile}_Bottom.CPseq", tile=TILES)
threads:
1
params:
cluster_memory = "1G",
cluster_time = "5:00:00",
tiledir = datadir + "tiles/"
conda:
"envs/ame.yml"
shell:
"""
python scripts/array_tools/CPscripts/splitCPseqIntoTiles.py -o {params.tiledir} -s bottom {input}
"""
rule filter_tiles:
input:
expand(datadir + "tiles/ALL_{tile}_Bottom.CPseq", tile=TILES),
config["FIDfilter"]
output:
expand(datadir + "filtered_tiles/ALL_{tile}_Bottom_filtered.CPseq", tile=TILES)
params:
tiledir = datadir + "tiles/",
filteredtiledir = datadir + "filtered_tiles/",
cluster_memory = "16G",
cluster_time = "5:00:00"
conda:
"envs/py36.yml"
#envmodules:
# "matlab"
threads:
8
shell:
"""
module load matlab
export MATLABPATH=/share/PI/wjg/lab/array_tools/CPscripts/:/share/PI/wjg/lab/array_tools/CPlibs/
python3 scripts/array_tools/CPscripts/alignmentFilterMultiple.py -rd {params.tiledir} -f {config[FIDfilter]} -od {params.filteredtiledir} -gv /share/PI/wjg/lab/array_tools -n 18
"""
rule filter_tiles_libregion:
input:
expand(datadir + "tiles/ALL_{tile}_Bottom.CPseq", tile=TILES),
config["LibRegionFilter"]
output:
expand(datadir + "filtered_tiles_libregion/ALL_{tile}_Bottom_filtered.CPseq", tile=TILES)
params:
tiledir = datadir + "tiles/",
filteredtiledir = datadir + "filtered_tiles_libregion/",
cluster_memory = "16G",
cluster_time = "5:00:00"
conda:
"envs/py36.yml"
threads:
8
shell:
"""
module load matlab
export MATLABPATH=scripts/array_tools/CPscripts/:scripts/array_tools/CPlibs/
python3 scripts/array_tools/CPscripts/alignmentFilterMultiple.py -rd {params.tiledir} -f {config[LibRegionFilter]} -od {params.filteredtiledir} -gv scripts/array_tools -n 18
"""
rule plot_fiducials:
input:
expand(datadir + "filtered_tiles/ALL_{tile}_Bottom_filtered.CPseq", tile=TILES)
output:
expand(expdir + "fig/fiducial/{tile}_Bottom_fiducial.png", tile=TILES)
conda:
"envs/plotting.yml"
params:
cluster_memory = "4G"
threads:
1
script:
"scripts/plotSeqs.py"
## quantify_images: quantify intensities in tif and write to CPfluor
## snakemake checks one tile per condition as input/output and submit one job per condition
rule quantify_images:
input:
image = config["tifdir"] + "{condition}/%s_{tile}_{channel}_600ms_{timestamp}.tif" % config["experimentName"],
libregion = expand(datadir + "filtered_tiles_libregion/ALL_{tile}_Bottom_filtered.CPseq", tile=TILES)
output:
CPfluor = config["fluordir"] + "{condition}/%s_{tile}_{channel}_600ms_{timestamp}.CPfluor" % config["experimentName"]#,
#roff = expand(datadir + "roff/{condition}/%s_{tile}_{channel}_600ms_{timestamp}.roff")
params:
image_dir = config["tifdir"] + "{condition}/",
seq_dir = datadir + "filtered_tiles_libregion/",
fluor_dir = config["fluordir"] + "{condition}/",
roff_dir = datadir + "roff/{condition}/",
reg_subset = "LibRegion",
log_dir = expdir + "log/quantify_image_{condition}.log",
num_cores = "18",
data_scaling = "MiSeq_to_TIRFStation1",
cluster_memory = "40G",
cluster_time = "15:00:00"
threads:
18
conda:
"envs/py36.yml"
shell:
"""
module load matlab
export MATLABPATH=scripts/array_tools/CPscripts:scripts/array_tools/CPlibs
python3 scripts/array_tools/CPscripts/quantifyTilesDownstream.py -id {params.image_dir} -ftd {params.seq_dir} -fd {params.fluor_dir} -rod {params.roff_dir} -n {params.num_cores} -rs {params.reg_subset} -sf {params.data_scaling} -gv scripts/array_tools/
"""
## write_old_mapfile: convert and prepare mapfile for the combine_signal step
rule write_old_mapfile:
input:
config['mapfile']
output:
oldmapfile = datadir + 'tmp/' + config["imagingExperiment"] + '.map'
params:
fluordir = config["fluordir"],
cluster_memory = "500M",
cluster_time = "0:15:00"
threads:
1
conda:
"envs/py36.yml"
shell:
"python3 scripts/writeOldMapfile.py {params.fluordir} {config[mapfile]} {output.oldmapfile}"
## combine_signal: Integrate and combine CPfluor files of different conditions into a single CPseries file per tile
rule combine_signal:
input:
fluorfiles = fluor_files,
oldmapfile = datadir + 'tmp/' + config["imagingExperiment"] + '.map',
libdata = sequencingResult
output:
get_series_tile_filenames(config["seriesdir"], config["prefix"])
params:
output_directory = directory(config["seriesdir"]),
cluster_memory = "80G",
cluster_time = "00:30:00",
num_cores = "6"
threads:
6
conda:
"envs/py36.yml"
shell:
"""
python3 scripts/array_tools/bin_py3/processData.py -mf {input.oldmapfile} -od {params.output_directory} --appendLibData {input.libdata} --num_cores {params.num_cores}
"""
## concat_tiles_signal: Concatenate one CPseries file per tile into one big CPseries file with all tiles
rule concat_tiles_signal:
input:
series = get_series_tile_filenames(config["seriesdir"], config["prefix"]),
mapfile = config["mapfile"]
output:
config["seriesfile"]
conda:
"envs/py36.yml"
shell:
"""
python3 scripts/concatTilesSignal.py --tiles {input.series} -o {output} -m {input.mapfile}
"""
## normalize_signal: Given one merged CPseq file, normalize fluorescence signal for single cluster fit
rule normalize_signal:
input:
CPseries_file = config["seriesfile"],
mapfile = config["mapfile"],
annotation = config["referenceLibrary"]
output:
out_file = datadir + 'series_normalized/' + config["imagingExperiment"] + "_normalized.pkl",
xdata_file = datadir + 'series_normalized/' + config["imagingExperiment"] + "_xdata.txt"
params:
figdir = expdir + "fig/normalization_%s/"%config["imagingExperiment"],
green_norm_condition = config["greenNormCondition"],
ext = ".pdf",
variant_col = config["variantCol"],
smooth = 'savgol_7_2' # savgol_{window_length}_{polyorder} or 'None'
conda:
"envs/fitting.yml"
threads:
1
script:
"scripts/normalizeNNNlib2bSignal.py"
## fit_single_cluster
rule fit_single_cluster:
input:
normalized = normalizedSeries,
xdata = datadir + "series_normalized/" + config["imagingExperiment"] + "_xdata.txt",
mapfile = config["mapfile"]
output:
datadir + "fitted_single_cluster/" + config["imagingExperiment"] + ".CPfitted.gz"
threads:
18
params:
cluster_time = "48:00:00",
cluster_memory = "32G"
conda:
"envs/fitting.yml"
shell:
"python3 scripts/nnn_fitting/singleClusterFits.py --parallel -b {input.normalized} -x {input.xdata} -o {output} --mapfile {input.mapfile}"
## bootstrap_variant_median
rule bootstrap_variant_median:
input:
cf = datadir + "fitted_single_cluster/" + config["imagingExperiment"] + ".CPfitted.gz",
annotation = sequencingResult.replace('.CPseq', '.CPannot')
output:
variant = datadir + "fitted_single_cluster/" + config["imagingExperiment"] + ".CPvariant",
good_clusters = datadir + "fitted_single_cluster/" + config["imagingExperiment"] + "_good_cluster_ind.txt"
params:
p = "dH Tm",
n_samples = "100",
good_fit = config["query"]["singleCluster"].replace(' ', ''),
vc = config["variantCol"],
cluster_time = "02:00:00",
cluster_memory = "8G"
threads:
12
conda:
"envs/fitting.yml"
shell:
"""
python3 scripts/nnn_fitting/bootStrapFitFile.py -cf {input.cf} -a {input.annotation} -g {output.good_clusters}\
-p {params.p} -vc {params.vc} --query {params.good_fit} --n_samples {params.n_samples}
"""
## fit_fmax_fmin_distribution
rule fit_fmax_fmin_distribution:
input:
vf = datadir + "fitted_single_cluster/" + config["imagingExperiment"] + ".CPvariant"
output:
fm = datadir + "fitted_fmax_fmin/%s-fmax_fmin.json" % config["imagingExperiment"],
plots = directory(expdir + "fig/fmax_fmin_%s/"%config["imagingExperiment"])
# plots = expand(expdir + "fig/fmax_fmin/{plotname}"%config["experimentName"], plotname=['fmax_vs_dG_init.pdf', 'fmin_vs_dG_init.pdf'])
params:
figdir = expdir + "fig/fmax_fmin_%s/"%config["imagingExperiment"],
fmax_q = config["query"]["fmaxVariant"].replace(' ', ''),
fmin_q = config["query"]["fminVariant"].replace(' ', ''),
variant_q = config["query"]["variant"].replace(' ', '')
threads:
1
conda:
"envs/fitting.yml"
shell:
"""
python3 scripts/nnn_fitting/findFmaxFminDist.py -vf {input.vf} -o {output.fm} --figdir {params.figdir}\
-fmaxq {params.fmax_q} -fminq {params.fmin_q} --variant_filter {params.variant_q}
"""
## fit_refine_variant: refine fit at the variant level using estimated fmax and fmin for those not reaching them
rule fit_refine_variant:
input:
cluster = normalizedSeries,
variant = datadir + "fitted_single_cluster/" + config["imagingExperiment"] + ".CPvariant",
xdata = datadir + "series_normalized/" + config["imagingExperiment"] + "_xdata.txt",
mapfile = config["mapfile"],
fm = datadir + "fitted_fmax_fmin/%s-fmax_fmin.json" % config["imagingExperiment"],
annotation = sequencingResult.replace('.CPseq', '.CPannot')
output:
fitted = datadir + "fitted_variant/" + config["imagingExperiment"] + ".CPvariant.gz"
params:
figdir = expdir + "fig/%s-fit_refine_variant/"%config["imagingExperiment"],
p = "dH Tm",
n_bootstraps = "100",
vc = config["variantCol"],
good_clusters = datadir + "fitted_single_cluster/" + config["imagingExperiment"] + "_good_cluster_ind.txt",
variant_q = config["query"]["variant"].replace(" ", ""),
cluster_time = "48:00:00",
cluster_memory = "32G"
threads:
20
conda:
"envs/fitting.yml"
shell:
"""
python3 scripts/nnn_fitting/refineVariantFits.py --parallel -b {input.cluster} -vf {input.variant} -x {input.xdata}\
--mapfile {input.mapfile} --cluster_annot {input.annotation} --fmax_fmin {input.fm} -o {output.fitted} --figdir {params.figdir}\
--param {params.p} --variant_col {params.vc} --n_bootstraps {params.n_bootstraps}
"""