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

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

## Setup

See the setup process for the PLINK2 executable and reference file in the [Clumping](https://nicksunderland.github.io/genepi.utils/articles/clumping.html) vignette.


```{r setup}
library(genepi.utils)

# the gwas data
gwas <- GWAS(dat=system.file("extdata", "example2_gwas_sumstats.tsv", package="genepi.utils"), map="ns_map", fill_rsid="b37_dbsnp156")
gwas <- as.data.table(gwas)

# check path to reference
which_1000G_reference("GRCh37")

# check path to plink2 executable
which_plink2()
```

## LD matrix

Generate the LD matrix. If we provide a GWAS data.table with the alleles then it will be harmonised with the LD matrix. 

```{r ld_matrix}
# produce LD matrix 
obj <- ld_matrix(gwas,
                 method    = "r2",
                 plink2    = genepi.utils::which_plink2(),
                 plink_ref = genepi.utils::which_1000G_reference(build="GRCh37"))

```


## Harmonise data against LD matrix alleles

The previous command has harmonised the `gwas` input data.table to the LD alleles. The return value is a list with elements `dat` 
and `ld_mat`.

```{r ld_matrix_info2}
# harmonised data 
print(obj['dat'])

# the LD matrix
print(obj['ld_mat'])
```
