-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathgargammel.pl
executable file
·1891 lines (1481 loc) · 47.6 KB
/
gargammel.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 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;
sub checkDeamParam{
my ($briggs,$matfile) = @_;
if ( (defined $matfile) ) {
if ( !(-e $matfile."5.dat")) {
die "Matrix file ".$matfile."5.dat does not exists\n";
}
if ( !(-e $matfile."3.dat")) {
die "Matrix file ".$matfile."3.dat does not exists\n";
}
}
if ( (defined $briggs) ) {
my @arr=split(",",$briggs);
if ($#arr != 3) {
die "The option for -damage must be 4 comma-delimited numbers, found ".($#arr+1)." parameters in ".$briggs."\n";
}
if (!looks_like_number($arr[0])) {
die "The option for -damage must be 4 comma-delimited numbers, parameter 1 :".$arr[0]." must be a number\n";
}
if (!looks_like_number($arr[1])) {
die "The option for -damage must be 4 comma-delimited numbers, parameter 2 :".$arr[1]." must be a number\n";
}
if (!looks_like_number($arr[2])) {
die "The option for -damage must be 4 comma-delimited numbers, parameter 3 :".$arr[2]." must be a number\n";
}
if (!looks_like_number($arr[3])) {
die "The option for -damage must be 4 comma-delimited numbers, parameter 4 :".$arr[3]." must be a number\n";
}
}
if ( (defined $matfile) &&
(defined $briggs) ) {
die "Specify either -matfile or -damage but not both\n";
}
}
sub copycmd{
my ($source,$destination) = @_;
print STDERR "copying ". $source." to ".$destination."\n";
if($mock != 1){
copy( $source,$destination ) or die "Copy file failed: $!";
}
}
sub runcmd{
my ($cmdtorun) = @_;
print STDERR "running cmd ". $cmdtorun."\n";
if($mock != 1){
my @argstorun = ( "bash", "-c", $cmdtorun );
if(system(@argstorun) != 0){
die "system cmd $cmdtorun failed: $?"
}else{
}
}
}
sub runcmdforce{
my ($cmdtorun) = @_;
print STDERR "running cmd ". $cmdtorun."\n";
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 listdir {
my ($dirtolist) = @_;
my @arrayToReturn;
opendir(DIR,$dirtolist) or die "Cannot open directory ".$!;
while(my $dir = readdir(DIR)){
if($dir eq "."){
next;
}
if($dir eq ".."){
next;
}
if(-d $dirtolist."".$dir){
#print "dir ".$dirtolist."".$dir."\n";
push(@arrayToReturn,$dirtolist."".$dir."/");
}
}
close(DIR);
return @arrayToReturn;
}
sub listdirFai {
my ($dirtolist) = @_;
my @arrayToReturn;
opendir(DIR,$dirtolist) or die "Cannot open directory ".$!;
while(my $dir = readdir(DIR)){
if($dir eq "."){
next;
}
if($dir eq ".."){
next;
}
if(-f $dirtolist."".$dir){
#print "dir ".$dirtolist."".$dir."\n";
if($dir =~ /.fai$/ ){
push(@arrayToReturn,$dirtolist."".$dir);
}
}
}
close(DIR);
return @arrayToReturn;
}
sub listdirFa {
my ($dirtolist) = @_;
my @arrayToReturn;
opendir(DIR,$dirtolist) or die "Cannot open directory ".$!;
while(my $dir = readdir(DIR)){
if($dir eq "."){
next;
}
if($dir eq ".."){
next;
}
if(-f $dirtolist."".$dir){
if($dir =~ /.fa$/ ||
$dir =~ /.fa.gz$/ ||
$dir =~ /.fna$/ ||
$dir =~ /.fasta$/ ||
$dir =~ /.fasta.gz$/ ){
push(@arrayToReturn,$dirtolist."".$dir);
}
}
}
close(DIR);
return @arrayToReturn;
}
sub listdirCdir {
my ($dirtolist) = @_;
my @arrayToReturn;
opendir(DIR,$dirtolist) or die "Cannot open directory ".$!;
while(my $dir = readdir(DIR)){
if($dir eq "."){
next;
}
if($dir eq ".."){
next;
}
if(-d $dirtolist."".$dir){
#print "dir ".$dirtolist."".$dir."\n";
#if($dir =~ /^C[0-9]+$/ ){
push(@arrayToReturn,$dirtolist."".$dir);
#}else{
}
}
close(DIR);
return @arrayToReturn;
}
sub fileExists{
my ($exeFile) = @_;
if (!( -e $exeFile)) {
die "Executable ".$exeFile." does not exist\n";
}
}
sub isZipped{
my ($fileToTest) = @_;
if(runcmdReturnOutput("gzip -t ".$fileToTest) eq "0"){
return 1;
}else{
return 0;
}
}
my @arraycwd=split("/",abs_path($0));
pop(@arraycwd);
my $pathdir = join("/",@arraycwd);
my $adptsim = $pathdir."/src/adptSim";
my $deamsim = $pathdir."/src/deamSim";
my $fragsim = $pathdir."/src/fragSim";
my $artprog = $pathdir."/art_src_MountRainier/art_illumina";
fileExists($adptsim);
fileExists($deamsim);
fileExists($fragsim);
fileExists($artprog);
my $fraglength=35;
my $numberOfFragments=1000;
my $comp = "0,0,1";
my $minsize=0;
my $maxsize=1000;
my $adapterF="AGATCGGAAGAGCACACGTCTGAACTCCAGTCACCGATTCGATCTCGTATGCCGTCTTCTGCTTG";
my $adapterR="AGATCGGAAGAGCGTCGTGTAGGGAAAGAGTGTAGATCTCGGTGGTCGCCGTATCATTT";
my $readlength=75;
my $distmis=1;
my $qs =0;
my $qs2=0;
sub usage
{
print "Unknown option: @_\n" if ( @_ );
print "\n\n
This script is a wrapper to run the different programs to create
a set of Illumina reads for ancient DNA from a set of fasta files
representing the endogenous, the contaminant from the same species
and the bacterial contamination.
\n\n usage:\t".$0." <options> [directory with fasta directories] \n\n".
" This directory should contain 3 directories:\n".
" \tbact/ The bacterial contamination\n".
" \tcont/ The contamination from the same species\n".
" \tendo/ The endogenous material\n".
" \n".
" Options:\n".
"\t--comp [B,C,E]\t\t\t\tComposition of the final set in fraction \n".
"\t\t\t\t\t\tthe 3 numbers represent the bacterial, contaminant and endogenous\n".
"\t\t\t\t\t\tex: --comp 0.6,0.02,0.38 will result\n".
"\t\t\t\t\t\tin 60% bacterial contamination while the rest will be from the same\n".
"\t\t\t\t\t\tspecies 5% will be contamination and 95% will be endogenous\n".
"\t\t\t\t\t\tDefault: --comp ".$comp."\n".
"\t--mock\t\t\t\t\tDo nothing, just print the commands that will be run\n".
"\t-o\t\t\t\t\tOutput prefix (default: [input dir]/simadna)\n".
" Either specify:\n".
"\t\t-n\t[number]\t\tGenerate [number] fragments (default: ".$numberOfFragments.")\n".
"\t\t-c\t[coverage]\t\tEndogenous coverage\n".
"\n".
" Fragment selection\n".
" ===================\n".
" \t\t--misince\t[file]\t\tBase misincorporation for the endogenous fragments (default none)\n".
" \t\t--misincc\t[file]\t\tBase misincorporation for the contaminant fragments (default none)\n".
" \t\t--misincb\t[file]\t\tBase misincorporation for the bacterial fragments (default none)\n".
" \t\t--distmis\t[dist]\t\tDistance to consider for base misincorporation (default ".$distmis.")\n".
" \t\t\t\t\t\tthis file is obtained from mapDamage\n".
"\n".
" \tFragment size distribution: specify either one of the 3 possible options:
-l\t[length]\t\tGenerate fragments of fixed length (default: ".$fraglength.")
-s\t[file]\t\t\tOpen file with size distribution (one fragment length per line)
-f\t[file]\t\t\tOpen file with size frequency in the following format:
\t\tlength[TAB]freq ex:
\t\t40 0.0525
\t\t41 0.0491
\t\t...
Length options:
\t--loc\t[file]\t\tLocation for lognormal distribution (default none)
\t--scale\t[file]\t\tScale for lognormal distribution (default none)\n\n".
" \tFragment size limit:
--minsize\t[length]\tMinimum fragments length (default: ".$minsize.")
--maxsize\t[length]\tMaximum fragments length (default: ".$maxsize.")\n".
"\n".
" \tFragment methylation:
--methyl\t\t\tAllow for lowercase C and G to denote
\t\t\t\tmethylated cytosines on the + and - strand respectively
\t\t\t\t(default: not used)\n".
"\n".
" Deamination\n".
" ===================\n".
" To add deamination to the bacterial and endogenous material,you can specify\n".
" either one of these options:\n".
"\t-mapdamage\t[mis.txt] [protocol]\tRead the miscorporation file [mis.txt]
\t\tproduced by mapDamage
\t\t[protocol] can be either \"single\" or \"double\" (without quotes)
\t\tSingle strand will have C->T damage on both ends
\t\tDouble strand will have and C->T at the 5' end and G->A damage at the 3' end".
"\n".
"\t-matfile\t[matrix file prefix]\tRead the matrix file of substitutions
Provide the prefix only, both files must end with
5.dat and 3.dat\n".
"\t-damage\t\t[v,l,d,s] \tFor the Briggs et al. 2007 model
\t\tThe parameters must be comma-separated e.g.: -damage 0.03,0.4,0.01,0.3
\t\t\tv: nick frequency
\t\t\tl: length of overhanging ends (geometric parameter)
\t\t\td: prob. of deamination of Cs in double-stranded parts
\t\t\ts: prob. of deamination of Cs in single-stranded parts\n".
"\n".
" Alternatively, you can specify these options independently for the endogenous (e), bacterial (b)\n".
" and present-day human contaminant (c) using the following options:\n".
"\t-mapdamagee\t[mis.txt] [protocol]\tEndogenous mapDamage misincorporation file\n".
"\t-matfilee\t[matrix file prefix]\tEndogenous matrix file of substitutions\n".
"\t-damagee\t[v,l,d,s] \tEndogenous Briggs parameters\n".
"\t-mapdamageb\t[mis.txt] [protocol]\tBacterial mapDamage misincorporation file\n".
"\t-matfileb\t[matrix file prefix]\tBacterial matrix file of substitutions\n".
"\t-damageb\t[v,l,d,s] \tBacterial Briggs parameters\n".
"\t-mapdamagec\t[mis.txt] [protocol]\tHuman contaminant mapDamage misincorporation file\n".
"\t-matfilec\t[matrix file prefix]\tHuman contaminant matrix file of substitutions\n".
"\t-damagecd\t[v,l,d,s] \tHuman contaminant Briggs parameters\n".
"\n please note that if you do specify deamination for one source but not for another, no deamination will be added\n".
"\n".
" If using --methyl, you can also specify different matrix file for methylated\n".
"\t-matfilenonmeth\t[matrix file prefix]\tRead the matrix file of substitutions for non-methylated Cs
\tProvide the prefix only, both files must end with
\t5.dat and 3.dat
\t-matfilemeth\t[matrix file prefix]\tRead the matrix file of substitutions for methylated Cs
\tProvide the prefix only, both files must end with
\t5.dat and 3.data".
"\n".
" Adapter and sequencing\n".
" ===================\n".
" -fa [seq] \tAdapter that is observed after the forward read (Default: ".substr($adapterF,0,10)."...)
-sa [seq] \tAdapter that is observed after the reverse read (Default: ".substr($adapterR,0,10)."...)
-rl [length] \tDesired read length (Default: ".$readlength.")
-se \tuse single-end sequencing (Default: paired-end)
\tThe following options change the sequencing error rate, please note that positive factor
\twill decrease the rate of such errors and a negative one will increase it.
-qs [factor] \tIncrease error rate for forward reads by a factor of 1/(10^([factor]/10)) (Default: ".$qs.")
-qs2 [factor] \tIncrease error rate for reverse reads by a factor of 1/(10^([factor]/10)) (Default: ".$qs2.")
-ss [system] \tIllumina platfrom to use, the parentheses indicate the max. read length
\tuse the shorthand in the left column:
(single-end, paired-end)
\t\t\t\t\t\t GA2 - GenomeAnalyzer II ( 50bp, 75bp)
\t\t\t\t\t\t HS20 - HiSeq 2000 ( 100bp, 100bp)
\t\t\t\t\t\t HS25 - HiSeq 2500 ( 125bp, 150bp) (Default)
\t\t\t\t\t\t HSXt - HiSeqX TruSeq ( 150bp, 150bp)
\t\t\t\t\t\t MSv1 - MiSeq v1 ( 250bp, 250bp)
\t\t\t\t\t\t MSv3 - MiSeq v3 ( 250bp, 250bp)".
"\n\n".
"\n\n".
#" Output Options:\n".
#" Input Options:\n".
"\n\n";
exit;
}
my $uniq =0;
my $starttime = time;
my $coverage=undef;
my $help;
my $outputprefix;
my $compB;
my $compC;
my $compE;
my $filefragsize;
my $filefragfreqsize;
my $loc;
my $scale;
my $misince;
my $misincb;
my $misincc;
my @mapdamage;
my $matfile;
my $matfilenonmeth;
my $matfilemeth;
my $briggs;
my @mapdamagee;
my $matfilee;
my $briggse;
my @mapdamageb;
my $matfileb;
my $briggsb;
my @mapdamagec;
my $matfilec;
my $briggsc;
my $se=0;
my $ss;
my $fa;
my $sa;
my $rl;
my $methyl;
my $dirWithChr = $ARGV[$#ARGV];
usage() if ( @ARGV < 1 or
! GetOptions('help|?' => \$help, 'mock' => \$mock, 'methyl' => \$methyl, 'uniq' => \$uniq, 'se' => \$se, 'ss=s' => \$ss, 'distmis=i' => \$distmis, 'misince=s' => \$misince,'misincb=s' => \$misincb,'misincc=s' => \$misincc, 'comp=s' => \$comp,'mapdamage=s{2}' => \@mapdamage, 'mapdamagee=s{2}' => \@mapdamagee, 'mapdamageb=s{2}' => \@mapdamageb, 'mapdamagec=s{2}' => \@mapdamagec,'matfile=s' => \$matfile, 'damage=s' => \$briggs,'matfilee=s' => \$matfilee,'matfilenonmeth=s' => \$matfilenonmeth, ,'matfilemeth=s' => \$matfilemeth, 'damagee=s' => \$briggse,'matfileb=s' => \$matfileb, 'damageb=s' => \$briggsb,'matfilec=s' => \$matfilec, 'damagec=s' => \$briggsc,'o=s' => \$outputprefix, 'n=i' => \$numberOfFragments,'l=i' => \$fraglength, 's=s' => \$filefragsize, 'f=s' => \$filefragfreqsize, 'loc=s' => \$loc, 'fa=s' => \$fa, 'sa=s' => \$sa, 'rl=s' => \$rl, 'scale=s' => \$scale, 'c=f' => \$coverage, 'minsize=i' => \$minsize,'maxsize=i' => \$maxsize,'qs=i' => \$qs,'qs2=i' => \$qs2)
or defined $help );
if( !(defined $ss) ){
$ss = "HS25";
}
if( defined $fa ){
$adapterF=uc($fa);
}
if( defined $sa ){
$adapterR=uc($sa);
}
if( defined $rl ){
$readlength=$rl;
}
if(index($adapterF, "N") != -1) {
my $tempadapStr="";
my @DNAalphabet = ("A","C","G","T");
for(my $i=0;$i<length($adapterF);$i++){
if(substr($adapterF,$i,1) eq "N"){
$tempadapStr=$tempadapStr.$DNAalphabet[int(rand(4))];
}else{
$tempadapStr=$tempadapStr.substr($adapterF,$i,1);
}
}
warn "WARNING: Unresolved bases found in forward adapter\n"."Replaced:\n".$adapterF."\nby random bases:\n".$tempadapStr."\n";
$adapterF=$tempadapStr;
}
if(index($adapterR, "N") != -1) {
my $tempadapStr="";
my @DNAalphabet = ("A","C","G","T");
for(my $i=0;$i<length($adapterR);$i++){
if(substr($adapterR,$i,1) eq "N"){
$tempadapStr=$tempadapStr.$DNAalphabet[int(rand(4))];
}else{
$tempadapStr=$tempadapStr.substr($adapterR,$i,1);
}
}
warn "WARNING Unresolved bases found in reverse adapter\n"."Replaced:\n".$adapterR."\nby random bases:\n".$tempadapStr."\n";
$adapterR=$tempadapStr;
}
if ($ss eq "GA2") { #- GenomeAnalyzer II (50bp, 75bp)
if ($se) {
if ($readlength>50) {
die "Read length ".$readlength." is greater than the one allowed by the platform\n";
}
} else {
if ($readlength>75) {
die "Read length ".$readlength." is greater than the one allowed by the platform\n";
}
}
} else {
if ($ss eq "HS20") { #- HiSeq 2000 (100bp,100bp)
#if ($se) {
if ($readlength>100) {
die "Read length ".$readlength." is greater than the one allowed by the platform\n";
}
#} else {
# die "The platform does not provide paired-end sequencing\n";
#}
} else {
if ($ss eq "HS25") { #- HiSeq 2500 (125bp, 150bp) (Default)
if ($se) {
if ($readlength>125) {
die "Read length ".$readlength." is greater than the one allowed by the platform\n";
}
} else {
if ($readlength>150) {
die "Read length ".$readlength." is greater than the one allowed by the platform\n";
}
}
} else {
if ($ss eq "HSXt") { #- HiSeqX TruSeq (150bp,150bp)
#$if ($se) {
if ($readlength>150) {
die "Read length ".$readlength." is greater than the one allowed by the platform\n";
}
#} else {
# die "The platform does not provide paired-end sequencing\n";
#}
} else{
if ($ss eq "MSv1") { #- MiSeq v1 (250bp,250bp)
#if ($se) {
if ($readlength>250) {
die "Read length ".$readlength." is greater than the one allowed by the platform\n";
}
#} else {
# die "The platform does not provide paired-end sequencing\n";
# }
} else {
if ($ss eq "MSv3") { #- MiSeq v3 (250bp,250bp)
#if ($se) {
if ($readlength>250) {
die "Read length ".$readlength." is greater than the one allowed by the platform\n";
}
#} else {
# die "The platform does not provide paired-end sequencing\n";
#}
} else {
die "Invalid sequencing platform ".$ss."\n";
}
}#not msv1
}#not HSXt
}#not HS25
}#not HS20
}#not GA2
if( (defined $misince) ){
if( !(-e $misince)){
die "Endogenous misincorporation file does not exist\n";
}
}
if( (defined $misincc) ){
if( !(-e $misincc)){
die "Contaminant misincorporation file does not exist\n";
}
}
if( (defined $misincb) ){
if( !(-e $misincb)){
die "Bacterial misincorporation file does not exist\n";
}
}
if( ($qs < -93) || ($qs > 93) ){
die "Please enter an integer between -93 and 93 for qs\n";
}
if( ($qs2 < -93) || ($qs2 > 93) ){
die "Please enter an integer between -93 and 93 for qs2\n";
}
if ($se) {
if( ($qs2 != 0 ) ){
die "Do not enter a value for -qs2 when using single-end\n";
}
}
checkDeamParam($briggs, $matfile );
checkDeamParam($briggse,$matfilee);
checkDeamParam($briggsb,$matfileb);
checkDeamParam($briggsc,$matfilec);
if ( (defined $matfile ) ||
(defined $matfilenonmeth ) ||
(defined $matfilemeth ) ||
(defined $briggs ) ||
(@mapdamage ) ) {
if ( (defined $matfilee) ||
(defined $briggse ) ) {
die "Specify either patterns for endogenous and bacterial using either -matfile or -damage but do not specify other parameters\n";
}
if ( (defined $matfileb) ||
(defined $briggsb ) ) {
die "Specify either patterns for endogenous and bacterial using either -matfile or -damage but do not specify other parameters\n";
}
if ( (defined $matfilec) ||
(defined $briggsc ) ) {
die "Specify either patterns for endogenous and bacterial using either -matfile or -damage but do not specify other parameters\n";
}
if(defined $matfile){
$matfilee = $matfile;
$matfileb = $matfile;
}
if(defined $briggs){
$briggse = $briggs;
$briggsb = $briggs;
}
if(defined $matfilenonmeth){
if( not(defined $methyl) ){
die "Must define --methyl for the fragment selection\n";
}
}
if(defined $matfilemeth){
if( not(defined $methyl) ){
die "Must define --methyl for the fragment selection\n";
}
}
if( (not(defined $matfilenonmeth) &&
( defined $matfilemeth) )
||
( (defined $matfilenonmeth) &&
not(defined $matfilemeth) )){
die "Must defined matrix file for both methylated and non-methylated";
}
if( (defined $matfilenonmeth) ||
(defined $matfilemeth ) ){
if( (defined $matfilee) ||
(defined $matfileb) ||
(defined $matfilec) ) {
die "Cannot specify as of yet, different deamination profiles for the endogenous, bacterial, contaminant and methylation deamination\n";
}
}
if(@mapdamage){
@mapdamagee = @mapdamage;
@mapdamageb = @mapdamage;
}
}
if( defined $methyl ){
if( not(defined $matfilenonmeth) ){
die "Must define --matfilenonmeth if methylation is used\n";
}
}
if( defined $methyl ){
if( not(defined $matfilemeth) ){
die "Must define --matfilemeth if methylation is used\n";
}
}
if( (defined $loc) &&
!(defined $scale) ){
die "Must specify both --loc and --scale, not just one";
}
if( !(defined $loc) &&
(defined $scale) ){
die "Must specify both --loc and --scale, not just one";
}
if( (defined $filefragsize) ){
if($fraglength!=35){
die "Cannot specify both -l and -s";
}
}
if( (defined $filefragfreqsize) ){
if($fraglength!=35){
die "Cannot specify both -l and -f";
}
}
if( (defined $filefragsize) &&
(defined $filefragfreqsize) ){
die "Cannot specify both -s and -f";
}
my @arraycomp = split(",",$comp);
if($#arraycomp != 2){
die "the --comp option must have 3 comma-delimited fields, found ".($#arraycomp+1);
}
if( (defined $coverage) &&
$numberOfFragments!=1000){
die "Must use the -c or -n but not both";
}
$compB=$arraycomp[0];
$compC=$arraycomp[1];
$compE=$arraycomp[2];
if(!looks_like_number($compB)){
die "The --comp option must have 3 comma-delimited numbers";
}
if(!looks_like_number($compC)){
die "The --comp option must have 3 comma-delimited numbers";
}
if(!looks_like_number($compE)){
die "The --comp option must have 3 comma-delimited numbers";
}
if( ($compB+$compC+$compE) != 1){
die "The --comp option must have 3 comma-delimited numbers that sum up to 1, current sum ".($compB+$compC+$compE);
}
if( (defined $coverage) && ($compE==0) ){
die "Cannot use the endogenous coverage -c and not specify some level of endogenous content.";
}
if(substr($dirWithChr,length($dirWithChr)-1,1) ne "/"){
$dirWithChr = $dirWithChr."/";
}
if( !(defined $outputprefix) ){
$outputprefix = $dirWithChr."simadna";
}else{
}
=comment
my @arrayofdirs=listdir($dirWithChr);
@arrayofdirs=sort(@arrayofdirs);
if($#arrayofdirs != 2){
die "The input directory must contain 3 directories:
bact/
endo/
cont/
";
}
if($arrayofdirs[0] ne $dirWithChr."bact/" ||
$arrayofdirs[1] ne $dirWithChr."cont/" ||
$arrayofdirs[2] ne $dirWithChr."endo/" ){
die "The input directory must contain 3 directories named:
bact/
endo/
cont/
";
}
=cut
my $bactdir = $dirWithChr."bact/";
my $contdir = $dirWithChr."cont/";
my $endodir = $dirWithChr."endo/";
if( !(-d $bactdir) || !(-d $contdir) || !(-d $endodir)) {
die "The input directory must contain 3 directories named:
bact/
endo/
cont/
";
}
my @arrayofFilesbact = listdirFa( $bactdir );
my @arrayofFilescont = listdirFa( $contdir );
my @arrayofFilesendo = listdirFa( $endodir );
my @arrayofCdirsendo = listdirCdir( $endodir );
my $sumB=0;
my $sumC=0;
my $sumE=0;
my @arrayofFilesbactSL;
my @arrayofFilesbactL;
my @arrayofFilesbactLfrac;
my @arrayofFilesbactToExtract;
my @arrayofFilesbactLFromList;
my @arrayofFilesbactLFromListW;
my @arrayofFilesbactLFromListB;
my @arrayofFilesbactLFromListP;
if($compB>0){
if($#arrayofFilesbact==-1){
die "If you want bacterial contamination, please have at least one file in the bact/ directory\n";
}
if( !( -e $bactdir."/list" ) ){
die "List file ".$bactdir."/list does not exist, this file must contain the list of files and their weight ex:
file1.fa\t0.3
file2.fa\t0.2
file3.fa\t0.15
file4.fa\t0.12
file5.fa\t0.1
file6.fa\t0.7
file7.fa\t0.5
\n";
}else{
my $sumWeight=0;
open(FILE,$bactdir."/list");
while(my $line = <FILE>){
if($line =~ /^(\S+)\s+(\S+)$/){
if(!looks_like_number($2)){
die "Cannot parse line from bacterial list: ".$bactdir."/list must be:\nfile\tweight(between 0 and 1)\nfound line:".$line;
}
push(@arrayofFilesbactLFromList, $1);
push(@arrayofFilesbactLFromListB, 0);
push(@arrayofFilesbactLFromListW,$2);
$sumWeight+=$2;
}else{
die "Cannot parse line from bacterial list: ".$bactdir."/list line:".$line;
}
}
close(FILE);
if($sumWeight<0.99 || $sumWeight>1.01 ){
die "Problem from bacterial list: ".$bactdir."/list sum is not 1, found: ".$sumWeight;
}
}
}
foreach my $fafile (@arrayofFilesbact){
print STDERR "Found bacterial contaminant file ".$fafile."\n";
if(isZipped($fafile)){
die "The following file ".$fafile." is zipped, please unzip it\n";
}
if(!(-f $fafile.".fai")){
my $cmd = "samtools faidx $fafile";
runcmdforce($cmd);
}else{
print STDERR "Found bacterial contaminant indx ".$fafile.".fai\n";
}
my $sumForFile=0;
open(FILE,$fafile.".fai");
while(my $line = <FILE>){
if($line =~ /(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/){
$sumB+=$2;
$sumForFile+=$2;
}else{
die "Cannot parse line from fasta index ".$fafile.".fai";
}
}
close(FILE);
for(my $i=0;$i<=$#arrayofFilesbactLFromList;$i++){
print $bactdir.$arrayofFilesbactLFromList[$i]."\n";
print $fafile."\n";
if($bactdir.$arrayofFilesbactLFromList[$i] eq $fafile){
if($arrayofFilesbactLFromListB[$i] == 1){
die "Fasta ".$fafile." was found twice in the list";
}
$arrayofFilesbactLFromListB[$i] = 1;
}
}
push(@arrayofFilesbactL, $sumForFile);
push(@arrayofFilesbactSL,$sumB);
push(@arrayofFilesbactToExtract,0);
}
for(my $i=0;$i<=$#arrayofFilesbactLFromList;$i++){
if($arrayofFilesbactLFromListB[$i] == 0){
die "Fasta ".$arrayofFilesbactLFromList[$i]." was not found in the directory but it was found in the list\n";
}
}
my $sumOfListW=0;
push(@arrayofFilesbactLFromListP,$sumOfListW);
for(my $i=0;$i<=$#arrayofFilesbactLFromListW;$i++){
#print $i."\t".$arrayofFilesbactLFromListW[$i]."\n";
$sumOfListW += $arrayofFilesbactLFromListW[$i];
push(@arrayofFilesbactLFromListP,$sumOfListW);
}
if($compC>0){
if($#arrayofFilescont==-1){
die "If you want contamination, please have at least one file in the cont/ directory\n";
}
}
my @arrayofFilescontSL;
my @arrayofFilescontL;
my @arrayofFilescontLfrac;
my @arrayofFilescontToExtract;
foreach my $fafile (@arrayofFilescont){
print STDERR "Found present-day human contamination file ".$fafile."\n";
if(isZipped($fafile)){
die "The following file ".$fafile." is zipped, please unzip it\n";
}
if(!(-f $fafile.".fai")){
my $cmd = "samtools faidx $fafile";
runcmdforce($cmd);
}else{
print STDERR "Found present-day human contamination index ".$fafile.".fai\n";
}
my $sumForFile=0;
open(FILE,$fafile.".fai");
while(my $line = <FILE>){
if($line =~ /(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/){
$sumC+=$2;
$sumForFile+=$2;
}else{
die "Cannot parse line from fasta index ".$fafile.".fai";
}
}
close(FILE);
push(@arrayofFilescontL, $sumForFile);
push(@arrayofFilescontSL,$sumC);
push(@arrayofFilescontToExtract,0);
}
foreach my $s (@arrayofFilescontL){
push( @arrayofFilescontLfrac, ($s/$sumC) );
}