-
Notifications
You must be signed in to change notification settings - Fork 0
/
bam_breaks_by_windowsize_and_numpairs.pl
executable file
·451 lines (390 loc) · 12.7 KB
/
bam_breaks_by_windowsize_and_numpairs.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
#!/usr/bin/env perl
use strict;
use warnings;
use List::Util qw/sum min max/;
use Data::Dumper; ### For test ###
use constant USAGE =><<EOH;
usage: $0 in.stats fasta.fai insert_mean insert_stdev Num_Pairs out.breaks
This script is used to detect breaks problem
in.stats: bam_scaffolding_separately.stats_noBioDBSAM.pl output
fasta.fai: index for fasta
insert_mean: Mean insert size
insert_stdev: StDev insert size
Num_Pairs: INT pairs would be count as 1 evidence
out.breaks: output
v20170417
EOH
die USAGE if (scalar(@ARGV) !=6 or $ARGV[0] eq '-h' or $ARGV[0] eq '--help');
my $input=shift @ARGV;
my $fastaindex=shift @ARGV;
my $distave=shift @ARGV;
my $disstdev=shift @ARGV;
my $numpairs=shift @ARGV;
my $outfile_breaks=shift @ARGV;
### Configurable
my $maxmappingtimes=1;
my $opt_disance=10000;
my $debug=0;
### Default
my $linenum=0;
my %idhash=();
my %allrefs=();
my %seqlength=();
my $numContigs=0;
my $total_bases=0;
my $total_errors=0;
#input output
unless (defined $input and -s $input) {
die "Error: invalid input.stats\n";
}
unless (defined $numpairs and $numpairs=~/^\d+/) {
die "Error: invalid Num_Pairs, should be INT\n";
}
unless (defined $outfile_breaks and $outfile_breaks=~/^[-\.\/\w_]+$/) {
die "Error: invalid out.breaks\n";
}
unlink $outfile_breaks if (-e $outfile_breaks);
$linenum=0;
### Read sequence length
open (FASTAINDEX, " < $fastaindex") || die "Error: can not open fasta.fai\n";
while (my $line=<FASTAINDEX>) {
chomp $line;
$linenum++;
my @arr=split(/\t/, $line);
unless ($arr[0]=~/^\S+$/ and $arr[1]=~/^\d+$/) {
die "Error: invalid fasta line: $line\n";
}
if (exists $seqlength{$arr[0]}) {
die "Error: repeated sequence ID: $arr[0]\n";
}
$seqlength{$arr[0]}=$arr[1];
}
close FASTAINDEX;
print "\n##### SUMMARY 1 #####\n";
print " STATS: $input\n";
print " Index: $fastaindex\n";
print " Read total fasta.fai lines: $linenum\n";
print " Total seqeunces: ", scalar(keys %seqlength), "\n\n";
$linenum=0;
open (STATSIN, " < $input") || die "Error: can not open in.stats: $input\n";
while (my $line=<STATSIN>) {
chomp $line;
$linenum++;
my @arr=split(/\t/, $line);
if (exists $idhash{$arr[0]}{$arr[2]}{$arr[1]}) {
$idhash{$arr[0]}{'EXCLUDEDPLZ'}++;
next;
}
if ($arr[5] eq '+') {
$idhash{$arr[0]}{$arr[2]}{$arr[1]}{'pos'}=$arr[3];
$idhash{$arr[0]}{$arr[2]}{$arr[1]}{'end'}=$arr[4];
$idhash{$arr[0]}{$arr[2]}{$arr[1]}{'str'}=$arr[5];
}
elsif ($arr[5] eq '-') {
$idhash{$arr[0]}{$arr[2]}{$arr[1]}{'pos'}=$arr[4];
$idhash{$arr[0]}{$arr[2]}{$arr[1]}{'end'}=$arr[3];
$idhash{$arr[0]}{$arr[2]}{$arr[1]}{'str'}=$arr[5];
}
else {
die "Error: strand error at line($linenum): $line\n";
}
}
close STATSIN;
my @allreads=();
@allreads=keys (%idhash);
foreach my $indread (@allreads) {
if (exists $idhash{$indread}{'EXCLUDEDPLZ'}) {
delete $idhash{$indread};
next;
}
my @contigs=keys %{$idhash{$indread}};
my @arr2=();
my @arr3=();
foreach my $contig (@contigs) {
push (@arr2, $contig) if (exists $idhash{$indread}{$contig}{1});
push (@arr3, $contig) if (exists $idhash{$indread}{$contig}{2});
}
my $maxmapping= max (scalar(@arr2), scalar(@arr3));
if ($maxmapping>$maxmappingtimes) {### control mapping times
delete $idhash{$indread};
next;
}
foreach (@contigs) {
$allrefs{$_}{$indread}++;
}
}
my %goodregions=();
my %forwardstrand=();
my %reversestrand=();
my %badregions=();
my $total_good_length=0;
my $total_good_num=0;
my $contigs;
open (BREAKOUT, " > $outfile_breaks") || die "Error: can not write $outfile_breaks\n";
foreach $contigs (sort keys %allrefs) {
my @readnames=keys %{$allrefs{$contigs}};
unless (scalar(@readnames)>=$numpairs) {
print STDERR "Warnings: less than $numpairs pairs on Seq $contigs\n" if ($debug);
next;
}
my $numunpaired=0;
my $numpaired=0;
my %readinfo=();
my $numproperpaired=0;
%goodregions=();
%forwardstrand=();
%reversestrand=();
%badregions=();
### detect paired or unpaired
foreach my $readid (@readnames) {
if (exists $idhash{$readid}{$contigs}{1} and $idhash{$readid}{$contigs}{2}) {
$numpaired++;
if (($idhash{$readid}{$contigs}{1}{'str'} eq '+') and ($idhash{$readid}{$contigs}{2}{'str'} eq '-')) {
my $distance=$idhash{$readid}{$contigs}{2}{'pos'}-$idhash{$readid}{$contigs}{1}{'pos'};
if ($distance>=($distave-$disstdev) and $distance<=($distave+$disstdev)) {
if (exists $goodregions{$idhash{$readid}{$contigs}{1}{'pos'}}) {
if ($idhash{$readid}{$contigs}{2}{'pos'}>$goodregions{$idhash{$readid}{$contigs}{1}{'pos'}}) {
$goodregions{$idhash{$readid}{$contigs}{1}{'pos'}}=$idhash{$readid}{$contigs}{2}{'pos'};
}
}
else {
$goodregions{$idhash{$readid}{$contigs}{1}{'pos'}}=$idhash{$readid}{$contigs}{2}{'pos'};
}
$numproperpaired++;
$readinfo{$readid}{'paired'}++;
}
}
elsif (($idhash{$readid}{$contigs}{1}{'str'} eq '-') and ($idhash{$readid}{$contigs}{2}{'str'} eq '+')) {
my $distance=$idhash{$readid}{$contigs}{1}{'pos'}-$idhash{$readid}{$contigs}{2}{'pos'};
if ($distance>=($distave-$disstdev) and $distance<=($distave+$disstdev)) {
if (exists $goodregions{$idhash{$readid}{$contigs}{2}{'pos'}}) {
if ($idhash{$readid}{$contigs}{1}{'pos'}>$goodregions{$idhash{$readid}{$contigs}{2}{'pos'}}) {
$goodregions{$idhash{$readid}{$contigs}{2}{'pos'}}=$idhash{$readid}{$contigs}{1}{'pos'};
}
}
else {
$goodregions{$idhash{$readid}{$contigs}{2}{'pos'}}=$idhash{$readid}{$contigs}{1}{'pos'};
}
$numproperpaired++;
$readinfo{$readid}{'paired'}++;
}
}
}
else {
$numunpaired++;
$readinfo{$readid}{'unpaired'}++;
}
}
my $temphash1=&MergeRegion(\%goodregions);
# print "Test: $contigs Total ",scalar(@readnames) ," Unpaired $numunpaired Paired $numpaired Properpaired $numproperpaired\n";
# print Dumper $temphash1; ### For test ###
if (scalar(keys %{$temphash1})>0) {
$total_good_num++;
foreach my $goodstart (sort {$a<=>$b} keys %{$temphash1}) {
$total_good_length=$total_good_length+(${$temphash1}{$goodstart}-$goodstart+1);
}
}
unless ($numunpaired>=$numpairs) {
print STDERR "Warnings: less than $numpairs unpaired on Seq $contigs\n" if ($debug);
next;
}
### group
my $temp_i=0;
foreach my $readid (@readnames) {
my @matenum=keys %{$idhash{$readid}{$contigs}};
foreach my $whichmate (@matenum) {
if ($idhash{$readid}{$contigs}{$whichmate}{'str'} eq '+') {
$forwardstrand{$temp_i++}=[$readid, $whichmate, $idhash{$readid}{$contigs}{$whichmate}{'pos'}];
}
else {
$reversestrand{$temp_i++}=[$readid, $whichmate, $idhash{$readid}{$contigs}{$whichmate}{'pos'}];
}
}
}
my $forwref1=&SortHash(\%forwardstrand);
my $revsref2=&SortHash(\%reversestrand);
for (my $temp_x=0; $temp_x<(scalar(@{$forwref1})-$numpairs); $temp_x++) {
my @temparr=();
foreach (my $temp_y=$temp_x; $temp_y<($temp_x+$numpairs); $temp_y++) {
push (@temparr, ${$forwref1}[$temp_y]);
}
next unless (($forwardstrand{$temparr[-1]}[2]-$forwardstrand{$temparr[0]}[2])<=$opt_disance);
my $total_evidence=0;
foreach my $temp2 (@temparr) {
my $temp3=$forwardstrand{$temp2}[0];
if (exists $readinfo{$temp3} and exists $readinfo{$temp3}{'unpaired'}) {
$total_evidence++;
}
}
if ($total_evidence==$numpairs) {
my $temp4=$forwardstrand{$temparr[-1]}[2]+($distave+$disstdev);
if ($temp4 < ($seqlength{$contigs}-$disstdev)) {
my $temp5=$idhash{$forwardstrand{$temparr[-1]}[0]}{$contigs}{$forwardstrand{$temparr[-1]}[1]}{'end'}+1;
unless (exists $badregions{$temp5} and $temp4<=$badregions{$temp5}) {
$badregions{$temp5}=$temp4;
}
}
}
}
# print "Test: Bad region1: \%badregions\n"; print Dumper \%badregions; print "\n"; ### For test ###
for (my $temp_x=0; $temp_x<(scalar(@{$revsref2})-$numpairs); $temp_x++) {
my @temparr=();
foreach (my $temp_y=$temp_x; $temp_y<($temp_x+$numpairs); $temp_y++) {
push (@temparr, ${$revsref2}[$temp_y]);
}
next unless (($reversestrand{$temparr[-1]}[2]-$reversestrand{$temparr[0]}[2])<=$opt_disance);
my $total_evidence=0;
foreach my $temp2 (@temparr) {
my $temp3=$reversestrand{$temp2}[0];
if (exists $readinfo{$temp3} and exists $readinfo{$temp3}{'unpaired'}) {
$total_evidence++;
}
}
if ($total_evidence==$numpairs) {
my $temp4=$reversestrand{$temparr[0]}[2] - ($distave+$disstdev);
if ($temp4 > $disstdev) {
my $temp5=$idhash{$reversestrand{$temparr[0]}[0]}{$contigs}{$reversestrand{$temparr[0]}[1]}{'end'}-1;
unless (exists $badregions{$temp4} and $temp5<=$badregions{$temp4}) {
$badregions{$temp4}=$temp5;
}
}
}
}
next unless (scalar(keys %badregions)>0);
# print "Test: Bad region2: \%badregions\n"; print Dumper \%badregions; print "\n"; ### For test ###
my $temphash2={};
$temphash2=&MergeRegion(\%badregions);
# print "Test: Good region: \$temphash1\n"; print Dumper $temphash1; print "\n"; ### For test ###
# print "Test: Bad region: \$temphash2\n"; print Dumper $temphash2; print "\n"; ### For test ###
my $finalhash={};
$finalhash=&BadDeductGood($temphash2, $temphash1);
# print "Test: $contigs length $seqlength{$contigs} Problematic region: \$finalhash\n"; print Dumper $finalhash; print "\n"; ### For test ###
next unless (scalar(keys %{$finalhash})>0);
$numContigs++;
foreach (sort {$a<=>$b} keys %{$finalhash}) {
print BREAKOUT $contigs, ':', $_, '-', ${$finalhash}{$_}, "\n";
$total_bases=$total_bases+${$finalhash}{$_}-$_+1;
$total_errors++;
}
}
close BREAKOUT;
print "##### SUMMARY 2 #####\n";
print " Good:\n";
print " Total number: $total_good_num\n";
print " Total bases: $total_good_length\n";
print " Error:\n";
print " Num errors: $total_errors\n";
print " Num contigs: $numContigs\n";
print " Num bases: $total_bases\n";
###
sub MergeRegion {
my $MRorginalhash=shift;
my %MRhash=();
my $MRtest=0;
my $laststart=0;
my $lastend=0;
foreach my $MRnum1 (sort {$a<=>$b} keys %{$MRorginalhash}) {
if ($MRtest==0) {
$laststart=$MRnum1;
$lastend=${$MRorginalhash}{$MRnum1};
$MRtest++;
}
else {
if ($MRnum1<=($lastend+1)) {
$lastend=${$MRorginalhash}{$MRnum1};
}
elsif ($MRnum1>($lastend+1)) {
$MRhash{$laststart}=$lastend;
$laststart=$MRnum1;
$lastend=${$MRorginalhash}{$MRnum1};
}
}
}
$MRhash{$laststart}=$lastend;
return \%MRhash;
}
### Global: $contigs
sub SortHash {
my $SHhash=shift;
# print "Test: Sort Hash\n"; print Dumper $SHhash; print "\n"; ### For test ###
my @SHarr=();
my %MRtemphash=();
foreach my $SHarrref (keys %{$SHhash}) {
$MRtemphash{${$SHhash}{$SHarrref}[2]}{$SHarrref}++;
}
foreach (sort {$a<=>$b} keys %MRtemphash) {
###Control repeats; TO BE CONTINUED
# my @SHarr2=sort {$a<=>$b} keys %{$MRtemphash{$_}};
#
# $contigs
foreach my $MRnum (keys %{$MRtemphash{$_}}) {
push (@SHarr, $MRnum);
}
}
# print "Test: Order \n"; print Dumper \@SHarr; print "\n"; ### For test ###
return \@SHarr;
}
sub BadDeductGood {
my ($BDGhash1, $BDGhash2)=@_;
my $BDGtest=0;
my $BDGsubinfo='SUB(BadDeductGood)';
BDGLOOP: while ($BDGtest==0) {
my $BDGoverlapnum=0;
# print $BDGsubinfo, "Test: \$BDGhash1\n"; print Dumper $BDGhash1; print "\n";
my @BDGarr1=sort {$a<=>$b} keys %{$BDGhash1};
foreach my $BDGbadstart (@BDGarr1) {
my $BDGbadend=${$BDGhash1}{$BDGbadstart};
foreach my $BDGgoodstart (sort {$a<=>$b} keys %{$BDGhash2}) {
my $BDGgoodend=${$BDGhash2}{$BDGgoodstart};
if ($BDGgoodend<$BDGbadstart or $BDGgoodstart>$BDGbadend) {
next;
}
else {
if ($BDGgoodstart<=$BDGbadstart and $BDGgoodend>=$BDGbadend) {
delete ${$BDGhash1}{$BDGbadstart}
}
elsif ($BDGgoodend<=$BDGbadend and $BDGgoodend>=$BDGbadstart) {
if ($BDGgoodstart>$BDGbadstart) {
${$BDGhash1}{$BDGbadstart}=$BDGgoodstart-1;
}
else {
delete ${$BDGhash1}{$BDGbadstart};
}
# if ($BDGgoodend<$BDGbadend) {
# if (exists ${$BDGhash1}{$BDGgoodend+1}) {
# die "($BDGsubinfo)Error: 111\n";
# }
# ${$BDGhash1}{$BDGgoodend+1}=$BDGbadend;
# }
}
elsif ($BDGgoodstart>=$BDGbadstart and $BDGgoodstart<=$BDGbadend) {
if ($BDGgoodstart>$BDGbadstart) {
${$BDGhash1}{$BDGbadstart}=$BDGgoodstart-1;
}
else {
delete ${$BDGhash1}{$BDGbadstart};
}
# if ($BDGgoodend<$BDGbadend) {
# if (exists ${$BDGhash1}{$BDGgoodend+1}) {
# die "($BDGsubinfo)Error: 222\n";
# }
# ${$BDGhash1}{$BDGgoodend+1}=$BDGbadend;
# }
}
else {
die "($BDGsubinfo)Error: unknow range Good $BDGgoodstart-$BDGgoodend Bad $BDGbadstart-$BDGbadend\n";
}
$BDGoverlapnum++;
next BDGLOOP;
}
}
}
if ($BDGoverlapnum==0) {
$BDGtest=1;
}
else {
$BDGtest=0;
}
}
return $BDGhash1;
}