SumstatsTable
Bases: object
A wrapper class for representing the summary statistics obtained from Genome-wide Association Studies (GWAS). GWAS software tools publish their results in the form of summary statistics, which include the SNP rsIDs, the effect/reference alleles tested, the marginal effect sizes (BETA), the standard errors (SE), the Z-scores, the p-values, etc.
This class provides a convenient way to access/manipulate/harmonize these summary statistics across various formats. Particularly, given the heterogeneity in summary statistics formats, this class provides a common interface to access these statistics in a consistent manner. The class also supports computing some derived statistics from the summary statistics, such as the pseudo-correlation between the SNP and the phenotype, the Chi-squared statistics, etc.
Attributes:
Name | Type | Description |
---|---|---|
table |
DataFrame
|
A pandas DataFrame containing the summary statistics. |
Source code in magenpy/SumstatsTable.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 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 |
|
a1
property
¶
See Also
Returns:
Type | Description |
---|---|
The alternative or effect allele for each variant in the summary statistics table. |
a2
property
¶
See Also
Returns:
Type | Description |
---|---|
The reference allele for each variant in the summary statistics table. |
alt_allele
property
¶
See Also
Returns:
Type | Description |
---|---|
The alternative or effect allele for each variant in the summary statistics table. |
beta_hat
property
¶
See Also
Returns:
Type | Description |
---|---|
The marginal beta from the association test of each variant on the phenotype. |
bp_pos
property
¶
Returns:
Type | Description |
---|---|
The base pair position for each variant in the summary statistics table. |
chromosome
property
¶
A convenience method to return the chromosome number if there is only one chromosome in the summary statistics. If multiple chromosomes are present, it returns None.
Returns:
Type | Description |
---|---|
The chromosome number if there is only one chromosome in the summary statistics. |
chromosomes
property
¶
Returns:
Type | Description |
---|---|
The unique chromosomes in the summary statistics table. |
effect_allele
property
¶
See Also
Returns:
Type | Description |
---|---|
The alternative or effect allele for each variant in the summary statistics table. |
effect_sign
property
¶
Returns:
Type | Description |
---|---|
The sign for the effect size (1 for positive effect, -1 for negative effect) of each genetic variant ib the phenotype. |
Raises:
Type | Description |
---|---|
KeyError
|
If the sign could not be inferred from available data. |
m
property
¶
maf
property
¶
Returns:
Type | Description |
---|---|
The minor allele frequency for each variant in the summary statistics table. |
maf_var
property
¶
Returns:
Type | Description |
---|---|
The variance of the minor allele frequency for each variant in the summary statistics table. |
marginal_beta
property
¶
See Also
Returns:
Type | Description |
---|---|
The marginal beta from the association test of each variant on the phenotype. |
n
property
¶
See Also
Returns:
Type | Description |
---|---|
The sample size for the association test of each variant in the summary statistics table. |
n_per_snp
property
¶
n_snps
property
¶
negative_log10_p_value
property
¶
Returns:
Type | Description |
---|---|
The negative log10 of the p-value (-log10(p_value)) of association test of each variant on the phenotype. |
odds_ratio
property
¶
Returns:
Type | Description |
---|---|
The odds ratio from the association test of each variant on case-control phenotypes. |
p_value
property
¶
See Also
Returns:
Type | Description |
---|---|
The p-value from the association test of each variant on the phenotype. |
pval
property
¶
See Also
Returns:
Type | Description |
---|---|
The p-value from the association test of each variant on the phenotype. |
ref_allele
property
¶
See Also
Returns:
Type | Description |
---|---|
The reference allele for each variant in the summary statistics table. |
se
property
¶
See Also
Returns:
Type | Description |
---|---|
The standard error from the association test of each variant on the phenotype. |
shape
property
¶
Returns:
Type | Description |
---|---|
The shape of the summary statistics table. |
snps
property
¶
Returns:
Type | Description |
---|---|
The rsIDs associated with each variant in the summary statistics table. |
standard_error
property
¶
See Also
Returns:
Type | Description |
---|---|
The standard error from the association test of each variant on the phenotype. |
standardized_marginal_beta
property
¶
Get the marginal BETAs assuming that both the genotype matrix and the phenotype vector are standardized column-wise to have mean zero and variance 1. In some contexts, this is also known as the per-SNP correlation or pseudo-correlation with the phenotype.
See Also
Returns:
Type | Description |
---|---|
The standardized marginal beta from the association test of each variant on the phenotype. |
z_score
property
¶
Returns:
Type | Description |
---|---|
The Z-score from the association test of each SNP on the phenotype. |
Raises:
Type | Description |
---|---|
KeyError
|
If the Z-score statistic is not available and could not be inferred from available data. |
__init__(ss_table)
¶
Initialize the summary statistics table.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ss_table
|
DataFrame
|
A pandas DataFrame containing the summary statistics. !!! seealso "See Also" * from_file |
required |
Source code in magenpy/SumstatsTable.py
drop_duplicates()
¶
Drop variants with duplicated rsIDs from the summary statistics table.
filter_by_allele_frequency(min_maf=None, min_mac=None)
¶
Filter variants in the summary statistics table by minimum minor allele frequency or allele count
Parameters:
Name | Type | Description | Default |
---|---|---|---|
min_maf
|
Minimum minor allele frequency |
None
|
|
min_mac
|
Minimum minor allele count |
None
|
Source code in magenpy/SumstatsTable.py
filter_snps(extract_snps=None, extract_file=None, extract_index=None)
¶
Filter the summary statistics table to keep a subset of SNPs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
extract_snps
|
A list or array of SNP IDs to keep. |
None
|
|
extract_file
|
A plink-style file containing the SNP IDs to keep. |
None
|
|
extract_index
|
A list or array of the indices of SNPs to retain. |
None
|
Source code in magenpy/SumstatsTable.py
from_file(sumstats_file, sumstats_format=None, parser=None, **parse_kwargs)
classmethod
¶
Initialize a summary statistics table from file. The user must provide either
the format for the summary statistics file or the parser object
(see parsers.sumstats_parsers
).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sumstats_file
|
The path to the summary statistics file. |
required | |
sumstats_format
|
The format for the summary statistics file. Currently, we support the following summary statistics formats: |
None
|
|
parser
|
An instance of SumstatsParser parser, implements basic parsing/conversion functionalities. |
None
|
|
parse_kwargs
|
arguments for the pandas |
{}
|
Returns:
Type | Description |
---|---|
A |
Source code in magenpy/SumstatsTable.py
get_chisq_statistic()
¶
Returns:
Type | Description |
---|---|
The Chi-Squared statistic from the association test of each variant on the phenotype. |
Raises:
Type | Description |
---|---|
KeyError
|
If the Chi-Squared statistic is not available and could not be inferred from available data. |
Source code in magenpy/SumstatsTable.py
get_col(col_name)
¶
Parameters:
Name | Type | Description | Default |
---|---|---|---|
col_name
|
The name of the column to extract. |
required |
Returns:
Type | Description |
---|---|
The column associated with |
Source code in magenpy/SumstatsTable.py
get_snp_pseudo_corr()
¶
Computes the pseudo-correlation coefficient (standardized beta) between the SNP and the phenotype (X_jTy / N) from GWAS summary statistics.
This method uses Equation 15 in Mak et al. 2017
$$
beta = z_j / sqrt(n - 1 + z_j^2)
$$
Where z_j
is the marginal GWAS Z-score.
See Also
Returns:
Type | Description |
---|---|
The pseudo-correlation coefficient between the SNP and the phenotype. |
Raises:
Type | Description |
---|---|
KeyError
|
If the Z-scores are not available or the sample size is not available. |
Source code in magenpy/SumstatsTable.py
get_yy_per_snp()
¶
Computes the quantity (y'y)_j/n_j following SBayesR (Lloyd-Jones 2019) and Yang et al. (2012).
(y'y)_j/n_j is defined as the empirical variance for continuous phenotypes and may be estimated from GWAS summary statistics by re-arranging the equation for the squared standard error:
$$
SE(b_j)^2 = (Var(y) - Var(x_j)*b_j^2) / (Var(x)*n)
$$
Which gives the following estimate:
$$
(y'y)_j / n_j = (n_j - 2)*SE(b_j)^2 + b_j^2
$$
Returns:
Type | Description |
---|---|
The quantity (y'y)_j/n_j for each SNP in the summary statistics table. |
Raises:
Type | Description |
---|---|
KeyError
|
If the marginal betas, standard errors or sample sizes are not available. |
Source code in magenpy/SumstatsTable.py
infer_a2(reference_table, allow_na=False)
¶
Infer the reference allele A2 (if not present in the SumstatsTable) from a reference table. Make sure that the reference table contains the identifier information for each SNP, in addition to the reference allele A2 and the alternative (i.e. effect) allele A1. It is the user's responsibility to make sure that the reference table matches the summary statistics in terms of the specification of reference vs. alternative. They have to be consistent across the two tables.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
reference_table
|
A pandas table containing the following columns at least: SNP identifiers ( |
required | |
allow_na
|
If True, allow the reference allele to be missing from the final result. |
False
|
Source code in magenpy/SumstatsTable.py
infer_snp_id(reference_table, allow_na=False)
¶
Infer the SNP ID (if not present in the SumstatsTable) from a reference table. Make sure that the reference table contains the SNP ID, chromosome ID, and position.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
reference_table
|
A pandas table containing the following columns at least: |
required | |
allow_na
|
If True, allow the SNP ID to be missing from the final result. |
False
|
Source code in magenpy/SumstatsTable.py
match(reference_table, correct_flips=True)
¶
Match the summary statistics table with a reference table, correcting for potential flips in the effect alleles.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
reference_table
|
The SNP table to use as a reference. Must be a pandas table with the following columns: SNP identifier (either |
required | |
correct_flips
|
If True, correct the direction of effect size estimates if the effect allele is reversed. |
True
|
Source code in magenpy/SumstatsTable.py
run_quality_control(reference_table=None)
¶
Run quality control checks on the summary statistics table.
TODO: Implement quality control checks following recommendations given by Prive et al.:
https://doi.org/10.1016/j.xhgg.2022.100136
Given user fine-control over which checks to run and which to skip.
Maybe move parts of this implementation to a module in stats
(TBD)
Source code in magenpy/SumstatsTable.py
set_sample_size(n)
¶
Set the sample size for each variant in the summary table. This can be useful when the overall sample size from the GWAS analysis is available, but not on a per-SNP basis.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n
|
A scalar or array of sample sizes for each variant. |
required |
Source code in magenpy/SumstatsTable.py
split_by_chromosome(snps_per_chrom=None)
¶
Split the summary statistics table by chromosome, so that we would
have a separate SumstatsTable
object for each chromosome.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
snps_per_chrom
|
A dictionary where the keys are the chromosome number and the value is an array or list of SNPs on that chromosome. |
None
|
Returns:
Type | Description |
---|---|
A dictionary where the keys are the chromosome number and the value is a |
Source code in magenpy/SumstatsTable.py
to_file(output_file, col_subset=None, **to_csv_kwargs)
¶
A convenience method to write the summary statistics table to file.
TODO: Add a format argument to this method and allow the user to output summary statistics according to supported formats (e.g. COJO, plink, fastGWA, etc.).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output_file
|
The path to the file where to write the summary statistics. |
required | |
col_subset
|
A subset of the columns to write to file. |
None
|
|
to_csv_kwargs
|
Keyword arguments to pass to pandas' |
{}
|
Source code in magenpy/SumstatsTable.py
to_table(col_subset=None)
¶
A convenience method to extract the summary statistics table or subsets of it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
col_subset
|
A list corresponding to a subset of columns to return. |
None
|
Returns:
Type | Description |
---|---|
A pandas DataFrame containing the summary statistics with the requested column subset. |