-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.nf
More file actions
94 lines (79 loc) · 3.28 KB
/
main.nf
File metadata and controls
94 lines (79 loc) · 3.28 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
nextflow.enable.dsl=2
include { AUTH } from './modules/auth'
include { PREPROCESSING } from './modules/preprocess'
include { CRYPTIC_VARIANT_DETECTION } from './modules/cryptic_variant_detection'
workflow {
if( !params.metadata ) {
throw new IllegalArgumentException("Provide a metadata file containing sample_id and primer_scheme via --metadata.")
}
def metadataFile = file(params.metadata)
if( !metadataFile.exists() ) {
throw new FileNotFoundException("Metadata file not found: ${metadataFile}")
}
def metadata_ch = Channel
.fromPath(metadataFile)
.splitCsv(header: true)
.map { Map record ->
def sampleId = (record.sample_id ?: '').toString().trim()
def primerScheme = (record.primer_scheme ?: '').toString().trim()
def collectionDate = (record.collection_date ?: '').toString().trim()
def primerFile = file("${params.primer_dir}/${primerScheme}.bed")
if( !primerFile.exists() ) {
throw new FileNotFoundException("Primer scheme '${primerScheme}' not found in ${params.primer_dir} (expected ${primerFile}).")
}
tuple(sampleId, primerFile)
}
// Get all fastq files and group by sample ID
def all_reads_ch = Channel
.fromPath(params.reads)
.map { file ->
def name = file.getName()
def sample_id = name
.replaceAll(/\.fastq\.gz$/, '')
// Handle Illumina format: _S\d+_L\d+_R[12]_\d+ (e.g., _S8_L001_R1_001)
.replaceAll(/_S\d+_L\d+_R[12]_\d+$/, '')
// Handle simple format: _R[12] (e.g., _R1, _R2)
.replaceAll(/_[R12]$/, '')
tuple(sample_id, file)
}
.groupTuple()
.map { sample_id, files ->
def reads
if (files.size() == 2) {
def sorted = files.sort { it.getName() }
// Paired end
reads = sorted
} else if (files.size() == 1) {
// Single-end
reads = files[0]
} else {
throw new IllegalArgumentException("Unexpected number of files (${files.size()}) for sample ${sample_id}: ${files}")
}
tuple(sample_id, reads)
}
// Get sample IDs from metadata and join with reads
def sample_ids_ch = Channel
.fromPath(metadataFile)
.splitCsv(header: true)
.map { Map record -> (record.sample_id ?: '').toString().trim() }
reads_ch = sample_ids_ch
.map { sample_id -> tuple(sample_id, null) }
.join(all_reads_ch, by: 0)
.map { sample_id, meta, reads ->
if (reads == null) {
throw new FileNotFoundException("No reads found for sample ${sample_id} matching pattern ${params.reads}")
}
tuple(sample_id, reads)
}
reference_ch = Channel.value(file(params.reference))
annotation_ch = Channel.value(file(params.annotation))
auth_status = AUTH()
final_bams = PREPROCESSING(reads_ch, reference_ch, metadata_ch)
CRYPTIC_VARIANT_DETECTION(
final_bams,
reference_ch,
annotation_ch,
Channel.value(metadataFile),
file(params.detect_cryptic_script)
)
}