PhenotypeSimulator
PhenotypeSimulator
¶
Bases: GWADataLoader
A wrapper class that supports simulating complex traits with a variety of genetic architectures and heritability values, using the standard linear model. The basic implementation supports simulating effect sizes from a sparse Gaussian mixture density, allowing some variants to have larger effects than others. The class also supports simulating binary phenotypes (case-control) by thresholding the continuous phenotype at a specified threshold.
To be concrete, the generative model for the simulation is as follows:
1) Simulate the mixture assignment for each variant based on the mixing proportions pi
.
2) Simulate the effect sizes for each variant from the corresponding Gaussian density that they were assigned.
3) Compute the polygenic score for each individual based on the simulated effect sizes.
4) Simulate the residual component of the phenotype, in such a way that the total heritability is preserved.
See Also
Attributes:
Name | Type | Description |
---|---|---|
pi |
The mixing proportions for the Gaussian mixture density. |
|
h2 |
The trait SNP heritability, or proportion of variance explained by SNPs. |
|
d |
The variance multipliers for each component of the Gaussian mixture density. |
|
prevalence |
The (disease) prevalence for binary (case-control) phenotypes. |
|
per_snp_h2 |
The per-SNP heritability for each variant in the dataset. |
|
per_snp_pi |
The per-SNP mixing proportions for each variant in the dataset. |
|
beta |
The effect sizes for each variant in the dataset. |
|
mixture_assignment |
The assignment of each variant to a mixture component. |
Source code in magenpy/simulation/PhenotypeSimulator.py
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 |
|
n_components
property
¶
Returns:
Type | Description |
---|---|
The number of Gaussian mixture components for the effect size distribution. |
__init__(bed_files, h2=0.2, pi=0.1, d=(0.0, 1.0), prevalence=0.15, **kwargs)
¶
Initialize the PhenotypeSimulator object with the necessary parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bed_files
|
A path (or list of paths) to PLINK BED files containing the genotype information. |
required | |
h2
|
The trait SNP heritability, or proportion of variance explained by SNPs. |
0.2
|
|
pi
|
The mixing proportions for the mixture of Gaussians (our model for the distribution of effect sizes). If a float is provided, it is converted to a tuple (1-pi, pi), where pi is the proportion of causal variants. |
0.1
|
|
d
|
The variance multipliers for each component of the Gaussian mixture density. By default, all components have the same variance multiplier. |
(0.0, 1.0)
|
|
prevalence
|
The (disease) prevalence for binary (case-control) phenotypes. |
0.15
|
Source code in magenpy/simulation/PhenotypeSimulator.py
get_causal_status()
¶
Returns:
Type | Description |
---|---|
A dictionary where the keys are the chromosome numbers and the values are binary vectors indicating which SNPs are causal for the simulated phenotype. |
Raises:
Type | Description |
---|---|
AssertionError
|
If the mixture assignment is not set. |
Source code in magenpy/simulation/PhenotypeSimulator.py
set_beta(new_beta)
¶
Set the variant effect sizes (beta) according to user-provided dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
new_beta
|
A dictionary where the keys are the chromosomes and the values are the beta (effect size) for each SNP on that chromosome. |
required |
Source code in magenpy/simulation/PhenotypeSimulator.py
set_causal_snps(causal_snps)
¶
A utility method to set the causal variants in the simulation based on an array or
list of SNPs specified by the user. The method takes an iterable (e.g. list or array) of causal_snps
and then creates a new mixture assignment object where only the causal_snps
contribute to the phenotype.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
causal_snps
|
A list or array of SNP rsIDs. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If all mixture components are causal. |
Source code in magenpy/simulation/PhenotypeSimulator.py
set_h2(new_h2)
¶
Set the total heritability (proportion of additive variance due to SNPs) for the trait
set_mixture_assignment(new_assignment)
¶
Set the mixture assignments according to user-provided dictionary. The mixture assignment indicates which mixture component the effect size of a particular variant comes from.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
new_assignment
|
A dictionary where the keys are the chromosomes and the values are the mixture assignment for each SNP on that chromosome. |
required |
Source code in magenpy/simulation/PhenotypeSimulator.py
set_per_snp_heritability()
¶
Set the per-SNP heritability (effect size variance) for each variant in the dataset. This is a convenience method that may come in handy for more flexible generative models.
Source code in magenpy/simulation/PhenotypeSimulator.py
set_per_snp_mixture_probability()
¶
Set the per-SNP mixing proportions for each variant in the dataset. This is a convenience method that may come in handy for more flexible generative models.
Source code in magenpy/simulation/PhenotypeSimulator.py
set_pi(new_pi)
¶
Set the mixture proportions (proportion of variants in each Gaussian mixture component).
simulate(reset_beta=True, reset_mixture_assignment=True, perform_gwas=False)
¶
A convenience method to simulate all the components of the generative model. Specifically, the simulation follows the standard linear model, where the phenotype is dependent on the genotype + environmental components that are assumed to be uncorrelated:
Y = XB + e
Where Y
is the vector of phenotypes, X
is the genotype matrix, B
is the vector of effect sizes,
and e
represents the residual effects. The generative model proceeds by:
1) Drawing the effect sizes beta
from a Gaussian mixture density.
2) Drawing the residual effect from an isotropic Gaussian density.
3) Setting the phenotype according to the equation above.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
reset_beta
|
If True, reset the effect sizes by drawing new ones from the prior density. |
True
|
|
reset_mixture_assignment
|
If True, reset the assignment of SNPs to mixture components. Set to False if you'd like to keep the same configuration of causal SNPs. |
True
|
|
perform_gwas
|
If True, automatically perform genome-wide association on the newly simulated phenotype. |
False
|
Source code in magenpy/simulation/PhenotypeSimulator.py
simulate_beta()
¶
Simulate the causal effect size for variants included in the dataset. Here, the variant effect size is drawn from a Gaussian density with mean zero and scale given by the root of per-SNP heritability.
Source code in magenpy/simulation/PhenotypeSimulator.py
simulate_mixture_assignment()
¶
Simulate assigning SNPs to the various mixture components
with probabilities given by mixing proportions pi
.
Source code in magenpy/simulation/PhenotypeSimulator.py
simulate_phenotype()
¶
Simulate complex phenotypes for the samples present in the genotype matrix, given their
genotype information and fixed effect sizes beta
that were simulated previous steps.
Given the simulated effect sizes, the phenotype is generated as follows:
Y = XB + e
Where Y
is the vector of phenotypes, X
is the genotype matrix, B
is the vector of effect sizes,
and e
represents the residual effects.
Source code in magenpy/simulation/PhenotypeSimulator.py
to_true_beta_table(per_chromosome=False)
¶
Export the simulated true effect sizes and causal status into a pandas dataframe.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
per_chromosome
|
If True, return a dictionary of tables for each chromosome separately. |
False
|
Returns:
Type | Description |
---|---|
A pandas DataFrame with the true effect sizes and causal status for each variant. |