forked from grenaud/schmutzi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschmutzi.pl
executable file
·1098 lines (790 loc) · 33.6 KB
/
schmutzi.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
#use bignum;
use strict;
use warnings;
use Getopt::Long;
use Cwd 'abs_path';
use List::Util qw(min max);
use Scalar::Util qw(looks_like_number);
use Data::Dumper;
use File::Copy;
use Time::HiRes qw/ time sleep /;
my $mock =0;
my $lengthDeam=2;
sub log10 {
my $n = shift;
return log($n)/log(10);
}
sub writeContToLogFile{
my ($cont,$contOutFile) = @_;
print "Writing contamination estimate $cont to file $contOutFile\n";
if ($mock != 1) {
open(FILEoutcont, ">".$contOutFile) or die "cannot write to ".$contOutFile."\n";
print FILEoutcont $cont."\t".$cont."\t".$cont."\n";
close(FILEoutcont);
}
}
sub computeAmountOfPositions{
my ($posfile) = @_;
print "reading position file ".$posfile."\n";
if ($mock != 1) {
open(FILE,$posfile) or die "cannot open ".$posfile."";
my $numberLines=0;
while (my $line = <FILE>) {
chomp($line);
$numberLines++;
}
close(FILE);
return $numberLines;
}else{
#mock, nothing to do
return -1;
}
}
sub computeIntervalCont{
my ($mtcontOutputLog) = @_;
print "reading the contamination log ".$mtcontOutputLog."\n";
if ($mock != 1) {
open(FILE,$mtcontOutputLog) or die "cannot open ".$mtcontOutputLog."";
my $maxL=10;
my $maxLi=0;
my @arrayOfValues;
while (my $line = <FILE>) {
chomp($line);
#print $line;
my @array = split("\t",$line);
if ($maxL == 10) {
$maxL = $array[2];
}
if ($maxL < $array[2]) {
$maxL = $array[2];
$maxLi = ($#arrayOfValues+1);
}
my $hashVal = { 'cont' => $array[1],
'logL' => $array[2]};
push(@arrayOfValues,$hashVal);
}
close(FILE);
my $sum=0;
foreach my $hashVal (@arrayOfValues) {
$hashVal->{'logLS'}=$hashVal->{'logL'}-$maxL;
# print "".($hashVal->{'logLS'})."\t".(10**($hashVal->{'logLS'}))."\t".$sum."\n";
$sum+=(10**($hashVal->{'logLS'}));
}
#print $sum;
my $targetSum = 0.95 * $sum;
my $il=$maxLi;
my $ih=$maxLi;
while (1) {
$il=max($il-1, 0);
$ih=min($ih+1, $#arrayOfValues+1 );
#print "i ".$il."\t".$ih."\n";
my $subsum = 0;
for (my $i=$il;$i<=$ih;$i++) {
$subsum += (10**($arrayOfValues[$i]->{'logLS'}));
}
#print $targetSum."\t".$subsum."\t".$il."\t".$ih."\n";
if ($subsum>$targetSum) {
last;
}
}
return ($arrayOfValues[$maxLi]->{'cont'},$arrayOfValues[$il]->{'cont'},$arrayOfValues[$ih]->{'cont'});
}else{
#mock, nothing to do
return (-1,-1,-1);
}
}
sub readMTCONToutlogfile{
my ($mtcontOutputLog) = @_;
print "reading the log file ".$mtcontOutputLog."\n";
my $sourceFile="###";
my $contMaxS = -1;
my $llikMaxS = -1;
my $contMaxSALL = -1;
my $llikMaxSALL = -1;
my $contFileInd = 0;
if ($mock != 1) {
open(FILEoutlogmtcont, $mtcontOutputLog) or die "cannot open ".$mtcontOutputLog."\n";
while(my $line = <FILEoutlogmtcont>){
my @arrayTemp = split("\t",$line);
if($#arrayTemp != 2){
die "Error line $line in $mtcontOutputLog does not have 3 fields";
}
if($arrayTemp[0] ne $sourceFile){ # new
if($sourceFile eq "###"){ #first one
$sourceFile = $arrayTemp[0];
}else{#new source
if( $contFileInd == 1 ){#first file
$contMaxSALL = $contMaxS;
$llikMaxSALL = $llikMaxS;
}else{
if($llikMaxSALL < $llikMaxS){
$contMaxSALL = $contMaxS;
$llikMaxSALL = $llikMaxS;
}
}
}
$contMaxS = $arrayTemp[1];
$llikMaxS = $arrayTemp[2];
$contFileInd++;
}else{
if($llikMaxS < $arrayTemp[2]){
$contMaxS = $arrayTemp[1];
$llikMaxS = $arrayTemp[2];
}
}
}
if($llikMaxSALL < $llikMaxS){
$contMaxSALL = $contMaxS;
$llikMaxSALL = $llikMaxS;
}
close(FILEoutlogmtcont);
if ( $contMaxSALL == -1) {
die "Cannot parse the contamination output file = ".$mtcontOutputLog."\n";
}
return $contMaxSALL;
} else {
return 0.5;
}
}
sub readMTCONTRetainMostlikely{
my ($mtcontOutputLog,$outputToWrite) = @_;
print "reading the log file ".$mtcontOutputLog." and print most likely source to ".$outputToWrite."\n";
my $sourceALL = -1;
my $contMaxSALL = -1;
my $llikMaxSALL = -1;
my $firstRecord = 1;
if ($mock != 1) {
open(FILEoutlogmtcont, $mtcontOutputLog) or die "cannot open ".$mtcontOutputLog."\n";
while(my $line = <FILEoutlogmtcont>){
my @arrayTemp = split("\t",$line);
if($#arrayTemp != 2){
die "Error line $line in $mtcontOutputLog does not have 3 fields";
}
if( $firstRecord == 1 ){#first record
$sourceALL = $arrayTemp[0];
$contMaxSALL = $arrayTemp[1];
$llikMaxSALL = $arrayTemp[2];
$firstRecord = 0;
}else{
if($llikMaxSALL < $arrayTemp[2]){
$sourceALL = $arrayTemp[0];
$contMaxSALL = $arrayTemp[1];
$llikMaxSALL = $arrayTemp[2];
}
}
}
close(FILEoutlogmtcont);
if ( $contMaxSALL == -1) {
die "Cannot parse the contamination output file = ".$mtcontOutputLog."\n";
}
open(FILEoutlogmtcont, $mtcontOutputLog) or die "cannot open ".$mtcontOutputLog."\n";
open(FILEoutlogmtcontOUT, ">".$outputToWrite) or die "cannot write to ".$outputToWrite."\n";
while(my $line = <FILEoutlogmtcont>){
my @arrayTemp = split("\t",$line);
if($#arrayTemp != 2){
die "Error line $line in $mtcontOutputLog does not have 3 fields";
}
if($sourceALL eq $arrayTemp[0]){
print FILEoutlogmtcontOUT $line;
}
}
close(FILEoutlogmtcont);
close(FILEoutlogmtcontOUT);
} else {
#nothing to do
}
}
sub copycmd{
my ($source,$destination) = @_;
print "copying ". $source." to ".$destination."\n";
if($mock != 1){
copy( $source,$destination ) or die "Copy file failed: $!";
}
}
sub makeEmptyProfFile{
my ($filename) = @_;
if($mock != 1){
open(FILEprof, ">".$filename) or die "cannot write to ".$filename;
print FILEprof "A>C\tA>G\tA>T\tC>A\tC>G\tC>T\tG>A\tG>C\tG>T\tT>A\tT>C\tT>G\n";
for(my $i=0;$i<$lengthDeam;$i++){
print FILEprof "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n";
}
close(FILEprof);
}else{
print "writing empty deamination profile to $filename\n";
}
}
sub readcont{
my ($filename) = @_;
#
print "reading the contamination estimate file: $filename\n";
#if ($mock != 1) {
open(FILEcont, $filename) or die "cannot open ".$filename;
my $line = <FILEcont>;
my @arrayTemp = split("\t",$line);
close(FILEcont);
return $arrayTemp[0];
#} else {
#print "reading the contamination estimate file: $filename\n";
# return 0.5;
#}
}
sub readSizeParam{
my ($filename) = @_;
print "reading the insert size parameter file: $filename\n";
if ($mock != 1) {
open(FILEparamEndo,$filename) or die "cannot open ".$filename."\n";
my @linesEndo = <FILEparamEndo>;
close(FILEparamEndo);
if ($#linesEndo != 2) {
die "ERROR: Parameter file from the log normal distribution does not have 3 lines, it has ".$#linesEndo." lines check the size distribution of the endogenous and contaminant molecules";
}
my $lineParamEndo= $linesEndo[1];
chomp($lineParamEndo);
$lineParamEndo =~ s/^\s+//;
$lineParamEndo =~ s/\s+$//;
my @arrayEndo = split(/\s+/,$lineParamEndo);
if (!looks_like_number( $arrayEndo[0] )) {
die "ERROR: Parameter file from the log normal distribution contains a line that does not have the expected numerical parameters: ".$lineParamEndo."\n";
}
if (!looks_like_number( $arrayEndo[1] )) {
die "ERROR: Parameter file from the log normal distribution contains a line that does not have the expected numerical parameters: ".$lineParamEndo."\n";
}
my $loc=$arrayEndo[0];
my $sca=$arrayEndo[1];
return ($loc,$sca);
} else {
return (1,1);
}
}
sub runcmd{
my ($cmdtorun) = @_;
print "running cmd ". $cmdtorun."\n";
if($mock != 1){
my @argstorun = ( "bash", "-c", $cmdtorun );
if(system(@argstorun) != 0){
die "system cmd $cmdtorun failed: $?"
}else{
}
}
}
sub runcmdReturnOutput {
my $command = join ' ', @_;
reverse ($_ = qx{$command 2>&1}, $? >> 8);
}
sub fileExists{
my ($exeFile) = @_;
if (!( -e $$exeFile)) {
if ( ( -e (($$exeFile).".exe")) ) {
$$exeFile=(($$exeFile).".exe");
}else{
die "Executable ".$$exeFile." does not exist\n";
}
}
}
my @arraycwd=split("/",abs_path($0));
pop(@arraycwd);
my $pathdir = join("/",@arraycwd);
my $mtCont = $pathdir."/mtCont";
my $endoCaller = $pathdir."/endoCaller";
my $insertSize = $pathdir."/insertSize";
my $countRec = $pathdir."/countRecords";
my $approxDist = $pathdir."/approxDist.R";
my $bam2prof = $pathdir."/bam2prof";
my $log2freq = $pathdir."/log2freq";
my $logs2pos = $pathdir."/logs2pos";
my $contDeam = $pathdir."/contDeam";
my $contDeamR = $pathdir."/posteriorDeam.R";
my $splitEndo = $pathdir."/splitEndoVsCont/poshap2splitbam";
fileExists(\$mtCont);
fileExists(\$endoCaller);
fileExists(\$insertSize);
fileExists(\$countRec);
fileExists(\$approxDist);
fileExists(\$bam2prof);
fileExists(\$log2freq);
fileExists(\$logs2pos);
fileExists(\$contDeam);
fileExists(\$contDeamR);
fileExists(\$splitEndo);
my $nameMT = "MT";
my $nameMTc = "MTc";
my $qualmin = 0;
my $lengthMT=16569;
my $logindel=50;
#
# protocol s or d
# output prefix
my $multipleC=0;
#my $usepredC=0;
my $notusepredC=0;
my $numthreads=1;
my $maxIterations = 100;
my $iterationSameCont = 3;
sub usage
{
print "Unknown option: @_\n" if ( @_ );
print "\n\n
This script is a wrapper to produce the endogenous mitochondrial
consensus genome and a contamination estimate given aligned
mitochondrial data and a set of putative contaminants. This script
takes the ouput of the contDeam.pl script and calls iteratively
the module that produces an endogenous consensus and the one that
estimates contamination. For non-hominin samples where the is no
risk of human contamination where the mitochondrial consensus is
needed, call endoCaller directly (see README.md).
\n\nusage:\t".$0." <options> [output prefix from contDeam.pl] [directory with *freq file] input.bam\n\n".
#"Mandatory parameters:\n".
#"\t[output prefix from contDeam]\tThis is the prefix of the output of contDeam.pl (option --out)\n".
#"\t[directory with *freq file]\tDirectory with all the *freq file containing the putative contaminants\n".
#"\t[reference *fasta]\tFasta file of the reference used for by aligner\n".
#"\t[input.bam]\tAligned BAM file\n\n".
#
"Options:\n".
"\n\t--iterations (# it.)\t\t\tMaximum number of iterations (Default : $maxIterations)\n".
"\t--t (threads #)\t\t\t\tUse this amount of threads (Default : $numthreads)\n".
"\t--lengthMT (bp)\t\t\t\tConsider that the real length of the MT genome is this (Default : $lengthMT)\n".
"\t--estdeam\t\t\t\tRe-estimate deamination parameters using newly computed segregating positions".
"\n\t\t\t\t\t\tThis setting is recommended if the contamination is deaminated".
"\n".
"\t--mock\t\t\t\t\tDo nothing, just print the commands used\n".
"\t--uselength\t\t\t\tUse length of the molecules as well\n".
"\t--lengthDeam (bp)\t\t\tOnly consider this about of bases to be deaminated on each end (Default : $lengthDeam)\n".
"\t--multipleC\t\t\t\tDo not assume that there is a single contaminant\n".
"\t--notusepredC\t\t\t\tIf assuming a single contaminant, do not use the predicted contaminant in the contamination estimate".
"\n\t\t\t\t\t\tThis might lead to worse results".
"\n\n".
#
" Output Options:\n".
"\t--out (out prefix)\t\t\tOutput prefix, otherwise use the same used by contDeam.pl\n".
"\t--name (name)\t\t\t\tName of the endogenous MT genome\n".
"\t--namec (name)\t\t\t\tName of the contaminant MT genome\n".
"\t--qual (qual)\t\t\t\tMinimum quality for a base in the MT consensus (on a PHRED scale, e.g. 50 = 1/100,000)\n".
"\t--logindel (logindel)\t\t\tMinimum difference between logs to call an indel (on a PHRED scale default : ".$logindel.")\n".
"\t--contknown (cont)\t\t\tIf you have prior knowledge about the contamination\n\t\t\t\t\t\trate, enter it here [0-1] and it will be plotted\n".
"\t--title (title)\t\t\t\tTitle for the graph of the posterior distribution\n".
"\n".
" Input Options:\n".
"\t--contprior (rate)\t\t\tIgnore the contamination rate prior found by contDeam.pl, use this rate instead\n".
"\t--ref (reference genome)\t\tThe fasta file used for alignment if not specified when calling contDeam.pl\n".
#"\t--out (output prefix)\t\tAll output files will share this prefix\n".
#"\t--title (title)\t\t\tTitle for the graph of the posterior distribution\n".
#"\t--cont (cont)\t\t\tIf you have prior knowledge about the contamination\n\t\t\t\t\trate, enter it here [0-1]\n".
#"\nInput options:\n".
#"\t--split (file)\t\t\tSplit endogenous/contaminant according to diagnostic positions\n".
#"\t\t\t\t\tThe file must have the following format:\n".
#"\t\t\t\t\t\t[coord]tab[nucleotide]tab[endo or cont]\n".
#"\t\t\t\t\tWhere the coordinate is on the reference genome\n".
#"\t\t\t\t\tex:\t385\tA\tendo\n".
#"\nMandatory:\n".
#"\t--ref (reference genome)\tThe fasta file used for alignment\n".
#"\t--help|-?".
"\n\n";
exit;
}
my $starttime = time;
my $help;
my $library = "none";
my $outputPrefix = "outputdeam";
my $contPriorKnow = -1;
my $contPriorKnowCMDLine = -1;
my $textGraph = "Posterior probability for contamination\\nusing mitochondrial positions";
my $referenceFasta = "none";
my $referenceFastaCMDL = "none";
my $splitPos = "";
my $useLength = 0;
my $useLengthContDEAM = 0; #from contdeam
my $outputPrefixCMDLINE = "none";
my $estdeam = 0 ;
#my $contPriorSpecified = 0 ;
my $contPriorUser = -1 ;
usage() if ( @ARGV < 1 or
! GetOptions('help|?' => \$help, 'iterations=i' => \$maxIterations,'ref=s' => \$referenceFastaCMDL, 't=i' => \$numthreads, 'mock' => \$mock, 'estdeam' => \$estdeam, 'uselength' => \$useLength, 'title=s' => \$textGraph, 'out=s' => \$outputPrefixCMDLINE,'name=s' => \$nameMT, 'namec=s' => \$nameMTc, 'contknown=f' => \$contPriorKnowCMDLine, 'lengthDeam' => \$lengthDeam,'lengthMT' => \$lengthMT,'multipleC' => \$multipleC,'notusepredC' => \$notusepredC,'contprior=f' => \$contPriorUser,'qual=f' => \$qualmin,'logindel=f' => \$logindel ) #,'name=s' => \$nameMT,'namec=s' => \$nameMTc
or defined $help );
my $prefixcontDeam = $ARGV[ $#ARGV -2 ];
my $configfiledeam = $prefixcontDeam.".deam.config";
my $freqDir = $ARGV[ $#ARGV -1 ];
my $inbam = $ARGV[ $#ARGV -0 ];
my $splitDeam =-1;
open(FILEcontdeam, $configfiledeam) or die "cannot open ".$configfiledeam;
while (my $line = <FILEcontdeam>) {
if($line =~ /^library\s(\S+)$/){ $library=$1;}
if($line =~ /^outputPrefix\s(\S+)$/){ $outputPrefix=$1;}
#if($line =~ /^inbam\s(\S+)$/){ $inbam=$1;}
if($line =~ /^referenceFasta\s(\S+)$/){ $referenceFasta=$1;}
if($line =~ /^contPriorKnow\s(\S+)$/){ $contPriorKnow=$1;}
#if($line =~ /^textGraph\s(\S+)$/){ $textGraph=$1;}
if($line =~ /^splitPos\s(\S+)$/){ $splitPos=$1;} #file containing positions, empty if not used
if($line =~ /^useLength\s(\S+)$/){ $useLengthContDEAM=$1;}
if($line =~ /^splitDeam\s(\S+)$/){ $splitDeam=$1;}
}
close(FILEcontdeam);
if($outputPrefixCMDLINE ne "none"){
$outputPrefix=$outputPrefixCMDLINE;
}
if($referenceFastaCMDL ne "none"){
if($referenceFasta ne "none" &&
$referenceFastaCMDL ne $referenceFasta){
print "WARNING: you are specifying the reference via the command line despite the fact that there is already one specified.\n";
}
$referenceFasta = $referenceFastaCMDL;
}
if($contPriorKnowCMDLine != -1){
$contPriorKnow = $contPriorKnowCMDLine;
}
if($contPriorUser != -1){
if($contPriorUser<0 || $contPriorUser>1){
die "ERROR: The contamination prior specified via --contprior should be between 0 and 1.\n";
}
}
if($numthreads <= 0){
die "The number of threads must be greater than 1\n";
}
if($splitDeam == -1){
die "The field splitDeam should be defined in $configfiledeam\n";
}
if($library ne "single" &&
$library ne "double" ){
die "Please enter the type of library as either double or single\n";
}
if($referenceFasta eq "none" ){
die "Please enter the fasta reference used for mapping\n";
}
#read all files in $freqDir
if($freqDir !~ /\/$/){
$freqDir .= "/";
}
my @arrayOfFreqFiles;
opendir(FREQDIR, $freqDir) || die "Cannot open directory: $freqDir\n";
while (my $freqf = readdir(FREQDIR)) {
#print "$freqf\n";
if($freqf =~ /\.freq$/){
push(@arrayOfFreqFiles, $freqDir.$freqf);
}
}
closedir(FREQDIR);
print "Found the following freq files:\n------------------\n";
foreach my $freqf (@arrayOfFreqFiles){
print $freqf."\n";
}
print "------------------\n";
#die;
# begin loop
my $numberIteration=1;
########################
#DEAMINATION PARAMETERS#
########################
#if(! $estdeam ){ #do not re-estimate at each iteration
copycmd( $prefixcontDeam.".endo.5p.prof" , $outputPrefix."_".$numberIteration."_endo.5p.prof" );
copycmd( $prefixcontDeam.".endo.3p.prof" , $outputPrefix."_".$numberIteration."_endo.3p.prof" );
if($splitDeam){
copycmd( $prefixcontDeam.".cont.5p.prof" , $outputPrefix."_".$numberIteration."_cont.5p.prof" );
copycmd( $prefixcontDeam.".cont.3p.prof" , $outputPrefix."_".$numberIteration."_cont.3p.prof" );
}else{#put dummy values for cont, the split was not done by contDeam and we will do it after the first iteration
makeEmptyProfFile($outputPrefix."_".$numberIteration."_cont.5p.prof" );
makeEmptyProfFile($outputPrefix."_".$numberIteration."_cont.3p.prof" );
}
#}else{
# #we re-estimate at each iteration the deamination
#}
copycmd( $prefixcontDeam.".cont.est" , $outputPrefix."_".$numberIteration."_cont.est" );
########################
# LENGTH PARAMETERS #
########################
if($useLength){#we use the length of the molecules in the endoCaller
if($useLengthContDEAM){ #if previously computed
copycmd( $prefixcontDeam."_endo.size.param" , $outputPrefix."_".($numberIteration)."_split_endo.size.param" );
copycmd( $prefixcontDeam."_cont.size.param" , $outputPrefix."_".($numberIteration)."_split_cont.size.param" );
}else{
#otherwise, the length will not be used for the first iteration
}
}
print "\n\n\n############################\n\n\n";
my $previousCurrentContEst=-1;
my $previousCurrentContEstItSameVal=0;
my $notEnoughDataSize=0;
while(1){
my $currentContEst=readcont( $outputPrefix."_".$numberIteration."_cont.est" );
if($numberIteration == 1 ){ #first iteration
if($contPriorUser != -1){ #user specified a prior contamination rate
$currentContEst=$contPriorUser;
}
}
my $stringToPrint1= "############################";
my $stringToPrint2="# ITERATION #".sprintf("%".(log10($maxIterations)+1)."s",$numberIteration)."";
while((length($stringToPrint2)+1)<length($stringToPrint1)){
$stringToPrint2.=" ";
}
$stringToPrint2.="#";
print "\n\n\n".$stringToPrint1."\n";
print $stringToPrint2."\n";
#\n";
print "# #\n";
print "# Contamination rate: ".sprintf("%.2f",$currentContEst)." #\n";
print "############################\n";
# call endoCaller
my $cmdEndoCaller = $endoCaller." ";
$cmdEndoCaller .= " -seq ".$outputPrefix."_".$numberIteration."_endo.fa ";
$cmdEndoCaller .= " -log ".$outputPrefix."_".$numberIteration."_endo.log ";
$cmdEndoCaller .= " -name ".$nameMT." ";
$cmdEndoCaller .= " -qual ".$qualmin." ";
$cmdEndoCaller .= " -logindel ".$logindel." ";
$cmdEndoCaller .= " -deamread ";
$cmdEndoCaller .= " -deam5p ".$outputPrefix."_".$numberIteration."_endo.5p.prof ";
$cmdEndoCaller .= " -deam3p ".$outputPrefix."_".$numberIteration."_endo.3p.prof ";
$cmdEndoCaller .= " -cont ".min(max($currentContEst,0.01),0.99)." ";#to allow for the possibility of having contamination
if(!$multipleC){ #we can assume a single contaminant
$cmdEndoCaller .= " -deam5pc ".$outputPrefix."_".$numberIteration."_cont.5p.prof ";
$cmdEndoCaller .= " -deam3pc ".$outputPrefix."_".$numberIteration."_cont.3p.prof ";
$cmdEndoCaller .= " -single ";
$cmdEndoCaller .= " -seqc ".$outputPrefix."_".$numberIteration."_cont.fa ";
$cmdEndoCaller .= " -logc ".$outputPrefix."_".$numberIteration."_cont.log ";
$cmdEndoCaller .= " -namec ".$nameMTc." ";
}
$cmdEndoCaller .= " -l ".$lengthMT." ";
# mol. length
if ($useLength) { #
my $notDefined=0;
my $locE=1;
my $scaE=1;
my $locC=1;
my $scaC=1;
if($numberIteration == 1){ #first iteration
if($useLengthContDEAM){ #if previously computed
($locE,$scaE)= readSizeParam($outputPrefix."_".($numberIteration)."_split_endo.size.param" );
($locC,$scaC)= readSizeParam($outputPrefix."_".($numberIteration)."_split_cont.size.param" );
}else{
#do not use length for the first iteration
$notDefined=1;
}
}else{
if($notEnoughDataSize){#there was not enough data in the previous iteration
$notDefined=1;
}else{
($locE,$scaE)= readSizeParam($outputPrefix."_".($numberIteration)."_split_endo.size.param" );
($locC,$scaC)= readSizeParam($outputPrefix."_".($numberIteration)."_split_cont.size.param" );
}
}
if(!$notDefined){
$cmdEndoCaller .= " --loce ".$locE." ";
$cmdEndoCaller .= " --scalee ".$scaE." ";
$cmdEndoCaller .= " --locc ".$locC." ";
$cmdEndoCaller .= " --scalec ".$scaC." ";
}
}
$cmdEndoCaller .= " $referenceFasta $inbam ";
runcmd($cmdEndoCaller);
my @listOfFreqFiles=@arrayOfFreqFiles;
#convert log to freq
if(!$multipleC){ #we can assume a single contaminant
if ( !$notusepredC ){
my $cmdLog2Freq = $log2freq." ".$outputPrefix."_".$numberIteration."_cont.log > ".$outputPrefix."_".$numberIteration."_cont.freq";
runcmd($cmdLog2Freq);
push(@listOfFreqFiles, $outputPrefix."_".$numberIteration."_cont.freq");
}
}
# estimate cont
my $cmdmtcont = $mtCont. " -o ".$outputPrefix."_".$numberIteration."_mtcont.out ";
#$cmdmtcont = " -deam5p "
$cmdmtcont .= " -deam5p ".$outputPrefix."_".$numberIteration."_endo.5p.prof ";
$cmdmtcont .= " -deam3p ".$outputPrefix."_".$numberIteration."_endo.3p.prof ";
$cmdmtcont .= " -t ".$numthreads." ";
$cmdmtcont .= " ".$outputPrefix."_".$numberIteration."_endo.log ";
$cmdmtcont .= " $referenceFasta $inbam ";
$cmdmtcont .= " ".join(" ",@listOfFreqFiles). " ";
runcmd($cmdmtcont);
# if likelihood stable, break loop
if($numberIteration >= $maxIterations ){
print "Reached the maximum number of iterations $numberIteration, exiting\n";
last;
}
#BEGIN read new cont esti. log
my $mtcontOutputLog = $outputPrefix."_".$numberIteration."_mtcont.out";
my $contFROMmtcont = readMTCONToutlogfile($mtcontOutputLog);
writeContToLogFile($contFROMmtcont,$outputPrefix."_".($numberIteration+1)."_cont.est");
#END read new cont esti. log
if (!$multipleC) { #we can assume a single contaminant
if ( ($estdeam || $useLength) ) { # we need to split if and only if we re-estimate the deamination at each iteration or use the length of the molecules
#print seg. sites
my $cmdLogs2Pos = $logs2pos." ".$outputPrefix."_".$numberIteration."_endo.log ".$outputPrefix."_".$numberIteration."_cont.log > ".$outputPrefix."_".$numberIteration."_split.pos";
runcmd($cmdLogs2Pos);
#count pos in .pos
my $countPosFound=computeAmountOfPositions($outputPrefix."_".$numberIteration."_split.pos");
if ($countPosFound<5) {
warn "Unable to find more than 5 positions that are different in ".$outputPrefix."_".$numberIteration."_endo.log ".$outputPrefix."_".$numberIteration."_cont.log\n".
"Either your sample has little or no contamination or too much, either case, we cannot split according to segrating positions.\n".
"WARNING: We will keep running without using --useLength nor --estdeam\n";
$estdeam = 0;
$useLength = 0;
goto SKIPESTLENGTH;
}
# split according to seg sites
my $cmdBamSplit = $splitEndo." ".$outputPrefix."_".$numberIteration."_split.pos $inbam ".$outputPrefix."_".($numberIteration)."_split > ".$outputPrefix."_split.log 2> /dev/null ";
#runcmd($cmdBamSplit);
my @argsBamSplit = ( "bash", "-c", $cmdBamSplit );
if(system(@argsBamSplit) != 0){
warn "system cmd $cmdBamSplit failed with code: $?";
warn "The following command: ".$cmdBamSplit." caused a segfault\n".
"Please run the program again with a higher stack/heap limit.\n".
"WARNING: We will keep running without using --useLength nor --estdeam\n";
$estdeam = 0;
$useLength = 0;
goto SKIPESTLENGTH;
#if ($? == -1) {
# die "ERROR: failed to execute: $!\n";
#}elsif ($? & 127) {
# warn "The following command: ".$cmdBamSplit." caused a segfault\n".
# "Please run the program again with a higher stack/heap limit.\n".
# "WARNING: We will keep running without using --useLength nor --estdeam\n";
# $estdeam = 0;
# $useLength = 0;
# goto SKIPESTLENGTH;
#}else {
# die "ERROR: failed to execute: $!\n";
#}
}else{
}
#re-measure deam rateS
if ( $estdeam ) { # re-estimate deamination at each iteration
my $cmdBam2ProfEndo = $bam2prof." -length $lengthDeam -".$library." -5p ".$outputPrefix."_".($numberIteration+1)."_endo.5p.prof -3p ".$outputPrefix."_".($numberIteration+1)."_endo.3p.prof ".$outputPrefix."_".($numberIteration)."_split_endo.bam";
runcmd($cmdBam2ProfEndo);
my $cmdBam2ProfCont = $bam2prof." -length $lengthDeam -".$library." -5p ".$outputPrefix."_".($numberIteration+1)."_cont.5p.prof -3p ".$outputPrefix."_".($numberIteration+1)."_cont.3p.prof ".$outputPrefix."_".($numberIteration)."_split_cont.bam";
runcmd($cmdBam2ProfCont);
#my $cmdBam2ProfCont = $bam2prof." -length $lengthDeam -".$library." -5p ".$outputPrefix.".cont.5p.prof -3p ".$outputPrefix.".cont.3p.prof ".$outputPrefix."_cont.bam";
#runcmd($cmdBam2ProfCont);
} else {
copycmd( $outputPrefix."_".$numberIteration."_endo.5p.prof" ,$outputPrefix."_".($numberIteration+1)."_endo.5p.prof" );
copycmd( $outputPrefix."_".$numberIteration."_endo.3p.prof" ,$outputPrefix."_".($numberIteration+1)."_endo.3p.prof" );
copycmd( $outputPrefix."_".$numberIteration."_cont.5p.prof" ,$outputPrefix."_".($numberIteration+1)."_cont.5p.prof" );
copycmd( $outputPrefix."_".$numberIteration."_cont.3p.prof" ,$outputPrefix."_".($numberIteration+1)."_cont.3p.prof" );
}
#check if enough data to measure insert size
my ($returnCE, $readCountE) = runcmdReturnOutput($countRec,$outputPrefix."_".($numberIteration)."_split_endo.bam");
my ($returnCC, $readCountC) = runcmdReturnOutput($countRec,$outputPrefix."_".($numberIteration)."_split_cont.bam");
if ($returnCE !=0) {
die "Unable to determine how many reads are in ".$outputPrefix."_".($numberIteration)."_split_endo.bam";
}
if ($returnCC !=0) {
die "Unable to determine how many reads are in ".$outputPrefix."_".($numberIteration)."_split_cont.bam";
}
if ($readCountE < 50) {
warn "Not enough reads are in ".$outputPrefix."_".($numberIteration)."_split_endo.bam, we will not use insert sizes for this iteration";
}
if ($readCountC < 50) {
warn "Not enough reads are in ".$outputPrefix."_".($numberIteration)."_split_cont.bam, we will not use insert sizes for this iteration";
}
if ($useLength) { #we use the length of the molecules in the endoCaller
if ($readCountE >= 50 &&
$readCountC >= 50 ) {
#measure insert size
my $cmdBam2InsertsizeEndo = $insertSize." ".$outputPrefix."_".($numberIteration)."_split_endo.bam |gzip > ".$outputPrefix."_".($numberIteration)."_split_endo.size.gz";
runcmd($cmdBam2InsertsizeEndo);
my $cmdBam2InsertsizeCont = $insertSize." ".$outputPrefix."_".($numberIteration)."_split_cont.bam |gzip > ".$outputPrefix."_".($numberIteration)."_split_cont.size.gz";
runcmd($cmdBam2InsertsizeCont);
#Log normal fit, copying parameters for next iteration
my $cmdinsertsize2LognormEndo = $approxDist." ".$outputPrefix."_".($numberIteration)."_split_endo.size.gz > ".$outputPrefix."_".($numberIteration+1)."_split_endo.size.param";
runcmd($cmdinsertsize2LognormEndo);
my $cmdinsertsize2LognormCont = $approxDist." ".$outputPrefix."_".($numberIteration)."_split_cont.size.gz > ".$outputPrefix."_".($numberIteration+1)."_split_cont.size.param";
runcmd($cmdinsertsize2LognormCont);
$notEnoughDataSize=0;
} else {
$notEnoughDataSize=1;
}
}
} else { #if we do not use the length or re-estimate deamintion, we will re-use the deamintion profile from contDeam.pl
SKIPESTLENGTH:
copycmd( $outputPrefix."_".$numberIteration."_endo.5p.prof" ,$outputPrefix."_".($numberIteration+1)."_endo.5p.prof" );
copycmd( $outputPrefix."_".$numberIteration."_endo.3p.prof" ,$outputPrefix."_".($numberIteration+1)."_endo.3p.prof" );
copycmd( $outputPrefix."_".$numberIteration."_cont.5p.prof" ,$outputPrefix."_".($numberIteration+1)."_cont.5p.prof" );
copycmd( $outputPrefix."_".$numberIteration."_cont.3p.prof" ,$outputPrefix."_".($numberIteration+1)."_cont.3p.prof" );
}
} else { #must use the previous deamination profiles from the previous iteration for the new one
copycmd( $outputPrefix."_".$numberIteration."_endo.5p.prof" ,$outputPrefix."_".($numberIteration+1)."_endo.5p.prof" );
copycmd( $outputPrefix."_".$numberIteration."_endo.3p.prof" ,$outputPrefix."_".($numberIteration+1)."_endo.3p.prof" );
copycmd( $outputPrefix."_".$numberIteration."_cont.5p.prof" ,$outputPrefix."_".($numberIteration+1)."_cont.5p.prof" );
copycmd( $outputPrefix."_".$numberIteration."_cont.3p.prof" ,$outputPrefix."_".($numberIteration+1)."_cont.3p.prof" );
}
# measure deam params + length
if($mock == 1){
die;
}
if($previousCurrentContEst == -1){
$previousCurrentContEst = $currentContEst;
$previousCurrentContEstItSameVal = 1;
}else{
my $diffBetweenItContRate = sprintf("%.2f",abs($previousCurrentContEst-$currentContEst));
if($diffBetweenItContRate <= 0.01){
$previousCurrentContEstItSameVal++;
}else{
$previousCurrentContEstItSameVal=1;
}
$previousCurrentContEst=$currentContEst;
if( $previousCurrentContEstItSameVal >= $iterationSameCont ){
print "Reached the maximum number of iterations ($iterationSameCont) with stable contamination rate at iteration # $numberIteration, exiting\n";
last;
}
}
$numberIteration++;
} # go to begin loop
print "############################\n";
print "Iterations done\n\n";