大家好,我是鄧飛,今天介紹一下TwoSampleMR
包如何設置token,從而可以使用數(shù)據(jù)庫的數(shù)據(jù)進行孟德爾隨機化的分析。
1,安裝TwoSampleMR包
這個包在github上面,之前流行用devtools
包安裝github上的R包,但是devtools本身就特別難安裝,好在現(xiàn)在有了 remotes
包,這個包比較好安裝,進而github上面的包也比較好安裝了。
# install.packages("remotes")
library(remotes)
install_github("MRCIEU/TwoSampleMR")
將上面的代碼貼到Rstudio中執(zhí)行,就可以了。如果顯示沒有 remote包,就把注釋去掉,再運行就行了。

2, 運行MR示例數(shù)據(jù)
library(TwoSampleMR)
# List available GWASs
ao <- available_outcomes()
# Get instruments
exposure_dat <- extract_instruments("ieu-a-2")
# Get effects of instruments on outcome
outcome_dat <- extract_outcome_data(snps=exposure_dat$SNP, outcomes = "ieu-a-7")
# Harmonise the exposure and outcome data
dat <- harmonise_data(exposure_dat, outcome_dat)
# Perform MR
然后發(fā)現(xiàn)報錯了:
> library(TwoSampleMR)
TwoSampleMR version 0.6.8
載入程輯包:'TwoSampleMR’
The following object is masked from 'package:remotes’:
add_metadata
> # List available GWASs
> ao <- available_outcomes()
Error in `dplyr::bind_rows()`:
! Argument 1 must be a data frame or a named atomic vector.
報錯的信息是:Error in dplyr::bind_rows()
:
! Argument 1 must be a data frame or a named atomic vector.
Run rlang::last_trace()
to see where the error occurred.
看起來是dplyr的錯,其實不是,繼續(xù)運行看看能不能讀取ieu的數(shù)據(jù):
> bmi_exp_dat <- extract_instruments(outcomes = 'ieu-a-2')
現(xiàn)在的報錯信息是:> bmi_exp_dat <- extract_instruments(outcomes = 'ieu-a-2')
Error in if (nrow(d) == 0) return(NULL) : 參數(shù)長度為零
憂傷,如果你以為自己是R語言大神,想從R語言包安裝的角度,包沖突的角度去解決問題,最后很大可能是 砸電腦!??!
正確的解決方法,是查看官網,如果你把報錯信息貼到網上面,大概率也是找不到答案,因為之前TwoSampleMR
還沒有這個問題,網上的東西是互相抄,垃圾信息滿天飛,第一手資料永遠是官網。
https://mrcieu./ieugwasr/articles/guide.html
里面有一句話:
From 1st May 2024, most queries to the OpenGWAS API will require user authentication. For more information on why this is necessary, see this [blog post](https://blog./posts/user-auth-spring-2024/).
從2024年5月1號,TwoSampleMR包需要設置token之后,才可以訪問數(shù)據(jù)庫,所以,下面就是如何設置token的問題了。
3,TwoSampleMR設置token
官方推薦方案:
A:更新你的R包
Please update your TwoSampleMR and ieugwasr packages - you can use the following command to do this.
install.packages("TwoSampleMR", repos = c("https://mrcieu.v", "https://cran."))
B:設置token
Then you need to obtain an OPENGWAS_JWT token - see the ieugwasr documentation https://mrcieu./ieugwasr/articles/guide.html - and store it in your .Renviron file - then restart R.
Login to https://api./profile/
Generate a new token
Add OPENGWAS_JWT=<token>
to your .Renviron
file. This file could be either in your home directory or in the working directory of your R session. You can check the location of your .Renviron
file by running Sys.getenv("R_ENVIRON_USER")
in R.
Restart your R session
To check that your token is being recognised, run [ieugwasr::get_opengwas_jwt()](https://mrcieu./ieugwasr/reference/get_opengwas_jwt.html)
. If it returns a long random string then you are authenticated.
To check that your token is working, run [user()](https://mrcieu./ieugwasr/reference/user.html)
. It will make a request to the API for your user information using your token. It should return a list with your user information. If it returns an error, then your token is not working.
下面,我將上面的步驟,結合我自己的成功操作過程,介紹一下,跟著我的步驟,你也肯定能搞定啦!
3.1 登錄opengwas,注冊一下
登錄注冊,用github賬號,https://api./profile/
3.2 創(chuàng)建token
按照下面的截圖就能搞定。

把token復制一下。
3.3 在R語言中檢測是否有token
如果之前沒有設置過,肯定是沒有的。
Sys.getenv("R_ENVIRON_USER")
我的返回結果:
> Sys.getenv("R_ENVIRON_USER")
[1] ""
可以看到,沒有設置。
那就需要在文檔文件夾中,新建一個.Renviron
文件

3.4 把token放到新建的.Renviron
文件中
OPENGWAS_JWT="這里粘貼你的token"

3.5 重啟R語言(必須)
重啟R語言,然后鍵入下面代碼,測試token是否設置成功:
## 測試token是否有效
library(ieugwasr)
user()

可以看到個人的信息,就設置成功了。
4. 測試MR示例數(shù)據(jù)
library(TwoSampleMR)
# List available GWASs
ao <- available_outcomes()
# Get instruments
exposure_dat <- extract_instruments("ieu-a-2")
# Get effects of instruments on outcome
outcome_dat <- extract_outcome_data(snps=exposure_dat$SNP, outcomes = "ieu-a-7")
# Harmonise the exposure and outcome data
dat <- harmonise_data(exposure_dat, outcome_dat)
# Perform MR
res <- mr(dat)
運行結果:

作圖結果:

這就搞定了。