Skip to content

Instantly share code, notes, and snippets.

@Ken-Kuroki
Last active October 4, 2019 12:46
Show Gist options
  • Save Ken-Kuroki/81ffc5cd92bde0c3cf6931bf847faa6b to your computer and use it in GitHub Desktop.
Save Ken-Kuroki/81ffc5cd92bde0c3cf6931bf847faa6b to your computer and use it in GitHub Desktop.
Generate iTOL "simple bar" annotation file from pandas dataframe
import numpy as np
import pandas as pd
from ete3 import PhyloTree
def generate_simplebar(df: pd.DataFrame, save_file: str) -> None:
# df columns must be labels and values.
output = """DATASET_SIMPLEBAR\n
SEPARATOR COMMA\n
DATASET_LABEL,label_simplebar\n
COLOR,#ff0000\n
DATA\n"""+df.to_csv(header=False, index=False)
with open(save_file, "w") as f:
f.write(output)
tree = PhyloTree("tree.nwk")
leaf_labels = [leaf.name for leaf in tree.get_leaves()]
values = np.random.randint(0, 100, len(leaf_labels)).tolist()
generate_simplebar(pd.DataFrame([leaf_labels, values]).T, "bar.txt")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment