# How to write a MatrixTable to a file as a tab separated table in wide format?

**URL:** <https://discuss.hail.is/t/how-to-write-a-matrixtable-to-a-file-as-a-tab-separated-table-in-wide-format/1338>\
**Category:** Hail Query & hailctl\
**Created:** [March 17, 2020, 11:06am UTC](https://discuss.hail.is/t/how-to-write-a-matrixtable-to-a-file-as-a-tab-separated-table-in-wide-format/1338 "2020-03-17T11:06:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![leocob](https://avatars.discourse-cdn.com/v4/letter/l/5daacb/32.png) [@leocob](https://discuss.hail.is/u/leocob)\
**Post date:** [March 17, 2020, 11:06am UTC](https://discuss.hail.is/t/how-to-write-a-matrixtable-to-a-file-as-a-tab-separated-table-in-wide-format/1338/1 "2020-03-17T11:06:36Z")

</div>

Hello Hail team!

I’ve recently started using Hail 0.2 but I got stuck with the following problem.

Is there a way to write a Hail table to a tab separated file in **wide** format?

More in details: I have a MatrixTable with this structure:

```
mt.describe()
----------------------------------------
Global fields:
    None
----------------------------------------
Column fields:
    's': str
----------------------------------------
Row fields:
    'gene_id': str
----------------------------------------
Entry fields:
    'number': int64
----------------------------------------
Column key: ['s']
Row key: ['gene_id']
----------------------------------------

```

`mt.show()`

| gene\_id | Sample1.number | Sample2.number | Sample3.number | Sample4.number |
| --- | --- | --- | --- | --- |
| str | int64 | int64 | int64 | int64 |
| “ENSG00000000457” | 9 | 5 | 5 | 5 |
| “ENSG00000000460” | 23 | 13 | 11 | 11 |
| “ENSG00000000971” | 0 | 4 | 4 | 4 |
| “ENSG00000001036” | 3 | 3 | 3 | 3 |

I would like to write the MatrixTable to a file table, keeping the same wide format that we can see from the mt.show().

The problem is that I have around 15k genes and around 500k samples.

I tried with the command [make\_table()](https://hail.is/docs/0.2/hail.MatrixTable.html?highlight=make_table#hail.MatrixTable.make_table) but after 24 hours running (on a cluster node with 28 processors) it was still only at half the process.

I tried using [ht = mt.entries()](https://hail.is/docs/0.2/hail.MatrixTable.html?highlight=entries#hail.MatrixTable.entries) and then [ht.export(“myfile.tsv.bgz”)](https://hail.is/docs/0.2/hail.Table.html?highlight=table%20export#hail.Table.export) but, after unzipping the bgz file, I got a file of 170 gigabytes, containing the table I wanted but in **long format** , of which an example is here:

```
gene_id s number
ENSG00000000419 Sample1 5
ENSG00000000419 Sample2 8
ENSG00000000419 Sample3 0
ENSG00000000419 Sample4 3
ENSG00000000419 Sample5 23
ENSG00000000419 Sample6 14

```

If the file was smaller I could easily pivot the table using pandas or tidyverse. But I’m afraid that 170 gigabytes is a bit too much. I could try using Spark or other strategies for big data but I keep telling myself there should be a way to do this natively with Hail!

Can anyone help me? Thank you! 🙂

---

<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:** [March 17, 2020, 11:58am UTC](https://discuss.hail.is/t/how-to-write-a-matrixtable-to-a-file-as-a-tab-separated-table-in-wide-format/1338/2 "2020-03-17T11:58:08Z")

</div>

I think this should work:

```python
mt.number.export('myfile.tsv.bgz')

```

This is a bit hard to find in the docs, sorry.

---

<div class="post-metadata">

**Author:** ![leocob](https://avatars.discourse-cdn.com/v4/letter/l/5daacb/32.png) [@leocob](https://discuss.hail.is/u/leocob)\
**Post date:** [March 17, 2020, 9:32pm UTC](https://discuss.hail.is/t/how-to-write-a-matrixtable-to-a-file-as-a-tab-separated-table-in-wide-format/1338/3 "2020-03-17T21:32:36Z")

</div>

Thank you so much! It works!  
If I had known, I would have asked my question before. I spent more than one week trying to solve this problem! 😛  
Next time I’ll know I can count on the fast support of the Hail team 🙂

---

<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:** [March 17, 2020, 9:34pm UTC](https://discuss.hail.is/t/how-to-write-a-matrixtable-to-a-file-as-a-tab-separated-table-in-wide-format/1338/4 "2020-03-17T21:34:19Z")

</div>

The more questions we get, the more information we have about how we can improve our docs! I promise we’ll actually start using that information to improve them soon…
