-
Notifications
You must be signed in to change notification settings - Fork 2
/
reads_simulator.pl
2479 lines (2352 loc) · 78.6 KB
/
reads_simulator.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
$| = 1;
use FindBin '$Bin';
use Switch;
$path = $Bin;
if(@ARGV < 1) {
print "\nusage: reads_simulator.pl <num_reads> <name> [options]\n\n";
print "<name> string is used in the names of the output files.\nUse only alphanumeric, underscores and dashes in this string.\n";
print "\nThis program outputs a fasta file of reads and a .cig file and .bed file representing\nthe truth of where these reads map.\n";
print " - bed file is in one-based coords and contains both endpoints of each span.\n";
print " - cig file has a cigar string representation of the mapping coordinates, and a more human\n readable representation of the coordinates. All coords are one-based.\n";
print "Also output are: a file of indels, a file of substitutions, a file of splice forms, and a file of junctions \n";
print "and gaps crossed by the simulated reads.\n";
print "\n options:\n";
print " -readlength n : Set readlength to n>0 bases (default n = 100).\n";
print " -fraglength n,k,m : Set minimum fragment length to n bases and the max to m bases and median to k bases. m > k > n and n must be at least as long as the readlength.\n";
print " -numgenes n : Choose n>=1 genes at random from a master pool of gene models (default n = 30000).\n This only works with the four config files that have no stem (see below for more info).\n";
print " -strandspecific : Generate strand specific data\n";
print " -error x : Set the error rate that any given base is sequenced wrong to 0<=x<=1 (default x = 0.005).\n";
print " -subfreq x : Set substitution rate to 0<=x<1 (default x = 0.001).\n";
print " -indelfreq x : Set indel rate to 0<=x<1 (default x = 0.0005).\n";
print " -intronfreq x : Set intron rate to 0<=x<1 (default is determined from the\n";
print " featureqantification file).\n";
print " -tlen n : Set the length of the low quality tail to n bases (default n = 10).\n";
print " -tpercent x : Set the percent of tails that are low quality to 0<=x<=1\n (default x = 0).\n";
print " -tqual x : Set quality of the low quality tail to 0<=x<=1 (default x = 0.8).\n";
print " -nalt n : Set the number of novel splice forms per gene to n>1 (default n = 2).\n";
print " -palt x : Set the percentage of signal coming from novel splice\n forms to 0<=x<=1 (default x = 0.2).\n";
print " -sn : Add sequence number to the first column of the bed file.\n";
print " -configstem x : The stem x will be added to the four default filenames for the four\n required files. Use this if you have made your own config files (see\n below about config files and custom config files).\n";
print " E.g. \"simulator_config_geneinfo\" becomes \"simulator_config_geneinfo_x\".\n";
print " -cntstart n : Start the read counter at n (default n = 1).\n";
print " -outdir x : x is a path to a directory to write to. Default is the current directory.\n";
print " -mastercfgdir x : x is a path to a directory where the master config files are. Default is the\n current directory.\n";
print " -customcfgdir x : If you are using -configstem option, then x is a path to a directory where the\n";
print " custom config files are. Default is the directory specified by -outdir, which.\n";
print " itself defaults to the current directory.\n";
print " -usesubs x : x is a file of substitutions in the format output by this program in case you want\n";
print " to resuse them for another run with the same gene models.\n";
print " -useindels x : x is a file of indels in the format output by this program in case you want\n";
print " to resuse them for another run with the same gene models.\n";
print " -usealts x : x is a file of transcripts in the format output by this program in case you want\n";
print " to resuse them for another run with the same gene models. This must be used in\n conjunction with -configstem for the same four files used the first time (those\n will be the files ending in 'temp' if you used the master pool the first time).\n";
print "\n";
print "This program depends on four (master config) files:\n";
print " 1) simulator_config_geneinfo\n";
print " 2) simulator_config_geneseq\n";
print " 3) simulator_config_intronseq\n";
print " 4) simulator_config_featurequantifications\n\n";
print "With these files the program chooses -numgenes of them at random.\nIf you create custom config files with a suffix and use the -configstem mode\nit then uses all genes in the file.\n";
print "To create custom config files for a subset of genes in the master config, use the script:\n";
print " - make_config_files_for_subset_of_gene_ids.pl\n";
print "Run it with no parameters for the usage\n";
print "To use the custom config files with this program put them in the same directory as the script, or\n";
print "in the directory specified by -outdir (not -mastercfgdir which specifies the master config files)\n";
print "and use the option -configstem\n";
print "\n";
exit(0);
}
$name = $ARGV[1];
if($name =~ /^(-|_)/) {
print STDERR "\nError: invalid name '$name' - name cannot start with a dash or an underscore.\n\n";
exit(0);
}
if($name =~ /[^a-zA-Z0-9_\-\.]/) {
print STDERR "\nError: name must be only letters, numbers, dashes, underscores and periods.\n\n";
exit(0);
}
if(!($name =~ /[a-zA-Z0-9_\-\.]/)) {
print STDERR "\nERROR: invalid name '$name', cannot be empty and must use only letters,\n numbers, dashes, underscores and periods.\n\n";
exit(0);
}
$READLENGTH = 100;
$FRAGLENGTH = "";
$substitutionfrequency = .001;
$indelfrequency = .0005;
$base_error = .005;
$low_qual_tail_length = 10;
$percent_of_tails_that_are_low_qual = 0;
$quality_of_low_qual_tail = .8;
$percent_alt_spliceforms = .2;
$num_alt_splice_forms_per_gene = 2;
$NUMGENES = 30000;
$stem = "";
$cntstart = 1;
$outdir = "./";
$mastercfgdir = "./";
$customcfgdir = "";
$intronfrequency = -1;
BEGIN {
eval {
require Math::Random;
# If you'd ordinarily "use Module::Name qw(foo bar baz);", pass
# the qw(foo bar baz) to import here.
import Math::Random qw(:all);
};
# If the eval failed, we don't have the module
if ($@) {
print STDERR "\nOops, you must first install the Math::Random module for this to work...\n\n";
exit(0);
}
}
use Math::Random qw(:all);
$num_reads = $ARGV[0];
if(!($num_reads =~ /^\d+$/) || ($num_reads <= 0)) {
print STDERR "\nError: number of reads must be an integer, not '$ARGV[0]'.\n\n";
exit(0);
}
$sum_of_gene_counts = 0;
$sum_of_intron_counts = 0;
$genecnt=0;
$exoncount_total=0;
$introncount_total=0;
$usesubs = "false"; # will become true if there is a custom subsitutions file specified
$useindels = "false"; # will become true if there is a custom indels file specified
$usealts = "false"; # will become true if there is a custom transcripts file specified
$seq_num_in_bedfile = "false";
$strandspecific = "false";
$outputfq = "false";
$varcov = "";
$dampen = 1;
$usevarcov = "false";
$usepcd = "false";
for($i=2; $i<@ARGV; $i++) {
$option_recognized = 0;
if($ARGV[$i] eq "-sn") {
$seq_num_in_bedfile = "true";
$option_recognized = 1;
}
if($ARGV[$i] eq "-outputfq") {
$outputfq = "true";
$option_recognized = 1;
}
if($ARGV[$i] eq "-strandspecific") {
$strandspecific = "true";
$option_recognized = 1;
}
if($ARGV[$i] eq "-configstem") {
$i++;
$stem = $ARGV[$i];
$option_recognized = 1;
if($stem =~ /^(-|_)/) {
print STDERR "\nError: -configstem cannot start with a dash or an underscore.\n\n";
exit(0);
}
if($stem =~ /[^a-zA-Z0-9._-]/) {
print STDERR "\nError: -configstem must be only letters, numbers, dashes, periods and underscores.\n\n";
exit(0);
}
$simulator_config_geneinfo = "simulator_config_geneinfo_$stem";
$simulator_config_featurequantifications = "simulator_config_featurequantifications_$stem";
$simulator_config_geneseq = "simulator_config_geneseq_$stem";
$simulator_config_intronseq = "simulator_config_intronseq_$stem";
}
if($ARGV[$i] eq "-tqual") {
$i++;
$quality_of_low_qual_tail = $ARGV[$i];
$option_recognized = 1;
if(!($quality_of_low_qual_tail =~ /^\d*\.?\d*$/)) {
print STDERR "\nError: -tqual has to be non-negative and no more than one.\n\n";
exit(0);
}
$quality_of_low_qual_tail = $quality_of_low_qual_tail + 0;
if($quality_of_low_qual_tail > 1 || $quality_of_low_qual_tail < 0) {
print STDERR "\nError: -tqual has to be non-negative and no more than one.\n\n";
exit(0);
}
}
if($ARGV[$i] eq "-tpercent") {
$i++;
$percent_of_tails_that_are_low_qual = $ARGV[$i];
$option_recognized = 1;
if(!($percent_of_tails_that_are_low_qual =~ /^\d*\.?\d*$/)) {
print STDERR "\nError: -tpercent has to be non-negative and no more than one.\n\n";
exit(0);
}
$percent_of_tails_that_are_low_qual = $percent_of_tails_that_are_low_qual + 0;
if($percent_of_tails_that_are_low_qual > 1 || $percent_of_tails_that_are_low_qual < 0) {
print STDERR "\nError: -tpercent has to be non-negative and no more than one.\n\n";
exit(0);
}
}
if($ARGV[$i] eq "-tlen") {
$i++;
$low_qual_tail_length = $ARGV[$i];
$option_recognized = 1;
if(!($low_qual_tail_length =~ /^\d+$/)) {
print STDERR "\nError: -teln must be a positive integer.\n\n";
exit(0);
}
if($low_qual_tail_length < 1) {
print STDERR "\nError: -teln must be a positive integer.\nIf you want there to be no tail error don't set -tlen\n\n";
exit(0);
}
}
if($ARGV[$i] eq "-error") {
$i++;
$base_error = $ARGV[$i];
$option_recognized = 1;
if(!($base_error =~ /^\d*\.?\d*$/)) {
print STDERR "\nError: -error has to be non-negative and less than one\n\n";
exit(0);
}
$base_error = $base_error + 0;
if($base_error >= 1 || $base_error < 0) {
print STDERR "\nError: -error has to be non-negative and less than one\n\n";
exit(0);
}
}
if($ARGV[$i] eq "-nalt") {
$i++;
$num_alt_splice_forms_per_gene = $ARGV[$i];
$option_recognized = 1;
if(!($num_alt_splice_forms_per_gene =~ /^\d+$/)) {
print STDERR "\nError: -nalt has to be a postive integer.\nIf you want there to be no alternate splice forms don't set -nalt, set -palt 0\n\n";
exit(0);
}
if($num_alt_splice_forms_per_gene < 1) {
print STDERR "\nError: -nalt has to be a postive integer.\nIf you want there to be no alternate splice forms don't set -nalt, set -palt 0\n\n";
exit(0);
}
}
if($ARGV[$i] eq "-numgenes") {
$i++;
$NUMGENES = $ARGV[$i];
$option_recognized = 1;
if(!($NUMGENES =~ /^\d+$/)) {
print STDERR "\nError: -numgenes has to be a postive integer.\n\n";
exit(0);
}
if($NUMGENES < 1) {
print STDERR "\nError: -numgenes has to be a postive integer.\n\n";
exit(0);
}
}
if($ARGV[$i] eq "-cntstart") {
$i++;
$cntstart = $ARGV[$i];
$option_recognized = 1;
if(!($cntstart =~ /^\d+$/)) {
print STDERR "\nError: -cntstart has to be a postive integer.\n\n";
exit(0);
}
if($cntstart < 1) {
print STDERR "\nError: -cntstart has to be a postive integer.\n\n";
exit(0);
}
}
if($ARGV[$i] eq "-outdir") {
$i++;
$outdir = $ARGV[$i];
$outdir =~ s!/$!!;
$outdir = $outdir . "/";
$option_recognized = 1;
if(!(-e $outdir)) {
print STDERR "\nError: cannot open the directory '$outdir' specified by the -outdir option.\n\n";
exit(0);
}
}
if($ARGV[$i] eq "-varcov") {
$i++;
$varcov = $ARGV[$i];
$option_recognized = 1;
if(!(-e $varcov)) {
print STDERR "\nError: cannot open the file '$varcov' specified by the -varcov option.\n\n";
exit(0);
}
}
if($ARGV[$i] eq "-dampen") {
$i++;
$dampen = $ARGV[$i];
$option_recognized = 1;
if(!($dampen =~ /^\d*\.?\d*$/)) {
print STDERR "\nError: -dampen has to be a number between 0 and 1 (inclusive).\n\n";
exit(0);
}
$dampen = $dampen + 0;
if($dampen > 1 || $dampen < 0) {
print STDERR "\nError: -dampen has to be strictly between 0 and 1 (inclusive).\n\n";
exit(0);
}
}
if($ARGV[$i] eq "-mastercfgdir") {
$i++;
$mastercfgdir = $ARGV[$i];
$mastercfgdir =~ s!/$!!;
$mastercfgdir = $mastercfgdir . "/";
$option_recognized = 1;
if(!(-e $mastercfgdir)) {
print STDERR "\nError: cannot open the directory '$mastercfgdir' specified by the -mastercfgdir option.\n\n";
exit(0);
}
}
if($ARGV[$i] eq "-usepcd") {
$option_recognized = 1;
$usepcd = "true";
}
if($ARGV[$i] eq "-usesubs") {
$i++;
$subsfile = $ARGV[$i];
$option_recognized = 1;
$usesubs = "true";
if(!(-e $subsfile)) {
print STDERR "\nError: cannot open the file '$subsfile' specified by the -usesubs option.\n\n";
exit(0);
}
}
if($ARGV[$i] eq "-useindels") {
$i++;
$indelsfile = $ARGV[$i];
$useindels = "true";
$option_recognized = 1;
if(!(-e $indelsfile)) {
print STDERR "\nError: cannot open the file '$indelsfile' specified by the -useindels option.\n\n";
exit(0);
}
}
if($ARGV[$i] eq "-usealts") {
$i++;
$altsfile = $ARGV[$i];
$usealts = "true";
$option_recognized = 1;
if(!(-e $altsfile)) {
print STDERR "\nError: cannot open the file '$altsfile' specified by the -usealts option.\n\n";
exit(0);
}
}
if($ARGV[$i] eq "-usevarcov") {
$i++;
$varcovfile = $ARGV[$i];
$option_recognized = 1;
$usevarcov = "true";
if(!(-e $varcovfile)) {
print STDERR "\nWarning: Cannot open the file '$varcovfile' specified by the -usevarcov option,\ngoing to assume you meant not to use this option.\n\n";
$usevarcov = "false";
$usepcd = "false";
}
}
if($ARGV[$i] eq "-customcfgdir") {
$i++;
$customcfgdir = $ARGV[$i];
$customcfgdir =~ s!/$!!;
$customcfgdir = $customcfgdir . "/";
$option_recognized = 1;
if(!(-e $customcfgdir)) {
print STDERR "\nError: cannot open the directory '$customcfgdir' specified by the -customcfgdir option.\n\n";
exit(0);
}
}
if($ARGV[$i] eq "-palt") {
$i++;
$percent_alt_spliceforms = $ARGV[$i];
$option_recognized = 1;
if(!($percent_alt_spliceforms =~ /^\d*\.?\d*$/)) {
print STDERR "\nError: -palt has to be non-negative and no more than one.\n\n";
exit(0);
}
$percent_alt_spliceforms = $percent_alt_spliceforms + 0;
if($percent_alt_spliceforms > 1 || $percent_alt_spliceforms < 0) {
print STDERR "\nError: -palt has to be non-negative and no more than one.\n\n";
exit(0);
}
if($percent_alt_spliceforms == 0) {
$num_alt_splice_forms_per_gene = 0;
}
}
if($ARGV[$i] eq "-indelfreq") {
$i++;
$indelfrequency = $ARGV[$i];
$option_recognized = 1;
if(!($indelfrequency =~ /^\d*\.?\d*$/)) {
print STDERR "\nError: -indelfreq has to be strictly between 0 and 1.\n\n";
exit(0);
}
$indelfrequency = $indelfrequency + 0;
if($indelfrequency >= 1 || $indelfrequency < 0) {
print STDERR "\nError: -indelfreq has to be strictly between 0 and 1.\n\n";
exit(0);
}
}
if($ARGV[$i] eq "-intronfreq") {
$i++;
$intronfrequency = $ARGV[$i];
$option_recognized = 1;
if(!($intronfrequency =~ /^\d*\.?\d*$/)) {
print STDERR "\nError: -intronfreq has to be between 0 and 1.\n\n";
exit(0);
}
$intronfrequency = $intronfrequency + 0;
if($intronfrequency >= 1 || $intronfrequency < 0) {
print STDERR "\nError: -intronfreq has to be between 0 and 1.\n\n";
exit(0);
}
}
if($ARGV[$i] eq "-subfreq") {
$i++;
$substitutionfrequency = $ARGV[$i];
$option_recognized = 1;
if(!($substitutionfrequency =~ /^\d*\.?\d*$/)) {
print STDERR "\nError: -subfreq has to be less than one and non-negative.\n\n";
exit(0);
}
$substitutionfrequency = $substitutionfrequency + 0;
if($substitutionfrequency >= 1 || $substitutionfrequency < 0) {
print STDERR "\nError: -subfreq has to be less than one and non-negative.\n\n";
exit(0);
}
}
if($ARGV[$i] eq "-readlength") {
$i++;
$READLENGTH = $ARGV[$i];
$option_recognized = 1;
if(!($READLENGTH =~ /^\d+$/)) {
print STDERR "\nError: -readlength has to be a postive integer.\n\n";
exit(0);
}
$READLENGTH = $READLENGTH + 0;
if($READLENGTH < 1) {
print STDERR "\nError: -readlength has to be a postive integer.\n\n";
exit(0);
}
}
if($ARGV[$i] eq "-fraglength") {
$i++;
$FRAGLENGTH = $ARGV[$i];
$option_recognized = 1;
if(!($FRAGLENGTH =~ /^\d+,\d+,\d+$/)) {
print STDERR "\nError: -fraglength has to be a pair of postive integers separated by a comma, with no whitespace.\n\n";
exit(0);
}
$FRAGLENGTH =~ /^(\d+),(\d+),(\d+)$/;
$minfraglength = $1;
$medfraglength = $2;
$maxfraglength = $3;
}
if($option_recognized == 0) {
print STDERR "\nError: option $ARGV[$i] not recognized.\n\n";
exit(0);
}
}
if($usevarcov eq "true" && (!$varcov =~ /\S/)) {
print STDERR "\nError: If you use the -usevarcov option which specifies the file mapping sim genes to ivt genes,\nyou still must also specify the original varcov file itself using the -varcov option.\n\n";
exit(0);
}
if($usepcd eq "true" && $usevarcov eq "false") {
print STDERR "\nError: If you use the -usepcd option you must also be using the -usevarcov option.\n\n";
exit(0);
}
#if($minfraglength =~ /\S/ && $minfraglength < $READLENGTH) {
# print STDERR "\nError: minimum fraglength has to be at least as big as the read length.\n\n";
# exit(0);
#}
if(!($minfraglength =~ /\S/)) {
$minfraglength = $READLENGTH;
$medfraglength = $READLENGTH * 2;
$maxfraglength = $READLENGTH * 5;
}
if($usesubs eq "false" && $useindels eq "true") {
die "Error: specify either both -usesubs and -useindels or neither.\n\n";
}
if($usesubs eq "true" && $useindels eq "false") {
die "Error: specify either both -usesubs and -useindels or neither.\n\n";
}
if($customcfgdir =~ /\S/ && !($stem =~ /\S/)) {
die "\nError: if you specify -customcfgdir you must also specify -stem.\n\n";
}
if($customcfgdir =~ /\S/) {
$simulator_config_geneinfo = $customcfgdir . $simulator_config_geneinfo;
$simulator_config_featurequantifications = $customcfgdir . $simulator_config_featurequantifications;
$simulator_config_geneseq = $customcfgdir . $simulator_config_geneseq;
$simulator_config_intronseq = $customcfgdir . $simulator_config_intronseq;
} else {
$simulator_config_geneinfo = $outdir . $simulator_config_geneinfo;
$simulator_config_featurequantifications = $outdir . $simulator_config_featurequantifications;
$simulator_config_geneseq = $outdir . $simulator_config_geneseq;
$simulator_config_intronseq = $outdir . $simulator_config_intronseq;
}
if($READLENGTH <= $low_qual_tail_length && $percent_of_tails_that_are_low_qual > 0) {
print STDERR "\nERROR: low quality tail length must be less than the readlength.\n\n";
exit(0);
}
if(!($stem =~ /\S/)) {
# Here construct random set of $NUMGENES genes and call the 'make_config_files_for_subset_of_gene_ids'
# script on the master-list files
$F = $mastercfgdir . 'simulator_config_geneinfo';
$total_num_genes = `wc -l $F`;
$total_num_genes =~ /\s*(\d+)/;
$total_num_genes = $1;
$genes_temp_filename = $outdir . "genes_temp";
open(OUTFILE, ">$genes_temp_filename") or die "\nError: cannot open file '$genes_temp_filename' for writing\n\n";
for($i=0; $i<$NUMGENES; $i++) {
$R = int(rand($total_num_genes))+1;
while($Ghash{$R}+0>0) {
$R = int(rand($total_num_genes));
}
$Ghash{$R}++;
print OUTFILE "GENE.$R\n";
}
close(OUTFILE);
$x = `perl $path/make_config_files_for_subset_of_gene_ids.pl temp $genes_temp_filename $mastercfgdir $outdir`;
$simulator_config_geneinfo = $outdir . "simulator_config_geneinfo_temp";
$simulator_config_featurequantifications = $outdir . "simulator_config_featurequantifications_temp";
$simulator_config_geneseq = $outdir . "simulator_config_geneseq_temp";
$simulator_config_intronseq = $outdir . "simulator_config_intronseq_temp";
}
$logfilename = $outdir . "simulated_reads_$name" . ".log";
$bedfilename = $outdir . "simulated_reads_$name" . ".bed";
$fafilename_forward = $outdir . "simulated_reads_$name" . ".forward.fa";
$fafilename_reverse = $outdir . "simulated_reads_$name" . ".reverse.fa";
if($outputfq eq "true") {
$fafilename_forward =~ s/fa$/fq/;
$fafilename_reverse =~ s/fa$/fq/;
}
$substitutionsfilename = $outdir . "simulated_reads_substitutions_$name" . ".txt";
$indelsfilename = $outdir . "simulated_reads_indels_$name" . ".txt";
$junctionssfilename = $outdir . "simulated_reads_junctions-crossed_$name" . ".txt";
$reads2genesfilename = $outdir . "simulated_reads2genes_$name" . ".txt";
$individual_forward_and_reverse_alignments_file = $outdir . "simulated_reads_$name" . ".cig";
#$frags_file = $outdir . "simulated_fragments_$name" . ".txt";
open(SIMLOGOUT, ">$logfilename") or die "\nError: cannot open file '$logfilename' for writing\n\n";
open(SIMBEDOUT, ">$bedfilename") or die "\nError: cannot open file '$bedfilename' for writing\n\n";
open(SIMFAOUTF, ">$fafilename_forward") or die "\nError: cannot open file '$fafilename_forward' for writing\n\n";
open(SIMFAOUTR, ">$fafilename_reverse") or die "\nError: cannot open file '$fafilename_reverse' for writing\n\n";
open(SIMSUBSOUT, ">$substitutionsfilename") or die "\nError: cannot open file '$substitutionsfilename' for writing\n\n";
open(SIMINDELSOUT, ">$indelsfilename") or die "\nError: cannot open file '$indelsfilename' for writing\n\n";
open(SIMJUNCTIONSOUT, ">$junctionssfilename") or die "\nError: cannot open file '$substitutionsfilename' for writing\n\n";
open(SIMREAD2GENE, ">$reads2genesfilename") or die "\nError: cannot open file '$reads2genesfilename' for writing\n\n";
open(SIMCIGOUT, ">$individual_forward_and_reverse_alignments_file") or die "\nError: cannot open file '$individual_forward_and_reverse_alignments_file' for writing\n\n";
#open(FRAGSEQOUT, ">$frags_file") or die "\nError: cannot open file '$frags_file' for writing\n\n";
$date = `date`;
chomp($date);
print SIMLOGOUT "Simulator run: '$name'\n";
print SIMLOGOUT "started: $date\n";
$f = format_large_int($num_reads);
print SIMLOGOUT "num reads: $f\n";
print SIMLOGOUT "readlength: $READLENGTH\n";
print SIMLOGOUT "substitution frequency: $substitutionfrequency\n";
print SIMLOGOUT "indel frequency: $indelfrequency\n";
print SIMLOGOUT "base error: $base_error\n";
if($intronfrequency >= 0) {
print SIMLOGOUT "Intron frequency: $intronfrequency\n";
} else {
print SIMLOGOUT "Intron frequency: taken from featurequantification config file\n";
}
print SIMLOGOUT "low quality tail length: $low_qual_tail_length\n";
print SIMLOGOUT "percent of tails that are low quality: $percent_of_tails_that_are_low_qual\n";
print SIMLOGOUT "quality of low qulaity tails: $quality_of_low_qual_tail\n";
print SIMLOGOUT "percent of alt splice forms: $percent_alt_spliceforms\n";
print SIMLOGOUT "number of alt splice forms per gene: $num_alt_splice_forms_per_gene\n";
print SIMLOGOUT "fraglength: $minfraglength, $medfraglength, $maxfraglength\n";
if($stem =~ /\S/) {
print SIMLOGOUT "stem: $stem\n";
} else {
$f = format_large_int($NUMGENES);
print SIMLOGOUT "num genes used to simulate with: $f\n";
}
close(SIMLOGOUT);
open(SIMLOGOUT, ">>$logfilename");
print STDERR "reading the gene info file\n";
open(INFILE, $simulator_config_geneinfo) or die "\nError: cannot open file '$simulator_config_geneinfo' for reading\n\n";
$total_transcriptome_length = 0;
while($line = <INFILE>) {
chomp($line);
@a = split(/\t/,$line);
@b = split(/::::/,$a[7]);
for($i=0; $i<@b; $i++) {
$b[$i] =~ s/\(.*\)//;
$a[5] =~ s/,\s*$//;
$a[6] =~ s/,\s*$//;
$starts{$b[$i]} = $a[5];
$ends{$b[$i]} = $a[6];
@c1 = split(/,/,$a[5]);
@c2 = split(/,/,$a[6]);
my $length=0;
for(my $j=0; $j<@c1; $j++) {
$length = $length + $c2[$j]-$c1[$j];
}
$gene_length{$b[$i]} = $length;
$total_transcriptome_length = $total_transcriptome_length + $length;
$chr{$b[$i]} = $a[0];
$strand{$b[$i]} = $a[1];
}
}
close(INFILE);
print STDERR "reading the feature quantification file\n";
open(INFILE, $simulator_config_featurequantifications) or die "\nError: cannot open file '$simulator_config_featurequantifications' for reading\n\n";
while($line = <INFILE>) {
chomp($line);
if($line =~ /(exon)/) {
@a = split(/\t/,$line);
@a2 = @{$exon2gene{$a[1]}};
$N = @a2 + 0;
$exon2gene{$a[1]}[$N] = $geneid;
$gene2exon{$geneid}[$exoncnt] = $a[1];
$exon2count{$a[1]} = $a[2];
$exoncnt++;
}
if($line =~ /(intron)/) {
@a = split(/\t/,$line);
@a2 = @{$intron2gene{$a[1]}};
$N = @a2 + 0;
$intron2gene{$a[1]}[$N] = $geneid;
$gene2intron{$geneid}[$introncnt] = $a[1];
$intron2count{$a[1]} = $a[2];
$introncnt++;
$gene2introncnt{$geneid}{$a[1]} = $introncnt;
}
if($line =~ /--------/) {
$line = <INFILE>;
chomp($line);
$line =~ s/\(.*//;
$line =~ s/\s*(\+|-)$//;
$geneid = $line;
$exoncnt=0;
$introncnt=0;
$line = <INFILE>;
$line = <INFILE>;
chomp($line);
@a = split(/\t/,$line);
$gene_count[$genecnt] = $a[2];
$gene_count_hash{$geneid} = $a[2];
$sum_of_gene_counts = $sum_of_gene_counts + $gene_count[$genecnt];
$genes[$genecnt] = $geneid;
$genecnt++;
}
}
close(INFILE);
#$genecnt--;
$numgenes = $genecnt;
if($varcov =~ /\S/) {
open(IVT, "$varcov");
$ivt_counter = 0;
while($line = <IVT>) {
$line =~ /^>([^ ]*) .*(.)/;
$IVT_id[$ivt_counter] = $1;
$IVT_strand[$ivt_counter] = $2;
$line = <IVT>;
$IVT_data[$ivt_counter] = $line;
$IVTid2cnt{$IVT_id[$ivt_counter]} = $ivt_counter;
$ivt_counter++;
}
if($usevarcov eq "true") {
open(VCF, "$varcovfile");
$gene_counter = 0;
while($line = <VCF>) {
chomp($line);
@a = split(/\t/,$line);
$gene2ivt[$gene_counter] = $IVTid2cnt{$a[1]};
if($usepcd eq "true") {
$gene2percentdropoff[$gene_counter] = $a[2];
} else {
$gene2percentdropoff[$gene_counter] = 1;
}
$gene_counter++;
}
} else {
$ivto = $outdir . "IVT2GENE.txt";
open(IVTO, ">$ivto");
for($i=0; $i<$numgenes; $i++) {
$rr = int(rand($ivt_counter));
my $G = $IVT_id[$rr];
$gene2ivt[$i] = $rr;
if($dampen < 1) {
$pcd = rand();
while($pcd < $dampen) {
$pcd = rand();
}
$pcd = int($pcd * 1000) / 1000;
} else {
$pcd = 1;
}
$gene2percentdropoff[$i] = $pcd;
$j=$i+1;
print IVTO "GENE.$j\t$G\t$pcd\n";
}
close(IVTO);
}
}
$f = format_large_int(int($numgenes));
print "$f genes total\n";
$f = format_large_int(int($sum_of_gene_counts));
print "sum_of_gene_counts = $f\n";
print SIMLOGOUT "sum of gene counts: $f\n";
if($varcov =~ /\S/) {
print SIMLOGOUT "varcov file used: $varcov\n";
}
if($dampen =~ /\S/) {
print SIMLOGOUT "dampen: $dampen\n";
}
close(SIMLOGOUT);
open(SIMLOGOUT, ">>$logfilename");
# making alternate (unknown) splice forms
if($percent_alt_spliceforms > 0) {
if($usealts eq 'false') {
print STDERR "creating the alternate (unknown) splice forms\n";
}
}
for($i=0; $i<$numgenes; $i++) {
# $i is the original gene id
# gene $genecnt is the same gene as $i but will be modified to be an alternate splice form
$geneid = $genes[$i];
@a = @{$gene2exon{$geneid}};
$exoncnt = @a;
$original_gene_count = $gene_count[$i];
# the following adjusts the original count because now it will be split between
# the normal and alt spice form
$gene_count[$i] = $original_gene_count * (1-$percent_alt_spliceforms);
$genes2[$i] = $geneid;
for($j=0; $j<$num_alt_splice_forms_per_gene; $j++) {
$geneid_x = $geneid . "_$j";
$genes2[$genecnt+$j*$numgenes] = $geneid_x;
$gene_count[$genecnt+$j*$numgenes] = $original_gene_count * $percent_alt_spliceforms/$num_alt_splice_forms_per_gene;
$exoncnt_x = 0;
$str = $starts{$geneid};
$str =~ s/^\s*,\s*//;
$str =~ s/\s*,\s*$//;
@S = split(/,/,$str);
$str = $ends{$geneid};
$str =~ s/^\s*,\s*//;
$str =~ s/\s*,\s*$//;
@E = split(/,/,$str);
$FLAG1 = 0;
$FLAG2 = 0;
while($FLAG1 == 0 || $FLAG2 == 0) {
$FLAG1 = 0;
$FLAG2 = 0;
$starts_new = "";
$ends_new = "";
$exoncnt_x = 0;
delete($gene2exon{$geneid_x});
for($k=0; $k<$exoncnt; $k++) {
$flip = int(rand(2));
if($flip == 1) {
if($strand{$geneid} eq "+") {
$starts_new = $starts_new . "$S[$k],";
$ends_new = $ends_new . "$E[$k],";
}
if($strand{$geneid} eq "-") {
$starts_new = "$S[$numexons-$k-1]," . $starts_new;
$ends_new = "$E[$numexons-$k-1]," . $ends_new;
}
$gene2exon{$geneid_x}[$exoncnt_x] = $gene2exon{$geneid}[$k];
$exoncnt_x++;
$FLAG1 = 1; # This makes sure we kept at least one exon
}
else {
$FLAG2 = 1; # This makes sure we skipped at least one exon
}
}
if($exoncnt == 1) {
$FLAG2 = 1;
}
}
$starts{$geneid_x} = $starts_new;
$ends{$geneid_x} = $ends_new;
$strand{$geneid_x} = $strand{$geneid};
$chr{$geneid_x} = $chr{$geneid};
}
$genecnt++;
}
@genes = @genes2;
$genecnt = @genes;
if($usealts eq "true") {
print STDERR "reading the user specified alternate (unknown) splice forms\n";
open(INFILE, $altsfile) or die "\nError: cannot open the file '$altsfile' for reading.\n\n";
$flag = 0;
$line = <INFILE>;
chomp($line);
while($flag == 0) {
if($line =~ /---------/) {
$line = <INFILE>;
chomp($line);
$line =~ /genes\[(\d+)\] = (.*)/;
$i = $1;
$geneid = $2;
$genes[$i] = $geneid;
$line = <INFILE>;
chomp($line);
$line =~ s/.*= //;
$starts{$geneid} = $line;
$line = <INFILE>;
chomp($line);
$line =~ s/.*= //;
$ends{$geneid} = $line;
$line = <INFILE>;
chomp($line);
$line =~ s/.*= //;
$strand{$geneid} = $line;
$line = <INFILE>;
chomp($line);
$line =~ s/.*= //;
$chr{$geneid} = $line;
$line = <INFILE>;
chomp($line);
$j=0;
delete($gene2exon{$geneid});
until($line =~ /---------/ || $line eq '') {
$gene2exon{$geneid}[$j] = $line;
$j++;
$line = <INFILE>;
chomp($line);
}
}
if($line eq '') {
$flag = 1;
}
}
}
$alttranscriptsfilename = $outdir . "simulated_reads_transcripts_$name" . ".txt";
open(ALTTRANSCRIPTS, ">$alttranscriptsfilename") or die "\nError: cannot open file '$alttranscriptsfilename' for writing.\n\n";
for($i=0; $i<@genes; $i++) {
$geneid = $genes[$i];
@a = @{$gene2exon{$geneid}};
$exoncnt = @a;
$str1 = $starts{$geneid};
$str2 = $ends{$geneid};
$str3 = $strand{$geneid};
$str4 = $chr{$geneid};
print ALTTRANSCRIPTS "---------\ngenes[$i] = $geneid\n";
print ALTTRANSCRIPTS "starts = $str1\n";
print ALTTRANSCRIPTS "ends = $str2\n";
print ALTTRANSCRIPTS "strand = $str3\n";
print ALTTRANSCRIPTS "chr = $str4\n";
for($j=0; $j<$exoncnt; $j++) {
print ALTTRANSCRIPTS "$a[$j]\n";
}
}
close(ALTTRANSCRIPTS);
if($sum_of_gene_counts <= 0) {
print STDERR "\nERROR: None of the genes have expression level above zero,\ncheck your feature quantification config file.\n\n";
print SIMLOGOUT "\nERROR: None of the genes have expression level above zero,\ncheck your feature quantification config file.\n\n";
exit(0);
}
for($i=0; $i<$genecnt; $i++) {
$gene_density[$i] = $gene_count[$i] / $sum_of_gene_counts;
$gene_distribution[$i] = $gene_density[$i];
}
$dist_file = $outdir . "simulated_reads_gene_distribution_" . $name . ".txt";
open(DIST, ">$dist_file");
print DIST "gene_distribution[0] = $gene_distribution[0]\n";
for($i=1; $i<$genecnt; $i++) {
$gene_distribution[$i] = $gene_distribution[$i] + $gene_distribution[$i-1];
print DIST "$genes[$i] = $gene_distribution[$i]\n";
}
close(DIST);
$introncount_total=0;
$sum_of_intron_counts = 0;
foreach $intron (keys %intron2gene) {
$introns[$introncount_total] = $intron;
$intron_count[$introncount_total] = $intron2count{$intron};
$sum_of_intron_counts = $sum_of_intron_counts + $intron_count[$introncount_total];
$introncount_total++;
}
$exoncount_total=0;
$sum_of_exon_counts = 0;
foreach $exon (keys %exon2gene) {
$exons[$exoncount_total] = $exon;
$exon_count[$exoncount_total] = $exon2count{$exon};
$sum_of_exon_counts = $sum_of_exon_counts + $exon_count[$exoncount_total];
$exoncount_total++;
}
$f = format_large_int(int($sum_of_intron_counts));
print "sum of intron counts = $f\n";
print SIMLOGOUT "sum of intron counts = $f\n";
$f = format_large_int(int($sum_of_exon_counts));
print "sum of exon counts = $f\n";
print SIMLOGOUT "sum of intron counts = $f\n";
# The following formula is based on the fact that introns are not treated as isolated but are padded with
# the rest of the exons.
print "sum_of_exon_counts = $sum_of_exon_counts\n";
$intron_freq = (2 * $sum_of_intron_counts) / ($sum_of_exon_counts + 2 * $sum_of_intron_counts);
$iftemp = $sum_of_intron_counts / ($sum_of_exon_counts + $sum_of_intron_counts);
if($intronfrequency > -1) {
$intron_freq = $intronfrequency;
} else {
print "intron frequency: $iftemp\n";
}
print "padded intron freq = $intron_freq\n";
print SIMLOGOUT "intron frequency: $iftemp\n";
print SIMLOGOUT "padded intron frequency: $intron_freq\n";
close(SIMLOGOUT);
open(SIMLOGOUT, ">>$logfilename");
for($i=0; $i<$introncount_total; $i++) {
if($sum_of_intron_counts>0) {
$intron_density[$i] = $intron_count[$i] / $sum_of_intron_counts;
} else {
$intron_density[$i] = 0;
}
$intron_distribution[$i] = $intron_density[$i];
}
for($i=1; $i<$introncount_total; $i++) {
$intron_distribution[$i] = $intron_distribution[$i] + $intron_distribution[$i-1];
}
#for($i=0; $i<$introncount_total; $i++) {
# print "intron_distribution[$i] = $intron_distribution[$i]\t$introns[$i]\t$intron_count[$i]\n";
#}
open(INFILE, $simulator_config_geneseq) or die "\nError: cannot open file '$simulator_config_geneseq' for reading.\n\n";
# this file has seqs on one line and minus strand seqs revserse complemented
# header line looks like this: >NM_175370:chr1:58714964-58752833_-
$CNT=0;
print "\nReading gene sequences\n";
while($line = <INFILE>) {
$CNT++;
if($CNT % 10000 == 0) {
print "$CNT\n";
}
chomp($line);
$line =~ /:(.*):\d+-\d+/;
$chr = $1;
$line =~ s/:.*//;
$line =~ s/>//;
$gene_id = $line;
$SEQ = <INFILE>;
chomp($SEQ);
$seq{$gene_id} = $SEQ;
@S = split(/,/, $starts{$gene_id});
@E = split(/,/, $ends{$gene_id});
$offset = 0;
for($exon=0; $exon<@S; $exon++) {
$S[$exon]++;
$len = $E[$exon] - $S[$exon] + 1;
$EXONSEQ = substr($SEQ,$offset,$len);
$exonname = $chr . ":" . $S[$exon] . "-" . $E[$exon];
$exonseq{$exonname} = $EXONSEQ;
$offset = $offset + $len;
}
}
close(INFILE);
open(INFILE, $simulator_config_intronseq) or die "\nError: cannot open file '$simulator_config_intronseq' for reading.\n\n";
print "\nReading intron sequences\n";
$flag = 0;
$line = <INFILE>;
$CNT=0;
while($flag == 0) {
$CNT++;
if($CNT % 10000 == 0) {
print "$CNT\n";
}
$line =~ /^>(.*)/;
$intron = $1;
$seq = "";
$line = <INFILE>;
until($line =~ /^>/ || $flag == 1) {
chomp($line);
if($line !~ /\S/) {
$flag = 1;
}
else {
if($line !~ /^>/) {
$seq = $seq . $line;
}
}
$line = <INFILE>;
}