-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathsaturationMutagenesis.nf
368 lines (305 loc) · 12.6 KB
/
saturationMutagenesis.nf
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
#!/usr/bin/env nextflow
/*
========================================================================================
MPRAflow
========================================================================================
MPRA Analysis Pipeline. Started 2019-07-29.
Saturation Mutagenesis Utility
#### Homepage / Documentation
https://github.com/shendurelab/MPRAflow
#### Authors
Max Schubach <[email protected]>
----------------------------------------------------------------------------------------
*/
def helpMessage() {
log.info"""
=========================================
shendurelab/MPRAflow v${params.version}
=========================================
Usage:
The typical command for running the pipeline is as follows:
nextflow run saturationMutagenesis.nf
Mandatory arguments:
--dir Directory of count files (must be surrounded with quotes)
--assignment Variant assignment file
--e, --experiment-file Experiment csv file
Options:
--outdir The output directory where the results will be saved (default outs)
--thresh Minimum number of observed barcodes to retain variant (default 10)
--pvalue pValue cutoff for significant different cvariant effects. For variant effect plots only. (default 1e-5)
""".stripIndent()
}
/*
* SET UP CONFIGURATION VARIABLES
*/
// Show help message
if (params.containsKey('h') || params.containsKey('help')){
helpMessage()
exit 0
}
// Configurable variables
//defaults
results_path = params.outdir
params.thresh = 10
params.pvalue = 1e-5
// Validate Inputs
// experiment file saved in params.experiment_file
if (params.containsKey('e')){
params.experiment_file=file(params.e)
} else if (params.containsKey("experiment-file")) {
params.experiment_file=file(params["experiment-file"])
} else {
exit 1, "Experiment file not specified with --e or --experiment-file"
}
if( !params.experiment_file.exists()) exit 1, "Experiment file ${params.experiment_file} does not exist"
// Association file in params.association_file
if ( params.containsKey("assignment")){
params.assignment_file=file(params.assignment)
if( !params.assignment_file.exists() ) exit 1, "Assignment file ${params.assignment_file} does not exist"
} else {
exit 1, "Assignment file not specified with --assignment"
}
// Create FASTQ channels
Channel.fromPath(params.experiment_file).splitCsv(header: true).flatMap{
row -> [[row.Condition, row.Replicate,file([params.dir,"/",row.COUNTS].join())]]
}.into{count_channel; count_1bpDel_channel}
// Header log info
log.info """=======================================================
,--./,-.
___ __ __ __ ___ /,-._.--~\'
|\\ | |__ __ / ` / \\ |__) |__ } {
| \\| | \\__, \\__/ | \\ |___ \\`-._,-`-,
`._,._,\'
MPRAflow v${params.version}"
======================================================="""
def summary = [:]
summary['Pipeline Name'] = 'shendurelab/MPRAflow'
summary['Pipeline Version'] = params.version
summary['Output dir'] = params.outdir
summary['Working dir'] = workflow.workDir
summary['Current home'] = "$HOME"
summary['Current user'] = "$USER"
summary['Current path'] = "$PWD"
summary['Working dir'] = workflow.workDir
summary['Output dir'] = params.outdir
summary['Script dir'] = workflow.projectDir
summary['Config Profile'] = workflow.profile
summary['Experiment File'] = params.experiment_file
summary['BC threshold'] = params.thresh
log.info summary.collect { k,v -> "${k.padRight(15)}: $v" }.join("\n")
log.info "========================================="
// Check that Nextflow version is up to date enough
// try / throw / catch works for NF versions < 0.25 when this was implemented
try {
if( ! nextflow.version.matches(">= $params.nf_required_version") ){
throw GroovyException('Nextflow version too old')
}
} catch (all) {
log.error "====================================================\n" +
" Nextflow version $params.nf_required_version required! You are running v$workflow.nextflow.version.\n" +
" Pipeline execution will continue, but things may break.\n" +
" Please run `nextflow self-update` to update Nextflow.\n" +
"============================================================"
}
println 'start analysis'
/*
* STEP 1: Calculate assignment variant Matrix with and witout indels
*/
process 'calc_assign_variantMatrix' {
publishDir "$params.outdir/$cond/$rep", mode:'copy'
label 'shorttime'
conda 'conf/mpraflow_py27.yml'
input:
file(assignments) from params.assignment_file
tuple val(cond), val(rep), file(counts) from count_channel
output:
tuple val(cond), val(rep), val("VarMatrix"), file("${cond}_${rep}.VarMatrix.tsv.gz") into variantMatrix, variantMatrixCombined
shell:
"""
python ${"$baseDir"}/src/satMut/extractVariantMatrix.py -a ${assignments} ${counts} | \
gzip -c > ${cond}_${rep}.VarMatrix.tsv.gz
"""
}
process 'calc_assign_variantMatrixWith1bpDel' {
publishDir "$params.outdir/$cond/$rep", mode:'copy'
label 'shorttime'
conda 'conf/mpraflow_py27.yml'
input:
file(assignments) from params.assignment_file
tuple val(cond), val(rep), file(counts) from count_1bpDel_channel
output:
tuple val(cond), val(rep), val("VarMatrix1bpDel"), file("${cond}_${rep}.VarMatrix1bpDel.tsv.gz") into variantMatrix1bpDel, variantMatrix1bpDelCombined
shell:
"""
python ${"$baseDir"}/src/satMut/extractVariantMatrixHandleIndels.py -a ${assignments} ${counts} | \
gzip -c > ${cond}_${rep}.VarMatrix1bpDel.tsv.gz
"""
}
/*
* STEP 2: FIT model
*/
process 'fitModel' {
publishDir "$params.outdir/$cond/$rep", mode:'copy'
label 'longtime'
conda 'conf/mpraflow_r.yml'
variantMatrixBoth = variantMatrix.concat(variantMatrix1bpDel)
input:
tuple val(cond), val(rep), val(type), file(variantMatrix) from variantMatrixBoth
output:
tuple val(cond), val(rep), val(type), file(variantMatrix), file("${cond}_${rep}.${type}.ModelCoefficients.txt") into variantMatrixModelCoefficients
shell:
"""
Rscript ${"$baseDir"}/src/satMut/fitModel.R $variantMatrix ${cond}_${rep}.${type}.ModelCoefficients.txt
"""
}
process 'summarizeVariantMatrix' {
publishDir "$params.outdir/$cond/$rep", mode:'copy'
label 'shorttime'
conda 'conf/mpraflow_py27.yml'
input:
tuple val(cond), val(rep), val(type), file(variantMatrix), file(modelCoefFile) from variantMatrixModelCoefficients
output:
tuple val(cond), val(rep), val(type), file(modelCoefFile), file("${variantMatrix}.stats") into variantMatrixModelCoefficientsStats, variantMatrixModelCoefficientsStatsForCombined
shell:
"""
python ${"$baseDir"}/src/satMut/summarizeVariantMatrix.py $variantMatrix
"""
}
process 'statsWithCoefficient' {
publishDir "$params.outdir/$cond/$rep", mode:'copy'
label 'shorttime'
input:
tuple val(cond), val(rep), val(type), file(modelCoefFile), file(statsFile) from variantMatrixModelCoefficientsStats
output:
tuple val(cond), val(rep), val(type), file("${cond}_${rep}.${type}.ModelCoefficientsVsStats.txt") into variantMatrixModelCoefficientsVsStats, variantMatrixModelCoefficientsVsStats2
shell:
"""
(
echo -e "Position\\tBarcodes\\tDNA\\tRNA\\tCoefficient\\tStdError\\ttValue\\tpValue";
join -t"\$(echo -e '\\t')" <(sort $statsFile) <( sed s/^X//g $modelCoefFile | sort );
) > ${cond}_${rep}.${type}.ModelCoefficientsVsStats.txt;
"""
}
List combinationsOf(List list, int r) {
assert (0..<list.size()).contains(r) // validate input
def combs = [] as Set
list.eachPermutation {
combs << it.subList(0, r).sort { a, b -> a <=> b }
}
combs as List
}
process 'plotCorrelation' {
publishDir "$params.outdir/$cond", mode:'copy'
label 'shorttime'
conda 'conf/mpraflow_r.yml'
result = variantMatrixModelCoefficientsVsStats2.groupTuple(by: [0,2]).map{n -> [n[0],n[2],combinationsOf(n[1],2),combinationsOf(n[3],2)]}.transpose(by:[2,3]).map{n -> [n[0],n[1],n[2][0],n[2][1],n[3][0],n[3][1]]}
input:
tuple val(cond), val(type), val(rep1), val(rep2), file(results1), file(results2) from result
val(thresh) from params.thresh
output:
tuple val(cond), val(type), file("${cond}.${type}.${rep1}_${rep2}.correlation.png") into correlations
shell:
"""
Rscript ${"$baseDir"}/src/satMut/plotCorrelation.R $results1 $results2 ${cond}.${type}.Replicate_${rep1} ${cond}.${type}.Replicate_${rep2} $thresh "${cond}.${type}.${rep1}_${rep2}.correlation.png"
"""
}
process 'plotStatsWithCoefficient' {
publishDir "$params.outdir/$cond/$rep", mode:'copy'
label 'shorttime'
conda 'conf/mpraflow_r.yml'
input:
tuple val(cond), val(rep), val(type), file(results) from variantMatrixModelCoefficientsVsStats
val(threshhold) from params.thresh
val(pvalue) from params.pvalue
output:
tuple val(cond), val(rep), val(type), file("${cond}.${type}.saturationMutagenesis.png") into satMutPlot
shell:
"""
Rscript ${"$baseDir"}/src/satMut/plotElements.R $results ${cond}_${rep}.${type} $threshhold $pvalue ${cond}.${type}.saturationMutagenesis.png
"""
}
process 'fitModelCombined' {
publishDir "$params.outdir/$cond", mode:'copy'
label 'longtime'
conda 'conf/mpraflow_r.yml'
result = variantMatrixCombined.concat(variantMatrix1bpDelCombined).groupTuple(by: [0,2]).multiMap{i ->
cond: i[0]
type: i[2]
replicate: i[1].join(" ")
files: i[3]
}
input:
val(cond) from result.cond
val(type) from result.type
val(replicates) from result.replicate
file variantMatrixFiles from result.files
output:
tuple val(cond), val(type), file("${cond}.Combined.${type}.ModelCoefficients.txt") into variantMatrixModelCoefficientsCombined
script:
variantMatrix = variantMatrixFiles.collect{"$it"}.join(' ')
shell:
"""
Rscript ${"$baseDir"}/src/satMut/fitModelCombined.R ${cond}.Combined.${type}.ModelCoefficients.txt $variantMatrix $replicates
"""
}
process 'combinedStats' {
publishDir "$params.outdir/$cond", mode:'copy'
label 'shorttime'
conda 'conf/mpraflow_r.yml'
result = variantMatrixModelCoefficientsStatsForCombined.groupTuple(by: [0,2]).multiMap{i ->
cond: i[0]
type: i[2]
replicate: i[1].join(" ")
files: i[4]
}
input:
val(cond) from result.cond
val(type) from result.type
val(replicates) from result.replicate
file statsFile from result.files
script:
stats = statsFile.collect{"$it"}.join(' ')
output:
tuple val(cond), val(type), file("${cond}.Combined.${type}.stats") into variantMatrixModelCoefficientsStatsCombined
shell:
"""
Rscript ${"$baseDir"}/src/satMut/combineStats.R ${cond}.Combined.${type}.stats $stats
"""
}
process 'statsWithCoefficientCombined' {
publishDir "$params.outdir/$cond", mode:'copy'
label 'shorttime'
result = variantMatrixModelCoefficientsStatsCombined.concat(variantMatrixModelCoefficientsCombined).groupTuple(by: [0,1]).multiMap{i ->
cond: i[0]
type: i[1]
files: i[2]
}
input:
val(cond) from result.cond
val(type) from result.type
tuple file(stat), file(model) from result.files
output:
tuple val(cond), val(type), file("${cond}.Combined.${type}.ModelCoefficientsVsStats.txt") into variantMatrixModelCoefficientsVsStatsCombined
shell:
"""
(
echo -e "Position\\tBarcodes\\tDNA\\tRNA\\tCoefficient\\tStdError\\ttValue\\tpValue";
join -t"\$(echo -e '\\t')" <(sort $stat | sort) <( sed s/^X//g $model | sort );
) > ${cond}.Combined.${type}.ModelCoefficientsVsStats.txt;
"""
}
process 'plotStatsWithCoefficientCombined' {
publishDir "$params.outdir/$cond", mode:'copy'
label 'shorttime'
conda 'conf/mpraflow_r.yml'
input:
tuple val(cond), val(type), file(results) from variantMatrixModelCoefficientsVsStatsCombined
val(thresh) from params.thresh
val(pvalue) from params.pvalue
output:
tuple val(cond), val(type), file("${cond}.Combined.${type}.saturationMutagenesis.png") into combinedSatMutPlot
shell:
"""
Rscript ${"$baseDir"}/src/satMut/plotElements.R $results ${cond}.Combined.${type} $thresh $pvalue ${cond}.Combined.${type}.saturationMutagenesis.png
"""
}