-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvk_nanopore_assembly.sh
1684 lines (1337 loc) · 48.3 KB
/
vk_nanopore_assembly.sh
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
#!/bin/bash
# Process and assemble Oxford nanopore sequencer data in fast5 format
# Include file format conversion, stats, qc and de novo assembly
#script version
version="0.4.7"
######################
# #
# User Defined #
# #
######################
# Analysis folder
export baseDir=/media/2TB_NVMe/NPWGS_20200217
# Reads
export fast5=/media/30tb_raid10/data/Mbovis/canada/nanopore/NPWGS_20200217/MBWGS183/20200218_2125_MN28835_ACA114_b6146520/fast5
# MinION sequencing kits
kit="SQK-LSK109" # Library kit: SQK-LSK109, SQK-LSK108 (discontinued), SQK-RBK004 (rapid),
flow="FLO-MIN106" # Flowcell kit: FLO-MIN106, FLO-FLG001
bk="EXP-NBD104" # Barcoding kit: SQK-RBK004 (rapid), EXP-NBD104 (1-12), EXP-NBD114 (13-24)
# Barcode to sample name file
bc_desc=/media/30tb_raid10/data/meta90_low_parasites/minion/bc.txt
# Program location
export prog=""${HOME}"/prog"
export scripts=""${HOME}"/scripts"
# Maximum number of cores used per sample for parallel processing
# A highier value reduces the memory footprint.
export maxProc=4
# Estimated genome size in bp
# export size=4850000 # salmonella
# export size=6000000 # Pseudomonas
export size=4400000 # Mbovis
# Set assembler to use: unicycler, unicycler_hybrid, canu shasta, or flye
export assembler="shasta"
# Set smallest contig size for assemblies
export smallest_contig=1000
# #Kraken DB to use
# export kraken2_db="/media/30tb_raid10/db/kraken2/standard" # refseq
export kraken2_db="/media/30tb_raid10/db/kraken2/nt"
#Annotation
export kingdom="Bacteria"
# export genus="Salmonella"
# export species="enterica"
export genus="Pseudomonas"
export species="syringaea"
export gram="neg"
export locus_tag="TOCHANGE"
export centre="OLF"
######################
# #
# Resources #
# #
######################
# Computer performance
export cpu=$(nproc) #total number of cores
export mem=$(($(grep MemTotal /proc/meminfo | awk '{print $2}')*85/100000000)) #85% of total memory in GB
memJava="-Xmx"$mem"g"
#######################
# #
# Data Stucture #
# #
#######################
# Folder structure
export logs=""${baseDir}"/logs"
export qc=""${baseDir}"/qc"
export fastq=""${baseDir}"/fastq"
export trimmed=""${baseDir}"/trimmed"
export filtered=""${baseDir}"/filtered"
export basecalled=""${baseDir}"/basecalled"
export assemblies=""${baseDir}"/assemblies"
export polished=""${baseDir}"/polished"
export annotation=""${baseDir}"/annotation"
export phaster=""${baseDir}"/phaster"
export amr=""${baseDir}"/amr"
# Create folders if do not exist
# "||" if test is false
# "&&" if test is true
[ -d "$baseDir" ] || mkdir -p "$baseDir"
[ -d "$logs" ] || mkdir -p "$logs"
[ -d "$qc" ] || mkdir -p "$qc"
[ -d "$fastq" ] || mkdir -p "$fastq"
[ -d "$trimmed" ] || mkdir -p "$trimmed"
[ -d "$filtered" ] || mkdir -p "$filtered"
[ -d "$basecalled" ] || mkdir -p "$basecalled"
[ -d "$assemblies" ] || mkdir -p "$assemblies"
[ -d "$polished" ] || mkdir -p "$polished"
[ -d "$annotation" ] || mkdir -p "$annotation"
[ -d "$phaster" ] || mkdir -p "$phaster"
[ -d "$amr" ] || mkdir -p "$amr"
################
# #
# Log #
# #
################
# Date
echo -e "$(date)\n" | tee "${logs}"/log.txt
echo -e "User: $(whoami)" | tee -a "${logs}"/log.txt
echo -e "Processors: "$cpu"" | tee -a "${logs}"/log.txt
echo -e "Memory: "$mem"G" | tee -a "${logs}"/log.txt
#script version
echo -e "\nnanopore_assembly.sh version "$version"\n" | tee -a "${logs}"/log.txt # $0
####################
# #
# Dependencies #
# #
####################
# Check for dependencies and log versions
# Java
if hash java 2>/dev/null; then
java -version 2>&1 1>/dev/null | grep "java version" | tr -d '"' | tee -a "${logs}"/log.txt
else
echo >&2 "java was not found. Aborting." | tee -a "${logs}"/log.txt
exit 1
fi
# Python
if hash python3 2>/dev/null; then
python3 --version | tee -a "${logs}"/log.txt
else
echo >&2 "python3 was not found. Aborting." | tee -a "${logs}"/log.txt
exit 1
fi
# Guppy
if hash guppy_basecaller 2>/dev/null; then
v=$(guppy_basecaller --version | rev | cut -d " " -f 1 | rev)
echo "Guppy v"${v}"" | tee -a "${logs}"/log.txt
else
echo >&2 "Guppy was not found. Aborting." | tee -a "${logs}"/log.txt
exit 1
fi
# FastQC
if hash fastqc 2>/dev/null; then
fastqc -v | tee -a "${logs}"/log.txt
else
echo >&2 "fastQC was not found. Aborting." | tee -a "${logs}"/log.txt
exit 1
fi
# MultiQC
source activate multiqc
if hash multiqc 2>/dev/null; then
multiqc --version | tee -a "${logs}"/log.txt
source deactivate
else
echo >&2 "multiqc was not found. Aborting." | tee -a "${logs}"/log.txt
exit 1
fi
# Kraken2
if hash kraken2 2>/dev/null; then
kraken2 --version | grep -F "version" | tee -a "${logs}"/log.txt
else
echo >&2 "kraken2 was not found. Aborting." | tee -a "${logs}"/log.txt
exit 1
fi
# Porechop
if hash porechop 2>/dev/null; then
v=$(porechop --version)
echo "Porechop v"${v}"" | tee -a "${logs}"/log.txt
else
echo >&2 "porechop was not found. Aborting." | tee -a "${logs}"/log.txt
exit 1
fi
# Filtlong
if hash filtlong 2>/dev/null; then
filtlong --version | tee -a "${logs}"/log.txt
else
echo >&2 "filtlong was not found. Aborting." | tee -a "${logs}"/log.txt
exit 1
fi
# Flye
if hash flye 2>/dev/null; then
v=$(flye --version)
echo "flye v"${v}"" | tee -a "${logs}"/log.txt
else
echo >&2 "flye was not found. Aborting." | tee -a "${logs}"/log.txt
exit 1
fi
# Unicycler
if hash unicycler 2>/dev/null; then
unicycler --version | tee -a "${logs}"/log.txt
else
echo >&2 "unicycler was not found. Aborting." | tee -a "${logs}"/log.txt
exit 1
fi
# Shasta
if hash shasta 2>/dev/null; then
shasta --version | grep -F "Shasta" | tee -a "${logs}"/log.txt
else
echo >&2 "shasta was not found. Aborting." | tee -a "${logs}"/log.txt
exit 1
fi
# Medaka
source activate medaka
if [ $? -eq 0 ]; then # environment was found and activated without error
medaka --version | tee -a "${logs}"/log.txt
source deactivate
else
echo >&2 "medaka was not found. Aborting." | tee -a "${logs}"/log.txt
exit 1
fi
#QUAST
source activate quast
if hash quast 2>/dev/null; then
quast --version | tee -a "${logs}"/log.txt
source deactivate
else
echo >&2 "QUAST was not found. Aborting." | tee -a "${logs}"/log.txt
exit 1
fi
# Mummer
if hash porechop 2>/dev/null; then
v=$(mummer --version)
echo "Mummer v"${v}"" | tee -a "${logs}"/log.txt
else
echo >&2 "mummer was not found. Aborting." | tee -a "${logs}"/log.txt
exit 1
fi
# Prokka
source activate prokka
if [ $? -eq 0 ]; then # environment was found and activated without error
prokka --version | tee -a "${logs}"/log.txt
source deactivate
else
echo >&2 "prokka was not found. Aborting." | tee -a "${logs}"/log.txt
exit 1
fi
# ResFinder
perl "${prog}"/resfinder/resfinder.pl -h
if [ $# -eq 0 ]; then
version=$(perl "${prog}"/resfinder/resfinder.pl -h | grep "Current" \
| cut -d ":" -f 2 | tr -d " ")
echo "resfinder v"${version}"" | tee -a "${logs}"/log.txt
else
echo >&2 "resfinder was not found. Aborting." | tee -a "${logs}"/log.txt
exit 1
fi
# minimap2
if hash minimap2 2>/dev/null; then
v=$(minimap2 --version 2>&1)
echo "minimap2 "$v"" | tee -a "${logs}"/log.txt
else
echo >&2 "minimap2 was not found. Aborting." | tee -a "${logs}"/log.txt
exit 1
fi
# Samtools
if hash samtools 2>/dev/null; then
samtools --version | grep -F 'samtools' | tee -a "${logs}"/log.txt
else
echo >&2 "samtools was not found. Aborting." | tee -a "${logs}"/log.txt
exit 1
fi
# Qualimap
if hash qualimap 2>/dev/null; then
qualimap --version | grep -F "QualiMap" | tee -a "${logs}"/log.txt
else
echo >&2 "qualimap was not found. Aborting." | tee -a "${logs}"/log.txt
exit 1
fi
# Blast
if hash blastn 2>/dev/null; then
v=$(blastn -version | grep -F "blastn" | cut -d " " -f 2)
echo "blastn v"${v}"" | tee -a "${logs}"/log.txt
else
echo >&2 "blastn was not found. Aborting." | tee -a "${logs}"/log.txt
exit 1
fi
# nanoQC
# Phaster
# CARD + RGI
####################################
# #
# Basecalling + Demultiplexing #
# #
####################################
guppy_bin_folder=$(dirname $(which guppy_basecaller))
guppy_data_folder="${guppy_bin_folder%/bin}/data"
##### GPU mode #####
#runners * chunks_per_runner * chunk_size < 100000 * [max GPU memory in GB]
# 2 * 1000 * 1000 < 100000 * 24
guppy_basecaller \
--input_path "$fast5" \
--save_path "$basecalled" \
--kit "$kit" \
--flowcell "$flow" \
--recursive \
--records_per_fastq 0 \
--compress_fastq \
--disable_pings \
--model_file "${guppy_data_folder}"/template_r9.4.1_450bps_hac.jsn \
--calib_detect \
--calib_reference lambda_3.6kb.fasta \
--hp_correct 1 \
--trim_strategy 'dna' \
--trim_threshold 2.5 \
--trim_min_events 3 \
--qscore_filtering \
--min_qscore 7 \
--num_callers 4 \
--gpu_runners_per_device 2 \
--chunk_size 1000 \
--chunks_per_runner 1000 \
--device "cuda:0" \
--num_barcode_threads "$cpu" \
--barcode_kits "$bk" \
--trim_barcodes
# --require_barcodes_both_ends
# If 1D2 experiment, run guppy_basecaller_1d2
guppy_basecaller_1d2 \
--input_path "$basecalled" \
--save_path "$fastq" \
--index_file "$basecalled"/sequencing_summary.txt \
--records_per_fastq 0 \
--compress_fastq \
--disable_pings \
--config "${guppy_data_folder}"/dna_r9.5_450bps_1d2_raw.cfg \
--num_callers 4 \
--gpu_runners_per_device 2 \
--chunk_size 1000 \
--chunks_per_runner 1000 \
--device "cuda:0"
# Trim and demultiplex already basecalled fastq
guppy_barcoder \
--input_path "$basecalled" \
--save_path "$fastq" \
--recursive \
--records_per_fastq 0 \
--compress_fastq \
--device "cuda:0" \
--barcode_kits "$bk" \
--trim_barcodes
# # no barcodes
# for i in $(find "$basecalled" -mindepth 1 -maxdepth 1 -type d); do
# flag=$(basename "$i")
# cat "${i}"/*.fastq.gz > "${i}"/"${flag}".fastq.gz
# # Remove non compressed files
# find "$i" -type f -name "*fastq_runid_*" -exec rm {} \;
# done
# Merge the basecalled and demultiplexed fastq
for i in $(find "$basecalled" -mindepth 2 -maxdepth 2 -type d); do # pass and fail
barcode=$(basename "$i")
flag=$(basename $(dirname "$i"))
# echo "${i}"/"${barcode}"_"${flag}".fastq.gz
# cat "${i}"/*.fastq.gz > "${i}"/"${barcode}"_"${flag}".fastq.gz #bash: /bin/cat: Argument list too long
find "$i" -mindepth 1 -maxdepth 1 -type f -name "*.fastq.gz" -name "fastq_runid_*" \
-exec cat {} \; > "${i}"/"${barcode}"_"${flag}".fastq.gz
# # Remove non compressed files
find "$i" -type f -name "*fastq_runid_*" -exec rm {} \;
done
#Rename samples
# Needs a translation files with 2 tab-separated columns
# barcode01 Sample1
# Parse translation talbe
#Do hash with conversion table
declare -A myArray=()
while IFS= read -r line || [[ -n "$line" ]]; do
line="$(sed -e 's/ /\t/' -e 's/\r//g' -e 's/\n//g' <<< "$line")" #transform the space output field separator from read into tabs, remove carriage return
key="$(cut -f 1 <<< "$line")"
value="$(cut -f 2 <<< "$line")"
myArray["${key}"]="${value}"
done < "$bc_desc"
# rename files
find "${basecalled}"/ -type f -name "*_pass*" -o -name "*_fail*" | while read i; do
# echo "$i"
pathPart="$(dirname "$i")"
# echo "$pathPart"
oldName="$(basename "$i")"
# echo "$oldName"
#for each file, check if a part of the name matches on
for j in "${!myArray[@]}"; do
# echo "$j"
if [ "$(echo "$oldName" | grep "$j")" ]; then
newName="$(echo "$oldName" | sed "s/"$j"/"${myArray["$j"]}"/")"
fullNewName=""${pathPart}"/"${newName}""
if [ -e "$rename" ]; then
echo "Cannot rename "$oldName" to "$newName", file already exists. Skipping"
continue
# exit 1
fi
echo ""$i" -> "$fullNewName""
mv "$i" "$fullNewName"
fi
if [ "$(echo "$pathPart" | grep "$j")" ]; then
# rename folder too
echo ""$pathPart" -> $(dirname "$pathPart")/"${myArray["$j"]}""
mv $pathPart $(dirname "$pathPart")/"${myArray["$j"]}"
fi
done
done
##########
# #
# QC #
# #
##########
# # nanoQC on "sequencing_summary.txt" file
# [ -d "${qc}"/nanoQC/raw/summary ] || mkdir -p "${qc}"/nanoQC/raw/summary
# python3 /home/bioinfo/PycharmProjects/nanoQC/nanoQC_Guppy_v3.1.5.py \
# -s "${basecalled}"/sequencing_summary.txt \
# -o "${qc}"/nanoQC/raw/summary
# Delete calibration strand folder if present
[ -d "${basecalled}"/calibration_strands ] && rm -rf "${basecalled}"/calibration_strands
# nanoQC on fastq files
[ -d "${qc}"/nanoQC/raw/fastq ] || mkdir -p "${qc}"/nanoQC/raw/fastq
python3 /home/bioinfo/PycharmProjects/nanoQC/nanoQC_Guppy_v3.1.5.py \
-f "$basecalled" \
-o "${qc}"/nanoQC/raw/fastq
function run_fastqc()
{
# sample=$(cut -d "_" -f 1 <<< $(basename "$1"))
sample=$(basename "${1%_filtered.fastq.gz}")
sample="${sample%_pass.fastq.gz}"
sample="${sample%_fail.fastq.gz}"
[ -d "${2}"/"$sample" ] || mkdir -p "${2}"/"$sample"
fastqc \
--o "${2}"/"$sample" \
--noextract \
--threads $((cpu/maxProc)) \
"$1"
}
export -f run_fastqc
# Create folder to store report
[ -d "${qc}"/fastqc/raw/pass ] || mkdir -p "${qc}"/fastqc/raw/pass
[ -d "${qc}"/fastqc/raw/fail ] || mkdir -p "${qc}"/fastqc/raw/fail
#raw
find "${basecalled}"/pass -type f -name "*.fastq.gz" \
| parallel --bar run_fastqc {} "${qc}"/fastqc/raw/pass
find "${basecalled}"/fail -type f -name "*.fastq.gz" \
| parallel --bar run_fastqc {} "${qc}"/fastqc/raw/fail
#Merge all FastQC reports together
source activate multiqc
multiqc \
-o "${qc}"/fastqc/raw/pass \
-n merged_pass.html \
"${qc}"/fastqc/raw/pass
multiqc \
-o "${qc}"/fastqc/raw/fail \
-n merged_fail.html \
"${qc}"/fastqc/raw/fail
source deactivate
################
# #
# Trimming #
# #
################
# Trim adapters
function chop()
{
sample=$(cut -d "_" -f 1 <<< $(basename "$1"))
porechop \
-i "$1" \
-o "${trimmed}"/"${sample}"_trimmed.fastq.gz \
--threads $((cpu/maxProc)) \
| tee -a "${logs}"/trimming/"${sample}"_porechop.log
# cleanup the log file by removing the progress lines
cat "${logs}"/trimming/"${sample}"_porechop.log \
| grep -vF '%' \
> "${logs}"/trimming/"${sample}"_porechop.log.tmp
mv "${logs}"/trimming/"${sample}"_porechop.log.tmp \
"${logs}"/trimming/"${sample}"_porechop.log
}
export -f chop
[ -d "${logs}"/trimming ] || mkdir -p "${logs}"/trimming
find "${basecalled}/pass" -type f -name "*.fastq.gz" ! -name "*unclassified*" \
| parallel --bar \
--env chop \
--env trimmed \
--env logs \
--env cpu \
--env maxProc \
--jobs "$maxProc" \
'chop {}'
#################
# #
# Filtering #
# #
#################
### Careful here. Filtered reads sometimes lead to a poorer quality assembly.
function filter()
{
#sample=$(cut -d "_" -f 1 <<< $(basename "$1"))
sample=$(basename "$1" "_trimmed.fastq.gz")
# https://github.com/rrwick/Filtlong
# --min_mean_q 90 -> phred 10
# --min_length 1000 \
# --target_bases -> max 100X coverage
filtlong \
--target_bases $((size*100)) \
--keep_percent 90 \
"$1" \
| pigz > "${filtered}"/"${sample}"_filtered.fastq.gz
# 2> >(tee "${logs}"/filtering/"${sample}".txt) \
}
export -f filter
[ -d "${logs}"/filtering ] || mkdir -p "${logs}"/filtering
find "$trimmed" -type f -name "*_trimmed.fastq.gz" \
| parallel --bar \
--env filter \
--env trimmed \
--env size \
--env filtered \
--env logs \
--jobs "$maxProc" \
'filter {}'
# No filtering
# Just create symbolic links
# find "$basecalled" -type f -name "*_pass.fastq.gz" ! -name "*unclassified*" -exec ln -s {} "$filtered" \;
######################
# #
# Quality Control #
# #
######################
# nanoQC on fastq file
[ -d "${qc}"/nanoQC/filtered ] || mkdir -p "${qc}"/nanoQC/filtered
python3 /home/bioinfo/PycharmProjects/nanoQC/nanoQC_Guppy_v3.1.5.py \
-f "$filtered" \
-o "${qc}"/nanoQC/filtered
### FastQC###
# Create folder to store report
[ -d "${qc}"/fastqc/filtered ] || mkdir -p "${qc}"/fastqc/filtered
#filtered
find -L "$filtered" -type f -name "*.fastq.gz" \
| parallel --bar run_fastqc {} "${qc}"/fastqc/filtered
#Merge all FastQC reports together
source activate multiqc
multiqc \
-o "${qc}"/fastqc/filtered \
-n merged_filtered.html \
"${qc}"/fastqc/filtered
source deactivate
#########################################
# #
# Taxonomic Classification of reads #
# #
#########################################
[ -d "${qc}"/kraken2/raw ] || mkdir -p "${qc}"/kraken2/raw
function run_kraken2 ()
{
sample=$(cut -d "_" -f 1 <<< $(basename "$1"))
# Create folder to store report
[ -d "${2}"/"${sample}" ] || mkdir -p "${2}"/"${sample}"
#run Kraken
kraken2 \
--memory-mapping \
--db "$kraken2_db" \
--output "${2}"/"${sample}"/"${sample}".kraken \
--report "${2}"/"${sample}"/"${sample}".report.tsv \
--threads "$cpu" \
--gzip-compressed \
"$1" \
&> >(tee "${2}"/"${sample}"/"${sample}".kraken2.log)
#Prepare result for display with Krona
ktImportTaxonomy \
-q 2 \
-t 3 \
-o "${2}"/"${sample}"/"${sample}".html \
"${2}"/"${sample}"/"${sample}".kraken
}
for i in $(find -L "${basecalled}/pass" -type f -name "*fastq.gz"); do
run_kraken2 "$i" "${qc}"/kraken2/raw
done
# function run_centrifuge()
# {
# sample=$(cut -d "_" -f 1 <<< $(basename "$1"))
# [ -d "${2}"/"${sample}" ] || mkdir -p "${2}"/"${sample}"
# #build the command
# centrifuge \
# -p "$cpu" \
# --mm \
# -t \
# --seed "$RANDOM" \
# -x "$db" \
# -U "$1" \
# --report-file "${2}"/"${sample}"/"${sample}"_report.tsv \
# > "${2}"/"${sample}"/"${sample}".tsv
# cat "${2}"/"${sample}"/"${sample}".tsv | \
# cut -f 1,3 | \
# ktImportTaxonomy /dev/stdin -o "${2}"/"${sample}"/"${sample}".html
# # Visualize the resutls in Firefow browser
# firefox file://"${2}"/"${sample}"/"${sample}".html &
# }
# for i in $(find "${basecalled}"/pass -type f -name "*fastq.gz"); do
# run_centrifuge "$i" "${qc}"/centrifuge/raw
# done
# ### CCMetagen
# # https://github.com/vrmarcelino/CCMetagen
# conda activate ccmetagen
# function ccmeta_it()
# {
# input="$1"
# sample=$(cut -d "_" -f 1 <<< $(basename "$1"))
# [ -d "${qc}"/ccmetagen/"$sample" ] || mkdir -p "${qc}"/ccmetagen/"$sample"
# # Map reads with KMA
# kma \
# -i "$input" \
# -o "${qc}"/ccmetagen/"${sample}"/"$sample" \
# -t_db /media/30tb_raid10/db/CCMetagen/compress_ncbi_nt/ncbi_nt \
# -t "$cpu" \
# -bcNano \
# -1t1 -mem_mode -and
# CCMetagen.py \
# -r nt \
# -i "${qc}"/ccmetagen/"${sample}"/"${sample}".res \
# -o "${qc}"/ccmetagen/"${sample}"/"${sample}".ccmeta
# CCMetagen_merge.py \
# -t Family \
# -i "${qc}"/ccmetagen/"${sample}" \
# -o "${qc}"/ccmetagen/"${sample}"/merged_samples
# }
# source deactivate
########################
# #
# de novo assembly #
# #
########################
function assemble()
{
sample=$(basename "${1%_filtered.fastq.gz}")
ass="$2"
[ -d "${assemblies}"/"${ass}" ] || mkdir -p "${assemblies}"/"${ass}"
[ -d "${qc}"/assembly_graphs/"$ass" ] || mkdir -p "${qc}"/assembly_graphs/"$ass"
if [[ "$ass" == "unicycler" ]]; then
unicycler \
-l "$1" \
-o "${assemblies}"/"${ass}"/"$sample" \
-t $((cpu/maxProc)) \
--verbosity 2 \
--mode normal
mv "${assemblies}"/"${ass}"/"${sample}"/assembly.fasta \
"${assemblies}"/"${ass}"/"${sample}"/"${sample}".fasta
mv "${assemblies}"/"${ass}"/"${sample}"/assembly.gfa \
"${assemblies}"/"${ass}"/"${sample}"/"${sample}".gfa
Bandage image \
"${assemblies}"/"${ass}"/"${sample}"/"${sample}".gfa \
"${qc}"/assembly_graphs/"${ass}"/"${sample}".png
elif [[ "$ass" == "unicycler_hybrid" ]]; then
# $3 unmerged R1
# $4 unmerged R2
# $5 merged
unicycler \
-1 "$3" \
-2 "$4" \
-s "$5" \
--no_correct \
-l "$1" \
-o "${assemblies}"/"${ass}"/"$sample" \
-t $((cpu/maxProc)) \
--verbosity 2 \
--mode normal \
--pilon_path "${prog}"/pilon/pilon-dev.jar
mv "${assemblies}"/"${ass}"/"${sample}"/assembly.fasta \
"${assemblies}"/"${ass}"/"${sample}"/"${sample}".fasta
elif [[ "$ass" == "canu" ]]; then
canu \
-p "$sample" \
-d "${assemblies}"/"${ass}"/"$sample" \
genomeSize="$size" \
maxThreads=$((cpu/maxProc)) \
-nanopore-raw "$1"
# rename assembly
mv "${assemblies}"/"${ass}"/"${sample}"/"${sample}".contigs.fasta \
"${assemblies}"/"${ass}"/"${sample}"/"${sample}".fasta
mv "${assemblies}"/"${ass}"/"${sample}"/"${sample}".contigs.gfa \
"${assemblies}"/"${ass}"/"${sample}"/"${sample}".gfa
Bandage image \
"${assemblies}"/"${ass}"/"${sample}"/"${sample}".gfa \
"${qc}"/assembly_graphs/"${ass}"/"${sample}".png
# Cleanup
find "${assemblies}"/"${ass}"/"$sample" -mindepth 1 -maxdepth 1 \
! -name "*.fasta" \
! -name "*.gfa" \
! -name "*.report" \
-o -name "*unitigs*" \
-o -name "*unassembled*" \
-exec rm -rf {} \;
elif [[ "$ass" == "flye" ]]; then
flye \
--nano-raw "$1" \
--out-dir "${assemblies}"/"${ass}"/"$sample" \
--genome-size "$size" \
--iterations 3 \
--threads $((cpu/maxProc))
# Rename assembly
mv "${assemblies}"/"${ass}"/"${sample}"/assembly.fasta \
"${assemblies}"/"${ass}"/"${sample}"/"${sample}".fasta
mv "${assemblies}"/"${ass}"/"${sample}"/assembly_graph.gfa \
"${assemblies}"/"${ass}"/"${sample}"/"${sample}".gfa
Bandage image \
"${assemblies}"/"${ass}"/"${sample}"/"${sample}".gfa \
"${qc}"/assembly_graphs/"${ass}"/"${sample}".png
# Maybe some cleanup
find "${assemblies}"/"${ass}"/"$sample" -mindepth 1 -maxdepth 1 \
! -name "*.fasta" \
! -name "*.gfa" \
! -name "*.log" \
! -name "*.txt" \
-exec rm -rf {} \;
# else
# echo 'Please chose one of the following assembler: "unicycler", "unicycler_hybrid", "canu" or "flye"'
fi
}
export -f assemble
find -L "${filtered}" -type f -name "*.fastq.gz" \
| parallel --bar \
--env cpu \
--env maxProc \
--env assemble \
--env assembler \
--env assemblies \
--env fastq2fasta \
--env size \
--jobs "$maxProc" \
"assemble {} "$assembler""
function fastq2fasta ()
{
input_fastq="$1" # assume gzipped file
if [ $(echo "$input_fastq" | grep -F ".fastq.gz") ]; then
zcat "$input_fastq" \
| sed -n '1~4s/^@/>/p;2~4p' \
> "${input_fastq%.fastq.gz}.fasta"
elif [ $(echo "$input_fastq" | grep -F ".fastq") ]; then
cat "$input_fastq" \
| sed -n '1~4s/^@/>/p;2~4p' \
> "${input_fastq%.fastq}.fasta"
else
echo "Wrong file extension. Please use \".fastq\" or \".fastq.gz\""
exit 1
fi
}
export -f fastq2fasta
function shasta_it()
{
sample=$(basename "${1%_filtered.fastq.gz}")
ass=shasta
file_ext=""
if [ $(echo "$1" | grep -F ".fastq.gz") ]; then
file_ext=".fastq.gz"
elif [ $(echo "$1" | grep -F ".fastq") ]; then
file_ext=".fastq"
else
echo "Wrong file extension. Please use \".fastq\" or \".fastq.gz\""
# exit 1
fi
# Convert reads to fasta
echo "Converting fastq to fasta for Shasta..."
fastq2fasta "$1"
[ -d "${assemblies}"/"${ass}" ] || mkdir -p "${assemblies}"/"${ass}"
mv "${1%$file_ext}.fasta" "${assemblies}"/"${ass}"/"${sample}".fasta
# Two problems here:
# 1- Requires sudo
# 2- Defaults to all CPUs
# shasta --command assemble \
# --input "${assemblies}"/"${ass}"/"${sample}".fasta \
# --assemblyDirectory "${assemblies}"/"${ass}"/"$sample" \
# --memoryBacking 2M \
# --memoryMode filesystem \
# --Reads.minReadLength=10000
shasta \
--command assemble \
--input ""${assemblies}"/"${ass}"/"${sample}".fasta" \
--assemblyDirectory "${assemblies}"/"${ass}"/"$sample" \
--memoryBacking 2M \
--memoryMode filesystem \
--Reads.minReadLength=10000 \
--threads "$cpu" \
--MinHash.minBucketSize 5 \
--MinHash.maxBucketSize 30 \
--MinHash.minFrequency 5 \
--Align.minAlignedFraction 0.4 \
--Assembly.consensusCaller 'Bayesian:guppy-3.0.5-a'
shasta --command cleanupBinaryData \
--assemblyDirectory "${assemblies}"/"${ass}"/"$sample"
# remove input fasta file
rm "${assemblies}"/"${ass}"/"${sample}".fasta
# Rename outputs
mv "${assemblies}"/"${ass}"/"${sample}"/Assembly.fasta \
"${assemblies}"/"${ass}"/"${sample}"/"${sample}".fasta
mv "${assemblies}"/"${ass}"/"${sample}"/Assembly.gfa \
"${assemblies}"/"${ass}"/"${sample}"/"${sample}".gfa
# Make assembly graph
[ -d "${qc}"/assembly_graphs/"$ass" ] || mkdir -p "${qc}"/assembly_graphs/"$ass"
Bandage image \
"${assemblies}"/"${ass}"/"${sample}"/"${sample}".gfa \
"${qc}"/assembly_graphs/"${ass}"/"${sample}".png
# Reformat fasta
perl "${scripts}"/formatFasta.pl \
-i "${assemblies}"/"${ass}"/"${sample}"/"${sample}".fasta \
-o "${assemblies}"/"${ass}"/"${sample}"/"${sample}".fasta1
mv "${assemblies}"/"${ass}"/"${sample}"/"${sample}".fasta1 \
"${assemblies}"/"${ass}"/"${sample}"/"${sample}".fasta
}
# Loop all the samples
if [[ "$assembler" == "shasta" ]]; then
for i in $(find -L "${filtered}" -type f -name "*.fastq.gz"); do
shasta_it "$i" # Needs sudo
done
fi
#################
# #
# Polishing #
# #
#################
# Marginpolish + HELEN to use on shasta assemblies -> seems to be more for human genome assemblies
# https://github.com/UCSC-nanopore-cgl/marginPolish
# https://github.com/kishwarshafin/helen
# Do 4 rounds of Racon polishing
# Polish with Medaka
# https://github.com/nanoporetech/medaka
source activate medaka
# source ~/my_virtualenv/medaka_gpu/bin/activate