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!
how can I save the distance matrix in csv file?
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?
• 2,193 views
•
link
1 answer
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']
• 0 views
•
link
• 0 views
•
link
Log in to answer this question.