Skip to content

Instantly share code, notes, and snippets.

@explodecomputer
Created April 2, 2021 08:13
Show Gist options
  • Save explodecomputer/2b4b8b77ba78a5b037d8aa8ebbe635f5 to your computer and use it in GitHub Desktop.
Save explodecomputer/2b4b8b77ba78a5b037d8aa8ebbe635f5 to your computer and use it in GitHub Desktop.
af_comparison
---
title: Comparison of effect allele frequencies in TwoSampleMR
author: Gibran Hemani
date: "`r Sys.time()`"
---
```{r, echo=FALSE}
library(knitr)
opts_chunk$set(warning=FALSE, message=FALSE)
```
## Load libraries
```{r}
library(TwoSampleMR)
library(ieugwasr)
library(tidyverse)
```
## Get allele frequencies for MR analysis
Get harmonised data for exposure and outcome
e.g. BMI vs CHD
```{r}
dat <- make_dat(exposures="ieu-a-2", outcomes="ieu-a-7")
glimpse(dat)
```
The `eaf.exposure` and `eaf.outcome` columns are present here. Can plot them
```{r}
plot(eaf.exposure ~ eaf.outcome, data=dat)
```
## Inferring the control sample allele frequencies
Add meta data to get case control sample sizes for CHD - sometimes these are available per-SNP but not in this case
```{r}
dat <- add_metadata(dat)
dat$ncase.outcome
dat$ncontrol.outcome
```
Infer the AF for controls only for CHD. Guess that CHD population prevalence is 0.2, but just made this up
```{r}
dat$eaf.outcome.controls <- get_population_allele_frequency(
af = dat$eaf.outcome,
prop = dat$ncase.outcome / (dat$ncase.outcome + dat$ncontrol.outcome),
odds_ratio = exp(dat$beta.outcome),
prevalence = 0.2
)
```
Plot again
```{r}
plot(eaf.exposure ~ eaf.outcome.controls, data=dat)
```
## Compare against 1000 genomes super populations
Look up the allele frequencies for these SNPs in 1000 genomes
```{r}
ref <- afl2_rsid(dat$SNP)
```
Compare allele frequencies of exposure, outcome, and reference populations
```{r}
dat %>%
select(SNP, eaf.exposure, eaf.outcome.controls) %>%
inner_join(., ref, by=c("SNP"="rsid")) %>%
select(eaf.exposure, eaf.outcome.controls, starts_with("AF.")) %>%
pairs()
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment