-
Notifications
You must be signed in to change notification settings - Fork 1
/
simsapiper.nf
419 lines (330 loc) · 15.6 KB
/
simsapiper.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
params.targetSequences = "$params.seqs/*.$params.seqFormat"
targetSequencesFile = file(params.targetSequences)
allSequences = Channel.fromPath(params.targetSequences)
//allSequences = Channel.fromPath(params.seqs).filter(*params.seqFormat)
//userStructures = Channel.fromPath(params.structures).filter(/\.pdb$/)
userStructures = Channel.fromPath("$params.structures/*.pdb")
//can not make parameters to strings! then path needs to be $pwd
log.info """\
Usage:
\$ nextflow run simsapiper.nf -profile server,withsingularity --magic
================================================================================
LIST OF PARAMETERS
================================================================================
GENERAL
Launch dir : $launchDir
Project dir : $projectDir
Execution time : $params.executionTimestamp
================================================================================
INPUT FILES
Input file folder (--data): $params.data
Input sequence format (--seqFormat fasta): $params.seqFormat
!Find all possible formats at https://biopython.org/wiki/SeqIO
Ignore sequences with % unknown characters (--seqQC 5): $params.seqQC
Collapse sequences with % sequence identity (--dropSimilar 90): $params.dropSimilar
Sequences that a required to be in the alignment (--favoriteSeqs "fav1,fav2"): $params.favoriteSeqs
================================================================================
OUTPUT FILES
Output folder: $params.outFolder
Final MSA file name (--outName mymsa): $params.outName
================================================================================
SUBSETTING
Creates subsets of % sequence identity (--createSubsets 30): $params.createSubsets
Set minimal % sequence identity in subsets (--minSubsetID 20): $params.minSubsetID
!Reduce the number of subsets by collating with --minSubsetID "min"
Set maximal size of subset(--maxSubsetSize 100): $params.maxSubsetSize
User provides fasta files with subsets (--useSubsets): $params.useSubsets
!Provide sequences not fitting any subset in a file containing 'orphan' in filename
================================================================================
ADD STRUCTURAL INFORMATION
Retrieve protein structure models from AFDB (--retrieve): $params.retrieve
Predict protein structure models with ESM Atlas (--model): $params.model
Predict protein structure models with Local ESMfold (--localModel X): $params.localModel
!Add factor X for each 100 sequences you expect to model
Maximal % of sequences not matched to a model (--strucQC 5): $params.strucQC
================================================================================
ALINGMENT PARAMETERS
Additional parameters for Tcoffee (--tcoffeeParams): $params.tcoffeeParams
Additional parameters for MAFFT (--mafftParams "--localpair --maxiterate 1000"): $params.mafftParams
Map DSSP to alignment (--dssp): $params.dssp
Save DSSP files (--dsspPath): $params.dsspPath
Squeeze alignment towards anchors (--squeeze "H,E"): $params.squeeze
!Select secondary structure elements for anchor points according to DSSP annotation
Set minimal occurence % of anchor element in MSA: (--squeezePerc 80): $params.squeezePerc
Order MSA by input files (--reorder "cluster3.fasta,cluster4.fasta"): $params.reorder
Convert final MSA output from fasta (--convertMSA clustal): $params.convertMSA
================================================================================
"""
include {readSeqs as convertSeqs;
readSeqs as convertTCMsa;
readSeqs as convertFinalMsa;
missingQC;
attendance;
writeFastaFromChannel as writeFastaFromMissing ;
writeFastaFromChannel as writeFastaFromFound ;
writeFastaFromChannel as writeFastaFromSeqsInvalid ;
writeFastaFromChannel as writeFastaFromSeqsValid ;
createSummary;
} from "$projectDir/modules/utils"
include {userSubsetting;
cdHitSubsetting;
cdHitCollapse;
} from "$projectDir/modules/subsetting"
include {
fetchEsmAtlasStructure;
getAFmodels;
runDssp;
esmFolds;
} from "$projectDir/modules/structures"
include{
runTcoffee ;
mergeMafft ;
reorder;
mapDssp as mapDsspRough;
mapDssp as mapDsspSqueeze;
squeeze;
}from "$projectDir/modules/msas"
workflow {
//convert input files to fasta format
allSequences.view{'Sequence file found:' + it}
convertSeqs(params.seqFormat, allSequences)
sequenceFastas = convertSeqs.out.convertedSeqs
//data reduction with cdhit
if (params.dropSimilar){
cdHitCollapse(sequenceFastas, params.dropSimilar, params.favoriteSeqs)
reducedSeqs = cdHitCollapse.out.seqs
reducedClusters= cdHitCollapse.out.clusters
} else {
reducedSeqs = sequenceFastas
reducedClusters = Channel.empty()
}
//Quality control input sequences
seqsRelabeled = reducedSeqs
.splitFasta( record: [header: true, sequence: true ])
.map { record -> [header: record.header.replaceAll("[^a-zA-Z0-9]", "_"),
sequence: record.sequence.replaceAll("\n","").replaceAll("[^ARNDCQEGHILKMFPOSUTWYVarndcqeghilkmfposutwvy]", "X")] }
seqsQC = seqsRelabeled
.branch{
valid: it.sequence.count('X')*100 / it.sequence.size() <= params.seqQC
invalid: it.sequence.count('X') *100/ it.sequence.size() > params.seqQC
}.set { seqsFiltered}
seqsFiltered.invalid.view { "INVALID >${it.header}" }
seqsInvalidCount = seqsFiltered.invalid.count()
writeFastaFromSeqsInvalid (seqsFiltered.invalid.map{record -> '>' + record.header + ',' + record.sequence}.collect(), "too_many_unknown_characters.fasta")
//compare sequence and structure labels
seqIDs =seqsFiltered.valid.map{tuple(it.header , it.sequence)}
allSequencesCount = seqIDs.count()
allSequencesCount.view{"Sequences to be aligned:" + it}
if (params.dropSimilar){
fullInputSeqsNum = cdHitCollapse.out.num.toInteger().sum().view{"Number of sequences before collapsing:" + it}
}else{
fullInputSeqsNum = allSequencesCount
}
strucIDs = userStructures.map{strucL -> tuple(strucL.baseName, strucL.name)}
strucIDs
.join(seqIDs, remainder: true)
.branch {
modelNotFound: it[1] == null
sequenceNotFound: it[2] == null
modelFound: true //it[1] != null || it[2] !=null
}.set{joined}
//joined.modelFound.view{"Provided model matched:" + it[0]}
joined.modelFound.count().view{"Provided model matched:" + it}
joined.sequenceNotFound.count().view {"Model without matching sequence:" +it}
nosequenceModels = joined.sequenceNotFound.map{it[1]}
//sumbit list of modelNotFound IDs to AFDB
if (params.retrieve) {
uniprotIDs = joined.modelNotFound.map{ it[0]}
getAFmodels (uniprotIDs)
protsFromAF = getAFmodels.out
//update list
getAFout = getAFmodels.out
.map{it -> tuple(it.baseName, it.name)}
getAFout
.mix(strucIDs)
.join(seqIDs, remainder: true)
.branch {
modelNotFound: it[1] == null
sequenceNotFound: it[2] == null
modelFound: true //it[1] != null || it[2] !=null
}.set{afjoined}
//afjoined.modelFound.view{"Post AF Models matched:" + it[0]}
//afjoined.modelNotFound.view{"Missing after AF2DB search:" + it[0]}
afmodelCount=afjoined.modelFound.count()
} else {
getAFout = Channel.empty()
protsFromAF= Channel.empty()
afmodelCount=Channel.empty()
}
//sumbit list of modelNotFound sequences from AFDB or folder search to ESM Atlas
if (params.model){
if (params.retrieve) { missingModels =afjoined.modelNotFound }
else {missingModels =joined.modelNotFound }
missingModels
.branch{
toolong: it[2].length() >= 400
good: it[2].length() < 400
}.set{tomodel}
tomodel.toolong.view{"Sequence to long to be predicted with ESM Atlas:" + it[0]}
fetchEsmAtlasStructure(tomodel.good.map{tuple(it[0],it[2])},tomodel.good.count())
protsFromESM = fetchEsmAtlasStructure.out
//update list
fetchEsmAtlasStructure.out
.map{it -> tuple(it.baseName, it.name)}
.mix(strucIDs,getAFout)
.join(seqIDs, remainder: true)
.branch {
modelNotFound: it[1] == null
sequenceNotFound: it[2] == null
modelFound: true //it[1] != null || it[2] !=null
}.set{esmfjoined}
//esmfjoined.modelFound.view{"Post ESMF Models matched:" + it[0]}
//esmfjoined.modelNotFound.view{"Missing after ESM Atlas search:" + it[0]}
esmmodelCount= esmfjoined.modelFound.count()
}else{
protsFromESM = Channel.empty()
esmmodelCount=Channel.of("none")
}
// assess which models could not be found in the folder, AFDB or ESM Atlas
if (params.model) {
finalMissingModels = esmfjoined.modelNotFound
finalModelFound = esmfjoined.modelFound
} else if (params.retrieve) {
finalMissingModels = afjoined.modelNotFound
finalModelFound = afjoined.modelFound
} else {
finalMissingModels = joined.modelNotFound
finalModelFound = joined.modelFound
}
structurelessCount=finalMissingModels.count()
structurelessCount.view{"Missing models: " + it}
//run local esmfold
if (params.localModel){
writeFastaFromMissing(finalMissingModels.map{record -> ">"+ record[0] + ',' + record[2]}.collect(), 'seqs_to_model.fasta')
seqs_to_model = writeFastaFromMissing.out.found
esmFolds(seqs_to_model)
foundSequencesCount = finalModelFound.mix(esmFolds.out.esmFoldsStructures).count()
//finalModelFound.view()
//esmFolds.out.esmFoldsStructures.view()
//finalModelFound.mix(esmFolds.out.esmFoldsStructures).view()
structureless_seqs=Channel.empty()
structurelessCount=structureless_seqs.count()
writeFastaFromSeqsValid (seqIDs.map{record -> ">"+ record[0]+ ',' + record[1]}.collect(),'seqs_to_align.fasta')
foundSeqs = writeFastaFromSeqsValid.out.found
}else{
foundSequencesCount = finalModelFound.count()
writeFastaFromMissing(finalMissingModels.map{record -> ">"+ record[0] + ',' + record[2]}.collect(), 'structureless_seqs.fasta')
structureless_seqs = writeFastaFromMissing.out.found
writeFastaFromSeqsValid (finalModelFound.map{record -> ">"+ record[0]+ ',' + record[2]}.collect(),'seqs_to_align.fasta')
foundSeqs = writeFastaFromSeqsValid.out.found
}
missingQC (allSequencesCount, foundSequencesCount, params.strucQC)
//subsetting
if (params.useSubsets){
userSubsetting(sequenceFastas,foundSeqs)
subsets = userSubsetting.out.subsetSeqsForTC.collect().flatten()
}else if (params.createSubsets){
cdHitSubsetting(foundSeqs, params.createSubsets, params.minSubsetID, params.maxSubsetSize)
subsets = cdHitSubsetting.out.seq.collect().flatten()
cdHitSubsettingClusters=cdHitSubsetting.out.clusters
}else{
subsets = foundSeqs
cdHitSubsettingClusters=Channel.empty()
}
subsets.branch{
orphans: it =~ /orphan/
subsets: true
}.set{subSeqs}
seqsToAlign = subSeqs.subsets
//submit to tcoffee
if (params.align){
runTcoffee(seqsToAlign, params.structures, params.tcoffeeParams, missingQC.out.gate)
strucMsa =runTcoffee.out.msa.flatten()
convertTCMsa('clustal', strucMsa)
convertedMsa = convertTCMsa.out.convertedSeqs.mix(structureless_seqs,subSeqs.orphans).collect()
//create final alignment
mergeMafft(convertedMsa, params.mafftParams, params.outName)
finalMsa = mergeMafft.out.finalMsa
}else{finalMsa=Channel.fromPath(params.alignment)
}
//check if all sequences been aligned
//attendance(finalMsa,fullInputSeqsNum,seqsInvalidCount,structurelessCount,foundSequencesCount)
//map to dssp
if (params.dssp){
foundModels = userStructures.mix(protsFromAF,protsFromESM ).map{mod -> tuple(mod.baseName, mod)}
foundDssps=Channel.fromPath(params.dsspPath).map{dssp -> tuple(dssp.baseName, dssp)}
foundModels.join(foundDssps, remainder:true)
.branch {
missingDssp: it[2] == null
missingModel: it[1] == null
matchedDssp: true
}.set{dsspFilter}
dsspFilter.matchedDssp.count().view{"DSSP files matched to models:" + it}
dsspFilter.missingDssp.count().view{"DSSP will be calculated for models:" + it}
//dsspFilter.missingModel.count().view{"DSSP file without matching model found, this is likely an error:" + it}
modsForDssp = dsspFilter.missingDssp.map{it -> it[1]}
runDssp(modsForDssp, missingQC.out.gate)
dssps = runDssp.out.dsspout.collect()
//relies on dssp being finished WAY before tcoffee alignment.
mapDsspRough(params.dsspPath, finalMsa, runDssp.out.gate)
mappedFinalMsa = mapDsspRough.out.mmsa
}
//squeeze MSA
if (params.squeeze){
squeeze(finalMsa,mappedFinalMsa,params.squeeze,params.squeezePerc)
squeezedMsa = squeeze.out.msa
//map dssp to final msa
mapDsspSqueeze(params.dsspPath, squeezedMsa, runDssp.out.gate)
mappedFinalMsaSqueeze = mapDsspSqueeze.out.mmsa
}else{squeezedMsa=finalMsa}
//reorder final MSA
if (params.reorder){
reorder(squeezedMsa,sequenceFastas.collect(), params.reorder)
reorderedFinalMsa = reorder.out.msaOrga
}else{reorderedFinalMsa= squeezedMsa}
//select output format for MSA
if (params.convertMSA){
convertFinalMsa(params.convertMSA, reorderedFinalMsa)
convertFinalMsaFile =convertFinalMsa.out.convertedSeqs
}else{convertFinalMsaFile=Channel.empty()}
createSummary(
Channel.empty().mix(finalMsa,convertFinalMsaFile,reorderedFinalMsa,squeezedMsa,mappedFinalMsaSqueeze).collect(),
params.outFolder,
allSequences,
fullInputSeqsNum,
params.seqs,
params.dropSimilar,
allSequencesCount,
params.favoriteSeqs,
params.seqQC,
seqsInvalidCount ,
params.structures,
joined.modelFound.count(),
"${params.retrieve}",
afmodelCount.ifEmpty('0'),
params.model,
esmmodelCount.ifEmpty('0'),
structurelessCount,
params.createSubsets,
params.useSubsets,
params.tcoffeeParams,
params.mafftParams,
params.dssp,
params.dsspPath ,
params.squeeze ,
params.squeezePerc ,
params.reorder ,
params.convertMSA,
"$workflow.commandLine"
)
}
workflow.onComplete {
println "Pipeline completed at : $workflow.complete"
println "Time to complete workflow execution : $workflow.duration"
println "Commands executed : $workflow.commandLine"
println "Execution status : ${workflow.success ? 'Success' : 'Failed' }"
println "Output folder : $params.outFolder"
}
workflow.onError {
println "Oops... Pipeline execution stopped with the following message: \n ${workflow.errorReport}"
}