-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.nf
212 lines (164 loc) · 6.23 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
/*
========================================================================================
FILE Download Statistics Workflow
========================================================================================
@#### Authors
Suresh Hewapathirana <[email protected]>
----------------------------------------------------------------------------------------
*/
/*
* Define the default parameters
*/
params.root_dir=''
params.output_file='parsed_data.parquet'
params.log_file=''
params.api_endpoint_file_download_per_project=''
params.api_endpoint_header=''
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
workDir : $workflow.workDir
RunName : $workflow.runName
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}
"""
process get_log_files {
label 'data_mover'
input:
val root_dir
output:
path "file_list.txt"
script:
"""
python3 ${workflow.projectDir}/filedownloadstat/main.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_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/main.py run_log_file_stat \
--file ${file_paths} \
--output "log_file_statistics.html"
"""
}
process process_log_file {
label 'process_very_low'
label 'data_mover'
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/main.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 analyze_parquet_files {
label 'process_low'
input:
val all_parquet_files // A comma-separated string of file paths
output:
path("file_download_counts.json"), emit: file_download_counts
path("summed_accession_counts.json"), emit: summed_accession_counts
path("all_data.json"), emit: all_data
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/main.py get_file_counts \
--input_dir all_parquet_files_list.txt \
--output_grouped file_download_counts.json \
--output_summed summed_accession_counts.json \
--all_data all_data.json
"""
}
process run_file_download_stat {
label 'process_low'
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/main.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 uploadJsonFile {
input:
path jsonFile // The JSON file to upload
output:
path "upload_response.txt" // Capture the response from the server
script:
"""
curl --location '${params.api_endpoint_file_download_per_project}' \
--header '${params.api_endpoint_header}' \
--form 'files=@\"${jsonFile}\"' > upload_response.txt
"""
}
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
// Step 3: Analyze Parquet files
analyze_parquet_files(parquet_file_list)
// Debug: View individual outputs
analyze_parquet_files.out.file_download_counts.view()
analyze_parquet_files.out.summed_accession_counts.view()
// Step 4: Generate Statistics for file downloads
run_file_download_stat(analyze_parquet_files.out.all_data)
// Step 5: Upload the JSON file
// uploadJsonFile(summed_accession_counts) // TODO: Only testing purpose
}