# Reading multiple matrix tables

**URL:** <https://discuss.hail.is/t/reading-multiple-matrix-tables/1212>\
**Category:** Hail Query & hailctl\
**Created:** [December 11, 2019, 11:33am UTC](https://discuss.hail.is/t/reading-multiple-matrix-tables/1212 "2019-12-11T11:33:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jjfarrell](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/jjfarrell/32/108_2.png) [@jjfarrell](https://discuss.hail.is/u/jjfarrell)\
**Post date:** [December 11, 2019, 11:33am UTC](https://discuss.hail.is/t/reading-multiple-matrix-tables/1212/1 "2019-12-11T11:33:47Z")

</div>

I would like a hail script that reads in the individual chromosomal matrix tables into one larger matrix table for ld pruning and running pc\_relate. I tried using the wild card chr\* but get an error. Is there a way to read in multiple matrix tables.

mt\_fn=’/project/adgc/imp.topmed\_adsp5k/mt/adgc.aa.imp30r2.topmed\_adsp5k.chr\*.mt’  
mt=hl.read\_matrix\_table(mt\_fn)

Hail version: 0.2.19-c6ec8b76eb26  
Error summary: HailException: MatrixTable and Table files are directories; path ‘/project/adgc/imp.topmed\_adsp5k/mt/adgc.aa.imp30r2.topmed\_adsp5k.chr\*.mt’ is not a directory

---

<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:** [December 11, 2019, 12:37pm UTC](https://discuss.hail.is/t/reading-multiple-matrix-tables/1212/2 "2019-12-11T12:37:45Z")

</div>

this is intentional; a MatrixTable is already a composite object, so it shouldn’t be a common use case to glob them.

You can just iterate in Python. First, let’s define a helper function that makes a nested union N log N, not quadratic (see [here](https://discuss.hail.is/t/performance-of-writing-matrixtable-on-0-2/663/9) for more info):

```python
def union_cols_all(mts):
    mts = mts[:]

    iteration = 0
    while (len(mts) > 1):
        iteration += 1
        print(f'iteration {iteration}')
        tmp = []
        for i in range(0, len(mts), 2):
            tmp.append(mts[i].union_cols(mts[i+1]))
        mts = tmp[:]
    return mts[0]

```

And then read and union in Python:

```python
files = [f'/path/to/chr{chrom}' for chrom in list(range(23)) + ['X', 'Y']]
mt = union_cols_all([hl.read_matrix_table(file) for file in files])

```
