linjf15 / MR_tricks

188 stars 37 forks source link

Questions about snp_add_eaf.R #6

Open Tengy5 opened 10 months ago

Tengy5 commented 10 months ago

How could I deal with "Error in FUN(X[[i]], ...) : Bad Request (HTTP 400)." Thank you very much! 大佬,这个报错怎么办呀,我试过了这个报错不定期跳出来,没啥规律,意思是同一个snp在不同时候都可能会跳这个报错,也不多,就20条以内就会跳出来TAT

Tengy5 commented 10 months ago

大佬我也是协和的!好有缘分哈哈哈哈!

Tengy5 commented 10 months ago

报错信息:

out.dat1 <- out.dat[c(10),] %>% snp_add_eaf.new(build = '37',pop = 'EAS') [1] "seaching for No.1 SNP" [1] "There is not information for population 1000GENOMES:phase_3:EAS" Error in snp_reverse_base(queried_effect_allele) : 不是所有的x %in% c("A", "T", "C", "G")都是TRUE out.dat[c(10),] : chr.outcome pos.outcome SNP effect_allele.outcome 10 6 32630741 rs17843717 T other_allele.outcome beta.outcome se.outcome pval.outcome outcome 10 C 0.0029615 0.012624 0.814525 telomere length mr_keep.outcome pval_origin.outcome id.outcome eaf.outcome 10 TRUE reported cA893O NA

linjf15 commented 9 months ago

How could I deal with "Error in FUN(X[[i]], ...) : Bad Request (HTTP 400)." Thank you very much! 大佬,这个报错怎么办呀,我试过了这个报错不定期跳出来,没啥规律,意思是同一个snp在不同时候都可能会跳这个报错,也不多,就20条以内就会跳出来TAT

The error might be associated with network connection.

linjf15 commented 9 months ago

报错信息:

out.dat1 <- out.dat[c(10),] %>% snp_add_eaf.new(build = '37',pop = 'EAS') [1] "seaching for No.1 SNP" [1] "There is not information for population 1000GENOMES:phase_3:EAS" Error in snp_reverse_base(queried_effect_allele) : 不是所有的x %in% c("A", "T", "C", "G")都是TRUE out.dat[c(10),] : chr.outcome pos.outcome SNP effect_allele.outcome 10 6 32630741 rs17843717 T other_allele.outcome beta.outcome se.outcome pval.outcome outcome 10 C 0.0029615 0.012624 0.814525 telomere length mr_keep.outcome pval_origin.outcome id.outcome eaf.outcome 10 TRUE reported cA893O NA

"There is not information for population 1000GENOMES:phase_3:EAS". Maybe the searched SNP is not reported in Asian population.

baixvwyl commented 6 months ago

大佬,请问一直出现报错怎么办?这是添加eaf的代码运行出现的错误 #########1.########[1] "seaching for No.1 SNP" Error in ans[npos] <- rep(no, length.out = len)[npos] : replacement has length zero In addition: Warning message: In rep(no, length.out = len) : 'x' is NULL so the result will be NULL Called from: ifelse("effect_allele.exposure" %in% names(dat), dat_i$effect_allele.exposure, dat_i$effect_allele) Browse[1]>

###########2.######但是单独添加other_allele或者单独添加effect_allele没有报错,如果一起添加会出现以下报错result <- snp_add_oa(data,build = "37", pop = "EUR") [1] "searching for No.1 SNP" Error in [[<-.data.frame(*tmp*, name_other_allele, value = logical(0)) : 替换数据里有0行,但数据有1 Called from: [[<-.data.frame(*tmp*, name_other_allele, value = logical(0)) Browse[1]> 请问该怎么解决啊?我的数据里面有SNP列的,

baixvwyl commented 6 months ago

Error in fetch_eaf(dat_i$SNP, pop, server) : Bad Request (HTTP 400). Called from: httr::stop_for_status(res)还会出现这样的错误,不定时没有规律

baixvwyl commented 6 months ago

换了网络Error in fetch_eaf(dat_i$SNP, pop, server) : Bad Request (HTTP 400). Called from: httr::stop_for_status(res)就没问题了,谢谢大佬的代码

baixvwyl commented 6 months ago

Error in snp_reverse_base(effect_allele) : 不是所有的x %in% c("A", "T", "C", "G")都是TRUE,这样错误该怎么解决呢?是要手动删除SNP吗?

baixvwyl commented 6 months ago

而且很奇怪,我再重新运行出现错误的位置就变了

linjf15 commented 6 months ago

Error in snp_reverse_base(effect_allele) : 不是所有的x %in% c("A", "T", "C", "G")都是TRUE,这样错误该怎么解决呢?是要手动删除SNP吗?

Delete in/del type SNP, and keep only point-mutation type SNP.

baixvwyl commented 5 months ago

好的,谢谢大佬的解答

---- 回复的原邮件 ---- | 发件人 | Jianfeng @.> | | 日期 | 2024年04月03日 07:16 | | 收件人 | @.> | | 抄送至 | @.>@.> | | 主题 | Re: [linjf15/MR_tricks] Questions about snp_add_eaf.R (Issue #6) |

Error in snp_reverse_base(effect_allele) : 不是所有的x %in% c("A", "T", "C", "G")都是TRUE,这样错误该怎么解决呢?是要手动删除SNP吗?

Delete in/del type SNP, and keep only point-mutation type SNP.

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>

mengdeyu539 commented 4 months ago

报错信息:

out.dat1 <- out.dat[c(10),] %>% snp_add_eaf.new(build = '37',pop = 'EAS') [1] "seaching for No.1 SNP" [1] "There is not information for population 1000GENOMES:phase_3:EAS" Error in snp_reverse_base(queried_effect_allele) : 不是所有的x %in% c("A", "T", "C", "G")都是TRUE out.dat[c(10),] : chr.outcome pos.outcome SNP effect_allele.outcome 10 6 32630741 rs17843717 T other_allele.outcome beta.outcome se.outcome pval.outcome outcome 10 C 0.0029615 0.012624 0.814525 telomere length mr_keep.outcome pval_origin.outcome id.outcome eaf.outcome 10 TRUE reported cA893O NA

snp_add_eaf <- function(dat, build = "37", pop = "EUR") { stopifnot(build %in% c("37", "38")) stopifnot("SNP" %in% names(dat))

server <- ifelse(build == "37", "http://grch37.rest.ensembl.org", "http://rest.ensembl.org") pop <- paste0("1000GENOMES:phase_3:", pop)

snp_reverse_base <- function(x) { x <- stringr::str_to_upper(x) stopifnot(x %in% c("A", "T", "C", "G")) switch(x, "A" = "T", "T" = "A", "C" = "G", "G" = "C") }

res_tab <- lapply(1:nrow(dat), function(i) { print(paste0("Searching for No.", i, " SNP")) dat_i <- dat[i, ]

ext <- paste0("/variation/Homo_sapiens/", dat_i$SNP, "?content-type=application/json;pops=1")
url <- paste(server, ext, sep = "")
res <- httr::GET(url)

httr::stop_for_status(res)

res <- httr::content(res)
res_pop <- jsonlite::fromJSON(jsonlite::toJSON(res))$populations

if (is.null(res_pop) || !is.data.frame(res_pop)) {
  print(paste0("There is no information for population ", pop))
  queried_effect_allele <- "NR"
  queried_other_allele <- "NR"
  queried_eaf <- -1
} else {
  res_pop <- res_pop[res_pop$population == pop, ]

  if (nrow(res_pop) == 0) {
    print(paste0("There is no information for population ", pop))
    queried_effect_allele <- "NR"
    queried_other_allele <- "NR"
    queried_eaf <- -1
  } else {
    queried_effect_allele <- res_pop[1, "allele"][[1]]
    queried_other_allele <- res_pop[2, "allele"][[1]]
    queried_eaf <- res_pop[1, "frequency"][[1]]
  }
}

effect_allele <- ifelse("effect_allele.exposure" %in% names(dat),
                        dat_i$effect_allele.exposure,
                        dat_i$effect_allele)

other_allele <- ifelse("effect_allele.exposure" %in% names(dat),
                       dat_i$other_allele.exposure,
                       dat_i$other_allele)

if ("effect_allele.exposure" %in% names(dat)) {
  name_output <- unique(c(names(dat), "eaf.exposure", "reliability.exposure"))
} else {
  name_output <- unique(c(names(dat), "eaf", "reliability.exposure"))
}

len_effect_allele <- nchar(effect_allele)
len_other_allele <- nchar(other_allele)

if (len_effect_allele == 1 & len_other_allele == 1) {
  if ((queried_effect_allele %in% c("A", "T", "C", "G")) & 
      (queried_other_allele %in% c("A", "T", "C", "G"))) {
    if ((queried_effect_allele == effect_allele & queried_other_allele == other_allele) |
        (queried_effect_allele == other_allele & queried_other_allele == effect_allele)) {
      dat_i$eaf.exposure <- ifelse(effect_allele == queried_effect_allele,
                                   queried_eaf,
                                   1 - queried_eaf)
      dat_i$eaf <- dat_i$eaf.exposure 
      dat_i$reliability.exposure <- "high"
    } else {
      r_queried_effect_allele <- snp_reverse_base(queried_effect_allele)
      r_queried_other_allele <- snp_reverse_base(queried_other_allele)
      if ((r_queried_effect_allele == effect_allele & r_queried_other_allele == other_allele) |
          (r_queried_effect_allele == other_allele & r_queried_other_allele == effect_allele)) {
        dat_i$eaf.exposure <- ifelse(effect_allele == r_queried_effect_allele,
                                     queried_eaf,
                                     1 - queried_eaf)
        dat_i$eaf <- dat_i$eaf.exposure 
        dat_i$reliability.exposure <- "high"
      } else {
        dat_i$eaf.exposure <- ifelse(effect_allele == queried_effect_allele,
                                     queried_eaf,
                                     1 - queried_eaf)
        dat_i$eaf <- dat_i$eaf.exposure 
        dat_i$reliability.exposure <- "low"
      }
    }
  } else {
    dat_i$eaf.exposure <- -1
    dat_i$eaf <- -1
    dat_i$reliability.exposure <- "low"
  }
} else {
  short_allele <- ifelse(len_effect_allele == 1,
                         effect_allele,
                         other_allele)
  short_allele_eaf <- ifelse(short_allele == queried_effect_allele, 
                             queried_eaf, 
                             1 - queried_eaf)
  dat_i$eaf.exposure <- ifelse(effect_allele == short_allele,
                               short_allele_eaf,
                               1 - short_allele_eaf)
  dat_i$eaf <- dat_i$eaf.exposure 
  dat_i$reliability.exposure <- "low"
}

dat_i[name_output]

})

return(do.call(rbind, res_tab)) } 可跳过错误

xiaobaiyaobianqiang commented 3 months ago

I meet the quertion too,and confused how to solve [1] "seaching for No.60 SNP" Error in res_pop[res_pop$population == pop, ] : 量度数目不对 [1] "There is not information for population 1000GENOMES:phase_3:EUR" Error in snp_reverse_base(queried_effect_allele) : 不是所有的x %in% c("A", "T", "C", "G")都是TRUE

actrionector commented 3 months ago

我也遇到了这个问题,并困惑如何解决 [1] “seaching for No.60 SNP” res_pop[res_pop$population == pop, ] 中的错误 : 量度数目不对 [1] “没有人口信息 1000GENOMES:phase_3:EUR” 中的错误 snp_reverse_base(queried_effect_allele) : 不是所有的 x %in% c(“A”, “T”, “C”, “G”)都是TRUE

我也是哇,不过删除报错的那一行就可以了:dat <- dat[-60, ]