-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.Rmd
406 lines (300 loc) · 9.7 KB
/
setup.Rmd
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
---
title: '494 final project'
author: "Kristy Ma, Rita Li"
date: "`r Sys.Date()`"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, error = TRUE)
```
# Install and Load Packages
if (!require("BiocManager", quietly = TRUE))
install.packages("BiocManager")
BiocManager::install("snpStats")
```{r load-packages, message = F}
library(snpStats)
library(dplyr)
library(ggplot2)
library(broom)
library(SNPRelate)
library(GENESIS)
library(GWASTools)
```
# Data preparation
## load data
```{r read data, cache=TRUE}
fam <- './1_QC_GWAS/HapMap_3_r3_1.fam'
bim <- './1_QC_GWAS/HapMap_3_r3_1.bim'
bed <- './1_QC_GWAS/HapMap_3_r3_1.bed'
hapmap <- read.plink(bed, bim, fam)
names(hapmap)
# snpgdsBED2GDS(bed, fam, bim, "lab2.gds")
# open
# genofile <- snpgdsOpen("lab2.gds")
# genofile
# # close
# snpgdsClose(genofile)
```
## Separate two sets: get uncorr, corr names
```{r use lab2.gds to do king robust estimation}
genofile <- snpgdsOpen("lab2.gds")
Kingoutput <- snpgdsIBDKING(genofile)
king.matrix <- Kingoutput$kinship
sample.id <- Kingoutput$sample.id
colnames(king.matrix) <- sample.id
rownames(king.matrix) <- sample.id
partition <- pcairPartition(kinobj = king.matrix ,divobj = king.matrix, )
# 58 individuals
related <- partition$rels
# 107 uncorrelated individuals
unrelated <- partition$unrels
```
- `genotypes` contains the genotype data
- `fam` contains information on each individual
- `map` contains information on each SNP
## Remove monomorphic from map and genotype
```{r remove-mono-from-map}
maf <- col.summary(hapmap$genotypes)$MAF
calls <- col.summary(hapmap$genotypes)$Call.rate
hapmap$map <- hapmap$map %>%
mutate(MAF = maf) %>% filter(MAF > 0 & calls == 1)
# nrow(hapmap$map) 949972
hapmap$genotypes <- hapmap$genotypes[,hapmap$map$snp.name]
```
## Explore correlated dataset (just hapmap itself!)
Get info about correlated dataset:
```{r explore-correlated_dataset}
cor_geno <- hapmap$genotypes # genotype information
cor_fam <- hapmap$fam #family information
map <- hapmap$map #SNPs information (can be used for both correlated and uncorrelated data set)
```
## Explore uncorrelated dataset (subsetting from the hapmap)
```{r explore-uncorrelated-dataset}
related.index <- c()
a <- 1
for (i in 1:165){
if (rownames(hapmap$genotypes[i,])%in%unrelated){
related.index[a] <- i
a <- a+1
}
}
uncor_geno <- hapmap$genotypes[related.index] #genotype information
uncor_fam <- hapmap$fam[rownames(hapmap$fam) %in% unrelated, ] #family information
```
## Shrinking SNP number
```{r select first 500 snps}
## correlated
cor_geno <- cor_geno[,1:500]
## uncorrelated
uncor_geno <- uncor_geno[,1:500]
map <- map %>%
filter(snp.name %in% colnames(cor_geno))
```
## Format genodata for LM + PCA
```{r format geno data}
# corr
cor.geno <- as(cor_geno, "numeric")
# nrow(cor.geno)
# ncol(cor.geno) this is 165*500
# uncorr
uncor.geno <- as(uncor_geno, "numeric")
# nrow(uncor.geno)
# ncol(uncor.geno) this is 107*500
```
## Simulate trait
```{r}
set.seed(494)
# null y:corr
y_cor_null <- rnorm(165, mean = 50, sd = 2)
# null y: uncorr
y_uncor_null <- rnorm(107, mean = 50, sd = 2)
# associative y: corr (based on "rs3766191" and "rs2985855")
y_cor_asso <- cor.geno[,"rs3766191"]*2 + cor.geno[,"rs2985855"] * 2 + rnorm(165, mean = 50, sd = 2)
# associative y: uncorr
y_uncor_asso <- uncor.geno[,"rs3766191"]*2 + uncor.geno[,"rs2985855"] * 2 + rnorm(107, mean = 50, sd = 2)
```
### write plink for LMM (GENESIS)
```{r write plink}
# snp.ids = colnames(cor_geno)
# cor_geno_int <- substr(snp.ids, start = 3, stop = nchar(snp.ids))
# cor_geno_gds <- cor_geno
# colnames(cor_geno_gds) <- cor_geno_int
## cor null
write.plink(file.base="cor_null",
snps= cor_geno,
subject.data=cor_fam,
phenotype = as.numeric(y_cor_null),
sex = as.numeric(cor_fam$sex),
snp.major=FALSE)
fam <- 'cor_null.fam'
bim <- 'cor_null.bim'
bed <- 'cor_null.bed'
snpgdsBED2GDS(bed, fam, bim, "cor_null.gds")
## cor asso
write.plink(file.base="cor_asso",
snps=cor_geno,
subject.data=cor_fam,
phenotype = as.numeric(y_cor_asso),
sex = as.numeric(cor_fam$sex),
snp.major=FALSE)
fam <- 'cor_asso.fam'
bim <- 'cor_asso.bim'
bed <- 'cor_asso.bed'
snpgdsBED2GDS(bed, fam, bim, "cor_asso.gds")
## uncor null
write.plink(file.base="uncor_null",
snps=uncor_geno,
subject.data=uncor_fam,
phenotype = as.numeric(y_uncor_null),
sex = as.numeric(uncor_fam$sex),
snp.major=FALSE)
fam <- 'uncor_null.fam'
bim <- 'uncor_null.bim'
bed <- 'uncor_null.bed'
snpgdsBED2GDS(bed, fam, bim, "uncor_null.gds")
## uncor asso
write.plink(file.base="uncor_asso",
snps=uncor_geno,
subject.data=uncor_fam,
phenotype = as.numeric(y_uncor_asso),
sex = as.numeric(uncor_fam$sex),
snp.major=FALSE)
fam <- 'uncor_asso.fam'
bim <- 'uncor_asso.bim'
bed <- 'uncor_asso.bed'
snpgdsBED2GDS(bed, fam, bim, "uncor_asso.gds")
```
## Reformat data for analysis
The `snpstats` package uses a unique format to store data. Currently, genotypes are coded as 01, 02, and 03 (with 00 representing missing values:
```{r look-at-genotypes}
# look at first five rows/columns
[email protected][1:5,1:5]
```
If the conversion was successful, you should now see a matrix of 0's, 1's, and 2's.
**Before you go on, check in with the others at your table. Was everyone able to get to this point successfully? Does anyone have any questions so far?**
## Simulate Trait
Let's simulate a trait that depends on the SNP known as *rs2476601*. Here's what we know about this SNP:
```{r look-at-causal-SNP}
hapmap$map %>%
filter(snp.name == 'rs2476601')
```
Now, let's create a quantitative trait `y` that depends on the genotype at this SNP plus some random noise:
```{r simulate-trait}
n <- nrow(X)
y <- X[,'rs2476601'] + rnorm(n, 0, 1)
head(y)
```
### Getting started
To start, let's look at what happens when we run marginal regression on the first five SNPs in this dataset:
```{r fit-initial-models}
## fit models at first few SNPs
mod1 <- lm(y ~ X[,1])
mod2 <- lm(y ~ X[,2])
mod3 <- lm(y ~ X[,3])
mod4 <- lm(y ~ X[,4])
mod5 <- lm(y ~ X[,5])
```
```{r remove-mono-from-map}
# keep only those SNPs with MAF > 0
map.clean <- hapmap$map %>%
filter(MAF > 0)
nrow(map.clean)
```
```{r remove-mono-from-genotypes}
# create vector of which SNPs have a MAF of 0
monomorphic <- which(maf == 0)
head(monomorphic)
# remove columns in the monomorphic vector
X.clean <- X[,-monomorphic]
#confirmed the ncol and nrow
ncol(X.clean)
nrow(X.clean)
```
**Confirm that the new "clean" genotype matrix has the correct number of rows and columns before you move on.**
### Analyze chromosome 1
Even after removing the monomorphic SNPs, we still have `r ncol(X, clean)` variants remaining. This might take awhile to analyze in R, so let's focus on just the SNPs on the first chromosome to start.
Run the code chunk below to make a list of which SNPs live on chromosome 1:
```{r find-chr1-snps}
chr1.snps <- which(map.clean$chromosome == 1)
head(chr1.snps)
length(chr1.snps)
```
Now, we're going to loop through each of the SNPs on chromosome 1, fitting a linear regression model at each SNP. For each model, we'll record the estimates (`betas`), standard errors (`ses`), test statistics (`tstats`) and p-values (`pvals`) for the coefficient of interest (the slope).
```{r run-gwas-chr1}
# set up empty vectors for storing results
betas <- c()
ses <- c()
tstats <- c()
pvals <- c()
# loop through chromosome 1 SNPs
for(i in chr1.snps){
# print out occasional updates telling us what SNP we're analyzing
if(i %% 5000 == 0) print(paste('Analyzing SNP', i))
# fit model
mod <- lm(y ~ X.clean[,i])
# get coefficient information
coefinfo <- tidy(mod)
# record estimate, SE, test stat, and p-value
betas[i] <- coefinfo$estimate[2]
ses[i] <- coefinfo$std.error[2]
tstats[i] <- coefinfo$statistic[2]
pvals[i] <- coefinfo$p.value[2]
}
```
Let's add our results to our map data frame that contains information about each SNP:
```{r chr1-results}
# start with the map info for the chr 1 SNPs
chr1.results <- map.clean %>%
filter(chromosome == 1)
# then add betas, SEs, etc.
chr1.results <- chr1.results %>%
mutate(Estimate = betas,
Std.Error = ses,
Test.Statistic = tstats,
P.Value = pvals)
# look at results
head(chr1.results)
```
```{r plot-minus-log-pvals}
ggplot(chr1.results, aes(x=position, y=-log(P.Value)))+geom_point()
```
### Analyze all chromosomes
As time allows, try repeating the analysis above, but now looking at the SNPs on other chromosomes as well. *Hint: the main thing you'll need to change is which SNPs you're looping over in your for loop.*
```{r}
chr3.snps <- which(map.clean$chromosome == 3)
head(chr3.snps)
length(chr3.snps)
betas <- c()
ses <- c()
tstats <- c()
pvals <- c()
# loop through chromosome 3 SNPs
for(i in chr3.snps){
# print out occasional updates telling us what SNP we're analyzing
if(i %% 5000 == 0) print(paste('Analyzing SNP', i))
# fit model
mod <- lm(y ~ X.clean[,i])
# get coefficient information
coefinfo <- tidy(mod)
# record estimate, SE, test stat, and p-value
betas[i] <- coefinfo$estimate[2]
ses[i] <- coefinfo$std.error[2]
tstats[i] <- coefinfo$statistic[2]
pvals[i] <- coefinfo$p.value[2]
}
chr3.results <- map.clean %>%
filter(chromosome == 3)
ncol(chr3.results)
head(betas)
# then add betas, SEs, etc.
chr3.results <- chr3.results %>%
mutate(Estimate = betas,
Std.Error = ses,
Test.Statistic = tstats,
P.Value = pvals)
# look at results
head(chr3.results)
```
```{r}
ggplot(chr3.results, aes(x=position, y=-log(P.Value)))+geom_point()
```