-
Notifications
You must be signed in to change notification settings - Fork 0
/
PanGenomeDataPlot.py
91 lines (88 loc) · 3.89 KB
/
PanGenomeDataPlot.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
# -*- coding: utf-8 -*-
import matplotlib
matplotlib.use("Agg")
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import spline
import argparse
import sys
def options( ):
parser = argparse.ArgumentParser(description="Generates Line plots with shaded area for the data generated by Pangenome tool",formatter_class=argparse.ArgumentDefaultsHel
pFormatter)
parser.add_argument("--rtab",dest="rtab",help="Input Rtab file",type=file)
parser.add_argument("--figname",dest="figname",help="Output figure file name",type=str,default="PrettyFig")
parser.add_argument("--title",dest="title",help="title of the figure",type=str,default="")
parser.add_argument("--width",dest="width",help="width of figure in inch",type=int,default=16)
parser.add_argument("--height",dest="height",help="height of figure in inch",type=int,default=9)
parser.add_argument("--dpi",dest="dpi",help="DPI",type=int,default=300)
parser.add_argument("--xlabel",dest="xlabel",help="X axis label",type=str,default="")
parser.add_argument("--ylabel",dest="ylabel",help="Y axis label",type=str,default="")
parser.add_argument("--font",dest="font",help="Font type",type=str,default="normal")
parser.add_argument("--fsize",dest="fsize",help="Font size",type=int,default=12)
parser.add_argument("--figformat",dest="figformat",type=str,default="png")
return parser.parse_args( )
def fig_plot(inputs):
font = {'family' : inputs["font"],
'weight' : 'bold',
'size' : inputs["fsize"]}
plt.rc(font)
plt.figure(figsize=(inputs["width"],inputs["height"]),dpi=inputs["dpi"])
data2 = pd.read_csv(inputs["rtab"],sep="\t",header=None)
bx = []
means = []
medians = []
lower_quartilles = []
upper_quartilles = []
lower_whiskers = []
upper_whiskers = []
for j in data2.columns.tolist():
tdt = np.array(data2[j])
median = np.median(tdt)
medians.append(median)
mean= np.mean(tdt)
means.append(mean)
lower_quartille = np.percentile(tdt,25)
lower_quartilles.append(lower_quartille)
upper_quartille = np.percentile(tdt,75)
upper_quartilles.append(upper_quartille)
iqr = upper_quartille-lower_quartille
lower_whisker = tdt[tdt>=lower_quartille-1.5*iqr].min()
lower_whiskers.append(lower_whisker)
upper_whisker = tdt[tdt<=upper_quartille+1.5*iqr].max()
upper_whiskers.append(upper_whisker)
x = np.arange(1,len(upper_whiskers)+1)
xl = np.linspace(x.min(),x.max(),len(x))
smooth = 1
if smooth:
medians = spline(x,medians,xl)
means = spline(x,means,xl)
upper_quartilles = spline(x,upper_quartilles,xl)
lower_quartilles = spline(x,lower_quartilles,xl)
upper_whiskers = spline(x,upper_whiskers,xl)
lower_whiskers = spline(x,lower_whiskers,xl)
plt.figure(figsize=(inputs["width"],inputs["height"]),dpi=inputs["dpi"])
plt.fill_between(xl,np.array(lower_whiskers),np.array(upper_whiskers),facecolor="orange",alpha=0.3,lw=0)
plt.fill_between(xl,np.array(lower_quartilles),np.array(upper_quartilles),facecolor="orange",alpha=0.3,lw=0)
plt.plot(xl,means,c='orange',alpha=1)
plt.plot(xl,medians,c='blue',alpha=1)
plt.xlabel(inputs["xlabel"])
plt.ylabel(inputs["ylabel"])
plt.title(inputs["title"])
plt.savefig("%s.%s"%(inputs["figname"],inputs["figformat"]),bbox_inches="tight")
if __name__=='__main__':
args = options( )
inputs = {
"rtab":args.rtab,
'figname':args.figname,
'title':args.title,
'width':args.width,
'height':args.height,
'dpi':args.dpi,
'xlabel':args.xlabel,
'ylabel':args.ylabel,
'font':args.font,
'fsize':args.fsize,
"figformat":args.figformat
}
fig_plot(inputs)