-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsimuG.pl
executable file
·4378 lines (4086 loc) · 232 KB
/
simuG.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 warnings FATAL => 'all';
use strict;
use Getopt::Long qw(GetOptions);
use Pod::Usage qw(pod2usage);
use List::Util qw(sum min max shuffle);
# use Data::Dumper;
##############################################################
# script: simuG.pl
# author: Jia-Xing Yue (GitHub ID: yjx1217)
# version: 1.0.0+
# last edited: 2021.03.25
# description: simuG.pl can simulate genome sequences with pre-defined or random genomic variants of full spectrum (e.g. SNP, INDEL, CNV, inversions, and translocations).
##############################################################
###################
# input parameters
###################
# General options
my $help;
my $man;
my $version;
# Options for the reference genome
my $refseq;
my $excluded_chr_list;
# Options for SNP variants simulation
my $snp_vcf;
my $snp_count;
my $snp_model;
my $titv_ratio = 0.5;
my $coding_partition_for_snp_simulation;
# Options for INDEL variants simulation
my $indel_vcf;
my $indel_count;
my $indel_model;
my $ins_del_ratio = 1.0;
my $indel_size_powerlaw_alpha = 2.0;
my $indel_size_powerlaw_constant = 0.5;
# Options for copy-number variants simulation
my $cnv_vcf;
my $cnv_count;
my $cnv_gain_loss_ratio = 1.0;
my $duplication_tandem_dispersed_ratio = 1.0;
my $cnv_max_copy_number = 10;
my $cnv_min_size = 100;
my $cnv_max_size = 10000;
# Options for inversions simulation
my $inversion_vcf;
my $inversion_count;
my $inversion_min_size = 1000;
my $inversion_max_size = 100000;
my $inversion_breakpoint_gff;
# Options for translocation simulation
my $translocation_vcf;
my $translocation_count;
my $translocation_breakpoint_gff;
# Option for defining centromere for CNV/inversion/translocation simulation
my $centromere_gff;
# Option for defining genes for SNP/INDEL/CNV/inversion/translocation simulation
my $gene_gff;
# Options for setting random seed and output file prefix
my $seed;
my $prefix = "output_prefix";
GetOptions(
'help|h|?' => \$help,
'man|m' => \$man,
'version|ver' => \$version,
'refseq|r:s' => \$refseq,
'excluded_chr_list:s' => \$excluded_chr_list,
'snp_vcf:s' => \$snp_vcf,
'snp_count:i' => \$snp_count,
'snp_model:s' => \$snp_model,
'titv_ratio:s' => \$titv_ratio,
'coding_partition_for_snp_simulation:s' => \$coding_partition_for_snp_simulation,
'indel_vcf:s' => \$indel_vcf,
'indel_count:i' => \$indel_count,
'indel_model:s' => \$indel_model,
'ins_del_ratio:s' => \$ins_del_ratio,
'indel_size_powerlaw_alpha:f' => \$indel_size_powerlaw_alpha,
'indel_size_powerlaw_constant:f' => \$indel_size_powerlaw_constant,
'cnv_vcf:s' => \$cnv_vcf,
'cnv_count:i' => \$cnv_count,
'cnv_gain_loss_ratio:s' => \$cnv_gain_loss_ratio,
'duplication_tandem_dispersed_ratio:s' => \$duplication_tandem_dispersed_ratio,
'cnv_max_copy_number:i' => \$cnv_max_copy_number,
'cnv_min_size:i' => \$cnv_min_size,
'cnv_max_size:i' => \$cnv_max_size,
'inversion_vcf:s' => \$inversion_vcf,
'inversion_count:i' => \$inversion_count,
'inversion_min_size:i' => \$inversion_min_size,
'inversion_max_size:i' => \$inversion_max_size,
'inversion_breakpoint_gff:s' => \$inversion_breakpoint_gff,
'translocation_vcf:s' => \$translocation_vcf,
'translocation_count:i' => \$translocation_count,
'translocation_breakpoint_gff:s' => \$translocation_breakpoint_gff,
'centromere_gff:s' => \$centromere_gff,
'gene_gff:s' => \$gene_gff,
'seed|s:i' => \$seed,
'prefix|p:s' => \$prefix,
);
## Option check and print out Usage if needed.
# If usage (or version number) was explicitly requested, print usage (or version number).
if (defined $help) {
pod2usage(-verbose => 1);
}
if (defined $man) {
pod2usage(-verbose => 2);
}
if (defined $version) {
pod2usage(-verbose => 99, -sections => qw(NAME|DESCRIPTION|VERSION));
}
if (not defined $refseq) {
pod2usage(-message => "Mandatory argument '-refseq' is missing! Exit!",
-exitval => 1,
-verbose => 1);
} elsif (not -e $refseq) {
print "!!! Error! The defined input file $refseq does not exist!\n";
print "!!! Exit!\n";
die;
}
## Main program
print "\n";
my $local_time = localtime();
print "[$local_time]\n";
print "Starting simuG ..\n\n";
$local_time = localtime();
print "[$local_time]\n";
print "Check specified options ..\n";
# check specified options
if ((defined $snp_vcf) or (defined $snp_count) or (defined $indel_vcf) or (defined $indel_count)) {
print "Running simuG for SNP/INDEL simulation >>\n";
print "Ignore all options for CNV/inversion/translocation simulation.\n";
undef $centromere_gff;
undef $cnv_vcf;
undef $cnv_count;
undef $cnv_gain_loss_ratio;
undef $duplication_tandem_dispersed_ratio;
undef $cnv_max_copy_number;
undef $cnv_min_size;
undef $cnv_max_size;
undef $inversion_vcf;
undef $inversion_count;
undef $inversion_min_size;
undef $inversion_max_size;
undef $inversion_breakpoint_gff;
undef $translocation_vcf;
undef $translocation_count;
undef $translocation_breakpoint_gff;
} elsif ((defined $cnv_vcf) or (defined $cnv_count)) {
print "Running simuG for CNV simulation >>\n\n";
print "Ignore all options for inversion/translocation simulation.\n";
undef $coding_partition_for_snp_simulation;
undef $inversion_vcf;
undef $inversion_count;
undef $inversion_min_size;
undef $inversion_max_size;
undef $inversion_breakpoint_gff;
undef $translocation_vcf;
undef $translocation_count;
undef $translocation_breakpoint_gff;
} elsif ((defined $inversion_vcf) or (defined $inversion_count)) {
print "Running simuG for inversion simulation >>\n\n";
print "Ignore all options for translocation simulation.\n";
undef $coding_partition_for_snp_simulation;
undef $translocation_vcf;
undef $translocation_count;
undef $translocation_breakpoint_gff;
} elsif ((defined $translocation_vcf) or (defined $translocation_count)) {
undef $coding_partition_for_snp_simulation;
print "Running simuG for translocation simulation >>\n\n";
} else {
print "\n";
print "!!! There seems no task for simuG to run !!!\n";
print "!!! 1) If you want to run simuG for SNP/INDEL simulation, at least one of the following options need to be specified:\n";
print "!!! -snp_vcf, -snp_count, -indel_vcf, -indel_count\n";
print "!!! 2) If you want to run simuG for CNV simulation, at least one of the following options need to be specified:\n";
print "!!! -cnv_vcf, -cnv_count\n";
print "!!! 3) If you want to run simuG for inversion simulation, at least one of the following options need to be specified:\n";
print "!!! -inversion_vcf, -inversion_count\n";
print "!!! 4) If you want to run simuG for translocation simulation, at least one of the following options need to be specified:\n";
print "!!! -translocation_vcf, translocation_count\n";
print "!!! 5) If you want to check all available options for simuG, type: perl simuG.pl -h\n";
print "!!! Exit !!!\n\n";
$local_time = localtime();
print "[$local_time]\n";
exit;
}
# define excluded chromosome(s) if any
my %excluded_chr_list = ();
if (defined $excluded_chr_list) {
my $excluded_chr_list_fh = read_file($excluded_chr_list);
%excluded_chr_list = parse_list_file($excluded_chr_list_fh);
$local_time = localtime();
print "\n[$local_time]\n";
print "Check for excluded chromosome(s) defined in $excluded_chr_list ..\n";
}
my %excluded_refseq = ();
# set up reference genome as the template
my @refseq = ();
my %refseq = ();
my $refseq_fh = read_file($refseq);
parse_fasta_file($refseq_fh, \%refseq, \@refseq);
close $refseq_fh;
# remove excluded chromosomes if specified
foreach my $chr (@refseq) {
if (exists $excluded_chr_list{$chr}) {
print "Exclude chromosome: $chr\n";
$excluded_refseq{$chr} = $refseq{$chr};
delete $refseq{$chr};
}
}
# profile the base composition of the reference genome
# my %refseq_base_freq = profile_base_freq(\%refseq);
my %refseq_base_freq = (
'A' => 0.25,
'T' => 0.25,
'G' => 0.25,
'C' => 0.25
);
my %centromere_by_chr = ();
if (defined $centromere_gff) {
my $centromere_gff_fh = read_file($centromere_gff);
my %input_gff = parse_gff_file($centromere_gff_fh);
foreach my $feature_id (sort keys %input_gff) {
my $feature_type = $input_gff{$feature_id}{'type'};
if ($feature_type eq "centromere") {
my $chr = $input_gff{$feature_id}{'chr'};
if (exists $refseq{$chr}) {
$centromere_by_chr{$chr} = \%{$input_gff{$feature_id}};
}
}
}
}
my %inversion_breakpoint_by_chr_by_type = ();
if (defined $inversion_breakpoint_gff) {
my $inversion_breakpoint_gff_fh = read_file($inversion_breakpoint_gff);
my %input_gff = parse_gff_file($inversion_breakpoint_gff_fh);
foreach my $feature_id (sort keys %input_gff) {
my $chr = $input_gff{$feature_id}{'chr'};
my $type = $input_gff{$feature_id}{'type'};
if (exists $refseq{$chr}) {
$inversion_breakpoint_by_chr_by_type{$chr}{$type}{$feature_id} = \%{$input_gff{$feature_id}};
}
}
}
my %translocation_breakpoint_by_chr_by_type = ();
if (defined $translocation_breakpoint_gff) {
my $translocation_breakpoint_gff_fh = read_file($translocation_breakpoint_gff);
my %input_gff = parse_gff_file($translocation_breakpoint_gff_fh);
foreach my $feature_id (sort keys %input_gff) {
my $chr = $input_gff{$feature_id}{'chr'};
my $type = $input_gff{$feature_id}{'type'};
if (exists $refseq{$chr}) {
$translocation_breakpoint_by_chr_by_type{$chr}{$type}{$feature_id} = \%{$input_gff{$feature_id}};
}
}
}
my %gene = ();
my %gene_by_chr = ();
if (defined $gene_gff) {
# check valid protein-coding genes
my $gene_gff_fh = read_file($gene_gff);
my %input_gff = parse_gff_file($gene_gff_fh);
foreach my $feature_id (sort keys %input_gff) {
my $feature_type = $input_gff{$feature_id}{'type'};
if ($feature_type eq "gene") {
$gene{$feature_id} = \%{$input_gff{$feature_id}};
}
}
foreach my $gene_id (sort keys %gene) {
my $gene_chr = $gene{$gene_id}{'chr'};
if (exists $refseq{$gene_chr}) {
$gene_by_chr{$gene_chr}{$gene_id}{'start'} = $gene{$gene_id}{'start'};
$gene_by_chr{$gene_chr}{$gene_id}{'end'} = $gene{$gene_id}{'end'};
$gene_by_chr{$gene_chr}{$gene_id}{'strand'} = $gene{$gene_id}{'strand'};
}
}
if ((defined $coding_partition_for_snp_simulation) and (defined $gene_gff)) {
# check overlapped genes
foreach my $chr (@refseq) {
if ((exists $refseq{$chr}) and (exists $gene_by_chr{$chr})) {
my @gene_by_chr = sort {$gene_by_chr{$chr}{$a}{'start'} <=> $gene_by_chr{$chr}{$b}{'start'} or $gene_by_chr{$chr}{$a}{'end'} <=> $gene_by_chr{$chr}{$b}{'end'}} keys %{$gene_by_chr{$chr}};
if ((scalar @gene_by_chr) > 0) {
my $gene_index = 0;
my $previous_gene_id;
my $previous_gene_start;
my $previous_gene_end;
my $previous_gene_length;
foreach my $gene_id (@gene_by_chr) {
if (exists $gene{$gene_id}) {
$gene_index++;
my $gene_start = $gene{$gene_id}{'start'};
my $gene_end = $gene{$gene_id}{'end'};
if ($gene_index == 1) {
$previous_gene_id = $gene_id;
$previous_gene_start = $gene_start;
$previous_gene_end = $gene_end;
$previous_gene_length = $previous_gene_end - $previous_gene_start + 1;
} else {
if ($previous_gene_end >= $gene_start) {
print "\n";
print "!!! Warning! Coordinate overlap detected between the gene $previous_gene_id and the gene $gene_id !!!\n";
my $gene_length = $gene_end - $gene_start + 1;
if ($gene_length <= $previous_gene_length) {
print "!!! simuG will ignore the smaller gene $gene_id !!!\n";
delete $gene{$gene_id};
delete $gene_by_chr{$chr}{$gene_id};
} else {
print "!!! simuG will ignore the smaller gene $previous_gene_id !!!\n";
delete $gene{$previous_gene_id};
delete $gene_by_chr{$chr}{$previous_gene_id};
$previous_gene_id = $gene_id;
$previous_gene_end = $gene_end;
$previous_gene_length = $gene_end - $gene_start + 1;
}
# sleep(3);
} else {
$previous_gene_id = $gene_id;
$previous_gene_end = $gene_end;
$previous_gene_length = $gene_end - $gene_start + 1;
}
}
}
}
}
}
}
}
}
# initialize the simulated genome
my %simseq = %refseq;
my %ref2sim_map = ();
# initialize the seed for random number generator
if (not defined $seed) {
$seed = int(rand(2**31));
}
print "\nThis simulation use the random seed: $seed\n\n";
srand($seed);
if (defined $snp_vcf) {
print "The option snp_vcf has been specified: snp_vcf = $snp_vcf\n";
print "Ignore incompatible option: snp_count\n";
undef $snp_count;
print "Ignore incompatible option: snp_model\n";
undef $snp_model;
print "Ignore incompatible option: titv_ratio\n";
undef $titv_ratio;
} elsif (defined $snp_count) {
print "The option snp_count has been specified: snp_count = $snp_count\n";
if (defined $snp_model) {
print "The option snp_model has been specified: snp_model = $snp_model\n";
print "Ignore incompatible option: titv_ratio\n";
undef $titv_ratio;
} else {
print "The option titv_ratio has been specified: titv_ratio = $titv_ratio\n";
}
if (defined $gene_gff) {
print "The option gene_gff has been specified: gene_gff = $gene_gff\n";
}
}
if (defined $indel_vcf) {
print "The option indel_vcf has been specified: indel_vcf = $indel_vcf\n";
print "Ignore incompatible option: indel_count\n";
undef $indel_count;
print "Ignore incompatible option: indel_model\n";
undef $indel_model;
print "Ignore incompatible option: ins_del_ratio\n";
undef $ins_del_ratio;
print "Ignore incompatible option: indel_size_powerlaw_alpha\n";
undef $indel_size_powerlaw_alpha;
print "Ignore incompatible option: indel_size_powerlaw_constant\n";
undef $indel_size_powerlaw_constant;
} elsif (defined $indel_count) {
print "The option indel_count has been specified: indel_count = $indel_count\n";
if (defined $indel_model) {
print "The option indel_model has been specified: indel_model = $indel_model\n";
print "Ignore incompatible option: ins_del_ratio\n";
undef $ins_del_ratio;
print "Ignore incompatible option: indel_size_powerlaw_alpha\n";
undef $indel_size_powerlaw_alpha;
print "Ignore incompatible option: indel_size_powerlaw_constant\n";
undef $indel_size_powerlaw_constant;
} else {
print "The option ins_del_ratio has been specified: ins_del_ratio = $ins_del_ratio\n";
print "The option indel_size_powerlaw_alpha has been specified: indel_size_powerlaw_alpha = $indel_size_powerlaw_alpha\n";
print "The option indel_size_powerlaw_constant has been specified: indel_size_powerlaw_constant = $indel_size_powerlaw_constant\n";
}
if (defined $gene_gff) {
print "The option gene_gff has been specified: gene_gff = $gene_gff\n";
}
}
if (defined $cnv_vcf) {
print "The option cnv_vcf has been specified: cnv_vcf = $cnv_vcf\n";
print "Ignore incompatible option: cnv_count\n";
undef $cnv_count;
print "Ignore incompatible option: cnv_gain_loss_ratio\n";
undef $cnv_gain_loss_ratio;
print "Ignore incompatible option: duplication_tandem_dispersed_ratio\n";
undef $duplication_tandem_dispersed_ratio;
print "Ignore incompatible option: cnv_max_copy_number\n";
undef $cnv_max_copy_number;
print "Ignore incompatible option: cnv_min_size\n";
undef $cnv_min_size;
print "Ignore incompatible option: cnv_max_size\n";
undef $cnv_max_size;
print "Ignore incompatible option: centromere_gff\n";
undef $centromere_gff;
} elsif (defined $cnv_count) {
print "The option cnv_count has been specified: cnv_count = $cnv_count\n";
print "The option duplication_tandem_dispersed_ratio has been specified: duplication_tandem_dispersed_ratio = $duplication_tandem_dispersed_ratio\n";
print "The option cnv_max_copy_number has been specified: cnv_max_copy_number = $cnv_max_copy_number\n";
print "The option cnv_min_size has been specified: cnv_min_size = $cnv_min_size\n";
print "The option cnv_min_size has been specified: cnv_max_size = $cnv_max_size\n";
if (defined $centromere_gff) {
print "The option centromere_gff has been specified: centromere_gff = $centromere_gff\n";
}
if (defined $gene_gff) {
print "The option gene_gff has been specified: gene_gff = $gene_gff\n";
}
}
if (defined $inversion_vcf) {
print "The option inversion_vcf has been specified: inversion_vcf = $inversion_vcf\n";
print "Ignore incompatible option: inversion_count\n";
undef $inversion_count;
print "Ignore incompatible option: inversion_min_size\n";
undef $inversion_min_size;
print "Ignore incompatible option: inversion_max_size\n";
undef $inversion_max_size;
print "Ignore incompatible option: inversion_breakpoint_gff\n";
undef $inversion_breakpoint_gff;
print "Ignore incompatible option: centromere_gff\n";
undef $centromere_gff;
} elsif (defined $inversion_count) {
print "The option inversion_count has been specified: inversion_count = $inversion_count\n";
if (defined $inversion_breakpoint_gff) {
print "The option inversion_breakpoint_gff has been specified: inversion_breakpoint_gff = $inversion_breakpoint_gff\n";
print "Ignore incompatible option: inversion_min_size\n";
undef $inversion_min_size;
print "Ignore incompatible option: inversion_max_size\n";
undef $inversion_max_size;
} else {
print "The option inversion_min_size has been specified: inversion_min_size = $inversion_min_size\n";
print "The option inversion_max_size has been specified: inversion_max_size = $inversion_max_size\n";
}
if (defined $centromere_gff) {
print "The option centromere_gff has been specified: centromere_gff = $centromere_gff\n";
}
if (defined $gene_gff) {
print "The option gene_gff has been specified: gene_gff = $gene_gff\n";
}
}
if (defined $translocation_vcf) {
print "The option translocation_vcf has been specified: translocation_vcf = $translocation_vcf\n";
print "Ignore incompatible option: translocation_count\n";
undef $translocation_count;
print "Ignore incompatible option: translocation_breakpoint_gff\n";
undef $translocation_breakpoint_gff;
print "Ignore incompatible option: centromere_gff\n";
undef $centromere_gff;
} elsif (defined $translocation_count) {
print "The option translocation_count has been specified: translocation_count = $translocation_count\n";
if (defined $translocation_breakpoint_gff) {
print "The option translocation_breakpoint_gff has been specified: translocation_breakpoint_gff = $translocation_breakpoint_gff\n";
}
if (defined $centromere_gff) {
print "The option centromere_gff has been specified: centromere_gff = $centromere_gff\n";
}
if (defined $gene_gff) {
print "The option gene_gff has been specified: gene_gff = $gene_gff\n";
}
}
print "\n";
my %snp_vcf = ();
if (defined $snp_vcf) {
$local_time = localtime();
print "[$local_time]\n";
print "Parsing the input vcf file: $snp_vcf\n\n";
my $snp_vcf_fh = read_file($snp_vcf);
%snp_vcf = parse_simple_vcf_file($snp_vcf_fh, 0, 'SNP');
close $snp_vcf_fh;
}
my %indel_vcf = ();
if (defined $indel_vcf) {
$local_time = localtime();
print "[$local_time]\n";
print "Parsing the input vcf file: $indel_vcf\n\n";
my $indel_vcf_fh = read_file($indel_vcf);
%indel_vcf = parse_simple_vcf_file($indel_vcf_fh, 0, 'INDEL');
close $indel_vcf_fh;
}
my %vcf = ();
if ((defined $snp_vcf) and (defined $indel_vcf)) {
%vcf = merge_vcf(\%snp_vcf, \%indel_vcf);
} elsif (defined $snp_vcf) {
%vcf = %snp_vcf;
} elsif (defined $indel_vcf) {
%vcf = %indel_vcf;
}
# introduce SNP/INDEL variants based on user-provided vcf(s)
if (%vcf) {
$local_time = localtime();
print "[$local_time]\n";
print "Introducing defined SNP/INDELs based on the input vcf file(s):\n";
if (defined $snp_vcf) {
print "> snp_vcf = $snp_vcf\n";
}
if (defined $indel_vcf) {
print "> indel_vcf = $indel_vcf\n";
}
introduce_defined_snp_indel(\%vcf, \%refseq, \%simseq, \%ref2sim_map);
print "\n";
}
# introduce random SNP variants
if (defined $snp_count) {
$local_time = localtime();
print "[$local_time] Introducing random SNPs based on the following parameters:\n";
print "> snp_count = $snp_count\n";
if (defined $snp_model) {
print "> snp_model = $snp_model\n";
} else {
print "> titv_ratio = $titv_ratio\n";
}
if (defined $coding_partition_for_snp_simulation) {
if (defined $gene_gff) {
print "> gene_gff = $gene_gff\n";
print "> coding_partition_for_snp_simulation = $coding_partition_for_snp_simulation\n";
introduce_random_snp_with_coding_partition($snp_count, $snp_model, $titv_ratio, $coding_partition_for_snp_simulation, \%gene, \%refseq, \%simseq, \%ref2sim_map);
} else {
print "!!! Warning! The option '-coding_partition_for_snp_simulation' need to be used together with '-gene_gff' !!!\n";
print "!!! '-gene_gff' is undefined !!! \n";
print "!!! Ignore the specified \'-coding_partition_for_snp_simulation $coding_partition_for_snp_simulation \' option. !!!\n";
}
} else {
introduce_random_snp($snp_count, $snp_model, $titv_ratio, \%refseq, \%simseq, \%ref2sim_map);
}
print "\n";
}
# introduce random INDEL variants
if (defined $indel_count) {
$local_time = localtime();
print "[$local_time]\n";
print "Introducing random INDELs based on the following parameters:\n";
print "> indel_count = $indel_count\n";
if (defined $indel_model) {
print "> indel_model = $indel_model\n";
} else {
print "> ins_del_ratio = $ins_del_ratio\n";
print "> indel_size_powerlaw_alpha = $indel_size_powerlaw_alpha\n";
print "> indel_size_powerlaw_constant = $indel_size_powerlaw_constant\n";
}
introduce_random_indel($indel_count, $indel_model, $ins_del_ratio, \%refseq_base_freq, \%refseq, \%simseq, \%ref2sim_map);
print "\n";
}
# introduce CNVs based on user-provided CNV VCF file.
if (defined $cnv_vcf) {
$local_time = localtime();
print "[$local_time]\n";
print "Introducing defined CNVs based on the input vcf file:\n";
print "> cnv_vcf = $cnv_vcf\n";
my $cnv_vcf_fh = read_file($cnv_vcf);
my %sv = parse_sv_vcf_file($cnv_vcf_fh);
my %cnv = extract_cnv_from_sv(\%sv);
introduce_defined_cnv(\%cnv, \%refseq, \%simseq, \%ref2sim_map);
print "\n";
}
# introduce random CNVs
if (defined $cnv_count) {
$local_time = localtime();
print "[$local_time]\n";
print "Introducing random CNVs with the following parameters:\n";
print "> cnv_count = $cnv_count\n";
print "> cnv_gain_loss_ratio = $cnv_gain_loss_ratio\n";
print "> duplication_tandem_dispersed_ratio = $duplication_tandem_dispersed_ratio\n";
print "> cnv_max_copy_number = $cnv_max_copy_number\n";
print "> cnv_min_size = $cnv_min_size\n";
print "> cnv_max_size = $cnv_max_size\n";
if (defined $centromere_gff) {
print "> centromere_gff = $centromere_gff\n";
}
if (defined $gene_gff) {
print "> gene_gff = $gene_gff\n";
}
introduce_random_cnv($cnv_count, $cnv_gain_loss_ratio, $duplication_tandem_dispersed_ratio, $cnv_min_size, $cnv_max_size, $cnv_max_copy_number, \%centromere_by_chr, \%gene_by_chr, \%refseq, \%simseq, \%ref2sim_map);
print "\n";
}
# introduce inversions based on the input inversion vcf file
if (defined $inversion_vcf) {
$local_time = localtime();
print "[$local_time]\n";
print "Introducing defined Inversions based on the input vcf file:\n";
print "> inversion_vcf = $inversion_vcf\n";
my $inversion_vcf_fh = read_file($inversion_vcf);
my %sv = parse_sv_vcf_file($inversion_vcf_fh);
my %inversion = extract_inversion_from_sv(\%sv);
introduce_defined_inversion(\%inversion, \%refseq, \%simseq, \%ref2sim_map);
print "\n";
}
# introduce random inversions
if (defined $inversion_count) {
$local_time = localtime();
print "[$local_time]\n";
print "Introducing random Inversions based on the following parameters:\n";
print "> inversion_count = $inversion_count\n";
if (defined $centromere_gff) {
print "> centromere_gff = $centromere_gff\n";
}
if (defined $gene_gff) {
print "> gene_gff = $gene_gff\n";
}
if (defined $inversion_breakpoint_gff) {
print "> inversion_breakpoint_gff = $inversion_breakpoint_gff\n";
} else {
print "> inversion_min_size = $inversion_min_size\n";
print "> inversion_max_size = $inversion_max_size\n";
}
introduce_random_inversion($inversion_count, $inversion_min_size, $inversion_max_size, \%centromere_by_chr, \%inversion_breakpoint_by_chr_by_type, \%gene_by_chr, \%refseq, \%simseq, \%ref2sim_map);
print "\n";
}
# introduce translocations based on the input translocation vcf file
if (defined $translocation_vcf) {
$local_time = localtime();
print "[$local_time]\n";
print "Introducing defined Translocations based on the input vcf file:\n";
print "> translocation_vcf = $translocation_vcf\n";
my $translocation_vcf_fh = read_file($translocation_vcf);
my %sv = parse_sv_vcf_file($translocation_vcf_fh);
my %translocation = extract_translocation_from_sv(\%sv);
introduce_defined_translocation(\%translocation, \%refseq, \%simseq, \%ref2sim_map);
print "\n";
}
# introduce random translocations
if (defined $translocation_count) {
$local_time = localtime();
print "[$local_time]\n";
print "Introducing random Translocations based on the following parameters:\n";
print "> translocation_count = $translocation_count\n";
if (defined $centromere_gff) {
print "> centromere_gff = $centromere_gff\n";
}
if (defined $gene_gff) {
print "> gene_gff = $gene_gff\n";
}
if (defined $translocation_breakpoint_gff) {
print "> translocation_breakpoint_gff = $translocation_breakpoint_gff\n";
}
introduce_random_translocation($translocation_count, \%centromere_by_chr, \%translocation_breakpoint_by_chr_by_type, \%gene_by_chr, \%refseq, \%simseq, \%ref2sim_map);
print "\n";
}
# generate output files
$local_time = localtime();
print "[$local_time]\n";
print "Simulation completed! :) \n\n";
$local_time = localtime();
print "[$local_time]\n";
print "Generating output files .. \n\n";
generate_output_files($prefix, \@refseq, \%refseq, \%simseq, \%ref2sim_map, \%excluded_refseq);
$local_time = localtime();
print "[$local_time]\n";
print "Done! :) \n\n";
sub read_file {
my $file = shift @_;
my $fh;
if ($file =~ /\.gz$/) {
open($fh, "gunzip -c $file |") or die "can't open pipe to $file";
} else {
open($fh, $file) or die "can't open $file";
}
return $fh;
}
sub write_file {
my $file = shift @_;
my $fh;
if ($file =~ /\.gz$/) {
open($fh, "| gzip -c >$file") or die "can't open pipe to $file\n";
} else {
open($fh, ">$file") or die "can't open $file\n";
}
return $fh;
}
sub parse_list_file {
my $fh = shift @_;
my %list = ();
while (<$fh>) {
chomp;
/^\s*$/ and next;
/^#/ and next;
if (exists $list{$_}) {
$list{$_}++;
} else {
$list{$_} = 1;
}
}
return %list;
}
sub parse_fasta_file {
my ($fh, $input_hashref, $input_arrayref) = @_;
my $seq_name = "";
while (<$fh>) {
chomp;
if (/^\s*$/) {
next;
} elsif (/^\s*#/) {
next;
} elsif (/^>(\S+)/) {
$seq_name = $1;
push @$input_arrayref, $seq_name;
$$input_hashref{$seq_name} = "";
} else {
my $seq_line = uc $_;
$$input_hashref{$seq_name} .= $seq_line;
}
}
}
sub profile_base_freq {
my $genome_hashref = shift @_;
my @base = qw(A T G C);
my %base_count = ();
my %base_freq = ();
foreach my $chr (sort keys %$genome_hashref) {
my $seq = uc $$genome_hashref{$chr};
foreach my $base (@base) {
my $bc = () = $seq =~ /$base/g;
if (exists $base_count{$base}) {
$base_count{$base} += $bc;
} else {
$base_count{$base} = $bc;
}
}
}
my $total_base_count = sum(values %base_count);
foreach my $base (@base) {
$base_freq{$base} = $base_count{$base}/$total_base_count;
}
return %base_freq;
}
sub create_genome_space {
my $genome_hashref = shift @_;
my %genome_space = ();
my $offset = 0;
foreach my $chr (sort keys %$genome_hashref) {
my $chr_length = length $$genome_hashref{$chr};
my $start = $offset + 1;
my $end = $start + $chr_length - 1;
# print "chr=$chr, chr_length=$chr_length, genome_space_start=$start, genome_space_end=$end\n";
$genome_space{'chr-wide'}{$chr}{"start"} = $start;
$genome_space{'chr-wide'}{$chr}{"end"} = $end;
$genome_space{'chr-wide'}{$chr}{"length"} = $chr_length;
$offset = $end;
if (not exists $genome_space{'genome-wide'}) {
$genome_space{'genome-wide'}{"start"} = $start;
$genome_space{'genome-wide'}{"end"} = $end;
$genome_space{'genome-wide'}{"length"} = $chr_length;
} else {
$genome_space{'genome-wide'}{"length"} += $chr_length;
$genome_space{'genome-wide'}{"end"} = $genome_space{'genome-wide'}{"length"};
}
}
return %genome_space;
}
sub parse_simple_vcf_file {
my ($fh, $qual_cutoff, $query_type) = @_;
my %vcf = ();
while (<$fh>) {
chomp;
/^#/ and next;
/^\s*$/ and next;
my ($ref_chr, $ref_start, $variant_id, $ref_allele, $alt_allele, $variant_qual, $variant_filter, $variant_info) = split /\t/, $_;
if (($variant_qual eq ".") or ($variant_qual >= $qual_cutoff)) {
my $variant_type;
my $ref_allele_length = length $ref_allele;
my $alt_allele_length = length $alt_allele;
my $ref_end = $ref_start + $ref_allele_length - 1;
if ($alt_allele =~ /,/) {
print "!!! Warning! Multiple alternative variants found at the same site:\n";
print "!!! $ref_chr:$ref_start $ref_allele=>$alt_allele QUAL=$variant_qual!\n";
print "!!! Ignore all variants at this site.\n\n";
} else {
if ($ref_allele_length ne $alt_allele_length) {
$variant_type = "INDEL";
} else {
$variant_type = "SNP";
}
if ((defined $query_type) and ($query_type ne $variant_type)) {
next;
} else {
my $check_overlap_flag = 0;
if (exists $vcf{$ref_chr}) {
if (exists $vcf{$ref_chr}{$ref_start}) {
$check_overlap_flag = 1;
print "!!! Warning! Multiple variants were defined within the same region: $ref_chr:$ref_start-$ref_end in the input vcf file!\n";
print "!!! Only keep the first instance: $ref_chr:$ref_start $vcf{$ref_chr}{$ref_start}{'ref_allele'}=>$vcf{$ref_chr}{$ref_start}{'alt_allele'} QUAL=$vcf{$ref_chr}{$ref_start}{'variant_qual'}.\n";
print "!!! Ignore the variant: $ref_chr:$ref_start $ref_allele => $alt_allele QUAL=$variant_qual.\n\n";
} else {
foreach my $s (sort {$a <=> $b} keys %{$vcf{$ref_chr}}) {
if ($ref_end < $s) {
last;
} elsif ($ref_start <= $vcf{$ref_chr}{$s}{'ref_end'}) {
if (($variant_type eq "SNP") and ($vcf{$ref_chr}{$s}{'variant_type'} eq "SNP")) {
next;
} else {
$check_overlap_flag = check_overlap_region($ref_start, $ref_end, $vcf{$ref_chr}{$s}{'ref_start'}, $vcf{$ref_chr}{$s}{'ref_end'});
if ($check_overlap_flag == 1) {
print "!!! Warning! Multiple variants were defined within the same region: $ref_chr:$ref_start-$ref_end in the input vcf file!\n";
print "!!! Only keep the first instance: $ref_chr:$s $vcf{$ref_chr}{$s}{'ref_allele'}=>$vcf{$ref_chr}{$s}{'alt_allele'} QUAL=$vcf{$ref_chr}{$s}{'variant_qual'}.\n";
print "!!! Ignore the variant: $ref_chr:$ref_start $ref_allele => $alt_allele QUAL=$variant_qual.\n\n";
last;
}
}
}
}
}
}
if ($check_overlap_flag == 0) {
$vcf{$ref_chr}{$ref_start}{'ref_chr'} = $ref_chr;
$vcf{$ref_chr}{$ref_start}{'ref_start'} = $ref_start;
$vcf{$ref_chr}{$ref_start}{'ref_end'} = $ref_end;
$vcf{$ref_chr}{$ref_start}{'ref_allele'} = $ref_allele;
$vcf{$ref_chr}{$ref_start}{'alt_allele'} = $alt_allele;
$vcf{$ref_chr}{$ref_start}{'variant_type'} = $variant_type;
$vcf{$ref_chr}{$ref_start}{'variant_id'} = $variant_id;
$vcf{$ref_chr}{$ref_start}{'variant_qual'} = $variant_qual;
$vcf{$ref_chr}{$ref_start}{'variant_info'} = $variant_info;
}
}
}
}
}
return %vcf;
}
sub merge_vcf {
my ($snp_vcf_hashref, $indel_vcf_hashref) = @_;
my %merged_vcf = %$snp_vcf_hashref;
foreach my $ref_chr (sort keys %$indel_vcf_hashref) {
foreach my $ref_start (sort {$a <=> $b} keys %{$$indel_vcf_hashref{$ref_chr}}) {
my $ref_start = $$indel_vcf_hashref{$ref_chr}{$ref_start}{'ref_start'};
my $ref_end = $$indel_vcf_hashref{$ref_chr}{$ref_start}{'ref_end'};
my $check_overlap_flag = 0;
if (exists $$snp_vcf_hashref{$ref_chr}) {
if (exists $$snp_vcf_hashref{$ref_chr}{$ref_start}) {
$check_overlap_flag = 1;
print "!!! Warning! Both SNP and INDEL variants were defined within the same region: $ref_chr:$ref_start-$ref_end in the input vcf files!\n";
print "!!! Only keep the SNP variant: $$snp_vcf_hashref{$ref_chr}{$ref_start}{'ref_allele'}=>$$snp_vcf_hashref{$ref_chr}{$ref_start}{'alt_allele'}.\n";
print "!!! Ignore the INDEL variant $$indel_vcf_hashref{$ref_chr}{$ref_start}{'ref_allele'}=>$$indel_vcf_hashref{$ref_chr}{$ref_start}{'alt_allele'} within this region.\n\n";
} else {
foreach my $s (sort {$a <=> $b} keys %{$$snp_vcf_hashref{$ref_chr}}) {
if ($ref_end < $s) {
last;
} elsif ($ref_start <= $$snp_vcf_hashref{$ref_chr}{$s}{'ref_end'}) {
$check_overlap_flag = check_overlap_region($ref_start, $ref_end, $$snp_vcf_hashref{$ref_chr}{$s}{'ref_start'}, $$snp_vcf_hashref{$ref_chr}{$s}{'ref_end'});
if ($check_overlap_flag == 1) {
print "!!! Warning! Both SNP and INDEL variants were defined within the same region: $ref_chr:$ref_start-$ref_end in the input vcf files!\n";
print "!!! Only keep the SNP variant: $$snp_vcf_hashref{$ref_chr}{$s}{'ref_allele'}=>$$snp_vcf_hashref{$ref_chr}{$s}{'alt_allele'}.\n";
print "!!! Ignore the INDEL variant $$indel_vcf_hashref{$ref_chr}{$ref_start}{'ref_allele'}=>$$indel_vcf_hashref{$ref_chr}{$ref_start}{'alt_allele'} within this region.\n\n";
last;
}
}
}
}
}
if ($check_overlap_flag == 0) {
$merged_vcf{$ref_chr}{$ref_start} = \%{$$indel_vcf_hashref{$ref_chr}{$ref_start}};
}
}
}
return %merged_vcf;
}
sub parse_gff_file {
my $fh = shift @_;
my %gff = ();
while (<$fh>) {
chomp;
/^##FASTA/ and last;
/^#/ and next;
/^\s*$/ and next;
my ($chr, $source, $type, $start, $end, $score, $strand, $phase, $attributes) = split /\t/, $_;
if ($attributes =~ /ID=([^;]+)/) {
my ($feature_id) = $1;
$gff{$feature_id}{'id'} = $feature_id;
$gff{$feature_id}{'type'} = $type;
$gff{$feature_id}{'chr'} = $chr;
$gff{$feature_id}{'start'} = $start;
$gff{$feature_id}{'end'} = $end;
$gff{$feature_id}{'strand'} = $strand;
$gff{$feature_id}{'source'} = $source;
$gff{$feature_id}{'score'} = $score;
$gff{$feature_id}{'phase'} = $phase;
$gff{$feature_id}{'attributes'} = $attributes;
if ($type eq 'mRNA') {
my ($mRNA_id, $gene_id) = ($attributes =~ /ID=([^;]+);\S*Parent=([^;]+)/);
$gff{$mRNA_id}{'parent'} = $gene_id;
}
}
if ($type eq 'exon') {
my ($mRNA_id) = ($attributes =~ /Parent=([^;]+)/);
my $exon_index = $start;
$gff{$mRNA_id}{'exon'}{$exon_index}{'chr'} = $chr;
$gff{$mRNA_id}{'exon'}{$exon_index}{'start'} = $start;
$gff{$mRNA_id}{'exon'}{$exon_index}{'end'} = $end;
$gff{$mRNA_id}{'exon'}{$exon_index}{'strand'} = $strand;
$gff{$mRNA_id}{'exon'}{$exon_index}{'source'} = $source;
$gff{$mRNA_id}{'exon'}{$exon_index}{'score'} = $score;
$gff{$mRNA_id}{'exon'}{$exon_index}{'phase'} = $phase;
} elsif ($type eq 'CDS') {
my ($mRNA_id) = ($attributes =~ /Parent=([^;]+)/);
my $cds_index = $start;
$gff{$mRNA_id}{'cds'}{$cds_index}{'chr'} = $chr;
$gff{$mRNA_id}{'cds'}{$cds_index}{'start'} = $start;
$gff{$mRNA_id}{'cds'}{$cds_index}{'end'} = $end;
$gff{$mRNA_id}{'cds'}{$cds_index}{'strand'} = $strand;
$gff{$mRNA_id}{'cds'}{$cds_index}{'source'} = $source;
$gff{$mRNA_id}{'cds'}{$cds_index}{'score'} = $score;
$gff{$mRNA_id}{'cds'}{$cds_index}{'phase'} = $phase;
}
}
foreach my $feature_id (sort keys %gff) {
if ($gff{$feature_id}{'type'} eq "mRNA") {
my $mRNA_id = $feature_id;
my $gene_id = $gff{$mRNA_id}{'parent'};
$gff{$gene_id}{'mRNA'}{$mRNA_id} = \%{$gff{$mRNA_id}};
}
}
# print Dumper(%gff);
return %gff;
}
sub analyze_coding_partition {
my ($refseq_hashref, $gene_gff_hashref) = @_;
my %coding_partition = ();
foreach my $gene_id (sort keys %$gene_gff_hashref) {
my $gene_chr = $$gene_gff_hashref{$gene_id}{'chr'};
if (exists $$refseq_hashref{$gene_chr}) {
my $gene_start = $$gene_gff_hashref{$gene_id}{'start'};
my $gene_end = $$gene_gff_hashref{$gene_id}{'end'};
my $gene_strand = $$gene_gff_hashref{$gene_id}{'strand'};
my %mRNA_length = ();
# print "gene_id=$gene_id\n";
if (not exists $$gene_gff_hashref{$gene_id}{'mRNA'}) {
# skip unexpected cases such as ENSG00000188403 that lacks the mRNA track in Ensembl's GFF3 file
next;
}
foreach my $mRNA_id (sort keys %{$$gene_gff_hashref{$gene_id}{'mRNA'}}) {
my $mRNA_length = $$gene_gff_hashref{$gene_id}{'mRNA'}{$mRNA_id}{'end'} - $$gene_gff_hashref{$gene_id}{'mRNA'}{$mRNA_id}{'start'} + 1;
$mRNA_length{$mRNA_id} = $mRNA_length;
}
my @mRNA_length = sort {$mRNA_length{$b} <=> $mRNA_length{$a}} keys %mRNA_length;
my $primary_mRNA_id = $mRNA_length[0];
my $primary_mRNA_strand = $$gene_gff_hashref{$gene_id}{'mRNA'}{$primary_mRNA_id}{'strand'};
my @cds_index = sort {$a <=> $b} keys %{$$gene_gff_hashref{$gene_id}{'mRNA'}{$primary_mRNA_id}{'cds'}};
my @coding_sites = ();
foreach my $cds_index (@cds_index) {
my $cds_start = $$gene_gff_hashref{$gene_id}{'mRNA'}{$primary_mRNA_id}{'cds'}{$cds_index}{'start'};