---
title: "Generate LD matrices"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Generate LD matrices}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  eval=FALSE
)
```


```{r setup}
library(gwasglue)
library(data.table)
```

For each region generate an LD matrix

```{r}
pop <- "EUR"
ldref <- paste0("/Users/gh13047/repo/mr-base-api/app/ld_files/", pop)
bim <- data.table::fread(paste0(ldref, ".bim"))
regionfile <- system.file("extdata", "ldetect", paste0(pop, ".bed"), package="gwasglue")
regions <- data.table::fread(regionfile, header=TRUE) %>%
	dplyr::mutate(
		chr=as.numeric(gsub("chr", "", chr)),
		start=as.numeric(start),
		stop=as.numeric(stop)
	) %>% dplyr::as_tibble()

for(i in 1:nrow(regions))
{
	message(i)
	x <- subset(bim, V1 == regions$chr[i] & V4 >= regions$start[i] & V4 <= regions$stop[i])$V2
	y <- ieugwasr::ld_matrix(x, pop=pop, bfile=ldref, plink_bin=genetics.binaRies::get_plink_binary(), with_alleles=FALSE)
	save(y, file="temp.rdata")
}
```

```sh
bin/emeraLD -i /mnt/storage/private/mrcieu/research/mr-eve/vcf-reference-datasets/1000g/ALL.chr6.phase3_shapeit2_mvncall_integrated_v5a.20130502.genotypes.vcf.gz --region 6:2458936-3573593 --out ~/mr-eve/temp.ld --no-phase

bin/emeraLD -i /mnt/storage/private/mrcieu/research/mr-eve/vcf-reference-datasets/1000g/1kg_v3_nomult.vcf.gz --region 1:30923-100923 --out ~/mr-eve/temp.ld --no-phase


bin/emeraLD -i /mnt/storage/private/mrcieu/research/mr-eve/vcf-reference-datasets/1000g/temp.vcf.gz --region 6:2458936-3573593 --out ~/mr-eve/temp.ld --no-phase
```

```{r}
betas <- hyprcoloc::test.betas
ses <- hyprcoloc::test.ses
trait.cor <- hyprcoloc::test.corr
ld.matrix <- hyprcoloc::test.ld
traits <- paste0("T", 1:dim(betas)[2])
rsid <- rownames(betas)
res <- hyprcoloc(betas, ses, trait.names=traits, snp.id=rsid)
res
```
