-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprodQC
executable file
·375 lines (295 loc) · 7.68 KB
/
prodQC
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
#!/usr/bin/env nextflow
params.expDir = false
params.expName = false
params.groupName = false
params.biosample = false
params.fastqDir = false
params.genewizBucket = false
params.s3Path = false
params.S3libraryID = false
params.genome = 'hg38'
params.noPreSeq = false
params.help = false
params.outDir = false
params.config = 'default'
def helpMessage() {
log.info"""
Usage:
The typical command for running the pipeline is as follows:
prodQC --biosample bisample1,biosample2,biosample3 --outDir ~/ebs/ref_push/prodEpi/myExpName
or
prodQC --fastqDir ~/ebs/ref_push/prodEpi/myExpName/fastqs/ --outDir ~/ebs/ref_push/prodEpi/myExpName
Mandatory arguments:
--outDir [pathOfDir] Path of where to publish the data, can be local or an S3 accessible bucket
Input (minimum one of these, can be mixed):
--biosample [str] Comma seperated list of BaseSpace biosamples.
--fastqDir [str] Path to a directoy of fastq files.
--s3Path [str] Path to a S3 dirctory in the format s3://bucket/prefix/
Restriting to a set of library:
--S3libraryID [str] Comma seperated list of library prefixes to subset the files in S3 directory.
Basespce token and host:
--config [config] if using a basespce config file different from ~/.basename/default.cfg pass ~/.basespace/<config>.cfg
References:
--genome [str] Name of the genome to use. Possible choice: hg38, mm10, dmel, rn6, GRCg6a, susScr11. Default: hg38.
Others:
--noPreSeq [bool] Do not run PreSeq. Default: false
""".stripIndent()
}
if (params.help){
helpMessage()
exit 0
}
if ( !(params.biosample || params.fastqDir || params.s3Path) ){
exit 1, "You need to use either --biosample aBaseSpace-biosample or --fastqDir path/to/bam/files or --s3Path s3://bucket/prefix/. Use --help to get the full usage"
}
if ( !(params.outDir) ){
exit 1, "--outDir is a required argument. Use --help to get the full usage"
}
Channel
.fromPath("${HOME}/ebs/genome/nextflow/${params.genome}", checkIfExists: true)
.ifEmpty { exit 1, "BWA index not found: ${params.genome}" }
.set { bwa_index }
if (params.biosample){
Channel
.from(params.biosample)
.splitCsv()
.flatten()
.set { biosample_ch }
} else {
Channel
.empty()
.set { biosample_ch }
}
if (params.fastqDir){
Channel
.fromFilePairs("${params.fastqDir}/*_{R1,R2}*.fastq.gz",
chekIfExists: true,
flat: true)
.into{fqDir_fqs_ch;fqDir_stat_ch}
} else {
Channel
.empty()
.into{fqDir_fqs_ch;fqDir_stat_ch}
}
if (params.s3Path){
Channel
.fromFilePairs("${params.s3Path}/*_{R1,R2}*.fastq.gz",
chekIfExists: true,
flat: true)
.set{s3_ch}
if (params.S3libraryID){
s3_ch
.map{ id,R1,R2 ->
for ( item in params.S3libraryID.split(/,/, -1) ){
if ( id =~/${item}/ ){
return(tuple id,R1,R2 )
}
}
}
.into{s3_fqs_ch;s3_stat_ch;s3_dwld}
} else {
s3_ch
.into{s3_fqs_ch;s3_stat_ch;s3_dwld}
}
process get_S3_files {
cpus 8
memory '16G'
container 'ubuntu:20.04'
publishDir "${params.outDir}/fastqs",
mode: 'copy'
input:
tuple val(id), path(R1s), path(R2s) from s3_dwld
output:
tuple val(id), path(R1s), path(R2s) into dwlded
script:
"""
echo downloading ${R1s} and ${R2s} from s3
"""
}
} else {
Channel
.empty()
.into{s3_fqs_ch;s3_stat_ch}
}
if (!params.biosample) {
Channel
.empty()
.into{bs_fqs_ch;bs_stat_ch}
} else {
/////
// Get BS tokent from config file
////
/// TODO: Add some logic if things are not working
bsConfig = file("${HOME}/.basespace/${params.config}.cfg", checkIfExists:true)
for ( line : bsConfig.readLines() ) {
if (m = line =~ /apiServer.+=(.+)/) {
host = m[0][1].replaceAll(' ','')
} else if ( m = line =~ /accessToken.+=(.+)/){
token = m [0][1].replaceAll(' ','')
}
}
process get_bs_files {
cpus 1
memory '1G'
container 'mblanche/basespace-cli'
input:
val bs from biosample_ch
output:
stdout into bs_id_ch
script:
"""
findNewestBS.sh ${bs} ${token} ${host}
"""
}
process download_bs {
echo true
label "movers"
cpus 4
memory '4G'
container 'mblanche/basespace-cli'
queue 'moversQ'
publishDir "${params.outDir}/fastqs",
mode: 'copy'
input:
tuple id, val(bs) from bs_id_ch
.splitCsv(header: false)
output:
tuple bs, path("*.fastq.gz") into bs_fqs_ch,bs_stat_ch
script:
"""
bs file download --api-server ${host} --access-token ${token} -i ${id} -o .
"""
}
}
process bwa_mem {
tag "${id}"
cpus 48
memory '100 GB'
container 'mblanche/bwa-samtools'
input:
tuple val(id), path(R1s), path(R2s) from bs_fqs_ch
.map{bs,file ->
pref = file.name.toString().take(file.name.toString().indexOf('_R'))
return(tuple(pref,file))
}
.groupTuple()
.flatten()
.collate(3)
.mix(fqDir_fqs_ch)
.mix(s3_fqs_ch)
.map { id, file1, file2 -> tuple(file1.name.toString().split('_')[0], file1, file2) }
.groupTuple()
path index from bwa_index.first()
output:
tuple id, path("*.sam") into sam4chrSize, sam_ch1
script:
"""
bwa mem -5SP -t ${task.cpus} \
${index}/${params.genome} \
<(zcat ${R1s}) \
<(zcat ${R2s}) \
> ${id}.sam
"""
}
process bam_sort {
tag "${id}"
cpus 48
memory '150 GB'
container 'mblanche/bwa-samtools'
publishDir "${params.outDir}/bam", mode: 'copy'
input:
tuple id, path(bam) from sam_ch1
output:
tuple id, path("*.bam"), path("*.bai") into bam_ch, bam_preseq_ch, bam_bigwig_ch
when:
!params.downloadOnly
script:
"""
samtools sort -m 2G \
-@ ${task.cpus} \
-o ${id}.bam \
${bam} \
&& samtools index -@${task.cpus} ${id}.bam
"""
}
if (!params.noPreSeq){
process preSeq {
tag "${id}"
cpus 1
memory '16 GB'
container 'mblanche/preseq'
input:
tuple id, path(bam), path(idx) from bam_preseq_ch
output:
path "*_cmplx_stat" into preseq_ch
script:
"""
preseq lc_extrap -B -P -e 2.1e9 -s 1e8 -o ${id}_cmplx_stat ${bam} \
|| touch ${id}_cmplx_stat
"""
}
} else {
Channel
.empty()
.set { preseq_ch }
}
process alnStats {
tag "${gName}"
cpus 24
memory '150 GB'
container 'mblanche/r-prod-qc'
publishDir "${params.outDir}", mode: 'copy'
input:
bam_ch
.multiMap { id, bam, idx ->
bam: bam
idx: idx
}
.set{ result }
path(bams) from result.bam.collect()
path(idx) from result.idx.collect()
path(preseq) from preseq_ch.collect()
output:
path "*.csv" into alnStats_ch
script:
gName = params.groupName ? "${params.groupName}_alnStats":"alnStats"
"""
alnStats ${gName} ${task.cpus} \$(echo ${preseq}|tr ' ' ,) \$(echo ${bams}|tr ' ' ,)
"""
}
process adptStats {
tag "${gName}"
cpus 48
memory '150 GB'
container 'mblanche/r-prod-qc'
publishDir "${params.outDir}", mode: 'copy'
input:
path fastq from bs_stat_ch
.mix(fqDir_stat_ch)
.mix(s3_stat_ch)
.map{it[1]}
.collect()
output:
path "*.csv" into adptStats_ch
script:
gName = params.groupName ? "${params.groupName}_adpStats":"adptStats"
"""
adptStats ${task.cpus} ${fastq}
"""
}
process bam2bw {
tag "_${id}"
cpus 20
memory '200 GB'
container 'mblanche/r-cov'
publishDir "${params.outDir}/bigwigs",
mode: 'copy'
input:
tuple id, path(bam), path(idx) from bam_bigwig_ch
output:
tuple id, path ("*.bw") into bigwig_ch
script:
"""
bam2bw ${bam} ${id}.bw ${task.cpus}
"""
}