# Distance matrix between taxas in OTU table

**URL:** <https://forum.qiime2.org/t/distance-matrix-between-taxas-in-otu-table/15576>\
**Category:** User Support\
**Tags:** distance-matrix, taxonomy\
**Created:** [July 1, 2020, 3:13pm UTC](https://forum.qiime2.org/t/distance-matrix-between-taxas-in-otu-table/15576 "2020-07-01T15:13:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![misterie](https://forum.qiime2.org/letter_avatar_proxy/v4/letter/m/a88e4f/32.png) [@misterie](https://forum.qiime2.org/u/misterie)\
**Post date:** [July 1, 2020, 3:13pm UTC](https://forum.qiime2.org/t/distance-matrix-between-taxas-in-otu-table/15576/1 "2020-07-01T15:13:06Z")

</div>

Hi,

I was exportin the OTU table to the csv file. Since I want to use this file for the mixed model I wanted to use genetic distances between OTUs as my variance-covariance matrix. Is there any possibility to generate the genetic distances matrix between the OTU or any other kind of similarity/distance matrix?

---

<div class="post-metadata">

**Author:** ![jwdebelius](https://forum.qiime2.org/user_avatar/forum.qiime2.org/jwdebelius/32/9655_2.png) [@jwdebelius](https://forum.qiime2.org/u/jwdebelius)\
**Post date:** [July 1, 2020, 3:17pm UTC](https://forum.qiime2.org/t/distance-matrix-between-taxas-in-otu-table/15576/2 "2020-07-01T15:17:58Z")

</div>

Hi @misterie,

Welcome to the :qiime2: forum!

There are two ways to answer this question. The first is that, underlying the phylogenetic tree, you have a distance matrix that describes the distance between two OTUs/ASVs/features.

This isn't easily retrievable through the standard QIIME API, so you'd either need to look at the underlying data structures. In python the (untested) code would be something like

```python
from skbio import TreeNode
from qiime2 import Artifact

tree = Artifact.load('path to your qza tree').view(TreeNode)
dist = tree.tip_to_tip_distance()
dist.save('path to your new distance matrix as a tsv')

```

Best,  
Justine

---

<div class="post-metadata">

**Author:** ![misterie](https://forum.qiime2.org/letter_avatar_proxy/v4/letter/m/a88e4f/32.png) [@misterie](https://forum.qiime2.org/u/misterie)\
**Post date:** [July 1, 2020, 5:33pm UTC](https://forum.qiime2.org/t/distance-matrix-between-taxas-in-otu-table/15576/3 "2020-07-01T17:33:25Z")

</div>

Hi @jwdebelius, thank you! Should I use rooted or unrooted tree?

---

<div class="post-metadata">

**Author:** ![jwdebelius](https://forum.qiime2.org/user_avatar/forum.qiime2.org/jwdebelius/32/9655_2.png) [@jwdebelius](https://forum.qiime2.org/u/jwdebelius)\
**Post date:** [July 1, 2020, 6:51pm UTC](https://forum.qiime2.org/t/distance-matrix-between-taxas-in-otu-table/15576/4 "2020-07-01T18:51:49Z")

</div>

Hi @misterie,

As far as I know, the two trees should perserve the same relationship, but the magnitude of the distance may be different - so id suggest picking one and just being clear about which you used.

However (a thought that just hit me), this may not be your best covariance matrix. Phylognetically similar organisms may have very different behavior (despite the fact that this is often the assumption we make!) So, you may also want to consider a co-occurance ranking. I think SparCC might be a good way to go for that kind of co-occurance/co-exclusion matrix.

Best,  
Justine

---

<div class="post-metadata">

**Author:** ![misterie](https://forum.qiime2.org/letter_avatar_proxy/v4/letter/m/a88e4f/32.png) [@misterie](https://forum.qiime2.org/u/misterie)\
**Post date:** [July 2, 2020, 1:28pm UTC](https://forum.qiime2.org/t/distance-matrix-between-taxas-in-otu-table/15576/5 "2020-07-02T13:28:49Z")

</div>

@jwdebelius thank you! My another question is: can I build this matrix only for classified OTU? Now it produces very big matrix, but number of my taxas is lower after the classification with greengenes db.

---

<div class="post-metadata">

**Author:** ![jwdebelius](https://forum.qiime2.org/user_avatar/forum.qiime2.org/jwdebelius/32/9655_2.png) [@jwdebelius](https://forum.qiime2.org/u/jwdebelius)\
**Post date:** [July 2, 2020, 2:10pm UTC](https://forum.qiime2.org/t/distance-matrix-between-taxas-in-otu-table/15576/6 "2020-07-02T14:10:39Z")

</div>

Hi @misterie,

You can eithe sheer the tree before hand (you'll need to look at the documentation to do so), or you can filter the tree to just your features using the "filter" property of the distance matrix:

```python
import pandas as pd

table = Artifact.load('path to your table').view(pd.DataFrame)
feature_ids = table.index.values

only_otu_dist = dist.filter(feature_ids)

```

Best,  
Justine

---

<div class="post-metadata">

**Author:** ![system](https://forum-qiime2-org.s3.dualstack.us-west-2.amazonaws.com/original/3X/2/1/21af5fe23cb6f4579467c66a9ed94e55274ca7bd.svg) [@system](https://forum.qiime2.org/u/system)\
**Post date:** [August 2, 2020, 8:10pm UTC](https://forum.qiime2.org/t/distance-matrix-between-taxas-in-otu-table/15576/7 "2020-08-02T20:10:41Z")

</div>

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.
