MRCIEU / ieugwasr

R interface to the IEU GWAS database API
https://mrcieu.github.io/ieugwasr/
Other
69 stars 23 forks source link

ieugwasr::ld_matrix cannot extract LD matrix #24

Open jajoko opened 2 years ago

jajoko commented 2 years ago

I would wish to extract LD matrix using ieugwasr::ld_matrix. However, I am unable to do so, because ld_matrix does not write a .bim file to my tmp folder. A reproducible example as follows:

variants <- c("rs12167289", "rs186528764", "rs12168819", "rs547447196", "rs117281477", "rs62230011", "rs6519012",   "rs71314992",  "rs555424086", "rs140000", "rs8140240", "rs62230012", "rs73169675")

extracted_matrix <- ld_matrix(
  variants = variants,
  plink_bin = genetics.binaRies::get_plink_binary(),
  bfile = 'path/to/LD/reference/local/dataset/EUR')

Output:

Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
  cannot open file 'C:\Users\jajoko\AppData\Local\Temp\RtmpW0YWmm\file2c90686ec4.bim': No such file or directory

traceback() gives

4: file(file, "rt")
3: read.table(paste0(fn, ".bim"), stringsAsFactors = FALSE)
2: ld_matrix_local(variants, bfile = bfile, plink_bin = plink_bin, 
       with_alleles = with_alleles)
1: ld_matrix(variants = variants, plink_bin = genetics.binaRies::get_plink_binary(), 
       bfile = "path/to/LD/reference/local/dataset/EUR")

To ensure that I got the path to bfile right, I used setwd() to set the path to "path/to/LD/reference/local/dataset/", and list.files() gives

 [1] "AFR.bed" "AFR.bim" "AFR.fam" "AMR.bed" "AMR.bim" "AMR.fam" "EAS.bed" "EAS.bim" "EAS.fam" "EUR.bed" "EUR.bim" "EUR.fam" "SAS.bed" "SAS.bim" "SAS.fam"

The Temp-folder indeed does not have the stated .bim-file. Instead, it has a file with the same name but without the .bim extension that includes the names of the variants. When looking at the ld_matrix_local(), which is used by ld_matrix() when plink references are given locally, the variants seem to pass the first part where tmp file without a file extension is made

plink_bin = genetics.binaRies::get_plink_binary()
bfile = "path/to/LD/reference/local/dataset/EUR"

ld_matrix_local <- function(variants, bfile, plink_bin, with_alleles=TRUE)
{

    # Make textfile
  shell <- ifelse(Sys.info()['sysname'] == "Windows", "cmd", "sh")
  fn <- tempfile()
  write.table(data.frame(variants), file=fn, row.names=F, col.names=F, quote=F)

but seems to fail the second part where plink is called to make a .bim file

    shQuote(plink_bin, type=shell),
    " --bfile ", shQuote(bfile, type=shell),
    " --extract ", shQuote(fn, type=shell), 
    " --make-just-bim ", 
    " --keep-allele-order ",
    " --out ", shQuote(fn, type=shell)
  )
  system(fun1)

And ultimately fails when trying to read the .bim file

bim <- read.table(paste0(fn, ".bim"), stringsAsFactors=FALSE)

The same seems to happen with the second part of the ld_matrix_local() that makes a .ld file from the same file


  fun2 <- paste0(
    shQuote(plink_bin, type=shell),
    " --bfile ", shQuote(bfile, type=shell),
    " --extract ", shQuote(fn, type=shell), 
    " --r square ", 
    " --keep-allele-order ",
    " --out ", shQuote(fn, type=shell)
  )
  system(fun2)
  res <- read.table(paste0(fn, ".ld"), header=FALSE) %>% as.matrix
  if(with_alleles)
  {
    rownames(res) <- colnames(res) <- paste(bim$V2, bim$V5, bim$V6, sep="_")
  } else {
    rownames(res) <- colnames(res) <- bim$V2
  }
  return(res)
}

What am I doing wrong? I succesfully reinstalled genetics.binaRies using devtools::install_github() just in case, because initially I got some error messages when installing it using remotes::install_github("MRCIEU/genetics.binaRies"). Is this a problem with ieugwasr or with genetics.binaries?

sessionInfo()

R version 4.1.3 (2022-03-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 14393)

Matrix products: default

locale:
[1] LC_COLLATE=Finnish_Finland.1252  LC_CTYPE=Finnish_Finland.1252    LC_MONETARY=Finnish_Finland.1252 LC_NUMERIC=C                     LC_TIME=Finnish_Finland.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] forcats_0.5.1   stringr_1.4.0   dplyr_1.0.8     purrr_0.3.4     readr_2.1.2     tidyr_1.2.0     tibble_3.1.6    ggplot2_3.3.6   tidyverse_1.3.1 ieugwasr_0.1.5  workflowr_1.7.0

loaded via a namespace (and not attached):
 [1] httr_1.4.2                   pkgload_1.2.4                jsonlite_1.8.0               modelr_0.1.8                 brio_1.1.3                   assertthat_0.2.1            
 [7] getPass_0.2-2                cellranger_1.1.0             yaml_2.3.5                   remotes_2.4.2                sessioninfo_1.2.2            pillar_1.7.0                
[13] backports_1.4.1              glue_1.6.2                   digest_0.6.29                promises_1.2.0.1             rvest_1.0.2                  colorspace_2.0-3            
[19] htmltools_0.5.2              httpuv_1.6.5                 pkgconfig_2.0.3              devtools_2.4.3               broom_0.7.12                 haven_2.4.3                 
[25] scales_1.2.0                 processx_3.5.2               whisker_0.4                  later_1.3.0                  tzdb_0.2.0                   git2r_0.30.1                
[31] generics_0.1.2               usethis_2.1.5                ellipsis_0.3.2               cachem_1.0.6                 pacman_0.5.1                 withr_2.5.0                 
[37] cli_3.1.0                    readxl_1.3.1                 magrittr_2.0.2               crayon_1.5.0                 memoise_2.0.1                evaluate_0.15               
[43] ps_1.6.0                     fs_1.5.2                     fansi_1.0.3                  xml2_1.3.3                   pkgbuild_1.3.1               tools_4.1.3                 
[49] prettyunits_1.1.1            hms_1.1.1                    lifecycle_1.0.1              reprex_2.0.1                 munsell_0.5.0                callr_3.7.0                 
[55] compiler_4.1.3               rlang_1.0.2                  grid_4.1.3                   rstudioapi_0.13              rmarkdown_2.14               testthat_3.1.2              
[61] gtable_0.3.0                 DBI_1.1.2                    curl_4.3.2                   R6_2.5.1                     lubridate_1.8.0              knitr_1.39                  
[67] fastmap_1.1.0                utf8_1.2.2                   rprojroot_2.0.2              desc_1.4.1                   stringi_1.7.6                genetics.binaRies_0.0.0.9000
[73] Rcpp_1.0.8.3                 vctrs_0.4.1                  dbplyr_2.1.1                 tidyselect_1.1.2             xfun_0.30                    
jajoko commented 1 year ago

Sorry, this problem resolved when I reinstalled the R & Rstudio, and reinstalled all packages. Should've tried that first.

Jianghong-Lu commented 1 year ago

Sorry, this problem resolved when I reinstalled the R & Rstudio, and reinstalled all packages. Should've tried that first.

Could you show your sessionInfo() after you reinstalled R for me? I am trapped with a similar problem.

jajoko commented 1 year ago

Sorry! I forgot to post this:

R version 4.2.1 (2022-06-23 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 14393)

Matrix products: default

locale:
[1] LC_COLLATE=Finnish_Finland.1252  LC_CTYPE=Finnish_Finland.1252    LC_MONETARY=Finnish_Finland.1252 LC_NUMERIC=C                    
[5] LC_TIME=Finnish_Finland.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] workflowr_1.7.0

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.8.3                 rstudioapi_0.13              knitr_1.39                   whisker_0.4                  magrittr_2.0.3              
 [6] genetics.binaRies_0.0.0.9000 getPass_0.2-2                R6_2.5.1                     rlang_1.0.3                  fastmap_1.1.0               
[11] stringr_1.4.0                httr_1.4.3                   tools_4.2.1                  xfun_0.31                    cli_3.3.0                   
[16] git2r_0.30.1                 htmltools_0.5.2              yaml_2.3.5                   digest_0.6.29                rprojroot_2.0.3             
[21] processx_3.6.1               ieugwasr_0.1.5               callr_3.7.1                  later_1.3.0                  promises_1.2.0.1            
[26] fs_1.5.2                     ps_1.7.1                     glue_1.6.2                   evaluate_0.15                rmarkdown_2.14              
[31] stringi_1.7.6                compiler_4.2.1               httpuv_1.6.5  
Jianghong-Lu commented 1 year ago

Sorry! I forgot to post this:

R version 4.2.1 (2022-06-23 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 14393)

Matrix products: default

locale:
[1] LC_COLLATE=Finnish_Finland.1252  LC_CTYPE=Finnish_Finland.1252    LC_MONETARY=Finnish_Finland.1252 LC_NUMERIC=C                    
[5] LC_TIME=Finnish_Finland.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] workflowr_1.7.0

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.8.3                 rstudioapi_0.13              knitr_1.39                   whisker_0.4                  magrittr_2.0.3              
 [6] genetics.binaRies_0.0.0.9000 getPass_0.2-2                R6_2.5.1                     rlang_1.0.3                  fastmap_1.1.0               
[11] stringr_1.4.0                httr_1.4.3                   tools_4.2.1                  xfun_0.31                    cli_3.3.0                   
[16] git2r_0.30.1                 htmltools_0.5.2              yaml_2.3.5                   digest_0.6.29                rprojroot_2.0.3             
[21] processx_3.6.1               ieugwasr_0.1.5               callr_3.7.1                  later_1.3.0                  promises_1.2.0.1            
[26] fs_1.5.2                     ps_1.7.1                     glue_1.6.2                   evaluate_0.15                rmarkdown_2.14              
[31] stringi_1.7.6                compiler_4.2.1               httpuv_1.6.5  

Thanks!I think I found the real reason. The Plink.exe file is broken. 手动下载那个plink.exe,然后放进指定路径就行了