# How to check matrix length

**URL:** <https://discuss.hail.is/t/how-to-check-matrix-length/2530>\
**Category:** Hail Query & hailctl\
**Created:** [February 25, 2022, 10:46am UTC](https://discuss.hail.is/t/how-to-check-matrix-length/2530 "2022-02-25T10:46:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![annalisasnow](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/annalisasnow/32/701_2.png) [@annalisasnow](https://discuss.hail.is/u/annalisasnow)\
**Post date:** [February 25, 2022, 10:47am UTC](https://discuss.hail.is/t/how-to-check-matrix-length/2530/1 "2022-02-25T10:47:00Z")

</div>

Good day!

I am currently making requests to mtxes, these are raid reqs:

```auto
        mtx.filter_rows(mtx.rsid == "rs28680688").rsid.show()

```

As far as I get it, that’s a tiny mtx, which contains data with my rsid. However, some of those may be empty. Is there a built-in function to check whether the mtx is empty? Also, can I export those data to tsv or some other file?

---

<div class="post-metadata">

**Author:** ![johnc1231](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/johnc1231/32/286_2.png) [@johnc1231](https://discuss.hail.is/u/johnc1231)\
**Post date:** [February 25, 2022, 10:24pm UTC](https://discuss.hail.is/t/how-to-check-matrix-length/2530/2 "2022-02-25T22:24:13Z")

</div>

If you want to check how many rows after a filter, you can do:

```auto
fmtx.filter_rows(mtx.rsid == "rs28680688").count_rows()

```

If there’s 0, then it’s empty.

What exactly do you want to export to a TSV? Just the row data, the rows and the entries, something else?

---

<div class="post-metadata">

**Author:** ![annalisasnow](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/annalisasnow/32/701_2.png) [@annalisasnow](https://discuss.hail.is/u/annalisasnow)\
**Post date:** [February 26, 2022, 11:50am UTC](https://discuss.hail.is/t/how-to-check-matrix-length/2530/3 "2022-02-26T11:50:40Z")

</div>

I guess that’s the row data. I wish I could sen to tsv the results of the query:

```auto
hl.export_vcf(mtx.filter_rows(mtx.rsid == searchIDorLOC[0]),
                                              vcfPathRSID)

```

That’s a tiny mtx, I currently reexport the one as a vcf. And then analyse with a different script. But I wish I could export the results (including rsid) to a tsv:

```auto
+---------------+------------+------------+------------+------------+
| locus | alleles | 'HG003'.GT | 'HG004'.GT | 'HG002'.GT |
+---------------+------------+------------+------------+------------+
| locus<GRCh37> | array<str> | call | call | call |
+---------------+------------+------------+------------+------------+
| 1:736523 | ["T","C"] | 1/1 | 1/1 | 1/1 |

```

Like this table (though extended a bit).

---

<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 27, 2022, 3:36pm UTC](https://discuss.hail.is/t/how-to-check-matrix-length/2530/4 "2022-02-27T15:36:46Z")

</div>

Hey @annalisasnow !

This is not well documented but you can export matrix table information to a TSV. This is documented under the documentation for [`Expression.export`](https://hail.is/docs/0.2/hail.expr.Expression.html#hail.expr.Expression.export).

```auto
In [4]: import hail as hl 
   ...: mt = hl.balding_nichols_model(3, 10, 100) 
   ...: mt.GT.export('/tmp/foo.tsv')                                                                                                                                                                                                                                            
2022-02-27 10:34:39 Hail: INFO: balding_nichols_model: generating genotypes for 3 populations, 10 samples, and 100 variants...
2022-02-27 10:34:40 Hail: INFO: Coerced sorted dataset
2022-02-27 10:34:40 Hail: INFO: merging 9 files totalling 5.4K...
2022-02-27 10:34:40 Hail: INFO: while writing:
    /tmp/foo.tsv
  merge time: 35.445ms

In [5]: !head /tmp/foo.tsv                                                                                                                                                                                                                                                      
locus	alleles	0	1	2	3	4	5	6	7	8	9
1:1	["A","C"]	0/0	0/0	0/1	1/1	0/1	0/0	0/1	0/1	0/1	0/1
1:2	["A","C"]	1/1	1/1	1/1	0/1	0/1	1/1	0/1	0/1	1/1	0/1
1:3	["A","C"]	1/1	0/0	0/1	1/1	1/1	0/0	1/1	0/0	1/1	1/1
1:4	["A","C"]	0/1	0/0	0/1	1/1	0/0	0/1	0/1	0/1	0/1	0/0
1:5	["A","C"]	1/1	0/0	0/0	0/0	0/0	0/0	0/1	1/1	0/0	0/1
1:6	["A","C"]	1/1	0/1	1/1	0/1	0/0	0/1	0/1	0/0	0/0	0/0
1:7	["A","C"]	0/1	0/0	0/0	0/1	0/1	1/1	0/1	0/1	0/0	0/0
1:8	["A","C"]	0/0	1/1	0/1	1/1	0/1	1/1	0/1	0/1	1/1	1/1
1:9	["A","C"]	1/1	1/1	1/1	1/1	1/1	1/1	1/1	1/1	1/1	1/1

```

If you would like to include the RSID in the output, add the RSID to the row key:

```auto
In [7]: import hail as hl 
   ...: mt = hl.balding_nichols_model(3, 10, 100) 
   ...: mt = mt.annotate_rows(rsid = 'abcdef123') 
   ...: mt = mt.key_rows_by(*mt.row_key, 'rsid') 
   ...: mt.GT.export('/tmp/foo.tsv')                                                                                                                                                                                                                                            
2022-02-27 10:36:21 Hail: INFO: balding_nichols_model: generating genotypes for 3 populations, 10 samples, and 100 variants...
2022-02-27 10:36:22 Hail: INFO: Coerced sorted dataset
2022-02-27 10:36:22 Hail: INFO: Coerced sorted dataset
2022-02-27 10:36:22 Hail: INFO: merging 9 files totalling 6.4K...
2022-02-27 10:36:22 Hail: INFO: while writing:
    /tmp/foo.tsv
  merge time: 30.581ms

In [8]: !head /tmp/foo.tsv                                                                                                                                                                                                                                                      
locus	alleles	rsid	0	1	2	3	4	5	6	7	8	9
1:1	["A","C"]	abcdef123	0/1	0/1	0/1	1/1	1/1	0/0	0/1	0/1	0/1	1/1
1:2	["A","C"]	abcdef123	0/1	0/1	0/1	0/0	0/1	0/1	1/1	1/1	0/1	1/1
1:3	["A","C"]	abcdef123	0/0	0/0	0/0	0/1	0/1	0/1	0/0	0/1	0/1	0/0
1:4	["A","C"]	abcdef123	0/1	1/1	0/1	0/0	0/1	0/1	0/1	0/0	0/0	0/0
1:5	["A","C"]	abcdef123	1/1	0/0	1/1	0/0	0/1	1/1	0/1	0/1	0/1	0/1
1:6	["A","C"]	abcdef123	0/1	1/1	1/1	0/1	1/1	1/1	0/1	0/1	0/0	1/1
1:7	["A","C"]	abcdef123	0/0	0/1	0/0	0/1	0/0	0/0	0/0	0/0	0/0	0/0
1:8	["A","C"]	abcdef123	1/1	1/1	1/1	1/1	1/1	1/1	1/1	0/1	0/1	1/1
1:9	["A","C"]	abcdef123	1/1	0/1	1/1	0/1	1/1	1/1	1/1	1/1	0/1	1/1

```
