-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmain.nf
More file actions
558 lines (525 loc) · 17.5 KB
/
main.nf
File metadata and controls
558 lines (525 loc) · 17.5 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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
nextflow.enable.dsl=2
// Validate and print key pipeline parameters
ParamLogger.initialise(workflow, params, log)
include { INPUT_READS } from './modules/input_reads.nf'
include { SCALE_PLEX } from './modules/scale_plex.nf'
include { SCALE_PLEX as SCALE_PLEX_REPORTING} from './modules/scale_plex.nf'
include { ALIGNMENT } from './modules/alignment.nf'
include { MultiSampleReport } from './modules/sample_reporting.nf'
include { MultiLibraryReport } from './modules/internal.nf'
include { ULTIMA } from './modules/ultima.nf'
include { SAMPLE_REPORTING } from './modules/sample_reporting.nf'
include { CELL_CALLING } from './modules/create_mtx.nf'
include { DOWNSTREAM } from './modules/downstream.nf'
include { DOWNSTREAM as COMPARE_DOWNSTREAM } from './modules/downstream.nf'
include { MERGING } from './modules/merging.nf'
include { getTotalSampleReads } from './modules/utils.nf'
include { shouldRunLibDetection } from './modules/utils.nf'
include { expandPath } from './modules/utils.nf'
include { loadGenome } from './modules/utils.nf'
include { resolveScalePlexToRnaMapping } from './modules/utils.nf'
// Prepare samples.csv with defaults, rename legacy columns, etc.
process RegularizeSamplesCsv {
label 'small'
cache 'deep'
// Publish user provided samples csv to output directory
publishDir params.outputDir, pattern: "samples.in.csv", mode: 'copy', saveAs: {'samples.csv'}
input:
path("samples.in.csv")
// Directory containing the library structure definition (barcode sequence lists are loaded from here)
path("references")
// Filename of the library structure definition .json
val(libStructName)
// File containing the mapping of scale plex to RNA PCR barcodes
path(mapping_file)
// Whether data was generated using QuantumScale assay
val(quantum)
output:
path("samples.csv"), emit: samples_csv
path("barcode_range_unwrapped_samples.csv"), emit: barcode_unwrapped_samples_csv, optional: true
path("samples.in.csv")
script:
libStruct = "references/$libStructName"
opts=""
if (params.scalePlex) {
opts += "--scalePlex "
if (mapping_file) {
opts += "--scalePlexToRnaMapping $mapping_file "
}
}
if (params.splitFastq) {
opts += "--splitSample "
}
if (params.reporting) {
opts += "--reporting "
}
if (params.resultDir) {
opts += "--resultDir ${params.resultDir} "
}
if (quantum) {
opts += "--quantum "
}
if (params.ultimaCramDir) {
opts += "--ultima "
}
if (params.fastqDir || params.ultimaCramDir) {
opts += "--fastq "
}
"""
regularize_samples_csv.py samples.in.csv --libraryStruct $libStruct $opts
"""
}
// Generate metrics for each library from the metrics of all samples in the library
process LibraryMetricsGeneration {
tag "$libName"
label 'large'
input:
tuple(val(libName), val(samples), path("allCells*.parquet"))
val(hashLibNames)
val(quantum)
output:
tuple(val(libName), path("library_${libName}_metrics"))
script:
opts = ""
if (quantum && params.internalReport && !(params.scalePlex && libName in hashLibNames)) {
opts += " --beadMetrics "
}
"""
get_library_metrics_from_sample_metrics.py --sample_metrics allCells* --libName ${libName} $opts
"""
}
// Generate report for each library from metrics generated in LibraryMetricsGeneration
process LibraryReportGeneration {
tag "$libName"
label 'report'
label 'optional'
publishDir params.outputDir, mode: 'copy'
input:
tuple(val(libName), path(demuxJson), path("metrics"), path(beadScores))
val(libJsonFn)
path(libStructDir)
val(scalePlexLibJsonFn)
path("scalePlexLibStructDir") // Cannot use a variable here since it will cause input name collision if libStructDir and scalePlexLibStructDir are the same
val(hashLibNames)
output:
path("${outDir}/library_${libName}.report.html")
path("${outDir}/csv/library_${libName}.overallMatches.csv"), emit: overallMatches, optional: true
path("${outDir}/csv/library_${libName}.typeLevelMatches.csv"), emit: typeLevelMatches, optional: true
path("${outDir}/csv/library_${libName}_unique_reads*.csv")
path("${outDir}/figures_internal"), optional: true
script:
outDir = "reports/library"
opts= "--minPassingSampleReads ${params.minPassingSampleReads} "
if (params.internalReport) {
opts = opts + "--internalReport --minDivergence ${params.minBeadDivergence} "
}
if (params.ultimaCramDir) {
opts = opts + "--ultima "
} else {
opts = opts + "--demuxMetrics $demuxJson "
}
if (params.scalePlex && libName in hashLibNames) {
libJson = "scalePlexLibStructDir/${scalePlexLibJsonFn}"
opts += " --scalePlexLib"
} else {
libJson = "${libStructDir}/${libJsonFn}"
}
"""
export DATAPANE_CDN_BASE="https://d3j2ibc7el7s1k.cloudfront.net/v0.17.0"
export TMPDIR=\$(mktemp -p `pwd` -d)
generate_library_report.py --libName $libName --outDir $outDir --libStruct $libJson --libMetrics metrics $opts
"""
}
//// Main entry point
// Run the workflow for one or multiple samples
// either from reads (--runFolder/ --fastqDir) or pre-existing alignment results (--resultDir)
workflow {
// Load rna library structure json
libJson = expandPath(params.libStructure, file(projectDir) / "references")
libJsonContents = Utils.loadJson(libJson)
// Load scalePlex library structure json
scalePlexLibJson = expandPath(params.scalePlexLibStructure, file(projectDir) / "references")
scalePlexLibJsonContents = Utils.loadJson(scalePlexLibJson)
scalePlexToRnaMapping = resolveScalePlexToRnaMapping(scalePlexLibJson)
libraryInfo = [
rnaLibraryStructureFile: libJson,
rnaTrimAdapter: libJsonContents["trimAdapter"],
rnaGenomicR1: libJsonContents.get('genomic_r1', false),
rnaGenomicR2: libJsonContents.get('genomic_r2', false),
scalePlexLibraryStructureFile: scalePlexLibJson,
scalePlexTrimAdapter: scalePlexLibJsonContents["trimAdapter"],
scalePlexToRnaMappingFile: scalePlexToRnaMapping,
quantum: libJsonContents.get('quantum', false),
sampleBarcode: libJsonContents["sample_barcode"],
]
// Prepare and load samples.csv
RegularizeSamplesCsv(
file(params.samples, checkIfExists:true),
libraryInfo.rnaLibraryStructureFile.getParent(),
libraryInfo.rnaLibraryStructureFile.getName(),
libraryInfo.scalePlexToRnaMappingFile,
libraryInfo.quantum
)
samplesCsv = RegularizeSamplesCsv.out.samples_csv
samples = samplesCsv.splitCsv(header:true, strip:true)
samples.dump(tag:'samples')
runLibDetection = shouldRunLibDetection(file(params.samples, checkIfExists:true), libraryInfo.quantum)
if (params.ultimaCramDir) {
RegularizeSamplesCsv.out.barcode_unwrapped_samples_csv.splitCsv(header:true, strip:true)
.dump(tag:'barcodeUnwrappedSamplesCsv')
.set {barcodeUnwrappedSamplesCsv}
} else {
barcodeUnwrappedSamplesCsv = []
}
genome = loadGenome(file(params.genome, checkIfExists:true))
isBarnyard = genome.get('isBarnyard', false)
if (params.reporting) {
// For reporting run, validate that all samples are present in the resultDir
samples
.map { it.sample }
.unique()
.collect()
.dump(tag:'sampleNames')
.set { sampleNames }
samples
.filter { it.resultDir == null }
.map {
ParamLogger.throwError("Sample ${it.id} does not have a resultDir")
}
// Filter out sample-library combinations not present in the resultDir
samples
.filter { it.rnaId == null && !(file(it.resultDir) / "alignment" / "${it.id}" / "${it.id}.star.solo").exists() }
.map { it.id }
.collect()
.dump(tag:'missingLibs')
.set { missingLibs }
samples
.filter {
// Keep sample-library combinations present in the resultDir
def id = it.rnaId ?: it.id
!(id in missingLibs.val)
}
.dump(tag:'filteredSamples')
.set { samples }
// Check that no samples were completely filtered out
samples
.map { it.sample }
.unique()
.collect()
.ifEmpty { [] }
.dump(tag:'filteredSampleNames')
.set { filteredSampleNames }
sampleNames
.flatMap()
.filter { !(it in filteredSampleNames.val) }
.map {
ParamLogger.throwError("Sample ${it} was not found in the resultDir")
}
samples
.filter { it.rnaId == null }
.map { sample -> getTotalSampleReads(sample).flatten() }
.dump(tag:'totalSampleReadsBySampleID')
.set { totalSampleReadsBySampleID }
// Load STARsolo output from previous pipeline output directory
samples
.filter { it.rnaId == null } // Only RNA samples have alignment results
.map {
def alignmentDir = file(it.resultDir) / "alignment"
tuple(it.id, file(alignmentDir / "${it.id}" / "${it.id}.star.solo", checkIfExists:true))
}
.dump(tag:'soloOut')
.set { soloOut }
// Load STARsolo log file from previous pipeline output directory
samples
.filter { it.rnaId == null } // Only RNA samples have alignment results
.map {
def alignmentDir = file(it.resultDir) / "alignment"
tuple(it.id, file(alignmentDir / "${it.id}" / "${it.id}.star.align" / "Log.final.out", checkIfExists:true))
}
.dump(tag:'soloLog')
.set { soloLog }
if (params.scalePlex) {
samples
.filter { it.rnaId != null } // Hash samples
.filter {
def scalePlexDir = file(it.resultDir) / "scaleplex"
def cellMetricsFile = scalePlexDir.resolve("${it.id}.cellMetrics.parquet")
def rawMatrixFile = scalePlexDir.resolve("${it.id}.raw.matrix/matrix.mtx.gz")
cellMetricsFile.exists() && rawMatrixFile.exists()
}
.map {
def scalePlexDir = file(it.resultDir) / "scaleplex"
[
[lib: it.libName, sample: it.sample],
scalePlexDir.resolve("${it.id}.cellMetrics.parquet"),
scalePlexDir.resolve("${it.id}.raw.matrix/matrix.mtx.gz"),
]
}
.set { perSampleCountRaw }
}
} else {
if (params.ultimaCramDir) {
ULTIMA(
samples,
barcodeUnwrappedSamplesCsv,
libraryInfo,
)
alignmentInput = ULTIMA.out.alignmentInput
ubamHash = ULTIMA.out.ubamHash
samples = ULTIMA.out.samples
// Construct totalSampleReadsBySampleID channel and set it to 0 for all samples
samples
.filter { it.rnaId == null } // Only RNA samples are passed on to CELL_CALLING
.map {
tuple(it.id, 0)
}
.set { totalSampleReadsBySampleID }
}
else {
// INPUT_READS gets/generates fastq files and runs bcParser to demux per sample
INPUT_READS(
samples,
samplesCsv,
libraryInfo,
params.runFolder,
params.fastqDir,
runLibDetection
)
alignmentInput = INPUT_READS.out.ubam
ubamHash = INPUT_READS.out.ubamHash
totalSampleReadsBySampleID = INPUT_READS.out.totalSampleReadsBySampleID
samples = INPUT_READS.out.samples
}
// STARSolo
ALIGNMENT(alignmentInput, genome)
ALIGNMENT.out.soloOut
.dump(tag:'soloOut')
.set { soloOut }
ALIGNMENT.out.soloLog
.dump(tag:'soloLog')
.set { soloLog }
}
// Cell calling and metrics generation
CELL_CALLING(
samples,
libraryInfo,
soloOut,
isBarnyard,
soloLog,
totalSampleReadsBySampleID,
)
//// ScalePlex
allCellsWithAssignment = Channel.empty()
if (params.scalePlex) {
SCALE_PLEX(
libraryInfo,
samples,
params.reporting ? [] : ubamHash, // No ubamHash for reporting run
CELL_CALLING.out.allCells,
CELL_CALLING.out.libCount,
params.reporting ? perSampleCountRaw : [] // perSampleCountRaw is empty if not a --reporting run
)
SCALE_PLEX
.out.metrics
// [meta, cellMetrics, filteredMtx, scaleplex_stats.csv, metrics.csv]
.map { [it[0].rnaId, it[1], it[3], it[4], it[0].lib] }
// [rnaId, cellMetrics, scaleplex_stats.csv, metrics.csv, hashLibName]
.set { scalePlexResults }
SCALE_PLEX
.out.allCells
// [meta, cellMetrics]
.map { [it[0].rnaId, it[1]] }
// [rnaId, library, cellMetrics]
.dump(tag:'allCellsWithAssignment')
.set { allCellsWithAssignment }
}
//// REPORTING
// Downstream analysis and per sample QC report
if (params.seurat || params.azimuth || params.annData) {
DOWNSTREAM(
samples,
CELL_CALLING.out.allCells,
CELL_CALLING.out.filteredMtx,
CELL_CALLING.out.cellCallingStats,
libJson,
false,
)
if (params.compSamples) {
COMPARE_DOWNSTREAM(
samples,
CELL_CALLING.out.allCells,
CELL_CALLING.out.filteredMtx,
CELL_CALLING.out.cellCallingStats,
libJson,
true,
)
}
}
SAMPLE_REPORTING(
samples,
libJson,
isBarnyard,
CELL_CALLING.out.allCells,
CELL_CALLING.out.allBarcodes,
CELL_CALLING.out.libCount,
CELL_CALLING.out.sampleStats,
params.scalePlex ? scalePlexResults : [],
false,
)
sampleStats = SAMPLE_REPORTING.out.sampleStats
CELL_CALLING.out.allCells
.join(allCellsWithAssignment, remainder: true)
.map {
id, lib, allCells, allCellsWithAssignment ->
// If allCellsWithAssignment is null, use allCells
[id, lib, allCellsWithAssignment ?: allCells]
}
.dump(tag:'allCells')
.set { allCells }
if (params.merge) {
// Concatenate cell metrics, filtered mtx, and STAR log for individual libraries
// Create merged sample reports and do downstream analysis
MERGING(
samples,
libJson,
isBarnyard,
allCells,
CELL_CALLING.out.allBarcodes,
CELL_CALLING.out.filteredMtx,
soloLog,
params.scalePlex ? scalePlexResults : [],
totalSampleReadsBySampleID,
)
sampleStats = sampleStats.concat(MERGING.out.sampleStats)
}
sampleStats.dump(tag:'sampleStats')
MultiSampleReport(sampleStats.collect()) // Combined original and merged samples
// Per library QC report
// Usually not re-run for reporting because there are no changes but if --internalReport,
// that tab has beads per passing cell which can change in reporting run
if (!params.reporting || params.internalReport) {
CELL_CALLING
.out.allBarcodes
.map {
tuple(it[1], it[0], it[2]) // [libName, sample, allCells]
}.groupTuple()
.set { metricsByLib }
if (params.scalePlex) {
metricsByLib.concat(
SCALE_PLEX
.out.metrics
.map {
// [meta, cellMetrics, filteredMtx, scaleplex_stats.csv, metrics.csv]
tuple(it[0].lib, it[0].sample, it[1])
}.groupTuple()
).set { metricsByLib }
}
metricsByLib.dump(tag:'metricsByLib')
// Get hash lib names
samples
.filter { it.rnaId != null }
.collect { it.libName }
.ifEmpty { [] }
.dump(tag:'hashLibNames')
.set { hashLibNames }
LibraryMetricsGeneration(
metricsByLib,
hashLibNames,
libraryInfo.quantum
)
// attach read metrics to library metrics for reporting
if (params.ultimaCramDir) {
// Empty list indicates bcParser demux metrics, which do not exist when starting from cram files
LibraryMetricsGeneration.out
.map { tuple(it[0], [], it[1]) }
.set { libraryReportMetrics }
}
else if (params.reporting) {
// Load metrics.json from previous pipeline output directory
samples
.map { it.subMap("libName", "resultDir") }
.unique()
.map {
def barcodesDir = file(it.resultDir) / "barcodes"
if (it.libName in hashLibNames.val) {
barcodesDir = file(it.resultDir).resolve("scaleplex/demux")
}
// libName, metrics.json
[it.libName, file(barcodesDir / "${it.libName}.metrics.json")]
}
// It's possible for the ScalePlex library to be dropped in the original run
.filter { lib, metricsFile ->
metricsFile.exists()
}
.join(LibraryMetricsGeneration.out)
.set { libraryReportMetrics }
} else {
INPUT_READS
.out.metrics.join(LibraryMetricsGeneration.out)
.set { libraryReportMetrics }
}
libraryReportMetrics.dump(tag:'libraryReportMetrics')
LibraryReportGeneration(
libraryReportMetrics.join(CELL_CALLING.out.beadScores, remainder: true).map {
libName, demuxJson, metrics, beadScores ->
// [libName, demuxJson, metrics, beadScores]
[libName, demuxJson, metrics, beadScores ?: []] // path value cannot be null
},
libraryInfo.rnaLibraryStructureFile.getName(),
libraryInfo.rnaLibraryStructureFile.getParent(),
libraryInfo.scalePlexLibraryStructureFile.getName(),
libraryInfo.scalePlexLibraryStructureFile.getParent(),
hashLibNames
)
// Barcode stats do not exist when starting from ultima cram files
if (params.internalReport && !params.ultimaCramDir) {
MultiLibraryReport(
LibraryReportGeneration.out.typeLevelMatches.collect(),
LibraryReportGeneration.out.overallMatches.collect()
)
}
}
}
//// When the workflow completes, publish 'workflow_info.json' containing information on pipeline metadata
workflow.onComplete {
testing = false
def workflow_data = ["Nextflow Information":
["Execution status": "${ workflow.success ? 'OK' : 'failed' }",
"Run name": "$workflow.runName",
"Pipeline completion timestamp": "$workflow.complete",
"Git repo URL": "$workflow.repository",
"Configuration files": "$workflow.configFiles",
"Container": "$workflow.container",
"Command line executed": "$workflow.commandLine",
"Configuration profile": "$workflow.profile",
"Start timestamp": "$workflow.start",
"Stop timestamp": "$workflow.complete",
"Exit status": "$workflow.exitStatus",
"Error message": "$workflow.errorMessage",
"Revision": "$workflow.revision",
"Commit ID": "$workflow.commitId",
"Duration": "$workflow.duration"]
]
def params_data = ["Parameters": [:]]
for (p in params) {
if (!p.key.contains('-')) {
params_data."Parameters".put("$p.key", "$p.value")
}
}
def reference_data = ["Reference Genome": [:]]
for (p in genome) {
reference_data."Reference Genome".put("$p.key", "$p.value")
}
def manifest_data = ["Workflow Manifest": [:]]
for (p in workflow.manifest.getProperties()) {
p = p.toString()
def split_str = p.split("=")
if (split_str[0].equals("name") || split_str[0].equals("version")) {
manifest_data["Workflow Manifest"].put(split_str[0], split_str[1])
}
}
def json_str = groovy.json.JsonOutput.toJson(manifest_data + workflow_data +params_data+reference_data)
def json_beauty = groovy.json.JsonOutput.prettyPrint(json_str)
def workflow_info = file(params.outputDir) / "workflow_info.json"
workflow_info.write(json_beauty)
}