-
Notifications
You must be signed in to change notification settings - Fork 104
/
bismark
executable file
·9999 lines (8579 loc) · 407 KB
/
bismark
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 strict;
use warnings;
use IO::Handle;
use Cwd;
$|++;
use Getopt::Long;
use FindBin qw($RealBin);
use lib "$RealBin/../lib";
## This program is Copyright (C) 2010-23, Felix Krueger (fkrueger@altoslabs.com)
## 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 $parent_dir = getcwd();
my $bismark_version = 'v0.24.2';
my $copyright_dates = "2010-23";
my $start_run = time();
my $command_line = join (" ",@ARGV);
### before processing the command line we will replace --solexa1.3-quals with --phred64-quals as the '.' in the option name will cause Getopt::Long to fail
foreach my $arg (@ARGV){
if ($arg eq '--solexa1.3-quals'){
$arg = '--phred64-quals';
}
}
my @filenames; # will be populated by processing the command line
my ($genome_folder,$CT_index_basename,$GA_index_basename,$path_to_bowtie,$path_to_hisat2,$path_to_minimap2,$sequence_file_format,$aligner_options,
$directional,$unmapped,$ambiguous,$phred64,$output_dir,$bowtie2,$hisat2,$sam_no_hd,$skip,$upto,$temp_dir,$non_bs_mm,$insertion_open,
$insertion_extend,$deletion_open,$deletion_extend,$gzip,$bam,$samtools_path,$pbat,$prefix,$old_flag,$basename,$score_min_intercept,
$score_min_slope,$bt2_large_index,$multicore,$rg_tag,$rg_id,$rg_sample,$ambig_bam,$cram,$cram_ref,$nucleotide_coverage,$dovetail,
$aligner_version,$slam,$icpc,$local,$strandID,$mm2,$maximum_length_cutoff) = process_command_line();
my @fhs; # stores alignment process names, bisulfite index location, bowtie filehandles and the number of times sequences produced an alignment
my %chromosomes; # stores the chromosome sequences of the mouse genome
my %SQ_order; # stores the order of sequences in the reference. This is to produce SAM/BAM files with a known order of chromosomes
my %counting; # counting various events
my $final_output_filename; # required for the nucleotide coverage report
my @pids; # storing the process IDs of child processes in parallel mode
my $seqID_contains_tabs;
my $verbose = 0;
if ($multicore > 1){
warn "Running Bismark Parallel version. Number of parallel instances to be spawned: $multicore\n\n";
}
sub multi_process_handling{
my $offset = 1;
my $process_id;
if ($multicore > 1){
until ($offset == $multicore){
# warn "multicore: $multicore\noffset: $offset\n";
my $fork = fork;
if (defined $fork){
if ($fork != 0){
$process_id = $fork;
push @pids, $process_id;
if ($offset < $multicore){
++$offset;
# warn "I am the parent process, child pid: $fork\nIncrementing offset counter to: $offset\n\n";
}
else{
# warn "Reached the number of maximum multicores. Proceeeding to processing...\n";
}
}
elsif ($fork == 0){
# warn "I am a child process, pid: $fork\nOffset counter is: $offset\nProceeding to processing...\n";
$process_id = $fork;
last;
}
}
else{
die "[FATAL ERROR]: Forking unsuccessful. This normally means that something is fundamentally not working with the fork command. Please run again without the --parallel option, or ask your system admin to look into this.\n";
}
}
# warn "\nThe Thread Identity\n===================\n";
if ($process_id){
# print "I am the parent process. My children are called:\n";
# print join ("\t",@pids),"\n";
# print "I am going to process the following line count: $offset\n\n";
}
elsif($process_id == 0){
# warn "I am a child process: Process ID: $process_id\n";
# warn "I am going to process the following line count: $offset\n\n";
}
else{
die "Process ID was: '$process_id'\n";
}
}
else{
warn "Single-core mode: setting pid to 1\n";
$process_id = 1;
}
return ($process_id,$offset);
}
sub subset_input_file_FastQ{
my ($filename,$process_id,$offset) = @_;
if ($filename =~ /gz$/){
open (OFFSET,"gunzip -c $filename |") or die "Couldn't read from file '$filename': $!\n";
}
else{
open (OFFSET,$filename) or die "Couldn't read from file '$filename': $!\n";
}
# warn "offset is $offset\n";
my $temp = $filename;
$temp .= ".temp.$offset";
$temp =~ s/^.*\///; # replacing everything upto and including the last /, i.e. removing file path information
if ($gzip){
$temp .= '.gz';
open (TEMPFQ,"| gzip -c - > ${temp_dir}${temp}") or die "Can't write to file ${temp_dir}${temp}: $!\n";
}
else{
open (TEMPFQ,'>',"${temp_dir}${temp}") or die "Failed to write output ${temp_dir}${temp}: $!\n";
}
my $line_count = 0;
my $seqs_processed = 0;
if (defined $upto){
# warn "Before we begin: -u was set to: -u $upto\n";
}
while (1){
my $l1 = <OFFSET>;
my $l2 = <OFFSET>;
my $l3 = <OFFSET>;
my $l4 = <OFFSET>;
last unless ($l4);
++$line_count;
# If user only want to process a subset of the input file
if (defined $upto){
if ($seqs_processed == $upto){
last;
}
}
if ( ($line_count - $offset)%$multicore == 0){
# warn "line count: $line_count\noffset: $offset\n";
# warn "Modulus: ",($line_count - $offset)%$multicore,"\n";
# warn "processing this line $line_count (processID: $process_id with \$offset $offset)\n";
print TEMPFQ "$l1$l2$l3$l4";
$seqs_processed++;
}
else{
# warn "skipping line $line_count for processID: $process_id with \$offset $offset)\n";
next;
}
}
close OFFSET; # or warn $!;
close TEMPFQ or warn "Failed to close file handle TEMPFQ: $!\n";
warn "Finished subdividing $filename for PID: $process_id and offset $offset (sequences written out: $seqs_processed)\n\n";
return ($temp); # returning the subset filename
}
sub subset_input_file_FastA{
my ($filename,$process_id,$offset) = @_;
if ($filename =~ /gz$/){
open (OFFSET,"gunzip -c $filename |") or die "Couldn't read from file '$filename': $!\n";
}
else{
open (OFFSET,$filename) or die "Couldn't read from file '$filename': $!\n";
}
# warn "offset is $offset\n";
my $temp = $filename;
$temp .= ".temp.$offset";
$temp =~ s/^.*\///; # replacing everything upto and including the last /, i.e. removing file path information
if ($gzip){
$temp .= '.gz';
open (TEMPFA,"| gzip -c - > ${temp_dir}${temp}") or die "Can't write to file ${temp_dir}${temp}: $!\n";
}
else{
open (TEMPFA,'>',"${temp_dir}${temp}") or die "Failed to write output ${temp_dir}${temp}: $!\n";
}
warn "Writing temporary infile to $temp\n";
my $line_count = 0;
my $seqs_processed = 0;
while (1){
my $l1 = <OFFSET>;
my $l2 = <OFFSET>;
last unless ($l2);
++$line_count;
# If user only want to process a subset of the input file
if (defined $upto){
if ($seqs_processed == $upto){
last;
}
}
if ( ($line_count - $offset)%$multicore == 0){
# warn "line count: $line_count\noffset: $offset\n";
# warn "Modulus: ",($line_count - $offset)%$multicore,"\n";
# warn "processing this line $line_count (processID: $process_id with \$offset $offset)\n";
print TEMPFA "$l1$l2";
$seqs_processed++;
}
else{
# warn "skipping line $line_count for processID: $process_id with \$offset $offset)\n";
next;
}
}
close OFFSET or warn $!;
close TEMPFA or warn "Failed to close file handle TEMPFQ: $!\n";
warn "Finished subdividing $filename for PID: $process_id and offset $offset (sequences processed: $seqs_processed)\n\n";
return ($temp); # returning the subset filename
}
####
####
foreach my $filename (@filenames){
my $original_filename = $filename;
my $original_filename_1;
my $original_filename_2;
chdir $parent_dir or die "Unable to move to initial working directory'$parent_dir' $!\n";
### resetting the counting hash and fhs
reset_counters_and_fhs($filename);
@pids = ();
$seqID_contains_tabs = 0;
### if 2 or more files are provided we can hold the genome in memory and don't need to read it in a second time
unless (%chromosomes){
my $cwd = getcwd(); # storing the path of the current working directory
warn "Current working directory is: $cwd\n\n";
read_genome_into_memory($cwd);
}
### As of version 0.14.0 we support multi-threading. In a first instance we accomplish this by
### splitting the input file(s) into several smaller subfiles and merging the results back at
### the end.
# get general settings (also for single-threaded use)
my ($pid,$offset) = multi_process_handling ();
my ($single_end,$paired_end);
### PAIRED-END ALIGNMENTS
if ($filename =~ ','){
$single_end = 0;
$paired_end = 1;
my ($C_to_T_infile_1,$G_to_A_infile_1); # to be made from mate1 file
$fhs[0]->{name} = 'CTread1GAread2CTgenome';
$fhs[1]->{name} = 'GAread1CTread2GAgenome';
$fhs[2]->{name} = 'GAread1CTread2CTgenome';
$fhs[3]->{name} = 'CTread1GAread2GAgenome';
warn "\nPaired-end alignments will be performed\n",'='x39,"\n\n";
my ($filename_1,$filename_2) = (split (/,/,$filename));
$original_filename_1 = $filename_1;
$original_filename_2 = $filename_2;
warn "The provided filenames for paired-end alignments are $filename_1 and $filename_2\n";
### subsetting the input file(s)
unless ($multicore == 1){ # not needed in single-core mode
# warn "My PID: $pid\nMy offset: $offset\n";
if ($sequence_file_format eq 'FASTA'){
my $temp_filename_1 = subset_input_file_FastA($filename_1,$pid,$offset);
warn "Using the subset file >${temp_dir}$temp_filename_1< as new in-file 1 (instead of >$filename_1<)\n";
$filename_1 = "${temp_dir}$temp_filename_1";
my $temp_filename_2 = subset_input_file_FastA($filename_2,$pid,$offset);
warn "Using the subset file >${temp_dir}$temp_filename_2< as new in-file 2 (instead of >$filename_2<)\n";
$filename_2 = "${temp_dir}$temp_filename_2";
}
else{ # FastQ format, default
my $temp_filename_1 = subset_input_file_FastQ($filename_1,$pid,$offset);
warn "Using the subset file >${temp_dir}$temp_filename_1< as new in-file 1 (instead of >$filename_1<)\n";
$filename_1 = "${temp_dir}$temp_filename_1";
my $temp_filename_2 = subset_input_file_FastQ($filename_2,$pid,$offset);
warn "Using the subset file >${temp_dir}$temp_filename_2< as new in-file 2 (instead of >$filename_2<)\n";
$filename_2 = "${temp_dir}$temp_filename_2";
}
}
### additional variables only for paired-end alignments
my ($C_to_T_infile_2,$G_to_A_infile_2); # to be made from mate2 file
my $read1_count; # to see if R1 and R2 have the same length
my $read2_count;
### FastA format
if ($sequence_file_format eq 'FASTA'){
warn "Input files are in FastA format\n";
if ($directional){
($C_to_T_infile_1,$read1_count) = biTransformFastAFiles_paired_end ($filename_1,1); # also passing the read number
($G_to_A_infile_2,$read2_count) = biTransformFastAFiles_paired_end ($filename_2,2);
$fhs[0]->{inputfile_1} = $C_to_T_infile_1;
$fhs[0]->{inputfile_2} = $G_to_A_infile_2;
$fhs[1]->{inputfile_1} = undef;
$fhs[1]->{inputfile_2} = undef;
$fhs[2]->{inputfile_1} = undef;
$fhs[2]->{inputfile_2} = undef;
$fhs[3]->{inputfile_1} = $C_to_T_infile_1;
$fhs[3]->{inputfile_2} = $G_to_A_infile_2;
}
elsif($pbat){ # PBAT-Seq
($G_to_A_infile_1,$read1_count) = biTransformFastAFiles_paired_end ($filename_1,1); # also passing the read number
($C_to_T_infile_2,$read2_count) = biTransformFastAFiles_paired_end ($filename_2,2);
$fhs[0]->{inputfile_1} = undef;
$fhs[0]->{inputfile_2} = undef;
$fhs[1]->{inputfile_1} = $G_to_A_infile_1;
$fhs[1]->{inputfile_2} = $C_to_T_infile_2;
$fhs[2]->{inputfile_1} = $G_to_A_infile_1;
$fhs[2]->{inputfile_2} = $C_to_T_infile_2;
$fhs[3]->{inputfile_1} = undef;
$fhs[3]->{inputfile_2} = undef;
}
else{
($C_to_T_infile_1,$G_to_A_infile_1,$read1_count) = biTransformFastAFiles_paired_end ($filename_1,1); # also passing the read number
($C_to_T_infile_2,$G_to_A_infile_2,$read2_count) = biTransformFastAFiles_paired_end ($filename_2,2);
$fhs[0]->{inputfile_1} = $C_to_T_infile_1;
$fhs[0]->{inputfile_2} = $G_to_A_infile_2;
$fhs[1]->{inputfile_1} = $G_to_A_infile_1;
$fhs[1]->{inputfile_2} = $C_to_T_infile_2;
$fhs[2]->{inputfile_1} = $G_to_A_infile_1;
$fhs[2]->{inputfile_2} = $C_to_T_infile_2;
$fhs[3]->{inputfile_1} = $C_to_T_infile_1;
$fhs[3]->{inputfile_2} = $G_to_A_infile_2;
}
unless ($read1_count eq $read2_count){
die "[FATAL ERROR]:\tNumber of bisulfite transformed reads are not equal between Read 1 (\#$read1_count) and Read 2 (\#$read2_count).\nPossible causes: file truncation, or as a result of specifying read pairs that do not belong to each other?! Please re-specify file names! Exiting...\n\n";
}
if ($bowtie2){
paired_end_align_fragments_to_bisulfite_genome_fastA_bowtie2 ($C_to_T_infile_1,$G_to_A_infile_1,$C_to_T_infile_2,$G_to_A_infile_2);
}
else{ # HISAT2
paired_end_align_fragments_to_bisulfite_genome_fastA_hisat2 ($C_to_T_infile_1,$G_to_A_infile_1,$C_to_T_infile_2,$G_to_A_infile_2);
}
}
### FastQ format
else{
warn "Input files are in FastQ format\n";
if ($directional){
if ($slam){
($C_to_T_infile_1,$read1_count) = biTransformFastQFiles_paired_end_slam ($filename_1,1); # also passing the read number
($G_to_A_infile_2,$read2_count) = biTransformFastQFiles_paired_end_slam ($filename_2,2);
}
else{
($C_to_T_infile_1,$read1_count) = biTransformFastQFiles_paired_end ($filename_1,1); # also passing the read number
($G_to_A_infile_2,$read2_count) = biTransformFastQFiles_paired_end ($filename_2,2);
}
$fhs[0]->{inputfile_1} = $C_to_T_infile_1;
$fhs[0]->{inputfile_2} = $G_to_A_infile_2;
$fhs[1]->{inputfile_1} = undef;
$fhs[1]->{inputfile_2} = undef;
$fhs[2]->{inputfile_1} = undef;
$fhs[2]->{inputfile_2} = undef;
$fhs[3]->{inputfile_1} = $C_to_T_infile_1;
$fhs[3]->{inputfile_2} = $G_to_A_infile_2;
}
elsif($pbat){ # PBAT-Seq
### At the moment we are only performing alignments only with uncompressed FastQ files
if ($slam){
($G_to_A_infile_1,$read1_count) = biTransformFastQFiles_paired_end_slam ($filename_1,1); # also passing the read number
($C_to_T_infile_2,$read2_count) = biTransformFastQFiles_paired_end_slam ($filename_2,2);
}
else{
($G_to_A_infile_1,$read1_count) = biTransformFastQFiles_paired_end ($filename_1,1); # also passing the read number
($C_to_T_infile_2,$read2_count) = biTransformFastQFiles_paired_end ($filename_2,2);
}
$fhs[0]->{inputfile_1} = undef;
$fhs[0]->{inputfile_2} = undef;
$fhs[1]->{inputfile_1} = $G_to_A_infile_1;
$fhs[1]->{inputfile_2} = $C_to_T_infile_2;
$fhs[2]->{inputfile_1} = $G_to_A_infile_1;
$fhs[2]->{inputfile_2} = $C_to_T_infile_2;
$fhs[3]->{inputfile_1} = undef;
$fhs[3]->{inputfile_2} = undef;
}
else{ # non-directional
if ($slam){
($C_to_T_infile_1,$G_to_A_infile_1,$read1_count) = biTransformFastQFiles_paired_end_slam ($filename_1,1); # also passing the read number
($C_to_T_infile_2,$G_to_A_infile_2,$read2_count) = biTransformFastQFiles_paired_end_slam ($filename_2,2);
}
else{
($C_to_T_infile_1,$G_to_A_infile_1,$read1_count) = biTransformFastQFiles_paired_end ($filename_1,1); # also passing the read number
($C_to_T_infile_2,$G_to_A_infile_2,$read2_count) = biTransformFastQFiles_paired_end ($filename_2,2);
}
$fhs[0]->{inputfile_1} = $C_to_T_infile_1;
$fhs[0]->{inputfile_2} = $G_to_A_infile_2;
$fhs[1]->{inputfile_1} = $G_to_A_infile_1;
$fhs[1]->{inputfile_2} = $C_to_T_infile_2;
$fhs[2]->{inputfile_1} = $G_to_A_infile_1;
$fhs[2]->{inputfile_2} = $C_to_T_infile_2;
$fhs[3]->{inputfile_1} = $C_to_T_infile_1;
$fhs[3]->{inputfile_2} = $G_to_A_infile_2;
}
unless ($read1_count eq $read2_count){
die "[FATAL ERROR]:\tNumber of bisulfite transformed reads are not equal between Read 1 (\#$read1_count) and Read 2 (\#$read2_count).\nPossible causes: file truncation, or as a result of specifying read pairs that do not belong to each other?! Please re-specify file names! Exiting...\n\n";
}
if ($bowtie2){
paired_end_align_fragments_to_bisulfite_genome_fastQ_bowtie2 ($C_to_T_infile_1,$G_to_A_infile_1,$C_to_T_infile_2,$G_to_A_infile_2);
}
elsif ($mm2){
paired_end_align_fragments_to_bisulfite_genome_fastQ_minimap2 ($C_to_T_infile_1,$G_to_A_infile_1,$C_to_T_infile_2,$G_to_A_infile_2);
}
else{ #
paired_end_align_fragments_to_bisulfite_genome_fastQ_hisat2 ($C_to_T_infile_1,$G_to_A_infile_1,$C_to_T_infile_2,$G_to_A_infile_2);
}
}
start_methylation_call_procedure_paired_ends($filename_1,$filename_2,$C_to_T_infile_1,$G_to_A_infile_1,$C_to_T_infile_2,$G_to_A_infile_2,$pid);
}
### Else we are performing SINGLE-END ALIGNMENTS
else{
warn "\nSingle-end alignments will be performed\n",'='x39,"\n\n";
$single_end = 1;
$paired_end = 0;
### subsetting the input file(s)
unless ($multicore == 1){ # not needed in single-core mode
# warn "My PID: $pid\nMy offset: $offset\n";
if ($sequence_file_format eq 'FASTA'){
my $temp_filename = subset_input_file_FastA($filename,$pid,$offset);
warn "Using the subset file >${temp_dir}$temp_filename< as new in-file (instead of >$filename<)\n";
$filename = "${temp_dir}$temp_filename";
}
else{ # FastQ format, default
my $temp_filename = subset_input_file_FastQ($filename,$pid,$offset);
warn "Using the subset file >${temp_dir}$temp_filename< as new in-file (instead of >$filename<)\n";
$filename = "${temp_dir}$temp_filename";
}
}
### Initialising bisulfite conversion filenames
my ($C_to_T_infile,$G_to_A_infile);
### FastA format
if ($sequence_file_format eq 'FASTA'){
warn "Input file is in FastA format\n";
if ($directional){
($C_to_T_infile) = biTransformFastAFiles ($filename);
$fhs[0]->{inputfile} = $fhs[1]->{inputfile} = $C_to_T_infile;
}
else{
($C_to_T_infile,$G_to_A_infile) = biTransformFastAFiles ($filename);
$fhs[0]->{inputfile} = $fhs[1]->{inputfile} = $C_to_T_infile;
$fhs[2]->{inputfile} = $fhs[3]->{inputfile} = $G_to_A_infile;
}
### Creating 4 different bowtie filehandles and storing the first entry
if ($bowtie2){
single_end_align_fragments_to_bisulfite_genome_fastA_bowtie2 ($C_to_T_infile,$G_to_A_infile);
}
else{
single_end_align_fragments_to_bisulfite_genome_fastA_hisat2 ($C_to_T_infile,$G_to_A_infile);
}
}
## FastQ format
else{
warn "Input file is in FastQ format\n";
if ($directional){
if ($slam){
($C_to_T_infile) = biTransformFastQFiles_slam ($filename);
}
else{
($C_to_T_infile) = biTransformFastQFiles ($filename);
}
$fhs[0]->{inputfile} = $fhs[1]->{inputfile} = $C_to_T_infile;
}
elsif($pbat){
if ($slam){
($G_to_A_infile) = biTransformFastQFiles_slam ($filename);
}
else{
($G_to_A_infile) = biTransformFastQFiles ($filename);
}
$fhs[0]->{inputfile} = $fhs[1]->{inputfile} = $G_to_A_infile; # PBAT-Seq only uses the G to A converted files
}
else{
if ($slam){
($C_to_T_infile,$G_to_A_infile) = biTransformFastQFiles_slam ($filename);
}
else{
($C_to_T_infile,$G_to_A_infile) = biTransformFastQFiles ($filename);
}
$fhs[0]->{inputfile} = $fhs[1]->{inputfile} = $C_to_T_infile;
$fhs[2]->{inputfile} = $fhs[3]->{inputfile} = $G_to_A_infile;
}
### Creating up to 4 different filehandles and storing the first entry
if ($pbat){
if ($bowtie2){ # as of version 0.10.2 we also support PBAT alignments for Bowtie 2
single_end_align_fragments_to_bisulfite_genome_fastQ_bowtie2 (undef,$G_to_A_infile);
}
elsif($mm2){
single_end_align_fragments_to_bisulfite_genome_fastQ_minimap2 (undef,$G_to_A_infile);
}
else{ # HISAT2
single_end_align_fragments_to_bisulfite_genome_fastQ_hisat2 (undef,$G_to_A_infile);
}
}
elsif ($bowtie2){
single_end_align_fragments_to_bisulfite_genome_fastQ_bowtie2 ($C_to_T_infile,$G_to_A_infile);
}
elsif($mm2){
# warn "Now kicking off the single-end Minimap2 alignments\n\n"; sleep(3);
single_end_align_fragments_to_bisulfite_genome_fastQ_minimap2 ($C_to_T_infile,$G_to_A_infile);
}
else{ # HISAT2
single_end_align_fragments_to_bisulfite_genome_fastQ_hisat2 ($C_to_T_infile,$G_to_A_infile);
}
}
start_methylation_call_procedure_single_ends($filename,$C_to_T_infile,$G_to_A_infile,$pid);
}
### MERGING AND DELETING TEMP FILES // TIDYING UP AFTER A MULTICORE PROCESS
if ($pid){ # only performing this for the parent process
if ($multicore > 1){
warn "Now waiting for all child processes to complete\n";
### we need to ensure that we wait for all child processes to be finished before continuing
# warn "here are the child IDs: @pids\n";
# warn "Looping through the child process IDs:\n";
my $all_children_succeeded = 1;
foreach my $id (@pids){
# print "$id\t";
my $kid = waitpid ($id,0);
# print "Returned: $kid\nExit status: $?\n";
unless ($? == 0){
$all_children_succeeded = 0;
warn "\nChild process terminated with exit signal: '$?'\n\n";
}
}
if ($all_children_succeeded) {
print "All child process successfully finished.";
}
else {
die "\nTerminating. Not all child processes successfully finished.";
}
# regenerating names for temporary files
my @temp_input;
my @temp_output;
my @temp_reports;
my @temp_unmapped_1; # will store single end reads or R1 of paired-end
my @temp_unmapped_2;
my @temp_ambiguous_1; # will store single end reads or R1 of paired-end
my @temp_ambiguous_2;
my @temp_ambig_bam;
for (1..$offset){
# Temp Input Files
if ($single_end){
if ($gzip){
push @temp_input, "${original_filename}.temp.${_}.gz";
}
else{
push @temp_input, "${original_filename}.temp.${_}";
}
}
elsif($paired_end){
if ($gzip){
push @temp_input, "${original_filename_1}.temp.${_}.gz";
push @temp_input, "${original_filename_2}.temp.${_}.gz";
}
else{
push @temp_input, "${original_filename_1}.temp.${_}";
push @temp_input, "${original_filename_2}.temp.${_}";
}
}
# if files had a prefix we need to specify it
my $add_prefix;
if (defined $prefix){
$add_prefix = "${prefix}.";
}
else{
$add_prefix = '';
}
if ($single_end){
# Temp Output Files
my $pathless_filename = ${original_filename}; # 10 Jan 2017
$pathless_filename =~ s/.*\///; # deleting path information
if ($bowtie2){
if ($gzip){
push @temp_output, "${output_dir}${add_prefix}${pathless_filename}.temp.${_}.gz_bismark_bt2.bam";
push @temp_reports, "${output_dir}${add_prefix}${pathless_filename}.temp.${_}.gz_bismark_bt2_SE_report.txt";
push @temp_ambig_bam, "${output_dir}${add_prefix}${pathless_filename}.temp.${_}.gz_bismark_bt2.ambig.bam"; # only for Bowtie 2
}
else{
push @temp_output, "${output_dir}${add_prefix}${pathless_filename}.temp.${_}_bismark_bt2.bam";
push @temp_reports, "${output_dir}${add_prefix}${pathless_filename}.temp.${_}_bismark_bt2_SE_report.txt";
push @temp_ambig_bam, "${output_dir}${add_prefix}${pathless_filename}.temp.${_}_bismark_bt2.ambig.bam"; # only for Bowtie 2
}
}
elsif($mm2){
if ($gzip){
push @temp_output, "${output_dir}${add_prefix}${pathless_filename}.temp.${_}.gz_bismark_mm22.bam";
push @temp_reports, "${output_dir}${add_prefix}${pathless_filename}.temp.${_}.gz_bismark_mm2_SE_report.txt";
}
else{
push @temp_output, "${output_dir}${add_prefix}${pathless_filename}.temp.${_}_bismark_mm2.bam";
push @temp_reports, "${output_dir}${add_prefix}${pathless_filename}.temp.${_}_bismark_mm2_SE_report.txt";
}
}
else{ # HISAT2
if ($gzip){
push @temp_output, "${output_dir}${add_prefix}${pathless_filename}.temp.${_}.gz_bismark_hisat2.bam";
push @temp_reports, "${output_dir}${add_prefix}${pathless_filename}.temp.${_}.gz_bismark_hisat2_SE_report.txt";
}
else{
push @temp_output, "${output_dir}${add_prefix}${pathless_filename}.temp.${_}_bismark_hisat2.bam";
push @temp_reports, "${output_dir}${add_prefix}${pathless_filename}.temp.${_}_bismark_hisat2_SE_report.txt";
}
}
if ($unmapped){
if ($gzip){
push @temp_unmapped_1, "${output_dir}${add_prefix}${pathless_filename}.temp.${_}.gz_unmapped_reads.fq";
}
else{
push @temp_unmapped_1, "${output_dir}${add_prefix}${pathless_filename}.temp.${_}_unmapped_reads.fq";
}
}
if ($ambiguous){
if ($gzip){
push @temp_ambiguous_1, "${output_dir}${add_prefix}${pathless_filename}.temp.${_}.gz_ambiguous_reads.fq";
}
else{
push @temp_ambiguous_1, "${output_dir}${add_prefix}${pathless_filename}.temp.${_}_ambiguous_reads.fq";
}
}
}
elsif($paired_end){
# Temp Output Files
my $pathless_filename_1 = ${original_filename_1}; # 10 Jan 2017
my $pathless_filename_2 = ${original_filename_2};
$pathless_filename_1 =~ s/.*\///; # deleting path information
$pathless_filename_2 =~ s/.*\///; # deleting path information
if ($bowtie2){
if ($gzip){
push @temp_output, "${output_dir}${add_prefix}${pathless_filename_1}.temp.${_}.gz_bismark_bt2_pe.bam";
push @temp_reports, "${output_dir}${add_prefix}${pathless_filename_1}.temp.${_}.gz_bismark_bt2_PE_report.txt";
push @temp_ambig_bam, "${output_dir}${add_prefix}${pathless_filename_1}.temp.${_}.gz_bismark_bt2_pe.ambig.bam"; # only for Bowtie 2
}
else{
push @temp_output, "${output_dir}${add_prefix}${pathless_filename_1}.temp.${_}_bismark_bt2_pe.bam";
push @temp_reports, "${output_dir}${add_prefix}${pathless_filename_1}.temp.${_}_bismark_bt2_PE_report.txt";
push @temp_ambig_bam, "${output_dir}${add_prefix}${pathless_filename_1}.temp.${_}_bismark_bt2_pe.ambig.bam"; # only for Bowtie 2
}
}
elsif($mm2){
if ($gzip){
push @temp_output, "${output_dir}${add_prefix}${pathless_filename_1}.temp.${_}.gz_bismark_mm2_pe.bam";
push @temp_reports, "${output_dir}${add_prefix}${pathless_filename_1}.temp.${_}.gz_bismark_mm2_PE_report.txt";
}
else{
push @temp_output, "${output_dir}${add_prefix}${pathless_filename_1}.temp.${_}_bismark_mm2_pe.bam";
push @temp_reports, "${output_dir}${add_prefix}${pathless_filename_1}.temp.${_}_bismark_mm2_PE_report.txt";
}
}
else{
if ($gzip){
push @temp_output, "${output_dir}${add_prefix}${pathless_filename_1}.temp.${_}.gz_bismark_hisat2_pe.bam";
push @temp_reports, "${output_dir}${add_prefix}${pathless_filename_1}.temp.${_}.gz_bismark_hisat2_PE_report.txt";
}
else{
push @temp_output, "${output_dir}${add_prefix}${pathless_filename_1}.temp.${_}_bismark_hisat2_pe.bam";
push @temp_reports, "${output_dir}${add_prefix}${pathless_filename_1}.temp.${_}_bismark_hisat2_PE_report.txt";
}
}
if ($unmapped){
if ($gzip){
push @temp_unmapped_1, "${output_dir}${add_prefix}${pathless_filename_1}.temp.${_}.gz_unmapped_reads_1.fq";
push @temp_unmapped_2, "${output_dir}${add_prefix}${pathless_filename_2}.temp.${_}.gz_unmapped_reads_2.fq";
}
else{
push @temp_unmapped_1, "${output_dir}${add_prefix}${pathless_filename_1}.temp.${_}_unmapped_reads_1.fq";
push @temp_unmapped_2, "${output_dir}${add_prefix}${pathless_filename_2}.temp.${_}_unmapped_reads_2.fq";
}
}
if ($ambiguous){
if ($gzip){
push @temp_ambiguous_1, "${output_dir}${add_prefix}${pathless_filename_1}.temp.${_}.gz_ambiguous_reads_1.fq";
push @temp_ambiguous_2, "${output_dir}${add_prefix}${pathless_filename_2}.temp.${_}.gz_ambiguous_reads_2.fq";
}
else{
push @temp_ambiguous_1, "${output_dir}${add_prefix}${pathless_filename_1}.temp.${_}_ambiguous_reads_1.fq";
push @temp_ambiguous_2, "${output_dir}${add_prefix}${pathless_filename_2}.temp.${_}_ambiguous_reads_2.fq";
}
}
}
}
warn "\n\nRight, cleaning up now...\n\n";
# deleting temp files;
warn "Deleting temporary sequence files...\n";
foreach my $temp (@temp_input){
#print "$temp\t";
$temp =~ s/.*\///; # deleting path information
print "${temp_dir}${temp}\t";
unlink "${temp_dir}${temp}" or warn "Failed to delete temporary FastQ file ${temp_dir}$temp: $!\n";
}
print "\n\n";
# merging temp BAM files
if ($single_end){
merge_individual_BAM_files(\@temp_output,$original_filename,$single_end);
}
else{
merge_individual_BAM_files(\@temp_output,$original_filename_1,$single_end);
}
# deleting temp BAM files
warn "Deleting temporary BAM files...\n";
foreach my $temp (@temp_output){
# print "$temp\t";
$temp =~ s/.*\///; # deleting path information
print "${output_dir}${temp}\t";
unlink "${output_dir}${temp}" or warn "Failed to delete temporary BAM file ${output_dir}${temp}: $!\n";
}
print "\n\n";
### AMBIGUOUS BAM files
if ($ambig_bam){
# merging temp AMBIG BAM files
if ($single_end){
merge_individual_ambig_BAM_files(\@temp_ambig_bam,$original_filename,$single_end);
}
else{
merge_individual_ambig_BAM_files(\@temp_ambig_bam,$original_filename_1,$single_end);
}
# deleting temp BAM files
warn "Deleting temporary ambiguous BAM files...\n";
foreach my $temp (@temp_ambig_bam){
# print "$temp\t";
$temp =~ s/.*\///; # deleting path information
print "${output_dir}${temp}\t";
unlink "${output_dir}${temp}" or warn "Failed to delete temporary ambiguous BAM file ${output_dir}${temp}: $!\n";
}
print "\n\n";
}
if ($unmapped){
if ($single_end){
merge_individual_unmapped_files(\@temp_unmapped_1,$original_filename,$single_end);
}
else{
merge_individual_unmapped_files(\@temp_unmapped_1,$original_filename_1,$single_end,'_1');
merge_individual_unmapped_files(\@temp_unmapped_2,$original_filename_2,$single_end,'_2');
}
# deleting temp unmapped files
warn "Deleting temporary unmapped files...\n";
foreach my $temp (@temp_unmapped_1){
print "$temp\t";
unlink "${output_dir}${temp}" or warn "Failed to delete temporary unmapped FastQ file ${output_dir}$temp: $!\n";
}
if ($paired_end){
foreach my $temp (@temp_unmapped_2){
print "$temp\t";
unlink "${output_dir}${temp}" or warn "Failed to delete temporary unmapped FastQ file ${output_dir}$temp: $!\n";
}
}
print "\n\n";
}
if ($ambiguous){
if ($single_end){
merge_individual_ambiguous_files(\@temp_ambiguous_1,$original_filename,$single_end);
}
else{
merge_individual_ambiguous_files(\@temp_ambiguous_1,$original_filename_1,$single_end,'_1');
merge_individual_ambiguous_files(\@temp_ambiguous_2,$original_filename_2,$single_end,'_2');
}
# deleting temp ambiguous files
warn "Deleting temporary ambiguous files...\n";
foreach my $temp (@temp_ambiguous_1){
print "$temp\t";
unlink "${output_dir}${temp}" or warn "Failed to delete temporary ambiguous FastQ file ${output_dir}$temp: $!\n";
}
if ($paired_end){
foreach my $temp (@temp_ambiguous_2){
print "$temp\t";
unlink "${output_dir}${temp}" or warn "Failed to delete temporary ambiguous FastQ file ${output_dir}$temp: $!\n";
}
}
print "\n\n";
}
# resetting the counters once more so we can add all data from all temporary reports
reset_counters_and_fhs($original_filename);
### Merging the Bismark mapping report files
if ($single_end){
merge_individual_mapping_reports(\@temp_reports,$original_filename,$single_end);
print_final_analysis_report_single_end('mock_file1','mock_file_2','mock_pid','mergeThis');
}
else{
merge_individual_mapping_reports(\@temp_reports,$original_filename_1,$single_end,$original_filename_2);
print_final_analysis_report_paired_ends('mock_file1','mock_file_2','mock_file3','mock_file_4','mock_pid','mergeThis');
}
# deleting temp report files
warn "Deleting temporary report files...\n";
foreach my $temp (@temp_reports){
print "$temp\t";
unlink "${output_dir}${temp}" or warn "Failed to delete temporary report file $output_dir$temp: $!\n";
}
print "\n\n";
}
}
if ($pid){ # only for the Parent
### Produce Run Time
my $end_run = time();
my $run_time = $end_run - $start_run;
my $days = int($run_time/(24*60*60));
my $hours = ($run_time/(60*60))%24;
my $mins = ($run_time/60)%60;
my $secs = $run_time%60;
warn "Bismark completed in ${days}d ${hours}h ${mins}m ${secs}s\n";
print REPORT "Bismark completed in ${days}d ${hours}h ${mins}m ${secs}s\n";
warn "\n====================\nBismark run complete\n====================\n\n";
if ($nucleotide_coverage){
warn "Now calculating observed and expected nucleotide coverage statistics... \n\n";
if ($final_output_filename =~ /(bam|cram)|/){
my @args;
push @args, "--genome $genome_folder";
push @args, "--dir '$output_dir'";
push @args, "--samtools_path $samtools_path";
push @args, $final_output_filename;
print "@args","\n"; sleep(3);
system ("$RealBin/bam2nuc @args");
warn "Finished bam2nuc calculation ...\n\n";
}
else{
warn "Nucleotide coverage statistics are currently only available for BAM or CRAM files\n\n";
}
}
# exit 0; # will terminate after a single supplied file....
}
else{
# If multiple files were supplied as the command line, like so:
# -1 R1.fastq,simulated_1.fastq,ZZZ_R1.fastq -2 R2.fastq,simulated_2.fastq,ZZZ_R2.fastq --multicore 4
# we need to exit from the child processes if we don't want a steady increase of new Bismark instances! Fixed 30 10 2017
# warn "Terminating Child process\n\n";
exit 0;
}
}
sub merge_individual_mapping_reports{
my ($temp_reports,$original_filename_1,$single_end,$original_filename_2) = @_;
my $report_file = $original_filename_1;
$report_file =~ s/.*\///; # removing path information
$report_file =~ s/(\.fastq\.gz|\.fq\.gz|\.fastq|\.fq)$//; # attempting to remove fastq.gz etc to make filename a little shorter
if ($prefix){
$report_file = "${prefix}.${report_file}";
}
if ($basename){ # Output file basename is set using the -B argument
$report_file = ${basename};
}
if ($single_end){
if ($bowtie2){
$report_file .= '_bismark_bt2_SE_report.txt';
}
elsif($mm2){
$report_file .= '_bismark_mm2_SE_report.txt';
}
else{
$report_file .= '_bismark_hisat2_SE_report.txt';
}
}
else{
if ($bowtie2){
$report_file .= '_bismark_bt2_PE_report.txt';
}
elsif($mm2){
$report_file .= '_bismark_mm2_PE_report.txt';
}
else{
$report_file .= '_bismark_hisat2_PE_report.txt';
}
}
warn "Writing report to ${output_dir}${report_file}\n";
open (REPORT,'>',"$output_dir$report_file") or die "Failed to write to ${output_dir}${report_file}: $!\n";
foreach my $temp(@$temp_reports){
$temp =~ s/.*\///; # removing path information
}
warn "Now merging temporary reports @$temp_reports into >>> ${output_dir}${report_file} <<<\n";
if ($single_end){
print REPORT "Bismark report for: $original_filename_1 (version: $bismark_version)\n";
}
else{ # paired-end
print REPORT "Bismark report for: $original_filename_1 and $original_filename_2 (version: $bismark_version)\n";
}
my $first = 0;
foreach my $temp(@$temp_reports){
# $temp =~ s/.*\///; # removing path information
warn "Merging from file >> $temp <<\n";
open (IN,"${output_dir}${temp}") or die "Failed to read from temporary mapping report '${output_dir}${temp}'\n";
### this is printing the first couple of lines
while (<IN>){
chomp;