-
Notifications
You must be signed in to change notification settings - Fork 12
/
neoflow_neoantigen.nf
332 lines (250 loc) · 8.57 KB
/
neoflow_neoantigen.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
#!/usr/bin/env nextflow
params.help = false
params.netmhcpan_dir = null
params.var_db = null
params.var_info_file = null
params.out_dir = "./"
params.hla_type = null
params.prefix = "neoflow"
params.cpu = 6
params.ref_db = null
params.var_pep_file = "-"
params.var_pep_info = "-"
def helpMessage() {
log.info"""
=========================================
neoflow => Neoantigen prediction
=========================================
Usage:
nextflow run neoflow_neoantigen.nf
Mandatory arguments:
--var_db Variant (somatic) database in fasta format generated by neoflow_db.nf
--var_info_file Variant (somatic) information in txt format generated by neoflow_db.nf
--ref_db Reference (known) protein database
--hla_type HLA typing result in txt format generated by Optitype
--netmhcpan_dir NetMHCpan 4.0 folder
--var_pep_file Variant peptide identification result generated by neoflow_msms.nf, optional.
--var_pep_info Variant information in txt format for customized database used for variant peptide identification
--prefix The prefix of output files
--out_dir Output directory
--cpu The number of CPUs
--help Print help message
""".stripIndent()
}
// Show help emssage
if (params.help){
helpMessage()
exit 0
}
output_prefix = params.prefix
hla_type_file = file(params.hla_type)
var_db = file(params.var_db)
var_info_file = file(params.var_info_file)
out_dir = file(params.out_dir)
netmhcpan_dir = file(params.netmhcpan_dir)
cpu = params.cpu
ref_db = file(params.ref_db)
var_pep_file = file(params.var_pep_file)
var_pep_info = file(params.var_pep_info)
/*
* validate input files
*/
netmhcpan_tool = file(params.netmhcpan_dir + "/netMHCpan")
if( !netmhcpan_tool.exists() ) exit 1, "netMHCpan is invalid: ${netmhcpan_tool}"
if(!out_dir.isDirectory()){
out_dir_result = out_dir.mkdirs()
println out_dir_result ? "Create folder: $out_dir!" : "Cannot create directory: $myDir!"
}
process split_file {
tag "split_file"
container "proteomics/pga:latest"
input:
file var_info_file
output:
file("var_info_*") into var_info_file_list mode flatten
script:
"""
#!/usr/bin/env /usr/local/bin/Rscript
library(dplyr)
library(readr)
library(parallel)
ncpu <- detectCores()
use_ncpu <- 1
user_ncpu <- as.numeric("${cpu}")
if(user_ncpu <= ncpu){
use_ncpu <- user_ncpu
}
if(use_ncpu <= 0){
use_ncpu <- ncpu
}
a <- read_tsv("${var_info_file}")
if(use_ncpu > nrow(a)){
## file is small
use_ncpu <- 1
}
#nlines_per_file <- ceiling(nrow(a)/use_ncpu)
nlines_per_file <- nrow(a) %/% use_ncpu
last_i <- 0
for(i in 1:use_ncpu){
i1 <- last_i + 1
if(i < use_ncpu){
i2 <- i1 + nlines_per_file - 1
}else{
i2 <- nrow(a)
}
write_tsv(a[i1:i2,], paste("var_info_",i,sep=""))
last_i <- i2
}
"""
}
process mhc_peptide_binding_prediction {
tag "${var_info_file_list}"
//publishDir "${out_dir}", mode: "copy", overwrite: true
//maxForks $cpu
cpus 1
container "proteomics/neoflow:latest"
input:
file hla_type_file
file var_db
file var_info_file_list
file netmhcpan_dir
output:
file("${var_info_file_list}_binding_prediction_result.csv") into mhc_binding_prediction_i
file("${var_info_file_list}_binding_prediction_result.csv") into mhc_binding_prediction_i_for_filtering
script:
"""
python ${baseDir}/bin/binding_prediction.py \
-p ${var_info_file_list} \
-hla_type ${hla_type_file} \
-var_db ${var_db} \
-var_info ${var_info_file_list} \
-o ./ \
-netmhcpan "${netmhcpan_dir}/netMHCpan" \
"""
}
process combine_prediction_results {
tag "combine_prediction_results"
container "proteomics/pga:latest"
input:
file "*_binding_prediction_result.csv" from mhc_binding_prediction_i.collect()
output:
file("${output_prefix}_binding_prediction_result.csv") into mhc_binding_prediction_file
file("${output_prefix}_binding_prediction_result.csv") into mhc_binding_prediction_file_for_filtering
script:
"""
#!/usr/bin/env /usr/local/bin/Rscript
library(dplyr)
library(readr)
fs <- list.files(path="./",pattern="*_binding_prediction_result.csv")
a <- lapply(fs,read.csv,stringsAsFactors=FALSE, colClasses=c("Ref"="character", "Alt"="character","AA_before"="character","AA_after"="character")) %>% bind_rows()
ofile <- paste("${output_prefix}","_binding_prediction_result.csv",sep="")
write_csv(a, ofile)
"""
}
/*
* map neoepitopes to reference protiens and remove
* neoepitopes who can map to a reference protein.
* data preparation
*/
process prepare_data_for_mapping {
tag "map_to_reference"
container "proteomics/pga:latest"
input:
file mhc_binding_prediction_file
output:
file("all_neoepitope.txt") into all_neoepitope_file
script:
"""
#!/usr/bin/env /usr/local/bin/Rscript
library(dplyr)
library(readr)
a <- read_csv("${mhc_binding_prediction_file}")
pep <- a %>% select(Neoepitope) %>% distinct()
write_tsv(pep,"all_neoepitope.txt",col_names=FALSE)
"""
}
/*
* map neoepitopes to reference protiens and remove
* neoepitopes who can map to a reference protein.
* mapping
*/
process peptide_mapping {
tag "peptide_mapping"
container "proteomics/neoflow:latest"
input:
file all_neoepitope_file
file ref_db
output:
file("pep2pro.tsv") into pep2pro_file
script:
"""
java -jar /opt/pepmap.jar -i ${all_neoepitope_file} -d ${ref_db} -o pep2pro.tsv
"""
}
/*
* map neoepitopes to reference protiens and remove
* neoepitopes who can map to a reference protein.
* filtering
*/
process filtering_by_reference {
tag "map_to_reference"
container "proteomics/pga:latest"
input:
file pep2pro_file
file mhc_binding_prediction_file_for_filtering
output:
file("${output_prefix}_neoepitope_filtered_by_reference.csv") into mhc_binding_prediction_filtered_file
script:
"""
#!/usr/bin/env /usr/local/bin/Rscript
library(dplyr)
library(readr)
a <- read_csv("${mhc_binding_prediction_file_for_filtering}")
pep2pro <- read_tsv("${pep2pro_file}")
a_filter <- a %>% filter(!(Neoepitope %in% pep2pro\$peptide))
write_csv(a_filter,"${output_prefix}_neoepitope_filtered_by_reference.csv")
"""
}
if(var_pep_file.exists() && var_pep_info.exists()){
process add_variant_pep_evidence {
tag "add_variant_pep_evidence"
container "proteomics/pga:latest"
publishDir "${out_dir}/neoantigen_prediction/", mode: "copy", overwrite: true
input:
file mhc_binding_prediction_filtered_file
file var_pep_file
file var_pep_info
output:
file("${output_prefix}_neoepitope_filtered_by_reference_add_variant_protein_evidence.tsv") into final_res
script:
"""
#!/usr/bin/env /usr/local/bin/Rscript
library(dplyr)
library(readr)
library(tidyr)
a <- read.csv("${mhc_binding_prediction_filtered_file}",stringsAsFactors=FALSE) %>%
mutate(Chr = as.character(Chr),
Start = as.character(Start),
End = as.character(End),
Ref = as.character(Ref),
Alt = as.character(Alt))
var_pep_psms <- read.delim("${var_pep_file}",stringsAsFactors=FALSE)
var_pep_info <- read.delim("${var_pep_info}",stringsAsFactors=FALSE) %>%
mutate(Chr = as.character(Chr))
var_pep_pro <- var_pep_psms %>% filter(pepquery==1) %>%
select(peptide,protein) %>% distinct() %>%
separate_rows(protein,sep=";")
var_pep_pro_info <- merge(var_pep_pro,var_pep_info,by.x="protein",by.y="Variant_ID") %>%
select(peptide,Chr,Start,End,Ref,Alt) %>%
mutate(Chr = as.character(Chr),
Start = as.character(Start),
End = as.character(End),
Ref = as.character(Ref),
Alt = as.character(Alt))
a_var <- left_join(a,var_pep_pro_info,by=c("Chr","Start","End","Ref", "Alt")) %>%
mutate(protein_var_evidence_pep=ifelse(is.na(peptide),"-",peptide)) %>%
mutate(peptide=NULL)
a_var %>% write_tsv("${output_prefix}_neoepitope_filtered_by_reference_add_variant_protein_evidence.tsv")
"""
}
}