-
Notifications
You must be signed in to change notification settings - Fork 0
/
msms.nf
419 lines (372 loc) · 12.2 KB
/
msms.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
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
process extract_exp_info {
container "${params.container.r_tidyverse}"
cpus 1
memory '4 GB'
executor 'local'
input:
path('manifest.txt')
output:
path "exp_*.tsv", emit: exp_ch
"""
#!/usr/bin/env Rscript
library(tidyverse)
mani <- read_tsv("manifest.txt", col_types = cols(experiment = col_character()))
exp <- mani %>% group_by(experiment)
experiments <- group_split(exp)
for (i in 1:length(experiments)){
exp_name <- unique(experiments[[i]][["experiment"]])
write_tsv(experiments[[i]], paste0("exp_", exp_name, ".tsv"), col_names=FALSE)
}
"""
}
process set_exp_info {
container "${params.container.ubuntu}"
cpus 1
memory '4 GB'
executor 'local'
input:
path(exp_info_file)
output:
tuple env(exp_name), path(exp_info_file), emit: res_ch
"""
exp_info=${exp_info_file}
exp_name=\${exp_info#exp_}
exp_name=\${exp_name%.tsv}
"""
}
process extract_s3_path {
container "${params.container.python}"
cpus 1
memory '4 GB'
executor 'local'
input:
tuple val(experiment), path('exp_info.txt')
output:
tuple val(experiment), env(s3_uri), emit: s3_ch
"""
# only read the first line
while IFS=\$'\\t' read -r -a myline
do
s3_uri="\${myline[5]}"
break
done < exp_info.txt
"""
}
process peptide_identification {
label 'r5_2xlarge'
container "${params.container.neoflow}"
cpus 8
memory '60 GB'
publishDir "${params.outdir_run}/msms_searching/",
mode: 'copy',
pattern: 'exp_*/*.mzid',
overwrite: true
input:
tuple val(experiment), path('exp_info.txt'), path(mzml_tar), path('ref.fasta')
path(msms_para_file)
output:
tuple val(experiment),
path("exp_*/*.mzML"),
emit: mzml_ch
tuple val(experiment),
path("exp_*/*${output_pattern}"),
emit: mzid_ch
script:
if (params.search_engine == 'msgf') {
output_pattern = ".mzid"
"""
# mzml_tar contains all mzml file
filename="exp_info.txt"
output_dir="exp_${experiment}"
mkdir -p \${output_dir}
ARCHIVE=${mzml_tar}
CONTENTS=\$(tar -tf "\$ARCHIVE")
# Check if the first entry in the archive is a directory
FIRST_ENTRY=\$(echo "\$CONTENTS" | head -n 1)
if [[ "\$FIRST_ENTRY" == */ ]]; then
tar -xvf "\$ARCHIVE" --strip-components 1
else
tar -xvf "\$ARCHIVE"
fi
mv *.gz \${output_dir}
head -n 1 \$filename | while read -r sample experiment wxs_file_name wxs_file_location mzml_files mzml_path fusion
do
IFS=';' read -r -a allNames <<< "\$mzml_files"
for index in "\${!allNames[@]}"
do
eachName="\${allNames[\$index]}"
gunzip \${output_dir}/\${eachName}
msmsName=\${output_dir}/\${eachName}
msmsName=\${msmsName/.gz/}
java -Xmx${params.search_engine_mem}g -jar /opt/MSGFPlus.jar \
-thread ${task.cpus}\
-s \$msmsName \
-d ref.fasta \
-tda 0 \
-o \${msmsName}.mzid \
-conf ${msms_para_file}
done
done
"""
} else if (params.search_engine == 'comet'){
output_pattern = "_rawResults.txt"
"""
# mzml_tar contains all mzml file
filename="exp_info.txt"
output_dir="exp_${experiment}"
mkdir -p \${output_dir}
ARCHIVE=${mzml_tar}
CONTENTS=\$(tar -tf "\$ARCHIVE")
# Check if the first entry in the archive is a directory
FIRST_ENTRY=\$(echo "\$CONTENTS" | head -n 1)
if [[ "\$FIRST_ENTRY" == */ ]]; then
tar -xvf "\$ARCHIVE" --strip-components 1
else
tar -xvf "\$ARCHIVE"
fi
mv *.gz \${output_dir}
head -n 1 \$filename | while read -r sample experiment wxs_file_name wxs_file_location mzml_files mzml_path fusion
do
IFS=';' read -r -a allNames <<< "\$mzml_files"
for index in "\${!allNames[@]}"
do
eachName="\${allNames[\$index]}"
gunzip \${output_dir}/\${eachName}
msmsName=\${output_dir}/\${eachName}
msmsName=\${msmsName/.gz/}
/opt/comet.2018014.linux.exe -P ${msms_para_file} -N \$msmsName_rawResults -D ref.fasta \${msmsName}
sed -i '1d' \${msmsName}_rawResults.txt
sed -i '1 s/\$/\tna/' \${msmsName}_rawResults.txt
done
done
"""
} else if (params.search_engine == 'xtandem'){
output_pattern = ".mzid"
"""
# mzml_tar contains all mzml file
filename="exp_info.txt"
output_dir="exp_${experiment}"
mkdir -p \${output_dir}
ARCHIVE=${mzml_tar}
CONTENTS=\$(tar -tf "\$ARCHIVE")
# Check if the first entry in the archive is a directory
FIRST_ENTRY=\$(echo "\$CONTENTS" | head -n 1)
if [[ "\$FIRST_ENTRY" == */ ]]; then
tar -xvf "\$ARCHIVE" --strip-components 1
else
tar -xvf "\$ARCHIVE"
fi
mv *.gz \${output_dir}
head -n 1 \$filename | while read -r sample experiment wxs_file_name wxs_file_location mzml_files mzml_path fusion
do
IFS=';' read -r -a allNames <<< "\$mzml_files"
for index in "\${!allNames[@]}"
do
eachName="\${allNames[\$index]}"
gunzip \${output_dir}/\${eachName}
msmsName=\${output_dir}/\${eachName}
msmsName=\${msmsName/.gz/}
xml_input=\${msmsName}_input.xml
python3 /opt/neoflow/bin/generate_xtandem_para_xml.py ${msms_para_file} \${msmsName} ref.fasta \${msmsName} \${xml_input}
## users must provide the main search parameter file for X!Tandem search
/opt/tandem-linux-17-02-01-4/bin/tandem.exe \${xml_input}
## convert xml to mzid
java -Xmx${params.search_engine_mem}g -jar /opt/mzidlib-1.7/mzidlib-1.7.jar Tandem2mzid \
\${msmsName}.xml \
\${msmsName}.mzid \
-outputFragmentation false \
-decoyRegex XXX_ \
-databaseFileFormatID MS:1001348 \
-massSpecFileFormatID MS:1001062 \
-idsStartAtZero false \
-compress false \
-proteinCodeRegex "\\S+"
done
done
"""
} else {
"""
# exit with 1 if the search engine is not supported
exit 1
"""
}
}
process calculate_fdr{
label 'r5_2xlarge'
container "${params.container.fdr_calc}"
cpus 8
memory '60 GB'
publishDir "${params.outdir_run}/fdr_estimation/",
mode: 'copy',
pattern: 'exp_*/*',
overwrite: true
input:
tuple val(experiment_name),
path(psm_raw_file),
path(search_db)
output:
tuple val(experiment_name),
path('exp_*/*_level'),
emit: pga_result_folder
tuple val(experiment_name),
path('exp_*/*-rawPSMs.txt'),
emit: raw_psm_file
script:
"""
mkdir -p peptide_level/global_fdr
mkdir -p psm_level/global_fdr
Rscript /usr/src/fdr_calc.R ./ ${search_db} ${params.pga_prefix} ${params.search_engine} ./
mkdir exp_${experiment_name}
mv peptide_level exp_${experiment_name}
mv psm_level exp_${experiment_name}
mv *-rawPSMs.txt exp_${experiment_name}
"""
}
process prepare_pepquery_input{
container "${params.container.pga}"
cpus 1
memory '4 GB'
executor 'local'
input:
tuple val(experiment_name),
path(pga_result_folder)
output:
tuple val(experiment_name),
path("novel_peptides_psm.tsv"),
emit: novel_psm_tsv
tuple val(experiment_name),
path("novel_peptides.tsv"),
emit: novel_peptide_tsv
"""
#!/usr/bin/env /usr/local/bin/Rscript
library(dplyr)
library(readr)
library(stringr)
is_novel_peptides=function(protein_ids){
ids <- str_split(protein_ids,pattern=";") %>% unlist()
is_novel <- all(str_detect(ids,pattern="^VAR"))
return(is_novel)
}
psms <- read_tsv("peptide_level/global_fdr/pga-peptideSummary.txt")
novel_psms <- psms %>% filter(Qvalue<=0.01) %>%
mutate(is_novel=sapply(protein,is_novel_peptides)) %>%
filter(is_novel==TRUE) %>%
filter(isdecoy==FALSE)
## output
out_novel_psm_file <- "novel_peptides_psm.tsv"
novel_psms %>% write_tsv(out_novel_psm_file)
novel_psms %>% select(peptide) %>% distinct() %>% write_tsv("novel_peptides.tsv",col_names=FALSE)
"""
}
process run_pepquery{
label 'r5_2xlarge'
container "${params.container.neoflow}"
cpus 8
memory '60 GB'
publishDir "${params.outdir_run}/pepquery/",
mode: 'copy',
pattern: 'exp_*/pepquery',
overwrite: true
input:
tuple val(experiment_name),
path(ms_data),
path(pv_refdb),
path(novel_peptide_tsv)
output:
tuple val(experiment_name),
path("*/pepquery"),
emit: pepquery_out
"""
mkdir -p pepquery_index
java -cp /opt/pepquery-1.6.2/pepquery-1.6.2.jar main.java.index.BuildMSlibrary \
-i ./ -o pepquery_index
mkdir -p pepquery
java -Xmx48g -jar /opt/pepquery-1.6.2/pepquery-1.6.2.jar \
-ms pepquery_index \
-pep ${novel_peptide_tsv} \
-db ${pv_refdb} \
-fixMod ${params.pv_fixmod} \
-varMod ${params.pv_varmod} \
-cpu ${task.cpus} \
-minScore 12 \
-tol ${params.pv_tol} \
-tolu ${params.pv_tolu} \
-itol ${params.pv_itol} \
-n 10000 \
-um \
-m 1 \
-prefix neoflow \
-o pepquery
outdir="exp_${experiment_name}"
mkdir \${outdir}
mv pepquery \${outdir}
"""
}
process add_pepquery_validation {
cpus 1
memory '4 GB'
executor 'local'
container "${params.container.pga}"
publishDir "${params.outdir_run}/novel_peptide_identification/",
mode: 'copy',
pattern: 'exp_*/*',
overwrite: true
input:
tuple val(experiment_name),
path(novel_psm_tsv),
path(pepquery_res_folder)
output:
tuple val(experiment_name),
path('*/novel_peptides_psm_pepquery.tsv'),
emit: novel_peptides_psm_pepquery
"""
#!/usr/bin/env /usr/local/bin/Rscript
library(dplyr)
library(readr)
library(stringr)
psms <- read_tsv("${novel_psm_tsv}")
psm_rank_file = "${pepquery_res_folder}/psm_rank.txt"
if(file.exists(psm_rank_file)){
psm_rank <- read_tsv(psm_rank_file)
if("n_ptm" %in% names(psm_rank)){
psm_rank <- psm_rank %>% filter(pvalue<=0.01,n_ptm==0,rank==1)
psms\$pepquery <- ifelse(psms\$peptide %in% psm_rank\$peptide,1,0)
}else{
psms\$pepquery <- 0
}
}else{
psms\$pepquery <- 0
}
dir.create(paste0("exp_", "${experiment_name}"))
psms %>% write_tsv(file.path(paste0("exp_", "${experiment_name}"),
"novel_peptides_psm_pepquery.tsv"))
"""
}
workflow msms_search {
take:
manifest_new
search_db_ch
ref_ch
main:
extract_exp_info(manifest_new)
set_exp_info(extract_exp_info.out.exp_ch.flatten())
extract_s3_path(set_exp_info.out.res_ch)
combined_ch_1 = set_exp_info.out.res_ch.combine(extract_s3_path.out.s3_ch, by:0)
combined_ch = combined_ch_1.combine(search_db_ch, by:0)
peptide_identification(combined_ch, params.search_para_file)
ch_1 = peptide_identification.out.mzid_ch
fdr_ch = ch_1.combine(search_db_ch, by:0)
calculate_fdr(fdr_ch)
prepare_pepquery_input(calculate_fdr.out.pga_result_folder)
mzml_ch = peptide_identification.out.mzml_ch
pepquery_in_1 = mzml_ch.combine(ref_ch, by:0)
novel_peptide_ch = prepare_pepquery_input.out.novel_peptide_tsv
pepquery_in = pepquery_in_1.combine(novel_peptide_ch, by:0)
run_pepquery(pepquery_in)
novel_psm_ch = prepare_pepquery_input.out.novel_psm_tsv
pepquery_res_ch = run_pepquery.out.pepquery_out
pepquery_valid_ch = novel_psm_ch.combine(pepquery_res_ch, by:0)
add_pepquery_validation(pepquery_valid_ch)
emit:
var_pep_file = add_pepquery_validation.out.novel_peptides_psm_pepquery
}