-
Notifications
You must be signed in to change notification settings - Fork 20
/
tag2sort
executable file
·1315 lines (1072 loc) · 42.2 KB
/
tag2sort
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
use warnings;
use strict;
$|++;
use Getopt::Long;
use Cwd;
use FindBin qw($Bin);
use lib "$Bin/../lib";
## This program is Copyright (C) 2014-23, Felix Krueger ([email protected])
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
my %counting;
my %fhs;
my $SNPsplit_version = '0.6.0';
my %yaml;
my $yaml_count = 0;
my ($paired,$hic,$verbose,$samtools_path,$bam,$output_dir,$conflict,$singletons,$parent_dir) = process_commandline();
my ($unassigned,$genome1,$genome2,$conflicting,$snp_found,$no_snp_found,$bizarre,$no_snp);
my $report_file;
foreach my $file (@ARGV){
warn "\nSummary of parameters for SNPsplit-sort:\n";
warn '='x40,"\n";
warn "SNPsplit tagged infile:\t\t$file\n";
$yaml{$yaml_count}->{tagged_infile} = $file;
++$yaml_count;
warn "Output directory:\t\t>$output_dir<\n";
warn "Parent directory:\t\t>$parent_dir<\n";
warn "Samtools path:\t\t\t$samtools_path\n";
if ($bam){
warn "Output format:\t\t\tBAM (default)\n";
}
else{
warn "Output format:\t\t\tSAM\n";
}
if ($hic){
warn "Input format:\t\t\tHi-C (by definition paired-end)\n";
}
else{
if ($paired){
if ($singletons){
warn "Input format:\t\t\tPaired-end (Singleton alignments will written to extra files)\n";
}
else{
warn "Input format:\t\t\tPaired-end\n";
}
}
else{
warn "Input format:\t\t\tSingle-End\n";
}
}
warn "\n\n";
sleep (1);
my $outfile;
if ($hic){
process_HiC_paired_end($file);
}
elsif ($paired){
princess_paired_end($file);
}
else{
process_single_end($file);
}
close G1;
close G2;
close UNASSIGNED;
if ($conflict){
close CONFLICT;
}
# returning to SNPsplit-tag, or end otherwise
warn "Sorting finished successfully\n\n";
# Print YAML report
print_YAML();
close YAML or warn "Had trouble closing YAML filehandle: $!\n";
}
sub print_YAML{
# warn "-Sorting:\n";
foreach my $entry (sort {$a<=>$b} keys %yaml){
foreach my $stat(keys %{$yaml{$entry}}){ # should only be 1 stat per entry
# warn "Entry: $entry\t$stat: $yaml{$entry}->{$stat}\n";
print YAML "$stat: $yaml{$entry}->{$stat}\n";
}
}
}
sub process_single_end{
my $file = shift;
open (IN,"samtools view -h ${output_dir}$file |") or die "Failed to read from BAM file ${output_dir}$file: $!\n";
warn "Now processing input file <<< $file >>>\n\n";
my $outfile = open_output_filehandles($file);
### READING FROM MAPPING BAM FILE
($genome1,$genome2,$conflicting,$unassigned) = (0,0,0,0);
my $count = 0;
while (<IN>){
if ($_ =~ /^\@/){ # header lines
print UNASSIGNED;
print G1;
print G2;
if ($conflict){
print CONFLICT;
}
next;
}
++$count;
if ($count%1000000 == 0){
warn "Processed $count lines so far\n";
}
if ($verbose){
print $_;
}
my $XX_tag = $1 if ($_ =~ /XX:Z:(.+?)\s+/); # allele-specificity flag
unless ($XX_tag){
die "Failed to extract XX:Z tag from line:\n$_\n";
}
if ($XX_tag eq 'UA'){
++$unassigned; # unassigned
print UNASSIGNED;
}
elsif($XX_tag eq 'G1'){
$genome1++; # genome 1-specific
print G1;
}
elsif($XX_tag eq 'G2'){
$genome2++; # genome 2-specific
print G2;
}
elsif($XX_tag eq 'CF'){
++$conflicting; # conflicting read, something weird was going on
if ($conflict){
print CONFLICT;
}
}
else{
die "The read did not have an expected XX-tag:\n$_\n\n";
}
}
### Printing Summary reports of the stats on a per-read basis
my ($perc_genome1,$perc_genome2,$perc_conflicting,$perc_unassigned);
if ($count == 0){
$perc_unassigned = $perc_genome1 = $perc_genome2 = $perc_conflicting = 'N/A';
}
else{
$perc_unassigned = sprintf ("%.2f",$unassigned*100/$count);
$perc_genome1 = sprintf ("%.2f",$genome1*100/$count);
$perc_genome2 = sprintf ("%.2f",$genome2*100/$count);
$perc_conflicting = sprintf ("%.2f",$conflicting*100/$count);
}
warn "\n\nAllele-specific single-end sorting report\n",'='x41,"\n";
warn "Read alignments processed in total:\t\t$count\n";
warn "Reads were unassignable:\t\t\t$unassigned ($perc_unassigned%)\n";
warn "Reads were specific for genome 1:\t\t$genome1 ($perc_genome1%)\n";
warn "Reads were specific for genome 2:\t\t$genome2 ($perc_genome2%)\n";
warn "Reads contained conflicting SNP information:\t$conflicting ($perc_conflicting)\n\n\n";
print REPORT "\n\nAllele-specific single-end sorting report\n",'='x41,"\n";
print REPORT "Read alignments processed in total:\t\t$count\n";
print REPORT "Reads were unassignable:\t\t\t$unassigned ($perc_unassigned%)\n";
print REPORT "Reads were specific for genome 1:\t\t$genome1 ($perc_genome1%)\n";
print REPORT "Reads were specific for genome 2:\t\t$genome2 ($perc_genome2%)\n";
print REPORT "Reads contained conflicting SNP information:\t$conflicting ($perc_conflicting)\n\n\n";
# total reads
$yaml{$yaml_count}->{SE_total_reads} = $count;
$yaml_count++;
# unassignable
$yaml{$yaml_count}->{SE_unassignable} = $unassigned;
$yaml_count++;
$yaml{$yaml_count}->{SE_percent_unassignable} = $perc_unassigned;
$yaml_count++;
# genome 1
$yaml{$yaml_count}->{SE_genome1} = $genome1;
$yaml_count++;
$yaml{$yaml_count}->{SE_percent_genome1} = $perc_genome1;
$yaml_count++;
# genome 2
$yaml{$yaml_count}->{SE_genome2} = $genome2;
$yaml_count++;
$yaml{$yaml_count}->{SE_percent_genome2} = $perc_genome2;
$yaml_count++;
# conflicting
$yaml{$yaml_count}->{SE_conflicting} = $conflicting;
$yaml_count++;
$yaml{$yaml_count}->{SE_percent_conflicting} = $perc_conflicting;
$yaml_count++;
}
sub process_HiC_paired_end{
my $file = shift;
open (IN,"samtools view -h ${output_dir}$file |") or die "Failed to read from BAM file $file: $!\n";
open_output_filehandles($file);
### READING FROM MAPPING BAM FILE
# Here we don't need to keep track of chromosome etc but can simply proceed line by line
my ($g1_ua , $ua_g1 , $g2_ua , $ua_g2, $g1_g2 , $g2_g1) = (0,0,0,0,0,0);
my ($g1_g1 , $g2_g2) = (0,0);
($unassigned,$conflicting) = (0,0);
my $count = 0;
my $last_id;
my $last_line;
while (<IN>){
if ($_ =~ /^\@/){ # header lines
print UNASSIGNED;
print G1;
print G2;
if ($conflict){
print CONFLICT;
}
print G1UA;
print G2UA;
print G1G2;
next;
}
my ($id) = (split /\t/)[0];
unless (defined $last_id){
# warn "Setting last_id to $id\n";
$last_id = $id;
$last_line = $_;
next; ### need to process last ID upon exiting the while loop
}
### Else, read ID has been set already. Determining if the last read and this read are part of a read pair. This is essential for HiCUP Hi-C data
if ($id eq $last_id){
# warn "Paired end read\n$last_id\n$id\n";
++$count; # increasing counter for read pair
my $XX_tag_1 = $1 if ($last_line =~ /XX:Z:(.+?)\s+/);
unless ($XX_tag_1){
die "Failed to extract XX:Z tag from line:\n$last_line\n";
} # warn "XX tag 1: $XX_tag_1\n";sleep(1);
my $XX_tag_2 = $1 if ($_ =~ /XX:Z:(.+?)\s+/);
unless ($XX_tag_2){
die "Failed to extract XX:Z tag from line:\n$_\n";
} # warn "XX tag 2: $XX_tag_2\n";sleep(1);
### Processing the allele-specificity of the read pair as a whole
if ( $XX_tag_1 eq 'UA' and $XX_tag_2 eq 'UA' ){
++$unassigned; # unassigned
print UNASSIGNED $last_line;
print UNASSIGNED;
}
elsif( $XX_tag_1 eq 'G1' and $XX_tag_2 eq 'G1' ){
$genome1++; # genome 1-specific
print G1 $last_line;
print G1;
}
elsif( $XX_tag_1 eq 'G2' and $XX_tag_2 eq 'G2' ){
$genome2++; # genome 2-specific
print G2 $last_line;
print G2;
}
elsif( $XX_tag_1 eq 'CF' or $XX_tag_2 eq 'CF'){
++$conflicting; # conflicting read, something weird was going on
if ($conflict){
print CONFLICT $last_line;
print CONFLICT;
}
}
elsif ($XX_tag_1 eq 'G1' and $XX_tag_2 eq 'UA' ){
++$g1_ua;
print G1UA $last_line;
print G1UA;
}
elsif ($XX_tag_1 eq 'UA' and $XX_tag_2 eq 'G1' ){
++$ua_g1;
print G1UA $last_line;
print G1UA;
}
elsif ($XX_tag_1 eq 'G2' and $XX_tag_2 eq 'UA' ){
++$g2_ua;
print G2UA $last_line;
print G2UA;
}
elsif ($XX_tag_1 eq 'UA' and $XX_tag_2 eq 'G2' ){
++$ua_g2;
print G2UA $last_line;
print G2UA;
}
elsif ($XX_tag_1 eq 'G1' and $XX_tag_2 eq 'G2' ){
++$g1_g2;
print G1G2 $last_line;
print G1G2;
}
elsif ($XX_tag_1 eq 'G2' and $XX_tag_2 eq 'G1' ){
++$g2_g1;
print G1G2 $last_line;
print G1G2;
}
else{
die "The read pairs did not have an expected XX-tag\nXX-tag 1: $XX_tag_1\nXX-tag 2: $XX_tag_2\n\n";
}
# warn "Resetting last_id after processing the paired-end read\n\n"; sleep(1);
$last_id = $last_line = undef; # resetting the last id and alignment
}
else{
die "Hi-C data has to be paired-end by definition, however the reads did not have the same read ID:\nRead 1: $last_line\nRead 2: $_\n\n";
}
if ($count%1000000 == 0){
warn "Processed $count lines so far\n";
}
}
if (defined $last_id){
warn "Last read was a singleton, skipping...\n";
}
### Printing Summary reports
my $g1_ua_total = $g1_ua + $ua_g1;
my $g2_ua_total = $g2_ua + $ua_g2;
my $g1_g2_total = $g1_g2 + $g2_g1;
my ($perc_unassigned,$perc_genome1,$perc_genome2,$perc_conflicting,$perc_g1_ua,$perc_g2_ua,$perc_g1_g2);
if ($count == 0){
$genome1 = $genome2 = 0;
$perc_unassigned = $perc_genome1 = $perc_genome2 = $perc_conflicting = $perc_g1_ua = $perc_g2_ua = $perc_g1_g2 = 'N/A';
}
else{
$perc_unassigned = sprintf ("%.2f",$unassigned *100/$count);
$perc_genome1 = sprintf ("%.2f",$genome1*100/$count);
$perc_genome2 = sprintf ("%.2f",$genome2*100/$count);
$perc_conflicting = sprintf ("%.2f",$conflicting*100/$count);
$perc_g1_ua = sprintf ("%.2f",$g1_ua_total*100/$count);
$perc_g2_ua = sprintf ("%.2f",$g2_ua_total*100/$count);
$perc_g1_g2 = sprintf ("%.2f",$g1_g2_total*100/$count);
}
warn "\n\nAllele-specific paired-end sorting report\n",'='x41,"\n";
warn "Read pairs processed in total:\t\t\t\t$count\n";
warn "Read pairs were unassignable (UA/UA):\t\t\t$unassigned ($perc_unassigned%)\n";
warn "Read pairs were specific for genome 1 (G1/G1):\t\t$genome1 ($perc_genome1%)\n";
warn "Read pairs were specific for genome 2 (G2/G2):\t\t$genome2 ($perc_genome2%)\n";
warn "Read pairs were a mix of G1 and UA:\t\t\t$g1_ua_total ($perc_g1_ua%). Of these,\n";
warn "\t\t\twere G1/UA: $g1_ua\n";
warn "\t\t\twere UA/G1: $ua_g1\n";
warn "Read pairs were a mix of G2 and UA:\t\t\t$g2_ua_total ($perc_g2_ua%). Of these,\n";
warn "\t\t\twere G2/UA: $g2_ua\n";
warn "\t\t\twere UA/G2: $ua_g2\n";
warn "Read pairs were a mix of G1 and G2:\t\t\t$g1_g2_total ($perc_g1_g2%). Of these,\n";
warn "\t\t\twere G1/G2: $g1_g2\n";
warn "\t\t\twere G2/G1: $g2_g1\n";
warn "Read pairs contained conflicting SNP information:\t$conflicting ($perc_conflicting%)\n\n";
print REPORT "\n\nAllele-specific Hi-C paired-end sorting report\n",'='x41,"\n";
print REPORT "Read pairs processed in total:\t\t\t\t$count\n";
print REPORT "Read pairs were unassignable (UA/UA):\t\t\t$unassigned ($perc_unassigned%)\n";
print REPORT "Read pairs were specific for genome 1 (G1/G1):\t\t$genome1 ($perc_genome1%)\n";
print REPORT "Read pairs were specific for genome 2 (G2/G2):\t\t$genome2 ($perc_genome2%)\n";
print REPORT "Read pairs were a mix of G1 and UA:\t\t\t$g1_ua_total ($perc_g1_ua%). Of these,\n";
print REPORT "\t\t\twere G1/UA: $g1_ua\n";
print REPORT "\t\t\twere UA/G1: $ua_g1\n";
print REPORT "Read pairs were a mix of G2 and UA:\t\t\t$g2_ua_total ($perc_g2_ua%). Of these,\n";
print REPORT "\t\t\twere G2/UA: $g2_ua\n";
print REPORT "\t\t\twere UA/G2: $ua_g2\n";
print REPORT "Read pairs were a mix of G1 and G2:\t\t\t$g1_g2_total ($perc_g1_g2%). Of these,\n";
print REPORT "\t\t\twere G1/G2: $g1_g2\n";
print REPORT "\t\t\twere G2/G1: $g2_g1\n";
print REPORT "Read pairs contained conflicting SNP information:\t$conflicting ($perc_conflicting%)\n\n";
### YAML
# total reads
$yaml{$yaml_count}->{HiC_total_pairs} = $count;
$yaml_count++;
# unassignable
$yaml{$yaml_count}->{HiC_unassignable_UA_UA} = $unassigned;
$yaml_count++;
$yaml{$yaml_count}->{HiC_percent_unassignable_UA_UA} = $perc_unassigned;
$yaml_count++;
# genome 1
$yaml{$yaml_count}->{HiC_genome1_G1_G1} = $genome1;
$yaml_count++;
$yaml{$yaml_count}->{HiC_percent_genome1_G1_G1} = $perc_genome1;
$yaml_count++;
# genome 2
$yaml{$yaml_count}->{HiC_genome2_G2_G2} = $genome2;
$yaml_count++;
$yaml{$yaml_count}->{HiC_percent_genome2_G2_G2} = $perc_genome2;
$yaml_count++;
# G1 / UA
$yaml{$yaml_count}->{HiC_G1_UA_total} = $g1_ua_total;
$yaml_count++;
$yaml{$yaml_count}->{HiC_percent_G1_UA_total} = $perc_g1_ua;
$yaml_count++;
$yaml{$yaml_count}->{HiC_G1_UA} = $g1_ua;
$yaml_count++;
$yaml{$yaml_count}->{HiC_UA_G1} = $ua_g1;
$yaml_count++;
# G2 / UA
$yaml{$yaml_count}->{HiC_G2_UA_total} = $g2_ua_total;
$yaml_count++;
$yaml{$yaml_count}->{HiC_percent_G2_UA_total} = $perc_g2_ua;
$yaml_count++;
$yaml{$yaml_count}->{HiC_G2_UA} = $g2_ua;
$yaml_count++;
$yaml{$yaml_count}->{HiC_UA_G2} = $ua_g2;
$yaml_count++;
# G1 / G2
$yaml{$yaml_count}->{HiC_G1_G2_total} = $g1_g2_total;
$yaml_count++;
$yaml{$yaml_count}->{HiC_percent_G1_G2_total} = $perc_g1_g2;
$yaml_count++;
$yaml{$yaml_count}->{HiC_G1_G2} = $g1_g2;
$yaml_count++;
$yaml{$yaml_count}->{HiC_G2_G1} = $g2_g1;
$yaml_count++;
# conflicting
$yaml{$yaml_count}->{HiC_conflicting} = $conflicting;
$yaml_count++;
$yaml{$yaml_count}->{HiC_percent_conflicting} = $perc_conflicting;
$yaml_count++;
}
sub princess_paired_end{
my $file = shift;
open (IN,"samtools view -h ${output_dir}$file |") or die "Failed to read from BAM file $file: $!\n";
open_output_filehandles($file);
### READING FROM MAPPING BAM FILE
# Here we don't need to keep track of chromosome etc but can simply proceed line by line
($unassigned,$genome1,$genome2,$conflicting) = (0,0,0,0);
my $count = 0;
my $count_pairs = 0;
my $count_singletons = 0;
my $g1_pairs = 0;
my $g1_singletons = 0;
my $g2_pairs = 0;
my $g2_singletons = 0;
my $unassigned_pairs = 0;
my $unassigned_singletons = 0;
my $conflicting_pairs = 0;
my $conflicting_singletons = 0;
my $last_id;
my $last_line;
while (<IN>){
if ($_ =~ /^\@/){ # header lines
print UNASSIGNED;
print G1;
print G2;
if ($conflict){
print CONFLICT;
}
if ($singletons){
print UNASSIGNED_ST;
print G1_ST;
print G2_ST;
if ($conflict){
print CONFLICT_ST;
}
}
next;
}
my ($id) = (split /\t/)[0];
unless (defined $last_id){
# warn "Setting last_id to $id\n"; #sleep(1);
$last_id = $id;
$last_line = $_;
next; ### need to process last ID upon exiting the while loop
}
### Else, read ID has been set already. Determining if the last read and this read are part of a read pair
if ($id eq $last_id){
# warn "Paired end read\n$last_id\n$id\n";
++$count; # increasing counter for read pair
++$count_pairs;
my $XX_tag_1 = $1 if ($last_line =~ /XX:Z:(.+?)\s+/);
unless ($XX_tag_1){
die "Failed to extract XX:Z tag from line:\n$last_line\n";
} # warn "XX tag 1: $XX_tag_1\n";sleep(1);
my $XX_tag_2 = $1 if ($_ =~ /XX:Z:(.+?)\s+/);
unless ($XX_tag_2){
die "Failed to extract XX:Z tag from line:\n$_\n";
} # warn "XX tag 2: $XX_tag_2\n";sleep(1);
### Processing the allele-specificity of the read pair as a whole
if ($XX_tag_1 eq 'UA' and $XX_tag_2 eq 'UA' ){
++$unassigned; # unassigned
++$unassigned_pairs;
print UNASSIGNED $last_line;
print UNASSIGNED;
}
elsif( ($XX_tag_1 eq 'G1' and $XX_tag_2 eq 'UA') or ($XX_tag_1 eq 'UA' and $XX_tag_2 eq 'G1') or ($XX_tag_1 eq 'G1' and $XX_tag_2 eq 'G1') ){
$genome1++; # genome 1-specific
$g1_pairs++;
print G1 $last_line;
print G1;
}
elsif( ($XX_tag_1 eq 'G2' and $XX_tag_2 eq 'UA') or ($XX_tag_1 eq 'UA' and $XX_tag_2 eq 'G2') or ($XX_tag_1 eq 'G2' and $XX_tag_2 eq 'G2') ){
$genome2++; # genome 2-specific
$g2_pairs++;
print G2 $last_line;
print G2;
}
elsif( ($XX_tag_1 eq 'G1' and $XX_tag_2 eq 'G2') or ($XX_tag_1 eq 'G2' and $XX_tag_2 eq 'G1') ){
++$conflicting; # conflicting read, something weird was going on
++$conflicting_pairs; # conflicting read, something weird was going on
if ($conflict){
print CONFLICT $last_line;
print CONFLICT;
}
}
elsif ($XX_tag_1 eq 'CF' or $XX_tag_2 eq 'CF' ){
++$conflicting; # conflicting read, something weird was going on
++$conflicting_pairs;
if ($conflict){
print CONFLICT $last_line;
print CONFLICT;
}
}
else{
die "The read pairs did not have an expected XX-tag\nXX-tag 1: $XX_tag_1\nXX-tag 2: $XX_tag_2\n\n";
}
# warn "Resetting last_id after processing the paired-end read\n\n"; sleep(1);
$last_id = $last_line = undef; # resetting the last id and alignment
}
else{
# warn "$last_id was a singleton. Processing this read on its own\n"; # sleep(1);
++$count; # increasing counter for singleton
++$count_singletons;
my $XX_tag = $1 if ($last_line =~ /XX:Z:(.+?)\s+/); # allele-specificity flag
# warn "Extracted XX:Z: '$XX_tag' tag from line:\n$last_line\n";
unless ($XX_tag){
die "Failed to extract XX:Z tag from line:\n$last_line\n";
}
if ($XX_tag eq 'UA'){
++$unassigned; # unassigned
++$unassigned_singletons;
if ($singletons){
print UNASSIGNED_ST $last_line;
}
else{
print UNASSIGNED $last_line;
}
}
elsif($XX_tag eq 'G1'){
$genome1++; # genome 1-specific
$g1_singletons++;
if ($singletons){
print G1_ST $last_line;
}
else{
print G1 $last_line;
}
}
elsif($XX_tag eq 'G2'){
$genome2++; # genome 2-specific
$g2_singletons++;
if ($singletons){
print G2_ST $last_line;
}
else{
print G2 $last_line;
}
}
elsif($XX_tag eq 'CF'){
++$conflicting; # conflicting read, something weird was going on
++$conflicting_singletons;
if ($conflict){
if ($singletons){
print CONFLICT_ST $last_line;
}
else{
print CONFLICT $last_line;
}
}
}
else{
die "The read did not have an expected XX-tag:\n$last_line\n\n";
}
## Once we are done processing
# warn "Setting last_id to the current id $id\n\n";# sleep(1);
$last_id = $id;
$last_line = $_;
}
if ($count%1000000 == 0){
warn "Processed $count lines so far\n";
}
}
if (defined $last_id){
# warn "one last singleton read to process:\n$last_id\n$last_line\n";
my $XX_tag = $1 if ($last_line =~ /XX:Z:(.+?)\s+/); # allele-specificity flag
++$count;
++$count_singletons;
unless ($XX_tag){
die "Failed to extract XX:Z tag from line:\n$_\n";
}
if ($XX_tag eq 'UA'){
++$unassigned; # unassigned
++$unassigned_singletons;
if ($singletons){
print UNASSIGNED_ST $last_line;
}
else{
print UNASSIGNED $last_line;
}
}
elsif($XX_tag eq 'G1'){
$genome1++; # genome 1-specific
++$g1_singletons;
if ($singletons){
print G1_ST $last_line;
}
else{
print G1 $last_line;
}
}
elsif($XX_tag eq 'G2'){
$genome2++; # genome 2-specific
++$g2_singletons;
if ($singletons){
print G2_ST $last_line;
}
else{
print G2 $last_line;
}
}
elsif($XX_tag eq 'CF'){
++$conflicting; # conflicting read, something weird was going on
++$conflicting_singletons;
if ($conflict){
if ($singletons){
print CONFLICT_ST $last_line;
}
else{
print CONFLICT $last_line;
}
}
}
else{
die "The read did not have an expected XX-tag:\n$_\n\n";
}
}
else{
warn "Last read was a read pair which has already been processed. all done\n\n";
}
### Printing Summary reports
my ($perc_unassigned,$perc_genome1,$perc_genome2,$perc_conflicting);
if ($count == 0){
$perc_unassigned = $perc_genome1 = $perc_genome2 = $perc_conflicting = 'N/A';
}
else{
$perc_unassigned = sprintf ("%.2f",$unassigned *100/$count);
$perc_genome1 = sprintf ("%.2f",$genome1*100/$count);
$perc_genome2 = sprintf ("%.2f",$genome2*100/$count);
$perc_conflicting = sprintf ("%.2f",$conflicting*100/$count);
}
warn "\n\nAllele-specific paired-end sorting report\n",'='x41,"\n";
warn "Read pairs/singletons processed in total:\t\t$count\n";
warn "\tthereof were read pairs:\t\t\t$count_pairs\n";
warn "\tthereof were singletons:\t\t\t$count_singletons\n";
warn "Reads were unassignable (not overlapping SNPs):\t\t$unassigned ($perc_unassigned%)\n";
warn "\tthereof were read pairs:\t$unassigned_pairs\n";
warn "\tthereof were singletons:\t$unassigned_singletons\n";
warn "Reads were specific for genome 1:\t\t\t$genome1 ($perc_genome1%)\n";
warn "\tthereof were read pairs:\t$g1_pairs\n";
warn "\tthereof were singletons:\t$g1_singletons\n";
warn "Reads were specific for genome 2:\t\t\t$genome2 ($perc_genome2%)\n";
warn "\tthereof were read pairs:\t$g2_pairs\n";
warn "\tthereof were singletons:\t$g2_singletons\n";
warn "Reads contained conflicting SNP information:\t\t$conflicting ($perc_conflicting%)\n";
warn "\tthereof were read pairs:\t$conflicting_pairs\n";
warn "\tthereof were singletons:\t$conflicting_singletons\n\n";
print REPORT "\n\nAllele-specific paired-end sorting report\n",'='x41,"\n";
print REPORT "Read pairs/singletons processed in total:\t\t$count\n";
print REPORT "\tthereof were read pairs:\t\t\t$count_pairs\n";
print REPORT "\tthereof were singletons:\t\t\t$count_singletons\n";
print REPORT "Reads were unassignable (not overlapping SNPs):\t\t$unassigned ($perc_unassigned%)\n";
print REPORT "\tthereof were read pairs:\t$unassigned_pairs\n";
print REPORT "\tthereof were singletons:\t$unassigned_singletons\n";
print REPORT "Reads were specific for genome 1:\t\t\t$genome1 ($perc_genome1%)\n";
print REPORT "\tthereof were read pairs:\t$g1_pairs\n";
print REPORT "\tthereof were singletons:\t$g1_singletons\n";
print REPORT "Reads were specific for genome 2:\t\t\t$genome2 ($perc_genome2%)\n";
print REPORT "\tthereof were read pairs:\t$g2_pairs\n";
print REPORT "\tthereof were singletons:\t$g2_singletons\n";
print REPORT "Reads contained conflicting SNP information:\t\t$conflicting ($perc_conflicting%)\n";
print REPORT "\tthereof were read pairs:\t$conflicting_pairs\n";
print REPORT "\tthereof were singletons:\t$conflicting_singletons\n\n";
### YAML
# total reads
$yaml{$yaml_count}->{PE_total_reads} = $count;
$yaml_count++;
$yaml{$yaml_count}->{PE_total_pairs} = $count_pairs;
$yaml_count++;
$yaml{$yaml_count}->{PE_total_singletons} = $count_singletons;
$yaml_count++;
# unassignable
$yaml{$yaml_count}->{PE_unassignable} = $unassigned;
$yaml_count++;
$yaml{$yaml_count}->{PE_percent_unassignable} = $perc_unassigned;
$yaml_count++;
$yaml{$yaml_count}->{PE_unassignable_pairs} = $unassigned_pairs;
$yaml_count++;
$yaml{$yaml_count}->{PE_unassignable_singletons} = $unassigned_singletons;
$yaml_count++;
# genome 1
$yaml{$yaml_count}->{PE_genome1} = $genome1;
$yaml_count++;
$yaml{$yaml_count}->{PE_percent_genome1} = $perc_genome1;
$yaml_count++;
$yaml{$yaml_count}->{PE_genome1_pairs} = $g1_pairs;
$yaml_count++;
$yaml{$yaml_count}->{PE_genome1_singletons} = $g1_singletons;
$yaml_count++;
# genome 2
$yaml{$yaml_count}->{PE_genome2} = $genome2;
$yaml_count++;
$yaml{$yaml_count}->{PE_percent_genome2} = $perc_genome2;
$yaml_count++;
$yaml{$yaml_count}->{PE_genome2_pairs} = $g2_pairs;
$yaml_count++;
$yaml{$yaml_count}->{PE_genome2_singletons} = $g2_singletons;
$yaml_count++;
# conflicting
$yaml{$yaml_count}->{PE_conflicting} = $conflicting;
$yaml_count++;
$yaml{$yaml_count}->{PE_percent_conflicting} = $perc_conflicting;
$yaml_count++;
$yaml{$yaml_count}->{PE_conflicting_pairs} = $conflicting_pairs;
$yaml_count++;
$yaml{$yaml_count}->{PE_conflicting_singletons} = $conflicting_singletons;
$yaml_count++;
}
sub open_output_filehandles{
my $infile = my $file = shift;
$file =~ s/allele_flagged\.bam$/bam/;
my $genome1_file = my $genome2_file = my $unassigned_file = my $conflicting_file = $report_file = my $yaml_file = $file;
my $genome1_file_st = my $genome2_file_st = my $unassigned_file_st = my $conflicting_file_st = $file;
my $g1_ua_file = my $g2_ua_file = my $g1_g2_file = $file;
# the input file needs to end in .bam (we tested this in while checking the command line options)
# using different filenames for Hi-C data altogether
if ($hic){
$genome1_file =~ s/bam$/G1_G1.sam/;
$genome2_file =~ s/bam$/G2_G2.sam/;
$unassigned_file =~ s/bam$/UA_UA.sam/;
$conflicting_file =~ s/bam$/conflicting.sam/;
$g1_ua_file =~ s/bam$/G1_UA.sam/;
$g2_ua_file =~ s/bam$/G2_UA.sam/;
$g1_g2_file =~ s/bam$/G1_G2.sam/;
}
else{
# for single-end or paired-end mode we end up with up to 4 files
$genome1_file =~ s/bam$/genome1.sam/;
$genome2_file =~ s/bam$/genome2.sam/;
$unassigned_file =~ s/bam$/unassigned.sam/;
$conflicting_file =~ s/bam$/conflicting.sam/;
# ... unless someone wants singletons to be treated differently to paired-end alignments...
if ($singletons){
$genome1_file_st =~ s/bam$/genome1_st.sam/;
$genome2_file_st =~ s/bam$/genome2_st.sam/;
$unassigned_file_st =~ s/bam$/unassigned_st.sam/;
$conflicting_file_st =~ s/bam$/conflicting_st.sam/;
}
}
$report_file =~ s/bam$/SNPsplit_sort.txt/;
$yaml_file =~ s/bam$/SNPsplit_sort.yaml/;
if ($bam){ # default
if ($hic){
$genome1_file =~ s/sam$/bam/;
$genome2_file =~ s/sam$/bam/;
$unassigned_file =~ s/sam$/bam/;
$conflicting_file =~ s/sam$/bam/;
$g1_ua_file =~ s/sam$/bam/;
$g2_ua_file =~ s/sam$/bam/;
$g1_g2_file =~ s/sam$/bam/;
open (G1,"| $samtools_path view -bS 2>/dev/null - > ${output_dir}$genome1_file") or die "Unable to write to BAM file '$genome1_file': $!\n";
open (G2,"| $samtools_path view -bS 2>/dev/null - > ${output_dir}$genome2_file") or die "Unable to write to BAM file '$genome2_file': $!\n";
open (UNASSIGNED,"| $samtools_path view -bS 2>/dev/null - > ${output_dir}$unassigned_file") or die "Unable to write to BAM file '$unassigned_file': $!\n";
if ($conflict){
open (CONFLICT,"| $samtools_path view -bS 2>/dev/null - > ${output_dir}$conflicting_file") or die "Unable to write to BAM file '$conflicting_file': $!\n";
}
open (G1UA,"| $samtools_path view -bS 2>/dev/null - > ${output_dir}$g1_ua_file") or die "Unable to write to BAM file '$g1_ua_file': $!\n";
open (G2UA,"| $samtools_path view -bS 2>/dev/null - > ${output_dir}$g2_ua_file") or die "Unable to write to BAM file '$g2_ua_file': $!\n";
open (G1G2,"| $samtools_path view -bS 2>/dev/null - > ${output_dir}$g1_g2_file") or die "Unable to write to BAM file '$g1_g2_file': $!\n";
}
else{
$genome1_file =~ s/sam$/bam/;
$genome2_file =~ s/sam$/bam/;
$unassigned_file =~ s/sam$/bam/;
$conflicting_file =~ s/sam$/bam/;
if ($singletons){
$genome1_file_st =~ s/sam$/bam/;
$genome2_file_st =~ s/sam$/bam/;
$unassigned_file_st =~ s/sam$/bam/;
$conflicting_file_st =~ s/sam$/bam/;
}
open (G1,"| $samtools_path view -bSh 2>/dev/null - > ${output_dir}$genome1_file") or die "Unable to write to BAM file '$genome1_file': $!\n";
open (G2,"| $samtools_path view -bSh 2>/dev/null - > ${output_dir}$genome2_file") or die "Unable to write to BAM file '$genome2_file': $!\n";
open (UNASSIGNED,"| $samtools_path view -bSh 2>/dev/null - > ${output_dir}$unassigned_file") or die "Unable to write to BAM file '$unassigned_file': $!\n";
if ($conflict){
open (CONFLICT,"| $samtools_path view -bSh 2>/dev/null - > ${output_dir}$conflicting_file") or die "Unable to write to BAM file '$conflicting_file': $!\n";
}
if ($singletons){
open (G1_ST,"| $samtools_path view -bSh 2>/dev/null - > ${output_dir}$genome1_file_st") or die "Unable to write to BAM file '$genome1_file_st': $!\n";
open (G2_ST,"| $samtools_path view -bSh 2>/dev/null - > ${output_dir}$genome2_file_st") or die "Unable to write to BAM file '$genome2_file_st': $!\n";
open (UNASSIGNED_ST,"| $samtools_path view -bSh 2>/dev/null - > ${output_dir}$unassigned_file_st") or die "Unable to write to BAM file '$unassigned_file_st': $!\n";
if ($conflict){
open (CONFLICT_ST,"| $samtools_path view -bSh 2>/dev/null - > ${output_dir}$conflicting_file_st") or die "Unable to write to BAM file '$conflicting_file_st': $!\n";
}
}
}
}
else{ # writing out to a SAM file
open (G1,'>',"${output_dir}$genome1_file") or die "Unable to write to file '$genome1_file': $!\n";
open (G2,'>',"${output_dir}$genome2_file") or die "Unable to write to file '$genome2_file': $!\n";
open (UNASSIGNED,'>',"${output_dir}$unassigned_file") or die "Unable to write to file '$unassigned_file': $!\n";
if ($conflict){
open (CONFLICT,'>',"${output_dir}$conflicting_file") or die "Unable to write to file '$conflicting_file': $!\n";
}
if ($singletons){
open (G1_ST,'>',"${output_dir}$genome1_file_st") or die "Unable to write to file '$genome1_file_st': $!\n";
open (G2_ST,'>',"${output_dir}$genome2_file_st") or die "Unable to write to file '$genome2_file_st': $!\n";
open (UNASSIGNED_ST,'>',"${output_dir}$unassigned_file_st") or die "Unable to write to file '$unassigned_file_st': $!\n";
if ($conflict){
open (CONFLICT_ST,'>',"${output_dir}$conflicting_file_st") or die "Unable to write to file '$conflicting_file_st': $!\n";
}
}
}
open (REPORT,'>',"${output_dir}$report_file") or die "Unable to write to file '$report_file': $!\n";
warn "Writing YAML report to $yaml_file\n\n";
open (YAML,'>',"${output_dir}$yaml_file") or die "Unable to write to file '$yaml_file': $!\n";
if ($hic){
warn "Input file:\t\t\t\t\t\t'$infile'\n";
print REPORT "Input file:\t\t\t\t\t\t'$infile'\n";
warn "Writing SNPsplit-sort report to:\t\t\t'$report_file'\n";
warn "Writing unassigned reads to:\t\t\t\t'$unassigned_file'\n";
print REPORT "Writing unassigned reads to:\t\t\t\t'$unassigned_file'\n";
warn "Writing genome 1-specific reads to:\t\t\t'$genome1_file'\n";
print REPORT "Writing genome 1-specific reads to:\t\t\t'$genome1_file'\n";
warn "Writing genome 2-specific reads to:\t\t\t'$genome2_file'\n";
print REPORT "Writing genome 2-specific reads to:\t\t\t'$genome2_file'\n";
warn "Writing G1/UA reads to:\t\t\t\t\t'$g1_ua_file'\n";
print REPORT "Writing G1/UA reads to:\t\t\t\t\t'$g1_ua_file'\n";
warn "Writing G2/UA reads to:\t\t\t\t\t'$g2_ua_file'\n";
print REPORT "Writing G2/UA reads to:\t\t\t\t\t'$g2_ua_file'\n";
warn "Writing G1/G2 reads to:\t\t\t\t\t'$g1_g2_file'\n";
print REPORT "Writing G1/G2 reads to:\t\t\t\t\t'$g1_g2_file'\n";
if ($conflict){
warn "Writing reads with conflicting number of SNPs to:\t'$conflicting_file'\n\n";
print REPORT "Writing reads with conflicting number of SNPs to:\t'$conflicting_file'\n\n";
}