# Ld\_matrix for specific variant/locus and radius

**URL:** <https://discuss.hail.is/t/ld-matrix-for-specific-variant-locus-and-radius/846>\
**Category:** Feature Requests\
**Created:** [January 31, 2019, 4:38pm UTC](https://discuss.hail.is/t/ld-matrix-for-specific-variant-locus-and-radius/846 "2019-01-31T16:38:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hhx037](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/hhx037/32/160_2.png) [@hhx037](https://discuss.hail.is/u/hhx037)\
**Post date:** [January 31, 2019, 4:38pm UTC](https://discuss.hail.is/t/ld-matrix-for-specific-variant-locus-and-radius/846/1 "2019-01-31T16:38:31Z")

</div>

Hi,

I was wondering if it would be possible to get a function like ld\_matrix that would return the LD between a single variant/locus and neighbouring variants within a specified radius, that would be very helpful.

Cheers,

Stephane

---

<div class="post-metadata">

**Author:** ![danking](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/danking/32/43_2.png) [@danking](https://discuss.hail.is/u/danking)\
**Post date:** [February 2, 2019, 5:50pm UTC](https://discuss.hail.is/t/ld-matrix-for-specific-variant-locus-and-radius/846/2 "2019-02-02T17:50:54Z")

</div>

You only care about the results for a single variant? If the radius is small, load the dataset, `hl.filter_intervals` to the relevant window, collect the data locally, then use numpy to compute `X dot X^T`.

Something like (I haven’t tested this, you probably need to mean center and variance normalize on line 4):

```python
mt = hl.read_matrix_table(...)
mt = hl.filter_intervals(mt, ... interval around variant of interest ...)
mt = mt.select_entries(n_alts = mt.gt.n_alt_alleles()).select_cols().select_rows()
mt = mt.select_entries(n_alts = ... normalize alt allele count appropriately ...)
t = mt._localize_entries('entries', 'cols')
list_of_rows = t.select(t.entries.map(lambda entry: entry.n_alts)).collect()
x = numpy.array(list_of_rows)
ld_matrix = x * x.T

```

---

<div class="post-metadata">

**Author:** ![hhx037](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/hhx037/32/160_2.png) [@hhx037](https://discuss.hail.is/u/hhx037)\
**Post date:** [February 8, 2019, 4:58pm UTC](https://discuss.hail.is/t/ld-matrix-for-specific-variant-locus-and-radius/846/3 "2019-02-08T16:58:35Z")

</div>

Hi Dan,

apologies for the late answer, and thank you for your help (though I’m not sure I understand all these steps).

Well, I’m interested in the results for lists of variants, in order to produce regional plots for each of the variants (BTW, that would be a great function to add to Hail 😁 )

---

<div class="post-metadata">

**Author:** ![danking](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/danking/32/43_2.png) [@danking](https://discuss.hail.is/u/danking)\
**Post date:** [February 11, 2019, 5:21pm UTC](https://discuss.hail.is/t/ld-matrix-for-specific-variant-locus-and-radius/846/4 "2019-02-11T17:21:52Z")

</div>

@jbloom Can provide a more detailed answer, but if you want to calculate the diagonal of the LD matrix without calculating the whole thing, you can use something like this:

```auto
ld = hl.ld_matrix(mt.GT.n_alt_alleles(), mt.locus, BASE_PAIR_RADIUS)

```

@jbloom, is there a simple way to take this and produce the list of sub-matrices defined by a list of loci and base pair radii?
