-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.nf
239 lines (174 loc) · 5.39 KB
/
main.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
process GENERATE_FAKE_FASTQ {
container 'community.wave.seqera.io/library/numpy:2.1.1--3063fc3d721f2cdf'
cpus 2
memory { 4.GB * task.attempt }
input:
tuple val(total_reads), val(file_index)
output:
path "random_sample_${file_index}.fastq"
script:
"""
#!/usr/bin/env python3
import numpy as np
import sys
def generate_random_sequence(length=53676):
bases = np.array(['A', 'C', 'G', 'T'], dtype='|S1')
return np.random.choice(bases, size=length).tobytes().decode('ascii')
def generate_random_quality(length=53676):
quality_ints = np.random.randint(0, 42, size=length) + 33
return ''.join(chr(q) for q in quality_ints)
def generate_fake_fastq(total_reads, file_index):
output_file = f"random_sample_{file_index}.fastq"
with open(output_file, 'w') as f:
for i in range(1, total_reads + 1):
seq_id = f"@fake_read_{file_index}_{i}"
sequence = generate_random_sequence()
quality = generate_random_quality()
f.write(f"{seq_id}\\n")
f.write(f"{sequence}\\n")
f.write("+\\n")
f.write(f"{quality}\\n")
print(f"Generated {output_file} with {total_reads} reads.")
generate_fake_fastq(${total_reads}, ${file_index})
"""
}
process CONCATENATE_FASTQ {
cpus 4
memory { 12.GB * task.attempt }
input:
path fastq_files
output:
path 'concatenated_samples.fastq'
script:
"""
cat ${fastq_files} > concatenated_samples.fastq
"""
}
process CHECKSUM_FASTQ {
cpus 4
memory { 6.GB * task.attempt }
input:
path fastq_file
output:
path 'checksum.md5'
script:
"""
md5sum ${fastq_file} > checksum.md5
"""
}
process COMPRESS_FASTQ {
container 'community.wave.seqera.io/library/pigz:2.8--cc287835d69f818b'
cpus 16
memory { 48.GB * task.attempt }
input:
path fastq_file
output:
path "${fastq_file}.gz", emit: compressed_fastq
script:
"""
pigz -p ${task.cpus} -c ${fastq_file} > ${fastq_file}.gz
"""
}
process MANY_SMALL_FILES {
cpus 2
memory { 6.GB * task.attempt }
input:
val num_files
output:
path 'generated_files', emit: files
path 'checksum.txt', emit: checksum
script:
"""
mkdir generated_files
for i in \$(seq 1 ${num_files}); do
hex_name=\$(printf "%x" \$i)
dd if=/dev/zero of=generated_files/\${hex_name}.bin bs=1M count=10
done
# Generate MD5 checksums
cd generated_files
find . -name '*.bin' -exec md5sum {} + > ../checksum.txt
"""
}
process COUNT_FILES {
cpus 2
memory { 6.GB * task.attempt }
input:
path files_folder
output:
stdout
script:
"""
cd ${files_folder}
find . -name '*.bin' -type f | wc -l
"""
}
process RENAME_AND_COMPRESS_FILES {
container 'community.wave.seqera.io/library/parallel:20240322--aeca7ef865f0e18b'
cpus 16
memory { 48.GB * task.attempt }
input:
path files_folder
output:
path 'renamed_files'
script:
"""
# First, create a copy of the original folder
cp -LR ${files_folder} copy_folder
# Now create the renamed_files directory and move files there
mkdir renamed_files
for file in copy_folder/*.bin; do
mv \$file renamed_files/\$(basename \$file)
done
# Compress all the files
cd renamed_files
find . -name '*.bin' -type f | parallel -j ${task.cpus} gzip
"""
}
process UNCOMPRESS_AND_VERIFY_FILES {
container 'community.wave.seqera.io/library/parallel:20240322--aeca7ef865f0e18b'
cpus 16
memory { 48.GB * task.attempt }
input:
path files_folder
path original_checksum
output:
path 'verification.txt'
script:
"""
# First, create a copy of the original folder
cp -LR ${files_folder} uncompressed_files
# Uncompress files
cd uncompressed_files
find . -name '*.gz' -type f | parallel -j ${task.cpus} gzip -d
# Verify checksums
md5sum -c ../${original_checksum} > ../verification.txt
if grep -q 'FAILED' verification_results.txt; then
echo "Checksum verification FAILED for some files"
exit 1
else
echo "All checksums verified successfully"
fi
"""
}
workflow {
// Create a channel with the parameters for each GENERATE_FAKE_FASTQ process
generate_params = Channel.from(1..params.num_files).map { it -> tuple(params.total_reads, it) }
// Run GENERATE_FAKE_FASTQ processes in parallel
fake_fastq_files = GENERATE_FAKE_FASTQ(generate_params)
// Collect all generated FASTQ files
collected_fastq_files = fake_fastq_files.collect()
// Concatenate all FASTQ files
CONCATENATE_FASTQ(collected_fastq_files)
// Compress the concatenated FASTQ file
COMPRESS_FASTQ(CONCATENATE_FASTQ.out)
// Checksum a big file (big read with small write)
CHECKSUM_FASTQ(CONCATENATE_FASTQ.out)
// Generate many small files in a single process
small_files = MANY_SMALL_FILES(params.small_files)
// Count how many files are generated
COUNT_FILES(small_files.files) | view { "Number of small files: $it" }
// Rename and compress all these files
RENAME_AND_COMPRESS_FILES(small_files.files)
// Extract and verify checksum of all the files
UNCOMPRESS_AND_VERIFY_FILES(RENAME_AND_COMPRESS_FILES.out, small_files.checksum)
}