Skip to content

Instantly share code, notes, and snippets.

@fredyr
Created April 27, 2021 09:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fredyr/5bff0d4a3fdc7fc41512791351b2da51 to your computer and use it in GitHub Desktop.
Save fredyr/5bff0d4a3fdc7fc41512791351b2da51 to your computer and use it in GitHub Desktop.
import sys
from io import StringIO
import pandas
from Bio.Phylo.TreeConstruction import DistanceMatrix
def distance_matrix(snp_tab):
df = pandas.read_csv(snp_tab, sep='\t', header=0)
cols = list(df.columns)[1:]
matrix = [[int(f) for f in df[c].values][: i + 1] for i, c in enumerate(cols)]
distmat = DistanceMatrix(cols, matrix=matrix)
phylip = StringIO()
distmat.format_phylip(phylip)
print(phylip.getvalue())
distance_matrix(sys.stdin)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment