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

2, 運(yùn)行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)報(bào)錯(cuò)了:
> 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.
報(bào)錯(cuò)的信息是: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的錯(cuò),其實(shí)不是,繼續(xù)運(yùn)行看看能不能讀取ieu的數(shù)據(jù):
> bmi_exp_dat <- extract_instruments(outcomes = 'ieu-a-2')
現(xiàn)在的報(bào)錯(cuò)信息是:> bmi_exp_dat <- extract_instruments(outcomes = 'ieu-a-2')
Error in if (nrow(d) == 0) return(NULL) : 參數(shù)長(zhǎng)度為零
憂傷,如果你以為自己是R語言大神,想從R語言包安裝的角度,包沖突的角度去解決問題,最后很大可能是 砸電腦?。?!
正確的解決方法,是查看官網(wǎng),如果你把報(bào)錯(cuò)信息貼到網(wǎng)上面,大概率也是找不到答案,因?yàn)橹?code style="word-wrap: break-word;margin: 0 2px;background-color: rgba(27,31,35,.05);font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 1.5;font-size: 14px;padding: 3px 5px;border-radius: 2px;color: #009688;">TwoSampleMR還沒有這個(gè)問題,網(wǎng)上的東西是互相抄,垃圾信息滿天飛,第一手資料永遠(yuǎn)是官網(wǎng)。
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號(hào),TwoSampleMR包需要設(shè)置token之后,才可以訪問數(shù)據(jù)庫,所以,下面就是如何設(shè)置token的問題了。
3,TwoSampleMR設(shè)置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:設(shè)置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.
下面,我將上面的步驟,結(jié)合我自己的成功操作過程,介紹一下,跟著我的步驟,你也肯定能搞定啦!
3.1 登錄opengwas,注冊(cè)一下
登錄注冊(cè),用github賬號(hào),https://api./profile/
3.2 創(chuàng)建token
按照下面的截圖就能搞定。

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

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

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

可以看到個(gè)人的信息,就設(shè)置成功了。
4. 測(cè)試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)
運(yùn)行結(jié)果:

作圖結(jié)果:

這就搞定了。