how can I save the distance matrix in csv file?
1
0
Entering edit mode
23 months ago
KHiLLCHiNG ▴ 10

I use Bio.Phylo.TreeConstruction DistanceCalculator to calculate a distance matrix. But I want to save the matrix in a csv or excel file. Not just print. Does anyone could tell how to do?

matrix biopython distancematrix • 913 views
ADD COMMENT
4
Entering edit mode
23 months ago
Joe 21k

If you take a look at the .matrix attribute of the DistanceCalculator instance you created, the matrix is already present as a list-of-lists.

From the docs:

from Bio.Phylo.TreeConstruction import DistanceCalculator
from Bio import AlignIO
aln = AlignIO.read(open('TreeConstruction/msa.phy'), 'phylip')

calculator = DistanceCalculator('identity')
dm = calculator.get_distance(aln)

If you now interrogate dm.matrix you'll find the list-of-lists of scores. This is pretty easy to write out as a csv, however it doesn't include the names of the samples as in the print-ed display. For this you can use dm.names which will be in the appropriate top-to-bottom and left-to-right order.

e.g. when I use some sample data:

dm.matrix
[[0], [0.14084507042253525, 0], [0.43661971830985913, 0.43661971830985913, 0]]
dm.names
['sp|P69905|HBA_HUMAN', 'sp|P01942|HBA_MOUSE', 'sp|P13786|HBAZ_CAPHI']
ADD COMMENT
1
Entering edit mode

Thank you, Joe! I really missedthe .matrix attribute. And you reminded me of it and I transfered the matrix into a dataframe and then write out as a csv! Thanks again!

ADD REPLY

Login before adding your answer.

Traffic: 2549 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6