-
Notifications
You must be signed in to change notification settings - Fork 0
/
gapFill.pl
1008 lines (883 loc) · 40.1 KB
/
gapFill.pl
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
# A wrapper script to gap-fill: stitch together genome maps that are sufficiently close to each other and (optionally) overlapping fragile sites as predicted from reference genome map.
# usage: perl gapFill.pl -x <input.xmap> -q <input_q.cmap> -r <input_r.cmap> -e <input.errbin> -o <output_prefix> [--bed <.bed fragile sites file>] [--round <start_round =1>] [--maxlab <max_label_gap_tolerence=0>] [--maxfill <max basepairs to fill between contigs = 35000>] [--wobble <fragile site wobble in bp = 0>] [--n <CPU cores to use>] [--alignmolAnalysis alignmolAnalysisOut.txt] [--minRatio <minRatio for single molecules =0.70>] [--pvalue alignment pvalue <1e-12>]
# Details:
# * Assumption: that contigs on XMAP is being read from left to right and is sorted by RefStartPos
# * Assumption: input _r.cmap has only one contig
# * If fragile sites BED file is provided then only contigs spanning fragile sites will be merged.
# * Designed to work with XMAP 0.1
use strict;
use warnings;
use IPC::System::Simple qw(system capture);
sub unique;
use Cwd;
use File::Find;
use Data::Dumper;
use File::Basename;
use File::Copy;
use Getopt::Long qw(:config bundling);
use Sys::Info;
use Sys::Info::Constants qw( :device_cpu );
use Sys::MemInfo qw(totalmem freemem totalswap);
use Fcntl qw(:flock SEEK_END);
print "\n";
print qx/ps -o args $$/;
print "\n";
## << usage statement and variable initialisation >>
my %inputs = ();
GetOptions( \%inputs, 'x|xmap=s', 'q|qcmap=s', 'r|rcmap=s', 'e|errbin=s', 'o|output|prefix=s', 'bed|b:s', 'round:i', 'maxlab:i', 'maxfill:i', 'wobble:i', 'n:i', 'alignmolAnalysis=s', 'minRatio=s', 'maxOverlap:i', 'maxOverlapLabels:i', 'pvalue:s');
if( !exists $inputs{x} | !exists $inputs{q} | !exists $inputs{r} | !exists $inputs{e} | !exists $inputs{o} ) {
print "Usage: perl gapFill.pl -x <input.xmap> -q <input_q.cmap> -r <input_r.cmap> -e <input.errbin> -o <output_prefix> [--bed <.bed fragile sites file>] [--round <start_round =1>] [--maxlab <max_label_gap_tolerence=0>] [--maxfill <max basepairs to fill between contigs = 35000>] [--wobble <fragile site wobble in bp = 0>] [--n <CPU cores to use>] [--alignmolAnalysis alignmolAnalysisOut.txt] [--minRatio <minRatio for single molecules =0.70>] [--pvalue alignment pvalue <1e-12>]\n";
exit 0;
}
my $scriptspath = Cwd::abs_path(dirname($0)); #gapFill.pl relies on two additional scripts (extractContigs.pl and mergeContigs.pl) and assumes thay are available from the same directory as gapFill.pl
## Default values
if( !exists $inputs{round} ) { $inputs{round} = 1; }
my $maxBp = 30000;
if( exists $inputs{maxfill} ) { $maxBp = $inputs{maxfill}; }
my $wobble = $maxBp;
if( exists $inputs{wobble} ) { $wobble = $inputs{wobble}; }
my $maxlab = 1;
if( exists $inputs{maxlab} ) { $maxlab = $inputs{maxlab}; }
if( !exists $inputs{minRatio} || $inputs{minRatio}>1 || $inputs{minRatio}<0 ) { $inputs{minRatio} = 0.70; }
if( !exists $inputs{maxOverlap} || $inputs{maxOverlap}<0 ) { $inputs{maxOverlap} = 0; }
if( !exists $inputs{maxOverlapLabels} || $inputs{maxOverlapLabels}<0 ) { $inputs{maxOverlapLabels} = 5; }
if( !exists $inputs{pvalue} ) { $inputs{pvalue} = "1e-12"; }
open XMAP, $inputs{x} or die "ERROR: $!\n";
open QCMAP, $inputs{q} or die "ERROR: $!\n";
open RCMAP, $inputs{r} or die "ERROR: $!\n";
my $outName = $inputs{q};
$outName =~ s/_q.cmap/_merged_contigs_q.cmap/g;
my $outName2 = $inputs{q};
$outName2 =~ s/_q.cmap/_temp/g;
my $outName3 = $inputs{o};
my @xmap;
my @q_cmap;
my @r_cmap;
my @bed;
my $alignments=0; #number of alignments
my @q_mapIds;
my @r_mapIds; #RefContigID should always be the same
my $q_sites=0; #total number of sites across all query maps in _q.cmap
my $r_sites=0; #total sites on single contig of _r.cmap
my $mergeCount=0;
my @firstContigList = ();
my @secondContigList= ();
#CMapId Start End Type Score Strand ThickStart ThickEnd ItemRgba Sequence
my @fsiteTypeList = ();
my @scoreList = ();
my @strandList = ();
my @thickStartList = ();
my @thickEndList = ();
my @itemRgbaList = ();
my @seqList = ();
my %missedLabelsPos;
my @labelsDistanceList = ();
my @firstQryConfList = ();
my @secondQryConfList = ();
my $idOffset = 100000;
#get number of CPU cores
my $info = Sys::Info->new;
my $cpu = $info->device( CPU => my %options );
#printf "There are %d CPUs\n" , $cpu->count || 1;
my $cpuCount = $cpu->count;
#get system RAM
my $mem = (((&totalmem / 1024) / 1024)) / 1024;
if( exists $inputs{n} ) { $cpuCount = $inputs{n}; }
print "\n";
## << read input XMAP >>
#my $alignments;
while (my $line = <XMAP>) {
chomp($line);
#if header then skip
if ($line =~ /^#/) {
next; }
else {
$alignments++;
my @s = split(/\t/,$line);
for (my $i=0; $i<scalar(@s); $i++) {
$s[$i] =~ s/^\s+|\s+$//g; }
#h XmapEntryID QryContigID RefContigID QryStartPos QryEndPos RefStartPos RefEndPos Orientation Confidence HitEnum QryLen RefLen LabelChannel Alignment
#f int int int float float float float string float string float float int string
my %xmap_line = (
"XmapEntryID" => "$s[0]",
"QryContigID" => "$s[1]",
"RefContigID" => "$s[2]",
"QryStartPos" => "$s[3]",
"QryEndPos" => "$s[4]",
"RefStartPos" => "$s[5]",
"RefEndPos" => "$s[6]",
"Orientation" => "$s[7]",
"Confidence" => "$s[8]",
"HitEnum" => "$s[9]",
"QryLen" => "$s[10]",
"RefLen" => "$s[11]",
"LabelChannel" => "$s[12]",
"Alignment" => "$s[13]"
);
push @xmap, \%xmap_line; }
}
print "Read in $alignments alignments from $inputs{x}\n";
print "\n";
#while (my $line = <XMAP>) {
#chomp($line);
##if header then skip
#if ($line =~ /^#/) {
#next; }
#else {
#$alignments++;
#my @s = split(/\t/,$line);
#for (my $i=0; $i<scalar(@s); $i++) {
#$s[$i] =~ s/^\s+|\s+$//g; }
##h XmapEntryID QryContigID RefContigID QryStartPos QryEndPos RefStartPos RefEndPos Orientation Confidence HitEnum
##f int int int float float float float string float string
## 1 14839 17 344.2 999946.6 42640026.3 43628302.0 + 138.45 2M1D4M1D8M1D10M1D7M1D7M1D2M1D6M1D2M1D2M1I10M2D3M1D13M2D6M1D10M1D10M1D6M5I4M
#my %xmap_line = (
#"XmapEntryID" => "$s[0]",
#"QryContigID" => "$s[1]",
#"RefContigID" => "$s[2]", #should be identical (one of the assumptions)
#"QryStartPos" => "$s[3]",
#"QryEndPos" => "$s[4]",
#"RefStartPos" => "$s[5]",
#"RefEndPos" => "$s[6]",
#"Orientation" => "$s[7]",
#"Confidence" => "$s[8]",
#"HitEnum" => "$s[9]",
#);
#push @xmap, \%xmap_line; }
#}
## << read input query CMAP >>
while (my $line = <QCMAP>) {
chomp($line);
#if header then skip
if ($line =~ /^#/) {
next; }
else {
$q_sites++;
my @s = split(/\t/,$line);
for (my $i=0; $i<scalar(@s); $i++) {
$s[$i] =~ s/^\s+|\s+$//g; }
##h CMapId ContigLength NumSites SiteID LabelChannel Position StdDev Coverage Occurrence GmeanSNR lnSNRsd SNR
# 2020 718132.6 74 1 1 20.0 81.9 14.0 14.0 12.4650 0.4814 0
push @q_mapIds, $s[0];
my %cmap_line = (
"CMapId" => "$s[0]",
"ContigLength" => "$s[1]",
"NumSites" => "$s[2]",
"SiteID" => "$s[3]",
"LabelChannel" => "$s[4]",
"Position" => "$s[5]",
"StdDev" => "$s[6]",
"Coverage" => "$s[7]",
"Occurrence" => "$s[8]"
);
push @q_cmap, \%cmap_line;
}
}
print "Read in $q_sites sites from $inputs{q}\n";
@q_mapIds = unique(@q_mapIds);
print "Read in ".scalar(@q_mapIds)." maps from $inputs{q}\n";
print "\n";
## << read input reference CMAP >>
while (my $line = <RCMAP>) {
chomp($line);
#if header then skip
if ($line =~ /^#/) {
next; }
else {
$r_sites++;
my @s = split(/\t/,$line);
#load first contig into hash
##h CMapId ContigLength NumSites SiteID LabelChannel Position StdDev Coverage Occurrence
# 2020 718132.6 74 1 1 20.0 81.9 14.0 14.0
push @r_mapIds, $s[0];
my %cmap_line = (
"CMapId" => "$s[0]", #should be identical (only single contig in _r.cmap)
"ContigLength" => "$s[1]",
"NumSites" => "$s[2]",
"SiteID" => "$s[3]",
"LabelChannel" => "$s[4]",
"Position" => "$s[5]",
"StdDev" => "$s[6]",
"Coverage" => "$s[7]",
"Occurrence" => "$s[8]"
);
push @r_cmap, \%cmap_line; }
}
print "Read in $r_sites sites from $inputs{r}\n";
@r_mapIds = unique(@r_mapIds);
print "Read in ".scalar(@r_mapIds)." maps from $inputs{r}\n";
if (scalar(@r_mapIds) > 1) {
die "ERROR: Input _r.cmap should have only 1 map\n"; }
print "\n";
## << fsite bed >>
# IF fsites BED file provided, only merge contigs that span fragile sites
my $hasfsites = 0;
my @fsites;
my $fragileSites = 0;
my $hasSeq = 0;
if( exists $inputs{bed} ) {
$hasfsites = 1;
my $fsitesFile = Cwd::abs_path($inputs{bed});
if (-e $fsitesFile && defined $wobble) {
print "Fragile sites BED file $fsitesFile provided\n";
print "Filtering merge list based on fragile sites BED file: $fsitesFile\n";
#read in .fsites file
open FSITES, "$fsitesFile" or die $!;
while (my $line = <FSITES>) {
chomp($line);
#if header then skip
if ($line =~ /^#/) {
next; }
else {
my @s = split(/\t/,$line);
if ($s[0] eq $r_mapIds[0]) { ## should be only one _r CMAP id
$fragileSites++;
#CMapId Start End Type Score Strand ThickStart ThickEnd ItemRgba Sequence
my %fsites_line = (
"CMapId" => "$s[0]",
"Start" => "$s[1]",
"End" => "$s[2]",
"Type" => "$s[3]",
"Score" => "$s[4]",
"Strand" => "$s[5]",
"ThickStart" => "$s[6]",
"ThickEnd" => "$s[7]",
"ItemRgba" => "$s[8]"
#"Sequence" => "$s[9]",
#"line" => $line
);
# deal with BED file that has sequences
if (scalar(@s) == 10) {
$hasSeq=1;
$fsites_line{"Sequence"} = "$s[9]";
}
push @fsites, \%fsites_line;
}
}
}
my @fsites = sort { $a->{'Start'} <=> $b->{'Start'} } @fsites;
print scalar(@fsites)." fragile sites identified in $fsitesFile \n";
close FSITES;
}
else {
print ".fsites and/or wobble undefined. Skipping .fsites filter\n\n"
}
}
## << alignmolAnalysis file >>
# IF alignmolAnalysis is present, also merge contigs that show molecule alignment evidence of fragile site
my $hasalignmol = 0;
my @alignmol;
my $alignmolMaps = 0;
if( exists $inputs{alignmolAnalysis} ) {
$hasalignmol = 1;
my $alignmolFile = Cwd::abs_path($inputs{alignmolAnalysis});
print "alignmolAnalysis file $alignmolFile provided. Also using single molecule evidence of fragile sites to merge contigs...\n";
#read in alignmolAnalysis file
open ALIGNMOL, "$alignmolFile" or die $!;
while (my $line = <ALIGNMOL>) {
chomp($line);
#if header then skip
if ($line =~ /^#/) {
next; }
else {
my @s = split(/\t/,$line);
$alignmolMaps++;
#CMapId StartRatio EndRatio
my %alignmol_line = (
"CMapId" => "$s[0]",
"StartRatio" => "$s[1]",
"EndRatio" => "$s[2]"
);
push @alignmol, \%alignmol_line;
}
}
my @alignmol = sort { $a->{'CMapId'} <=> $b->{'CMapId'} } @alignmol;
print scalar(@alignmol)." alignmol maps identified in $alignmolFile \n";
close ALIGNMOL;
}
## << calculate distance in labels between each adjacent alignments >>
# Generate list of contigs to be merged
print "\n";
my $previous = 0;
my $count=0;
for (my $i=0; $i < scalar(@xmap); $i++) {
my $id1 = $xmap[$i]->{'XmapEntryID'};
my $firstRefStartPos = $xmap[$i]->{'RefStartPos'};
my $firstRefEndPos = $xmap[$i]->{'RefEndPos'};
my $firstQryContigID = $xmap[$i]->{'QryContigID'};
my $firstOrientation = $xmap[$i]->{'Orientation'};
my $firstQryStartPos = $xmap[$i]->{'QryStartPos'};
my $firstQryEndPos = $xmap[$i]->{'QryEndPos'};
my $firstQryConf = $xmap[$i]->{'Confidence'};
if (($i+1)<scalar(@xmap)) {
my $id2 = $xmap[$i+1]->{'XmapEntryID'};
my $secondRefStartPos = $xmap[$i+1]->{'RefStartPos'};
my $secondRefEndPos = $xmap[$i+1]->{'RefEndPos'};
my $secondQryContigID = $xmap[$i+1]->{'QryContigID'};
my $secondOrientation = $xmap[$i+1]->{'Orientation'};
my $secondQryStartPos = $xmap[$i+1]->{'QryStartPos'};
my $secondQryEndPos = $xmap[$i+1]->{'QryEndPos'};
my $secondQryConf = $xmap[$i+1]->{'Confidence'};
my $posDistance = $secondRefStartPos - $firstRefEndPos;
print "\nConsidering merge between XmapEntryID: $id1 QryContigID: $firstQryContigID and XmapEntryID: $id2 QryContigID: $secondQryContigID Distance: $posDistance\n";
if (
(($secondRefStartPos >= $firstRefEndPos) && ($secondRefEndPos >= $firstRefEndPos) && ($secondRefStartPos >= $firstRefStartPos) && ($secondRefEndPos >= $firstRefStartPos))
||
( (abs($secondRefStartPos-$firstRefEndPos)<=$inputs{maxOverlap}) && ($secondRefEndPos >= $firstRefEndPos) && ($secondRefStartPos >= $firstRefStartPos) && ($secondRefEndPos >= $firstRefStartPos) )
) {
print "\tOverlap filter: PASS\n";
if ( $posDistance <= $maxBp ) {
print "\tDistance filter: PASS Distance: $posDistance\n";
#check to make sure that the alignment extends to start/end of contig
my $firstQryStartPosSite;
my $firstQryEndPosSite;
my $secondQryStartPosSite;
my $secondQryEndPosSite;
my $firstNumSites;
my $secondNumSites;
foreach my $hash (@q_cmap) {
#print Dumper($hash);
if (($hash->{'CMapId'} eq $firstQryContigID) && ($hash->{'Position'} eq $firstQryStartPos)) {
$firstQryStartPosSite = $hash->{'SiteID'};
$firstNumSites = $hash->{'NumSites'};
}
elsif (($hash->{'CMapId'} eq $firstQryContigID) && ($hash->{'Position'} eq $firstQryEndPos)) {
$firstQryEndPosSite = $hash->{'SiteID'};
$firstNumSites = $hash->{'NumSites'};
}
elsif (($hash->{'CMapId'} eq $secondQryContigID) && ($hash->{'Position'} eq $secondQryStartPos)) {
$secondQryStartPosSite = $hash->{'SiteID'};
$secondNumSites = $hash->{'NumSites'};
}
elsif (($hash->{'CMapId'} eq $secondQryContigID) && ($hash->{'Position'} eq $secondQryEndPos)) {
$secondQryEndPosSite = $hash->{'SiteID'};
$secondNumSites = $hash->{'NumSites'};
}
}
if ( (($firstOrientation eq "+" && $secondOrientation eq "+") && ($firstQryEndPosSite eq $firstNumSites) && ($secondQryStartPosSite eq 1)) || (($firstOrientation eq "+" && $secondOrientation eq "-") && ($firstQryEndPosSite eq $firstNumSites) && ($secondQryStartPosSite eq $secondNumSites)) || (($firstOrientation eq "-" && $secondOrientation eq "+") && ($firstQryEndPosSite eq 1) && ($secondQryStartPosSite eq 1)) || (($firstOrientation eq "-" && $secondOrientation eq "-") && ($firstQryEndPosSite eq 1) && ($secondQryStartPosSite eq $secondNumSites)) ) {
print "\tAlignment filter: PASS\n";
my $firstRefEndPosSite = 0;
my $secondRefStartPosSite = 0;
foreach my $hash (@r_cmap) {
if ($hash->{'Position'} eq $firstRefEndPos) {
$firstRefEndPosSite = $hash->{'SiteID'};
}
if ($hash->{'Position'} eq $secondRefStartPos) {
$secondRefStartPosSite = $hash->{'SiteID'};
}
}
my $labelsDistance = $secondRefStartPosSite - $firstRefEndPosSite - 1;
print "\tLabels: $labelsDistance\n";
if ( ($posDistance>=0 && $labelsDistance<=$maxlab) || ($posDistance<0 && abs($labelsDistance)<=$inputs{maxOverlapLabels}) ) {
print "\tLabel filter: PASS\n";
if ( (grep {$_ eq $firstQryContigID} @firstContigList) || (grep {$_ eq $firstQryContigID} @secondContigList) || (grep {$_ eq $secondQryContigID} @secondContigList) || (grep {$_ eq $secondQryContigID} @firstContigList)) {
print "\tOne of the contigs already ID'ed to merge...\n";
next; }
else {
print "\tLooking for fragile sites...\n";
#IF fsites defined, only output contigs that span fragile sites
if ($hasfsites!=0) {
#previous and next reference label as borders for fragile site search if no overlap, otherwise use ends of contigs as boundaries
my $prevLabelId = $firstRefEndPosSite - 1;
my $nextLabelId = $secondRefStartPosSite + 1;
if ($posDistance<0) {
$prevLabelId = $firstRefEndPosSite;
$nextLabelId = $secondRefStartPosSite;
}
my $prevLabelPos = 0;
my $nextLabelPos = 0;
my $missLabelPos=0;
my $missLabelPosSite = $firstRefEndPosSite + $labelsDistance; # assumed $labelsDistance to be at most 1
foreach my $hash (@r_cmap) {
if ($hash->{'SiteID'} eq $prevLabelId) {
$prevLabelPos = $hash->{'Position'};
}
if ($hash->{'SiteID'} eq $nextLabelId) {
$nextLabelPos = $hash->{'Position'};
}
if ( ($hash->{'SiteID'} eq $missLabelPosSite) && ($labelsDistance == 1) ) {
$missLabelPos = $hash->{'Position'};
}
}
#print "\tPrevLabel Id: $prevLabelId Pos: $prevLabelPos\tNextLabel Id: $nextLabelId Pos: $nextLabelPos\n";
my $globalMin=$prevLabelPos-1000;
my $globalMax=$nextLabelPos+1000;
#actually only search
if ($labelsDistance < -1) {
$globalMax = $firstRefEndPos+1000;
$globalMin = $secondRefStartPos-1000;
}
print "\tLooking for fsites in global window Min: $globalMin Max: $globalMax Range: ".abs($globalMax - $globalMin)."\n";
#print "Working on $inputs{bed} with ".$fragileSites." fsites\n";
my $fsiteFound = 0;
foreach my $hash (@fsites) {
#CMapId Start End Type Score Strand ThickStart ThickEnd ItemRgba Sequence
my $start = $hash->{'Start'};
my $end = $hash->{'End'};
my $type = $hash->{'Type'};
my $score = $hash->{'Score'};
my $strand = $hash->{'Strand'};
my $thickStart = $hash->{'ThickStart'};
my $thickEnd = $hash->{'ThickEnd'};
my $itemRgba = $hash->{'ItemRgba'};
my $seq = $hash->{'Sequence'};
my $min=0;
my $max=0;
if ( abs($start - $globalMin) > $wobble) {
#print "\tDifference: ".abs($start - $globalMin)." Changing min from $globalMin to ".($start - $wobble)."\n";
$min = $start - $wobble - 1;
}
else {
$min = $globalMin;
}
if ( abs($globalMax - $end) > $wobble) {
#print "\tDifference: ".abs($globalMax - $end)." Changing max from $globalMax to ".($end + $wobble)."\n";
$max = $end + $wobble + 1;
}
else {
$max = $globalMax;
}
if ( ($firstRefEndPos >= $min) && ($firstRefEndPos <= $max) && ($secondRefStartPos <= $max) && ($secondRefStartPos >= $min) ) {
print "\tActual Fsite Start: $start End: $end Range: ".abs($end - $start)."\n";
print "\tFragile site located within Window corrected for max wobble fsite window Min: $min Max: $max Distance: ".abs($max - $min)."\n";
#print "\tLeft wobble: $leftWobble Right wobble: $rightWobble\n";
next if ($firstQryContigID eq $previous);
#generate new alignmol stats for newContig
print "\tNew alignmolAnalysis entry created\n";
my $firstRatio=0;
my $secondRatio=0;
my $newId = $firstQryContigID+$idOffset;
my $newStartRatio=0;
my $newEndRatio=0;
foreach my $alignmolEntry_ref (@alignmol) {
my %alignmolEntry = %$alignmolEntry_ref;
if ($alignmolEntry{CMapId} eq $firstQryContigID || $alignmolEntry{CMapId} eq $secondQryContigID) {
print "\t\tCmapID: $alignmolEntry{CMapId} StartRatio: $alignmolEntry{StartRatio} EndRatio: $alignmolEntry{EndRatio}\n";
}
if ($alignmolEntry{CMapId} eq $firstQryContigID) {
if ($firstOrientation eq "+") {
$firstRatio = $alignmolEntry{EndRatio};
$newStartRatio = $alignmolEntry{StartRatio};
}
else {
$firstRatio = $alignmolEntry{StartRatio};
$newStartRatio = $alignmolEntry{EndRatio};
}
}
elsif ($alignmolEntry{CMapId} eq $secondQryContigID) {
if ($secondOrientation eq "+") {
$secondRatio = $alignmolEntry{StartRatio};
$newEndRatio = $alignmolEntry{EndRatio};
}
else {
$secondRatio = $alignmolEntry{EndRatio};
$newEndRatio = $alignmolEntry{StartRatio};
}
}
}
my $out = "$inputs{alignmolAnalysis}";
open OUT, "+>>$out" or die "ERROR: Cannot open $out for writing! $!\n";
flock (OUT, LOCK_EX) or die "ERROR: Cannot open $out for locking! $!\n";
seek (OUT, 0, 2);
print OUT "$newId\t$newStartRatio\t$newEndRatio\n";
print "\tNewCmapID: $newId NewStartRatio: $newStartRatio NewEndRatio: $newEndRatio\n";
flock(OUT, LOCK_UN) or die "ERROR: Cannot unlock $out! $!\n";
close OUT;
my %alignmol_line = (
"CMapId" => "$newId",
"StartRatio" => "$newStartRatio",
"EndRatio" => "$newEndRatio"
);
push @alignmol, \%alignmol_line;
push @firstContigList,$firstQryContigID;
push @secondContigList,$secondQryContigID;
#CMapId Start End Type Score Strand ThickStart ThickEnd ItemRgba Sequence
push @fsiteTypeList, $type;
push @scoreList, $score;
push @strandList, $strand;
push @thickStartList, $thickStart;
push @thickEndList, $thickEnd;
push @itemRgbaList, $itemRgba;
$missedLabelsPos{$count} = $missLabelPos;
push @labelsDistanceList, $labelsDistance;
push @firstQryConfList, $firstQryConf;
push @secondQryConfList, $secondQryConf;
$count++;
if ($hasSeq == 1) { push @seqList, $seq; }
$previous = $firstQryContigID;
$fsiteFound = 1;
last;
}
}
if ($fsiteFound eq 0 && $hasalignmol eq 1 && $labelsDistance >= -1) {
print "\tLooking for single molecule alignment-based fragile sites...\n";
my $firstRatio=0;
my $secondRatio=0;
my $newId = $firstQryContigID+$idOffset;
my $newStartRatio=0;
my $newEndRatio=0;
foreach my $alignmolEntry_ref (@alignmol) {
my %alignmolEntry = %$alignmolEntry_ref;
#print "\n\tCmapID: $alignmolEntry{CMapId} StartRatio: $alignmolEntry{StartRatio} EndRatio: $alignmolEntry{EndRatio}\n";
if ($alignmolEntry{CMapId} eq $firstQryContigID) {
if ($firstOrientation eq "+") {
$firstRatio = $alignmolEntry{EndRatio};
$newStartRatio = $alignmolEntry{StartRatio};
}
else {
$firstRatio = $alignmolEntry{StartRatio};
$newStartRatio = $alignmolEntry{EndRatio};
}
}
elsif ($alignmolEntry{CMapId} eq $secondQryContigID) {
if ($secondOrientation eq "+") {
$secondRatio = $alignmolEntry{StartRatio};
$newEndRatio = $alignmolEntry{EndRatio};
}
else {
$secondRatio = $alignmolEntry{EndRatio};
$newEndRatio = $alignmolEntry{StartRatio};
}
}
}
print "\tQryContigID: $firstQryContigID EndRatio: $firstRatio QryContigID: $secondQryContigID StartRatio: $secondRatio\n";
my $minRatio = $inputs{minRatio} + 0;
if ($firstRatio >= $minRatio && $secondRatio >= $minRatio && ( ($posDistance>=0 && $labelsDistance<=1) || ($posDistance<0 && abs($labelsDistance)<=$inputs{maxOverlapLabels}) ) ) {
print "\t\tFragile site observed at genome map ends\n";
push @firstContigList,$firstQryContigID;
push @secondContigList,$secondQryContigID;
push @fsiteTypeList, "Observed";
push @scoreList, "0";
push @strandList, "0";
push @thickStartList, $prevLabelPos;
push @thickEndList, $nextLabelPos;
push @itemRgbaList, "255,255,255,255";
push @firstQryConfList, $firstQryConf;
push @secondQryConfList, $secondQryConf;
if ($hasSeq == 1) { push @seqList, "NNNNNNNNNN"; }
push @labelsDistanceList, $labelsDistance;
$previous = $firstQryContigID;
$missedLabelsPos{$count} = $missLabelPos;
$count++;
$fsiteFound = 1;
my $out = "$inputs{alignmolAnalysis}";
open OUT, "+>>$out" or die "ERROR: Cannot open $out for writing! $!\n";
flock (OUT, LOCK_EX) or die "ERROR: Cannot open $out for locking! $!\n";
seek (OUT, 0, 2);
print OUT "$newId\t$newStartRatio\t$newEndRatio\n";
flock(OUT, LOCK_UN) or die "ERROR: Cannot unlock $out! $!\n";
close OUT;
my %alignmol_line = (
"CMapId" => "$newId",
"StartRatio" => "$newStartRatio",
"EndRatio" => "$newEndRatio"
);
push @alignmol, \%alignmol_line;
}
else {
print "\t\tNo fragile site observed at genome map ends\n";
}
}
if ($fsiteFound eq 0) { print "\tNo fragile site found in Window\n"; }
}
else {
#print $inputs{bed}, " undefined'."\n";
push @firstContigList,$firstQryContigID;
push @secondContigList,$secondQryContigID;
}
}
##IF alignmol defined, merge maps whose ends look like fragile sites
#if ($hasalignmol != 0) {
#if ( (grep {$_ eq $firstQryContigID} @firstContigList) || (grep {$_ eq $firstQryContigID} @secondContigList) || (grep {$_ eq $secondQryContigID} @secondContigList) || (grep {$_ eq$secondQryContigID} @firstContigList)) {
##print "\tOne of the contigs already ID'ed to merge...\n";
#next; }
#else {
#print "\tLooking for single molecule alignment-based fragile sites...\n";
#my $firstRatio=0;
#my $secondRatio=0;
#foreach my $alignmolEntry_ref (@alignmol) {
#my %alignmolEntry = %$alignmolEntry_ref;
#if ($alignmolEntry{CMapId} eq $firstQryContigID) {
#if ($firstOrientation eq "+") {
#$firstRatio = $alignmolEntry{EndRatio};
#}
#else {
#$firstRatio = $alignmolEntry{StartRatio};
#}
#}
#elsif ($alignmolEntry{CMapId} eq $secondQryContigID) {
#if ($firstOrientation eq "+") {
#$secondRatio = $alignmolEntry{StartRatio};
#}
#else {
#$secondRatio = $alignmolEntry{EndRatio};
#}
#}
#}
#my $minRatio=0.90;
#if ($firstRatio >= $minRatio && $secondRatio >= $minRatio && $labelsDistance <= 0) {
#push @firstContigList,$firstQryContigID;
#push @secondContigList,$secondQryContigID;
#push @fsiteTypeList, $type;
#push @scoreList, $score;
#push @strandList, $strand;
#push @thickStartList, $thickStart;
#push @thickEndList, $thickEnd;
#push @itemRgbaList, "255,255,255,255";
#}
#}
#}
}
else {
print "\tLabel filter: FAIL\n";
}
}
else {
print "\tAlignment filter: FAIL\n";
#if ( (($firstOrientation eq "+" && $secondOrientation eq "+") && ($firstQryEndPosSite eq $firstNumSites) && ($secondQryStartPosSite eq 1)) || (($firstOrientation eq "+" && $secondOrientation eq "-") && ($firstQryEndPosSite eq $firstNumSites) && ($secondQryStartPosSite eq $secondNumSites)) || (($firstOrientation eq "-" && $secondOrientation eq "+") && ($firstQryEndPosSite eq 1) && ($secondQryStartPosSite eq 1)) || (($firstOrientation eq "-" && $secondOrientation eq "-") && ($firstQryEndPosSite eq 1) && ($secondQryStartPosSite eq $secondNumSites)) ) {
print "\t\tFirst contig: $id1 Orientation: $firstOrientation Start: $firstQryStartPos SiteId: $firstQryStartPosSite End: $firstQryEndPos SiteId: $firstQryEndPosSite NumSites: $firstNumSites\n";
print "\t\tSecond contig: $id2 Orientation: $secondOrientation Start: $secondQryStartPos SiteId: $secondQryStartPosSite End: $secondQryEndPos SiteId: $secondQryEndPosSite NumSites: $secondNumSites\n";
}
}
else {
print "\tDistance filter: FAIL\n";
}
}
else {
print "\tOverlap filter: FAIL\n";
}
}
else {
print "All possible merges identified.\n\n";
}
}
# create AoH of pairs of contigs
my @contigs;
for (my $i=0; $i<(scalar(@secondContigList)); $i++) {
my %merge = (
"firstContig" => $firstContigList[$i],
"secondContig" => $secondContigList[$i]
);
push @contigs, \%merge;
}
## << Merge Contigs >>
# Iterate over identified array list and merge contigs
#my $nomerge = 0;
if (scalar(@secondContigList) > 0) {
if (scalar(@firstContigList) == scalar(@secondContigList)) {
print "Merging between ".scalar(@secondContigList)." maps\n\n";
my $extractScript = $scriptspath."/extractContigs.pl";
# Perform first merge
#CMapId Start End Type Score Strand ThickStart ThickEnd ItemRgba Sequence
my @ARGS = ($inputs{x}, $inputs{q}, $inputs{r}, $firstContigList[0], $secondContigList[0],$fsiteTypeList[0],$scoreList[0],$strandList[0],$thickStartList[0],$thickEndList[0],$itemRgbaList[0]);
if ($hasSeq == 1 ) {
push @ARGS, $seqList[0];
}
else { push @ARGS,"N";}
if (exists $missedLabelsPos{0}) {
push @ARGS, $missedLabelsPos{0};
}
else { push @ARGS,"0"; }
push @ARGS, $labelsDistanceList[0];
push @ARGS, $firstQryConfList[0];
push @ARGS, $secondQryConfList[0];
my $cwd = cwd();
print "Running command: ".$^X." $extractScript ". join(" ",@ARGS)."\n";
system($^X, "$extractScript", @ARGS);
print "QryContigID $firstContigList[0] merged with QryContigID $secondContigList[0] into QueryContigID ".($firstContigList[0]+$idOffset)."\n";
print "\n";
my $temp = $secondContigList[0];
# Perform next merges
for (my $i=1; $i<(scalar(@secondContigList)); $i++) {
next if ( ($secondContigList[$i] eq $secondContigList[0]) ) ;
#for (my $i=1; $i<(3); $i++) {
@ARGS = ($inputs{x}, $outName, $inputs{r}, $firstContigList[$i], $secondContigList[$i], $fsiteTypeList[$i],$scoreList[$i],$strandList[$i],$thickStartList[$i],$thickEndList[$i],$itemRgbaList[$i]);
if ($hasSeq == 1 ) {
push @ARGS, $seqList[$i];
}
else { push @ARGS,"N";}
if (exists $missedLabelsPos{$i}) {
push @ARGS, $missedLabelsPos{$i};
}
else { push @ARGS,"0"; }
push @ARGS, $labelsDistanceList[$i];
push @ARGS, $firstQryConfList[$i];
push @ARGS, $secondQryConfList[$i];
print "Running command: ".$^X." $extractScript ". join(" ",@ARGS)."\n";
system($^X, "$extractScript", @ARGS);
print "QryContigID $firstContigList[$i] merged with QryContigID $secondContigList[$i] into QueryContigID ".($firstContigList[$i]+$idOffset)."\n";
print "\n";
}
}
else { print "ERROR: uneven number of contigs in firstQueryList and secondQueryList\n"; }
print "DONE: merging ".scalar(@secondContigList)." contigs complete.\n\n";
# Align new _q.cmap with merged contigs back to _r.cmap
my $veto = q/-output-veto-filter '(_intervals.txt|.err|.maprate|[a-z|A-Z].map)$'/;
#$veto = $veto." -output-veto-filter .err";
#print "Veto: $veto\n";
if ($inputs{round} == 1) {
# Perform first alignment round
print "===== Performing round $inputs{round} alignment =====\n";
# system("~/tools/RefAligner -ref $inputs{r} -i $outName -o $outName2 -maxthreads $cpuCount -res 2.9 -FP 0.6 -FN 0.06 -sf 0.20 -sd 0.10 -extend 1 -outlier 0.0001 -endoutlier 0.001 -deltaX 12 -deltaY 12 -xmapchim 14 -hashgen 5 3 2.4 1.5 0.05 5.0 1 1 1 -hash -hashdelta 50 -mres 2.9 -insertThreads 4 -nosplit 2 -biaswt 0 -f -maxmem $mem -T $inputs{pvalue} -BestRef 1 $veto -readparameters $inputs{e} -stdout -stderr");
system("~/tools/RefAligner -ref $inputs{r} -i $outName -o $outName2 -maxthreads $cpuCount -res 2.9 -FP 0.6 -FN 0.06 -sf 0.20 -sd 0.10 -extend 1 -outlier 0.0001 -endoutlier 0.001 -deltaX 12 -deltaY 12 -xmapchim 14 -hashgen 5 3 2.4 1.5 0.05 5.0 1 1 1 -hash -hashdelta 50 -mres 0.5 -insertThreads 4 -nosplit 2 -biaswt 0 -f -maxmem $mem -T $inputs{pvalue} -BestRef 1 $veto -readparameters $inputs{e} -stdout");
print "\nFIRST ROUND COMPLETE.\n\n";
# Launch second round
# usage: perl gapFill.pl -x <input.xmap> -q <input_q.cmap> -r <input_r.cmap> -o <output_prefix> [--bed <.bed fragile sites file>] [--round <start_round=1>] [--maxlab <max_label_gap_tolerence=0>] [--maxfill <max basepairs to fill between contigs = 35000>] [--wobble <fragile site wobble in bp = 0>]
print "****** Launching round 2 ******\n";
$outName2 =~ s/.xmap//g;
my %args = %inputs;
$args{x} = $outName2.".xmap";
$args{q} = $outName2."_q.cmap";
$args{round} = 2;
my @args;
foreach my $opt (keys %args) { push @args, "--$opt $args{$opt}"; }
my $syscall = $^X." $0 ". join(" ",@args);
print "Running command: $syscall \n";
system($syscall);
print "\n======== ALL ROUNDS COMPLETE =========\n\n";
}
elsif ($inputs{round} == 2) {
# If second round, perform 2nd round of merge
print "====== Performing round $inputs{round} alignment ======= \n";
# system("~/tools/RefAligner -ref $inputs{r} -i $outName -o $outName3 -maxthreads $cpuCount -res 2.9 -FP 0.6 -FN 0.06 -sf 0.20 -sd 0.10 -extend 1 -outlier 0.0001 -endoutlier 0.001 -deltaX 12 -deltaY 12 -xmapchim 14 -hashgen 5 3 2.4 1.5 0.05 5.0 1 1 1 -hash -hashdelta 50 -mres 2.9 -insertThreads 4 -nosplit 2 -biaswt 0 -f -maxmem $mem -T $inputs{pvalue} -BestRef 1 $veto -readparameters $inputs{e} -stdout -stderr");
#system("~/tools/RefAligner -ref $inputs{r} -i $outName -o $outName3 -maxthreads $cpuCount -res 2.9 -FP 0.6 -FN 0.06 -sf 0.20 -sd 0.10 -extend 1 -outlier 0.0001 -endoutlier 0.001 -deltaX 12 -deltaY 12 -xmapchim 14 -hashgen 5 3 2.4 1.5 0.05 5.0 1 1 1 -hash -hashdelta 50 -mres 2.9 -insertThreads 4 -nosplit 2 -biaswt 0 -f -maxmem $mem -T $inputs{pvalue} -BestRef 1 $veto -readparameters $inputs{e} -stdout");
system("~/tools/RefAligner -ref $inputs{r} -i $outName -o $outName2 -maxthreads $cpuCount -res 2.9 -FP 0.6 -FN 0.06 -sf 0.20 -sd 0.10 -extend 1 -outlier 0.0001 -endoutlier 0.001 -deltaX 12 -deltaY 12 -xmapchim 14 -hashgen 5 3 2.4 1.5 0.05 5.0 1 1 1 -hash -hashdelta 50 -mres 0.5 -insertThreads 4 -nosplit 2 -biaswt 0 -f -maxmem $mem -T $inputs{pvalue} -BestRef 1 $veto -readparameters $inputs{e} -stdout");
print "\nSECOND ROUND COMPLETE.\n\n";
#$inputs{round} = 0;
# Launch third round
# usage: perl gapFill.pl -x <input.xmap> -q <input_q.cmap> -r <input_r.cmap> -o <output_prefix> [--bed <.bed fragile sites file>] [--round <start_round=1>] [--maxlab <max_label_gap_tolerence=0>] [--maxfill <max basepairs to fill between contigs = 35000>] [--wobble <fragile site wobble in bp = 0>]
print "****** Launching round 3******\n";
$outName2 =~ s/.xmap//g;
my %args = %inputs;
$args{x} = $outName2.".xmap";
$args{q} = $outName2."_q.cmap";
$args{round} = 3;
my @args;
foreach my $opt (keys %args) { push @args, "--$opt $args{$opt}"; }
my $syscall = $^X." $0 ". join(" ",@args);
print "Running command: $syscall \n";
system($syscall);
exit 0; #If second round, exit script gracefully to return back to first round script to do more work
}
elsif ($inputs{round} == 3) {
# If third round, perform 3rd round of merge
print "====== Performing round $inputs{round} alignment ======= \n";
system("~/tools/RefAligner -ref $inputs{r} -i $outName -o $outName2 -maxthreads $cpuCount -res 2.9 -FP 0.6 -FN 0.06 -sf 0.20 -sd 0.10 -extend 1 -outlier 0.0001 -endoutlier 0.001 -deltaX 12 -deltaY 12 -xmapchim 14 -hashgen 5 3 2.4 1.5 0.05 5.0 1 1 1 -hash -hashdelta 50 -mres 0.5 -insertThreads 4 -nosplit 2 -biaswt 0 -f -maxmem $mem -T $inputs{pvalue} -BestRef 1 $veto -readparameters $inputs{e} -stdout");
print "\nTHIRD ROUND COMPLETE.\n\n";
# Launch third round
# usage: perl gapFill.pl -x <input.xmap> -q <input_q.cmap> -r <input_r.cmap> -o <output_prefix> [--bed <.bed fragile sites file>] [--round <start_round=1>] [--maxlab <max_label_gap_tolerence=0>] [--maxfill <max basepairs to fill between contigs = 35000>] [--wobble <fragile site wobble in bp = 0>]
# Launch fourth round
print "****** Launching round 4******\n";
$outName2 =~ s/.xmap//g;
my %args = %inputs;
$args{x} = $outName2.".xmap";
$args{q} = $outName2."_q.cmap";
$args{round} = 4;
my @args;
foreach my $opt (keys %args) { push @args, "--$opt $args{$opt}"; }
my $syscall = $^X." $0 ". join(" ",@args);
print "Running command: $syscall \n";
system($syscall);
exit 0; #If third round, exit script gracefully to return back to second round script to do more work
}
elsif ($inputs{round} == 4) {
# If fourth round, perform 4th round of merge
print "====== Performing round $inputs{round} alignment ======= \n";
system("~/tools/RefAligner -ref $inputs{r} -i $outName -o $outName2 -maxthreads $cpuCount -res 2.9 -FP 0.6 -FN 0.06 -sf 0.20 -sd 0.10 -extend 1 -outlier 0.0001 -endoutlier 0.001 -deltaX 12 -deltaY 12 -xmapchim 14 -hashgen 5 3 2.4 1.5 0.05 5.0 1 1 1 -hash -hashdelta 50 -mres 0.5 -insertThreads 4 -nosplit 2 -biaswt 0 -f -maxmem $mem -T $inputs{pvalue} -BestRef 1 $veto -readparameters $inputs{e} -stdout");
print "\nFOURTH ROUND COMPLETE.\n\n";
# Launch fifth round
print "****** Launching round 5******\n";
$outName2 =~ s/.xmap//g;
my %args = %inputs;
$args{x} = $outName2.".xmap";
$args{q} = $outName2."_q.cmap";
$args{round} = 5;
my @args;
foreach my $opt (keys %args) { push @args, "--$opt $args{$opt}"; }
my $syscall = $^X." $0 ". join(" ",@args);
print "Running command: $syscall \n";
system($syscall);
exit 0; #If fourth round, exit script gracefully to return back to third round script to do more work
}
elsif ($inputs{round} == 5) {
# If fifth round, perform 5th round of merge
print "====== Performing round $inputs{round} alignment ======= \n";
system("~/tools/RefAligner -ref $inputs{r} -i $outName -o $outName2 -maxthreads $cpuCount -res 2.9 -FP 0.6 -FN 0.06 -sf 0.20 -sd 0.10 -extend 1 -outlier 0.0001 -endoutlier 0.001 -deltaX 12 -deltaY 12 -xmapchim 14 -hashgen 5 3 2.4 1.5 0.05 5.0 1 1 1 -hash -hashdelta 50 -mres 0.5 -insertThreads 4 -nosplit 2 -biaswt 0 -f -maxmem $mem -T $inputs{pvalue} -BestRef 1 $veto -readparameters $inputs{e} -stdout");
print "\nFIFTH ROUND COMPLETE.\n\n";
# Launch sixth round
print "****** Launching round 6******\n";
$outName2 =~ s/.xmap//g;
my %args = %inputs;
$args{x} = $outName2.".xmap";
$args{q} = $outName2."_q.cmap";
$args{round} = 6;
my @args;
foreach my $opt (keys %args) { push @args, "--$opt $args{$opt}"; }
my $syscall = $^X." $0 ". join(" ",@args);
print "Running command: $syscall \n";
system($syscall);
exit 0; #If fifth round, exit script gracefully to return back to fourth round script to do more work
}
elsif ($inputs{round} == 6) {
# If sixth round, perform 6th round of merge
print "====== Performing round $inputs{round} alignment ======= \n";
system("~/tools/RefAligner -ref $inputs{r} -i $outName -o $outName3 -maxthreads $cpuCount -res 2.9 -FP 0.6 -FN 0.06 -sf 0.20 -sd 0.10 -extend 1 -outlier 0.0001 -endoutlier 0.001 -deltaX 12 -deltaY 12 -xmapchim 14 -hashgen 5 3 2.4 1.5 0.05 5.0 1 1 1 -hash -hashdelta 50 -mres 0.5 -insertThreads 4 -nosplit 2 -biaswt 0 -f -maxmem $mem -T $inputs{pvalue} -BestRef 1 $veto -readparameters $inputs{e} -stdout");
print "\nSIXTH ROUND COMPLETE.\n\n";
exit 0; #If sixth round, exit script gracefully to return back to fifth round script to do more work
}
}
else {
print "No merges possible. Copying input data unchanged.\n\n";
copy($inputs{x},$outName3.".xmap") or die "Copy failed: $!";
copy($inputs{q},$outName3."_q.cmap") or die "Copy failed: $!";
copy($inputs{r},$outName3."_r.cmap") or die "Copy failed: $!";
#$nomerge = 1;
if ($inputs{round}==1) {
print "\nFIRST ROUND COMPLETE.\n\n";
}
elsif ($inputs{round}==2) {
print "\nSECOND ROUND COMPLETE.\n\n";
}
elsif ($inputs{round}==3) {
print "\nTHIRD ROUND COMPLETE.\n\n";
}
elsif ($inputs{round}==4) {
print "\nFOURTH ROUND COMPLETE.\n\n";
}
}
if ($inputs{round}==1) {
## << Calculate stats on original and new CMAPs >>
# usage: calc_cmap_stats.pl <CMAP_File>
my $dir = glob("~/scripts/HybridScaffold/scripts");
my $script = "calc_cmap_stats.pl";
if (-e "$dir/$script") {
#if ($nomerge == 1) {
print "Original query CMAP $inputs{q} stats:\n";
#chdir $dir or die "ERROR: Cannot change directory to $dir: $!\n";
my $cmd = "perl $dir/$script $inputs{q}";
print "Running command: $cmd\n";
system($cmd);
print "\n";
my $file = $outName3."_q.cmap";
print "Gap-filled query CMAP $file stats:\n";
#chdir $dir or die "ERROR: Cannot change directory to $dir: $!\n";
$cmd = "perl $dir/$script $file";
print "Running command: $cmd\n";
system($cmd);
print "\n";
#}
}
else {
print "perl script calc_cmap_stats.pl not found at $script\n"; }
}
sub wanted {
m/_temp/ and do {
unlink $_ or warn "Could not unlink file $_\n"; }; }
sub wanted2 {
m/_merged_contigs_q/ and do {