{"id":243,"date":"2024-07-18T18:26:34","date_gmt":"2024-07-18T10:26:34","guid":{"rendered":"http:\/\/www.alearnerlin.top\/?p=243"},"modified":"2024-07-18T18:27:48","modified_gmt":"2024-07-18T10:27:48","slug":"eaf%e5%80%bc%e5%a1%ab%e8%a1%a5-%e6%90%ac%e7%a0%96%e8%b4%b4","status":"publish","type":"post","link":"https:\/\/www.alearnerlin.top\/index.php\/2024\/07\/18\/eaf%e5%80%bc%e5%a1%ab%e8%a1%a5-%e6%90%ac%e7%a0%96%e8%b4%b4\/","title":{"rendered":"EAF\u503c\u586b\u8865&#8212;\u642c\u7816\u8d34"},"content":{"rendered":"<h2>EAF\u586b\u8865<\/h2>\n<p>\u5728gwas\u6570\u636e\u83b7\u53d6\u5f53\u4e2d\uff0c\u6709\u8bb8\u591a\u6570\u636e\u5e76\u672a\u542b\u6709eaf\u503c\uff0c\u8fd9\u5bf9\u540e\u7eed\u7684\u5206\u6790\u6781\u4e3a\u4e0d\u5229\u3002<\/p>\n<ul>\n<li>EAF\u503c\uff1a<\/li>\n<\/ul>\n<h2>\u5982\u4f55\u8fdb\u884cEAF\u503c\u7684\u586b\u8865<\/h2>\n<p>\u53c2\u8003\u6587\u7ae0\uff1a<a href=\"https:\/\/cloud.tencent.com\/developer\/article\/2327202\">https:\/\/cloud.tencent.com\/developer\/article\/2327202<\/a><\/p>\n<p>\u6211\u91c7\u53d6\u4e86\u4ee5\u4e0b\u4e24\u79cd\u65b9\u6cd5<\/p>\n<ol>\n<li>\n<p>snp_add_eaf\u51fd\u6570<\/p>\n<pre><code class=\"language-r\">snp_add_eaf <- function(dat, build = \"37\", pop = \"EUR\")\n{\nstopifnot(build %in% c(\"37\",\"38\"))\nstopifnot(\"SNP\" %in% names(dat))\n\n# Create and get a url\nserver <- ifelse(build == \"37\",\"http:\/\/grch37.rest.ensembl.org\",\"http:\/\/rest.ensembl.org\")\npop <- paste0(\"1000GENOMES:phase_3:\",pop)\n\nsnp_reverse_base <- function(x)\n{\nx <- stringr::str_to_upper(x)\nstopifnot(x %in% c(\"A\",\"T\",\"C\",\"G\"))\nswitch(x,\"A\"=\"T\",\"T\"=\"A\",\"C\"=\"G\",\"G\"=\"C\")\n}\n\nres_tab <- lapply(1:nrow(dat), function(i)\n{\nprint(paste0(\"seaching for No.\", i, \" SNP\"))\ndat_i <- dat[i,]\n\next <- paste0(\"\/variation\/Homo_sapiens\/\",dat_i$SNP, \"?content-type=application\/json;pops=1\")\nurl <- paste(server, ext, sep = \"\")\nres <- httr::GET(url)\n\n# Converts http errors to R errors or warnings\nhttr::stop_for_status(res)\n\n# Convert R objects from JSON\nres <- httr::content(res)\nres_pop <- jsonlite::fromJSON(jsonlite::toJSON(res))$populations\n\n# Filter query results based on population set\nres_pop <- try(res_pop[res_pop$population == pop,])\nif(\"try-error\" %in% class(res_pop))\n{\n  print(paste0(\"There is not information for population \",pop))\n  queried_effect_allele <- \"NR\"\n  queried_other_allele <- \"NR\"\n  queried_eaf <- -1\n}\nelse\n{\n  if(nrow(res_pop)==0)\n  {\n    print(paste0(\"There is not information for population \",pop))\n    queried_effect_allele <- \"NR\"\n    queried_other_allele <- \"NR\"\n    queried_eaf <- -1\n  }\n  else\n  {\n    queried_effect_allele <- res_pop[1,\"allele\"][[1]]\n    queried_other_allele <- res_pop[2,\"allele\"][[1]]\n    queried_eaf <- res_pop[1,\"frequency\"][[1]]    \n  }\n}\n\neffect_allele <- ifelse(\"effect_allele.exposure\" %in% names(dat),\n                        dat_i$effect_allele.exposure,\n                        dat_i$effect_allele)\n\nother_allele <- ifelse(\"effect_allele.exposure\" %in% names(dat),\n                        dat_i$other_allele.exposure,\n                        dat_i$other_allele)\n\nif(\"effect_allele.exposure\" %in% names(dat))\n{\n  name_output <- unique(c(names(dat), \"eaf.exposure\",\"reliability.exposure\"))\n}\nelse\n{\n  name_output <- unique(c(names(dat), \"eaf\",\"reliability.exposure\"))\n}\n\nlen_effect_allele <- nchar(effect_allele)\nlen_other_allele <- nchar(other_allele)\n\nif(len_effect_allele==1&len_other_allele==1)\n{\n  if((queried_effect_allele==effect_allele & queried_other_allele==other_allele)|\n     (queried_effect_allele==other_allele & queried_other_allele==effect_allele))\n  {\n    dat_i$eaf.exposure <- ifelse(effect_allele == queried_effect_allele,\n                                 queried_eaf,\n                                 1-queried_eaf)\n    dat_i$eaf <- dat_i$eaf.exposure \n    dat_i$reliability.exposure <- \"high\"\n  }\n  else\n  {\n    r_queried_effect_allele <- snp_reverse_base(queried_effect_allele)\n    r_queried_other_allele <- snp_reverse_base(queried_other_allele)\n    if((r_queried_effect_allele==effect_allele & r_queried_other_allele==other_allele)|\n       (r_queried_effect_allele==other_allele & r_queried_other_allele==effect_allele))\n    {\n      dat_i$eaf.exposure <- ifelse(effect_allele == r_queried_effect_allele,\n                                   queried_eaf,\n                                   1-queried_eaf)\n      dat_i$eaf <- dat_i$eaf.exposure \n      dat_i$reliability.exposure <- \"high\"\n    }\n    else\n    {\n      dat_i$eaf.exposure <- ifelse(effect_allele == queried_effect_allele,\n                                   queried_eaf,\n                                   1-queried_eaf)\n      dat_i$eaf <- dat_i$eaf.exposure \n      dat_i$reliability.exposure <- \"low\"\n    }\n  }\n}\n\nelse\n{\n  # To identify the potential DEL\/ INS\n  short_allele <- ifelse(len_effect_allele==1,\n                         effect_allele,\n                         other_allele)\n  short_allele_eaf <- ifelse(short_allele == queried_effect_allele, \n                             queried_eaf, \n                             1-queried_eaf)\n  dat_i$eaf.exposure <- ifelse(effect_allele == short_allele,\n                               short_allele_eaf,\n                               1-short_allele_eaf)\n  dat_i$eaf <- dat_i$eaf.exposure \n  dat_i$reliability.exposure <- \"low\"\n}\n\ndat_i[name_output]\n})\n\nreturn(do.call(rbind, res_tab))\n}<\/code><\/pre>\n<p>\u76f4\u63a5\u8fd0\u884c\u51fd\u6570\uff0c\u5728R\u4e2d\u5c31\u4f1a\u51fa\u73b0snp_add_eaf()\u51fd\u6570\u4e86<\/p>\n<p><strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><br \/>\n<strong>1. \u6b64\u65b9\u6cd5\u9002\u5408\u4e8e\u5c11\u91cf\u7684\u6570\u636e<\/strong><br \/>\n<strong>2.\u56e0\u4e3a\u4f7f\u7528\u4e86API\uff0c\u5927\u91cf\u8bf7\u6c42\u7684\u65f6\u5019\uff0c\u4f1a\u51fa\u73b0\u65e0\u6cd5\u54cd\u5e94\u7684\u95ee\u9898<\/strong><br \/>\n\u539f\u59cb\u4ee3\u7801\u51fa\u5904\uff1a<br \/>\n<a href=\"https:\/\/github.com\/linjf15\/MR_tricks\/tree\/main\/GWAS_preprocessing\">https:\/\/github.com\/linjf15\/MR_tricks\/tree\/main\/GWAS_preprocessing<\/a><br \/>\n<strong>2. \u4f7f\u7528get_eaf_from_1000G()<\/strong><\/p>\n<pre><code class=\"language-r\">\nget_eaf_from_1000G&lt;-function(dat,path,type=&quot;exposure&quot;){\n      corrected_eaf_expo&lt;-function(data_MAF){\n      effect=data_MAF$effect_allele.exposure\n      other=data_MAF$other_allele.exposure\n      A1=data_MAF$A1\n      A2=data_MAF$A2\n      MAF_num=data_MAF$MAF\n      EAF_num=1-MAF_num\n\n    harna&lt;-is.na(data_MAF$A1)\n    harna&lt;-data_MAF$SNP[which(harna==T)]\n    cor1&lt;-which(data_MAF$effect_allele.exposure !=data_MAF$A1)\n    data_MAF$eaf.exposure=data_MAF$MAF\n    data_MAF$type=&quot;raw&quot;\n    data_MAF$eaf.exposure[cor1]=EAF_num[cor1]\n    data_MAF$type[cor1]=&quot;corrected&quot;\n    cor2&lt;-which(data_MAF$other_allele.exposure ==data_MAF$A1)\n    cor21&lt;-setdiff(cor2,cor1)\n    cor12&lt;-setdiff(cor1,cor2)\n    error&lt;-c(cor12,cor21)\n    data_MAF$eaf.exposure[error]=NA\n    data_MAF$type[error]=&quot;error&quot;\n\n    data_MAF&lt;-list(data_MAF=data_MAF,cor1=cor1,harna=harna,error=error)\n\n    return(data_MAF)\n\n     }\n\n      corrected_eaf_out&lt;-function(data_MAF){\n        effect=data_MAF$effect_allele.outcome\n        other=data_MAF$other_allele.outcome\n        A1=data_MAF$A1\n        A2=data_MAF$A2\n        MAF_num=data_MAF$MAF\n        EAF_num=1-MAF_num\n\n    harna&lt;-is.na(data_MAF$A1)\n    harna&lt;-data_MAF$SNP[which(harna==T)]\n\n    cor1&lt;-which(data_MAF$effect_allele.outcome !=data_MAF$A1)\n\n    data_MAF$eaf.outcome=data_MAF$MAF\n    data_MAF$type=&quot;raw&quot;\n    data_MAF$eaf.outcome[cor1]=EAF_num[cor1]\n    data_MAF$type[cor1]=&quot;corrected&quot;\n    cor2&lt;-which(data_MAF$other_allele.outcome ==data_MAF$A1)\n    cor21&lt;-setdiff(cor2,cor1)\n    cor12&lt;-setdiff(cor1,cor2)\n    error&lt;-c(cor12,cor21)\n    data_MAF$eaf.outcome[error]=NA\n    data_MAF$type[error]=&quot;error&quot;\n\n    data_MAF&lt;-list(data_MAF=data_MAF,cor1=cor1,harna=harna,error=error)\n\n    return(data_MAF)\n\n      }\n\n      if(type==&quot;exposure&quot; &amp;&amp; (is.na(dat$eaf.exposure[1])==T || is.null(dat$eaf.exposure)==T)){\n        r&lt;-nrow(dat)\n\n    setwd(path)\n    MAF&lt;-fread(&quot;fileFrequency.frq&quot;,header = T)\n\n    dat&lt;-merge(dat,MAF,by.x = &quot;SNP&quot;,by.y = &quot;SNP&quot;,all.x = T)\n\n    dat&lt;-corrected_eaf_expo(dat)\n\n    cor1&lt;-dat$cor1\n\n    harna&lt;-dat$harna\n\n    error&lt;-dat$error\n\n    dat&lt;-dat$data_MAF\n\n    print(paste0(&quot;\u4e00\u5171\u6709&quot;,(r-length(harna)-length(error)),&quot;\u4e2aSNP\u6210\u529f\u5339\u914dEAF,\u5360\u6bd4&quot;,(r-length(harna)-length(error))\/r*100,&quot;%&quot;))\n\n    print(paste0(&quot;\u4e00\u5171\u6709&quot;,length(cor1),&quot;\u4e2aSNP\u662fmajor allele\uff0cEAF\u88ab\u8ba1\u7b97\u4e3a1-MAF,\u5728\u6210\u529f\u5339\u914d\u6570\u76ee\u4e2d\u5360\u6bd4&quot;,length(cor1)\/(r-length(harna)-length(error))*100,&quot;%&quot;))\n\n    print(paste0(&quot;\u4e00\u5171\u6709&quot;,length(harna),&quot;\u4e2aSNP\u57281000G\u4e2d\u627e\u4e0d\u5230\uff0c\u5360\u6bd4&quot;,length(harna)\/r*100,&quot;%&quot;))\n\n    print(paste0(&quot;\u4e00\u5171\u6709&quot;,length(error),&quot;\u4e2aSNP\u5728\u8f93\u5165\u6570\u636e\u4e0e1000G\u4e2d\u6548\u5e94\u5217\u4e0e\u53c2\u7167\u5217\uff0c\u5c06\u5254\u9664eaf\uff0c\u5360\u6bd4&quot;,length(error)\/r*100,&quot;%&quot;))\n\n    print(&quot;\u8f93\u51fa\u6570\u636e\u4e2d\u7684type\u5217\u8bf4\u660e\uff1a&quot;)\n    print(&quot;raw\uff1aEAF\u76f4\u63a5\u7b49\u4e8e1000G\u91cc\u7684MAF\u6570\u503c\uff0c\u56e0\u4e3a\u6548\u5e94\u5217\u662fminor allele&quot;)\n    print(&#039;corrected\uff1aEAF\u7b49\u4e8e1000G\u4e2d1-MAF\uff0c\u56e0\u4e3a\u6548\u5e94\u5217\u662fmajor allele&#039;)\n    print(&quot;error\uff1a\u8f93\u5165\u6570\u636e\u4e0e1000G\u91cc\u9762\u63d0\u4f9b\u7684\u6570\u636e\u5b8c\u5168\u4e0d\u4e00\u81f4\uff0c\u6bd4\u5982\u8fd9\u4e2aSNP\u8f93\u5165\u7684\u6548\u5e94\u5217\u662fC\uff0c\u53c2\u7167\u5217\u662fG\uff0c\u4f46\u662f1000G\u63d0\u4f9b\u7684\u662fA-T\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cEAF\u4f1a\u88ab\u6e05\u7a7a\uff08NA\uff09\uff0c\u5f53\u6210\u5339\u914d\u5931\u8d25&quot;)\n\n    return(dat)\n      }\n\n      if(type==&quot;outcome&quot; &amp;&amp; (is.na(dat$eaf.outcome[1])==T || is.null(dat$eaf.outcome)==T)){\n        r&lt;-nrow(dat)\n\n    setwd(path)\n    MAF&lt;-fread(&quot;fileFrequency.frq&quot;,header = T)\n\n    dat&lt;-merge(dat,MAF,by.x = &quot;SNP&quot;,by.y = &quot;SNP&quot;,all.x = T)\n\n    dat&lt;-corrected_eaf_out(dat)\n\n    cor1&lt;-dat$cor1\n\n    harna&lt;-dat$harna\n\n    error&lt;-dat$error\n\n    dat&lt;-dat$data_MAF\n\n    print(paste0(&quot;\u4e00\u5171\u6709&quot;,(r-length(harna)-length(error)),&quot;\u4e2aSNP\u6210\u529f\u5339\u914dEAF,\u5360\u6bd4&quot;,(r-length(harna)-length(error))\/r*100,&quot;%&quot;))\n\n    print(paste0(&quot;\u4e00\u5171\u6709&quot;,length(cor1),&quot;\u4e2aSNP\u662fmajor allele\uff0cEAF\u88ab\u8ba1\u7b97\u4e3a1-MAF,\u5728\u6210\u529f\u5339\u914d\u6570\u76ee\u4e2d\u5360\u6bd4&quot;,length(cor1)\/(r-length(harna)-length(error))*100,&quot;%&quot;))\n\n    print(paste0(&quot;\u4e00\u5171\u6709&quot;,length(harna),&quot;\u4e2aSNP\u57281000G\u627e\u4e0d\u5230\uff0c\u5360\u6bd4&quot;,length(harna)\/r*100,&quot;%&quot;))\n\n    print(paste0(&quot;\u4e00\u5171\u6709&quot;,length(error),&quot;\u4e2aSNP\u5728\u8f93\u5165\u6570\u636e\u4e0e1000G\u4e2d\u6548\u5e94\u5217\u4e0e\u53c2\u7167\u5217\uff0c\u5c06\u5254\u9664eaf\uff0c\u5360\u6bd4&quot;,length(error)\/r*100,&quot;%&quot;))\n\n    print(&quot;\u8f93\u51fa\u6570\u636e\u4e2d\u7684type\u5217\u8bf4\u660e\uff1a&quot;)\n    print(&quot;raw\uff1aEAF\u76f4\u63a5\u7b49\u4e8e1000G\u91cc\u7684MAF\u6570\u503c\uff0c\u56e0\u4e3a\u6548\u5e94\u5217\u662fminor allele&quot;)\n    print(&#039;corrected\uff1aEAF\u7b49\u4e8e1000G\u4e2d1-MAF\uff0c\u56e0\u4e3a\u6548\u5e94\u5217\u662fmajor allele&#039;)\n    print(&quot;error\uff1a\u8f93\u5165\u6570\u636e\u4e0e1000G\u91cc\u9762\u63d0\u4f9b\u7684\u6570\u636e\u5b8c\u5168\u4e0d\u4e00\u81f4\uff0c\u6bd4\u5982\u8fd9\u4e2aSNP\u8f93\u5165\u7684\u6548\u5e94\u5217\u662fC\uff0c\u53c2\u7167\u5217\u662fG\uff0c\u4f46\u662f1000G\u63d0\u4f9b\u7684\u662fA-T\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cEAF\u4f1a\u88ab\u6e05\u7a7a\uff08NA\uff09\uff0c\u5f53\u6210\u5339\u914d\u5931\u8d25&quot;)\n\n    return(dat)\n    }\n    else{return(dat)}\n    }<\/code><\/pre>\n<\/li>\n<\/ol>\n<pre><code>\n\n\u76f4\u63a5\u8fd0\u884c\u51fd\u6570\uff0c\u5728R\u4e2d\u5c31\u4f1a\u51fa\u73b0get_eaf_from_1000G()\u51fd\u6570\u4e86\n\n**\u6ce8\u610f\u4e8b\u9879\uff1a**\n**1. \u6b64\u65b9\u6cd5\u901f\u5ea6\u5feb\uff0c\u6548\u679c\u7a33\u5b9a\uff0c\u4e2a\u4eba\u6bd4\u8f83\u63a8\u8350**\n**2. \u9700\u8981\u83b7\u53d61000G\u53c2\u8003\u6587\u4ef6**\n\u539f\u59cb\u4ee3\u7801\u51fa\u5904\uff1a\n[https:\/\/github.com\/HaobinZhou\/Get_MR\/blob\/main\/Get_MR1.0 help.md]\n(\u4ee3\u7801\u4f5c\u8005\uff1a\u5e7f\u5dde\u533b\u79d1\u5927\u5b66\u7b2c\u4e00\u4e34\u5e8a\u5b66\u9662\u5468\u6d69\u5f6c \uff0c\u7b2c\u4e8c\u4e34\u5e8a\u5b66\u9662\u8c22\u6cbb\u946b)\n\n**\u53e6\uff1a\u6b64\u4e3a\u65b9\u6cd52\u4e2d\u53c2\u8003\u6587\u4ef6\u7684\u767e\u5ea6\u4e91\u94fe\u63a5**\n\u94fe\u63a5: https:\/\/pan.baidu.com\/s\/1ob6WjavJfk-6lgXa2wcp2Q?pwd=euht \u63d0\u53d6\u7801: euht \n\n\u5982\u6709\u76f8\u5173\u95ee\u9898\u9700\u8981\u8ba8\u8bba\uff0c\u53ef\u901a\u8fc7\u90ae\u7bb1\uff1alearner__lin2003@yeah.net\u8054\u7cfb\u6211\u3002\n2024-07-18 16:54:19 \u661f\u671f\u56db<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>EAF\u586b\u8865 \u5728gwas\u6570\u636e\u83b7\u53d6\u5f53\u4e2d\uff0c\u6709\u8bb8\u591a\u6570\u636e\u5e76\u672a\u542b\u6709eaf\u503c\uff0c\u8fd9\u5bf9\u540e\u7eed\u7684\u5206\u6790\u6781\u4e3a\u4e0d\u5229\u3002 EAF\u503c\uff1a \u5982\u4f55\u8fdb\u884c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,6],"tags":[],"class_list":["post-243","post","type-post","status-publish","format-standard","hentry","category-r","category-6"],"_links":{"self":[{"href":"https:\/\/www.alearnerlin.top\/index.php\/wp-json\/wp\/v2\/posts\/243","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.alearnerlin.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.alearnerlin.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.alearnerlin.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.alearnerlin.top\/index.php\/wp-json\/wp\/v2\/comments?post=243"}],"version-history":[{"count":10,"href":"https:\/\/www.alearnerlin.top\/index.php\/wp-json\/wp\/v2\/posts\/243\/revisions"}],"predecessor-version":[{"id":253,"href":"https:\/\/www.alearnerlin.top\/index.php\/wp-json\/wp\/v2\/posts\/243\/revisions\/253"}],"wp:attachment":[{"href":"https:\/\/www.alearnerlin.top\/index.php\/wp-json\/wp\/v2\/media?parent=243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.alearnerlin.top\/index.php\/wp-json\/wp\/v2\/categories?post=243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.alearnerlin.top\/index.php\/wp-json\/wp\/v2\/tags?post=243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}