-
Notifications
You must be signed in to change notification settings - Fork 0
/
02_hkref_build.nf
executable file
·1144 lines (951 loc) · 37.1 KB
/
02_hkref_build.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env nextflow
//Daniel Stribling | ORCID: 0000-0002-0649-9506
//Renne Lab, University of Florida
//
//Version:
// v01 - 2021-02-15
nextflow.enable.dsl=2
// --------------- Modules Setup ---------------
use_modules = ['helper.nf']
//'helper.nf', 'bowtie2.nf', 'seqkit.nf', 'samtools.nf', 'process_idxstats.py']
params.use_local_modules = true
params.update_modules = false
if( ( !params.use_local_modules || params.update_modules )
&& !params.containsKey('main_modules_dir') ) {
log.error "No local modules defined, or update is requested."
log.error "Please provide source NF modules directory: 'params.main_modules_dir'"
exit 1
}
if( params.use_local_modules ) {
params.modules_dir = "${projectDir}/bin_nf"
} else {
params.modules_dir = params.main_modules_dir
}
if( params.use_local_modules && params.update_modules && use_modules ) {
println "\n -- Copying Source Modules to: ${params.modules_dir} -- \n"
exe_dir = file(params.modules_dir)
exe_dir.mkdir()
use_modules.each { mod_name ->
full_mod_name = "${mod_name}"
println " -- -- ${mod_name}"
source_file = file("${params.main_modules_dir}/${full_mod_name}", checkIfExists: true)
source_file.copyTo(exe_dir)
}
}
if( use_modules ) {
println "\n -- Using Modules in: ${params.modules_dir} -- \n"
}
// --------------- Helper Functions Setup ---------------
include {
ensure_params_paths; // Initialize, fill, and check default variables
get_max_local_cpus; // Set maximum cpus available per task for local execution
workflow_details; // Print initial workflow execution details
execution_details; // Print final workflow execution details
test_params_key_allowed; // Ensure key in params with allowed options
test_params_key; // Ensure key in params
test_params_keys; // Ensure list of keys in params
echo_process_details; // Echo details of process to process stdout
Standardize_Channel; // Standardize channel to an N-tuple of items
check_full_file_path; // Check a file path for existence with verbose error
return_as_list // Ensure variable object is a list
eexit; // Log error message and exit
glob_to_files; // Convert "glob" path to file path
} from params.modules_dir + "/helper"
// --------------- Set Default Process Paramter Values ---------------
params.main_prefix = "hkref"
params.all_publish_dir = "${projectDir}/${params.main_prefix}_working"
params.raw_publish = true
params.raw_out_dir = '01_raw_seqs'
params.clustered_publish = true
params.clustered_out_dir = '02_clustered_seqs'
params.mask_publish = true
params.mask_out_dir = '03_masked_seqs'
params.combined_publish = true
params.combined_out_dir = '04_combined_seqs'
params.mask_seed = '11111'
params.final_publish_dir = "${projectDir}"
params.final_publish = true
params.logs_publish = true
params.logs_dir = "${params.all_publish_dir}/logs"
// --------------- Define Processes ---------------
build_db_names = []
params.build_dbs.each { name, details ->
build_db_names.add(name)
}
Channel.fromList(build_db_names)
.set { db_names }
// -- Map dbs for Ensembl downloads
db_names.flatMap { db_name ->
def out_lists = []
db_details = params.build_dbs[db_name]
if( db_details.containsKey('ensembl_sets') ) {
db_details['ensembl_sets'].each{ set_name ->
def out_list = [db_name, set_name, db_details.db_settings, params.settings_output]
out_lists.add(out_list)
}
} else {
def out_list = [db_name, null, db_details.db_settings, params.settings_output]
out_lists.add(out_list)
}
out_lists
}
.set { ensembl_sets }
// -- Map dbs for Genbank downloads
if( !(params.containsKey('user_email') && params['user_email']) ) {
eexit('Please Provide Parameter --user_email (params.user_email) for Entrez Downloads')
}
db_names.map { db_name ->
db_details = params.build_dbs[db_name]
db_genbank = params.build_dbs[db_name]['genbank_trnas']
[db_name, params.user_email, db_details.db_settings, params.settings_output, db_genbank]
}
.filter { it -> it[4] }
.set { genbank_sets }
// -- Map dbs for miRBase downloads
db_names.map { db_name ->
db_details = params.build_dbs[db_name]
db_file_lines = file(db_details.db_settings).readLines()
db_file_lines.each{ line ->
if( line.contains('mirbase_db_url') ) {
mirbase_db_url = line.stripIndent() - ~/mirbase_db_url:/
mirbase_db_url = mirbase_db_url.replaceAll('"', '').replaceAll("'", '')
mirbase_db_url = mirbase_db_url.stripIndent()
} else if( line.contains('mirbase_db_ver') ) {
mirbase_db_ver = line.stripIndent() - ~/mirbase_db_ver:/
mirbase_db_ver = mirbase_db_ver.replaceAll('"', '').replaceAll("'", '')
mirbase_db_ver = mirbase_db_ver.stripIndent()
}
}
[db_name,
mirbase_db_ver,
mirbase_db_url,
db_details.db_settings,
params.settings_output
]
}
.set { mirbase_sets }
// -- Map dbs for External DB downloads
db_names.flatMap { db_name ->
db_details = params.build_dbs[db_name]
def out_items = []
if( db_details.containsKey('ext_dbs') && db_details.ext_dbs ) {
db_details.ext_dbs.each { ext_db_name ->
out_items.add([db_name, ext_db_name, db_details.db_settings, params.settings_output])
}
}
out_items
}
.set { ext_db_sets }
// -- Create Settings Channels
output_settings = Channel.fromPath( params.settings_output )
.collect()
process DL_Ensembl {
conda params.python_conda
tag { if( db_detail ) { "${db_name}-${db_detail}" } else { db_name } }
//label 'multi_cpu'
label 'cluster'
cache 'lenient'
memory 2.GB
cpus 1
//cpus { task.label.contains('multi_cpu') ? params.max_local_cpus : 1 }
beforeScript { echo_process_details(task) }
//echo true
//stageInMode 'copy'
input:
tuple val(db_name), val(db_detail), path(db_settings), path(out_settings)
output:
tuple val(db_name), path(all_outputs)
path '.command.log'
publishDir "${params.all_publish_dir}", mode: 'copy',
pattern: "${out_dir}/*",
enabled: params.raw_publish
publishDir "${params.logs_dir}", mode: 'copy',
pattern: ".command.log", saveAs: { out_log },
enabled: params.logs_publish
script:
out_log = "${task.tag}.${task.process}.nflog.txt".replaceAll(":","_")
out_dir = params.raw_out_dir
all_outputs = "${out_dir}/*"
if( db_detail ) {
use_query_flag = "--use_query ${db_detail}"
} else {
use_query_flag = ""
}
shell:
'''
set -v -H -o history
echo "Database Name: !{db_name}"
echo "Database Detail: !{db_detail}"
echo "Database Settings File: !{db_settings}"
echo "Output Settings File: !{out_settings}"
mkdir !{out_dir}
cd !{out_dir}
python !{projectDir}/bin/dl_ensembl.py \\
--db_settings_file ../!{db_settings} \\
--out_settings_file ../!{out_settings} \\
--no_cache \\
!{use_query_flag}
COMMAND="$( echo !! )"
set +v +H +o history
'''
}
process DL_GenBank {
conda params.python_conda
tag { db_name }
//label 'multi_cpu'
//label 'cluster'
cache 'lenient'
memory 2.GB
cpus 1
//cpus { task.label.contains('multi_cpu') ? params.max_local_cpus : 1 }
beforeScript { echo_process_details(task) }
//echo true
//stageInMode 'copy'
input:
tuple val(db_name), val(user_email), path(db_settings), path(out_settings), val(dl_genbank)
output:
tuple val(db_name), path(all_outputs)
path '.command.log'
publishDir "${params.all_publish_dir}", mode: 'copy',
pattern: "${out_dir}/*",
enabled: params.raw_publish
publishDir "${params.logs_dir}", mode: 'copy',
pattern: ".command.log", saveAs: { out_log },
enabled: params.logs_publish
script:
out_log = "${task.tag}.${task.process}.nflog.txt".replaceAll(":","_")
out_dir = params.raw_out_dir
all_outputs = "${out_dir}/*"
shell:
'''
set -v -H -o history
echo "Database Name: !{db_name}"
echo "Database Settings File: !{db_settings}"
echo "Output Settings File: !{out_settings}"
mkdir !{out_dir}
cd !{out_dir}
python !{projectDir}/bin/dl_genbank.py \\
--db_settings_file ../!{db_settings} \\
--out_settings_file ../!{out_settings} \\
--user_email !{user_email}
COMMAND="$(echo !!)"
set +v +H +o history
'''
}
process DL_miRBase {
conda params.seqkit_conda
tag { db_name }
//label 'multi_cpu'
//label 'cluster'
cache 'lenient'
memory 2.GB
cpus 1
//cpus { task.label.contains('multi_cpu') ? params.max_local_cpus : 1 }
beforeScript { echo_process_details(task) }
//echo true
//stageInMode 'copy'
input:
tuple val(db_name), val(mirbase_ver), val(mirbase_url), path(db_settings), path(out_settings)
output:
tuple val(db_name), path(all_outputs)
path '.command.log'
publishDir "${params.all_publish_dir}", mode: 'copy',
pattern: "${out_dir}/*",
enabled: params.raw_publish
publishDir "${params.logs_dir}", mode: 'copy',
pattern: ".command.log", saveAs: { out_log },
enabled: params.logs_publish
script:
out_log = "${task.tag}.${task.process}.nflog.txt".replaceAll(":","_")
out_dir = params.raw_out_dir
out_file_name = "hkref_${db_name}_miRBase-${mirbase_ver}-full.fa"
out_file = "${out_dir}/${out_file_name}"
all_outputs = "${out_dir}/*"
shell:
'''
echo "Database Name: !{db_name}"
echo "Database Fasta: !{mirbase_url}"
echo "Database Settings File: !{db_settings}"
echo "Output Settings File: !{out_settings}"
mkdir !{out_dir}
ls
set -v -H -o history
wget -vc --no-check-certificate !{mirbase_url}
gunzip -f mature.fa.gz
mv mature.fa miRBase_mature.fa
seqkit grep -r -p '^hsa' miRBase_mature.fa | \\
seqkit seq --rna2dna | \\
seqkit replace -p ' Homo sapiens ' -r '_miRBase_hsa-' | \\
seqkit replace -p '.+MIMAT' -r 'MIMAT' | \\
seqkit replace -p '$' -r '_microRNA' | \\
seqkit sort -N -o !{out_file}
SEQKIT_COMMAND="!!"
echo """
-- !{out_file_name} --
File: '!{out_file_name}' was created on $(date).
Mature miRNA sequences (version !{mirbase_ver}) were downloaded from miRBase:
!{mirbase_url}
were converted to DNA alphabet,
and identifiers were renamed to match Hyb convention.
Command: ${SEQKIT_COMMAND}
""" > !{out_file}.notes.txt
set +v +H +o history
'''
}
process DL_Ext_DB {
conda params.seqkit_conda
tag { "${db_name}-${ext_db_name}" }
//label 'multi_cpu'
//label 'cluster'
cpus 1
cache 'lenient'
memory 2.GB
//cpus { task.label.contains('multi_cpu') ? params.max_local_cpus : 1 }
beforeScript { echo_process_details(task) }
//echo true
//stageInMode 'copy'
input:
tuple val(db_name), val(ext_db_name), path(db_settings), path(out_settings)
output:
tuple val(db_name), path(all_outputs)
path '.command.log'
publishDir "${params.all_publish_dir}", mode: 'copy',
pattern: "${out_dir}/*",
enabled: params.raw_publish
publishDir "${params.logs_dir}", mode: 'copy',
pattern: ".command.log", saveAs: { out_log },
enabled: params.logs_publish
script:
out_log = "${task.tag}.${task.process}.nflog.txt".replaceAll(":","_")
out_dir = params.raw_out_dir
//out_file_name = "hkref_${db_name}_miRBase-${mirbase_ver}-full.fa"
//out_file = "${out_dir}/${out_file_name}"
out_file_prefix = "${params.main_prefix}_${db_name}"
all_outputs = "${out_dir}/*"
shell:
'''
echo "Database Name: !{db_name}"
echo "External DB Name: !{ext_db_name}"
echo "Get YAML Details:"
DB_VER=$(python !{projectDir}/bin/detail_from_yaml.py --yaml_file !{db_settings} \\
--detail ext_dbs !{ext_db_name} version)
echo -e "Ext DB Version: ${DB_VER}"
DB_SOURCE=$(python !{projectDir}/bin/detail_from_yaml.py --yaml_file !{db_settings} \\
--detail ext_dbs !{ext_db_name} source)
echo -e "Ext DB Source: ${DB_SOURCE}"
DB_DETAIL=$(python !{projectDir}/bin/detail_from_yaml.py --yaml_file !{db_settings} \\
--detail ext_dbs !{ext_db_name} detail)
echo -e "Ext DB Source: ${DB_DETAIL}"
DB_URL=$(python !{projectDir}/bin/detail_from_yaml.py --yaml_file !{db_settings} \\
--detail ext_dbs !{ext_db_name} url)
echo -e "Ext DB Details: ${DB_DETAIL}"
DB_PREP=$(python !{projectDir}/bin/detail_from_yaml.py --yaml_file !{db_settings} \\
--detail ext_dbs !{ext_db_name} prep_commands)
echo -e "Ext DB Prep - Preprocessing Commands: ${DB_PREP}"
DB_PREPPED_NAME=$(python !{projectDir}/bin/detail_from_yaml.py --yaml_file !{db_settings} \\
--detail ext_dbs !{ext_db_name} prepped_name)
echo -e "Ext DB Prep - Preprocessing Commands: ${DB_PREPPED_NAME}"
DB_PROC=$(python !{projectDir}/bin/detail_from_yaml.py --yaml_file !{db_settings} \\
--detail ext_dbs !{ext_db_name} proc_commands)
echo -e "Ext DB Prep - Processing Commands: ${DB_PROC}"
DB_OUT_NAME=$(python !{projectDir}/bin/detail_from_yaml.py --yaml_file !{db_settings} \\
--detail ext_dbs !{ext_db_name} out_name)
echo -e "Ext DB Prep - Output Name: ${DB_OUT_NAME}"
mkdir !{out_dir}
echo -e "\\nDownloading Resource\\n"
set -v -H -o history
wget -vc --no-check-certificate ${DB_URL}
${DB_PREP}
echo -e "\\nProcessing Resource\\n"
ls
PROC_COMMAND="cat ${DB_PREPPED_NAME} | ${DB_PROC} > !{out_dir}/!{out_file_prefix}_${DB_OUT_NAME}"
echo "${PROC_COMMAND}"
eval ${PROC_COMMAND}
echo """
-- !{out_file_prefix}_${DB_OUT_NAME} --
File: '!{out_file_prefix}_${DB_OUT_NAME}' was created on $(date).
Sequences were acqured from:
${DB_URL}
Details: ${DB_DETAIL}
Version: ${DB_VER}
Source: ${DB_SOURCE}
Prepared by:
${DB_PREP}
${PROC_COMMAND}
""" > !{out_dir}/!{out_file_prefix}_${DB_OUT_NAME}.notes.txt
set +v +H +o history
'''
}
process Proc_Transcripts {
conda params.seqkit_conda
tag { db_name }
//label 'multi_cpu'
//label 'cluster'
cache 'lenient'
memory 2.GB
cpus 1
//cpus { task.label.contains('multi_cpu') ? params.max_local_cpus : 1 }
beforeScript { echo_process_details(task) }
//echo true
//stageInMode 'copy'
input:
tuple val(db_name), path(in_files)
output:
tuple val(db_name), path(all_outputs)
path '.command.log'
publishDir "${params.all_publish_dir}", mode: 'copy',
pattern: "${out_dir}/*",
enabled: params.raw_publish
publishDir "${params.logs_dir}", mode: 'copy',
pattern: ".command.log", saveAs: { out_log },
enabled: params.logs_publish
script:
out_log = "${task.tag}.${task.process}.nflog.txt".replaceAll(":","_")
out_dir = params.raw_out_dir
all_outputs = "${out_dir}/*"
in_fastas = in_files.findAll {fn -> ( "${fn}".endsWith(".fa")
|| "${fn}".endsWith(".fasta") ) }
in_fastas_notes = []
in_fastas.each { fn -> in_fastas_notes.add("${fn}.notes.txt") }
in_details = in_files.findAll {fn -> ( "${fn}".endsWith(".csv") ) }
in_details_notes = []
in_details.each { fn -> in_details_notes.add("${fn}.notes.txt") }
out_fasta_name = "hkref_${db_name}_orig-transcripts.fa"
out_fasta = "${out_dir}/${out_fasta_name}"
out_details_name = "${out_fasta_name}".replace('.fa', '.csv')
out_details = "${out_dir}/${out_details_name}"
shell:
'''
set -v -H -o history
echo "All input files: !{in_files}"
echo "Input Fastas: !{in_fastas}"
echo "Input Fastas Notes: !{in_fastas_notes}"
echo "Input Details: !{in_details}"
echo "Input Details Notes: !{in_details_notes}"
mkdir !{out_dir}
# -- Join and sort output sequences
set -v -H -o history
cat !{in_fastas.join(' ')} | \\
seqkit sort --by-name --natural-order | \\
seqkit sliding -g -s 655000 -W 655000 --suffix "" -o !{out_fasta}
SEQKIT_COMMAND="!!"
cat !{in_fastas_notes.join(' ')} > !{out_fasta}.notes.txt
set +v +H +o history
echo """
-- !{out_fasta_name} --
File: '!{out_fasta_name}' was created on $(date).
by concatenating files: !{in_fastas}
and sorting using seqkit
Command:
${SEQKIT_COMMAND}
""" >> !{out_fasta}.notes.txt
# -- Combine output details
set -v -H -o history
cat !{in_details.join(' ')} > !{out_details}
DETAIL_COMMAND="!!"
cat !{in_details_notes.join(' ')} > !{out_details}.notes.txt
set +v +H +o history
echo """
-- !{out_details_name} --
File: '!{out_details_name}' was created on $(date).
by concatenating files: !{in_details}
and sorting using seqkit
Command:
${DETAIL_COMMAND}
""" >> !{out_details}.notes.txt
echo -e "\\nDone\\n"
'''
}
process Dedup_Transcripts {
conda params.cluster_conda
tag { db_name }
label 'multi_cpu'
label 'cluster'
cache 'lenient'
memory 7.GB
//cpus { task.label.contains('multi_cpu') ? params.max_local_cpus : 1 }
beforeScript { echo_process_details(task) }
//echo true
//stageInMode 'copy'
input:
tuple val(db_name), path(in_files)
output:
tuple val(db_name), path(all_outputs)
path '.command.log'
publishDir "${params.all_publish_dir}", mode: 'copy',
pattern: "${out_dir}/*",
enabled: params.clustered_publish
publishDir "${params.logs_dir}", mode: 'copy',
pattern: ".command.log", saveAs: { out_log },
enabled: params.logs_publish
script:
out_log = "${task.tag}.${task.process}.nflog.txt".replaceAll(":","_")
out_dir = params.clustered_out_dir
out_file_name = "hkref_${db_name}_clust.fa"
out_file = "${out_dir}/${out_file_name}"
all_outputs = "${out_dir}/*"
mem_flag = ""
transcript_fasta = files_to_fasta(in_files)
transcript_note = transcript_fasta + '.notes.txt'
transcript_details = "${transcript_fasta}".replaceAll('.fa', '.csv')
out_file_details = "${out_file}".replaceAll('.fa', '.csv')
if( task.memory ) {
reduced_mem = ( task.memory * 0.9 )
mem_flag = "-M ${reduced_mem.toUnit("MB")}"
}
shell:
'''
set -v -H -o history
echo "All input files: !{in_files}"
echo "Input Fasta: !{transcript_fasta}"
echo "Input notes file: !{transcript_note}"
mkdir !{out_dir}
echo -e "\nRemoving duplicates and subsequences from file: !{transcript_fasta}"
set -v -H -o history
cd-hit-est -i !{transcript_fasta} \\
-o !{out_file} \\
-T !{task.cpus} \\
!{mem_flag} \\
-c 1.0 -t 1 -uS 0.0 \\
-sc 1 \\
-r 0 \\
-g 0 \\
-d 100 \\
CLUSTER_COMMAND="!!"
# -sc : sort .clustr file by cluster size
# -c : required identity proportion
# -t : Allowed redundance (default 2)
# -uS : Maximum percent not aligned of smaller read
# -r : 1 or 0, default 1, by default do both +/+ & +/- alignments
# if set to 0, only +/+ strand alignment
# -g : 1 or 0, if 1 use slow clustering algorithm for best cluster result
# -d : length of description in clstr file
#/apps/cdhit/4.6.8/psi-cd-hit/psi-cd-hit.pl \\
# -i !{transcript_fasta} \\
# -o !{out_file} \\
# -c 1.0 \\
# -aS 1.0 \\
# -g 1 \\
# -prog blastn \\
#-sc 1 \\
#-T !{task.cpus} \\
#!{mem_flag} \\
# Ex: ./psi-cd-hit.pl -i db.fna -o db90.fna -c 0.9 -G 1 -g 1 -prog blastn -circle 1 -exec local -core 32
# -aS alignment coverage for the shorter sequence, default 0
# if set to 0.9, the alignment must covers 90% of the sequence
#-g (1/0), default 1
# by cd-hit's default algorithm, a sequence is clustered to the first
# cluster that meet the threshold (fast cluster). If set to 1, the program
# will cluster it into the most similar cluster that meet the threshold
# (accurate but slow mode)
# but either 1 or 0 won't change the representatives of final clusters
echo -e """
-- !{out_file_name} --
File: '!{out_file_name}' was created on $(date).
by clustering file: !{transcript_fasta}
using cd-hit command:
${CLUSTER_COMMAND}
""" >> !{out_file}.notes.txt
echo -e "Passing through transcript detail files:"
cp -vPR !{transcript_details} !{out_file_details}
cp -vPR !{transcript_details}.notes.txt !{out_file_details}.notes.txt
echo -e "\\nDone\\n"
'''
}
process Merge_Transcripts {
conda params.python_conda
tag { db_name }
//label 'multi_cpu'
//label 'cluster'
cache 'lenient'
memory 7.GB
cpus 1
//cpus { task.label.contains('multi_cpu') ? params.max_local_cpus : 1 }
beforeScript { echo_process_details(task) }
//echo true
//stageInMode 'copy'
input:
tuple val(db_name), path(in_files)
path(out_settings_file)
output:
tuple val(db_name), path(all_outputs)
path '.command.log'
publishDir "${params.all_publish_dir}", mode: 'copy',
pattern: "${out_dir}/*",
enabled: params.clustered_publish
publishDir "${params.logs_dir}", mode: 'copy',
pattern: ".command.log", saveAs: { out_log },
enabled: params.logs_publish
script:
out_log = "${task.tag}.${task.process}.nflog.txt".replaceAll(":","_")
out_dir = params.clustered_out_dir
out_file_name = "hkref_${db_name}_clust_merged.fa"
out_file = "${out_dir}/${out_file_name}"
all_outputs = "${out_dir}/*"
mem_flag = ""
transcript_fasta = files_to_fasta(in_files)
transcript_note = transcript_fasta + '.notes.txt'
transcript_det = "${transcript_fasta}".replaceAll('.fa', '.csv')
transcript_det_note = transcript_det + '.notes.txt'
out_file_details = "${out_file}".replaceAll('.fa', '.csv')
shell:
'''
echo "All input files: !{in_files}"
echo "Input Fasta: !{transcript_fasta}"
echo "Input Fasta Notes: !{transcript_note}"
echo "Input Details: !{transcript_det}"
echo "Input Detail Notes: !{transcript_det_note}"
echo "Output Settings: !{out_settings_file}"
mkdir !{out_dir}
echo -e "\nMerging split sequences from file: !{transcript_fasta}"
set -v -H -o history
python !{projectDir}/bin/merge_split_seqs.py \\
--in_file !{transcript_fasta} \\
--in_notes !{transcript_note} \\
--out_file !{out_file} \\
--out_notes !{out_file}.notes.txt \\
--out_settings_file !{out_settings_file} \\
COMMAND="!!"
set +v +H +o history
echo -e "Passing through transcript detail files:"
cp -vPR !{transcript_det} !{out_file_details}
cp -vPR !{transcript_det_note} !{out_file_details}.notes.txt
echo -e "\\nDone\\n"
'''
}
process Cluster_miRNAs {
conda params.cluster_conda
tag { db_name }
//label 'multi_cpu'
//label 'cluster'
cache 'lenient'
memory 7.GB
cpus 1
//cpus { task.label.contains('multi_cpu') ? params.max_local_cpus : 1 }
beforeScript { echo_process_details(task) }
//echo true
//stageInMode 'copy'
input:
tuple val(db_name), path(in_files)
output:
tuple val(db_name), path(all_outputs)
path '.command.log'
publishDir "${params.all_publish_dir}", mode: 'copy',
pattern: "${out_dir}/*",
enabled: params.clustered_publish
publishDir "${params.logs_dir}", mode: 'copy',
pattern: ".command.log", saveAs: { out_log },
enabled: params.logs_publish
script:
out_log = "${task.tag}.${task.process}.nflog.txt".replaceAll(":","_")
out_dir = params.clustered_out_dir
all_outputs = "${out_dir}/*"
mem_flag = ""
in_fasta = files_to_fasta(in_files)
in_note = files_to_note(in_files)
out_file_name = "${file(in_fasta).getName()}".replaceAll('-full', '-clust')
out_file = "${out_dir}/${out_file_name}"
if( task.memory ) {
reduced_mem = ( task.memory * 0.9 )
mem_flag = "-M ${reduced_mem.toUnit("MB")}"
}
shell:
'''
set -v -H -o history
echo "All input files: !{in_files}"
echo "Input Fastas: !{in_fasta}"
echo "Input notes files: !{in_note}"
mkdir !{out_dir}
echo -e "\\nRemoving duplicates and subsequences from file: !{in_fasta}"
set -v -H -o history
cd-hit-est -i !{in_fasta} \\
-o !{out_file} \\
-T !{task.cpus} \\
!{mem_flag} \\
-c 1.0 -t 1 -uS 0.0 \\
-sc 1 \\
-r 0 \\
-d 100 \\
-p 1 \\
-g 1 \\
# -sc : sort .clustr file by cluster size
# -c : required identity proportion
# -t : Allowed redundance (default 2)
# -uS : Maximum percent not aligned of smaller read
# -r : 1 or 0, default 1, by default do both +/+ & +/- alignments
# if set to 0, only +/+ strand alignment
# -d : .clustr file description length
# -p : add alignment overlap to .clustr file
# -g : enable slower algorithm
#cd-hit -i !{in_fasta} \\
# -o !{out_file} \\
# !{mem_flag} \\
# -T !{task.cpus} \\
# -t 1 -d 100 -p 1 -sc 1 \\
# -g 1 -c 1
# -t : Allowed redundance (default 2)
# -d : .clustr file description length
# -p : add alignment overlap to .clustr file
# -sc : sort .clustr file by cluster size
# -g : enable slower algorithm
# -c : sequence identity proportion of shorter sequence
CLUSTER_COMMAND="!!"
cp -v !{in_note} !{out_file}.notes.txt
echo """
-- !{out_file_name} --
File: '!{out_file_name}' was created on $(date).
by clustering file: !{in_fasta}
using cd-hit command:
${CLUSTER_COMMAND}
""" >> !{out_file}.notes.txt
REPORT_PROPS="0.95 0.90 0.85 0.80" # 0.75"
echo -e "\\nGenerating clustering reports for proportions: ${REPORT_PROPS}"
for prop in ${REPORT_PROPS}; do
prop_name="${prop/\\./}"
cd-hit-est -i !{in_fasta} \\
-o !{out_file_name}.cl${prop_name} \\
-T !{task.cpus} \\
!{mem_flag} \\
-c ${prop} -t 1 \\
-sc 1 \\
-r 0 \\
-d 100 \\
-p 1 \\
-g 1 \\
#cd-hit -i !{in_fasta} \\
# -o !{out_file_name}.cl${prop_name} \\
# !{mem_flag} \\
# -T !{task.cpus} \\
# -t 1 -d 100 -p 1 -sc 1 \\
# -g 1 -c ${prop}
done
cp -v *.clstr !{out_dir}
echo -e "\\nDone\\n"
'''
}
process Mask_miRNAs {
conda params.mask_conda
tag { db_name }
label 'multi_cpu'
label 'cluster'
cache 'lenient'
memory 7.GB
//cpus { task.label.contains('multi_cpu') ? params.max_local_cpus : 1 }
beforeScript { echo_process_details(task) }
//echo true
//stageInMode 'copy'
input:
tuple val(db_name), path(mirna_files), path(transcript_files)
output:
tuple val(db_name), path(mask_outputs)
tuple val(db_name), path(combined_outputs)
path '.command.log'
publishDir "${params.all_publish_dir}", mode: 'copy',
pattern: "${mask_out_dir}/*",
enabled: params.mask_publish
publishDir "${params.all_publish_dir}", mode: 'copy',
pattern: "${combined_out_dir}/*",
enabled: params.combined_publish
publishDir "${params.logs_dir}", mode: 'copy',
pattern: ".command.log", saveAs: { out_log },
enabled: params.logs_publish
script:
out_log = "${task.tag}.${task.process}.nflog.txt".replaceAll(":","_")
mask_out_dir = params.mask_out_dir
combined_out_dir = params.combined_out_dir
mask_outputs = "${mask_out_dir}/*"
combined_outputs = "${combined_out_dir}/*"
seed = params.mask_seed
mirna_fasta = files_to_fasta(mirna_files)
mirna_note = files_to_note(mirna_files)
transcript_fasta = files_to_fasta(transcript_files)
transcript_note = transcript_fasta + '.notes.txt'
transcript_details = "${transcript_fasta}".replaceAll('.fa', '.csv')
mask_fasta = "${mask_out_dir}/${file(transcript_fasta).getBaseName()}.fa"
mask_fasta = "${mask_fasta}".replaceAll('orig-transcripts', 'masked')
mask_fasta_name = file(mask_fasta).getName()
mask_details = "${mask_fasta}".replaceAll('.fa', '.csv')
combined_fasta = "${combined_out_dir}/${file(transcript_fasta).getBaseName()}.fa"
combined_fasta = "${combined_fasta}".replaceAll('orig-transcripts', 'masked-combined')
combined_fasta_name = file(combined_fasta).getName()
combined_details = "${combined_fasta}".replaceAll('.fa', '.csv')
bt2_db = "${transcript_fasta}" - ~/.fa$/
mirna_alns_sam = ("${mirna_fasta}" - ~/.fa$/) + ".sam"
mirna_alns_bed = mirna_alns_sam.replace('.sam', '.bed')
if( task.memory ) {
reduced_mem = ( task.memory * 0.9 )
split_mem = ( reduced_mem.div(task.cpus) )
mem_flag = "-m ${split_mem.toUnit("MB")}"
}
shell:
'''
set -v -H -o history
echo "All input mirna files: !{mirna_files}"
echo "All input transcript files: !{transcript_files}"
mkdir !{mask_out_dir}
mkdir !{combined_out_dir}
unset BOWTIE2_INDEXES
echo -e "Aligning miRNAs: !{mirna_fasta}"
echo -e " to Transcripts: !{transcript_fasta}"
echo -e " to find overlap."
set -v -H -o history
bowtie2-build --threads !{task.cpus} !{transcript_fasta} !{bt2_db}
ALL_COMMANDS="!!"
bowtie2 -x !{bt2_db} -f !{mirna_fasta} -S !{mirna_alns_sam} \\
--threads !{task.cpus} \\
--no-unal --xeq \\
--norc -D 20 -R 3 -N 0 -L 16 -a --local -i S,1,0.50 \\
--score-min L,17,0 --ma 1 --np 0 --mp 2,2 --rdg 5,1 --rfg 5,1
COMMAND="!!"
ALL_COMMANDS="${ALL_COMMANDS}\\n ${COMMAND}"
set +v +H +o history
# ---- Create bed file from alignments ----
echo -e "\\nProcessing alignments into BED file"
set -v -H -o history
sam2bed < !{mirna_alns_sam} | cut -f1-6 > !{mirna_alns_bed}
COMMAND="!!"
ALL_COMMANDS="${ALL_COMMANDS}\\n ${COMMAND}"
set +v +H +o history
#echo "reducing size of miRNA-regions by 6 nt"
#set -v -H -o history
#bedtools slop -i ${ALN_BED} -g ${SEQ_GENOME} -b "-6" > ${ALN_BED_TRIM} #/.bed/_unsort.bed}
#COMMAND="!!"
#ALL_COMMANDS="${ALL_COMMANDS}\\n${COMMAND}"
#set +v +H +o history
# ---- Create masked fasta ----
set -v -H -o history
bedtools maskfasta -fi !{transcript_fasta} -bed !{mirna_alns_bed} -fo !{mask_fasta}
COMMAND="!!"
ALL_COMMANDS="${ALL_COMMANDS}\\ ${COMMAND}"
set +v +H +o history
# ---- Create notes file for !{mask_fasta} ----
cp -v !{transcript_fasta}.notes.txt !{mask_fasta}.notes.txt
echo -e "Passing through transcript detail files:"
cp -vPR !{transcript_details} !{mask_details}
cp -vPR !{transcript_details}.notes.txt !{mask_details}.notes.txt
echo -e """
-- !{mask_fasta_name} --
File: '!{mask_fasta_name}' was created on $(date).
by commands:
${ALL_COMMANDS}
""" >> !{mask_fasta}.notes.txt
# ---- Create combined fasta file ----
cat !{mask_fasta} !{mirna_fasta} > !{combined_fasta}
# ---- Create notes file for combined fasta file ----
cat !{mask_fasta}.notes.txt !{mirna_note} > !{combined_fasta}.notes.txt
echo -e """