-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathannotation.py
More file actions
33 lines (25 loc) · 899 Bytes
/
Copy pathannotation.py
File metadata and controls
33 lines (25 loc) · 899 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import seaborn as sns
import pandas as pd
def prepare_annotation(df, annotation):
if annotation is None:
return df, None
common = df.columns.intersection(annotation.index)
df = df[common]
annotation = annotation.loc[common]
return df, annotation
def build_annotation_colors(annotation_df):
"""
annotation_df: index=Sample, columns=annotation
return:
col_colors: DataFrame (Sample x annotation)
lut_dict: dict for legend
"""
col_colors = pd.DataFrame(index=annotation_df.index)
lut_dict = {}
for col in annotation_df.columns:
categories = annotation_df[col].astype(str).unique()
palette = sns.color_palette("tab10", len(categories))
lut = dict(zip(categories, palette))
lut_dict[col] = lut
col_colors[col] = annotation_df[col].astype(str).map(lut)
return col_colors, lut_dict