/
demonstrateSim.pl
212 lines (188 loc) · 7.85 KB
/
demonstrateSim.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
#!/usr/bin/perl
use strict;
use warnings;
use Math::Random qw(random_binomial random_poisson);
use Data::Dumper;
use Getopt::Long;
my $numSites;
my $fracPolymorphic;
my $fracHet;
my $outFile;
my $meanReadDepth;
my $help;
GetOptions ("sites=i" => \$numSites,
"poly=f" => \$fracPolymorphic,
"het=s" => \$fracHet,
"out=s" => \$outFile,
"depth=f" => \$meanReadDepth,
"help|man" => \$help) || die "Couldn't get options with GetOpt::Long: $!\n";
if (!$numSites or !$fracPolymorphic or !$fracHet or !$outFile or !$meanReadDepth or $help) {
die "Must supply --sites, --poly, --het, --samps, --out, and --depth.
--sites is the total number of sites to simulate
--poly is the fraction of segregating sites
--het is per-sample heterozygosities, separated by commas (and no whitespace)
--samps is the number of individuals simulated
--out is the name of the binary output file that holds the readcount
--depth is the average per-sample read depth";
}
my $adjustedHeterozygosity = $fracHet / $fracPolymorphic; # This is the fraction of polymorphic sites that are het for samples
die if ($fracHet > $fracPolymorphic);
# Generate a list of 0s and 1s. The 1s are polymorphic sites. Binomial draw with $fracPolymorphic chance of being polymorphic
my @sites = random_binomial($numSites, 1, $fracPolymorphic);
my @hetSitesA;
my @hetSitesB;
my $hetCounterA = 0;
my $hetCounterB = 0;
my $numberOfPolymorphicSites = 0;
foreach my $site (@sites) {
if ($site == 0) {
# Nonpolymorphic, so can't be a het
push(@hetSitesA, 0);
push(@hetSitesB, 0);
} else {
# This is a polymorphic site, so it could be het in either or both samples
$numberOfPolymorphicSites++;
# Draw for sample A
if (random_binomial(1,1,$adjustedHeterozygosity) == 1) {
push(@hetSitesA, 1);
$hetCounterA++;
} else {
push(@hetSitesA, 0);
}
# Draw for sample B
if (random_binomial(1,1,$adjustedHeterozygosity) == 1) {
push(@hetSitesB, 1);
$hetCounterB++;
} else {
push(@hetSitesB, 0);
}
}
}
print "Empirical heterozygosity for Sample 1: " . $hetCounterA / $numSites . "\n";
print "Empirical heterozygosity for Sample 2: " . $hetCounterB / $numSites . "\n";
### Generate per-site coverages
my @coveragesA;
my @coveragesB;
foreach my $site (@sites) {
# Poisson-distribution with a mean of 1
push(@coveragesA, random_poisson(1,$meanReadDepth));
push(@coveragesB, random_poisson(1,$meanReadDepth));
}
### Generate per-site tracked-allele depths
my @alleleDepthsA;
my @alleleDepthsB;
# Sample 1
my $coverageIndex = 0;
foreach my $hetSiteA (@hetSitesA) {
if ($hetSiteA == 1) {
push(@alleleDepthsA, random_binomial(1, $coveragesA[$coverageIndex], .5))
} else {
push(@alleleDepthsA, 0);
}
$coverageIndex++;
}
# Sample 2
$coverageIndex = 0;
foreach my $hetSiteB (@hetSitesB) {
if ($hetSiteB == 1) {
push(@alleleDepthsB, random_binomial(1, $coveragesB[$coverageIndex], .5))
} else {
push(@alleleDepthsB, 0);
}
$coverageIndex++;
}
### Now print out the results in the right format
open(my $outFH, ">", $outFile) or die "Couldn't open $outFile for writing: $!\n";
my $counter = 0;
my $fixedDiff = 0;
my $hetOneFixOther = 0;
my $actuallyNotPolymorphic = 0;
my $hetInBoth = 0;
foreach my $site (@sites) {
if ($site == 0) {
print $outFH pack("C", $coveragesA[$counter]); # Sample 1 allele A depth as an 8-bit char
print $outFH pack("C", 0); # Sample 1 allele B depth as an 8-bit char
print $outFH pack("C", $coveragesB[$counter]); # Sample 2 allele A depth as an 8-bit char
print $outFH pack("C", 0); # Sample 2 allele B depth as an 8-bit char
} else {
my $alleleSamp1;
my $alleleSamp2;
# This is a polymorphic site, so samples could be het. If they're NOT
# het, then the sample needs to choose whether it's homozygous for
# allele A or allele B
if ($hetSitesA[$counter] == 1) {
# This site is heterozygous for sample 1
$alleleSamp1 = "het";
print $outFH pack("C", $alleleDepthsA[$counter]); # Sample 1 allele A depth as an 8-bit char
print $outFH pack("C", $coveragesA[$counter] - $alleleDepthsA[$counter]); # Sample 1 allele B depth as an 8-bit char
} else {
# Choose A or B
if (random_binomial(1, 1, .5) == 0) {
$alleleSamp1 = "alleleA";
print $outFH pack("C", $coveragesA[$counter]); # Sample 1 allele A depth as an 8-bit char
print $outFH pack("C", 0); # Sample 1 allele B depth as an 8-bit char
} else {
$alleleSamp1 = "alleleB";
print $outFH pack("C", 0); # Sample 1 allele A depth as an 8-bit char
print $outFH pack("C", $coveragesA[$counter]); # Sample 1 allele B depth as an 8-bit char
}
}
if ($hetSitesB[$counter] == 1) {
# This site is heterozygous for sample 2
$alleleSamp2 = "het";
print $outFH pack("C", $alleleDepthsB[$counter]); # Sample 1 allele A depth as an 8-bit char
print $outFH pack("C", $coveragesB[$counter] - $alleleDepthsB[$counter]); # Sample 1 allele B depth as an 8-bit char
} else {
# Choose A or B
if (random_binomial(1, 1, .5) == 0) {
$alleleSamp2 = "alleleA";
print $outFH pack("C", $coveragesB[$counter]); # Sample 2 allele A depth as an 8-bit char
print $outFH pack("C", 0); # Sample 1 allele B depth as an 8-bit char
} else {
$alleleSamp2 = "alleleB";
print $outFH pack("C", 0); # Sample 1 allele A depth as an 8-bit char
print $outFH pack("C", $coveragesB[$counter]); # Sample 2 allele B depth as an 8-bit char
}
}
# We'll now count up the different categories of differences between sample 1 and sample 2.
# These will be used to calculate the empirical pairwise pi values
if ($alleleSamp1 eq "alleleA" and $alleleSamp2 eq "alleleB") {
$fixedDiff++;
}
if ($alleleSamp1 eq "alleleB" and $alleleSamp2 eq "alleleA") {
$fixedDiff++;
}
if ($alleleSamp1 eq "alleleA" and $alleleSamp2 eq "het") {
$hetOneFixOther++;
}
if ($alleleSamp1 eq "alleleB" and $alleleSamp2 eq "het") {
$hetOneFixOther++;
}
if ($alleleSamp1 eq "het" and $alleleSamp2 eq "alleleA") {
$hetOneFixOther++;
}
if ($alleleSamp1 eq "het" and $alleleSamp2 eq "alleleB") {
$hetOneFixOther++;
}
if ($alleleSamp1 eq "alleleA" and $alleleSamp2 eq "alleleA") {
$actuallyNotPolymorphic++;
}
if ($alleleSamp1 eq "alleleB" and $alleleSamp2 eq "alleleB") {
$actuallyNotPolymorphic++;
}
if ($alleleSamp1 eq "het" and $alleleSamp2 eq "het") {
$hetInBoth++;
}
}
$counter++;
}
print $numSites . " total sites\n";
my $numberActuallyPolymorphic = $numberOfPolymorphicSites - $actuallyNotPolymorphic;
print $numberActuallyPolymorphic . " total polymorphic sites\n";
print $hetOneFixOther . " sites that are fixed in one sample and heterozygous in the other\n";
print $fixedDiff . " sites that are fixed different between the two samples\n";
print $hetInBoth . " sites that are het in both samples\n";
print "Self PWP A (fraction of het sites / 2): " . ($hetCounterA / $numSites) / 2 . "\n";
print "Self PWP B (fraction of het sites / 2): " . ($hetCounterB / $numSites) / 2 . "\n";
my $pwp = ($fixedDiff / $numSites) + 0.5 * ($hetOneFixOther/$numSites) + 0.5 * ($hetInBoth/$numSites);
print "A vs B pwp: " . $pwp . "\n";