-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathOrganelleRef_PBA
executable file
·2725 lines (1961 loc) · 74.7 KB
/
OrganelleRef_PBA
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/perl
=head1 NAME
OrganelleRef_PBA
Pipeline to assemble plastid genomes using PacBio
=cut
our $version = '1.0.8';
=head1 SYPNOSIS
OrganelleRef_PBA [-h] -i <input_pacbio> -r <fasta_reference> -o <output_dir>
[-t <input_type>] [-b <blasr_arguments>]
[-s <sprai_ec_options>] [-c <sprai_pbsm_options>]
[-x <fraction_for_scaffolding>] [-l <sspace_long_options>]
[-n <repeat_block_size>] [-m <coverage_ratio4repeats>]
[-z <breaks_overlap>] [-a <highest_ori_position>]
[-j <startend_margin_circ>] [-k <overlap_length_circ>]
[-q <min_blasr_align_perc>]
[-C] [-V] [-v]
=head2 I<Flags:>
=over
=item -i
B<input_pacbio> input PacBio subreads (mandatory)
=item -t
B<input_type> input type (fasta or fastq; default=fastq)
=item -r
B<fasta_reference> organelle reference genome, fasta format (mandatory)
=item -o
B<output_dir> output directory (mandatory)
=item -b
B<blasr_arguments> pass the blasr arguments as '-arg1=val1,-arg2=val2'
=item -s
B<sprai_ec_options> passing sprai ec options as 'opt1=val1,opt2=val2'
=item -c
B<sprai_pbasm_options> passing sprai pbasm options as 'opt1=val1,opt2=val2'
=item -x
B<complete_fraction> fraction that decide for a new scaffolding (def. 1.0)
=item -l
B<sspace_long_options> passing the SSPACE-Long options as 'opt1=val1...'
=item -n
B<repeat_block_size> size of the block to detect repeats by cov (def. 1000)
=item -m
B<coverage_ratio4repeats> coverage ratio to define repetitive region (def. 1.6)
=item -z
B<breaks_overlap> overlap produced during repeats breaks (default 0)
=item -a
B<highest_oripos> highest position to set up an origin (default 1)
=item -j
B<start_end_margin_circ> start-end margin to check circularity (default 10)
=item -k
B<overlap_length_circ> overlap length to check circularity (default 100)
=item -q
B<min_blasr_align_perc> minimum alignment percentage for BlasR (default 5)
=item -C
B<check_completeness> check completeness after the circularation check
=item -V
B<be_verbose> be verbose and print the parsing status.
=item -v
B<version> print version
=item -h
B<help> print the help
=back
=cut
=head1 DESCRIPTION
This program assembles the PacBio reads into a organelle fasta sequence.
Fasta (-t fasta) and Fastq (-t fastq, default) input can be used.
It works in several steps:
1- First mapping round of the PacBio reads with the organelle reference.
2- First assembly round of the mapped reads using Sprai.
+ Evaluate the assembly, if longest contig size >= reference size
it will move to resolve the repeat (4). Otherwise, it will try to
improve the assembly using SSPACE-Long and the whole dataset (3).
+ The completeness will be controled by -x <completeness_fraction>
By default 1.0 is used.
3- Rescaffolding of the contigs produced by Sprai with SSPACE-Long.
4- Check circularity and origin, if both are positive it will evaluate
completeness (-C), and finish the script if it is complete (>98% coverage)
5- First mapping round with the assembly to find repetitive regions.
6- Repetitive region break based in the coverage and a self blast
7- Second assembly round with the repetitive region.
This program uses three different external programs:
a- BlasR (for mapping).
b- samtools (to manipulate sam).
c- Blast, Sprai and WGS-Assembler (for assembly).
d- SSPACE-Long for rescaffolding.
e- Bedtools to calculate coverage
Optionally it can use seqtk to speed up the Fastq to Fasta conversion
and the Fastq read selection.
=cut
=head1 AUTHORS
Aureliano Bombarely Gomez.
=cut
=head1 METHODS
OrganelleRef_PBA
=cut
use strict;
use warnings;
use autodie;
use Getopt::Std;
use File::Spec;
use File::Basename;
use File::Copy;
use File::Path qw(make_path remove_tree);
use IPC::Cmd qw[can_run run];
use Bio::SeqIO;
use Bio::Tools::Run::StandAloneBlastPlus;
use Math::BigFloat;
our ($opt_i, $opt_t, $opt_o, $opt_r, $opt_b, $opt_s, $opt_c, $opt_x, $opt_l,
$opt_n, $opt_m, $opt_z, $opt_a, $opt_j, $opt_k, $opt_q, $opt_C, $opt_V,
$opt_v, $opt_h);
getopts("i:t:o:r:b:s:c:x:l:n:m:z:a:j:k:q:CVvh");
if (!$opt_i && !$opt_t && !$opt_o && !$opt_r && !$opt_b && !$opt_s && !$opt_x
&& !$opt_l && !$opt_c && !$opt_n && !$opt_m && !$opt_z && !$opt_a &&
!$opt_j && !$opt_k && !$opt_q && !$opt_C && !$opt_V && !$opt_v && !$opt_h) {
print "There are n\'t any tags. Print help\n\n";
help();
}
if ($opt_h) {
help();
}
elsif ($opt_v) {
print STDERR "\nOrganelleRef_PBA version:$version\n";
print STDERR "Developed at the Bombarely Laboratory (2015-10-01)\n\n";
exit(1);
}
my $date = `date`;
chomp($date);
print STDERR "\n\n############################################################";
print STDERR "\n## OrganelleRef_PBA Starts ($date) ##\n";
print STDERR "############################################################\n\n";
## Define a variable to keep the stats;
my %gstats = ();
print_header("0) Checking arguments");
##############################################################################
## CHECK ARGUMENTS
##############################################################################
## Get the arguments and check them
my $pbin = $opt_i ||
die("\nINPUT ARG. ERROR: -i <pacbio_input> argument was not supplied.\n\n");
print STDERR "\tInput PacBio reads file: $pbin\n";
## The script should be able to use Fasta and Fastq
## If Fasta is used, we'll need to create a cheat Fastq file for Sprai
## If Fastq is used, we'll need to get the Fasta for BlasR
## By default it will use the Fastq option, but it should be able also
## to use Fasta. This will be controled by -t <input_type> default 'fastq'
my $intype = 'fastq';
my %perm_intypes = (
'fastq' => 1,
'fasta' => 1
);
if ($opt_t) {
unless (exists $perm_intypes{$opt_t}) {
die("\nERROR: -t <input_type> permited values are fasta or fasta.\n");
}
else {
$intype = $opt_t;
}
}
print STDERR "\tInput type: $intype\n";
unless (-s $pbin) {
die("\nERROR: Input PacBio reads does not exist.\n\n");
}
my $outdir = $opt_o ||
die("\nINPUT ARG. ERROR: -o <output_dir> argument was not supplied.\n\n");
print STDERR "\tOutput directory: $outdir\n";
unless (-d $outdir) {
die("\nERROR: Out directory does not exist. Please create it.\n\n");
}
my $orgref = $opt_r ||
die("\nINPUT ARG. ERROR: -r <fasta_ref> argument was not supplied.\n\n");
print STDERR "\tOrganelle reference fasta file: $orgref\n";
my $refsize;
unless (-s $orgref) {
die("ERROR: Organelle reference fasta files does not exist.\n\n");
}
else {
## It will get the size and use for est_size
## Also, produce a warning if more than one sequence is in the ref file
my $refc = 0;
my $ref_seqio = Bio::SeqIO->new( -file => $orgref, -type => 'fasta' );
while( my $refseq = $ref_seqio->next_seq() ) {
my $refid = $refseq->id();
my $ref_l = $refseq->length();
$refsize = $ref_l;
$refc++;
}
if ($refc > 1) {
warn("\nWARNING: Reference file has more than one sequence\n");
}
print STDERR "\t\tOrganelle reference size: $refsize bp\n";
$gstats{'00_reference_size'} = $refsize;
}
my $repeat_blocksize = 1000;
if ($opt_n) {
if ($opt_n !~ m/^\d+$/) {
die("ERROR: -n <repeatblock_size> is a non-numeric argument.\n");
}
else {
$repeat_blocksize = $opt_n;
}
}
print STDERR "\t\tRepeat block size coverage detection: $repeat_blocksize\n";
my $hlratio = 1.6;
if ($opt_m) {
if ($opt_m !~ m/^d+\.?\d*$/) {
die("ERROR: -m <coverage_ratio_for_repeats> is not number.\n");
}
else {
$hlratio = $opt_m;
}
}
print STDERR "\t\tCoverage ratio to define repeatitive region: $hlratio\n";
## Check completeness fraction
my $asmfrc = "1.0";
if ($opt_x) {
unless ($opt_x =~ m/\d+\.?\d?/) {
die("ERROR: -x <assembly_completeness_fraction> isnt numeric.\n");
}
else {
$asmfrc = $opt_x;
}
}
print STDERR "\tCompleteness fraction: $asmfrc\n";
my $break_ovl = 0;
if ($opt_z) {
unless ($opt_z =~ m/\d+\.?\d?/) {
die("ERROR: -z <breaks_overlap> isnt numeric.\n");
}
else {
$break_ovl = $opt_z;
}
}
print STDERR "\tBreaks overlap: $break_ovl\n";
my $orimargin = 1;
if ($opt_a) {
unless ($opt_a =~ m/^\d+$/) {
die("ERROR: -a <highest_origin_position> isnt numeric.\n");
}
else {
$orimargin = $opt_a;
}
}
print STDERR "\tHighest position to set up origin: $orimargin\n";
my $mar = 10;
if ($opt_j) {
unless ($opt_j =~ m/^\d+$/) {
die("ERROR: -j <start_end_margins_circularity_check> isnt numeric.\n");
}
else {
$mar = $opt_j;
}
}
print STDERR "\tStarting-End margin to check circularity: $mar\n";
my $ovl = 100;
if ($opt_k) {
unless ($opt_k =~ m/^\d+$/) {
die("ERROR: -a <overlap_length_circularity_check> isnt numeric.\n");
}
else {
$ovl = $opt_k;
}
}
print STDERR "\tOverlap length to check circularity: $ovl\n";
## Min BlasR alignment percentage
my $mbap = 5;
if ($opt_q) {
unless ($opt_q =~ m/^\d+$/) {
die("ERROR: -q <min_blasr_align_perc> isnt numeric.\n");
}
else {
$mbap = $opt_q;
}
}
print STDERR "\tMinimum BlasR alignment percentage: $mbap\n";
## Process the passing options
## It will process the BlasR options below
if ($opt_b) {
print STDERR "\tPassing BlasR args:\n\t\t$opt_b\n";
}
my %sprai_ec = ();
if ($opt_s) {
print STDERR "\tPassing Sprai ec options:\n\t\t$opt_s\n";
## Options is a -s and then add extra options
my @ec_list = split(/,/, $opt_s);
foreach my $ec (@ec_list) {
if ($ec =~ m/^(.+)=(.+)$/) {
$sprai_ec{$1} = $2;
}
}
}
my %sprai_pbasm = ();
if ($opt_c) {
print STDERR "\tPassing Sprai (CA) pbasm options:\n\t\t$opt_c\n";
my @pbasm_list = split(/,/, $opt_c);
foreach my $pbasm (@pbasm_list) {
if ($pbasm =~ m/^(.+)=(.+)$/) {
$sprai_pbasm{$1} = $2;
}
}
}
if ($opt_l) {
print STDERR "\tPassing SSPACE-Long options:\n\t\t$opt_l\n";
}
print STDERR "\n";
## Check the executables
my %exec_list = (
BLASR_PATH => ['blasr'],
SAMTOOLS_PATH => ['samtools'],
SPRAI_PATH => ['ezez_vx1.pl', 'ezez4qsub_vx1.pl', 'bfmt72s',
'm52bfmt7', 'myrealigner', 'nss2v_v3', 'fa2fq.pl',
'check_circularity.pl', 'dfq2fq_v2.pl'],
BLAST_PATH => ['blastn', 'makeblastdb'],
CA_PATH => ['runCA', 'pacBioToCA', 'PBcR'],
SSPACELONG_PATH => ['SSPACE-LongRead.pl'],
BEDTOOLS_PATH => ['bedtools']
);
## Note: When you install SSPACE-LongRead.pl has the wrong interpreter
## it is used with perl.
my %exepath = ();
foreach my $exec_key (sort keys %exec_list) {
my @execitems = @{$exec_list{$exec_key}};
foreach my $e_item (@execitems) {
if (exists $ENV{$exec_key}) {
print STDERR "\t$exec_key PATH defined for $e_item executable\n";
$exepath{$e_item} = File::Spec->catfile($ENV{$exec_key}, $e_item);
}
else {
my $exe_path = can_run($e_item);
if ($exe_path) {
$exepath{$e_item} = $exe_path;
print STDERR "\t$e_item is available in the PATH ($exe_path)\n";
}
else {
print STDERR "\nERROR: $e_item executable ($exec_key package)";
die(" is not accessible.\n");
}
}
}
}
## Check that bedtools version is v2.20.0 or superior
my $checkbedv_cmd = "bedtools --version";
my @checkbedv01run = run( command => $checkbedv_cmd, verbose => $opt_V );
my $bedversion = $checkbedv01run[3]->[0];
$bedversion =~ s/bedtools v//;
my @bedversion = split(/\./, $bedversion);
my $lowerbedversion = 0;
if ($bedversion[0] < 2) {
$lowerbedversion = 1;
}
elsif ($bedversion[1] < 20) {
$lowerbedversion = 1;
}
if ($lowerbedversion == 1) {
die("\nERROR: Please use Bedtools version 2.20.0 or above.\n");
}
## Get the permited options
my $blasr_argsline = $opt_b;
my %blasr_args = ();
## There are some options that should not be used because it will
## produce problems downstream to process the output
my %blasr_ban = (
'--sam' => 1,
'--m' => 1,
'--header' => 1
);
if ($blasr_argsline) {
my $blasr_h = "$exepath{blasr} -h ";
$blasr_h .= "| sed -r 's/^\\s+//' | grep '^-' | sed -r 's/\\s+.+//'";
my @blasr_h_run = run( command => $blasr_h );
my %perm_blasr = ();
foreach my $blasr_arg (split(/\n/, $blasr_h_run[3]->[0])) {
$perm_blasr{$blasr_arg} = 1;
}
foreach my $b_argline (split(/,/, $blasr_argsline)) {
if ($b_argline =~ m/^(-+\w+)=?(\w*)$/) {
my $b_arg = $1;
my $b_val = $2;
unless (exists $perm_blasr{$b_arg}) {
die("\nBLASR OPTION ERROR: $b_arg is not BlasR option.\n");
}
else {
if (exists $blasr_ban{$b_arg}) {
warn("\nWARNING: BlasR argument $b_arg can not be used.\n");
}
else {
$blasr_args{$b_arg} = $b_val;
}
}
}
}
}
## Get the permitted options for $opt_l
my $sspace_argsline = $opt_l;
my %sspace_args = ();
## There are some options that should not be used because it will
## produce problems downstream to process the output
my %sspace_ban = (
'-c' => 1,
'-p' => 1,
'-b' => 1,
'-s' => 1
);
if ($sspace_argsline) {
my $sspace_h = "perl $exepath{'SSPACE-LongRead.pl'} 2>&1 ";
## Printed as STDERR
$sspace_h .= "| grep '^-' | sed -r 's/\\s+.+//'";
my @sspace_h_run = run( command => $sspace_h );
my %perm_sspace = ();
foreach my $sspace_arg (split(/\n/, $sspace_h_run[3]->[0])) {
$perm_sspace{$sspace_arg} = 1;
}
foreach my $l_argline (split(/,/, $sspace_argsline)) {
if ($l_argline =~ m/^(-\w+)=?(\w*)$/) {
my $l_arg = $1;
my $l_val = $2;
unless (exists $perm_sspace{$l_arg}) {
die("\nSSPACE OPTION ERROR: $l_arg isnt SSPACE-Long option.\n");
}
else {
if (exists $sspace_ban{$l_arg}) {
warn("\nWARNING: SSPACE arg. $l_arg can not be used.\n");
}
else {
$sspace_args{$l_arg} = $l_val;
}
}
}
}
}
##############################################################################
## Reduce the complexity by mapping
##############################################################################
## First run the mapping
print_header("1) Running BlasR");
print STDERR "\t1.1- Converting Fastq to Fasta.\n";
my $blasr_in;
my $seqcounter = 0;
## Check if seqtk is installed to run some steps faster
my $fastool_path = can_run('seqtk');
if ($intype eq 'fastq') {
$blasr_in = File::Spec->catfile($outdir, "01_1_BlasR_in.fasta");
if ($fastool_path) {
print STDERR "\t Using Seqtk to convert Fastq to Fasta.\n";
my $fastool_cmd = "$fastool_path seq -A $pbin > $blasr_in";
my @fastool01run = run( command => $fastool_cmd, verbose => $opt_V );
}
else {
print STDERR "\t Using Bioperl to convert Fastq to Fasta.\n";
print STDERR "\t (Note: it can take a while)\n";
my $seqio1_in = Bio::SeqIO->new(-file => $pbin, -format => 'fastq');
my $seqio1_out = Bio::SeqIO->new(
-file => ">$blasr_in",
-format => 'fasta'
);
while( my $seqobj1 = $seqio1_in->next_seq() ) {
$seqio1_out->write_seq($seqobj1);
$seqcounter++;
}
}
}
else {
$blasr_in = $pbin;
print STDERR "\t Input type is fasta. Skipping step.\n";
}
## It will still count the sequences
my $count_cmd = "grep -c '>' $blasr_in";
my @count01run = run( command => $count_cmd, verbose => $opt_V );
my $seqcount = $count01run[3]->[0];
chomp($seqcount);
my $seqsize_cmd = "grep -v '>' $blasr_in | wc -m";
my @seqsize01run = run( command => $seqsize_cmd, verbose => $opt_V );
my $seqsize = $seqsize01run[3]->[0];
chomp($seqsize);
## The PacBio ID can be:
## 4-ID/difit/digit_digit/digit_digit
## 3-ID/digit/digit_digit
## 2-ID/digit
## 1-ID
## It will analyze the blasr_in file to determine what kind of type it is
my $checkidtype_cmd = "grep '>' $blasr_in | head -n 1";
my @checkidtype01run = run( command => $checkidtype_cmd, verbose => $opt_V );
my $firstid = $checkidtype01run[3]->[0];
chomp($firstid);
my @firstid_split = split(/\//, $firstid);
my $seqidtype = scalar(@firstid_split);
$seqcounter = $seqcount;
## It will overwrite the out
my $blasr_out = File::Spec->catfile($outdir, "01_2_BlasR_out.txt");
if (exists $blasr_args{'--out'}) {
warn("WARNING: BlasR out will be rewrite to $blasr_out\n");
}
else {
$blasr_args{'--out'} = $blasr_out;
}
## Build the $cmd line
print STDERR "\t Input PacBio read file contains $seqcounter reads\n\n";
$gstats{'00_input_reads_count'} = $seqcounter;
$gstats{'00_input_reads_size'} = $seqsize;
my @blasr_cmd = ($exepath{'blasr'}, $blasr_in, $orgref);
foreach my $blasr_k (sort keys %blasr_args) {
push @blasr_cmd, $blasr_k;
if (defined $blasr_args{$blasr_k}) {
push @blasr_cmd, $blasr_args{$blasr_k};
}
}
print STDERR "\t1.2- Running BlasR mapping.\n\n";
my @blasr01run = run( command => \@blasr_cmd, verbose => $opt_V );
## BlasR has produced the output, so the program will get them
## One simplier option could be to get a sam file and convert it
## into a fasta (it will be faster because it won't need to re-read
## the file again), but Sprai uses fastq files, so it will parse the
## regular BlasR output and use it to get the sequences from the
## fastq file.
## Check that there is a BlasR out
unless (-s $blasr_out) {
my $msg = "No BlasR output was created.";
$msg .= "Please check the input and the running parameters.\n";
die($msg);
}
print STDERR "\t1.3- Processing the BlasR output.\n";
my $selids_file = File::Spec->catfile($outdir, "01_3_BlasR_hit_ids.txt");
my $l = 0;
my %selected_ids = ();
open(my $blasrout_fh, '<', $blasr_out);
open(my $blasrhit_fh, '>', $selids_file);
my $total_len = 0;
while(<$blasrout_fh>) {
chomp($_);
## BlasR output fields are:
## 0-qName 1-tName 2-qStrand 3-tStrand 4-score 5-percentSimilarity 6-tStart
## 7-tEnd 8-tLength 9-qStart 10-qEnd 11-qLength 12-nCells
my @line = split(/\s+/, $_);
## BlasR output add an extra /0_\d+ at the end of the line
## This extra output, may or may not be present in the PacBio fastq file
## It will modify this formating depending of the ID type detected
my @id = split(/\//, $line[0]);
## Now it will adjust the ID to the id type
my $idtype = scalar(@id);
while($idtype > $seqidtype) {
pop(@id);
$idtype = scalar(@id);
}
if ($idtype < $seqidtype) {
die("ERROR: BlasR SeqID is different from Fastq input file.\n");
}
my $len = $line[11];
## Filter by %alignment with the target (qEnd - qStart) * 100 / qLength
my $qbap = ($line[10] - $line[9]) * 100 / $line[11];
my $id = join("/", @id);
if ($qbap >= $mbap) {
$selected_ids{$id} = $len;
$l++;
$total_len += $len;
print $blasrhit_fh "$id\n";
}
}
print STDERR "\t $l reads ($total_len bp) will be selected\n";
$gstats{'01_mapped_reads'} = $l;
$gstats{'01_mapped_size'} = $total_len;
## It will also produce the estimated size
my $est_size = $sprai_ec{estimated_genome_size} || $refsize;
## Chloroplast by default
my $est_depth_obj = Math::BigFloat->new($total_len/$est_size);
my $est_depth = $est_depth_obj->bfround(0);
print STDERR "\t Estimated depth (reference based): $est_depth X.\n\n";
$gstats{'01_estimated_depth'} = $est_depth;
print STDERR "\t1.4- Selecting reads from the $intype file\n";
## For sequence selection:
## + seqtk doesn't care about input type
## + Bioperl will use type=$intype
my $sprai_in = File::Spec->catfile($outdir, "02_0_Sprai_in.fastq");
if ($intype eq 'fasta') {
$sprai_in = File::Spec->catfile($outdir, "02_0_Sprai_in.fasta");
}
if ($fastool_path) {
print STDERR "\t Using Seqtk to select reads from $intype.\n";
my $fastool_cmd = "$fastool_path subseq $pbin $selids_file > $sprai_in";
my @fastool01run = run( command => $fastool_cmd, verbose => $opt_V );
}
else {
print STDERR "\t Using Bioperl to select reads from $intype.\n";
print STDERR "\t (Note: it can take a while)\n";
my $seqio2_in = Bio::SeqIO->new(-file => $pbin, -format => $intype);
my $seqio2_out = Bio::SeqIO->new(-file => ">$sprai_in", -format => $intype);
while( my $seqobj2 = $seqio2_in->next_seq() ) {
my $seqid = $seqobj2->id();
my $seqlen = $seqobj2->length();
if (exists $selected_ids{$seqid}) {
$seqio2_out->write_seq($seqobj2);
}
}
}
## Now at this point it will check the intype. Sprai needs fastq files
## so, if the infile is fasta it will need to check the quality
## for that it will use the sprai script fa2fq.pl from Sprai.
if ($intype eq 'fasta') {
print STDERR "\t1.5- Changing format from fasta to fastq for Sprai\n";
my $sprai_alt = File::Spec->catfile($outdir, "02_0_Sprai_in.fastq");
my $fa2fq_cmd = "$exepath{'fa2fq.pl'} $sprai_in > $sprai_alt";
my @fa2fq01run = run( command => $fa2fq_cmd, verbose => $opt_V );
## Overwrite the $sprai_in with the new name
$sprai_in = $sprai_alt;
}
##############################################################################
## Assembly
##############################################################################
print_header("2) Running assembly (Sprai)");
## Sprai will create a file with result_ date +%Y%m%d_%H%M%S
## to store the run.
my @date01run = run( command => "date +%Y%m%d_%H%M%S", verbose => $opt_V );
my $dateline = $date01run[3]->[0];
chomp($dateline);
my $sprai_outdir = "result_$dateline";
mkdir($sprai_outdir);
## It will need two files
## + pbasm.spec
## + ec.spec
## and then it will run the program as ezez_vx1.pl ec.spec pbasm.ec
print STDERR "\t2.1- Creating the ec.spec file for Sprai.\n";
## First, build the ec.spec file
my $sprai_ec = File::Spec->catfile($outdir, "02_1_Sprai_ec.spec");
## Overwrite the following options
$sprai_ec{estimated_depth} = $est_depth;
$sprai_ec{input_for_database} = $sprai_in;
$sprai_ec{estimated_genome_size} = $est_size * 2;
create_sprai_ec_file($sprai_ec, \%exepath, \%sprai_ec);
## Second, build the pbasm.spec file
print STDERR "\n\t2.2- Creating the pbasm.spec file for Sprai.\n";
my $sprai_pb = File::Spec->catfile($outdir, "02_1_Sprai_pbasm.spec");
create_ca_pbasm_file($sprai_pb, \%sprai_pbasm);
my $sprai_log = File::Spec->catfile($outdir, "02_1_Sprai_log.txt");
## Sprai uses some additional scripts that it may not be in the PATH
## It is recommended to change the PATH to let sprai as script
## use them. In theory it should be able to find these scripts using
## sprai_ec conf. file but this secript doesn't have the path integrated
## with its executation
if ($ENV{SPRAI_PATH}) {
$ENV{PATH} = "$ENV{PATH}:$ENV{SPRAI_PATH}";
}
## Build the command
print STDERR "\n\t2.3- Running Sprai.\n";
my $spraicmd = "$exepath{'ezez_vx1.pl'} $sprai_ec $sprai_pb -now $dateline ";
$spraicmd .= "> $sprai_log 2>&1";
my @sprai01run = run( command => $spraicmd, verbose => $opt_V );
## Once the program is done, it will move the outdir
my $sprai_outdir2 = File::Spec->catfile($outdir, "02_1_".$sprai_outdir);
move($sprai_outdir, $sprai_outdir2);
print STDERR "\n\t2.3- Analyzing the results\n";
my $sprai_finaldir = File::Spec->catfile($sprai_outdir2, "CA", "9-terminator");
## Die if doesn't produce an output
unless (-d $sprai_finaldir) {
die("ERROR: Sprai/CA didnt produced an output 9-terminator. Check log.\n");
}
opendir(my $sprai_dh, $sprai_finaldir);
my $sprai_scf;
while( my $file = readdir($sprai_dh)) {
if ($file =~ m/.scf.fasta$/) {
$sprai_scf = $file;
}
}
my $sprai_scf_fp = File::Spec->catfile($sprai_finaldir, $sprai_scf);
print STDERR "\tScaffold output file: $sprai_scf_fp\n";
my $assembly01 = File::Spec->catfile($outdir,"02_3_organelle_assembly01.fasta");
copy($sprai_scf_fp, $assembly01);
## Get the stats
my @seqstats01 = get_seqstats($assembly01, $refsize);
print_seqstats("Assembly Step 1 Results", \@seqstats01);
$gstats{'02_seqtotal_count'} = $seqstats01[0];
$gstats{'02_seqtotal_length'} = $seqstats01[1];
$gstats{'02_seqtotal_gaplength'} = $seqstats01[6];
$gstats{'02_seqtotal_refratio'} = $seqstats01[4];
$gstats{'02_seqlongest_length'} = $seqstats01[2];
$gstats{'02_seqlongest_gaplength'} = $seqstats01[7];
$gstats{'02_seqlongest_refratio'} = $seqstats01[5];
$gstats{'02_seqtotal_refdif'} = $seqstats01[8];