-
Notifications
You must be signed in to change notification settings - Fork 0
/
two_level.py
143 lines (119 loc) · 4.01 KB
/
two_level.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/Bio/User/renchaobo/software/miniconda3/bin/python
# -*- coding: utf-8 -*-
# @Author: Ming
# @Date: 2020-05-24 10:58:40
# @Last Modified by: Ming
# @Last Modified time: 2020-05-24 15:57:37
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
import click
import logging
#### Some Functions ####
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s')
__version__ = '1.0.0'
sns.set(style='white')
sns.set_palette("Set3")
def get_first_level_pos(df):
"""
"""
res = {}
pos = 0
info_l1 = set()
info_l2 = set()
for l1, l2 in df.index.values:
if l1 not in info_l1 and l2 not in info_l2:
if len(res) == 0:
pass
else:
pos += 1
start = pos
res[l1] = [start, start]
info_l1.add(l1)
info_l2.add(l2)
elif l1 in info_l1 and l2 not in info_l2:
pos += 1
end = pos
res[l1][1] = end
info_l2.add(l2)
else:
pass
return res
def plot_first_level(axis, position, df):
"""
"""
offset_y = 0.1
offset_x = df.max() / 100
for name, position in position.items():
x = max(df.loc[name].max(), df.max() / 2)
axis.plot([x + offset_x, x + offset_x],
[position[0] - offset_y, position[1] + offset_y],
lw=0.5,
color='black')
axis.text(x + offset_x * 2, np.mean(position), name,
horizontalalignment='left',
verticalalignment='center')
########################
CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])
@click.command(context_settings=CONTEXT_SETTINGS)
@click.version_option(version=__version__)
@click.option('-i', '--input',
required=True,
type=click.Path(),
help="The input table two level file to plot")
@click.option('--xname',
required=True,
help="The x column name for the plot")
@click.option('--huename',
default=False,
show_default=True,
help="The hue column name for the plot")
@click.option('--hueorder',
default=None,
required=False,
show_default=True,
help="The order of hue info(sep by ,)")
@click.option('--color',
default=None,
required=False,
show_default=True,
help="The order of colors(sep by ,)")
@click.option('-p', '--prefix',
default='./result',
show_default=True,
help="The out put preifx")
def cli(input, xname, huename, hueorder, color, prefix):
"""
Two level plot with matplotlib
The first two column must be level1 and level2 index
"""
hue_order = hueorder.strip().split(',') if hueorder else None
sample_color = color.strip().split(',') if color else None
logging.info(f'Reading input file {input}...')
df = pd.read_csv(input, sep='\t', index_col=[0, 1])
df.sort_index(inplace=True)
logging.info(f'Start to draw...')
figure, axis = plt.subplots(figsize=(4, 12))
if huename:
sns.barplot(data=df, x=xname, y=[i[1] for i in df.index.values],
hue=huename, hue_order=hue_order,
palette=sample_color, ax=axis)
else:
sns.barplot(data=df, x=xname, y=[i[1] for i in df.index.values],
palette=sample_color, ax=axis)
# first level positon info
positon_levle1 = get_first_level_pos(df)
plot_first_level(axis, positon_levle1, df[xname])
# 边框设置
axis.spines['right'].set_color("none")
axis.spines['top'].set_color("none")
# legend设置
plt.legend(bbox_to_anchor=(1.05, 0.5), loc=6, borderaxespad=0.)
plt.savefig(f'{prefix}.svg', bbox_inches='tight')
plt.savefig(f'{prefix}.png', dpi=300, bbox_inches='tight')
if __name__ == "__main__":
cli()