/
runBNG
executable file
·2324 lines (2076 loc) · 92.4 KB
/
runBNG
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
# This script aims to help complete key Bionano OM analyses using command line
# ScriptName: runBNG
# Last modified: 05/04/2021
# Copyright: Copyright (c) 2017 Applided Bioinformatics Group @UWA, Perth, Australia
#
###====================================== General configuration check ==========================================
##system check
if [ "$(uname)" == "Darwin" ]; then
echo -e "\nWarning! Currently, '`basename $0`' is not supportted in MacOS system\n"; exit 1;
fi
##check readlink
RL=`which readlink`
if [[ ! -s "$RL" ]] || [[ ! -x "$RL" ]]; then
echo -e "\nOops! It seems 'readlink' is not installed in your system or executable. Please check!\n"; exit 1;
fi
##check python
mypython=`which python2`
if [[ ! -s "$mypython" ]] || [[ ! -x "$mypython" ]]; then
echo -e "\nOops! It seems 'python2' is not installed in your system or executable. Please check!\n"; exit 1;
fi
py_v=`python2 -c 'import sys; print(".".join(map(str, sys.version_info[:3])))'`
s1=`echo $py_v | cut -d"." -f1`
s2=`echo $py_v | cut -d"." -f2`
s3=`echo $py_v | cut -d"." -f3`
check=`awk '{ if ($1==2 && $2 ==7 && $3>=5) print "yes"}' <<< "$s1 $s2 $s3"`
if [[ $check != "yes" ]]; then
echo -e "\nPlease set your default python2 to v2.7.5 or above\n"; exit 1;
fi
##check perl
myperl=`which perl`
if [[ ! -s "$myperl" ]] || [[ ! -x "$myperl" ]];then
echo -e "\nOops! It seems 'perl' is not installed in your system or executable. Please check!\n"; exit 1;
fi
pl_v=`perl -e 'print $];'`
check1=`awk '{ if ($1 >=5.010 && $1 <5.011) print "yes"}' <<< "$pl_v"`
check2=`awk '{ if ($1 >=5.014 && $1 <5.015) print "yes"}' <<< "$pl_v"`
check3=`awk '{ if ($1 >=5.016 && $1 <5.017) print "yes"}' <<< "$pl_v"`
if [[ $check1 != "yes" ]] && [[ $check2 != "yes" ]] && [[ $check3 != "yes" ]]; then
echo -e "\nPlease set your default Perl to v5.10.x, v5.14.x or v5.16.x\n"; exit 1;
fi
##check gcc
mygcc=`which gcc`
if [[ ! -s "$mygcc" ]] || [[ ! -x "$mygcc" ]]; then
echo -e "\nOops! It seems 'gcc' is not installed in your system or executable. Please check!\n"; exit 1;
fi
currentver="$(gcc -dumpversion)"
requiredver="4.4.7"
if [ "$(printf "$requiredver\n$currentver" | sort -V | head -n1)" == "$currentver" ] && [ "$currentver" != "$requiredver" ]; then
echo -e "\nPlease set your default GCC to v4.4.7 or above\n"; exit 1;
fi
##check glibc
myldd=`which ldd`
if [[ ! -s "$myldd" ]] || [[ ! -x "$myldd" ]]; then
echo -e "\nOops! It seems 'glibc' is not installed in your system or execuatble. Please check!\n"; exit 1;
fi
currver=`ldd --version`
ver=`echo $currver | cut -d" " -f4`
check=`awk '{if($1>=2.15) print "yes"}' <<< "$ver"`
if [[ $check != "yes" ]];then
echo -e "\nPlease set your default glibc to v2.15 or above\n"; exit 1;
fi
##======================================= Functions =========================================
function check_integer () {
if ! [[ $1 =~ ^[0-9]+$ ]]; then
echo -e "\nOpps! Your input '$1' is not an integer or smaller than 0. Please check!\n" 1>&2
exit 1
fi
}
check_option(){
if [[ -z "$1" ]]; then
echo -e "\nOops! One of the options: '$2' is missing. Please check!";
$3; exit 1;
fi
}
check_path(){
myDir=`$RL -f $1`;
if [[ ! -d "$myDir" ]] || [[ ! -w "$myDir" ]]; then
echo -e "\nOops! It seems the directory '$myDir' is not existent or writable, please check!\n";
exit 1;
fi
}
check_file(){
if [[ ! -s "$1" ]] || [[ ! -r "$1" ]]; then
echo -e "\nOops! It seems file: '${1}' is not existent or readable. Please check!\n"
exit 1
fi
}
check_yesno(){
if [[ "$1" != 'yes' ]] && [[ "$1" != 'no' ]]; then
echo -e "\nOops! Please use 'yes' or 'no' after the '$2' option\n"; exit 1;
fi
}
check_enz(){
if [[ `echo "$1" | grep -qi '^BspQI$' && echo "yes" || echo "no"` == "yes" ]]; then
echo "BSPQI"
elif [[ `echo "$1" | grep -qi '^BbvCI$' && echo "yes" || echo "no"` == "yes" ]]; then
echo "BBVCI"
elif [[ `echo "$1" | grep -qi '^BsmI$' && echo "yes" || echo "no"` == "yes" ]]; then
echo "BSMI"
elif [[ `echo "$1" | grep -qi '^BsrDI$' && echo "yes" || echo "no"` == "yes" ]]; then
echo "BSRDI"
elif [[ `echo "$1" | grep -qi '^bseCI$' && echo "yes" || echo "no"` == "yes" ]]; then
echo "BSECI"
elif [[ `echo "$1" | grep -qi '^BssSI$' && echo "yes" || echo "no"` == "yes" ]]; then
echo "BSSSI"
elif [[ `echo "$1" | grep -qi '^DLE1$' && echo "yes" || echo "no"` == "yes" ]]; then
echo "DLE1"
else
echo -e "Oops! We cannot recognise the enzyme name. Please check or contact us for a solution\n"
exit 1;
fi
}
check_tool(){
which $1 >/dev/null 2>&1;
if [[ $? != 0 ]]; then
echo -e "\nOops! '$1' is not in the PATH. Please check!\n";
exit 1;
fi
mytool=`$RL -f $(which $1)`;
if [[ ! -s "$mytool" ]] || [[ ! -x "$mytool" ]]; then
echo -e "\nOops! It seems '$1' is not executable. Please check!\n";
exit 1;
fi
}
VERSION=2.0.1
###========================================== messages ===============================================
##Start message
usage_global(){
echo "
------------------------------------------------------------------------------------------------------
Program: `basename $0`
Version: $VERSION
Author: Yuxuan Yuan (yuxuan.yuan@outlook.com)
------------------------------------------------------------------------------------------------------
Synopsis: This pipeline aims to help complete key Bionano optical mapping analyses using command line
Usage: `basename $0` <command> [options]
Command: fa2cmap convert a given fasta format file into a cmap file
cmapstats check stats of a cmap file
bnxmerge merge different bnx files into one
bnxstats check stats of a bnx file
bnxfilter filter a bnx file
MQR get a molecule quality report for the Bionano data
repeatCheck check repeats using Bionano raw data
denovo de novo assemble Bionano single molecule maps
compare compare two different cmap files
hybrid perform Bionano hybrid scaffolding using one enzyme
dual perform Bionano hybrid scaffolding using two enzymes
SV structural variation detection
options: -h/-help show this message and exit
-----------------------------------------------------------------------------------------------------
"
}
##Digest a fasta file into a cmap file
usage_digest(){
echo "
----------------------------------------------------------------------------------------
Synopsis: Digest a given fasta format file into a cmap file using particular enzyme
Usage: `basename $0` fa2cmap [options]
----------------------------------------------------------------------------------------
-f <str> a fasta format file to be digested
-o <str> full path to the output directory
-e <str> name of selected enzyme <BspQI|BbvCI|BsmI|BsrDI|BseCI|BssSI|DLE1>
-q <str> sequence of the enzyme when -e is not given
-z <int> filter criteria: min molecule length (kb). Default: 20
-l <int> filter criteria: min number of selected enzymes in the molecule. Default: 5
-s <str> full path to the latest Bionano Solve folder
-h/--help show this message and exit
----------------------------------------------------------------------------------------
"
}
##Merge multiple bnx files into a single bnx file
usage_merge(){
echo "
----------------------------------------------------------------------------------------
Synopsis: Merge different bnx files into one file. All bnx files should be generated
from the same sample using the same enzyme. The version of those bnx files
should be the same
Usage: `basename $0` bnxmerge [options]
----------------------------------------------------------------------------------------
-l <str> a file describing all bnx files to be merged:
Each line lists the full path along with a descriptive name for that file
-t <int> number of threads or CPUs
-m <int> memory used (Gb)
-p <str> prefix for the merged file
-r <str> full path to Bionano RefAligner
-o <str> full path to the output directory
-h/--help show this message and exit
----------------------------------------------------------------------------------------
"
}
##Filter unqualified molecule maps
usage_filter(){
echo "
------------------------------------------------------------------------------------------------
Synopsis: Molecule map quality control--filter unqualified molecule maps and get a new bnx file
Usage: `basename $0` bnxfilter [options]
------------------------------------------------------------------------------------------------
-b <str> the bnx file to be filtered
-t <int> number of threads or CPUs
-m <int> memory used (Gb)
-p <str> prefix for the output files
-r <str> full path to Bionano RefAligner
-l <int> min molecule length to be filtered (Kb). Default: 150
-s <int> min number of selected enzymes in the molecule. Default: 8
-M <int> max number of selected enzymes in the molecule. Default: 200
-i <flt> maxIntensity to be filtered. Default: 0.6
-x <flt> reduce resolution of input molecule maps by xxx*500bp. Default: 0.001
-o <str> full path to output directory
-h/--help show this message and exit
------------------------------------------------------------------------------------------------
"
}
##bnxstats
usage_bstats(){
echo "
------------------------------------------------------------------------------------------------
Synopsis: Check stats of a given bnx file (N_molecules, length, label density, SNR, intensity)
Usage: `basename $0` bnxstats [options]
------------------------------------------------------------------------------------------------
-b <str> the bnx file to be checked
-p <str> prefix for the table extracted from the bnx file
-o <str> full path to the output directory
-h/--help show this message and exit
------------------------------------------------------------------------------------------------
"
}
##MQR
usage_mqr(){
echo "
------------------------------------------------------------------------------------------------
Synopsis: Report the quality of given Bionano single molecules
Usage: `basename $0` MQR [options]
------------------------------------------------------------------------------------------------
-b <str> the raw molecule map file (e.g. Molecules.bnx)
-r <str> reference maps (.cmap). May use '`basename $0 fa2cmap`' to generate one if only have a fasta format reference
-z <int> genome size of input species (Mb). Using this option can help to estimate a suitable pvalue (optional)
-R <str> full path to Bionano RefAligner
-t <int> maximum threads or CPUs
-m <int> maximum memory used (Gb)
-s <int> minimum length of single molecules to be filtered. Default: 150 (Kb)
-n <int> times to regenerate the hashtable. Default: 3
-i <int> iterations for each hashtable. Default: 3
-l <int> cutoff of the label interval differences between matched ref and qry maps (Kb). Default: 40
Disable this option when an input value >1000
-d <int> label density of the reference genome if -r is given (xxx/100Kb). For example: 10 (optional)
-p <str> prefix for the output files
-o <str> full path to the output directory
-h/--help show this message and exit
------------------------------------------------------------------------------------------------
"
}
##cmap stats
usage_cmapstats(){
echo "
----------------------------------------------------------
Synopsis: Get stats of a cmap file
Usage: `basename $0` cmapstats [options]
----------------------------------------------------------
-c <str> full path to the cmap file
-s <str> full path to the Bionano Solve folder
-h/--help show this message and exit
----------------------------------------------------------
"
}
##Repeat check
usage_repeat(){
echo "
----------------------------------------------------------
Synopsis: Check repeats using Bionano raw data
Usage: `basename $0` repeatCheck [options]
----------------------------------------------------------
-b <str> the raw molecule map file (e.g. Molecules.bnx)
-p <str> prefix for the output files.
-R <str> full path to Bionano RefAligner
-o <str> full path to the output directory
-h/--help show this message and exit
----------------------------------------------------------
"
}
##Denovo options
usage_denovo(){
echo "
------------------------------------------------------------------------
Synopsis: De novo assembly for Bionano single molecules
Usage: `basename $0` denovo [options]
------------------------------------------------------------------------
-P <str> platform used to generate optical maps <irys|saphyr>. Default: saphyr
-H <str> haplotype based or not <yes|no>. Default: no (if human data, select: yes)
-M <str> the data from human or not <yes|no>. Default: no
-e <str> the enzyme is DLE1 or not <yes|no>. Default: yes
-c <str> cut complex multi-path Regions (>= 140 Kb) <yes|no>. Default: yes
This could decrease the number of chimeric maps and increase the total number of assembled maps.
For human genome, it's recommended. For other species try both.
-E <str> extend and split for maps <yes|no>. Default: yes
-t <str> full path to RefAligner folder
-s <str> full path to Bionano Solve folder
-b <str> the raw molecule map file (e.g. Molecules.bnx)
-r <str> the digested reference (.cmap). Default: NULL
-a <flt> label density (*/100kb). Default: NULL
-T <int> number of threads or CPUs
-l <int> minimum length to filter out (Kb). Default: irys [150]; saphyr [120]
-m <int> minimum labels on the molecule. Default: 8
-B <flt> maximum backbone intensity. Default: 0.6
-j <int> number of threads for each subjob
-i <int> times of iteration. Default: 5
-k <int> skip steps, using previous result. 0:None, 1:ImgDetect, 2:NoiseChar/Subsample,
3:Pairwise, 4:Assembly, 5:RefineA, 6:RefineB, 7:merge0, 8+(i-1)*2:Ext(i),
9+(i-1)*2:Mrg(i), N+1:alignmol. Default: 0
-z <int> the genome size of input species (Mb)
-p <flt> flase positive density (/100Kb). Default: 2.0
-n <flt> false negative rate (%/100). Default: 0.10
-d <flt> scalingSD (Kb^1/2). Default: 0.0
-f <flt> siteSD (Kb). Default: irys [0.15]; saphyr [0.12]
-R <flt> relativeSD. Default: 0.03
-L <int> large jobs maximum memory (GB). Default: 128
-S <int> small jobs maximum memory (GB). Default: 8
-o <str> full path to the output directory
-h/--help show this message and exit
------------------------------------------------------------------------
"
}
##Compare assembled Bionano data to a ref cmap file
usage_compare(){
echo "
------------------------------------------------------------------------
Synopsis: Compare a query cmap file to a ref cmap file
Usage: `basename $0` compare [options]
------------------------------------------------------------------------
-R <str> path to Bionano RefAligner
-r <str> full path to the reference cmap file
-q <str> full path to the query cmap file
-z <int> the genome size of input species (Mb)
-t <int> maximum threads or CPUs
-m <int> Memory used (Gb)
-p <str> prefix for the output files
-o <str> full path to the output directory
-h/--help show this message and exit
------------------------------------------------------------------------
"
}
##SV detection
usage_sv(){
echo "
------------------------------------------------------------------------------------------------
Synopsis: Compare cmaps from different individuals to detect structural variation.
Usage: `basename $0` SV [options]
------------------------------------------------------------------------------------------------
-P <str> platform used to generate optical maps <irys|saphyr>. Default: saphyr
-H <str> haplotype based or not <yes|no>. Default: no (if human data, select: yes)
-M <str> the data from human or not <yes|no>. Default: no
-e <str> the enzyme is DLE1 or not <yes|no>. Default: yes
-c <str> cut complex multi-path Regions (>= 140 Kb) <yes|no>. Default: yes
This could decrease the number of chimeric maps and increase the total number of assembled maps.
For human genome, it's recommended. For other species try both.
-E <str> extend and split for maps <yes|no>. Default: yes
-r <str> full path to the reference cmap file
-b <str> a .bed file containing gap information to assist the further accuracy of SV calling (optional)
-q <str> full path to the query cmap (can be the denovo assembled EXP_REFINEFINAL1.cmap)
-s <str> full path to Bionano Solve folder
-R <str> full path to Bionano RefAligner
-z <str> the genome size of input species (Mb)
-t <int> maximum threads or CPUs
-o <str> full path to the output directory
-h/--help show this message and exit
------------------------------------------------------------------------------------------------
"
}
##Hybrid pipeline
usage_hybrid(){
echo "
------------------------------------------------------------------------------------------------
Synopsis: Run the Bionano hybrid assembly pipeline for maps digested by one enzyme.
Usage: `basename $0` hybrid [options]
------------------------------------------------------------------------------------------------
-s <str> full path to Bionano Solve folder
-R <str> full path to Bionano RefAligner folder
-r <str> full path to the NGS sequence file
-b <str> full path to the raw molecule map file (e.g. Molecules.bnx)
-m <int> filter: min molecule length (Kb). Default: 0
-l <int> filter: min number of selected enzyme in molecule. Default: 0
-e <str> name of selected enzyme <BspQI|BbvCI|BsmI|BsrDI|BssSI|DLE1>. Default: DLE1
-B <int> Bionano conflict filter level: 1 no filter, 2 cut contig at conflict, 3 exclude conflicting contig
-N <int> NGS conflict filter level: 1 no filter, 2 cut contig at conflict, 3 exclude conflicting contig
-f <str> final assembled cmap file (e.g. EXP_REFINEFINAL1.cmap)
-x <int> the xml file used to produce the final assembled cmap file
-c <flt> Minimum confidence value to output intial alignments. Recommended starting value of 1e-5/genome size in Mb.
Default: 1e-10
-u <flt> Minimum confidence value used to flag chimeric/conflicting alignments. Default: 1e-11
-g <flt> Minimum confidence value used to merge alignments. Recommand to set it to be the same as the -u.
Default: irys [1e-13]; saphry [1e-11]
-d <int> The distance (kb) from a conflicting site within which the chimeric quality score of Bionano genome map labels will be examined.
Default: 10
-p <int> The minimal percentage (%) of molecules spanning to the left and right of a label of interest, thus supporting the Bionano assembly
at that region. Default: 35
-t <int> maximum threads or CPUs
-M <int> maximum RAM used (Gb)
-o <str> output directory
-h/--help show this message and exit
------------------------------------------------------------------------------------------------
"
}
usage_dual(){
echo "
------------------------------------------------------------------------------------------------
Synopsis: Run the Bionano hybrid assembly pipeline for maps digested by two enzymes.
Usage: `basename $0` dual [options]
------------------------------------------------------------------------------------------------
-s <str> full path to Bionano Solve folder
-R <str> full path to Bionano RefAligner
-1 <str> full path to the denovo assembled cmap file by the first enzyme
-2 <str> full path to the denovo assembled cmap file by the second enzyme
-e <str> name of first enzyme <BspQI|BbvCI|BsmI|BsrDI|BssSI|DLE1>
-E <str> name of second enzyme <BspQI|BbvCI|BsmI|BsrDI|BssSI|DLE1>
-n <str> full path to the NGS assembly
-o <str> output directory
-h/--help show this message and exit
------------------------------------------------------------------------------------------------
"
}
###=========================================================== Programs ==================================================================
##Print help if no agrs input
if [[ $# -eq 0 ]];then
usage_global;
exit 0;
fi
##Check the first arg
if [[ $1 != "fa2cmap" ]] && [[ $1 != "cmapstats" ]] && [[ $1 != "bnxstats" ]] && [[ $1 != "bnxmerge" ]] && [[ $1 != "bnxfilter" ]] && [[ $1 != "denovo" ]] && [[ $1 != "MQR" ]] && [[ $1 != "repeatCheck" ]] && [[ $1 != "compare" ]] && [[ $1 != "hybrid" ]] && [[ $1 != "dual" ]] && [[ $1 != "SV" ]];
then
if [[ $1 == "-h" ]] || [[ $1 == "-help" ]] || [[ $1 == "--help" ]]; then
usage_global;
exit 0;
else
echo -e "\nOops! Unknown option '"$1"'"
echo -e "Please check '`basename $0` -h' and continue...\n";
exit 1;
fi
fi
##======================= Select "fa2cmap" flag =======================
##Print help if only select "fa2cmap"
if [[ $1 == "fa2cmap" ]] && [[ $# -eq 1 ]]; then
usage_digest;
exit 0;
fi
if [[ $1 == "fa2cmap" ]] && [[ $# -gt 1 ]]; then
shift
dig_options=':hf:o:e:s:z:l:q:';
while getopts "$dig_options" opt; do
case "$opt" in
h) usage_digest; exit 0;;
f) fa=${OPTARG};;
o) outDir=${OPTARG};;
e) enz_n=${OPTARG};;
q) enz_s=${OPTARG};;
z) minLen=${OPTARG};;
l) minEnzy=${OPTARG};;
s) scriptsDir=${OPTARG};;
\?) echo -e "\nOops! Unknown option -$OPTARG">&2; echo -e "Please check '`basename $0` fa2cmap' and continue...\n"; exit 1;;
:) echo -e "\nMissing option argument for -$OPTARG\n">&2; exit 1;;
esac
done
shift "$((OPTIND - 1))"
#check mandatory options
check_option "$fa" -f usage_digest; check_option "$outDir" -o usage_digest; check_option "$scriptsDir" -s usage_digest;
#check enzyme
if [[ -z "$enz_n" ]] && [[ -z "$enz_s" ]]; then
echo -e "\nPlease select one enzyme\n";
exit 1;
fi
if [[ -n "$enz_n" ]] && [[ -n "$enz_s" ]];then
echo -e "\nPlease select either enzyme name or enzyme sequence. Do not use both.\n"; exit 1;
fi
#check the fasta
fa=`readlink -f "$fa"`;
check_file "$fa";
#check outdir
last_chr="${outDir: -1}"
if [[ "$last_chr" == "/" ]]; then
outDir="${outDir%?}"
fi
if [[ "$outDir" == "." ]];then
outDir="$PWD"
elif [[ ${outDir: -2} == ".." ]];then
outDir="$PWD/$outDir"
fi
check_path "$outDir"
#check scripts
last_chr_s="${scriptsDir: -1}"
if [[ "$last_chr_s" == "/" ]]; then
scriptsDir="${scriptsDir%?}"
fi
if [[ "$scriptsDir" == "." ]];then
scriptsDir="$PWD"
elif [[ "${scriptsDir: -2}" == ".." ]];then
scriptsDir="$PWD/$scriptsDir"
fi
check_path "$scriptsDir"
#check minlen
if [[ -z "$minLen" ]]; then
minLen=20
fi
check_integer $minLen;
#check mindensity
if [[ -z "$minEnzy" ]];then
minEnzy=5
fi
check_integer $minEnzy
echo "========================================== fa2cmap starts ============================================="
echo "## Start date: `date`"
echo "## The fasta format file is: ${fa}"
echo "## The output directory is: ${outDir}"
if [[ -z "$enz_s" ]]; then
echo "## The selected enzyme name is: ${enz_n}"
fi
if [[ -z "$enz_n" ]]; then
echo "## The selected enzyme sequence is: ${enz_s}"
fi
echo "## The min molecule length (Kb) to filter is: ${minLen}"
echo "## The min number of enzyme in a molecule to filter is: ${minEnzy}"
echo "## The path to Bionano Solve folder is: ${scriptsDir}"
echo
name=`basename "$fa" | rev | cut -d"." -f2- | rev`
digestScript="$scriptsDir"/HybridScaffold/1.0/scripts/fa2cmap_multi_color.pl
cmap_stat="$scriptsDir"/HybridScaffold/1.0/scripts/calc_cmap_stats.pl
if [[ ! -s "$digestScript" ]] || [[ ! -s "$cmap_stat" ]]; then
echo "Please use the original Bionano Solve from Bionano Genomics"
echo -e "You may download it from https://bionanogenomics.com/support/software-downloads \n"
echo "## End date: `date`"
echo "======================================================================================================="
exit 1;
fi
if [[ -z "$enz_n" ]] && [[ -n "$enz_s" ]]; then
if [[ `echo "$enz_s" | grep -qi '^GCTCTTC$' && echo "yes" || echo "no"` == "yes" ]]; then
enz_n=BSPQI
elif [[ `echo "$enz_s" | grep -qi '^CCTCAGC$' && echo "yes" || echo "no"` == "yes" ]];then
enz_n=BBVCI
elif [[ `echo "$enz_s" | grep -qi '^GAATGC$' && echo "yes" || echo "no"` == "yes" ]];then
enz_n=BSMI
elif [[ `echo "$enz_s" | grep -qi '^GCAATG$' && echo "yes" || echo "no"` == "yes" ]];then
enz_n=BSRDI
elif [[ `echo "$enz_s" | grep -qi '^ATCGAT$' && echo "yes" || echo "no"` == "yes" ]];then
enz_n=BSECI
elif [[ `echo "$enz_s" | grep -qi '^CACGAG$' && echo "yes" || echo "no"` == "yes" ]];then
enz_n=BSSSI
elif [[ `echo "$enz_s" | grep -qi '^CTTAAG$' && echo "yes" || echo "no"` == "yes" ]];then
enz_n=DLE1
else
echo -e "Oops! We cannot recognise the enzyme sequence. Please check or contact us for a solution\n"
echo "## End date: `date`"
echo "======================================================================================================="
exit 1;
fi
perl $digestScript -i "$fa" -o "$outDir" -e "$enz_s" 1 -m "$minEnzy" -M "$minLen"
fi
if [[ -n "$enz_n" ]] && [[ -z "$enz_s" ]]; then
if [[ `echo "$enz_n" | grep -qi '^BspQI$' && echo "yes" || echo "no"` == "yes" ]]; then
enz_n=BSPQI
elif [[ `echo "$enz_n" | grep -qi '^BbvCI$' && echo "yes" || echo "no"` == "yes" ]]; then
enz_n=BBVCI
elif [[ `echo "$enz_n" | grep -qi '^BsmI$' && echo "yes" || echo "no"` == "yes" ]]; then
enz_n=BSMI
elif [[ `echo "$enz_n" | grep -qi '^BsrDI$' && echo "yes" || echo "no"` == "yes" ]]; then
enz_n=BSRDI
elif [[ `echo "$enz_n" | grep -qi '^bseCI$' && echo "yes" || echo "no"` == "yes" ]]; then
enz_n=BSECI
elif [[ `echo "$enz_n" | grep -qi '^BssSI$' && echo "yes" || echo "no"` == "yes" ]]; then
enz_n=BSSSI
elif [[ `echo "$enz_n" | grep -qi '^DLE1$' && echo "yes" || echo "no"` == "yes" ]]; then
enz_n=DLE1
else
echo -e "Oops! We cannot recognise the enzyme name. Please check or contact us for a solution\n"
echo "## End date: `date`"
echo "======================================================================================================="
exit 1;
fi
perl $digestScript -i "$fa" -o "$outDir" -e "$enz_n" 1 -m "$minEnzy" -M "$minLen"
fi
echo "Stats of the generated cmap file:"
perl $cmap_stat ${outDir}/${name}_${enz_n}_${minLen}kb_${minEnzy}labels.cmap
density=`grep -v "^#" ${outDir}/${name}_${enz_n}_${minLen}kb_${minEnzy}labels.cmap | cut -f1-3 | uniq | awk '{sum+=$2;SUM+=$3}END{print SUM/sum*100000}'`
echo "Label density (/100Kb) = `printf "%.3f" $density`"
echo
echo "## End date: `date`"
echo "======================================================================================================="
fi
##==================== Select "cmapstats" flag =====================
##Print help if only select cmapstats
if [[ $1 == "cmapstats" ]] && [[ $# -eq 1 ]]; then
usage_cmapstats;
exit 0;
fi
##Select "cmapstats" flag
if [[ $1 == "cmapstats" ]] && [[ $# -gt 1 ]]; then
shift
c_options=':hc:s:';
while getopts "$c_options" opt; do
case "$opt" in
h) usage_cmapstats; exit 0;;
c) cmap=${OPTARG};;
s) scriptsDir=${OPTARG};;
\?) echo -e "\nOops! Unknown option -$OPTARG">&2; echo -e "Please check '`basename $0` cmapstats' and continue...\n"; exit 1;;
:) echo -e "\nMissing option argument for -$OPTARG\n">&2; exit 1;;
esac
done
shift "$((OPTIND - 1))"
#check mandatory options
check_option "$cmap" -c usage_cmapstats; check_option "$scriptsDir" -s usage_cmapstats;
#check cmap
cmap=`readlink -f "$cmap"`;
check_file "$cmap"
last_chr_s="${scriptsDir: -1}"
if [[ "$last_chr_s" == "/" ]]; then
scriptsDir="${scriptsDir%?}"
fi
if [[ "$scriptsDir" == "." ]];then
scriptsDir="$PWD"
elif [[ "${scriptsDir: -2}" == ".." ]];then
scriptsDir="$PWD/$scriptsDir"
fi
if [[ ! -d "$scriptsDir" ]] || [[ ! -r "$scriptsDir" ]]; then
echo -e "\nOops! It seems that the Bionano Solve folder is not existent or readable. Please check!"
echo -e "You may also download it from https://bionanogenomics.com/support/software-downloads, and give a read permission.\n"
exit 1
fi
echo "========================================== cmapstats starts ============================================"
echo "## Start date: `date`"
echo "## The cmap file is: ${cmap}"
echo "## The path to Bionano Solve folder is: ${scriptsDir}"
echo
cmap_stat="$scriptsDir"/HybridScaffold/1.0/scripts/calc_cmap_stats.pl
if [[ ! -s "$cmap_stat" ]];then
echo "Please use the original Bionano Solve folder from Bionano Genomics"
echo "You may download it from https://bionanogenomics.com/support/software-downloads"
echo
echo "## End date: `date`"
echo "======================================================================================================="
exit
fi
echo "Stats of the cmap file:"
perl "$cmap_stat" "$cmap"
density=`grep -v "^#" "$cmap" | cut -f1-3 | uniq | awk '{sum+=$2;SUM+=$3}END{print SUM/sum*100000}'`
echo "Label density (/100Kb) = `printf "%.3f" $density`"
echo
echo "## End date: `date`"
echo "======================================================================================================="
fi
##====================== Select "bnxmerge" flag ========================
##Print help if only select bnxmerge
if [[ $1 == "bnxmerge" ]] && [[ $# -eq 1 ]]; then
usage_merge;
exit 0;
fi
##Select bnxmerge flag
if [[ $1 == "bnxmerge" ]] && [[ $# -gt 1 ]]; then
shift
mg_options=':hl:t:m:p:r:o:';
while getopts "$mg_options" opt; do
case "$opt" in
h) usage_merge; exit 0;;
l) list=${OPTARG};;
t) threads=${OPTARG};;
m) mem=${OPTARG};;
p) name=${OPTARG};;
r) refA=${OPTARG};;
o) outDir=${OPTARG};;
\?) echo -e "\nOops! Unknown option -$OPTARG">&2; echo -e "Please check '`basename $0` bnxmerge' and continue...\n"; exit 0;;
:) echo -e "\nMissing option argument for -$OPTARG\n">&2; exit 1;;
esac
done
shift "$((OPTIND - 1))"
#check mandatory options
check_option "$list" -l usage_merge; check_option "$threads" -t usage_merge; check_option "$mem" -m usage_merge; check_option "$name" -p usage_merge;
check_option "$refA" -r usage_merge; check_option "$outDir" -o usage_merge;
#check list
list=`readlink -f "$list"`
check_file "$list"
#check outDir
last_chr="${outDir: -1}"
if [[ "$last_chr" == "/" ]]; then
outDir="${outDir%?}"
fi
if [[ "$outDir" == "." ]];then
outDir="$PWD"
elif [[ ${outDir: -2} == ".." ]];then
outDir="$PWD/$outDir"
fi
check_path "$outDir"
#check threads and mem
check_integer $threads; check_integer $mem
echo "========================================== bnxmerge starts ============================================="
echo "## Start date: `date`"
echo "## The list file is: ${list}"
echo "## number of threads/CPUs is: ${threads}"
echo "## RAMs (Gb) are: ${mem}"
echo "## output bnx file name is: ${name}"
echo "## output directory is: ${outDir}"
echo
refA=`readlink -f "$refA"`
if [[ -s "$refA" ]] && [[ -x "$refA" ]]; then
"$refA" -f -if "$list" -merge -sort-idinc -bnx -maxthreads "$threads" -maxmem "$mem" -o "${outDir}/${name}" -XmapStatWrite "${outDir}"/merged_stats.txt -stdout -stderr
else
echo "Please check the availability or executability of your RefAligner!"
echo "You may download it from https://bionanogenomics.com/support/software-downloads, and give an executable permission."
fi
echo
echo "## End date: `date`"
echo "======================================================================================================="
fi
##==================== Select "bnxstats flag =========================
##Print help if only select bnxstats
if [[ $1 == "bnxstats" ]] && [[ $# -eq 1 ]]; then
usage_bstats;
exit 0
fi
##Select bnxstats flag
if [[ $1 == "bnxstats" ]] && [[ $# -gt 1 ]]; then
shift
bs_options=':hb:p:o:';
while getopts "$bs_options" opt; do
case "$opt" in
h) usage_bstats; exit 0;;
b) bnx=${OPTARG};;
p) name=${OPTARG};;
o) outDir=${OPTARG};;
\?) echo -e "\nOops! Unknown option -$OPTARG">&2; echo -e "Please check '`basename $0` bnxstats' and continue...\n"; exit 0;;
:) echo -e "\nMissing option argument for -$OPTARG\n">&2; exit 1;;
esac
done
shift "$((OPTIND - 1))"
#check mandatory options
check_option "$bnx" -b usage_bstats; check_option "$name" -p usage_bstats; check_option "$outDir" -o usage_bstats;
bnx=`readlink -f "$bnx"`;
check_file "$bnx";
check=`grep -c "# BNX File Version:" "$bnx"`
if [[ "$check" -eq 0 ]]; then
echo -e "\nOops! It seems the input file is not a bnx file. Please check!\n"
exit 1
fi
#check outDir
last_chr="${outDir: -1}"
if [[ "$last_chr" == "/" ]]; then
outDir="${outDir%?}"
fi
if [[ "$outDir" == "." ]];then
outDir="$PWD"
elif [[ ${outDir: -2} == ".." ]];then
outDir="$PWD/$outDir"
fi
check_path "$outDir";
for i in `grep -v "^#" "$bnx" | awk '{if(NR%4==1) print $1}'`; do
if [[ $i != 0 ]]; then
echo -e "\nOops! It seems the bnx file: '${bnx}' is not complete. Please check!\n"
exit 1
fi
done
rm -rf "$outDir"/"$name".txt
printf "Length""\t""AvgIntensity""\t""SNR""\t""NumberofLabels""\n" >> "$outDir"/"$name".txt
grep -v "^#" "$bnx" | awk 'BEGIN{OFS="\t"}{if(NR%4==1) print $3,$4,$5,$6}' >> "$outDir"/"$name".txt
n_mol=`grep -v "^#" "$bnx" | awk '{if (NR%4==1) print $0}' | wc -l`
cL=`grep -v "^#" "$bnx" | awk '{if (NR%4==1) sum+=$3}END{print sum/1000000}'`
avgL=`grep -v "^#" "$bnx" | awk -v n=$n_mol '{if (NR%4==1) sum+=$3}END{print sum/n/1000}'`
minL=`grep -v "^#" "$bnx" | awk '{if (NR%4==1) print $3}' | sort -n | head -n 1`
maxL=`grep -v "^#" "$bnx" | awk '{if (NR%4==1) print $3}' | sort -rn | head -n 1`
half=`grep -v "^#" "$bnx" | awk '{if (NR%4==1) sum+=$3}END{print sum/2}'`
n=`grep -v "^#" "$bnx" | awk '{if (NR%4==1) print $3}' | sort -n | awk -v hv=$half '{sum+=$1} {if(sum<=hv) print NR}' | tail -1`
if [[ `awk "BEGIN{print $n_mol%2}"` == 0 ]]; then
s1=`grep -v "^#" "$bnx" | awk '{if (NR%4==1) print $3}' | sort -n | awk -v n=$n '{if(NR==n) print $1}'`
s2=`grep -v "^#" "$bnx" | awk '{if (NR%4==1) print $3}' | sort -n | awk -v n=$n '{if(NR==n+1) print $1}'`
n50=`awk "BEGIN{print ($s1+$s2)/2}"`
else
n50=`grep -v "^#" "$bnx" | awk '{if (NR%4==1) print $3}' | sort -n | awk -v n=$n '{if(NR==n+1) print $1}'`
fi
stdL=`grep -v "^#" "$bnx" | awk '{if (NR%4==1) print $3}' | awk '{x[NR]=$1; s+=$1; n++} END {a=s/n; for (i in x) {ss+=(x[i]-a)^2} sd=sqrt(ss/n); print sd}'`
den=`grep -v "^#" "$bnx" | awk '{if (NR%4==1) sum+=$6}END{print sum}'`
minSNR=`grep -v "^#" "$bnx" | awk '{if (NR%4==1) print $5}' | sort -n | head -n 1`
maxSNR=`grep -v "^#" "$bnx" | awk '{if (NR%4==1) print $5}' | sort -rn | head -n 1`
avgSNR=`grep -v "^#" "$bnx" | awk '{if (NR%4==1) print $5}' | awk '{sum+=$1; n++} END {print sum/n}'`
minten=`grep -v "^#" "$bnx" | awk '{if (NR%4==1) print $4}' | sort -n | head -n 1`
maxten=`grep -v "^#" "$bnx" | awk '{if (NR%4==1) print $4}' | sort -rn | head -n 1`
avgten=`grep -v "^#" "$bnx" | awk '{if (NR%4==1) print $4}' | awk '{sum+=$1; n++} END {print sum/n}'`
echo "========================================== bnxstats starts ============================================="
echo "## Start date: `date`"
echo "## The checked bnx file is: ${bnx}"
echo "## Name for the saved table: ${name}.txt"
echo "## Output directory is: ${outDir}"
echo
echo "Stats of molecules in the input bnx file:"
echo "Number of molecules is: ${n_mol}"
echo "Total length of those molecules is (Mb): ${cL}"
echo "The length N50 is (Kb): `awk "BEGIN{ print ${n50}/1000}"`"
echo "Minimum length of those molecules is (Kb): `awk "BEGIN{ print $minL/1000}"`"
echo "Maximum length of those molecules is (Kb): `awk "BEGIN{ print $maxL/1000}"`"
echo "Average length of those molecules is (kb): ${avgL}"
echo "Length standard deviation is (Kb): `awk "BEGIN{ print $stdL/1000}"`"
echo "Total number of labels is: $den"
echo "Label density is (/100Kb): `awk "BEGIN{print $den/$cL/10}"`"
echo "Maximum signal noise ratio (SNR) is: ${maxSNR}"
echo "Minimum signal noise ratio (SNR) is: ${minSNR}"
echo "Average signal noise ratio (SNR) is: ${avgSNR}"
echo "Maximum avgIntensity is: ${maxten}"
echo "Minimum avgIntensity is: ${minten}"
echo "Average avgIntensity is: ${avgten}"
echo
echo "## End date: `date`"
echo "========================================================================================================="
fi
##===================== Select "bnxfilter" flag =======================
##Print help if only select bnxfilter
if [[ $1 == "bnxfilter" ]] && [[ $# -eq 1 ]]; then
usage_filter;
exit 0;
fi
##Select bnxfilter flag
if [[ $1 == "bnxfilter" ]] && [[ $# -gt 1 ]]; then
shift
ft_options=':hb:t:m:p:r:l:s:M:i:x:o:';
while getopts "$ft_options" opt; do
case "$opt" in
h) usage_filter; exit 0;;
b) bnx=${OPTARG};;
t) threads=${OPTARG};;
m) mem=${OPTARG};;
p) name=${OPTARG};;
r) refA=${OPTARG};;
l) minlen=${OPTARG};;
s) minsite=${OPTARG};;
M) maxsite=${OPTARG};;
i) maxinten=${OPTARG};;
x) pix=${OPTARG};;
o) outDir=${OPTARG};;
\?) echo;echo -e "Oops! Unknown option -$OPTARG">&2; echo -e "Please check '`basename $0` bnxfilter' and continue...\n"; exit 0;;
:) echo;echo -e "Missing option argument for -$OPTARG\n">&2; exit 1;;
esac
done
shift "$((OPTIND - 1))"
#check mandatory options
check_option "$bnx" -b usage_filter; check_option "$threads" -t usage_filter; check_option "$mem" -m usage_filter; check_option "$name" -p usage_filter;
check_option "$refA" -r usage_filter; check_option "$outDir" -o usage_filter;
bnx=`readlink -f "$bnx"`
check_file "$bnx";
refA=`readlink -f "$refA"`
if [[ ! -s "refA" ]] && [[ ! -x "$refA" ]]; then
echo -e "\nPlease check your RefAligner whether it is existent or executable"
echo -e "You may download it from https://bionanogenomics.com/support/software-downloads, and give an executable permission.\n"
exit 1
fi
#check outDir
last_chr="${outDir: -1}"
if [[ "$last_chr" == "/" ]]; then
outDir="${outDir%?}"
fi
if [[ "$outDir" == "." ]];then
outDir="$PWD"
elif [[ "${outDir: -2}" == ".." ]];then
outDir="$PWD/$outDir"
fi
check_path "$outDir"
if [[ -z "$minlen" ]]; then
minlen=100
fi
if [[ -z "$minsite" ]]; then
minsite=6
fi
if [[ -z "$maxsite" ]]; then
maxsite=200
fi
if [[ -z "$maxinten" ]]; then
maxinten=0.6
fi
if [[ -z "$pix" ]]; then
pix=0.001
fi
check_integer $threads; check_integer $mem; check_integer $minlen; check_integer $minsite;
echo "====================================== bnxfilter starts ======================================="
echo "## Start date: `date`"
echo "## The bnx file is: ${bnx}"
echo "## The RefAligner is: ${refA}"