forked from PRIDE-Archive/nf-downloadstats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.nf
351 lines (276 loc) · 11.5 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
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
/*
========================================================================================
FILE Download Statistics Workflow
========================================================================================
@#### Authors
Suresh Hewapathirana <sureshhewabi@gmail.com>
----------------------------------------------------------------------------------------
*/
/*
* Define the default parameters
*/
params.root_dir=''
params.output_file='parsed_data.parquet'
params.log_file=''
params.api_endpoint_file_download_per_project=''
params.protocols=''
log.info """\
===================================================
F I L E D O W N L O A D S T A T I S T I C S
===================================================
FOR DEVELOPERS USE
SessionId : $workflow.sessionId
LaunchDir : $workflow.launchDir
projectDir : $workflow.projectDir
dataDir : ${params.root_dir}
workDir : $workflow.workDir
RunName : $workflow.runName
Profile : $workflow.profile
NextFlow version : $nextflow.version
Nextflow location : ${params.nextflow_location}
Date : ${new java.util.Date()}
Protocols : ${params.protocols}
Resource Identifiers: ${params.resource_identifiers}
Completeness : ${params.completeness}
Public/Private : ${params.public_private}
Report Template : ${params.report_template}
Batch Size : ${params.log_file_batch_size}
Resource Base URL : ${params.resource_base_url}
Report copy location: ${params.report_copy_filepath}
Skipped Years : ${params.skipped_years}
Accession Pattern : ${params.accession_pattern}
chunk size for file : ${params.chunk_size}
Disable DB Update : ${params.disable_db_update}
api_endpoint_file_downloads_per_project : ${params.api_endpoint_file_downloads_per_project}
api_endpoint_file_downloads_per_file : ${params.api_endpoint_file_downloads_per_file}
"""
process get_log_files {
label 'process_very_low'
input:
val root_dir
output:
path "file_list.txt"
script:
"""
python3 ${workflow.projectDir}/filedownloadstat/file_download_stat.py get_log_files \
--root_dir $root_dir \
--output "file_list.txt" \
--protocols "${params.protocols.join(',')}" \
--public "${params.public_private.join(',')}"
"""
}
process run_log_file_stat{
label 'process_very_low'
input:
val file_paths // Input the file generated by get_log_files
output:
path "log_file_statistics.html" // Output the visualizations as an HTML report
script:
"""
python3 ${workflow.projectDir}/filedownloadstat/file_download_stat.py run_log_file_stat \
--file ${file_paths} \
--output "log_file_statistics.html"
"""
}
process process_log_file {
label 'process_very_low'
label 'error_retry_max' // try 30 times
input:
val file_path // Each file object from the channel
output:
path "*.parquet",optional: true // Output files with unique names
script:
"""
# Extract a unique identifier from the log file name
filename=\$(basename ${file_path} .log.tsv.gz)
python3 ${workflow.projectDir}/filedownloadstat/file_download_stat.py process_log_file \
-f ${file_path} \
-o "\${filename}.parquet" \
-r "${params.resource_identifiers.join(",")}" \
-c "${params.completeness.join(",")}" \
-b ${params.log_file_batch_size} \
-a ${params.accession_pattern} \
> process_log_file.log 2>&1
"""
}
process merge_parquet_files {
label 'process_low'
label 'error_retry_medium'
input:
val all_parquet_files // A comma-separated string of file paths
output:
path("output_parquet"), emit: output_parquet
script:
"""
# Write the file paths to a temporary file, because otherwise Argument list(file list) will be too long
echo "${all_parquet_files.join('\n')}" > all_parquet_files_list.txt
python3 ${workflow.projectDir}/filedownloadstat/file_download_stat.py merge_parquet_files \
--input_dir all_parquet_files_list.txt \
--output_parquet "output_parquet"
"""
}
process analyze_parquet_files {
label 'process_low'
// label 'process_high'
label 'error_retry_medium'
input:
val output_parquet
output:
path("project_level_download_counts.json"), emit: project_level_download_counts
path("file_level_download_counts.json"), emit: file_level_download_counts
path("project_level_yearly_download_counts.json"), emit: project_level_yearly_download_counts
path("project_level_top_download_counts.json"), emit: project_level_top_download_counts
path("all_data.json"), emit: all_data
script:
"""
python3 ${workflow.projectDir}/filedownloadstat/file_download_stat.py analyze_parquet_files \
--output_parquet ${output_parquet} \
--project_level_download_counts project_level_download_counts.json \
--file_level_download_counts file_level_download_counts.json \
--project_level_yearly_download_counts project_level_yearly_download_counts.json \
--project_level_top_download_counts project_level_top_download_counts.json \
--all_data all_data.json
"""
}
process run_file_download_stat {
label 'process_low'
// label 'process_high'
label 'error_retry_medium'
input:
path all_data // Input the file generated by analyze_parquet_files
output:
path "file_download_stat.html" // Output the visualizations as an HTML report
script:
"""
python3 ${workflow.projectDir}/filedownloadstat/file_download_stat.py run_file_download_stat \
--file ${all_data} \
--output "file_download_stat.html" \
--report_template ${params.report_template} \
--baseurl ${params.resource_base_url} \
--report_copy_filepath ${params.report_copy_filepath} \
--skipped_years "${params.skipped_years.join(',')}"
"""
}
process update_project_download_counts {
label 'error_retry'
input:
path project_level_download_counts // The JSON file to upload
output:
path "upload_response_file_downloads_per_project.txt" // Capture the response from the server
script:
"""
curl --location --max-time 300 '${params.api_endpoint_file_downloads_per_project}' \
--header '${params.api_endpoint_header}' \
--form 'files=@\"${project_level_download_counts}\"' > upload_response_file_downloads_per_project.txt
"""
}
process update_project_yearly_download_counts {
label 'error_retry'
input:
path project_level_yearly_download_counts // The JSON file to upload
output:
path "upload_response_file_downloads_YEARLY_per_project.txt" // Capture the response from the server
script:
"""
curl --location --max-time 300 '${params.api_endpoint_file_downloads_per_project}' \
--header '${params.api_endpoint_header}' \
--form 'files=@\"${project_level_yearly_download_counts}\"' > upload_response_file_downloads_YEARLY_per_project.txt
"""
}
process update_file_level_download_counts {
// label 'process_medium_memory'
label 'error_retry'
input:
path file_level_download_counts // The large JSON file to process
script:
"""
# Create directories for split files and responses
mkdir -p split_files upload_responses
# Initialize variables
chunk_size=${params.chunk_size} # Number of objects per chunk
total_lines=\$(jq '. | length' ${file_level_download_counts}) # Total number of objects in JSON
chunk_number=0
start_index=0
echo "=== STARTING PROCESS ==="
echo "Total lines in input JSON: \$total_lines"
echo "Chunk size: \$chunk_size"
# Split the JSON file into chunks
while [ \$start_index -lt \$total_lines ]; do
end_index=\$((start_index + chunk_size))
echo "Creating chunk \$chunk_number: Start index = \$start_index, End index = \$end_index"
# Use jq to extract a chunk of JSON objects
jq -c ". | .[\${start_index}:\${end_index}]" ${file_level_download_counts} > "split_files/part_\${chunk_number}.json"
chunk_file="split_files/part_\${chunk_number}.json"
# Check if the chunk file is valid and non-empty
if [ -s "\$chunk_file" ]; then
echo "Chunk file created: \$chunk_file (Size: \$(wc -c < "\$chunk_file") bytes)"
else
echo "Warning: Chunk file \$chunk_file is empty!"
fi
# Increment start index and chunk number
start_index=\$((end_index))
chunk_number=\$((chunk_number + 1))
done
echo "=== CHUNKING COMPLETED ==="
echo "Total chunks created: \$chunk_number"
echo "Files in split_files directory:"
ls -lh split_files/
# Process and upload each JSON file
for part_file in split_files/*; do
echo "Processing file: \$part_file (Size: \$(wc -c < "\$part_file") bytes)"
# Upload the JSON file and capture the server response
curl --location --max-time 300 '${params.api_endpoint_file_downloads_per_file}' \
--header '${params.api_endpoint_header}' \
--form "files=@\${part_file}" \
> "upload_responses/\$(basename \${part_file})_response.txt"
if [ \$? -eq 0 ]; then
echo "Response saved to upload_responses/\$(basename \${part_file})_response.txt"
else
echo "Error: Upload failed for file \$part_file"
fi
done
echo "=== UPLOAD COMPLETED ==="
echo "Responses saved in upload_responses/ directory:"
ls -lh upload_responses/
"""
}
workflow {
// Step 1: Gather file names
def root_dir = params.root_dir
def file_paths = get_log_files(root_dir)
// Step 2: Run statistics in parallel with processing log files
def stats_file = run_log_file_stat(file_paths)
file_paths
.splitText() // Split file_list.txt into individual lines
.map { it.split('\t')[0].trim() } // Split each line by tab and take the first column (file name)
.set { file_path } // Save the channel
// Step 2: Process each log file and generate Parquet files
def all_parquet_files = process_log_file(file_path)
// Collect all parquet files into a single channel for analysis
all_parquet_files
.collect() // Collect all parquet files into a single list
.set { parquet_file_list } // Save the collected files as a new channel
merge_parquet_files(parquet_file_list)
// Step 3: Analyze Parquet files
analyze_parquet_files(merge_parquet_files.out.output_parquet)
// Step 4: Generate Statistics for file downloads
run_file_download_stat(analyze_parquet_files.out.all_data)
// Step 5: Update project level downloads in MongoDB
if (!params.disable_db_update) {
update_project_download_counts(analyze_parquet_files.out.project_level_download_counts)
} else {
println "Skipping update_project_download_counts because disable_db_update=true"
}
// Step 6: Update project level YEARLY downloads in MongoDB
if (!params.disable_db_update) {
update_project_yearly_download_counts(analyze_parquet_files.out.project_level_yearly_download_counts)
} else {
println "Skipping update_project_yearly_download_counts because disable_db_update=true"
}
// Step 7: Update project level downloads in MongoDB
if (!params.disable_db_update) {
update_file_level_download_counts(analyze_parquet_files.out.file_level_download_counts)
} else {
println "Skipping update_file_level_download_counts because disable_db_update=true"
}
}