This repository has been archived by the owner on Jan 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 90
/
bam2pindel.pl
executable file
·1062 lines (937 loc) · 32 KB
/
bam2pindel.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/env perl
########################################################################################################
# #
# CGP Software License #
# #
# Copyright (c) 2010 Genome Research Ltd. #
# Author: Cancer Genome Project, cgpit@sanger.ac.uk #
# #
# THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT #
# NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND #
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES #
# OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN #
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #
# #
# This code is free software; you can redistribute it and/or modify it under the terms of the BSD #
# License. #
# #
# Any redistribution or derivation in whole or in part including any substantial portion of this code #
# must include this copyright and permission notice. #
# #
########################################################################################################
use strict;
use English qw( -no_match_vars );
use warnings FATAL => 'all';
use Getopt::Long 'GetOptions';
use Data::Dumper;
use Carp;
use File::Path qw(make_path);
BEGIN {
use Cwd qw(abs_path);
my $prog_path = abs_path($0);
$prog_path =~ s/bam2pindel\.pl$/Adaptor.pm/;
my $adaptor_path;
if(-e $prog_path) {
$adaptor_path = $prog_path;
}
if($ENV{'BAM_2_PINDEL_ADAPT'}) {
$adaptor_path = $ENV{'BAM_2_PINDEL_ADAPT'};
}
if(!$adaptor_path) {
die "\nAdaptor.pm was not found in same location as bam2pindel.pl, nor was the Environment variable of BAM_2_PINDEL_ADAPT set with the path to your Adaptor.pm\n\n";
}
require $adaptor_path;
};
my $proper_pm_mask = 2; #0x0002
my $query_unmap_mask = 4; #0x0004
my $mate_unmap_mask = 8; #0x0008
my $strand_mask = 16; #0x0010 (set when reverse)
my $first_in_pair_mask = 64;
my $second_in_pair_mask = 128;
my %file_handles;
my %cent_coords;
my @record_store;
my $record_count = 0;
my %total_records; # used to check output file is complete
my $loops = 0; # for profiling
my $no_rec_in_write = 0;
my $file_count_loc;
my $bam_position = 0;
my $resume_pos = 0;
my $debug = 0;
{
my $options = option_builder();
rg_parse($options);
if($options->{'c'}) {
centromere_parser($options->{'c'});
}
if($options->{'q'}) {
qual_profile($options);
}
resume_run();
run_parser($options);
unlink $file_count_loc;
}
sub resume_run {
if(-e $file_count_loc && -s $file_count_loc) {
# first stat time file was created
my @stat_arr = stat($file_count_loc);
my $mtime = $stat_arr[9];
open my $RESUME, '<', $file_count_loc or croak "Unable to open $file_count_loc for reading";
while (my $line = <$RESUME>) {
chomp $line;
if($line =~ m/^(.*)\t(.*)\t([0-9]+)$/) {
# is a file, chr_arm and count
my $file = $1;
my $chr_arm = $2;
my $count = $3;
@stat_arr = stat($file);
if($stat_arr[9] > $mtime) {
# this indicates that a partial write occurred
# unable to resume from this
undef %file_handles;
undef %total_records;
$resume_pos = 0;
last;
}
$file_handles{$chr_arm}{'fn'} = $file;
$total_records{$file} = $count;
}
elsif($line =~ m/^([0-9]+)$/) {
# this is the bam position
$resume_pos = $1;
}
}
close $RESUME;
if($resume_pos != 0) {
print STDERR 'Able to resume at record ',$resume_pos,"\n";
}
else {
print STDERR "Unable to resume, starting from scratch\n";
}
}
return 1;
}
sub write_records {
my ($options, $input) = @_;
if($input) {
#print STDERR "start cache\n";
foreach my $rec(@{$input}) {
if($rec && @{$rec} > 0) {
my $chr_arm = get_chr_arm($options, $rec->[1], $rec->[2]);
if($file_handles{$chr_arm}{'data'}) {
push @{$file_handles{$chr_arm}{'data'}}, $rec->[0];
}
else {
$file_handles{$chr_arm}{'data'} = [$rec->[0]];
}
$record_count++;
}
}
#print STDERR "end cache\n";
}
if($record_count >= $options->{'mr'} || !$input) {
my $file_count_str = q{};
print STDERR 'Starting write of ',$record_count,' records',"\n";
foreach my $chr_arm(keys %file_handles) {
if($file_handles{$chr_arm}{'data'} && @{$file_handles{$chr_arm}{'data'}} > 0) {
my ($CHR_FH, $file_path) = get_chr_fh($options, $chr_arm);
foreach my $rec(@{$file_handles{$chr_arm}{'data'}}) {
print $CHR_FH $rec;
# keeps track of number of records that should be in each output file
if($total_records{$file_path}) {
$total_records{$file_path}++;
}
else {
$total_records{$file_path} = 1;
}
}
close $CHR_FH;
$file_count_str .= $file_path."\t".$chr_arm."\t".$total_records{$file_path}."\n";
$file_handles{$chr_arm}{'data'} = [];
}
}
$record_count = 0;
open my $FILE_COUNT, '>', $file_count_loc or croak "Unable to open for writing $file_count_loc";
print $FILE_COUNT $file_count_str or croak "Unable to write output to $file_count_loc";
print $FILE_COUNT $bam_position,"\n" or croak "Unable to write output to $file_count_loc";
close $FILE_COUNT or croak "Failed to close $file_count_loc";
print STDERR 'Finished writing records',"\n";
}
return;
}
sub centromere_parser {
my ($cent_file) = @_;
open my $CENTS, '<', $cent_file or croak "Could not open file $cent_file";
while (my $line=<$CENTS>) {
if(index($line,'#') == 0) {
next;
}
my @gff_bits = split /\t/, $line;
my $cent_chr = $gff_bits[0];
my $cent_start = $gff_bits[3];
my $cent_end = $gff_bits[4];
$cent_coords{$cent_chr} = [$cent_start, $cent_end];
}
close $CENTS;
return;
}
sub rg_parse {
my ($options) = @_;
# get the header only
my $command = find_prog('samtools').' view -H '.$options->{'i'};
my $pid = open my $PROC, '-|', $command or croak "Could not fork: $OS_ERROR";
while( my $tmp = <$PROC> ) {
if($tmp !~ /^\@RG/xs) {
next;
}
chomp $tmp;
my @rg_data = split /\t/xms, $tmp;
# have to find tag each time as merged data may have different tag positions
my $id_pos = tag_finder('ID', \@rg_data, 1); # no point parsing 0, as just @RG tag
my $pi_pos = tag_finder('PI', \@rg_data, 1); # no point parsing 0, as just @RG tag
if($pi_pos) {
if(!$options->{'rgs'}) {
$options->{'rgs'} = {};
}
my (undef, $id) = split /:/xms, $rg_data[$id_pos];
my (undef, $pi) = split /:/xms, $rg_data[$pi_pos];
next if($options->{'rg'} && $id != $options->{'rg'});
$options->{'rgs'}->{$id} = $pi;
}
}
close $PROC or croak 'Failed to close process: '.$command."\nERROR CODE: ".$CHILD_ERROR;
if(!$options->{'rgs'} && !$options->{'pi'}) {
croak q{No @RG header lines with PI tags detected, you must define '-pi'};
}
}
# will return false if not a name sorted bam
sub name_sorted {
my ($bam) = @_;
my $command = find_prog('samtools').' view -H '.$bam.q{ | grep -F '@HD' | grep -F 'SO:queryname'};
my $res = `$command`;
return length($res);
}
sub find_prog {
my ($prog) = @_;
my $prog_path = abs_path($0);
$prog_path =~ s/bam2pindel\.pl$/$prog/;
if(!-e $prog_path) {
undef $prog_path;
my @possible_locs = split ':', $ENV{'PATH'};
foreach my $path(@possible_locs) {
$prog_path = $path.'/'.$prog;
if(-e $prog_path) {
last;
}
undef $prog_path;
}
}
return $prog_path;
}
sub run_parser {
my ($options) = @_;
my $command = find_prog('samtools').' view ';
if($options->{'om'}) { # use oldmethod for parsing of bam file - SLOW
$command = find_prog('samtools').' view ';
}
elsif($options->{'nm'}) {
$command = find_prog('samgroupbyname').' -p ';
if($options->{'rg'}) {
$command .= '-r '.$options->{'rg'}.' ';
}
}
else {
# no method supplied - check bam is name sorted
if(!name_sorted($options->{'i'})) {
bam_sort_order_error($options->{'i'});
}
}
$command .= $options->{'i'};
print STDERR "Forked pipe to run: ",$command,"\n";
my $pid = open my $PROC, '-|', $command or croak "Could not fork: $OS_ERROR";
my @pair;
eval {
MAIN: while( my $tmp = <$PROC> ) {
last MAIN unless(defined $tmp);
if(index($tmp, '@') == 0) {
next;
}
$bam_position++;
next if($bam_position < $resume_pos);
my @records;
chomp $tmp;
my @comps = split /\t/, $tmp;
my $read_1 = Pindel::Bam::Adaptor->new(\@comps);
@pair = ($read_1);
# this forces a paired set of reads into @pair
my $paired = 0;
while($paired == 0) {
$tmp = <$PROC>;
last MAIN unless(defined $tmp);
$bam_position++;
chomp $tmp;
my @comps2 = split /\t/, $tmp;
my $read_2 = Pindel::Bam::Adaptor->new(\@comps2);
if($pair[0]->name() ne $read_2->name()) {
# at this point take the existing [0]
# read and run through single end recovery
if($pair[0]->mapped()) {
#debug('would run recovery on mapped unpaired read');
push @records, @{read_recovery($pair[0], $options)};
}
$pair[0] = $read_2;
}
else {
push @pair, $read_2;
$paired = 1;
}
}
my ($used_1, $used_2, $proc_res) = process_data(\@pair, $options);
if($used_1 == 1 || $used_2 == 1) {
push @records, @{$proc_res};
}
# run recovery over any read not used
if($used_1 == 0) {
push @records, @{read_recovery($pair[0], $options)};
}
if($used_2 == 0) {
push @records, @{read_recovery($pair[1], $options)};
}
write_records($options, \@records) if(@records > 0);
}
};
if($EVAL_ERROR) {
my $error = 'Error while processing: '.$command."\nEVAL ERROR: ".$EVAL_ERROR."\nCHILD ERROR: ".$CHILD_ERROR."\n\nReads in buffer:\n";
$error .= Dumper(\@pair);
$error .= "\nBroke at record $bam_position\n";
croak $error;
}
# close samtools view
close $PROC or croak 'Failed to close process: '.$command."\nERROR CODE: ".$CHILD_ERROR;
write_records($options);
check_output_files();
}
sub check_output_files {
my @files = keys %total_records;
foreach my $file(@files) {
my $res = `wc -l $file`;
chomp $res;
my ($count,undef) = split / /, $res;
if(! defined $count) {
croak 'failed to parse wc -l';
}
if($total_records{$file} != ($count/3)) {
croak 'Output file is corrupt, please re-run';
unlink $file_count_loc;
}
}
return 1;
}
sub get_chr_arm {
my ($options, $chr, $pos) = @_;
my $file_chr = $chr;
if($options->{'c'} && $chr ne 'MT') {
if($pos > $cent_coords{$chr}->[0]) {
$file_chr .= '-q';
}
else {
$file_chr .= '-p';
}
}
return $file_chr;
}
sub get_chr_fh {
my ($options, $file_chr) = @_;
my $CHR_FH;
my $file_path;
if(!$file_handles{$file_chr}{'fn'}) {
$file_path = $options->{'o'}.'_'.$file_chr.'.txt';
open $CHR_FH, '>', $file_path or croak 'Could not create file: '.$file_path;
$file_handles{$file_chr}{'fn'} = $file_path;
#$file_handles{$file_chr}{'count'} = 1;
print STDERR 'First open: ',$file_path," (1)\n";
}
else {
$file_path = $file_handles{$file_chr}{'fn'};
open $CHR_FH, '>>', $file_path or croak 'Could not append to file: '.$file_path;
#$file_handles{$file_chr}{'count'}++;
#print STDERR 'Reopened: ',$file_path,' (',$file_handles{$file_chr}{'count'},")\n";
}
return ($CHR_FH, $file_path);
}
sub process_data {
my ($read_pair, $options) = @_;
my ($used_1, $used_2) = (0,0);
# setup for basic short circuits to skip readpairs
{
if($options->{'r'} && $read_pair->[0]->rname() ne $options->{'r'} && $read_pair->[1]->rname() ne $options->{'r'}) {
return ($used_1, $used_2);
}
}
my $read_1 = $read_pair->[0];
my $read_2 = $read_pair->[1];
#if($debug) {
#debug("\n");
##debug(join "\t", @{$read_1->sam()});
##debug(join "\t", @{$read_2->sam()});
#debug(Dumper($read_1));
#debug(Dumper($read_2));
#}
if(!$read_1->mapped() && !$read_2->mapped()) { # both reads unmapped
#debug($read_pair->[0]->name().' Both reads unmapped');
return ($used_1, $used_2);
}
# skip if both are SW
if($read_1->sw() && $read_2->sw()) {
#debug($read_pair->[0]->name().' Both reads are SW');
return ($used_1, $used_2);
}
# skip if both are unique with no edit distanct
# NOTE: added NM check as hides all small indels otherwise
if($read_1->unique() && $read_2->unique() && $read_1->edits() == 0 && $read_2->edits() == 0) {
#debug($read_pair->[0]->name().' Both reads are unique with no edits');
return ($used_1, $used_2);
}
# skip if both are suboptimal
if($read_1->suboptimal() && $read_2->suboptimal()) {
#debug($read_pair->[0]->name().' Both reads suboptimal');
return ($used_1, $used_2);
}
my $max_edit = int ($read_1->read_length() / $options->{'e'}) + 1;
# to be useful for Pindel at least one read must have full match with max of 2 edits
if($read_1->edits() > $max_edit && $read_2->edits() > $max_edit) {
#debug($read_pair->[0]->name().' Both reads have more than '.$max_edit.' edits (allowing 1 edit per '.$options->{'e'}.' bps + 1)');
return ($used_1, $used_2);
}
my $uniq;
if($read_1->mapped() && $read_2->mapped()) {
if($read_1->unique() && $read_1->edits() <= $max_edit && $read_2->edits() > 0) {
$uniq = 1;
}
elsif($read_2->unique() && $read_2->edits() <= $max_edit && $read_1->edits() > 0) {
$uniq = 2;
}
else {
#debug($read_pair->[0]->name().' Both reads mapped at sufficient accuracy, edit distance not useful');
return ($used_1, $used_2);
}
}
elsif(!$read_1->mapped() && $read_2->edits() <= $max_edit) {
$uniq = 2;
}
elsif(!$read_2->mapped() && $read_1->edits() <= $max_edit) {
$uniq = 1;
}
else {
#debug($read_pair->[0]->name().' One read mapped at sufficient accuracy, but nm for anchor is >'.$max_edit.' edits (allowing 1 edit per '.$options->{'e'}.' bps + 1)');
return ($used_1, $used_2);
}
my @records;
if($uniq == 1) {
#debug('Read 1 is anchor');
my $ref_a = build_record($read_1, $read_2, $options);
if($ref_a) {
push @records, $ref_a;
$used_2 = 1;
}
if($read_2->unique() && $read_2->edits() <= $max_edit && $read_1->edits() > 0) {
#debug('Read 2 is also anchor');
my $ref = build_record($read_2, $read_1, $options);
if($ref) {
push @records, $ref;
$used_1 = 1;
}
}
}
elsif($uniq == 2) {
#debug('Read 2 is anchor');
my $ref = build_record($read_2, $read_1, $options);
if($ref) {
push @records, $ref;
$used_1 = 1;
}
}
return ($used_1, $used_2, \@records);
}
sub read_recovery {
my ($read_1, $options) = @_;
my @results;
if($options->{'r'} && $read_1->rname() ne $options->{'r'}) {
return \@results;
}
# read_1 should be the read not previously used as a query
#debug(Dumper($read_1));
# try to use read_1 without ref to read 2
if($read_1->mapped()) {
if(cigar_is_clean($read_1->cigar())) {
#debug('clean cigar in read recovery, no use to pindel');
return \@results;
}
### new addition to try and reduce false positives
if($read_1->sw()) {
#debug('sw in read recovery, no use to pindel');
return \@results;
}
if($read_1->suboptimal()) {
#debug('suboptimal in recovery, no use to pindel');
return \@results;
}
my @tmp_sam = @{$read_1->sam()}; # no longer a reference as would break stuff
my $pi;
if($options->{'pi'}) {
$pi = $options->{'pi'};
}
else {
$pi = $options->{'rgs'}{$read_1->read_group()};
}
if($read_1->strand()) { # +ve
# make negative
$tmp_sam[1] += 16;
# as now negative + pi *1.2;
$tmp_sam[3] += int($pi*1.2);
}
else {
$tmp_sam[1] -= 16;
# as now positive - pi *1.2;
$tmp_sam[3] -= int($pi*1.2);
}
my $fake_anchor = Pindel::Bam::Adaptor->new(\@tmp_sam);
#debug(Dumper($fake_anchor));
#debug('fake anchor used');
push @results, build_record($fake_anchor, $read_1, $options);
return \@results;
}
else {
#debug('Unmapped in recovery, discard');
return \@results;
}
# not actually possible to get here
return \@results;
}
# meaning all M
sub cigar_is_clean {
my $cigar = shift;
$cigar =~ s/[0-9]//g;
if($cigar eq 'M') {
return 1;
}
return 0;
}
sub trim_on_qual {
my ($seq, $quals_in, $thresh, $flip) = @_;
my $trim_at = 0;
my @quals;
if($flip) {
$seq = reverse $seq;
@quals = reverse @{$quals_in};
}
my $i = 0;
for(; $i<@quals-1; $i++) {
#print "$i - ",$quals[$i],"\n";
if($quals[$i] > $thresh && $quals[$i+1] > $thresh) {
#print "Threshold found";
last;
}
}
#print $i,"\n";
#print $seq,"\n";
my $new_seq;
#if($i >= 4) {
$new_seq = substr $seq, $i;
if(length $new_seq < ((length $seq) * 0.4) ) {
return undef;
}
#}
#else {
# $new_seq = $seq;
#}
#print $new_seq,"\n";
if($flip) {
$new_seq = reverse $new_seq;
#print $new_seq,"\n\n";
}
return $new_seq;
}
sub unmapped_seq {
my ($options, $unmap_read) = @_;
my $seq = $unmap_read->sam()->[9];
# this component added to apply quality trimming
if($options->{'q'}) {
my $quals = qual_to_vals($unmap_read->sam()->[10]);
my $qual_thresh = $options->{'rg_profile'}->{$unmap_read->read_group}->{'qual_thresh'};
if($quals->[0] < $qual_thresh) {
$seq = trim_on_qual($seq, $quals, $qual_thresh, 0);
}
elsif($quals->[-1] < $qual_thresh) {
$seq = trim_on_qual($seq, $quals, $qual_thresh, 1);
}
if(!$seq) {
return $seq;
}
}
if(!$unmap_read->strand()) {
$seq =~ tr/ACGT/TGCA/;
$seq = reverse $seq;
}
if(!$unmap_read->mapped() || (index($unmap_read->sam()->[5], 'S') > -1)) {
$seq =~ s/^N+//;
$seq =~ s/N+$//;
}
return $seq;
}
sub length_correct {
my ($map_read) = @_;
my $length = 0;
my ($seq, $cigar) = ($map_read->sam()->[9], $map_read->sam()->[5]);
my @cigar_lengths = split /[A-Z]+/, $cigar;
my @cigar_operations = split /[0-9]+/, $cigar;
shift @cigar_operations; # will always have empty first element;
if(@cigar_lengths != @cigar_operations) {
print STDERR Dumper($map_read);
croak 'Cigar string has unmatched lengths and operators';
}
if(@cigar_lengths == 1) {
if($cigar_lengths[0] == length $seq) {
$length = $cigar_lengths[0];
}
else {
print STDERR Dumper($map_read);
croak 'Cigar string has no variation yet does not match sequence length';
}
}
else {
for my $i(0..@cigar_lengths-1) {
if($cigar_operations[$i] eq 'M') {
$length += $cigar_lengths[$i];
}
elsif($cigar_operations[$i] eq 'I') {
$length += $cigar_lengths[$i];
}
elsif($cigar_operations[$i] eq 'S') {
$length += $cigar_lengths[$i];
}
elsif($cigar_operations[$i] eq 'D') {
$length -= $cigar_lengths[$i];
}
else {
$debug = 1;
#debug('CIGAR: '.$cigar.' seq length: '.length $seq);
#debug('Operation Length:', Dumper(\@cigar_lengths));
#debug('Operation Type:', Dumper(\@cigar_operations));
croak 'I only understand M,I,S,D for length correction';
}
}
}
return $length;
}
sub trim_n_from_ends {
my ($seq) = @_;
$seq =~ s/^N+//;
$seq =~ s/N+$//;
my $seq_len = length $seq; # seq withouth runs of N's at each end
my $n_in_seq = $seq =~ tr/N//; # returns count of N's within remaning seq
return ($seq_len, $n_in_seq);
}
sub build_record {
my ($map_read, $unmap_read, $options) = @_;
my $pindel_rec;
my @extended_rec;
my $sequence = unmapped_seq($options, $unmap_read);
if(!$sequence) {
return \@extended_rec;
}
my $read_name = $map_read->name();
my ($seq_len_unmap, $n_in_seq_unmap) = trim_n_from_ends($unmap_read->sam()->[9]);
my ($seq_len_map, $n_in_seq_map) = trim_n_from_ends($map_read->sam()->[9]);
my $max_n_unmap = int ($seq_len_unmap * $options->{'mn'});
my $max_n_map = int ($seq_len_map * $options->{'mn'});
if($seq_len_unmap >= $options->{'ms'} && $n_in_seq_unmap <= $max_n_unmap
&& $seq_len_map >= $options->{'ms'} && $n_in_seq_map <= $max_n_map) {
my $length = -1; # pindel is 0 based not 1 based so coords of +ve mappings will be -1 to SAM record
if(!$map_read->strand()) {
# -1 so return the location of the last base not the one after
$length = length_correct($map_read) - 1;
}
my $pi_val;
if(!$map_read->read_group() && !$options->{'pi'}) {
print STDERR Dumper($map_read);
my $error = "Readpair does not have a RG field and no default PI has been specified\n\n";
$error .= join "\t", @{$map_read->sam()};
$error .= "\n";
$error .= join "\t", @{$unmap_read->sam()};
$error .= "\n";
croak $error;
}
else {
$pi_val = $options->{'pi'};
}
if(!$pi_val) {
if($options->{'rgs'}->{$map_read->read_group()}) {
$pi_val = $options->{'rgs'}->{$map_read->read_group()};
}
elsif($options->{'rgs'}) {
my $error = 'Header @RG line does not include a PI value for this readpairs readgroup'."\n\n";
$error .= join "\t", @{$map_read->sam()};
$error .= "\n";
$error .= join "\t", @{$unmap_read->sam()};
$error .= "\n";
croak $error;
}
}
if($map_read->sam()->[2] eq $unmap_read->sam()->[2]
&& $map_read->sam()->[3] != $unmap_read->sam()->[3]
&& abs($map_read->sam()->[8]) < $pi_val) {
# fix for reads that map closely together
if($map_read->strand()) {
$length -= $pi_val;
}
else {
$length += $pi_val;
}
}
my $read_num;
if(($unmap_read->sam()->[1] | $first_in_pair_mask) == $unmap_read->sam()->[1]) {
$read_num = 1;
}
elsif(($unmap_read->sam()->[1] | $second_in_pair_mask) == $unmap_read->sam()->[1]) {
$read_num = 2;
}
if($unmap_read->sam()->[1] == $map_read->sam()->[1]) {
if($read_num == 1) {
$read_num = 2;
}
else {
$read_num = 1;
}
}
$pindel_rec = '@'.$unmap_read->sam()->[0].'/'.$read_num."\n"; # the readname of unmapped record
$pindel_rec .= $sequence."\n"; # sequence of unmapped read
if($map_read->strand()) {
$pindel_rec .= '+';
}
else {
$pindel_rec .= '-';
}
$pindel_rec .= "\t".$map_read->sam()->[2]."\t"; # refseq name
my $calc_pos = ($map_read->sam()->[3] + $length);
if($calc_pos < 1) {
$calc_pos = 1;
}
$pindel_rec .= $calc_pos."\t".$map_read->sam()->[4]."\t"; # position, quality
$pindel_rec .= $pi_val."\t"; # predicted insert size
$pindel_rec .= $options->{'s'}."\n"; # sample tag
}
#else {
#debug($unmap_read->sam()->[0].' more than allowed Ns in trimmed sequence (allowing max '.($options->{'mn'}*100).'%) or hard limit of remaning sequence reached - '.$options->{'ms'});
#}
if($pindel_rec) {
@extended_rec = ($pindel_rec, $map_read->sam()->[2], $map_read->sam()->[3]);
}
return \@extended_rec;
}
sub tag_finder {
my ($tag, $comp_ref, $start_at) = @_;
my $tag_pos;
for my $i($start_at..@{$comp_ref}-1) {
my @tag = split ":", $comp_ref->[$i];
if($tag[0] eq $tag) {
$tag_pos = $i;
last;
}
}
return $tag_pos;
}
sub qual_profile {
my ($options) = @_;
my %group_profiles;
#$options->{'rgs'}->{$id};
#map{ord($_)- 33 }
foreach my $rg_id(keys %{$options->{'rgs'}}) {
$group_profiles{$rg_id}->{'qual_sum'} = 0;
$group_profiles{$rg_id}->{'base_count'} = 0;
# must be mapped and in specified rg
my $command = find_prog('samtools').' view -F 4 -r '.$rg_id.' '.$options->{'i'}; #.' | cut -f 11 | head -n 500000';
my $pid = open my $PROC, '-|', $command or croak "Could not fork: $OS_ERROR";
my $count = 0;
while( my $sam = <$PROC> ) {
chomp $sam;
my $tmp = (split /\t/,$sam)[10];
#print $tmp,"\n";
my $quals = qual_to_vals($tmp);
#print join ',',@quals;
#print "\n\n";
foreach my $qual(@{$quals}) {
$group_profiles{$rg_id}->{'qual_sum'} += $qual;
$group_profiles{$rg_id}->{'base_count'}++;
}
$count++;
last if($count >= 500_000);
}
close $PROC;# or croak 'Failed to close process, '.$command;
if($count < 250_000) {
warn 'Insufficient reads in RG '.$rg_id.' to create a profile for quality trimming, defaulting to 8';
$group_profiles{$rg_id}->{'qual_thresh'} = 8;
}
else {
$group_profiles{$rg_id}->{'qual_thresh'} = ($group_profiles{$rg_id}->{'qual_sum'} / $group_profiles{$rg_id}->{'base_count'}) * 0.3;
}
}
print Dumper(\%group_profiles);
$options->{'rg_profile'} = \%group_profiles;
return 1;
}
sub qual_to_vals {
my ($qual) = @_;
my @vals = map{ord($_)- 33 } split //, $qual;
return \@vals;
}
sub option_builder {
my ($factory) = @_;
my %opts = ();
my $result = GetOptions (
'i|input=s' => \$opts{'i'},
'o|output=s' => \$opts{'o'},
's|sample=s' => \$opts{'s'},
'c|centromeres=s' => \$opts{'c'},
'om|oldmethod' => \$opts{'om'},
'nm|newmethod' => \$opts{'nm'},
'pi|insert=n' => \$opts{'pi'},
'r|restrict=s' => \$opts{'r'},
'rg|readgroup=s' => \$opts{'rg'},
'd|debug' => \$opts{'d'},
'h|help' => \$opts{'h'},
'mn|max_n=f' => \$opts{'mn'},
'ms|min_seq=n' => \$opts{'ms'},
'e|edits=n' => \$opts{'e'},
'mr|max_rec=n' => \$opts{'mr'},
'q|qual_trim' => \$opts{'q'},
);
if(!$result || !$opts{'i'} || !$opts{'o'} || !$opts{'s'} || $opts{'h'}) {
if($opts{'h'}) {
licence();
}
usage();
}
if($opts{'d'}) {
$debug = 1;
}
if(!$opts{'mn'}) {
$opts{'mn'} = 0.1;
}
if(!$opts{'ms'}) {
$opts{'ms'} = 22;
}
if(!$opts{'e'}) {
$opts{'e'} = 20;
}
if(!$opts{'mr'}) {
$opts{'mr'} = 200000;
}
if($opts{'rg'}) {
# need to modify the output path
my @bits = split /\//, $opts{'o'};
unshift @bits, '.' if(@bits == 1);
my $stub = pop @bits;
my $full_path = join '/', @bits;
$full_path .= '/'.$opts{'rg'}.'/';
make_path($full_path);
$full_path .= $stub;
$opts{'o'} = $full_path;
}
$file_count_loc = $opts{'o'}.'_write.count';
return \%opts;
}
## UTILITIES
sub debug {
my @data = @_;
if($debug) {
foreach my $item(@data) {
warn $item."\n";
}
}
return;
}
sub bam_sort_order_error {
my ($file) = @_;
print STDERR "Error while processing input: $file\n";
print STDERR <<'BAM_SORT_ORDER_MESSAGE';
Input file is not 'queryname' sorted, explained below.
No preference for parsing method has been defined so the default of 'samtools view'
was applied. Inorder to use this method your input file should be sorted by queryname.
If you are confident that the file is appropriately sorted (but missing the appropriate
header tag) you can suppress this error by adding '-om' to the command.
------------------
ALTERNATIVE OPTION
------------------
'-nm' option can be applied if you have 'samgroupbyname' installed.
At time of writing this is not publically available, however please check with the
authors if this has been released in the interim (-h for contact details).
BAM_SORT_ORDER_MESSAGE
exit 1;
}
sub licence {
print <<'LICENCE_DOC';
########################################################################################################
# #
# CGP Software License #
# #
# Copyright (c) 2010 Genome Research Ltd. #
# Author: Cancer Genome Project, cgpit@sanger.ac.uk #
# #
# THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT #
# NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND #
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES #
# OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN #
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #
# #
# This code is free software; you can redistribute it and/or modify it under the terms of the BSD #