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?
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?
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']
Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
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!