# Merge MTs different order of alleles and locus

**URL:** <https://discuss.hail.is/t/merge-mts-different-order-of-alleles-and-locus/1532>\
**Category:** Hail Query & hailctl\
**Created:** [July 15, 2020, 12:00pm UTC](https://discuss.hail.is/t/merge-mts-different-order-of-alleles-and-locus/1532 "2020-07-15T12:00:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![danchubb](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/danchubb/32/484_2.png) [@danchubb](https://discuss.hail.is/u/danchubb)\
**Post date:** [July 15, 2020, 12:00pm UTC](https://discuss.hail.is/t/merge-mts-different-order-of-alleles-and-locus/1532/1 "2020-07-15T12:00:58Z")

</div>

Hi,

I’m trying to merge 1KG and my mt to perform PCA. After merging I have this error:

ValueError: ‘MatrixTable.union\_rows’ expects row types for all datasets to be the same. Found:  
dataset 0: struct{alleles: array, locus: locus}  
dataset 1: struct{locus: locus, alleles: array}

so it looks like the order in the 1kg is allele locus and in mine it is locus allele. Not sure why this is the case but cat anyone suggest a fix on how to swap one over to match? The syntax escapes me.

Thanks a lot

Dan

---

<div class="post-metadata">

**Author:** ![tpoterba](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/tpoterba/32/61_2.png) [@tpoterba](https://discuss.hail.is/u/tpoterba)\
**Post date:** [July 15, 2020, 12:04pm UTC](https://discuss.hail.is/t/merge-mts-different-order-of-alleles-and-locus/1532/2 "2020-07-15T12:04:14Z")

</div>

for dataset0, do `mt = mt.key_rows_by('locus', 'alleles')`

---

<div class="post-metadata">

**Author:** ![danchubb](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/danchubb/32/484_2.png) [@danchubb](https://discuss.hail.is/u/danchubb)\
**Post date:** [July 15, 2020, 1:07pm UTC](https://discuss.hail.is/t/merge-mts-different-order-of-alleles-and-locus/1532/3 "2020-07-15T13:07:48Z")

</div>

Thanks a lot for the advice Tim,

It doesn’t seem to work

I have tried:

thousand\_mt\_prune=thousand\_mt\_prune.key\_rows\_by(‘locus’,‘alleles’)  
mt\_prune=mt\_prune.key\_rows\_by(‘locus’,‘alleles’)

thousand\_mt\_prune.rows().show()  
mt\_prune.rows().show()

dataset\_result = thousand\_mt\_prune.union\_rows(mt\_prune)

and I get:

```
alleles | locus
array<str> | locus<GRCh38>

locus| alleles
locus<GRCh38> | array<str>

```

ValueError: ‘MatrixTable.union\_rows’ expects row types for all datasets to be the same. Found:  
dataset 0: struct{alleles: array, locus: locus}  
dataset 1: struct{locus: locus, alleles: array}

---

<div class="post-metadata">

**Author:** ![tpoterba](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/tpoterba/32/61_2.png) [@tpoterba](https://discuss.hail.is/u/tpoterba)\
**Post date:** [July 15, 2020, 3:41pm UTC](https://discuss.hail.is/t/merge-mts-different-order-of-alleles-and-locus/1532/4 "2020-07-15T15:41:01Z")

</div>

Oh! The key type is the same, the order is different. This is a little annoying to fix. We have a to-do item to add type unification like Table.union has, but for now, you should be able to do the following:

```python
mt = mt.rename({'locus': 'locus2', 'allelles': 'alleles2'})
mt = mt.select_rows(locus=mt.locus2, alleles=mt.alleles2)
mt = mt.key_rows_by('locus', 'alleles')

```

Hacky.

---

<div class="post-metadata">

**Author:** ![danchubb](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/danchubb/32/484_2.png) [@danchubb](https://discuss.hail.is/u/danchubb)\
**Post date:** [July 16, 2020, 4:15pm UTC](https://discuss.hail.is/t/merge-mts-different-order-of-alleles-and-locus/1532/5 "2020-07-16T16:15:23Z")

</div>

Hi Tim,

That’s fantastic, hacky but it works fine 🙂

Thanks a lot

Dan
